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