]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
76b31a29 | 2 | #include <dirent.h> |
a43783ae | 3 | #include <errno.h> |
fd20e811 | 4 | #include <inttypes.h> |
1eae20c1 | 5 | #include <regex.h> |
3f067dca | 6 | #include "callchain.h" |
b0a7d1a0 ACM |
7 | #include "debug.h" |
8 | #include "event.h" | |
3f067dca ACM |
9 | #include "evsel.h" |
10 | #include "hist.h" | |
9d2f8e22 ACM |
11 | #include "machine.h" |
12 | #include "map.h" | |
3f067dca | 13 | #include "sort.h" |
69d2591a | 14 | #include "strlist.h" |
9d2f8e22 | 15 | #include "thread.h" |
d027b640 | 16 | #include "vdso.h" |
9d2f8e22 | 17 | #include <stdbool.h> |
7a8ef4c4 ACM |
18 | #include <sys/types.h> |
19 | #include <sys/stat.h> | |
20 | #include <unistd.h> | |
3f067dca | 21 | #include "unwind.h" |
8b7bad58 | 22 | #include "linux/hash.h" |
f3b3614a | 23 | #include "asm/bug.h" |
9d2f8e22 | 24 | |
3d689ed6 ACM |
25 | #include "sane_ctype.h" |
26 | #include <symbol/kallsyms.h> | |
27 | ||
b91fc39f ACM |
28 | static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock); |
29 | ||
e167f995 ACM |
30 | static void dsos__init(struct dsos *dsos) |
31 | { | |
32 | INIT_LIST_HEAD(&dsos->head); | |
33 | dsos->root = RB_ROOT; | |
e8807844 | 34 | pthread_rwlock_init(&dsos->lock, NULL); |
e167f995 ACM |
35 | } |
36 | ||
69d2591a ACM |
37 | int machine__init(struct machine *machine, const char *root_dir, pid_t pid) |
38 | { | |
93b0ba3c | 39 | memset(machine, 0, sizeof(*machine)); |
11246c70 | 40 | map_groups__init(&machine->kmaps, machine); |
69d2591a | 41 | RB_CLEAR_NODE(&machine->rb_node); |
3d39ac53 | 42 | dsos__init(&machine->dsos); |
69d2591a ACM |
43 | |
44 | machine->threads = RB_ROOT; | |
b91fc39f | 45 | pthread_rwlock_init(&machine->threads_lock, NULL); |
d2c11034 | 46 | machine->nr_threads = 0; |
69d2591a ACM |
47 | INIT_LIST_HEAD(&machine->dead_threads); |
48 | machine->last_match = NULL; | |
49 | ||
d027b640 | 50 | machine->vdso_info = NULL; |
4cde998d | 51 | machine->env = NULL; |
d027b640 | 52 | |
69d2591a ACM |
53 | machine->pid = pid; |
54 | ||
14bd6d20 | 55 | machine->id_hdr_size = 0; |
caf8a0d0 | 56 | machine->kptr_restrict_warned = false; |
cfe1c414 | 57 | machine->comm_exec = false; |
fbe2af45 | 58 | machine->kernel_start = 0; |
611a5ce8 | 59 | |
cc1121ab MH |
60 | memset(machine->vmlinux_maps, 0, sizeof(machine->vmlinux_maps)); |
61 | ||
69d2591a ACM |
62 | machine->root_dir = strdup(root_dir); |
63 | if (machine->root_dir == NULL) | |
64 | return -ENOMEM; | |
65 | ||
66 | if (pid != HOST_KERNEL_ID) { | |
1fcb8768 | 67 | struct thread *thread = machine__findnew_thread(machine, -1, |
314add6b | 68 | pid); |
69d2591a ACM |
69 | char comm[64]; |
70 | ||
71 | if (thread == NULL) | |
72 | return -ENOMEM; | |
73 | ||
74 | snprintf(comm, sizeof(comm), "[guest/%d]", pid); | |
162f0bef | 75 | thread__set_comm(thread, comm, 0); |
b91fc39f | 76 | thread__put(thread); |
69d2591a ACM |
77 | } |
78 | ||
b9d266ba AH |
79 | machine->current_tid = NULL; |
80 | ||
69d2591a ACM |
81 | return 0; |
82 | } | |
83 | ||
8fb598e5 DA |
84 | struct machine *machine__new_host(void) |
85 | { | |
86 | struct machine *machine = malloc(sizeof(*machine)); | |
87 | ||
88 | if (machine != NULL) { | |
89 | machine__init(machine, "", HOST_KERNEL_ID); | |
90 | ||
91 | if (machine__create_kernel_maps(machine) < 0) | |
92 | goto out_delete; | |
93 | } | |
94 | ||
95 | return machine; | |
96 | out_delete: | |
97 | free(machine); | |
98 | return NULL; | |
99 | } | |
100 | ||
7d132caa ACM |
101 | struct machine *machine__new_kallsyms(void) |
102 | { | |
103 | struct machine *machine = machine__new_host(); | |
104 | /* | |
105 | * FIXME: | |
106 | * 1) MAP__FUNCTION will go away when we stop loading separate maps for | |
107 | * functions and data objects. | |
108 | * 2) We should switch to machine__load_kallsyms(), i.e. not explicitely | |
109 | * ask for not using the kcore parsing code, once this one is fixed | |
110 | * to create a map per module. | |
111 | */ | |
112 | if (machine && __machine__load_kallsyms(machine, "/proc/kallsyms", MAP__FUNCTION, true) <= 0) { | |
113 | machine__delete(machine); | |
114 | machine = NULL; | |
115 | } | |
116 | ||
117 | return machine; | |
118 | } | |
119 | ||
d3a7c489 | 120 | static void dsos__purge(struct dsos *dsos) |
69d2591a ACM |
121 | { |
122 | struct dso *pos, *n; | |
123 | ||
e8807844 ACM |
124 | pthread_rwlock_wrlock(&dsos->lock); |
125 | ||
8fa7d87f | 126 | list_for_each_entry_safe(pos, n, &dsos->head, node) { |
4598a0a6 | 127 | RB_CLEAR_NODE(&pos->rb_node); |
e266a753 | 128 | pos->root = NULL; |
d3a7c489 ACM |
129 | list_del_init(&pos->node); |
130 | dso__put(pos); | |
69d2591a | 131 | } |
e8807844 ACM |
132 | |
133 | pthread_rwlock_unlock(&dsos->lock); | |
d3a7c489 | 134 | } |
e8807844 | 135 | |
d3a7c489 ACM |
136 | static void dsos__exit(struct dsos *dsos) |
137 | { | |
138 | dsos__purge(dsos); | |
e8807844 | 139 | pthread_rwlock_destroy(&dsos->lock); |
69d2591a ACM |
140 | } |
141 | ||
3f067dca ACM |
142 | void machine__delete_threads(struct machine *machine) |
143 | { | |
b91fc39f | 144 | struct rb_node *nd; |
3f067dca | 145 | |
b91fc39f ACM |
146 | pthread_rwlock_wrlock(&machine->threads_lock); |
147 | nd = rb_first(&machine->threads); | |
3f067dca ACM |
148 | while (nd) { |
149 | struct thread *t = rb_entry(nd, struct thread, rb_node); | |
150 | ||
3f067dca | 151 | nd = rb_next(nd); |
b91fc39f | 152 | __machine__remove_thread(machine, t, false); |
3f067dca | 153 | } |
b91fc39f | 154 | pthread_rwlock_unlock(&machine->threads_lock); |
3f067dca ACM |
155 | } |
156 | ||
69d2591a ACM |
157 | void machine__exit(struct machine *machine) |
158 | { | |
ebe9729c | 159 | machine__destroy_kernel_maps(machine); |
69d2591a | 160 | map_groups__exit(&machine->kmaps); |
e8807844 | 161 | dsos__exit(&machine->dsos); |
9a4388c7 | 162 | machine__exit_vdso(machine); |
04662523 | 163 | zfree(&machine->root_dir); |
b9d266ba | 164 | zfree(&machine->current_tid); |
b91fc39f | 165 | pthread_rwlock_destroy(&machine->threads_lock); |
69d2591a ACM |
166 | } |
167 | ||
168 | void machine__delete(struct machine *machine) | |
169 | { | |
32ca678d ACM |
170 | if (machine) { |
171 | machine__exit(machine); | |
172 | free(machine); | |
173 | } | |
69d2591a ACM |
174 | } |
175 | ||
876650e6 ACM |
176 | void machines__init(struct machines *machines) |
177 | { | |
178 | machine__init(&machines->host, "", HOST_KERNEL_ID); | |
179 | machines->guests = RB_ROOT; | |
180 | } | |
181 | ||
182 | void machines__exit(struct machines *machines) | |
183 | { | |
184 | machine__exit(&machines->host); | |
185 | /* XXX exit guest */ | |
186 | } | |
187 | ||
188 | struct machine *machines__add(struct machines *machines, pid_t pid, | |
69d2591a ACM |
189 | const char *root_dir) |
190 | { | |
876650e6 | 191 | struct rb_node **p = &machines->guests.rb_node; |
69d2591a ACM |
192 | struct rb_node *parent = NULL; |
193 | struct machine *pos, *machine = malloc(sizeof(*machine)); | |
194 | ||
195 | if (machine == NULL) | |
196 | return NULL; | |
197 | ||
198 | if (machine__init(machine, root_dir, pid) != 0) { | |
199 | free(machine); | |
200 | return NULL; | |
201 | } | |
202 | ||
203 | while (*p != NULL) { | |
204 | parent = *p; | |
205 | pos = rb_entry(parent, struct machine, rb_node); | |
206 | if (pid < pos->pid) | |
207 | p = &(*p)->rb_left; | |
208 | else | |
209 | p = &(*p)->rb_right; | |
210 | } | |
211 | ||
212 | rb_link_node(&machine->rb_node, parent, p); | |
876650e6 | 213 | rb_insert_color(&machine->rb_node, &machines->guests); |
69d2591a ACM |
214 | |
215 | return machine; | |
216 | } | |
217 | ||
cfe1c414 AH |
218 | void machines__set_comm_exec(struct machines *machines, bool comm_exec) |
219 | { | |
220 | struct rb_node *nd; | |
221 | ||
222 | machines->host.comm_exec = comm_exec; | |
223 | ||
224 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { | |
225 | struct machine *machine = rb_entry(nd, struct machine, rb_node); | |
226 | ||
227 | machine->comm_exec = comm_exec; | |
228 | } | |
229 | } | |
230 | ||
876650e6 | 231 | struct machine *machines__find(struct machines *machines, pid_t pid) |
69d2591a | 232 | { |
876650e6 | 233 | struct rb_node **p = &machines->guests.rb_node; |
69d2591a ACM |
234 | struct rb_node *parent = NULL; |
235 | struct machine *machine; | |
236 | struct machine *default_machine = NULL; | |
237 | ||
876650e6 ACM |
238 | if (pid == HOST_KERNEL_ID) |
239 | return &machines->host; | |
240 | ||
69d2591a ACM |
241 | while (*p != NULL) { |
242 | parent = *p; | |
243 | machine = rb_entry(parent, struct machine, rb_node); | |
244 | if (pid < machine->pid) | |
245 | p = &(*p)->rb_left; | |
246 | else if (pid > machine->pid) | |
247 | p = &(*p)->rb_right; | |
248 | else | |
249 | return machine; | |
250 | if (!machine->pid) | |
251 | default_machine = machine; | |
252 | } | |
253 | ||
254 | return default_machine; | |
255 | } | |
256 | ||
876650e6 | 257 | struct machine *machines__findnew(struct machines *machines, pid_t pid) |
69d2591a ACM |
258 | { |
259 | char path[PATH_MAX]; | |
260 | const char *root_dir = ""; | |
261 | struct machine *machine = machines__find(machines, pid); | |
262 | ||
263 | if (machine && (machine->pid == pid)) | |
264 | goto out; | |
265 | ||
266 | if ((pid != HOST_KERNEL_ID) && | |
267 | (pid != DEFAULT_GUEST_KERNEL_ID) && | |
268 | (symbol_conf.guestmount)) { | |
269 | sprintf(path, "%s/%d", symbol_conf.guestmount, pid); | |
270 | if (access(path, R_OK)) { | |
271 | static struct strlist *seen; | |
272 | ||
273 | if (!seen) | |
4a77e218 | 274 | seen = strlist__new(NULL, NULL); |
69d2591a ACM |
275 | |
276 | if (!strlist__has_entry(seen, path)) { | |
277 | pr_err("Can't access file %s\n", path); | |
278 | strlist__add(seen, path); | |
279 | } | |
280 | machine = NULL; | |
281 | goto out; | |
282 | } | |
283 | root_dir = path; | |
284 | } | |
285 | ||
286 | machine = machines__add(machines, pid, root_dir); | |
287 | out: | |
288 | return machine; | |
289 | } | |
290 | ||
876650e6 ACM |
291 | void machines__process_guests(struct machines *machines, |
292 | machine__process_t process, void *data) | |
69d2591a ACM |
293 | { |
294 | struct rb_node *nd; | |
295 | ||
876650e6 | 296 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { |
69d2591a ACM |
297 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
298 | process(pos, data); | |
299 | } | |
300 | } | |
301 | ||
302 | char *machine__mmap_name(struct machine *machine, char *bf, size_t size) | |
303 | { | |
304 | if (machine__is_host(machine)) | |
305 | snprintf(bf, size, "[%s]", "kernel.kallsyms"); | |
306 | else if (machine__is_default_guest(machine)) | |
307 | snprintf(bf, size, "[%s]", "guest.kernel.kallsyms"); | |
308 | else { | |
309 | snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms", | |
310 | machine->pid); | |
311 | } | |
312 | ||
313 | return bf; | |
314 | } | |
315 | ||
876650e6 | 316 | void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size) |
69d2591a ACM |
317 | { |
318 | struct rb_node *node; | |
319 | struct machine *machine; | |
320 | ||
876650e6 ACM |
321 | machines->host.id_hdr_size = id_hdr_size; |
322 | ||
323 | for (node = rb_first(&machines->guests); node; node = rb_next(node)) { | |
69d2591a ACM |
324 | machine = rb_entry(node, struct machine, rb_node); |
325 | machine->id_hdr_size = id_hdr_size; | |
326 | } | |
327 | ||
328 | return; | |
329 | } | |
330 | ||
29ce3612 AH |
331 | static void machine__update_thread_pid(struct machine *machine, |
332 | struct thread *th, pid_t pid) | |
333 | { | |
334 | struct thread *leader; | |
335 | ||
336 | if (pid == th->pid_ || pid == -1 || th->pid_ != -1) | |
337 | return; | |
338 | ||
339 | th->pid_ = pid; | |
340 | ||
341 | if (th->pid_ == th->tid) | |
342 | return; | |
343 | ||
b91fc39f | 344 | leader = __machine__findnew_thread(machine, th->pid_, th->pid_); |
29ce3612 AH |
345 | if (!leader) |
346 | goto out_err; | |
347 | ||
348 | if (!leader->mg) | |
11246c70 | 349 | leader->mg = map_groups__new(machine); |
29ce3612 AH |
350 | |
351 | if (!leader->mg) | |
352 | goto out_err; | |
353 | ||
354 | if (th->mg == leader->mg) | |
355 | return; | |
356 | ||
357 | if (th->mg) { | |
358 | /* | |
359 | * Maps are created from MMAP events which provide the pid and | |
360 | * tid. Consequently there never should be any maps on a thread | |
361 | * with an unknown pid. Just print an error if there are. | |
362 | */ | |
363 | if (!map_groups__empty(th->mg)) | |
364 | pr_err("Discarding thread maps for %d:%d\n", | |
365 | th->pid_, th->tid); | |
8e160b2e | 366 | map_groups__put(th->mg); |
29ce3612 AH |
367 | } |
368 | ||
369 | th->mg = map_groups__get(leader->mg); | |
abd82868 ACM |
370 | out_put: |
371 | thread__put(leader); | |
29ce3612 | 372 | return; |
29ce3612 AH |
373 | out_err: |
374 | pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid); | |
abd82868 | 375 | goto out_put; |
29ce3612 AH |
376 | } |
377 | ||
abd82868 | 378 | /* |
bd1a0be5 | 379 | * Caller must eventually drop thread->refcnt returned with a successful |
abd82868 ACM |
380 | * lookup/new thread inserted. |
381 | */ | |
b91fc39f ACM |
382 | static struct thread *____machine__findnew_thread(struct machine *machine, |
383 | pid_t pid, pid_t tid, | |
384 | bool create) | |
9d2f8e22 ACM |
385 | { |
386 | struct rb_node **p = &machine->threads.rb_node; | |
387 | struct rb_node *parent = NULL; | |
388 | struct thread *th; | |
389 | ||
390 | /* | |
38051234 | 391 | * Front-end cache - TID lookups come in blocks, |
9d2f8e22 ACM |
392 | * so most of the time we dont have to look up |
393 | * the full rbtree: | |
394 | */ | |
29ce3612 | 395 | th = machine->last_match; |
f3b623b8 ACM |
396 | if (th != NULL) { |
397 | if (th->tid == tid) { | |
398 | machine__update_thread_pid(machine, th, pid); | |
abd82868 | 399 | return thread__get(th); |
f3b623b8 ACM |
400 | } |
401 | ||
0ceb8f6e | 402 | machine->last_match = NULL; |
99d725fc | 403 | } |
9d2f8e22 ACM |
404 | |
405 | while (*p != NULL) { | |
406 | parent = *p; | |
407 | th = rb_entry(parent, struct thread, rb_node); | |
408 | ||
38051234 | 409 | if (th->tid == tid) { |
0ceb8f6e | 410 | machine->last_match = th; |
29ce3612 | 411 | machine__update_thread_pid(machine, th, pid); |
abd82868 | 412 | return thread__get(th); |
9d2f8e22 ACM |
413 | } |
414 | ||
38051234 | 415 | if (tid < th->tid) |
9d2f8e22 ACM |
416 | p = &(*p)->rb_left; |
417 | else | |
418 | p = &(*p)->rb_right; | |
419 | } | |
420 | ||
421 | if (!create) | |
422 | return NULL; | |
423 | ||
99d725fc | 424 | th = thread__new(pid, tid); |
9d2f8e22 ACM |
425 | if (th != NULL) { |
426 | rb_link_node(&th->rb_node, parent, p); | |
427 | rb_insert_color(&th->rb_node, &machine->threads); | |
cddcef60 JO |
428 | |
429 | /* | |
430 | * We have to initialize map_groups separately | |
431 | * after rb tree is updated. | |
432 | * | |
433 | * The reason is that we call machine__findnew_thread | |
434 | * within thread__init_map_groups to find the thread | |
435 | * leader and that would screwed the rb tree. | |
436 | */ | |
418029b7 | 437 | if (thread__init_map_groups(th, machine)) { |
0170b14f | 438 | rb_erase_init(&th->rb_node, &machine->threads); |
b91fc39f | 439 | RB_CLEAR_NODE(&th->rb_node); |
abd82868 | 440 | thread__put(th); |
cddcef60 | 441 | return NULL; |
418029b7 | 442 | } |
f3b623b8 ACM |
443 | /* |
444 | * It is now in the rbtree, get a ref | |
445 | */ | |
446 | thread__get(th); | |
0ceb8f6e | 447 | machine->last_match = th; |
d2c11034 | 448 | ++machine->nr_threads; |
9d2f8e22 ACM |
449 | } |
450 | ||
451 | return th; | |
452 | } | |
453 | ||
b91fc39f ACM |
454 | struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid) |
455 | { | |
456 | return ____machine__findnew_thread(machine, pid, tid, true); | |
457 | } | |
458 | ||
314add6b AH |
459 | struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, |
460 | pid_t tid) | |
9d2f8e22 | 461 | { |
b91fc39f ACM |
462 | struct thread *th; |
463 | ||
464 | pthread_rwlock_wrlock(&machine->threads_lock); | |
abd82868 | 465 | th = __machine__findnew_thread(machine, pid, tid); |
b91fc39f ACM |
466 | pthread_rwlock_unlock(&machine->threads_lock); |
467 | return th; | |
9d2f8e22 ACM |
468 | } |
469 | ||
d75e6097 JO |
470 | struct thread *machine__find_thread(struct machine *machine, pid_t pid, |
471 | pid_t tid) | |
9d2f8e22 | 472 | { |
b91fc39f ACM |
473 | struct thread *th; |
474 | pthread_rwlock_rdlock(&machine->threads_lock); | |
abd82868 | 475 | th = ____machine__findnew_thread(machine, pid, tid, false); |
b91fc39f ACM |
476 | pthread_rwlock_unlock(&machine->threads_lock); |
477 | return th; | |
9d2f8e22 | 478 | } |
b0a7d1a0 | 479 | |
cfe1c414 AH |
480 | struct comm *machine__thread_exec_comm(struct machine *machine, |
481 | struct thread *thread) | |
482 | { | |
483 | if (machine->comm_exec) | |
484 | return thread__exec_comm(thread); | |
485 | else | |
486 | return thread__comm(thread); | |
487 | } | |
488 | ||
162f0bef FW |
489 | int machine__process_comm_event(struct machine *machine, union perf_event *event, |
490 | struct perf_sample *sample) | |
b0a7d1a0 | 491 | { |
314add6b AH |
492 | struct thread *thread = machine__findnew_thread(machine, |
493 | event->comm.pid, | |
494 | event->comm.tid); | |
65de51f9 | 495 | bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC; |
b91fc39f | 496 | int err = 0; |
b0a7d1a0 | 497 | |
cfe1c414 AH |
498 | if (exec) |
499 | machine->comm_exec = true; | |
500 | ||
b0a7d1a0 ACM |
501 | if (dump_trace) |
502 | perf_event__fprintf_comm(event, stdout); | |
503 | ||
65de51f9 AH |
504 | if (thread == NULL || |
505 | __thread__set_comm(thread, event->comm.comm, sample->time, exec)) { | |
b0a7d1a0 | 506 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); |
b91fc39f | 507 | err = -1; |
b0a7d1a0 ACM |
508 | } |
509 | ||
b91fc39f ACM |
510 | thread__put(thread); |
511 | ||
512 | return err; | |
b0a7d1a0 ACM |
513 | } |
514 | ||
f3b3614a HB |
515 | int machine__process_namespaces_event(struct machine *machine __maybe_unused, |
516 | union perf_event *event, | |
517 | struct perf_sample *sample __maybe_unused) | |
518 | { | |
519 | struct thread *thread = machine__findnew_thread(machine, | |
520 | event->namespaces.pid, | |
521 | event->namespaces.tid); | |
522 | int err = 0; | |
523 | ||
524 | WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES, | |
525 | "\nWARNING: kernel seems to support more namespaces than perf" | |
526 | " tool.\nTry updating the perf tool..\n\n"); | |
527 | ||
528 | WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES, | |
529 | "\nWARNING: perf tool seems to support more namespaces than" | |
530 | " the kernel.\nTry updating the kernel..\n\n"); | |
531 | ||
532 | if (dump_trace) | |
533 | perf_event__fprintf_namespaces(event, stdout); | |
534 | ||
535 | if (thread == NULL || | |
536 | thread__set_namespaces(thread, sample->time, &event->namespaces)) { | |
537 | dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n"); | |
538 | err = -1; | |
539 | } | |
540 | ||
541 | thread__put(thread); | |
542 | ||
543 | return err; | |
544 | } | |
545 | ||
b0a7d1a0 | 546 | int machine__process_lost_event(struct machine *machine __maybe_unused, |
162f0bef | 547 | union perf_event *event, struct perf_sample *sample __maybe_unused) |
b0a7d1a0 ACM |
548 | { |
549 | dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n", | |
550 | event->lost.id, event->lost.lost); | |
551 | return 0; | |
552 | } | |
553 | ||
c4937a91 KL |
554 | int machine__process_lost_samples_event(struct machine *machine __maybe_unused, |
555 | union perf_event *event, struct perf_sample *sample) | |
556 | { | |
557 | dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n", | |
558 | sample->id, event->lost_samples.lost); | |
559 | return 0; | |
560 | } | |
561 | ||
9f2de315 ACM |
562 | static struct dso *machine__findnew_module_dso(struct machine *machine, |
563 | struct kmod_path *m, | |
564 | const char *filename) | |
da17ea33 JO |
565 | { |
566 | struct dso *dso; | |
da17ea33 | 567 | |
e8807844 ACM |
568 | pthread_rwlock_wrlock(&machine->dsos.lock); |
569 | ||
570 | dso = __dsos__find(&machine->dsos, m->name, true); | |
da17ea33 | 571 | if (!dso) { |
e8807844 | 572 | dso = __dsos__addnew(&machine->dsos, m->name); |
da17ea33 | 573 | if (dso == NULL) |
e8807844 | 574 | goto out_unlock; |
da17ea33 | 575 | |
6b335e8f | 576 | dso__set_module_info(dso, m, machine); |
ca33380a | 577 | dso__set_long_name(dso, strdup(filename), true); |
da17ea33 JO |
578 | } |
579 | ||
d3a7c489 | 580 | dso__get(dso); |
e8807844 ACM |
581 | out_unlock: |
582 | pthread_rwlock_unlock(&machine->dsos.lock); | |
da17ea33 JO |
583 | return dso; |
584 | } | |
585 | ||
4a96f7a0 AH |
586 | int machine__process_aux_event(struct machine *machine __maybe_unused, |
587 | union perf_event *event) | |
588 | { | |
589 | if (dump_trace) | |
590 | perf_event__fprintf_aux(event, stdout); | |
591 | return 0; | |
592 | } | |
593 | ||
0ad21f68 AH |
594 | int machine__process_itrace_start_event(struct machine *machine __maybe_unused, |
595 | union perf_event *event) | |
596 | { | |
597 | if (dump_trace) | |
598 | perf_event__fprintf_itrace_start(event, stdout); | |
599 | return 0; | |
600 | } | |
601 | ||
0286039f AH |
602 | int machine__process_switch_event(struct machine *machine __maybe_unused, |
603 | union perf_event *event) | |
604 | { | |
605 | if (dump_trace) | |
606 | perf_event__fprintf_switch(event, stdout); | |
607 | return 0; | |
608 | } | |
609 | ||
c03d5184 WN |
610 | static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename) |
611 | { | |
612 | const char *dup_filename; | |
613 | ||
614 | if (!filename || !dso || !dso->long_name) | |
615 | return; | |
616 | if (dso->long_name[0] != '[') | |
617 | return; | |
618 | if (!strchr(filename, '/')) | |
619 | return; | |
620 | ||
621 | dup_filename = strdup(filename); | |
622 | if (!dup_filename) | |
623 | return; | |
624 | ||
5dcf16df | 625 | dso__set_long_name(dso, dup_filename, true); |
c03d5184 WN |
626 | } |
627 | ||
9f2de315 ACM |
628 | struct map *machine__findnew_module_map(struct machine *machine, u64 start, |
629 | const char *filename) | |
3f067dca | 630 | { |
ca33380a | 631 | struct map *map = NULL; |
566c69c3 | 632 | struct dso *dso = NULL; |
ca33380a | 633 | struct kmod_path m; |
3f067dca | 634 | |
ca33380a | 635 | if (kmod_path__parse_name(&m, filename)) |
3f067dca ACM |
636 | return NULL; |
637 | ||
bc84f464 JO |
638 | map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION, |
639 | m.name); | |
c03d5184 WN |
640 | if (map) { |
641 | /* | |
642 | * If the map's dso is an offline module, give dso__load() | |
643 | * a chance to find the file path of that module by fixing | |
644 | * long_name. | |
645 | */ | |
646 | dso__adjust_kmod_long_name(map->dso, filename); | |
bc84f464 | 647 | goto out; |
c03d5184 | 648 | } |
bc84f464 | 649 | |
9f2de315 | 650 | dso = machine__findnew_module_dso(machine, &m, filename); |
ca33380a JO |
651 | if (dso == NULL) |
652 | goto out; | |
653 | ||
3f067dca ACM |
654 | map = map__new2(start, dso, MAP__FUNCTION); |
655 | if (map == NULL) | |
ca33380a | 656 | goto out; |
3f067dca | 657 | |
3f067dca | 658 | map_groups__insert(&machine->kmaps, map); |
ca33380a | 659 | |
9afcb420 MH |
660 | /* Put the map here because map_groups__insert alread got it */ |
661 | map__put(map); | |
ca33380a | 662 | out: |
566c69c3 MH |
663 | /* put the dso here, corresponding to machine__findnew_module_dso */ |
664 | dso__put(dso); | |
ca33380a | 665 | free(m.name); |
3f067dca ACM |
666 | return map; |
667 | } | |
668 | ||
876650e6 | 669 | size_t machines__fprintf_dsos(struct machines *machines, FILE *fp) |
3f067dca ACM |
670 | { |
671 | struct rb_node *nd; | |
3d39ac53 | 672 | size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp); |
3f067dca | 673 | |
876650e6 | 674 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { |
3f067dca | 675 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
3d39ac53 | 676 | ret += __dsos__fprintf(&pos->dsos.head, fp); |
3f067dca ACM |
677 | } |
678 | ||
679 | return ret; | |
680 | } | |
681 | ||
8fa7d87f | 682 | size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp, |
3f067dca ACM |
683 | bool (skip)(struct dso *dso, int parm), int parm) |
684 | { | |
3d39ac53 | 685 | return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm); |
3f067dca ACM |
686 | } |
687 | ||
876650e6 | 688 | size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp, |
3f067dca ACM |
689 | bool (skip)(struct dso *dso, int parm), int parm) |
690 | { | |
691 | struct rb_node *nd; | |
876650e6 | 692 | size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm); |
3f067dca | 693 | |
876650e6 | 694 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { |
3f067dca ACM |
695 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
696 | ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm); | |
697 | } | |
698 | return ret; | |
699 | } | |
700 | ||
701 | size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp) | |
702 | { | |
703 | int i; | |
704 | size_t printed = 0; | |
a5e813c6 | 705 | struct dso *kdso = machine__kernel_map(machine)->dso; |
3f067dca ACM |
706 | |
707 | if (kdso->has_build_id) { | |
708 | char filename[PATH_MAX]; | |
d2396999 KJ |
709 | if (dso__build_id_filename(kdso, filename, sizeof(filename), |
710 | false)) | |
3f067dca ACM |
711 | printed += fprintf(fp, "[0] %s\n", filename); |
712 | } | |
713 | ||
714 | for (i = 0; i < vmlinux_path__nr_entries; ++i) | |
715 | printed += fprintf(fp, "[%d] %s\n", | |
716 | i + kdso->has_build_id, vmlinux_path[i]); | |
717 | ||
718 | return printed; | |
719 | } | |
720 | ||
721 | size_t machine__fprintf(struct machine *machine, FILE *fp) | |
722 | { | |
d2c11034 | 723 | size_t ret; |
3f067dca ACM |
724 | struct rb_node *nd; |
725 | ||
b91fc39f ACM |
726 | pthread_rwlock_rdlock(&machine->threads_lock); |
727 | ||
d2c11034 ACM |
728 | ret = fprintf(fp, "Threads: %u\n", machine->nr_threads); |
729 | ||
3f067dca ACM |
730 | for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) { |
731 | struct thread *pos = rb_entry(nd, struct thread, rb_node); | |
732 | ||
733 | ret += thread__fprintf(pos, fp); | |
734 | } | |
735 | ||
b91fc39f ACM |
736 | pthread_rwlock_unlock(&machine->threads_lock); |
737 | ||
3f067dca ACM |
738 | return ret; |
739 | } | |
740 | ||
741 | static struct dso *machine__get_kernel(struct machine *machine) | |
742 | { | |
743 | const char *vmlinux_name = NULL; | |
744 | struct dso *kernel; | |
745 | ||
746 | if (machine__is_host(machine)) { | |
747 | vmlinux_name = symbol_conf.vmlinux_name; | |
748 | if (!vmlinux_name) | |
0a77582f | 749 | vmlinux_name = DSO__NAME_KALLSYMS; |
3f067dca | 750 | |
459ce518 ACM |
751 | kernel = machine__findnew_kernel(machine, vmlinux_name, |
752 | "[kernel]", DSO_TYPE_KERNEL); | |
3f067dca ACM |
753 | } else { |
754 | char bf[PATH_MAX]; | |
755 | ||
756 | if (machine__is_default_guest(machine)) | |
757 | vmlinux_name = symbol_conf.default_guest_vmlinux_name; | |
758 | if (!vmlinux_name) | |
759 | vmlinux_name = machine__mmap_name(machine, bf, | |
760 | sizeof(bf)); | |
761 | ||
459ce518 ACM |
762 | kernel = machine__findnew_kernel(machine, vmlinux_name, |
763 | "[guest.kernel]", | |
764 | DSO_TYPE_GUEST_KERNEL); | |
3f067dca ACM |
765 | } |
766 | ||
767 | if (kernel != NULL && (!kernel->has_build_id)) | |
768 | dso__read_running_kernel_build_id(kernel, machine); | |
769 | ||
770 | return kernel; | |
771 | } | |
772 | ||
773 | struct process_args { | |
774 | u64 start; | |
775 | }; | |
776 | ||
15a0a870 AH |
777 | static void machine__get_kallsyms_filename(struct machine *machine, char *buf, |
778 | size_t bufsz) | |
779 | { | |
780 | if (machine__is_default_guest(machine)) | |
781 | scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms); | |
782 | else | |
783 | scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir); | |
784 | } | |
785 | ||
a93f0e55 SQ |
786 | const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL}; |
787 | ||
788 | /* Figure out the start address of kernel map from /proc/kallsyms. | |
789 | * Returns the name of the start symbol in *symbol_name. Pass in NULL as | |
790 | * symbol_name if it's not that important. | |
791 | */ | |
b843f62a ACM |
792 | static int machine__get_running_kernel_start(struct machine *machine, |
793 | const char **symbol_name, u64 *start) | |
3f067dca | 794 | { |
15a0a870 | 795 | char filename[PATH_MAX]; |
b843f62a | 796 | int i, err = -1; |
a93f0e55 SQ |
797 | const char *name; |
798 | u64 addr = 0; | |
3f067dca | 799 | |
15a0a870 | 800 | machine__get_kallsyms_filename(machine, filename, PATH_MAX); |
3f067dca ACM |
801 | |
802 | if (symbol__restricted_filename(filename, "/proc/kallsyms")) | |
803 | return 0; | |
804 | ||
a93f0e55 | 805 | for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) { |
b843f62a ACM |
806 | err = kallsyms__get_function_start(filename, name, &addr); |
807 | if (!err) | |
a93f0e55 SQ |
808 | break; |
809 | } | |
810 | ||
b843f62a ACM |
811 | if (err) |
812 | return -1; | |
813 | ||
a93f0e55 SQ |
814 | if (symbol_name) |
815 | *symbol_name = name; | |
3f067dca | 816 | |
b843f62a ACM |
817 | *start = addr; |
818 | return 0; | |
3f067dca ACM |
819 | } |
820 | ||
821 | int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel) | |
822 | { | |
a0b2f5af | 823 | int type; |
b843f62a ACM |
824 | u64 start = 0; |
825 | ||
826 | if (machine__get_running_kernel_start(machine, NULL, &start)) | |
827 | return -1; | |
3f067dca | 828 | |
cc1121ab MH |
829 | /* In case of renewal the kernel map, destroy previous one */ |
830 | machine__destroy_kernel_maps(machine); | |
831 | ||
3f067dca ACM |
832 | for (type = 0; type < MAP__NR_TYPES; ++type) { |
833 | struct kmap *kmap; | |
77e65977 | 834 | struct map *map; |
3f067dca ACM |
835 | |
836 | machine->vmlinux_maps[type] = map__new2(start, kernel, type); | |
837 | if (machine->vmlinux_maps[type] == NULL) | |
838 | return -1; | |
839 | ||
840 | machine->vmlinux_maps[type]->map_ip = | |
841 | machine->vmlinux_maps[type]->unmap_ip = | |
842 | identity__map_ip; | |
a5e813c6 | 843 | map = __machine__kernel_map(machine, type); |
77e65977 | 844 | kmap = map__kmap(map); |
ba92732e WN |
845 | if (!kmap) |
846 | return -1; | |
847 | ||
3f067dca | 848 | kmap->kmaps = &machine->kmaps; |
77e65977 | 849 | map_groups__insert(&machine->kmaps, map); |
3f067dca ACM |
850 | } |
851 | ||
852 | return 0; | |
853 | } | |
854 | ||
855 | void machine__destroy_kernel_maps(struct machine *machine) | |
856 | { | |
a0b2f5af | 857 | int type; |
3f067dca ACM |
858 | |
859 | for (type = 0; type < MAP__NR_TYPES; ++type) { | |
860 | struct kmap *kmap; | |
a5e813c6 | 861 | struct map *map = __machine__kernel_map(machine, type); |
3f067dca | 862 | |
77e65977 | 863 | if (map == NULL) |
3f067dca ACM |
864 | continue; |
865 | ||
77e65977 ACM |
866 | kmap = map__kmap(map); |
867 | map_groups__remove(&machine->kmaps, map); | |
ba92732e | 868 | if (kmap && kmap->ref_reloc_sym) { |
3f067dca ACM |
869 | /* |
870 | * ref_reloc_sym is shared among all maps, so free just | |
871 | * on one of them. | |
872 | */ | |
873 | if (type == MAP__FUNCTION) { | |
04662523 ACM |
874 | zfree((char **)&kmap->ref_reloc_sym->name); |
875 | zfree(&kmap->ref_reloc_sym); | |
876 | } else | |
877 | kmap->ref_reloc_sym = NULL; | |
3f067dca ACM |
878 | } |
879 | ||
e96e4078 | 880 | map__put(machine->vmlinux_maps[type]); |
3f067dca ACM |
881 | machine->vmlinux_maps[type] = NULL; |
882 | } | |
883 | } | |
884 | ||
876650e6 | 885 | int machines__create_guest_kernel_maps(struct machines *machines) |
3f067dca ACM |
886 | { |
887 | int ret = 0; | |
888 | struct dirent **namelist = NULL; | |
889 | int i, items = 0; | |
890 | char path[PATH_MAX]; | |
891 | pid_t pid; | |
892 | char *endp; | |
893 | ||
894 | if (symbol_conf.default_guest_vmlinux_name || | |
895 | symbol_conf.default_guest_modules || | |
896 | symbol_conf.default_guest_kallsyms) { | |
897 | machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID); | |
898 | } | |
899 | ||
900 | if (symbol_conf.guestmount) { | |
901 | items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL); | |
902 | if (items <= 0) | |
903 | return -ENOENT; | |
904 | for (i = 0; i < items; i++) { | |
905 | if (!isdigit(namelist[i]->d_name[0])) { | |
906 | /* Filter out . and .. */ | |
907 | continue; | |
908 | } | |
909 | pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10); | |
910 | if ((*endp != '\0') || | |
911 | (endp == namelist[i]->d_name) || | |
912 | (errno == ERANGE)) { | |
913 | pr_debug("invalid directory (%s). Skipping.\n", | |
914 | namelist[i]->d_name); | |
915 | continue; | |
916 | } | |
917 | sprintf(path, "%s/%s/proc/kallsyms", | |
918 | symbol_conf.guestmount, | |
919 | namelist[i]->d_name); | |
920 | ret = access(path, R_OK); | |
921 | if (ret) { | |
922 | pr_debug("Can't access file %s\n", path); | |
923 | goto failure; | |
924 | } | |
925 | machines__create_kernel_maps(machines, pid); | |
926 | } | |
927 | failure: | |
928 | free(namelist); | |
929 | } | |
930 | ||
931 | return ret; | |
932 | } | |
933 | ||
876650e6 | 934 | void machines__destroy_kernel_maps(struct machines *machines) |
3f067dca | 935 | { |
876650e6 ACM |
936 | struct rb_node *next = rb_first(&machines->guests); |
937 | ||
938 | machine__destroy_kernel_maps(&machines->host); | |
3f067dca ACM |
939 | |
940 | while (next) { | |
941 | struct machine *pos = rb_entry(next, struct machine, rb_node); | |
942 | ||
943 | next = rb_next(&pos->rb_node); | |
876650e6 | 944 | rb_erase(&pos->rb_node, &machines->guests); |
3f067dca ACM |
945 | machine__delete(pos); |
946 | } | |
947 | } | |
948 | ||
876650e6 | 949 | int machines__create_kernel_maps(struct machines *machines, pid_t pid) |
3f067dca ACM |
950 | { |
951 | struct machine *machine = machines__findnew(machines, pid); | |
952 | ||
953 | if (machine == NULL) | |
954 | return -1; | |
955 | ||
956 | return machine__create_kernel_maps(machine); | |
957 | } | |
958 | ||
e02092b9 | 959 | int __machine__load_kallsyms(struct machine *machine, const char *filename, |
be39db9f | 960 | enum map_type type, bool no_kcore) |
3f067dca | 961 | { |
a5e813c6 | 962 | struct map *map = machine__kernel_map(machine); |
be39db9f | 963 | int ret = __dso__load_kallsyms(map->dso, filename, map, no_kcore); |
3f067dca ACM |
964 | |
965 | if (ret > 0) { | |
966 | dso__set_loaded(map->dso, type); | |
967 | /* | |
968 | * Since /proc/kallsyms will have multiple sessions for the | |
969 | * kernel, with modules between them, fixup the end of all | |
970 | * sections. | |
971 | */ | |
972 | __map_groups__fixup_end(&machine->kmaps, type); | |
973 | } | |
974 | ||
975 | return ret; | |
976 | } | |
977 | ||
e02092b9 | 978 | int machine__load_kallsyms(struct machine *machine, const char *filename, |
be39db9f | 979 | enum map_type type) |
e02092b9 | 980 | { |
be39db9f | 981 | return __machine__load_kallsyms(machine, filename, type, false); |
e02092b9 ACM |
982 | } |
983 | ||
be39db9f | 984 | int machine__load_vmlinux_path(struct machine *machine, enum map_type type) |
3f067dca | 985 | { |
a5e813c6 | 986 | struct map *map = machine__kernel_map(machine); |
be39db9f | 987 | int ret = dso__load_vmlinux_path(map->dso, map); |
3f067dca | 988 | |
39b12f78 | 989 | if (ret > 0) |
3f067dca | 990 | dso__set_loaded(map->dso, type); |
3f067dca ACM |
991 | |
992 | return ret; | |
993 | } | |
994 | ||
995 | static void map_groups__fixup_end(struct map_groups *mg) | |
996 | { | |
997 | int i; | |
998 | for (i = 0; i < MAP__NR_TYPES; ++i) | |
999 | __map_groups__fixup_end(mg, i); | |
1000 | } | |
1001 | ||
1002 | static char *get_kernel_version(const char *root_dir) | |
1003 | { | |
1004 | char version[PATH_MAX]; | |
1005 | FILE *file; | |
1006 | char *name, *tmp; | |
1007 | const char *prefix = "Linux version "; | |
1008 | ||
1009 | sprintf(version, "%s/proc/version", root_dir); | |
1010 | file = fopen(version, "r"); | |
1011 | if (!file) | |
1012 | return NULL; | |
1013 | ||
1014 | version[0] = '\0'; | |
1015 | tmp = fgets(version, sizeof(version), file); | |
1016 | fclose(file); | |
1017 | ||
1018 | name = strstr(version, prefix); | |
1019 | if (!name) | |
1020 | return NULL; | |
1021 | name += strlen(prefix); | |
1022 | tmp = strchr(name, ' '); | |
1023 | if (tmp) | |
1024 | *tmp = '\0'; | |
1025 | ||
1026 | return strdup(name); | |
1027 | } | |
1028 | ||
bb58a8a4 JO |
1029 | static bool is_kmod_dso(struct dso *dso) |
1030 | { | |
1031 | return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE || | |
1032 | dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE; | |
1033 | } | |
1034 | ||
1035 | static int map_groups__set_module_path(struct map_groups *mg, const char *path, | |
1036 | struct kmod_path *m) | |
1037 | { | |
1038 | struct map *map; | |
1039 | char *long_name; | |
1040 | ||
1041 | map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name); | |
1042 | if (map == NULL) | |
1043 | return 0; | |
1044 | ||
1045 | long_name = strdup(path); | |
1046 | if (long_name == NULL) | |
1047 | return -ENOMEM; | |
1048 | ||
1049 | dso__set_long_name(map->dso, long_name, true); | |
1050 | dso__kernel_module_get_build_id(map->dso, ""); | |
1051 | ||
1052 | /* | |
1053 | * Full name could reveal us kmod compression, so | |
1054 | * we need to update the symtab_type if needed. | |
1055 | */ | |
1056 | if (m->comp && is_kmod_dso(map->dso)) | |
1057 | map->dso->symtab_type++; | |
1058 | ||
1059 | return 0; | |
1060 | } | |
1061 | ||
3f067dca | 1062 | static int map_groups__set_modules_path_dir(struct map_groups *mg, |
61d4290c | 1063 | const char *dir_name, int depth) |
3f067dca ACM |
1064 | { |
1065 | struct dirent *dent; | |
1066 | DIR *dir = opendir(dir_name); | |
1067 | int ret = 0; | |
1068 | ||
1069 | if (!dir) { | |
1070 | pr_debug("%s: cannot open %s dir\n", __func__, dir_name); | |
1071 | return -1; | |
1072 | } | |
1073 | ||
1074 | while ((dent = readdir(dir)) != NULL) { | |
1075 | char path[PATH_MAX]; | |
1076 | struct stat st; | |
1077 | ||
1078 | /*sshfs might return bad dent->d_type, so we have to stat*/ | |
1079 | snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name); | |
1080 | if (stat(path, &st)) | |
1081 | continue; | |
1082 | ||
1083 | if (S_ISDIR(st.st_mode)) { | |
1084 | if (!strcmp(dent->d_name, ".") || | |
1085 | !strcmp(dent->d_name, "..")) | |
1086 | continue; | |
1087 | ||
61d4290c RY |
1088 | /* Do not follow top-level source and build symlinks */ |
1089 | if (depth == 0) { | |
1090 | if (!strcmp(dent->d_name, "source") || | |
1091 | !strcmp(dent->d_name, "build")) | |
1092 | continue; | |
1093 | } | |
1094 | ||
1095 | ret = map_groups__set_modules_path_dir(mg, path, | |
1096 | depth + 1); | |
3f067dca ACM |
1097 | if (ret < 0) |
1098 | goto out; | |
1099 | } else { | |
bb58a8a4 | 1100 | struct kmod_path m; |
3f067dca | 1101 | |
bb58a8a4 JO |
1102 | ret = kmod_path__parse_name(&m, dent->d_name); |
1103 | if (ret) | |
1104 | goto out; | |
c00c48fc | 1105 | |
bb58a8a4 JO |
1106 | if (m.kmod) |
1107 | ret = map_groups__set_module_path(mg, path, &m); | |
c00c48fc | 1108 | |
bb58a8a4 | 1109 | free(m.name); |
3f067dca | 1110 | |
bb58a8a4 | 1111 | if (ret) |
3f067dca | 1112 | goto out; |
3f067dca ACM |
1113 | } |
1114 | } | |
1115 | ||
1116 | out: | |
1117 | closedir(dir); | |
1118 | return ret; | |
1119 | } | |
1120 | ||
1121 | static int machine__set_modules_path(struct machine *machine) | |
1122 | { | |
1123 | char *version; | |
1124 | char modules_path[PATH_MAX]; | |
1125 | ||
1126 | version = get_kernel_version(machine->root_dir); | |
1127 | if (!version) | |
1128 | return -1; | |
1129 | ||
61d4290c | 1130 | snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s", |
3f067dca ACM |
1131 | machine->root_dir, version); |
1132 | free(version); | |
1133 | ||
61d4290c | 1134 | return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0); |
3f067dca | 1135 | } |
203d8a4a SSG |
1136 | int __weak arch__fix_module_text_start(u64 *start __maybe_unused, |
1137 | const char *name __maybe_unused) | |
1138 | { | |
1139 | return 0; | |
1140 | } | |
3f067dca | 1141 | |
9ad4652b TR |
1142 | static int machine__create_module(void *arg, const char *name, u64 start, |
1143 | u64 size) | |
3f067dca | 1144 | { |
316d70d6 | 1145 | struct machine *machine = arg; |
3f067dca | 1146 | struct map *map; |
316d70d6 | 1147 | |
203d8a4a SSG |
1148 | if (arch__fix_module_text_start(&start, name) < 0) |
1149 | return -1; | |
1150 | ||
9f2de315 | 1151 | map = machine__findnew_module_map(machine, start, name); |
316d70d6 AH |
1152 | if (map == NULL) |
1153 | return -1; | |
9ad4652b | 1154 | map->end = start + size; |
316d70d6 AH |
1155 | |
1156 | dso__kernel_module_get_build_id(map->dso, machine->root_dir); | |
1157 | ||
1158 | return 0; | |
1159 | } | |
1160 | ||
1161 | static int machine__create_modules(struct machine *machine) | |
1162 | { | |
3f067dca ACM |
1163 | const char *modules; |
1164 | char path[PATH_MAX]; | |
1165 | ||
f4be904d | 1166 | if (machine__is_default_guest(machine)) { |
3f067dca | 1167 | modules = symbol_conf.default_guest_modules; |
f4be904d AH |
1168 | } else { |
1169 | snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir); | |
3f067dca ACM |
1170 | modules = path; |
1171 | } | |
1172 | ||
aa7fe3b0 | 1173 | if (symbol__restricted_filename(modules, "/proc/modules")) |
3f067dca ACM |
1174 | return -1; |
1175 | ||
316d70d6 | 1176 | if (modules__parse(modules, machine, machine__create_module)) |
3f067dca ACM |
1177 | return -1; |
1178 | ||
316d70d6 AH |
1179 | if (!machine__set_modules_path(machine)) |
1180 | return 0; | |
3f067dca | 1181 | |
316d70d6 | 1182 | pr_debug("Problems setting modules path maps, continuing anyway...\n"); |
3f067dca | 1183 | |
8f76fcd9 | 1184 | return 0; |
3f067dca ACM |
1185 | } |
1186 | ||
1187 | int machine__create_kernel_maps(struct machine *machine) | |
1188 | { | |
1189 | struct dso *kernel = machine__get_kernel(machine); | |
b843f62a ACM |
1190 | const char *name = NULL; |
1191 | u64 addr = 0; | |
1154c957 MH |
1192 | int ret; |
1193 | ||
45e90056 | 1194 | if (kernel == NULL) |
5512cf24 | 1195 | return -1; |
3f067dca | 1196 | |
1154c957 MH |
1197 | ret = __machine__create_kernel_maps(machine, kernel); |
1198 | dso__put(kernel); | |
1199 | if (ret < 0) | |
3f067dca ACM |
1200 | return -1; |
1201 | ||
1202 | if (symbol_conf.use_modules && machine__create_modules(machine) < 0) { | |
1203 | if (machine__is_host(machine)) | |
1204 | pr_debug("Problems creating module maps, " | |
1205 | "continuing anyway...\n"); | |
1206 | else | |
1207 | pr_debug("Problems creating module maps for guest %d, " | |
1208 | "continuing anyway...\n", machine->pid); | |
1209 | } | |
1210 | ||
1211 | /* | |
1212 | * Now that we have all the maps created, just set the ->end of them: | |
1213 | */ | |
1214 | map_groups__fixup_end(&machine->kmaps); | |
5512cf24 | 1215 | |
3f938ee2 JO |
1216 | if (!machine__get_running_kernel_start(machine, &name, &addr)) { |
1217 | if (name && | |
1218 | maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name, addr)) { | |
1219 | machine__destroy_kernel_maps(machine); | |
1220 | return -1; | |
1221 | } | |
5512cf24 AH |
1222 | } |
1223 | ||
3f067dca ACM |
1224 | return 0; |
1225 | } | |
1226 | ||
b0a7d1a0 ACM |
1227 | static void machine__set_kernel_mmap_len(struct machine *machine, |
1228 | union perf_event *event) | |
1229 | { | |
4552cf0f NK |
1230 | int i; |
1231 | ||
1232 | for (i = 0; i < MAP__NR_TYPES; i++) { | |
1233 | machine->vmlinux_maps[i]->start = event->mmap.start; | |
1234 | machine->vmlinux_maps[i]->end = (event->mmap.start + | |
1235 | event->mmap.len); | |
1236 | /* | |
1237 | * Be a bit paranoid here, some perf.data file came with | |
1238 | * a zero sized synthesized MMAP event for the kernel. | |
1239 | */ | |
1240 | if (machine->vmlinux_maps[i]->end == 0) | |
1241 | machine->vmlinux_maps[i]->end = ~0ULL; | |
1242 | } | |
b0a7d1a0 ACM |
1243 | } |
1244 | ||
8e0cf965 AH |
1245 | static bool machine__uses_kcore(struct machine *machine) |
1246 | { | |
1247 | struct dso *dso; | |
1248 | ||
3d39ac53 | 1249 | list_for_each_entry(dso, &machine->dsos.head, node) { |
8e0cf965 AH |
1250 | if (dso__is_kcore(dso)) |
1251 | return true; | |
1252 | } | |
1253 | ||
1254 | return false; | |
1255 | } | |
1256 | ||
b0a7d1a0 ACM |
1257 | static int machine__process_kernel_mmap_event(struct machine *machine, |
1258 | union perf_event *event) | |
1259 | { | |
1260 | struct map *map; | |
1261 | char kmmap_prefix[PATH_MAX]; | |
1262 | enum dso_kernel_type kernel_type; | |
1263 | bool is_kernel_mmap; | |
1264 | ||
8e0cf965 AH |
1265 | /* If we have maps from kcore then we do not need or want any others */ |
1266 | if (machine__uses_kcore(machine)) | |
1267 | return 0; | |
1268 | ||
b0a7d1a0 ACM |
1269 | machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix)); |
1270 | if (machine__is_host(machine)) | |
1271 | kernel_type = DSO_TYPE_KERNEL; | |
1272 | else | |
1273 | kernel_type = DSO_TYPE_GUEST_KERNEL; | |
1274 | ||
1275 | is_kernel_mmap = memcmp(event->mmap.filename, | |
1276 | kmmap_prefix, | |
1277 | strlen(kmmap_prefix) - 1) == 0; | |
1278 | if (event->mmap.filename[0] == '/' || | |
1279 | (!is_kernel_mmap && event->mmap.filename[0] == '[')) { | |
9f2de315 ACM |
1280 | map = machine__findnew_module_map(machine, event->mmap.start, |
1281 | event->mmap.filename); | |
b0a7d1a0 ACM |
1282 | if (map == NULL) |
1283 | goto out_problem; | |
1284 | ||
b0a7d1a0 ACM |
1285 | map->end = map->start + event->mmap.len; |
1286 | } else if (is_kernel_mmap) { | |
1287 | const char *symbol_name = (event->mmap.filename + | |
1288 | strlen(kmmap_prefix)); | |
1289 | /* | |
1290 | * Should be there already, from the build-id table in | |
1291 | * the header. | |
1292 | */ | |
b837a8bd NK |
1293 | struct dso *kernel = NULL; |
1294 | struct dso *dso; | |
1295 | ||
e8807844 ACM |
1296 | pthread_rwlock_rdlock(&machine->dsos.lock); |
1297 | ||
3d39ac53 | 1298 | list_for_each_entry(dso, &machine->dsos.head, node) { |
1f121b03 WN |
1299 | |
1300 | /* | |
1301 | * The cpumode passed to is_kernel_module is not the | |
1302 | * cpumode of *this* event. If we insist on passing | |
1303 | * correct cpumode to is_kernel_module, we should | |
1304 | * record the cpumode when we adding this dso to the | |
1305 | * linked list. | |
1306 | * | |
1307 | * However we don't really need passing correct | |
1308 | * cpumode. We know the correct cpumode must be kernel | |
1309 | * mode (if not, we should not link it onto kernel_dsos | |
1310 | * list). | |
1311 | * | |
1312 | * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN. | |
1313 | * is_kernel_module() treats it as a kernel cpumode. | |
1314 | */ | |
1315 | ||
1316 | if (!dso->kernel || | |
1317 | is_kernel_module(dso->long_name, | |
1318 | PERF_RECORD_MISC_CPUMODE_UNKNOWN)) | |
b837a8bd NK |
1319 | continue; |
1320 | ||
1f121b03 | 1321 | |
b837a8bd NK |
1322 | kernel = dso; |
1323 | break; | |
1324 | } | |
1325 | ||
e8807844 ACM |
1326 | pthread_rwlock_unlock(&machine->dsos.lock); |
1327 | ||
b837a8bd | 1328 | if (kernel == NULL) |
aa7cc2ae | 1329 | kernel = machine__findnew_dso(machine, kmmap_prefix); |
b0a7d1a0 ACM |
1330 | if (kernel == NULL) |
1331 | goto out_problem; | |
1332 | ||
1333 | kernel->kernel = kernel_type; | |
d3a7c489 ACM |
1334 | if (__machine__create_kernel_maps(machine, kernel) < 0) { |
1335 | dso__put(kernel); | |
b0a7d1a0 | 1336 | goto out_problem; |
d3a7c489 | 1337 | } |
b0a7d1a0 | 1338 | |
330dfa22 NK |
1339 | if (strstr(kernel->long_name, "vmlinux")) |
1340 | dso__set_short_name(kernel, "[kernel.vmlinux]", false); | |
96d78059 | 1341 | |
b0a7d1a0 ACM |
1342 | machine__set_kernel_mmap_len(machine, event); |
1343 | ||
1344 | /* | |
1345 | * Avoid using a zero address (kptr_restrict) for the ref reloc | |
1346 | * symbol. Effectively having zero here means that at record | |
1347 | * time /proc/sys/kernel/kptr_restrict was non zero. | |
1348 | */ | |
1349 | if (event->mmap.pgoff != 0) { | |
1350 | maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, | |
1351 | symbol_name, | |
1352 | event->mmap.pgoff); | |
1353 | } | |
1354 | ||
1355 | if (machine__is_default_guest(machine)) { | |
1356 | /* | |
1357 | * preload dso of guest kernel and modules | |
1358 | */ | |
be39db9f | 1359 | dso__load(kernel, machine__kernel_map(machine)); |
b0a7d1a0 ACM |
1360 | } |
1361 | } | |
1362 | return 0; | |
1363 | out_problem: | |
1364 | return -1; | |
1365 | } | |
1366 | ||
5c5e854b | 1367 | int machine__process_mmap2_event(struct machine *machine, |
162f0bef | 1368 | union perf_event *event, |
473398a2 | 1369 | struct perf_sample *sample) |
5c5e854b | 1370 | { |
5c5e854b SE |
1371 | struct thread *thread; |
1372 | struct map *map; | |
1373 | enum map_type type; | |
1374 | int ret = 0; | |
1375 | ||
1376 | if (dump_trace) | |
1377 | perf_event__fprintf_mmap2(event, stdout); | |
1378 | ||
473398a2 ACM |
1379 | if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL || |
1380 | sample->cpumode == PERF_RECORD_MISC_KERNEL) { | |
5c5e854b SE |
1381 | ret = machine__process_kernel_mmap_event(machine, event); |
1382 | if (ret < 0) | |
1383 | goto out_problem; | |
1384 | return 0; | |
1385 | } | |
1386 | ||
1387 | thread = machine__findnew_thread(machine, event->mmap2.pid, | |
11c9abf2 | 1388 | event->mmap2.tid); |
5c5e854b SE |
1389 | if (thread == NULL) |
1390 | goto out_problem; | |
1391 | ||
1392 | if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) | |
1393 | type = MAP__VARIABLE; | |
1394 | else | |
1395 | type = MAP__FUNCTION; | |
1396 | ||
2a03068c | 1397 | map = map__new(machine, event->mmap2.start, |
5c5e854b | 1398 | event->mmap2.len, event->mmap2.pgoff, |
bf2e710b | 1399 | event->mmap2.maj, |
5c5e854b SE |
1400 | event->mmap2.min, event->mmap2.ino, |
1401 | event->mmap2.ino_generation, | |
7ef80703 DZ |
1402 | event->mmap2.prot, |
1403 | event->mmap2.flags, | |
5835edda | 1404 | event->mmap2.filename, type, thread); |
5c5e854b SE |
1405 | |
1406 | if (map == NULL) | |
b91fc39f | 1407 | goto out_problem_map; |
5c5e854b | 1408 | |
8132a2a8 HK |
1409 | ret = thread__insert_map(thread, map); |
1410 | if (ret) | |
1411 | goto out_problem_insert; | |
1412 | ||
b91fc39f | 1413 | thread__put(thread); |
84c2cafa | 1414 | map__put(map); |
5c5e854b SE |
1415 | return 0; |
1416 | ||
8132a2a8 HK |
1417 | out_problem_insert: |
1418 | map__put(map); | |
b91fc39f ACM |
1419 | out_problem_map: |
1420 | thread__put(thread); | |
5c5e854b SE |
1421 | out_problem: |
1422 | dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n"); | |
1423 | return 0; | |
1424 | } | |
1425 | ||
162f0bef | 1426 | int machine__process_mmap_event(struct machine *machine, union perf_event *event, |
473398a2 | 1427 | struct perf_sample *sample) |
b0a7d1a0 | 1428 | { |
b0a7d1a0 ACM |
1429 | struct thread *thread; |
1430 | struct map *map; | |
bad40917 | 1431 | enum map_type type; |
b0a7d1a0 ACM |
1432 | int ret = 0; |
1433 | ||
1434 | if (dump_trace) | |
1435 | perf_event__fprintf_mmap(event, stdout); | |
1436 | ||
473398a2 ACM |
1437 | if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL || |
1438 | sample->cpumode == PERF_RECORD_MISC_KERNEL) { | |
b0a7d1a0 ACM |
1439 | ret = machine__process_kernel_mmap_event(machine, event); |
1440 | if (ret < 0) | |
1441 | goto out_problem; | |
1442 | return 0; | |
1443 | } | |
1444 | ||
314add6b | 1445 | thread = machine__findnew_thread(machine, event->mmap.pid, |
11c9abf2 | 1446 | event->mmap.tid); |
b0a7d1a0 ACM |
1447 | if (thread == NULL) |
1448 | goto out_problem; | |
bad40917 SE |
1449 | |
1450 | if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) | |
1451 | type = MAP__VARIABLE; | |
1452 | else | |
1453 | type = MAP__FUNCTION; | |
1454 | ||
2a03068c | 1455 | map = map__new(machine, event->mmap.start, |
b0a7d1a0 | 1456 | event->mmap.len, event->mmap.pgoff, |
bf2e710b | 1457 | 0, 0, 0, 0, 0, 0, |
5c5e854b | 1458 | event->mmap.filename, |
5835edda | 1459 | type, thread); |
bad40917 | 1460 | |
b0a7d1a0 | 1461 | if (map == NULL) |
b91fc39f | 1462 | goto out_problem_map; |
b0a7d1a0 | 1463 | |
8132a2a8 HK |
1464 | ret = thread__insert_map(thread, map); |
1465 | if (ret) | |
1466 | goto out_problem_insert; | |
1467 | ||
b91fc39f | 1468 | thread__put(thread); |
84c2cafa | 1469 | map__put(map); |
b0a7d1a0 ACM |
1470 | return 0; |
1471 | ||
8132a2a8 HK |
1472 | out_problem_insert: |
1473 | map__put(map); | |
b91fc39f ACM |
1474 | out_problem_map: |
1475 | thread__put(thread); | |
b0a7d1a0 ACM |
1476 | out_problem: |
1477 | dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n"); | |
1478 | return 0; | |
1479 | } | |
1480 | ||
b91fc39f | 1481 | static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock) |
236a3bbd | 1482 | { |
f3b623b8 | 1483 | if (machine->last_match == th) |
0ceb8f6e | 1484 | machine->last_match = NULL; |
f3b623b8 | 1485 | |
e34f5b11 | 1486 | BUG_ON(refcount_read(&th->refcnt) == 0); |
b91fc39f ACM |
1487 | if (lock) |
1488 | pthread_rwlock_wrlock(&machine->threads_lock); | |
0170b14f | 1489 | rb_erase_init(&th->rb_node, &machine->threads); |
b91fc39f | 1490 | RB_CLEAR_NODE(&th->rb_node); |
d2c11034 | 1491 | --machine->nr_threads; |
236a3bbd | 1492 | /* |
f3b623b8 ACM |
1493 | * Move it first to the dead_threads list, then drop the reference, |
1494 | * if this is the last reference, then the thread__delete destructor | |
1495 | * will be called and we will remove it from the dead_threads list. | |
236a3bbd DA |
1496 | */ |
1497 | list_add_tail(&th->node, &machine->dead_threads); | |
b91fc39f ACM |
1498 | if (lock) |
1499 | pthread_rwlock_unlock(&machine->threads_lock); | |
f3b623b8 | 1500 | thread__put(th); |
236a3bbd DA |
1501 | } |
1502 | ||
b91fc39f ACM |
1503 | void machine__remove_thread(struct machine *machine, struct thread *th) |
1504 | { | |
1505 | return __machine__remove_thread(machine, th, true); | |
1506 | } | |
1507 | ||
162f0bef FW |
1508 | int machine__process_fork_event(struct machine *machine, union perf_event *event, |
1509 | struct perf_sample *sample) | |
b0a7d1a0 | 1510 | { |
d75e6097 JO |
1511 | struct thread *thread = machine__find_thread(machine, |
1512 | event->fork.pid, | |
1513 | event->fork.tid); | |
314add6b AH |
1514 | struct thread *parent = machine__findnew_thread(machine, |
1515 | event->fork.ppid, | |
1516 | event->fork.ptid); | |
b91fc39f | 1517 | int err = 0; |
b0a7d1a0 | 1518 | |
5cb73340 AH |
1519 | if (dump_trace) |
1520 | perf_event__fprintf_task(event, stdout); | |
1521 | ||
1522 | /* | |
1523 | * There may be an existing thread that is not actually the parent, | |
1524 | * either because we are processing events out of order, or because the | |
1525 | * (fork) event that would have removed the thread was lost. Assume the | |
1526 | * latter case and continue on as best we can. | |
1527 | */ | |
1528 | if (parent->pid_ != (pid_t)event->fork.ppid) { | |
1529 | dump_printf("removing erroneous parent thread %d/%d\n", | |
1530 | parent->pid_, parent->tid); | |
1531 | machine__remove_thread(machine, parent); | |
1532 | thread__put(parent); | |
1533 | parent = machine__findnew_thread(machine, event->fork.ppid, | |
1534 | event->fork.ptid); | |
1535 | } | |
1536 | ||
236a3bbd | 1537 | /* if a thread currently exists for the thread id remove it */ |
b91fc39f | 1538 | if (thread != NULL) { |
236a3bbd | 1539 | machine__remove_thread(machine, thread); |
b91fc39f ACM |
1540 | thread__put(thread); |
1541 | } | |
236a3bbd | 1542 | |
314add6b AH |
1543 | thread = machine__findnew_thread(machine, event->fork.pid, |
1544 | event->fork.tid); | |
b0a7d1a0 ACM |
1545 | |
1546 | if (thread == NULL || parent == NULL || | |
162f0bef | 1547 | thread__fork(thread, parent, sample->time) < 0) { |
b0a7d1a0 | 1548 | dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n"); |
b91fc39f | 1549 | err = -1; |
b0a7d1a0 | 1550 | } |
b91fc39f ACM |
1551 | thread__put(thread); |
1552 | thread__put(parent); | |
b0a7d1a0 | 1553 | |
b91fc39f | 1554 | return err; |
b0a7d1a0 ACM |
1555 | } |
1556 | ||
162f0bef FW |
1557 | int machine__process_exit_event(struct machine *machine, union perf_event *event, |
1558 | struct perf_sample *sample __maybe_unused) | |
b0a7d1a0 | 1559 | { |
d75e6097 JO |
1560 | struct thread *thread = machine__find_thread(machine, |
1561 | event->fork.pid, | |
1562 | event->fork.tid); | |
b0a7d1a0 ACM |
1563 | |
1564 | if (dump_trace) | |
1565 | perf_event__fprintf_task(event, stdout); | |
1566 | ||
b91fc39f | 1567 | if (thread != NULL) { |
236a3bbd | 1568 | thread__exited(thread); |
b91fc39f ACM |
1569 | thread__put(thread); |
1570 | } | |
b0a7d1a0 ACM |
1571 | |
1572 | return 0; | |
1573 | } | |
1574 | ||
162f0bef FW |
1575 | int machine__process_event(struct machine *machine, union perf_event *event, |
1576 | struct perf_sample *sample) | |
b0a7d1a0 ACM |
1577 | { |
1578 | int ret; | |
1579 | ||
1580 | switch (event->header.type) { | |
1581 | case PERF_RECORD_COMM: | |
162f0bef | 1582 | ret = machine__process_comm_event(machine, event, sample); break; |
b0a7d1a0 | 1583 | case PERF_RECORD_MMAP: |
162f0bef | 1584 | ret = machine__process_mmap_event(machine, event, sample); break; |
f3b3614a HB |
1585 | case PERF_RECORD_NAMESPACES: |
1586 | ret = machine__process_namespaces_event(machine, event, sample); break; | |
5c5e854b | 1587 | case PERF_RECORD_MMAP2: |
162f0bef | 1588 | ret = machine__process_mmap2_event(machine, event, sample); break; |
b0a7d1a0 | 1589 | case PERF_RECORD_FORK: |
162f0bef | 1590 | ret = machine__process_fork_event(machine, event, sample); break; |
b0a7d1a0 | 1591 | case PERF_RECORD_EXIT: |
162f0bef | 1592 | ret = machine__process_exit_event(machine, event, sample); break; |
b0a7d1a0 | 1593 | case PERF_RECORD_LOST: |
162f0bef | 1594 | ret = machine__process_lost_event(machine, event, sample); break; |
4a96f7a0 AH |
1595 | case PERF_RECORD_AUX: |
1596 | ret = machine__process_aux_event(machine, event); break; | |
0ad21f68 | 1597 | case PERF_RECORD_ITRACE_START: |
ceb92913 | 1598 | ret = machine__process_itrace_start_event(machine, event); break; |
c4937a91 KL |
1599 | case PERF_RECORD_LOST_SAMPLES: |
1600 | ret = machine__process_lost_samples_event(machine, event, sample); break; | |
0286039f AH |
1601 | case PERF_RECORD_SWITCH: |
1602 | case PERF_RECORD_SWITCH_CPU_WIDE: | |
1603 | ret = machine__process_switch_event(machine, event); break; | |
b0a7d1a0 ACM |
1604 | default: |
1605 | ret = -1; | |
1606 | break; | |
1607 | } | |
1608 | ||
1609 | return ret; | |
1610 | } | |
3f067dca | 1611 | |
b21484f1 | 1612 | static bool symbol__match_regex(struct symbol *sym, regex_t *regex) |
3f067dca | 1613 | { |
a7c3899c | 1614 | if (!regexec(regex, sym->name, 0, NULL, 0)) |
3f067dca | 1615 | return 1; |
3f067dca ACM |
1616 | return 0; |
1617 | } | |
1618 | ||
bb871a9c | 1619 | static void ip__resolve_ams(struct thread *thread, |
3f067dca ACM |
1620 | struct addr_map_symbol *ams, |
1621 | u64 ip) | |
1622 | { | |
1623 | struct addr_location al; | |
3f067dca ACM |
1624 | |
1625 | memset(&al, 0, sizeof(al)); | |
52a3cb8c ACM |
1626 | /* |
1627 | * We cannot use the header.misc hint to determine whether a | |
1628 | * branch stack address is user, kernel, guest, hypervisor. | |
1629 | * Branches may straddle the kernel/user/hypervisor boundaries. | |
1630 | * Thus, we have to try consecutively until we find a match | |
1631 | * or else, the symbol is unknown | |
1632 | */ | |
bb871a9c | 1633 | thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al); |
3f067dca | 1634 | |
3f067dca ACM |
1635 | ams->addr = ip; |
1636 | ams->al_addr = al.addr; | |
1637 | ams->sym = al.sym; | |
1638 | ams->map = al.map; | |
8780fb25 | 1639 | ams->phys_addr = 0; |
3f067dca ACM |
1640 | } |
1641 | ||
bb871a9c | 1642 | static void ip__resolve_data(struct thread *thread, |
8780fb25 KL |
1643 | u8 m, struct addr_map_symbol *ams, |
1644 | u64 addr, u64 phys_addr) | |
98a3b32c SE |
1645 | { |
1646 | struct addr_location al; | |
1647 | ||
1648 | memset(&al, 0, sizeof(al)); | |
1649 | ||
bb871a9c | 1650 | thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al); |
06b2afc0 DZ |
1651 | if (al.map == NULL) { |
1652 | /* | |
1653 | * some shared data regions have execute bit set which puts | |
1654 | * their mapping in the MAP__FUNCTION type array. | |
1655 | * Check there as a fallback option before dropping the sample. | |
1656 | */ | |
bb871a9c | 1657 | thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al); |
06b2afc0 DZ |
1658 | } |
1659 | ||
98a3b32c SE |
1660 | ams->addr = addr; |
1661 | ams->al_addr = al.addr; | |
1662 | ams->sym = al.sym; | |
1663 | ams->map = al.map; | |
8780fb25 | 1664 | ams->phys_addr = phys_addr; |
98a3b32c SE |
1665 | } |
1666 | ||
e80faac0 ACM |
1667 | struct mem_info *sample__resolve_mem(struct perf_sample *sample, |
1668 | struct addr_location *al) | |
98a3b32c SE |
1669 | { |
1670 | struct mem_info *mi = zalloc(sizeof(*mi)); | |
1671 | ||
1672 | if (!mi) | |
1673 | return NULL; | |
1674 | ||
bb871a9c | 1675 | ip__resolve_ams(al->thread, &mi->iaddr, sample->ip); |
8780fb25 KL |
1676 | ip__resolve_data(al->thread, al->cpumode, &mi->daddr, |
1677 | sample->addr, sample->phys_addr); | |
98a3b32c SE |
1678 | mi->data_src.val = sample->data_src; |
1679 | ||
1680 | return mi; | |
1681 | } | |
1682 | ||
c4ee0625 JY |
1683 | struct iterations { |
1684 | int nr_loop_iter; | |
1685 | u64 cycles; | |
1686 | }; | |
1687 | ||
37592b8a | 1688 | static int add_callchain_ip(struct thread *thread, |
91d7b2de | 1689 | struct callchain_cursor *cursor, |
37592b8a AK |
1690 | struct symbol **parent, |
1691 | struct addr_location *root_al, | |
73dbcd65 | 1692 | u8 *cpumode, |
410024db JY |
1693 | u64 ip, |
1694 | bool branch, | |
1695 | struct branch_flags *flags, | |
c4ee0625 | 1696 | struct iterations *iter, |
b851dd49 | 1697 | u64 branch_from) |
37592b8a AK |
1698 | { |
1699 | struct addr_location al; | |
c4ee0625 JY |
1700 | int nr_loop_iter = 0; |
1701 | u64 iter_cycles = 0; | |
37592b8a AK |
1702 | |
1703 | al.filtered = 0; | |
1704 | al.sym = NULL; | |
73dbcd65 | 1705 | if (!cpumode) { |
8b7bad58 AK |
1706 | thread__find_cpumode_addr_location(thread, MAP__FUNCTION, |
1707 | ip, &al); | |
73dbcd65 | 1708 | } else { |
2e77784b KL |
1709 | if (ip >= PERF_CONTEXT_MAX) { |
1710 | switch (ip) { | |
1711 | case PERF_CONTEXT_HV: | |
73dbcd65 | 1712 | *cpumode = PERF_RECORD_MISC_HYPERVISOR; |
2e77784b KL |
1713 | break; |
1714 | case PERF_CONTEXT_KERNEL: | |
73dbcd65 | 1715 | *cpumode = PERF_RECORD_MISC_KERNEL; |
2e77784b KL |
1716 | break; |
1717 | case PERF_CONTEXT_USER: | |
73dbcd65 | 1718 | *cpumode = PERF_RECORD_MISC_USER; |
2e77784b KL |
1719 | break; |
1720 | default: | |
1721 | pr_debug("invalid callchain context: " | |
1722 | "%"PRId64"\n", (s64) ip); | |
1723 | /* | |
1724 | * It seems the callchain is corrupted. | |
1725 | * Discard all. | |
1726 | */ | |
91d7b2de | 1727 | callchain_cursor_reset(cursor); |
2e77784b KL |
1728 | return 1; |
1729 | } | |
1730 | return 0; | |
1731 | } | |
73dbcd65 DH |
1732 | thread__find_addr_location(thread, *cpumode, MAP__FUNCTION, |
1733 | ip, &al); | |
2e77784b KL |
1734 | } |
1735 | ||
37592b8a | 1736 | if (al.sym != NULL) { |
de7e6a7c | 1737 | if (perf_hpp_list.parent && !*parent && |
37592b8a AK |
1738 | symbol__match_regex(al.sym, &parent_regex)) |
1739 | *parent = al.sym; | |
1740 | else if (have_ignore_callees && root_al && | |
1741 | symbol__match_regex(al.sym, &ignore_callees_regex)) { | |
1742 | /* Treat this symbol as the root, | |
1743 | forgetting its callees. */ | |
1744 | *root_al = al; | |
91d7b2de | 1745 | callchain_cursor_reset(cursor); |
37592b8a AK |
1746 | } |
1747 | } | |
1748 | ||
b49a8fe5 NK |
1749 | if (symbol_conf.hide_unresolved && al.sym == NULL) |
1750 | return 0; | |
c4ee0625 JY |
1751 | |
1752 | if (iter) { | |
1753 | nr_loop_iter = iter->nr_loop_iter; | |
1754 | iter_cycles = iter->cycles; | |
1755 | } | |
1756 | ||
410024db | 1757 | return callchain_cursor_append(cursor, al.addr, al.map, al.sym, |
c4ee0625 JY |
1758 | branch, flags, nr_loop_iter, |
1759 | iter_cycles, branch_from); | |
37592b8a AK |
1760 | } |
1761 | ||
644f2df2 ACM |
1762 | struct branch_info *sample__resolve_bstack(struct perf_sample *sample, |
1763 | struct addr_location *al) | |
3f067dca | 1764 | { |
3f067dca | 1765 | unsigned int i; |
644f2df2 ACM |
1766 | const struct branch_stack *bs = sample->branch_stack; |
1767 | struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info)); | |
3f067dca | 1768 | |
3f067dca ACM |
1769 | if (!bi) |
1770 | return NULL; | |
1771 | ||
1772 | for (i = 0; i < bs->nr; i++) { | |
bb871a9c ACM |
1773 | ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to); |
1774 | ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from); | |
3f067dca ACM |
1775 | bi[i].flags = bs->entries[i].flags; |
1776 | } | |
1777 | return bi; | |
1778 | } | |
1779 | ||
c4ee0625 JY |
1780 | static void save_iterations(struct iterations *iter, |
1781 | struct branch_entry *be, int nr) | |
1782 | { | |
1783 | int i; | |
1784 | ||
1785 | iter->nr_loop_iter = nr; | |
1786 | iter->cycles = 0; | |
1787 | ||
1788 | for (i = 0; i < nr; i++) | |
1789 | iter->cycles += be[i].flags.cycles; | |
1790 | } | |
1791 | ||
8b7bad58 AK |
1792 | #define CHASHSZ 127 |
1793 | #define CHASHBITS 7 | |
1794 | #define NO_ENTRY 0xff | |
1795 | ||
1796 | #define PERF_MAX_BRANCH_DEPTH 127 | |
1797 | ||
1798 | /* Remove loops. */ | |
c4ee0625 JY |
1799 | static int remove_loops(struct branch_entry *l, int nr, |
1800 | struct iterations *iter) | |
8b7bad58 AK |
1801 | { |
1802 | int i, j, off; | |
1803 | unsigned char chash[CHASHSZ]; | |
1804 | ||
1805 | memset(chash, NO_ENTRY, sizeof(chash)); | |
1806 | ||
1807 | BUG_ON(PERF_MAX_BRANCH_DEPTH > 255); | |
1808 | ||
1809 | for (i = 0; i < nr; i++) { | |
1810 | int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ; | |
1811 | ||
1812 | /* no collision handling for now */ | |
1813 | if (chash[h] == NO_ENTRY) { | |
1814 | chash[h] = i; | |
1815 | } else if (l[chash[h]].from == l[i].from) { | |
1816 | bool is_loop = true; | |
1817 | /* check if it is a real loop */ | |
1818 | off = 0; | |
1819 | for (j = chash[h]; j < i && i + off < nr; j++, off++) | |
1820 | if (l[j].from != l[i + off].from) { | |
1821 | is_loop = false; | |
1822 | break; | |
1823 | } | |
1824 | if (is_loop) { | |
c4ee0625 JY |
1825 | j = nr - (i + off); |
1826 | if (j > 0) { | |
1827 | save_iterations(iter + i + off, | |
1828 | l + i, off); | |
1829 | ||
1830 | memmove(iter + i, iter + i + off, | |
1831 | j * sizeof(*iter)); | |
1832 | ||
1833 | memmove(l + i, l + i + off, | |
1834 | j * sizeof(*l)); | |
1835 | } | |
1836 | ||
8b7bad58 AK |
1837 | nr -= off; |
1838 | } | |
1839 | } | |
1840 | } | |
1841 | return nr; | |
1842 | } | |
1843 | ||
384b6055 KL |
1844 | /* |
1845 | * Recolve LBR callstack chain sample | |
1846 | * Return: | |
1847 | * 1 on success get LBR callchain information | |
1848 | * 0 no available LBR callchain information, should try fp | |
1849 | * negative error code on other errors. | |
1850 | */ | |
1851 | static int resolve_lbr_callchain_sample(struct thread *thread, | |
91d7b2de | 1852 | struct callchain_cursor *cursor, |
384b6055 KL |
1853 | struct perf_sample *sample, |
1854 | struct symbol **parent, | |
1855 | struct addr_location *root_al, | |
1856 | int max_stack) | |
3f067dca | 1857 | { |
384b6055 | 1858 | struct ip_callchain *chain = sample->callchain; |
18ef15c6 | 1859 | int chain_nr = min(max_stack, (int)chain->nr), i; |
73dbcd65 | 1860 | u8 cpumode = PERF_RECORD_MISC_USER; |
b851dd49 | 1861 | u64 ip, branch_from = 0; |
384b6055 KL |
1862 | |
1863 | for (i = 0; i < chain_nr; i++) { | |
1864 | if (chain->ips[i] == PERF_CONTEXT_USER) | |
1865 | break; | |
1866 | } | |
1867 | ||
1868 | /* LBR only affects the user callchain */ | |
1869 | if (i != chain_nr) { | |
1870 | struct branch_stack *lbr_stack = sample->branch_stack; | |
410024db JY |
1871 | int lbr_nr = lbr_stack->nr, j, k; |
1872 | bool branch; | |
1873 | struct branch_flags *flags; | |
384b6055 KL |
1874 | /* |
1875 | * LBR callstack can only get user call chain. | |
1876 | * The mix_chain_nr is kernel call chain | |
1877 | * number plus LBR user call chain number. | |
1878 | * i is kernel call chain number, | |
1879 | * 1 is PERF_CONTEXT_USER, | |
1880 | * lbr_nr + 1 is the user call chain number. | |
1881 | * For details, please refer to the comments | |
1882 | * in callchain__printf | |
1883 | */ | |
1884 | int mix_chain_nr = i + 1 + lbr_nr + 1; | |
1885 | ||
384b6055 | 1886 | for (j = 0; j < mix_chain_nr; j++) { |
18ef15c6 | 1887 | int err; |
410024db JY |
1888 | branch = false; |
1889 | flags = NULL; | |
1890 | ||
384b6055 KL |
1891 | if (callchain_param.order == ORDER_CALLEE) { |
1892 | if (j < i + 1) | |
1893 | ip = chain->ips[j]; | |
410024db JY |
1894 | else if (j > i + 1) { |
1895 | k = j - i - 2; | |
1896 | ip = lbr_stack->entries[k].from; | |
1897 | branch = true; | |
1898 | flags = &lbr_stack->entries[k].flags; | |
1899 | } else { | |
384b6055 | 1900 | ip = lbr_stack->entries[0].to; |
410024db JY |
1901 | branch = true; |
1902 | flags = &lbr_stack->entries[0].flags; | |
b851dd49 JY |
1903 | branch_from = |
1904 | lbr_stack->entries[0].from; | |
410024db | 1905 | } |
384b6055 | 1906 | } else { |
410024db JY |
1907 | if (j < lbr_nr) { |
1908 | k = lbr_nr - j - 1; | |
1909 | ip = lbr_stack->entries[k].from; | |
1910 | branch = true; | |
1911 | flags = &lbr_stack->entries[k].flags; | |
1912 | } | |
384b6055 KL |
1913 | else if (j > lbr_nr) |
1914 | ip = chain->ips[i + 1 - (j - lbr_nr)]; | |
410024db | 1915 | else { |
384b6055 | 1916 | ip = lbr_stack->entries[0].to; |
410024db JY |
1917 | branch = true; |
1918 | flags = &lbr_stack->entries[0].flags; | |
b851dd49 JY |
1919 | branch_from = |
1920 | lbr_stack->entries[0].from; | |
410024db | 1921 | } |
384b6055 KL |
1922 | } |
1923 | ||
410024db JY |
1924 | err = add_callchain_ip(thread, cursor, parent, |
1925 | root_al, &cpumode, ip, | |
c4ee0625 | 1926 | branch, flags, NULL, |
b851dd49 | 1927 | branch_from); |
384b6055 KL |
1928 | if (err) |
1929 | return (err < 0) ? err : 0; | |
1930 | } | |
1931 | return 1; | |
1932 | } | |
1933 | ||
1934 | return 0; | |
1935 | } | |
1936 | ||
1937 | static int thread__resolve_callchain_sample(struct thread *thread, | |
91d7b2de | 1938 | struct callchain_cursor *cursor, |
384b6055 KL |
1939 | struct perf_evsel *evsel, |
1940 | struct perf_sample *sample, | |
1941 | struct symbol **parent, | |
1942 | struct addr_location *root_al, | |
1943 | int max_stack) | |
1944 | { | |
1945 | struct branch_stack *branch = sample->branch_stack; | |
1946 | struct ip_callchain *chain = sample->callchain; | |
b49a821e | 1947 | int chain_nr = 0; |
73dbcd65 | 1948 | u8 cpumode = PERF_RECORD_MISC_USER; |
bf8bddbf | 1949 | int i, j, err, nr_entries; |
8b7bad58 AK |
1950 | int skip_idx = -1; |
1951 | int first_call = 0; | |
1952 | ||
b49a821e JY |
1953 | if (chain) |
1954 | chain_nr = chain->nr; | |
1955 | ||
acf2abbd | 1956 | if (perf_evsel__has_branch_callstack(evsel)) { |
91d7b2de | 1957 | err = resolve_lbr_callchain_sample(thread, cursor, sample, parent, |
384b6055 KL |
1958 | root_al, max_stack); |
1959 | if (err) | |
1960 | return (err < 0) ? err : 0; | |
1961 | } | |
1962 | ||
8b7bad58 AK |
1963 | /* |
1964 | * Based on DWARF debug information, some architectures skip | |
1965 | * a callchain entry saved by the kernel. | |
1966 | */ | |
bf8bddbf | 1967 | skip_idx = arch_skip_callchain_idx(thread, chain); |
3f067dca | 1968 | |
8b7bad58 AK |
1969 | /* |
1970 | * Add branches to call stack for easier browsing. This gives | |
1971 | * more context for a sample than just the callers. | |
1972 | * | |
1973 | * This uses individual histograms of paths compared to the | |
1974 | * aggregated histograms the normal LBR mode uses. | |
1975 | * | |
1976 | * Limitations for now: | |
1977 | * - No extra filters | |
1978 | * - No annotations (should annotate somehow) | |
1979 | */ | |
1980 | ||
1981 | if (branch && callchain_param.branch_callstack) { | |
1982 | int nr = min(max_stack, (int)branch->nr); | |
1983 | struct branch_entry be[nr]; | |
c4ee0625 | 1984 | struct iterations iter[nr]; |
8b7bad58 AK |
1985 | |
1986 | if (branch->nr > PERF_MAX_BRANCH_DEPTH) { | |
1987 | pr_warning("corrupted branch chain. skipping...\n"); | |
1988 | goto check_calls; | |
1989 | } | |
1990 | ||
1991 | for (i = 0; i < nr; i++) { | |
1992 | if (callchain_param.order == ORDER_CALLEE) { | |
1993 | be[i] = branch->entries[i]; | |
b49a821e JY |
1994 | |
1995 | if (chain == NULL) | |
1996 | continue; | |
1997 | ||
8b7bad58 AK |
1998 | /* |
1999 | * Check for overlap into the callchain. | |
2000 | * The return address is one off compared to | |
2001 | * the branch entry. To adjust for this | |
2002 | * assume the calling instruction is not longer | |
2003 | * than 8 bytes. | |
2004 | */ | |
2005 | if (i == skip_idx || | |
2006 | chain->ips[first_call] >= PERF_CONTEXT_MAX) | |
2007 | first_call++; | |
2008 | else if (be[i].from < chain->ips[first_call] && | |
2009 | be[i].from >= chain->ips[first_call] - 8) | |
2010 | first_call++; | |
2011 | } else | |
2012 | be[i] = branch->entries[branch->nr - i - 1]; | |
2013 | } | |
2014 | ||
c4ee0625 JY |
2015 | memset(iter, 0, sizeof(struct iterations) * nr); |
2016 | nr = remove_loops(be, nr, iter); | |
410024db | 2017 | |
8b7bad58 | 2018 | for (i = 0; i < nr; i++) { |
c4ee0625 JY |
2019 | err = add_callchain_ip(thread, cursor, parent, |
2020 | root_al, | |
2021 | NULL, be[i].to, | |
2022 | true, &be[i].flags, | |
2023 | NULL, be[i].from); | |
410024db | 2024 | |
8b7bad58 | 2025 | if (!err) |
91d7b2de | 2026 | err = add_callchain_ip(thread, cursor, parent, root_al, |
410024db JY |
2027 | NULL, be[i].from, |
2028 | true, &be[i].flags, | |
c4ee0625 | 2029 | &iter[i], 0); |
8b7bad58 AK |
2030 | if (err == -EINVAL) |
2031 | break; | |
2032 | if (err) | |
2033 | return err; | |
2034 | } | |
b49a821e JY |
2035 | |
2036 | if (chain_nr == 0) | |
2037 | return 0; | |
2038 | ||
8b7bad58 AK |
2039 | chain_nr -= nr; |
2040 | } | |
2041 | ||
2042 | check_calls: | |
bf8bddbf | 2043 | for (i = first_call, nr_entries = 0; |
a29d5c9b | 2044 | i < chain_nr && nr_entries < max_stack; i++) { |
3f067dca | 2045 | u64 ip; |
3f067dca ACM |
2046 | |
2047 | if (callchain_param.order == ORDER_CALLEE) | |
a60335ba | 2048 | j = i; |
3f067dca | 2049 | else |
a60335ba SB |
2050 | j = chain->nr - i - 1; |
2051 | ||
2052 | #ifdef HAVE_SKIP_CALLCHAIN_IDX | |
2053 | if (j == skip_idx) | |
2054 | continue; | |
2055 | #endif | |
2056 | ip = chain->ips[j]; | |
3f067dca | 2057 | |
bf8bddbf ACM |
2058 | if (ip < PERF_CONTEXT_MAX) |
2059 | ++nr_entries; | |
a29d5c9b | 2060 | |
410024db JY |
2061 | err = add_callchain_ip(thread, cursor, parent, |
2062 | root_al, &cpumode, ip, | |
c4ee0625 | 2063 | false, NULL, NULL, 0); |
3f067dca | 2064 | |
3f067dca | 2065 | if (err) |
2e77784b | 2066 | return (err < 0) ? err : 0; |
3f067dca ACM |
2067 | } |
2068 | ||
2069 | return 0; | |
2070 | } | |
2071 | ||
2072 | static int unwind_entry(struct unwind_entry *entry, void *arg) | |
2073 | { | |
2074 | struct callchain_cursor *cursor = arg; | |
b49a8fe5 NK |
2075 | |
2076 | if (symbol_conf.hide_unresolved && entry->sym == NULL) | |
2077 | return 0; | |
3f067dca | 2078 | return callchain_cursor_append(cursor, entry->ip, |
410024db | 2079 | entry->map, entry->sym, |
b851dd49 | 2080 | false, NULL, 0, 0, 0); |
3f067dca ACM |
2081 | } |
2082 | ||
9919a65e CP |
2083 | static int thread__resolve_callchain_unwind(struct thread *thread, |
2084 | struct callchain_cursor *cursor, | |
2085 | struct perf_evsel *evsel, | |
2086 | struct perf_sample *sample, | |
2087 | int max_stack) | |
3f067dca | 2088 | { |
3f067dca ACM |
2089 | /* Can we do dwarf post unwind? */ |
2090 | if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) && | |
2091 | (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER))) | |
2092 | return 0; | |
2093 | ||
2094 | /* Bail out if nothing was captured. */ | |
2095 | if ((!sample->user_regs.regs) || | |
2096 | (!sample->user_stack.size)) | |
2097 | return 0; | |
2098 | ||
91d7b2de | 2099 | return unwind__get_entries(unwind_entry, cursor, |
352ea45a | 2100 | thread, sample, max_stack); |
9919a65e | 2101 | } |
3f067dca | 2102 | |
9919a65e CP |
2103 | int thread__resolve_callchain(struct thread *thread, |
2104 | struct callchain_cursor *cursor, | |
2105 | struct perf_evsel *evsel, | |
2106 | struct perf_sample *sample, | |
2107 | struct symbol **parent, | |
2108 | struct addr_location *root_al, | |
2109 | int max_stack) | |
2110 | { | |
2111 | int ret = 0; | |
2112 | ||
2113 | callchain_cursor_reset(&callchain_cursor); | |
2114 | ||
2115 | if (callchain_param.order == ORDER_CALLEE) { | |
2116 | ret = thread__resolve_callchain_sample(thread, cursor, | |
2117 | evsel, sample, | |
2118 | parent, root_al, | |
2119 | max_stack); | |
2120 | if (ret) | |
2121 | return ret; | |
2122 | ret = thread__resolve_callchain_unwind(thread, cursor, | |
2123 | evsel, sample, | |
2124 | max_stack); | |
2125 | } else { | |
2126 | ret = thread__resolve_callchain_unwind(thread, cursor, | |
2127 | evsel, sample, | |
2128 | max_stack); | |
2129 | if (ret) | |
2130 | return ret; | |
2131 | ret = thread__resolve_callchain_sample(thread, cursor, | |
2132 | evsel, sample, | |
2133 | parent, root_al, | |
2134 | max_stack); | |
2135 | } | |
2136 | ||
2137 | return ret; | |
3f067dca | 2138 | } |
35feee19 DA |
2139 | |
2140 | int machine__for_each_thread(struct machine *machine, | |
2141 | int (*fn)(struct thread *thread, void *p), | |
2142 | void *priv) | |
2143 | { | |
2144 | struct rb_node *nd; | |
2145 | struct thread *thread; | |
2146 | int rc = 0; | |
2147 | ||
2148 | for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) { | |
2149 | thread = rb_entry(nd, struct thread, rb_node); | |
2150 | rc = fn(thread, priv); | |
2151 | if (rc != 0) | |
2152 | return rc; | |
2153 | } | |
2154 | ||
2155 | list_for_each_entry(thread, &machine->dead_threads, node) { | |
2156 | rc = fn(thread, priv); | |
2157 | if (rc != 0) | |
2158 | return rc; | |
2159 | } | |
2160 | return rc; | |
2161 | } | |
58d925dc | 2162 | |
a5499b37 AH |
2163 | int machines__for_each_thread(struct machines *machines, |
2164 | int (*fn)(struct thread *thread, void *p), | |
2165 | void *priv) | |
2166 | { | |
2167 | struct rb_node *nd; | |
2168 | int rc = 0; | |
2169 | ||
2170 | rc = machine__for_each_thread(&machines->host, fn, priv); | |
2171 | if (rc != 0) | |
2172 | return rc; | |
2173 | ||
2174 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { | |
2175 | struct machine *machine = rb_entry(nd, struct machine, rb_node); | |
2176 | ||
2177 | rc = machine__for_each_thread(machine, fn, priv); | |
2178 | if (rc != 0) | |
2179 | return rc; | |
2180 | } | |
2181 | return rc; | |
2182 | } | |
2183 | ||
a33fbd56 | 2184 | int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool, |
602ad878 | 2185 | struct target *target, struct thread_map *threads, |
9d9cad76 KL |
2186 | perf_event__handler_t process, bool data_mmap, |
2187 | unsigned int proc_map_timeout) | |
58d925dc | 2188 | { |
602ad878 | 2189 | if (target__has_task(target)) |
9d9cad76 | 2190 | return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap, proc_map_timeout); |
602ad878 | 2191 | else if (target__has_cpu(target)) |
9d9cad76 | 2192 | return perf_event__synthesize_threads(tool, process, machine, data_mmap, proc_map_timeout); |
58d925dc ACM |
2193 | /* command specified */ |
2194 | return 0; | |
2195 | } | |
b9d266ba AH |
2196 | |
2197 | pid_t machine__get_current_tid(struct machine *machine, int cpu) | |
2198 | { | |
2199 | if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid) | |
2200 | return -1; | |
2201 | ||
2202 | return machine->current_tid[cpu]; | |
2203 | } | |
2204 | ||
2205 | int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid, | |
2206 | pid_t tid) | |
2207 | { | |
2208 | struct thread *thread; | |
2209 | ||
2210 | if (cpu < 0) | |
2211 | return -EINVAL; | |
2212 | ||
2213 | if (!machine->current_tid) { | |
2214 | int i; | |
2215 | ||
2216 | machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t)); | |
2217 | if (!machine->current_tid) | |
2218 | return -ENOMEM; | |
2219 | for (i = 0; i < MAX_NR_CPUS; i++) | |
2220 | machine->current_tid[i] = -1; | |
2221 | } | |
2222 | ||
2223 | if (cpu >= MAX_NR_CPUS) { | |
2224 | pr_err("Requested CPU %d too large. ", cpu); | |
2225 | pr_err("Consider raising MAX_NR_CPUS\n"); | |
2226 | return -EINVAL; | |
2227 | } | |
2228 | ||
2229 | machine->current_tid[cpu] = tid; | |
2230 | ||
2231 | thread = machine__findnew_thread(machine, pid, tid); | |
2232 | if (!thread) | |
2233 | return -ENOMEM; | |
2234 | ||
2235 | thread->cpu = cpu; | |
b91fc39f | 2236 | thread__put(thread); |
b9d266ba AH |
2237 | |
2238 | return 0; | |
2239 | } | |
fbe2af45 AH |
2240 | |
2241 | int machine__get_kernel_start(struct machine *machine) | |
2242 | { | |
a5e813c6 | 2243 | struct map *map = machine__kernel_map(machine); |
fbe2af45 AH |
2244 | int err = 0; |
2245 | ||
2246 | /* | |
2247 | * The only addresses above 2^63 are kernel addresses of a 64-bit | |
2248 | * kernel. Note that addresses are unsigned so that on a 32-bit system | |
2249 | * all addresses including kernel addresses are less than 2^32. In | |
2250 | * that case (32-bit system), if the kernel mapping is unknown, all | |
2251 | * addresses will be assumed to be in user space - see | |
2252 | * machine__kernel_ip(). | |
2253 | */ | |
2254 | machine->kernel_start = 1ULL << 63; | |
2255 | if (map) { | |
be39db9f | 2256 | err = map__load(map); |
4b1303d0 | 2257 | if (!err) |
fbe2af45 AH |
2258 | machine->kernel_start = map->start; |
2259 | } | |
2260 | return err; | |
2261 | } | |
aa7cc2ae ACM |
2262 | |
2263 | struct dso *machine__findnew_dso(struct machine *machine, const char *filename) | |
2264 | { | |
e8807844 | 2265 | return dsos__findnew(&machine->dsos, filename); |
aa7cc2ae | 2266 | } |
c3168b0d ACM |
2267 | |
2268 | char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp) | |
2269 | { | |
2270 | struct machine *machine = vmachine; | |
2271 | struct map *map; | |
be39db9f | 2272 | struct symbol *sym = map_groups__find_symbol(&machine->kmaps, MAP__FUNCTION, *addrp, &map); |
c3168b0d ACM |
2273 | |
2274 | if (sym == NULL) | |
2275 | return NULL; | |
2276 | ||
2277 | *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL; | |
2278 | *addrp = map->unmap_ip(map, sym->start); | |
2279 | return sym->name; | |
2280 | } |