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