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