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