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