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