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