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