Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1 | #include "callchain.h" |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 2 | #include "debug.h" |
| 3 | #include "event.h" |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 4 | #include "evsel.h" |
| 5 | #include "hist.h" |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 6 | #include "machine.h" |
| 7 | #include "map.h" |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 8 | #include "sort.h" |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 9 | #include "strlist.h" |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 10 | #include "thread.h" |
| 11 | #include <stdbool.h> |
Arnaldo Carvalho de Melo | c506c96 | 2013-12-11 09:15:00 -0300 | [diff] [blame] | 12 | #include <symbol/kallsyms.h> |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 13 | #include "unwind.h" |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 14 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 15 | int machine__init(struct machine *machine, const char *root_dir, pid_t pid) |
| 16 | { |
| 17 | map_groups__init(&machine->kmaps); |
| 18 | RB_CLEAR_NODE(&machine->rb_node); |
| 19 | INIT_LIST_HEAD(&machine->user_dsos); |
| 20 | INIT_LIST_HEAD(&machine->kernel_dsos); |
| 21 | |
| 22 | machine->threads = RB_ROOT; |
| 23 | INIT_LIST_HEAD(&machine->dead_threads); |
| 24 | machine->last_match = NULL; |
| 25 | |
| 26 | machine->kmaps.machine = machine; |
| 27 | machine->pid = pid; |
| 28 | |
Adrian Hunter | 611a5ce | 2013-08-08 14:32:20 +0300 | [diff] [blame] | 29 | machine->symbol_filter = NULL; |
Jiri Olsa | 14bd6d2 | 2014-01-07 13:47:19 +0100 | [diff] [blame] | 30 | machine->id_hdr_size = 0; |
Adrian Hunter | 611a5ce | 2013-08-08 14:32:20 +0300 | [diff] [blame] | 31 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 32 | machine->root_dir = strdup(root_dir); |
| 33 | if (machine->root_dir == NULL) |
| 34 | return -ENOMEM; |
| 35 | |
| 36 | if (pid != HOST_KERNEL_ID) { |
Adrian Hunter | 1fcb876 | 2014-07-14 13:02:25 +0300 | [diff] [blame] | 37 | struct thread *thread = machine__findnew_thread(machine, -1, |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 38 | pid); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 39 | char comm[64]; |
| 40 | |
| 41 | if (thread == NULL) |
| 42 | return -ENOMEM; |
| 43 | |
| 44 | snprintf(comm, sizeof(comm), "[guest/%d]", pid); |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 45 | thread__set_comm(thread, comm, 0); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 46 | } |
| 47 | |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 48 | machine->current_tid = NULL; |
| 49 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | |
David Ahern | 8fb598e | 2013-09-28 13:13:00 -0600 | [diff] [blame] | 53 | struct machine *machine__new_host(void) |
| 54 | { |
| 55 | struct machine *machine = malloc(sizeof(*machine)); |
| 56 | |
| 57 | if (machine != NULL) { |
| 58 | machine__init(machine, "", HOST_KERNEL_ID); |
| 59 | |
| 60 | if (machine__create_kernel_maps(machine) < 0) |
| 61 | goto out_delete; |
| 62 | } |
| 63 | |
| 64 | return machine; |
| 65 | out_delete: |
| 66 | free(machine); |
| 67 | return NULL; |
| 68 | } |
| 69 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 70 | static void dsos__delete(struct list_head *dsos) |
| 71 | { |
| 72 | struct dso *pos, *n; |
| 73 | |
| 74 | list_for_each_entry_safe(pos, n, dsos, node) { |
| 75 | list_del(&pos->node); |
| 76 | dso__delete(pos); |
| 77 | } |
| 78 | } |
| 79 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 80 | void machine__delete_dead_threads(struct machine *machine) |
| 81 | { |
| 82 | struct thread *n, *t; |
| 83 | |
| 84 | list_for_each_entry_safe(t, n, &machine->dead_threads, node) { |
| 85 | list_del(&t->node); |
| 86 | thread__delete(t); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void machine__delete_threads(struct machine *machine) |
| 91 | { |
| 92 | struct rb_node *nd = rb_first(&machine->threads); |
| 93 | |
| 94 | while (nd) { |
| 95 | struct thread *t = rb_entry(nd, struct thread, rb_node); |
| 96 | |
| 97 | rb_erase(&t->rb_node, &machine->threads); |
| 98 | nd = rb_next(nd); |
| 99 | thread__delete(t); |
| 100 | } |
| 101 | } |
| 102 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 103 | void machine__exit(struct machine *machine) |
| 104 | { |
| 105 | map_groups__exit(&machine->kmaps); |
| 106 | dsos__delete(&machine->user_dsos); |
| 107 | dsos__delete(&machine->kernel_dsos); |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 108 | zfree(&machine->root_dir); |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 109 | zfree(&machine->current_tid); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void machine__delete(struct machine *machine) |
| 113 | { |
| 114 | machine__exit(machine); |
| 115 | free(machine); |
| 116 | } |
| 117 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 118 | void machines__init(struct machines *machines) |
| 119 | { |
| 120 | machine__init(&machines->host, "", HOST_KERNEL_ID); |
| 121 | machines->guests = RB_ROOT; |
Adrian Hunter | 611a5ce | 2013-08-08 14:32:20 +0300 | [diff] [blame] | 122 | machines->symbol_filter = NULL; |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void machines__exit(struct machines *machines) |
| 126 | { |
| 127 | machine__exit(&machines->host); |
| 128 | /* XXX exit guest */ |
| 129 | } |
| 130 | |
| 131 | struct machine *machines__add(struct machines *machines, pid_t pid, |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 132 | const char *root_dir) |
| 133 | { |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 134 | struct rb_node **p = &machines->guests.rb_node; |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 135 | struct rb_node *parent = NULL; |
| 136 | struct machine *pos, *machine = malloc(sizeof(*machine)); |
| 137 | |
| 138 | if (machine == NULL) |
| 139 | return NULL; |
| 140 | |
| 141 | if (machine__init(machine, root_dir, pid) != 0) { |
| 142 | free(machine); |
| 143 | return NULL; |
| 144 | } |
| 145 | |
Adrian Hunter | 611a5ce | 2013-08-08 14:32:20 +0300 | [diff] [blame] | 146 | machine->symbol_filter = machines->symbol_filter; |
| 147 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 148 | while (*p != NULL) { |
| 149 | parent = *p; |
| 150 | pos = rb_entry(parent, struct machine, rb_node); |
| 151 | if (pid < pos->pid) |
| 152 | p = &(*p)->rb_left; |
| 153 | else |
| 154 | p = &(*p)->rb_right; |
| 155 | } |
| 156 | |
| 157 | rb_link_node(&machine->rb_node, parent, p); |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 158 | rb_insert_color(&machine->rb_node, &machines->guests); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 159 | |
| 160 | return machine; |
| 161 | } |
| 162 | |
Adrian Hunter | 611a5ce | 2013-08-08 14:32:20 +0300 | [diff] [blame] | 163 | void machines__set_symbol_filter(struct machines *machines, |
| 164 | symbol_filter_t symbol_filter) |
| 165 | { |
| 166 | struct rb_node *nd; |
| 167 | |
| 168 | machines->symbol_filter = symbol_filter; |
| 169 | machines->host.symbol_filter = symbol_filter; |
| 170 | |
| 171 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { |
| 172 | struct machine *machine = rb_entry(nd, struct machine, rb_node); |
| 173 | |
| 174 | machine->symbol_filter = symbol_filter; |
| 175 | } |
| 176 | } |
| 177 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 178 | struct machine *machines__find(struct machines *machines, pid_t pid) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 179 | { |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 180 | struct rb_node **p = &machines->guests.rb_node; |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 181 | struct rb_node *parent = NULL; |
| 182 | struct machine *machine; |
| 183 | struct machine *default_machine = NULL; |
| 184 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 185 | if (pid == HOST_KERNEL_ID) |
| 186 | return &machines->host; |
| 187 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 188 | while (*p != NULL) { |
| 189 | parent = *p; |
| 190 | machine = rb_entry(parent, struct machine, rb_node); |
| 191 | if (pid < machine->pid) |
| 192 | p = &(*p)->rb_left; |
| 193 | else if (pid > machine->pid) |
| 194 | p = &(*p)->rb_right; |
| 195 | else |
| 196 | return machine; |
| 197 | if (!machine->pid) |
| 198 | default_machine = machine; |
| 199 | } |
| 200 | |
| 201 | return default_machine; |
| 202 | } |
| 203 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 204 | struct machine *machines__findnew(struct machines *machines, pid_t pid) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 205 | { |
| 206 | char path[PATH_MAX]; |
| 207 | const char *root_dir = ""; |
| 208 | struct machine *machine = machines__find(machines, pid); |
| 209 | |
| 210 | if (machine && (machine->pid == pid)) |
| 211 | goto out; |
| 212 | |
| 213 | if ((pid != HOST_KERNEL_ID) && |
| 214 | (pid != DEFAULT_GUEST_KERNEL_ID) && |
| 215 | (symbol_conf.guestmount)) { |
| 216 | sprintf(path, "%s/%d", symbol_conf.guestmount, pid); |
| 217 | if (access(path, R_OK)) { |
| 218 | static struct strlist *seen; |
| 219 | |
| 220 | if (!seen) |
| 221 | seen = strlist__new(true, NULL); |
| 222 | |
| 223 | if (!strlist__has_entry(seen, path)) { |
| 224 | pr_err("Can't access file %s\n", path); |
| 225 | strlist__add(seen, path); |
| 226 | } |
| 227 | machine = NULL; |
| 228 | goto out; |
| 229 | } |
| 230 | root_dir = path; |
| 231 | } |
| 232 | |
| 233 | machine = machines__add(machines, pid, root_dir); |
| 234 | out: |
| 235 | return machine; |
| 236 | } |
| 237 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 238 | void machines__process_guests(struct machines *machines, |
| 239 | machine__process_t process, void *data) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 240 | { |
| 241 | struct rb_node *nd; |
| 242 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 243 | 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] | 244 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 245 | process(pos, data); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | char *machine__mmap_name(struct machine *machine, char *bf, size_t size) |
| 250 | { |
| 251 | if (machine__is_host(machine)) |
| 252 | snprintf(bf, size, "[%s]", "kernel.kallsyms"); |
| 253 | else if (machine__is_default_guest(machine)) |
| 254 | snprintf(bf, size, "[%s]", "guest.kernel.kallsyms"); |
| 255 | else { |
| 256 | snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms", |
| 257 | machine->pid); |
| 258 | } |
| 259 | |
| 260 | return bf; |
| 261 | } |
| 262 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 263 | 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] | 264 | { |
| 265 | struct rb_node *node; |
| 266 | struct machine *machine; |
| 267 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 268 | machines->host.id_hdr_size = id_hdr_size; |
| 269 | |
| 270 | 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] | 271 | machine = rb_entry(node, struct machine, rb_node); |
| 272 | machine->id_hdr_size = id_hdr_size; |
| 273 | } |
| 274 | |
| 275 | return; |
| 276 | } |
| 277 | |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 278 | static void machine__update_thread_pid(struct machine *machine, |
| 279 | struct thread *th, pid_t pid) |
| 280 | { |
| 281 | struct thread *leader; |
| 282 | |
| 283 | if (pid == th->pid_ || pid == -1 || th->pid_ != -1) |
| 284 | return; |
| 285 | |
| 286 | th->pid_ = pid; |
| 287 | |
| 288 | if (th->pid_ == th->tid) |
| 289 | return; |
| 290 | |
| 291 | leader = machine__findnew_thread(machine, th->pid_, th->pid_); |
| 292 | if (!leader) |
| 293 | goto out_err; |
| 294 | |
| 295 | if (!leader->mg) |
| 296 | leader->mg = map_groups__new(); |
| 297 | |
| 298 | if (!leader->mg) |
| 299 | goto out_err; |
| 300 | |
| 301 | if (th->mg == leader->mg) |
| 302 | return; |
| 303 | |
| 304 | if (th->mg) { |
| 305 | /* |
| 306 | * Maps are created from MMAP events which provide the pid and |
| 307 | * tid. Consequently there never should be any maps on a thread |
| 308 | * with an unknown pid. Just print an error if there are. |
| 309 | */ |
| 310 | if (!map_groups__empty(th->mg)) |
| 311 | pr_err("Discarding thread maps for %d:%d\n", |
| 312 | th->pid_, th->tid); |
| 313 | map_groups__delete(th->mg); |
| 314 | } |
| 315 | |
| 316 | th->mg = map_groups__get(leader->mg); |
| 317 | |
| 318 | return; |
| 319 | |
| 320 | out_err: |
| 321 | pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid); |
| 322 | } |
| 323 | |
Adrian Hunter | 99d725f | 2013-08-26 16:00:19 +0300 | [diff] [blame] | 324 | static struct thread *__machine__findnew_thread(struct machine *machine, |
| 325 | pid_t pid, pid_t tid, |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 326 | bool create) |
| 327 | { |
| 328 | struct rb_node **p = &machine->threads.rb_node; |
| 329 | struct rb_node *parent = NULL; |
| 330 | struct thread *th; |
| 331 | |
| 332 | /* |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 333 | * Front-end cache - TID lookups come in blocks, |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 334 | * so most of the time we dont have to look up |
| 335 | * the full rbtree: |
| 336 | */ |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 337 | th = machine->last_match; |
| 338 | if (th && th->tid == tid) { |
| 339 | machine__update_thread_pid(machine, th, pid); |
| 340 | return th; |
Adrian Hunter | 99d725f | 2013-08-26 16:00:19 +0300 | [diff] [blame] | 341 | } |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 342 | |
| 343 | while (*p != NULL) { |
| 344 | parent = *p; |
| 345 | th = rb_entry(parent, struct thread, rb_node); |
| 346 | |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 347 | if (th->tid == tid) { |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 348 | machine->last_match = th; |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 349 | machine__update_thread_pid(machine, th, pid); |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 350 | return th; |
| 351 | } |
| 352 | |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 353 | if (tid < th->tid) |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 354 | p = &(*p)->rb_left; |
| 355 | else |
| 356 | p = &(*p)->rb_right; |
| 357 | } |
| 358 | |
| 359 | if (!create) |
| 360 | return NULL; |
| 361 | |
Adrian Hunter | 99d725f | 2013-08-26 16:00:19 +0300 | [diff] [blame] | 362 | th = thread__new(pid, tid); |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 363 | if (th != NULL) { |
| 364 | rb_link_node(&th->rb_node, parent, p); |
| 365 | rb_insert_color(&th->rb_node, &machine->threads); |
| 366 | machine->last_match = th; |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 367 | |
| 368 | /* |
| 369 | * We have to initialize map_groups separately |
| 370 | * after rb tree is updated. |
| 371 | * |
| 372 | * The reason is that we call machine__findnew_thread |
| 373 | * within thread__init_map_groups to find the thread |
| 374 | * leader and that would screwed the rb tree. |
| 375 | */ |
Adrian Hunter | 418029b | 2014-07-16 10:19:44 +0300 | [diff] [blame] | 376 | if (thread__init_map_groups(th, machine)) { |
| 377 | thread__delete(th); |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 378 | return NULL; |
Adrian Hunter | 418029b | 2014-07-16 10:19:44 +0300 | [diff] [blame] | 379 | } |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | return th; |
| 383 | } |
| 384 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 385 | struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, |
| 386 | pid_t tid) |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 387 | { |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 388 | return __machine__findnew_thread(machine, pid, tid, true); |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 389 | } |
| 390 | |
Jiri Olsa | d75e609 | 2014-03-14 15:00:03 +0100 | [diff] [blame] | 391 | struct thread *machine__find_thread(struct machine *machine, pid_t pid, |
| 392 | pid_t tid) |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 393 | { |
Jiri Olsa | d75e609 | 2014-03-14 15:00:03 +0100 | [diff] [blame] | 394 | return __machine__findnew_thread(machine, pid, tid, false); |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 395 | } |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 396 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 397 | int machine__process_comm_event(struct machine *machine, union perf_event *event, |
| 398 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 399 | { |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 400 | struct thread *thread = machine__findnew_thread(machine, |
| 401 | event->comm.pid, |
| 402 | event->comm.tid); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 403 | |
| 404 | if (dump_trace) |
| 405 | perf_event__fprintf_comm(event, stdout); |
| 406 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 407 | if (thread == NULL || thread__set_comm(thread, event->comm.comm, sample->time)) { |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 408 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); |
| 409 | return -1; |
| 410 | } |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | int machine__process_lost_event(struct machine *machine __maybe_unused, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 416 | union perf_event *event, struct perf_sample *sample __maybe_unused) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 417 | { |
| 418 | dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n", |
| 419 | event->lost.id, event->lost.lost); |
| 420 | return 0; |
| 421 | } |
| 422 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 423 | struct map *machine__new_module(struct machine *machine, u64 start, |
| 424 | const char *filename) |
| 425 | { |
| 426 | struct map *map; |
| 427 | struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename); |
| 428 | |
| 429 | if (dso == NULL) |
| 430 | return NULL; |
| 431 | |
| 432 | map = map__new2(start, dso, MAP__FUNCTION); |
| 433 | if (map == NULL) |
| 434 | return NULL; |
| 435 | |
| 436 | if (machine__is_host(machine)) |
| 437 | dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE; |
| 438 | else |
| 439 | dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE; |
| 440 | map_groups__insert(&machine->kmaps, map); |
| 441 | return map; |
| 442 | } |
| 443 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 444 | size_t machines__fprintf_dsos(struct machines *machines, FILE *fp) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 445 | { |
| 446 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 447 | size_t ret = __dsos__fprintf(&machines->host.kernel_dsos, fp) + |
| 448 | __dsos__fprintf(&machines->host.user_dsos, fp); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 449 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 450 | 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] | 451 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 452 | ret += __dsos__fprintf(&pos->kernel_dsos, fp); |
| 453 | ret += __dsos__fprintf(&pos->user_dsos, fp); |
| 454 | } |
| 455 | |
| 456 | return ret; |
| 457 | } |
| 458 | |
| 459 | size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp, |
| 460 | bool (skip)(struct dso *dso, int parm), int parm) |
| 461 | { |
| 462 | return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, skip, parm) + |
| 463 | __dsos__fprintf_buildid(&machine->user_dsos, fp, skip, parm); |
| 464 | } |
| 465 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 466 | 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] | 467 | bool (skip)(struct dso *dso, int parm), int parm) |
| 468 | { |
| 469 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 470 | 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] | 471 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 472 | 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] | 473 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 474 | ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm); |
| 475 | } |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp) |
| 480 | { |
| 481 | int i; |
| 482 | size_t printed = 0; |
| 483 | struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso; |
| 484 | |
| 485 | if (kdso->has_build_id) { |
| 486 | char filename[PATH_MAX]; |
| 487 | if (dso__build_id_filename(kdso, filename, sizeof(filename))) |
| 488 | printed += fprintf(fp, "[0] %s\n", filename); |
| 489 | } |
| 490 | |
| 491 | for (i = 0; i < vmlinux_path__nr_entries; ++i) |
| 492 | printed += fprintf(fp, "[%d] %s\n", |
| 493 | i + kdso->has_build_id, vmlinux_path[i]); |
| 494 | |
| 495 | return printed; |
| 496 | } |
| 497 | |
| 498 | size_t machine__fprintf(struct machine *machine, FILE *fp) |
| 499 | { |
| 500 | size_t ret = 0; |
| 501 | struct rb_node *nd; |
| 502 | |
| 503 | for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) { |
| 504 | struct thread *pos = rb_entry(nd, struct thread, rb_node); |
| 505 | |
| 506 | ret += thread__fprintf(pos, fp); |
| 507 | } |
| 508 | |
| 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | static struct dso *machine__get_kernel(struct machine *machine) |
| 513 | { |
| 514 | const char *vmlinux_name = NULL; |
| 515 | struct dso *kernel; |
| 516 | |
| 517 | if (machine__is_host(machine)) { |
| 518 | vmlinux_name = symbol_conf.vmlinux_name; |
| 519 | if (!vmlinux_name) |
| 520 | vmlinux_name = "[kernel.kallsyms]"; |
| 521 | |
| 522 | kernel = dso__kernel_findnew(machine, vmlinux_name, |
| 523 | "[kernel]", |
| 524 | DSO_TYPE_KERNEL); |
| 525 | } else { |
| 526 | char bf[PATH_MAX]; |
| 527 | |
| 528 | if (machine__is_default_guest(machine)) |
| 529 | vmlinux_name = symbol_conf.default_guest_vmlinux_name; |
| 530 | if (!vmlinux_name) |
| 531 | vmlinux_name = machine__mmap_name(machine, bf, |
| 532 | sizeof(bf)); |
| 533 | |
| 534 | kernel = dso__kernel_findnew(machine, vmlinux_name, |
| 535 | "[guest.kernel]", |
| 536 | DSO_TYPE_GUEST_KERNEL); |
| 537 | } |
| 538 | |
| 539 | if (kernel != NULL && (!kernel->has_build_id)) |
| 540 | dso__read_running_kernel_build_id(kernel, machine); |
| 541 | |
| 542 | return kernel; |
| 543 | } |
| 544 | |
| 545 | struct process_args { |
| 546 | u64 start; |
| 547 | }; |
| 548 | |
Adrian Hunter | 15a0a87 | 2014-01-29 16:14:38 +0200 | [diff] [blame] | 549 | static void machine__get_kallsyms_filename(struct machine *machine, char *buf, |
| 550 | size_t bufsz) |
| 551 | { |
| 552 | if (machine__is_default_guest(machine)) |
| 553 | scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms); |
| 554 | else |
| 555 | scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir); |
| 556 | } |
| 557 | |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 558 | const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL}; |
| 559 | |
| 560 | /* Figure out the start address of kernel map from /proc/kallsyms. |
| 561 | * Returns the name of the start symbol in *symbol_name. Pass in NULL as |
| 562 | * symbol_name if it's not that important. |
| 563 | */ |
| 564 | static u64 machine__get_kernel_start_addr(struct machine *machine, |
| 565 | const char **symbol_name) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 566 | { |
Adrian Hunter | 15a0a87 | 2014-01-29 16:14:38 +0200 | [diff] [blame] | 567 | char filename[PATH_MAX]; |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 568 | int i; |
| 569 | const char *name; |
| 570 | u64 addr = 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 571 | |
Adrian Hunter | 15a0a87 | 2014-01-29 16:14:38 +0200 | [diff] [blame] | 572 | machine__get_kallsyms_filename(machine, filename, PATH_MAX); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 573 | |
| 574 | if (symbol__restricted_filename(filename, "/proc/kallsyms")) |
| 575 | return 0; |
| 576 | |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 577 | for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) { |
| 578 | addr = kallsyms__get_function_start(filename, name); |
| 579 | if (addr) |
| 580 | break; |
| 581 | } |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 582 | |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 583 | if (symbol_name) |
| 584 | *symbol_name = name; |
| 585 | |
| 586 | return addr; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel) |
| 590 | { |
| 591 | enum map_type type; |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 592 | u64 start = machine__get_kernel_start_addr(machine, NULL); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 593 | |
| 594 | for (type = 0; type < MAP__NR_TYPES; ++type) { |
| 595 | struct kmap *kmap; |
| 596 | |
| 597 | machine->vmlinux_maps[type] = map__new2(start, kernel, type); |
| 598 | if (machine->vmlinux_maps[type] == NULL) |
| 599 | return -1; |
| 600 | |
| 601 | machine->vmlinux_maps[type]->map_ip = |
| 602 | machine->vmlinux_maps[type]->unmap_ip = |
| 603 | identity__map_ip; |
| 604 | kmap = map__kmap(machine->vmlinux_maps[type]); |
| 605 | kmap->kmaps = &machine->kmaps; |
| 606 | map_groups__insert(&machine->kmaps, |
| 607 | machine->vmlinux_maps[type]); |
| 608 | } |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | void machine__destroy_kernel_maps(struct machine *machine) |
| 614 | { |
| 615 | enum map_type type; |
| 616 | |
| 617 | for (type = 0; type < MAP__NR_TYPES; ++type) { |
| 618 | struct kmap *kmap; |
| 619 | |
| 620 | if (machine->vmlinux_maps[type] == NULL) |
| 621 | continue; |
| 622 | |
| 623 | kmap = map__kmap(machine->vmlinux_maps[type]); |
| 624 | map_groups__remove(&machine->kmaps, |
| 625 | machine->vmlinux_maps[type]); |
| 626 | if (kmap->ref_reloc_sym) { |
| 627 | /* |
| 628 | * ref_reloc_sym is shared among all maps, so free just |
| 629 | * on one of them. |
| 630 | */ |
| 631 | if (type == MAP__FUNCTION) { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 632 | zfree((char **)&kmap->ref_reloc_sym->name); |
| 633 | zfree(&kmap->ref_reloc_sym); |
| 634 | } else |
| 635 | kmap->ref_reloc_sym = NULL; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | map__delete(machine->vmlinux_maps[type]); |
| 639 | machine->vmlinux_maps[type] = NULL; |
| 640 | } |
| 641 | } |
| 642 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 643 | int machines__create_guest_kernel_maps(struct machines *machines) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 644 | { |
| 645 | int ret = 0; |
| 646 | struct dirent **namelist = NULL; |
| 647 | int i, items = 0; |
| 648 | char path[PATH_MAX]; |
| 649 | pid_t pid; |
| 650 | char *endp; |
| 651 | |
| 652 | if (symbol_conf.default_guest_vmlinux_name || |
| 653 | symbol_conf.default_guest_modules || |
| 654 | symbol_conf.default_guest_kallsyms) { |
| 655 | machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID); |
| 656 | } |
| 657 | |
| 658 | if (symbol_conf.guestmount) { |
| 659 | items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL); |
| 660 | if (items <= 0) |
| 661 | return -ENOENT; |
| 662 | for (i = 0; i < items; i++) { |
| 663 | if (!isdigit(namelist[i]->d_name[0])) { |
| 664 | /* Filter out . and .. */ |
| 665 | continue; |
| 666 | } |
| 667 | pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10); |
| 668 | if ((*endp != '\0') || |
| 669 | (endp == namelist[i]->d_name) || |
| 670 | (errno == ERANGE)) { |
| 671 | pr_debug("invalid directory (%s). Skipping.\n", |
| 672 | namelist[i]->d_name); |
| 673 | continue; |
| 674 | } |
| 675 | sprintf(path, "%s/%s/proc/kallsyms", |
| 676 | symbol_conf.guestmount, |
| 677 | namelist[i]->d_name); |
| 678 | ret = access(path, R_OK); |
| 679 | if (ret) { |
| 680 | pr_debug("Can't access file %s\n", path); |
| 681 | goto failure; |
| 682 | } |
| 683 | machines__create_kernel_maps(machines, pid); |
| 684 | } |
| 685 | failure: |
| 686 | free(namelist); |
| 687 | } |
| 688 | |
| 689 | return ret; |
| 690 | } |
| 691 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 692 | void machines__destroy_kernel_maps(struct machines *machines) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 693 | { |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 694 | struct rb_node *next = rb_first(&machines->guests); |
| 695 | |
| 696 | machine__destroy_kernel_maps(&machines->host); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 697 | |
| 698 | while (next) { |
| 699 | struct machine *pos = rb_entry(next, struct machine, rb_node); |
| 700 | |
| 701 | next = rb_next(&pos->rb_node); |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 702 | rb_erase(&pos->rb_node, &machines->guests); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 703 | machine__delete(pos); |
| 704 | } |
| 705 | } |
| 706 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 707 | 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] | 708 | { |
| 709 | struct machine *machine = machines__findnew(machines, pid); |
| 710 | |
| 711 | if (machine == NULL) |
| 712 | return -1; |
| 713 | |
| 714 | return machine__create_kernel_maps(machine); |
| 715 | } |
| 716 | |
| 717 | int machine__load_kallsyms(struct machine *machine, const char *filename, |
| 718 | enum map_type type, symbol_filter_t filter) |
| 719 | { |
| 720 | struct map *map = machine->vmlinux_maps[type]; |
| 721 | int ret = dso__load_kallsyms(map->dso, filename, map, filter); |
| 722 | |
| 723 | if (ret > 0) { |
| 724 | dso__set_loaded(map->dso, type); |
| 725 | /* |
| 726 | * Since /proc/kallsyms will have multiple sessions for the |
| 727 | * kernel, with modules between them, fixup the end of all |
| 728 | * sections. |
| 729 | */ |
| 730 | __map_groups__fixup_end(&machine->kmaps, type); |
| 731 | } |
| 732 | |
| 733 | return ret; |
| 734 | } |
| 735 | |
| 736 | int machine__load_vmlinux_path(struct machine *machine, enum map_type type, |
| 737 | symbol_filter_t filter) |
| 738 | { |
| 739 | struct map *map = machine->vmlinux_maps[type]; |
| 740 | int ret = dso__load_vmlinux_path(map->dso, map, filter); |
| 741 | |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 742 | if (ret > 0) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 743 | dso__set_loaded(map->dso, type); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 744 | |
| 745 | return ret; |
| 746 | } |
| 747 | |
| 748 | static void map_groups__fixup_end(struct map_groups *mg) |
| 749 | { |
| 750 | int i; |
| 751 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 752 | __map_groups__fixup_end(mg, i); |
| 753 | } |
| 754 | |
| 755 | static char *get_kernel_version(const char *root_dir) |
| 756 | { |
| 757 | char version[PATH_MAX]; |
| 758 | FILE *file; |
| 759 | char *name, *tmp; |
| 760 | const char *prefix = "Linux version "; |
| 761 | |
| 762 | sprintf(version, "%s/proc/version", root_dir); |
| 763 | file = fopen(version, "r"); |
| 764 | if (!file) |
| 765 | return NULL; |
| 766 | |
| 767 | version[0] = '\0'; |
| 768 | tmp = fgets(version, sizeof(version), file); |
| 769 | fclose(file); |
| 770 | |
| 771 | name = strstr(version, prefix); |
| 772 | if (!name) |
| 773 | return NULL; |
| 774 | name += strlen(prefix); |
| 775 | tmp = strchr(name, ' '); |
| 776 | if (tmp) |
| 777 | *tmp = '\0'; |
| 778 | |
| 779 | return strdup(name); |
| 780 | } |
| 781 | |
| 782 | static int map_groups__set_modules_path_dir(struct map_groups *mg, |
Richard Yao | 61d4290 | 2014-04-26 13:17:55 -0400 | [diff] [blame] | 783 | const char *dir_name, int depth) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 784 | { |
| 785 | struct dirent *dent; |
| 786 | DIR *dir = opendir(dir_name); |
| 787 | int ret = 0; |
| 788 | |
| 789 | if (!dir) { |
| 790 | pr_debug("%s: cannot open %s dir\n", __func__, dir_name); |
| 791 | return -1; |
| 792 | } |
| 793 | |
| 794 | while ((dent = readdir(dir)) != NULL) { |
| 795 | char path[PATH_MAX]; |
| 796 | struct stat st; |
| 797 | |
| 798 | /*sshfs might return bad dent->d_type, so we have to stat*/ |
| 799 | snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name); |
| 800 | if (stat(path, &st)) |
| 801 | continue; |
| 802 | |
| 803 | if (S_ISDIR(st.st_mode)) { |
| 804 | if (!strcmp(dent->d_name, ".") || |
| 805 | !strcmp(dent->d_name, "..")) |
| 806 | continue; |
| 807 | |
Richard Yao | 61d4290 | 2014-04-26 13:17:55 -0400 | [diff] [blame] | 808 | /* Do not follow top-level source and build symlinks */ |
| 809 | if (depth == 0) { |
| 810 | if (!strcmp(dent->d_name, "source") || |
| 811 | !strcmp(dent->d_name, "build")) |
| 812 | continue; |
| 813 | } |
| 814 | |
| 815 | ret = map_groups__set_modules_path_dir(mg, path, |
| 816 | depth + 1); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 817 | if (ret < 0) |
| 818 | goto out; |
| 819 | } else { |
| 820 | char *dot = strrchr(dent->d_name, '.'), |
| 821 | dso_name[PATH_MAX]; |
| 822 | struct map *map; |
| 823 | char *long_name; |
| 824 | |
| 825 | if (dot == NULL || strcmp(dot, ".ko")) |
| 826 | continue; |
| 827 | snprintf(dso_name, sizeof(dso_name), "[%.*s]", |
| 828 | (int)(dot - dent->d_name), dent->d_name); |
| 829 | |
| 830 | strxfrchar(dso_name, '-', '_'); |
| 831 | map = map_groups__find_by_name(mg, MAP__FUNCTION, |
| 832 | dso_name); |
| 833 | if (map == NULL) |
| 834 | continue; |
| 835 | |
| 836 | long_name = strdup(path); |
| 837 | if (long_name == NULL) { |
| 838 | ret = -1; |
| 839 | goto out; |
| 840 | } |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 841 | dso__set_long_name(map->dso, long_name, true); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 842 | dso__kernel_module_get_build_id(map->dso, ""); |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | out: |
| 847 | closedir(dir); |
| 848 | return ret; |
| 849 | } |
| 850 | |
| 851 | static int machine__set_modules_path(struct machine *machine) |
| 852 | { |
| 853 | char *version; |
| 854 | char modules_path[PATH_MAX]; |
| 855 | |
| 856 | version = get_kernel_version(machine->root_dir); |
| 857 | if (!version) |
| 858 | return -1; |
| 859 | |
Richard Yao | 61d4290 | 2014-04-26 13:17:55 -0400 | [diff] [blame] | 860 | snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s", |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 861 | machine->root_dir, version); |
| 862 | free(version); |
| 863 | |
Richard Yao | 61d4290 | 2014-04-26 13:17:55 -0400 | [diff] [blame] | 864 | 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] | 865 | } |
| 866 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 867 | static int machine__create_module(void *arg, const char *name, u64 start) |
| 868 | { |
| 869 | struct machine *machine = arg; |
| 870 | struct map *map; |
| 871 | |
| 872 | map = machine__new_module(machine, start, name); |
| 873 | if (map == NULL) |
| 874 | return -1; |
| 875 | |
| 876 | dso__kernel_module_get_build_id(map->dso, machine->root_dir); |
| 877 | |
| 878 | return 0; |
| 879 | } |
| 880 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 881 | static int machine__create_modules(struct machine *machine) |
| 882 | { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 883 | const char *modules; |
| 884 | char path[PATH_MAX]; |
| 885 | |
Adrian Hunter | f4be904 | 2013-09-22 13:22:09 +0300 | [diff] [blame] | 886 | if (machine__is_default_guest(machine)) { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 887 | modules = symbol_conf.default_guest_modules; |
Adrian Hunter | f4be904 | 2013-09-22 13:22:09 +0300 | [diff] [blame] | 888 | } else { |
| 889 | snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 890 | modules = path; |
| 891 | } |
| 892 | |
Adrian Hunter | aa7fe3b | 2013-09-22 13:22:09 +0300 | [diff] [blame] | 893 | if (symbol__restricted_filename(modules, "/proc/modules")) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 894 | return -1; |
| 895 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 896 | if (modules__parse(modules, machine, machine__create_module)) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 897 | return -1; |
| 898 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 899 | if (!machine__set_modules_path(machine)) |
| 900 | return 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 901 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 902 | pr_debug("Problems setting modules path maps, continuing anyway...\n"); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 903 | |
Jason Wessel | 8f76fcd | 2013-07-15 15:27:53 -0500 | [diff] [blame] | 904 | return 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | int machine__create_kernel_maps(struct machine *machine) |
| 908 | { |
| 909 | struct dso *kernel = machine__get_kernel(machine); |
Adrian Hunter | 5512cf2 | 2014-01-29 16:14:39 +0200 | [diff] [blame] | 910 | const char *name; |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 911 | u64 addr = machine__get_kernel_start_addr(machine, &name); |
Adrian Hunter | 5512cf2 | 2014-01-29 16:14:39 +0200 | [diff] [blame] | 912 | if (!addr) |
| 913 | return -1; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 914 | |
| 915 | if (kernel == NULL || |
| 916 | __machine__create_kernel_maps(machine, kernel) < 0) |
| 917 | return -1; |
| 918 | |
| 919 | if (symbol_conf.use_modules && machine__create_modules(machine) < 0) { |
| 920 | if (machine__is_host(machine)) |
| 921 | pr_debug("Problems creating module maps, " |
| 922 | "continuing anyway...\n"); |
| 923 | else |
| 924 | pr_debug("Problems creating module maps for guest %d, " |
| 925 | "continuing anyway...\n", machine->pid); |
| 926 | } |
| 927 | |
| 928 | /* |
| 929 | * Now that we have all the maps created, just set the ->end of them: |
| 930 | */ |
| 931 | map_groups__fixup_end(&machine->kmaps); |
Adrian Hunter | 5512cf2 | 2014-01-29 16:14:39 +0200 | [diff] [blame] | 932 | |
| 933 | if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name, |
| 934 | addr)) { |
| 935 | machine__destroy_kernel_maps(machine); |
| 936 | return -1; |
| 937 | } |
| 938 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 939 | return 0; |
| 940 | } |
| 941 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 942 | static void machine__set_kernel_mmap_len(struct machine *machine, |
| 943 | union perf_event *event) |
| 944 | { |
Namhyung Kim | 4552cf0 | 2012-11-07 16:27:10 +0900 | [diff] [blame] | 945 | int i; |
| 946 | |
| 947 | for (i = 0; i < MAP__NR_TYPES; i++) { |
| 948 | machine->vmlinux_maps[i]->start = event->mmap.start; |
| 949 | machine->vmlinux_maps[i]->end = (event->mmap.start + |
| 950 | event->mmap.len); |
| 951 | /* |
| 952 | * Be a bit paranoid here, some perf.data file came with |
| 953 | * a zero sized synthesized MMAP event for the kernel. |
| 954 | */ |
| 955 | if (machine->vmlinux_maps[i]->end == 0) |
| 956 | machine->vmlinux_maps[i]->end = ~0ULL; |
| 957 | } |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 958 | } |
| 959 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 960 | static bool machine__uses_kcore(struct machine *machine) |
| 961 | { |
| 962 | struct dso *dso; |
| 963 | |
| 964 | list_for_each_entry(dso, &machine->kernel_dsos, node) { |
| 965 | if (dso__is_kcore(dso)) |
| 966 | return true; |
| 967 | } |
| 968 | |
| 969 | return false; |
| 970 | } |
| 971 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 972 | static int machine__process_kernel_mmap_event(struct machine *machine, |
| 973 | union perf_event *event) |
| 974 | { |
| 975 | struct map *map; |
| 976 | char kmmap_prefix[PATH_MAX]; |
| 977 | enum dso_kernel_type kernel_type; |
| 978 | bool is_kernel_mmap; |
| 979 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 980 | /* If we have maps from kcore then we do not need or want any others */ |
| 981 | if (machine__uses_kcore(machine)) |
| 982 | return 0; |
| 983 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 984 | machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix)); |
| 985 | if (machine__is_host(machine)) |
| 986 | kernel_type = DSO_TYPE_KERNEL; |
| 987 | else |
| 988 | kernel_type = DSO_TYPE_GUEST_KERNEL; |
| 989 | |
| 990 | is_kernel_mmap = memcmp(event->mmap.filename, |
| 991 | kmmap_prefix, |
| 992 | strlen(kmmap_prefix) - 1) == 0; |
| 993 | if (event->mmap.filename[0] == '/' || |
| 994 | (!is_kernel_mmap && event->mmap.filename[0] == '[')) { |
| 995 | |
| 996 | char short_module_name[1024]; |
| 997 | char *name, *dot; |
| 998 | |
| 999 | if (event->mmap.filename[0] == '/') { |
| 1000 | name = strrchr(event->mmap.filename, '/'); |
| 1001 | if (name == NULL) |
| 1002 | goto out_problem; |
| 1003 | |
| 1004 | ++name; /* skip / */ |
| 1005 | dot = strrchr(name, '.'); |
| 1006 | if (dot == NULL) |
| 1007 | goto out_problem; |
| 1008 | snprintf(short_module_name, sizeof(short_module_name), |
| 1009 | "[%.*s]", (int)(dot - name), name); |
| 1010 | strxfrchar(short_module_name, '-', '_'); |
| 1011 | } else |
| 1012 | strcpy(short_module_name, event->mmap.filename); |
| 1013 | |
| 1014 | map = machine__new_module(machine, event->mmap.start, |
| 1015 | event->mmap.filename); |
| 1016 | if (map == NULL) |
| 1017 | goto out_problem; |
| 1018 | |
| 1019 | name = strdup(short_module_name); |
| 1020 | if (name == NULL) |
| 1021 | goto out_problem; |
| 1022 | |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 1023 | dso__set_short_name(map->dso, name, true); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1024 | map->end = map->start + event->mmap.len; |
| 1025 | } else if (is_kernel_mmap) { |
| 1026 | const char *symbol_name = (event->mmap.filename + |
| 1027 | strlen(kmmap_prefix)); |
| 1028 | /* |
| 1029 | * Should be there already, from the build-id table in |
| 1030 | * the header. |
| 1031 | */ |
| 1032 | struct dso *kernel = __dsos__findnew(&machine->kernel_dsos, |
| 1033 | kmmap_prefix); |
| 1034 | if (kernel == NULL) |
| 1035 | goto out_problem; |
| 1036 | |
| 1037 | kernel->kernel = kernel_type; |
| 1038 | if (__machine__create_kernel_maps(machine, kernel) < 0) |
| 1039 | goto out_problem; |
| 1040 | |
| 1041 | machine__set_kernel_mmap_len(machine, event); |
| 1042 | |
| 1043 | /* |
| 1044 | * Avoid using a zero address (kptr_restrict) for the ref reloc |
| 1045 | * symbol. Effectively having zero here means that at record |
| 1046 | * time /proc/sys/kernel/kptr_restrict was non zero. |
| 1047 | */ |
| 1048 | if (event->mmap.pgoff != 0) { |
| 1049 | maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, |
| 1050 | symbol_name, |
| 1051 | event->mmap.pgoff); |
| 1052 | } |
| 1053 | |
| 1054 | if (machine__is_default_guest(machine)) { |
| 1055 | /* |
| 1056 | * preload dso of guest kernel and modules |
| 1057 | */ |
| 1058 | dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION], |
| 1059 | NULL); |
| 1060 | } |
| 1061 | } |
| 1062 | return 0; |
| 1063 | out_problem: |
| 1064 | return -1; |
| 1065 | } |
| 1066 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1067 | int machine__process_mmap2_event(struct machine *machine, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1068 | union perf_event *event, |
| 1069 | struct perf_sample *sample __maybe_unused) |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1070 | { |
| 1071 | u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 1072 | struct thread *thread; |
| 1073 | struct map *map; |
| 1074 | enum map_type type; |
| 1075 | int ret = 0; |
| 1076 | |
| 1077 | if (dump_trace) |
| 1078 | perf_event__fprintf_mmap2(event, stdout); |
| 1079 | |
| 1080 | if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL || |
| 1081 | cpumode == PERF_RECORD_MISC_KERNEL) { |
| 1082 | ret = machine__process_kernel_mmap_event(machine, event); |
| 1083 | if (ret < 0) |
| 1084 | goto out_problem; |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | thread = machine__findnew_thread(machine, event->mmap2.pid, |
Don Zickus | 11c9abf | 2014-02-26 10:45:27 -0500 | [diff] [blame] | 1089 | event->mmap2.tid); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1090 | if (thread == NULL) |
| 1091 | goto out_problem; |
| 1092 | |
| 1093 | if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) |
| 1094 | type = MAP__VARIABLE; |
| 1095 | else |
| 1096 | type = MAP__FUNCTION; |
| 1097 | |
| 1098 | map = map__new(&machine->user_dsos, event->mmap2.start, |
| 1099 | event->mmap2.len, event->mmap2.pgoff, |
| 1100 | event->mmap2.pid, event->mmap2.maj, |
| 1101 | event->mmap2.min, event->mmap2.ino, |
| 1102 | event->mmap2.ino_generation, |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 1103 | event->mmap2.prot, |
| 1104 | event->mmap2.flags, |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1105 | event->mmap2.filename, type); |
| 1106 | |
| 1107 | if (map == NULL) |
| 1108 | goto out_problem; |
| 1109 | |
| 1110 | thread__insert_map(thread, map); |
| 1111 | return 0; |
| 1112 | |
| 1113 | out_problem: |
| 1114 | dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n"); |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1118 | int machine__process_mmap_event(struct machine *machine, union perf_event *event, |
| 1119 | struct perf_sample *sample __maybe_unused) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1120 | { |
| 1121 | u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 1122 | struct thread *thread; |
| 1123 | struct map *map; |
Stephane Eranian | bad4091 | 2013-01-24 16:10:40 +0100 | [diff] [blame] | 1124 | enum map_type type; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1125 | int ret = 0; |
| 1126 | |
| 1127 | if (dump_trace) |
| 1128 | perf_event__fprintf_mmap(event, stdout); |
| 1129 | |
| 1130 | if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL || |
| 1131 | cpumode == PERF_RECORD_MISC_KERNEL) { |
| 1132 | ret = machine__process_kernel_mmap_event(machine, event); |
| 1133 | if (ret < 0) |
| 1134 | goto out_problem; |
| 1135 | return 0; |
| 1136 | } |
| 1137 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 1138 | thread = machine__findnew_thread(machine, event->mmap.pid, |
Don Zickus | 11c9abf | 2014-02-26 10:45:27 -0500 | [diff] [blame] | 1139 | event->mmap.tid); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1140 | if (thread == NULL) |
| 1141 | goto out_problem; |
Stephane Eranian | bad4091 | 2013-01-24 16:10:40 +0100 | [diff] [blame] | 1142 | |
| 1143 | if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) |
| 1144 | type = MAP__VARIABLE; |
| 1145 | else |
| 1146 | type = MAP__FUNCTION; |
| 1147 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1148 | map = map__new(&machine->user_dsos, event->mmap.start, |
| 1149 | event->mmap.len, event->mmap.pgoff, |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 1150 | event->mmap.pid, 0, 0, 0, 0, 0, 0, |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1151 | event->mmap.filename, |
Stephane Eranian | bad4091 | 2013-01-24 16:10:40 +0100 | [diff] [blame] | 1152 | type); |
| 1153 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1154 | if (map == NULL) |
| 1155 | goto out_problem; |
| 1156 | |
| 1157 | thread__insert_map(thread, map); |
| 1158 | return 0; |
| 1159 | |
| 1160 | out_problem: |
| 1161 | dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n"); |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1165 | static void machine__remove_thread(struct machine *machine, struct thread *th) |
| 1166 | { |
| 1167 | machine->last_match = NULL; |
| 1168 | rb_erase(&th->rb_node, &machine->threads); |
| 1169 | /* |
| 1170 | * We may have references to this thread, for instance in some hist_entry |
| 1171 | * instances, so just move them to a separate list. |
| 1172 | */ |
| 1173 | list_add_tail(&th->node, &machine->dead_threads); |
| 1174 | } |
| 1175 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1176 | int machine__process_fork_event(struct machine *machine, union perf_event *event, |
| 1177 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1178 | { |
Jiri Olsa | d75e609 | 2014-03-14 15:00:03 +0100 | [diff] [blame] | 1179 | struct thread *thread = machine__find_thread(machine, |
| 1180 | event->fork.pid, |
| 1181 | event->fork.tid); |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 1182 | struct thread *parent = machine__findnew_thread(machine, |
| 1183 | event->fork.ppid, |
| 1184 | event->fork.ptid); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1185 | |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1186 | /* if a thread currently exists for the thread id remove it */ |
| 1187 | if (thread != NULL) |
| 1188 | machine__remove_thread(machine, thread); |
| 1189 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 1190 | thread = machine__findnew_thread(machine, event->fork.pid, |
| 1191 | event->fork.tid); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1192 | if (dump_trace) |
| 1193 | perf_event__fprintf_task(event, stdout); |
| 1194 | |
| 1195 | if (thread == NULL || parent == NULL || |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1196 | thread__fork(thread, parent, sample->time) < 0) { |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1197 | dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n"); |
| 1198 | return -1; |
| 1199 | } |
| 1200 | |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1204 | int machine__process_exit_event(struct machine *machine, union perf_event *event, |
| 1205 | struct perf_sample *sample __maybe_unused) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1206 | { |
Jiri Olsa | d75e609 | 2014-03-14 15:00:03 +0100 | [diff] [blame] | 1207 | struct thread *thread = machine__find_thread(machine, |
| 1208 | event->fork.pid, |
| 1209 | event->fork.tid); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1210 | |
| 1211 | if (dump_trace) |
| 1212 | perf_event__fprintf_task(event, stdout); |
| 1213 | |
| 1214 | if (thread != NULL) |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1215 | thread__exited(thread); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1216 | |
| 1217 | return 0; |
| 1218 | } |
| 1219 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1220 | int machine__process_event(struct machine *machine, union perf_event *event, |
| 1221 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1222 | { |
| 1223 | int ret; |
| 1224 | |
| 1225 | switch (event->header.type) { |
| 1226 | case PERF_RECORD_COMM: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1227 | ret = machine__process_comm_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1228 | case PERF_RECORD_MMAP: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1229 | ret = machine__process_mmap_event(machine, event, sample); break; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1230 | case PERF_RECORD_MMAP2: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1231 | ret = machine__process_mmap2_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1232 | case PERF_RECORD_FORK: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1233 | ret = machine__process_fork_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1234 | case PERF_RECORD_EXIT: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1235 | ret = machine__process_exit_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1236 | case PERF_RECORD_LOST: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1237 | ret = machine__process_lost_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1238 | default: |
| 1239 | ret = -1; |
| 1240 | break; |
| 1241 | } |
| 1242 | |
| 1243 | return ret; |
| 1244 | } |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1245 | |
Greg Price | b21484f | 2012-12-06 21:48:05 -0800 | [diff] [blame] | 1246 | 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] | 1247 | { |
Greg Price | b21484f | 2012-12-06 21:48:05 -0800 | [diff] [blame] | 1248 | if (sym->name && !regexec(regex, sym->name, 0, NULL, 0)) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1249 | return 1; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1250 | return 0; |
| 1251 | } |
| 1252 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1253 | static void ip__resolve_ams(struct machine *machine, struct thread *thread, |
| 1254 | struct addr_map_symbol *ams, |
| 1255 | u64 ip) |
| 1256 | { |
| 1257 | struct addr_location al; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1258 | |
| 1259 | memset(&al, 0, sizeof(al)); |
Arnaldo Carvalho de Melo | 52a3cb8 | 2014-03-11 16:16:49 -0300 | [diff] [blame] | 1260 | /* |
| 1261 | * We cannot use the header.misc hint to determine whether a |
| 1262 | * branch stack address is user, kernel, guest, hypervisor. |
| 1263 | * Branches may straddle the kernel/user/hypervisor boundaries. |
| 1264 | * Thus, we have to try consecutively until we find a match |
| 1265 | * or else, the symbol is unknown |
| 1266 | */ |
| 1267 | thread__find_cpumode_addr_location(thread, machine, MAP__FUNCTION, ip, &al); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1268 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1269 | ams->addr = ip; |
| 1270 | ams->al_addr = al.addr; |
| 1271 | ams->sym = al.sym; |
| 1272 | ams->map = al.map; |
| 1273 | } |
| 1274 | |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 1275 | static void ip__resolve_data(struct machine *machine, struct thread *thread, |
| 1276 | u8 m, struct addr_map_symbol *ams, u64 addr) |
| 1277 | { |
| 1278 | struct addr_location al; |
| 1279 | |
| 1280 | memset(&al, 0, sizeof(al)); |
| 1281 | |
Adrian Hunter | 61710bd | 2013-08-08 14:32:26 +0300 | [diff] [blame] | 1282 | thread__find_addr_location(thread, machine, m, MAP__VARIABLE, addr, |
| 1283 | &al); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 1284 | ams->addr = addr; |
| 1285 | ams->al_addr = al.addr; |
| 1286 | ams->sym = al.sym; |
| 1287 | ams->map = al.map; |
| 1288 | } |
| 1289 | |
Arnaldo Carvalho de Melo | e80faac | 2014-01-22 13:05:06 -0300 | [diff] [blame] | 1290 | struct mem_info *sample__resolve_mem(struct perf_sample *sample, |
| 1291 | struct addr_location *al) |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 1292 | { |
| 1293 | struct mem_info *mi = zalloc(sizeof(*mi)); |
| 1294 | |
| 1295 | if (!mi) |
| 1296 | return NULL; |
| 1297 | |
Arnaldo Carvalho de Melo | e80faac | 2014-01-22 13:05:06 -0300 | [diff] [blame] | 1298 | ip__resolve_ams(al->machine, al->thread, &mi->iaddr, sample->ip); |
| 1299 | ip__resolve_data(al->machine, al->thread, al->cpumode, |
| 1300 | &mi->daddr, sample->addr); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 1301 | mi->data_src.val = sample->data_src; |
| 1302 | |
| 1303 | return mi; |
| 1304 | } |
| 1305 | |
Arnaldo Carvalho de Melo | 644f2df | 2014-01-22 13:15:36 -0300 | [diff] [blame] | 1306 | struct branch_info *sample__resolve_bstack(struct perf_sample *sample, |
| 1307 | struct addr_location *al) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1308 | { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1309 | unsigned int i; |
Arnaldo Carvalho de Melo | 644f2df | 2014-01-22 13:15:36 -0300 | [diff] [blame] | 1310 | const struct branch_stack *bs = sample->branch_stack; |
| 1311 | 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] | 1312 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1313 | if (!bi) |
| 1314 | return NULL; |
| 1315 | |
| 1316 | for (i = 0; i < bs->nr; i++) { |
Arnaldo Carvalho de Melo | 644f2df | 2014-01-22 13:15:36 -0300 | [diff] [blame] | 1317 | ip__resolve_ams(al->machine, al->thread, &bi[i].to, bs->entries[i].to); |
| 1318 | ip__resolve_ams(al->machine, al->thread, &bi[i].from, bs->entries[i].from); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1319 | bi[i].flags = bs->entries[i].flags; |
| 1320 | } |
| 1321 | return bi; |
| 1322 | } |
| 1323 | |
| 1324 | static int machine__resolve_callchain_sample(struct machine *machine, |
| 1325 | struct thread *thread, |
| 1326 | struct ip_callchain *chain, |
Greg Price | b21484f | 2012-12-06 21:48:05 -0800 | [diff] [blame] | 1327 | struct symbol **parent, |
Waiman Long | 91e9561 | 2013-10-18 10:38:48 -0400 | [diff] [blame] | 1328 | struct addr_location *root_al, |
| 1329 | int max_stack) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1330 | { |
| 1331 | u8 cpumode = PERF_RECORD_MISC_USER; |
Waiman Long | 91e9561 | 2013-10-18 10:38:48 -0400 | [diff] [blame] | 1332 | int chain_nr = min(max_stack, (int)chain->nr); |
| 1333 | int i; |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 1334 | int j; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1335 | int err; |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 1336 | int skip_idx __maybe_unused; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1337 | |
| 1338 | callchain_cursor_reset(&callchain_cursor); |
| 1339 | |
| 1340 | if (chain->nr > PERF_MAX_STACK_DEPTH) { |
| 1341 | pr_warning("corrupted callchain. skipping...\n"); |
| 1342 | return 0; |
| 1343 | } |
| 1344 | |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 1345 | /* |
| 1346 | * Based on DWARF debug information, some architectures skip |
| 1347 | * a callchain entry saved by the kernel. |
| 1348 | */ |
| 1349 | skip_idx = arch_skip_callchain_idx(machine, thread, chain); |
| 1350 | |
Waiman Long | 91e9561 | 2013-10-18 10:38:48 -0400 | [diff] [blame] | 1351 | for (i = 0; i < chain_nr; i++) { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1352 | u64 ip; |
| 1353 | struct addr_location al; |
| 1354 | |
| 1355 | if (callchain_param.order == ORDER_CALLEE) |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 1356 | j = i; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1357 | else |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 1358 | j = chain->nr - i - 1; |
| 1359 | |
| 1360 | #ifdef HAVE_SKIP_CALLCHAIN_IDX |
| 1361 | if (j == skip_idx) |
| 1362 | continue; |
| 1363 | #endif |
| 1364 | ip = chain->ips[j]; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1365 | |
| 1366 | if (ip >= PERF_CONTEXT_MAX) { |
| 1367 | switch (ip) { |
| 1368 | case PERF_CONTEXT_HV: |
| 1369 | cpumode = PERF_RECORD_MISC_HYPERVISOR; |
| 1370 | break; |
| 1371 | case PERF_CONTEXT_KERNEL: |
| 1372 | cpumode = PERF_RECORD_MISC_KERNEL; |
| 1373 | break; |
| 1374 | case PERF_CONTEXT_USER: |
| 1375 | cpumode = PERF_RECORD_MISC_USER; |
| 1376 | break; |
| 1377 | default: |
| 1378 | pr_debug("invalid callchain context: " |
| 1379 | "%"PRId64"\n", (s64) ip); |
| 1380 | /* |
| 1381 | * It seems the callchain is corrupted. |
| 1382 | * Discard all. |
| 1383 | */ |
| 1384 | callchain_cursor_reset(&callchain_cursor); |
| 1385 | return 0; |
| 1386 | } |
| 1387 | continue; |
| 1388 | } |
| 1389 | |
Namhyung Kim | b3cef7f | 2014-03-17 16:59:21 -0300 | [diff] [blame] | 1390 | al.filtered = 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1391 | thread__find_addr_location(thread, machine, cpumode, |
Adrian Hunter | 61710bd | 2013-08-08 14:32:26 +0300 | [diff] [blame] | 1392 | MAP__FUNCTION, ip, &al); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1393 | if (al.sym != NULL) { |
| 1394 | if (sort__has_parent && !*parent && |
Greg Price | b21484f | 2012-12-06 21:48:05 -0800 | [diff] [blame] | 1395 | symbol__match_regex(al.sym, &parent_regex)) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1396 | *parent = al.sym; |
Greg Price | b21484f | 2012-12-06 21:48:05 -0800 | [diff] [blame] | 1397 | else if (have_ignore_callees && root_al && |
| 1398 | symbol__match_regex(al.sym, &ignore_callees_regex)) { |
| 1399 | /* Treat this symbol as the root, |
| 1400 | forgetting its callees. */ |
| 1401 | *root_al = al; |
| 1402 | callchain_cursor_reset(&callchain_cursor); |
| 1403 | } |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | err = callchain_cursor_append(&callchain_cursor, |
| 1407 | ip, al.map, al.sym); |
| 1408 | if (err) |
| 1409 | return err; |
| 1410 | } |
| 1411 | |
| 1412 | return 0; |
| 1413 | } |
| 1414 | |
| 1415 | static int unwind_entry(struct unwind_entry *entry, void *arg) |
| 1416 | { |
| 1417 | struct callchain_cursor *cursor = arg; |
| 1418 | return callchain_cursor_append(cursor, entry->ip, |
| 1419 | entry->map, entry->sym); |
| 1420 | } |
| 1421 | |
| 1422 | int machine__resolve_callchain(struct machine *machine, |
| 1423 | struct perf_evsel *evsel, |
| 1424 | struct thread *thread, |
| 1425 | struct perf_sample *sample, |
Greg Price | b21484f | 2012-12-06 21:48:05 -0800 | [diff] [blame] | 1426 | struct symbol **parent, |
Waiman Long | 91e9561 | 2013-10-18 10:38:48 -0400 | [diff] [blame] | 1427 | struct addr_location *root_al, |
| 1428 | int max_stack) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1429 | { |
| 1430 | int ret; |
| 1431 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1432 | ret = machine__resolve_callchain_sample(machine, thread, |
Waiman Long | 91e9561 | 2013-10-18 10:38:48 -0400 | [diff] [blame] | 1433 | sample->callchain, parent, |
| 1434 | root_al, max_stack); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1435 | if (ret) |
| 1436 | return ret; |
| 1437 | |
| 1438 | /* Can we do dwarf post unwind? */ |
| 1439 | if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) && |
| 1440 | (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER))) |
| 1441 | return 0; |
| 1442 | |
| 1443 | /* Bail out if nothing was captured. */ |
| 1444 | if ((!sample->user_regs.regs) || |
| 1445 | (!sample->user_stack.size)) |
| 1446 | return 0; |
| 1447 | |
| 1448 | return unwind__get_entries(unwind_entry, &callchain_cursor, machine, |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 1449 | thread, sample, max_stack); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1450 | |
| 1451 | } |
David Ahern | 35feee1 | 2013-09-28 13:12:58 -0600 | [diff] [blame] | 1452 | |
| 1453 | int machine__for_each_thread(struct machine *machine, |
| 1454 | int (*fn)(struct thread *thread, void *p), |
| 1455 | void *priv) |
| 1456 | { |
| 1457 | struct rb_node *nd; |
| 1458 | struct thread *thread; |
| 1459 | int rc = 0; |
| 1460 | |
| 1461 | for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) { |
| 1462 | thread = rb_entry(nd, struct thread, rb_node); |
| 1463 | rc = fn(thread, priv); |
| 1464 | if (rc != 0) |
| 1465 | return rc; |
| 1466 | } |
| 1467 | |
| 1468 | list_for_each_entry(thread, &machine->dead_threads, node) { |
| 1469 | rc = fn(thread, priv); |
| 1470 | if (rc != 0) |
| 1471 | return rc; |
| 1472 | } |
| 1473 | return rc; |
| 1474 | } |
Arnaldo Carvalho de Melo | 58d925d | 2013-11-11 11:28:02 -0300 | [diff] [blame] | 1475 | |
Arnaldo Carvalho de Melo | a33fbd5 | 2013-11-11 11:36:12 -0300 | [diff] [blame] | 1476 | 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] | 1477 | struct target *target, struct thread_map *threads, |
Arnaldo Carvalho de Melo | a33fbd5 | 2013-11-11 11:36:12 -0300 | [diff] [blame] | 1478 | perf_event__handler_t process, bool data_mmap) |
Arnaldo Carvalho de Melo | 58d925d | 2013-11-11 11:28:02 -0300 | [diff] [blame] | 1479 | { |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1480 | if (target__has_task(target)) |
Arnaldo Carvalho de Melo | 58d925d | 2013-11-11 11:28:02 -0300 | [diff] [blame] | 1481 | return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap); |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1482 | else if (target__has_cpu(target)) |
Arnaldo Carvalho de Melo | 58d925d | 2013-11-11 11:28:02 -0300 | [diff] [blame] | 1483 | return perf_event__synthesize_threads(tool, process, machine, data_mmap); |
| 1484 | /* command specified */ |
| 1485 | return 0; |
| 1486 | } |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 1487 | |
| 1488 | pid_t machine__get_current_tid(struct machine *machine, int cpu) |
| 1489 | { |
| 1490 | if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid) |
| 1491 | return -1; |
| 1492 | |
| 1493 | return machine->current_tid[cpu]; |
| 1494 | } |
| 1495 | |
| 1496 | int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid, |
| 1497 | pid_t tid) |
| 1498 | { |
| 1499 | struct thread *thread; |
| 1500 | |
| 1501 | if (cpu < 0) |
| 1502 | return -EINVAL; |
| 1503 | |
| 1504 | if (!machine->current_tid) { |
| 1505 | int i; |
| 1506 | |
| 1507 | machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t)); |
| 1508 | if (!machine->current_tid) |
| 1509 | return -ENOMEM; |
| 1510 | for (i = 0; i < MAX_NR_CPUS; i++) |
| 1511 | machine->current_tid[i] = -1; |
| 1512 | } |
| 1513 | |
| 1514 | if (cpu >= MAX_NR_CPUS) { |
| 1515 | pr_err("Requested CPU %d too large. ", cpu); |
| 1516 | pr_err("Consider raising MAX_NR_CPUS\n"); |
| 1517 | return -EINVAL; |
| 1518 | } |
| 1519 | |
| 1520 | machine->current_tid[cpu] = tid; |
| 1521 | |
| 1522 | thread = machine__findnew_thread(machine, pid, tid); |
| 1523 | if (!thread) |
| 1524 | return -ENOMEM; |
| 1525 | |
| 1526 | thread->cpu = cpu; |
| 1527 | |
| 1528 | return 0; |
| 1529 | } |