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