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