Merge git://git.kvack.org/~bcrl/aio-next
[linux-2.6-microblaze.git] / tools / perf / util / event.c
1 #include <dirent.h>
2 #include <errno.h>
3 #include <inttypes.h>
4 #include <linux/kernel.h>
5 #include <linux/types.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
10 #include <api/fs/fs.h>
11 #include <linux/perf_event.h>
12 #include "event.h"
13 #include "debug.h"
14 #include "hist.h"
15 #include "machine.h"
16 #include "sort.h"
17 #include "string2.h"
18 #include "strlist.h"
19 #include "thread.h"
20 #include "thread_map.h"
21 #include "sane_ctype.h"
22 #include "symbol/kallsyms.h"
23 #include "asm/bug.h"
24 #include "stat.h"
25
26 static const char *perf_event__names[] = {
27         [0]                                     = "TOTAL",
28         [PERF_RECORD_MMAP]                      = "MMAP",
29         [PERF_RECORD_MMAP2]                     = "MMAP2",
30         [PERF_RECORD_LOST]                      = "LOST",
31         [PERF_RECORD_COMM]                      = "COMM",
32         [PERF_RECORD_EXIT]                      = "EXIT",
33         [PERF_RECORD_THROTTLE]                  = "THROTTLE",
34         [PERF_RECORD_UNTHROTTLE]                = "UNTHROTTLE",
35         [PERF_RECORD_FORK]                      = "FORK",
36         [PERF_RECORD_READ]                      = "READ",
37         [PERF_RECORD_SAMPLE]                    = "SAMPLE",
38         [PERF_RECORD_AUX]                       = "AUX",
39         [PERF_RECORD_ITRACE_START]              = "ITRACE_START",
40         [PERF_RECORD_LOST_SAMPLES]              = "LOST_SAMPLES",
41         [PERF_RECORD_SWITCH]                    = "SWITCH",
42         [PERF_RECORD_SWITCH_CPU_WIDE]           = "SWITCH_CPU_WIDE",
43         [PERF_RECORD_NAMESPACES]                = "NAMESPACES",
44         [PERF_RECORD_HEADER_ATTR]               = "ATTR",
45         [PERF_RECORD_HEADER_EVENT_TYPE]         = "EVENT_TYPE",
46         [PERF_RECORD_HEADER_TRACING_DATA]       = "TRACING_DATA",
47         [PERF_RECORD_HEADER_BUILD_ID]           = "BUILD_ID",
48         [PERF_RECORD_FINISHED_ROUND]            = "FINISHED_ROUND",
49         [PERF_RECORD_ID_INDEX]                  = "ID_INDEX",
50         [PERF_RECORD_AUXTRACE_INFO]             = "AUXTRACE_INFO",
51         [PERF_RECORD_AUXTRACE]                  = "AUXTRACE",
52         [PERF_RECORD_AUXTRACE_ERROR]            = "AUXTRACE_ERROR",
53         [PERF_RECORD_THREAD_MAP]                = "THREAD_MAP",
54         [PERF_RECORD_CPU_MAP]                   = "CPU_MAP",
55         [PERF_RECORD_STAT_CONFIG]               = "STAT_CONFIG",
56         [PERF_RECORD_STAT]                      = "STAT",
57         [PERF_RECORD_STAT_ROUND]                = "STAT_ROUND",
58         [PERF_RECORD_EVENT_UPDATE]              = "EVENT_UPDATE",
59         [PERF_RECORD_TIME_CONV]                 = "TIME_CONV",
60         [PERF_RECORD_HEADER_FEATURE]            = "FEATURE",
61 };
62
63 static const char *perf_ns__names[] = {
64         [NET_NS_INDEX]          = "net",
65         [UTS_NS_INDEX]          = "uts",
66         [IPC_NS_INDEX]          = "ipc",
67         [PID_NS_INDEX]          = "pid",
68         [USER_NS_INDEX]         = "user",
69         [MNT_NS_INDEX]          = "mnt",
70         [CGROUP_NS_INDEX]       = "cgroup",
71 };
72
73 const char *perf_event__name(unsigned int id)
74 {
75         if (id >= ARRAY_SIZE(perf_event__names))
76                 return "INVALID";
77         if (!perf_event__names[id])
78                 return "UNKNOWN";
79         return perf_event__names[id];
80 }
81
82 static const char *perf_ns__name(unsigned int id)
83 {
84         if (id >= ARRAY_SIZE(perf_ns__names))
85                 return "UNKNOWN";
86         return perf_ns__names[id];
87 }
88
89 static int perf_tool__process_synth_event(struct perf_tool *tool,
90                                           union perf_event *event,
91                                           struct machine *machine,
92                                           perf_event__handler_t process)
93 {
94         struct perf_sample synth_sample = {
95         .pid       = -1,
96         .tid       = -1,
97         .time      = -1,
98         .stream_id = -1,
99         .cpu       = -1,
100         .period    = 1,
101         .cpumode   = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
102         };
103
104         return process(tool, event, &synth_sample, machine);
105 };
106
107 /*
108  * Assumes that the first 4095 bytes of /proc/pid/stat contains
109  * the comm, tgid and ppid.
110  */
111 static int perf_event__get_comm_ids(pid_t pid, char *comm, size_t len,
112                                     pid_t *tgid, pid_t *ppid)
113 {
114         char filename[PATH_MAX];
115         char bf[4096];
116         int fd;
117         size_t size = 0;
118         ssize_t n;
119         char *name, *tgids, *ppids;
120
121         *tgid = -1;
122         *ppid = -1;
123
124         snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
125
126         fd = open(filename, O_RDONLY);
127         if (fd < 0) {
128                 pr_debug("couldn't open %s\n", filename);
129                 return -1;
130         }
131
132         n = read(fd, bf, sizeof(bf) - 1);
133         close(fd);
134         if (n <= 0) {
135                 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
136                            pid);
137                 return -1;
138         }
139         bf[n] = '\0';
140
141         name = strstr(bf, "Name:");
142         tgids = strstr(bf, "Tgid:");
143         ppids = strstr(bf, "PPid:");
144
145         if (name) {
146                 char *nl;
147
148                 name += 5;  /* strlen("Name:") */
149                 name = ltrim(name);
150
151                 nl = strchr(name, '\n');
152                 if (nl)
153                         *nl = '\0';
154
155                 size = strlen(name);
156                 if (size >= len)
157                         size = len - 1;
158                 memcpy(comm, name, size);
159                 comm[size] = '\0';
160         } else {
161                 pr_debug("Name: string not found for pid %d\n", pid);
162         }
163
164         if (tgids) {
165                 tgids += 5;  /* strlen("Tgid:") */
166                 *tgid = atoi(tgids);
167         } else {
168                 pr_debug("Tgid: string not found for pid %d\n", pid);
169         }
170
171         if (ppids) {
172                 ppids += 5;  /* strlen("PPid:") */
173                 *ppid = atoi(ppids);
174         } else {
175                 pr_debug("PPid: string not found for pid %d\n", pid);
176         }
177
178         return 0;
179 }
180
181 static int perf_event__prepare_comm(union perf_event *event, pid_t pid,
182                                     struct machine *machine,
183                                     pid_t *tgid, pid_t *ppid)
184 {
185         size_t size;
186
187         *ppid = -1;
188
189         memset(&event->comm, 0, sizeof(event->comm));
190
191         if (machine__is_host(machine)) {
192                 if (perf_event__get_comm_ids(pid, event->comm.comm,
193                                              sizeof(event->comm.comm),
194                                              tgid, ppid) != 0) {
195                         return -1;
196                 }
197         } else {
198                 *tgid = machine->pid;
199         }
200
201         if (*tgid < 0)
202                 return -1;
203
204         event->comm.pid = *tgid;
205         event->comm.header.type = PERF_RECORD_COMM;
206
207         size = strlen(event->comm.comm) + 1;
208         size = PERF_ALIGN(size, sizeof(u64));
209         memset(event->comm.comm + size, 0, machine->id_hdr_size);
210         event->comm.header.size = (sizeof(event->comm) -
211                                 (sizeof(event->comm.comm) - size) +
212                                 machine->id_hdr_size);
213         event->comm.tid = pid;
214
215         return 0;
216 }
217
218 pid_t perf_event__synthesize_comm(struct perf_tool *tool,
219                                          union perf_event *event, pid_t pid,
220                                          perf_event__handler_t process,
221                                          struct machine *machine)
222 {
223         pid_t tgid, ppid;
224
225         if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
226                 return -1;
227
228         if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
229                 return -1;
230
231         return tgid;
232 }
233
234 static void perf_event__get_ns_link_info(pid_t pid, const char *ns,
235                                          struct perf_ns_link_info *ns_link_info)
236 {
237         struct stat64 st;
238         char proc_ns[128];
239
240         sprintf(proc_ns, "/proc/%u/ns/%s", pid, ns);
241         if (stat64(proc_ns, &st) == 0) {
242                 ns_link_info->dev = st.st_dev;
243                 ns_link_info->ino = st.st_ino;
244         }
245 }
246
247 int perf_event__synthesize_namespaces(struct perf_tool *tool,
248                                       union perf_event *event,
249                                       pid_t pid, pid_t tgid,
250                                       perf_event__handler_t process,
251                                       struct machine *machine)
252 {
253         u32 idx;
254         struct perf_ns_link_info *ns_link_info;
255
256         if (!tool || !tool->namespace_events)
257                 return 0;
258
259         memset(&event->namespaces, 0, (sizeof(event->namespaces) +
260                (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
261                machine->id_hdr_size));
262
263         event->namespaces.pid = tgid;
264         event->namespaces.tid = pid;
265
266         event->namespaces.nr_namespaces = NR_NAMESPACES;
267
268         ns_link_info = event->namespaces.link_info;
269
270         for (idx = 0; idx < event->namespaces.nr_namespaces; idx++)
271                 perf_event__get_ns_link_info(pid, perf_ns__name(idx),
272                                              &ns_link_info[idx]);
273
274         event->namespaces.header.type = PERF_RECORD_NAMESPACES;
275
276         event->namespaces.header.size = (sizeof(event->namespaces) +
277                         (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
278                         machine->id_hdr_size);
279
280         if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
281                 return -1;
282
283         return 0;
284 }
285
286 static int perf_event__synthesize_fork(struct perf_tool *tool,
287                                        union perf_event *event,
288                                        pid_t pid, pid_t tgid, pid_t ppid,
289                                        perf_event__handler_t process,
290                                        struct machine *machine)
291 {
292         memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
293
294         /*
295          * for main thread set parent to ppid from status file. For other
296          * threads set parent pid to main thread. ie., assume main thread
297          * spawns all threads in a process
298         */
299         if (tgid == pid) {
300                 event->fork.ppid = ppid;
301                 event->fork.ptid = ppid;
302         } else {
303                 event->fork.ppid = tgid;
304                 event->fork.ptid = tgid;
305         }
306         event->fork.pid  = tgid;
307         event->fork.tid  = pid;
308         event->fork.header.type = PERF_RECORD_FORK;
309
310         event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
311
312         if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
313                 return -1;
314
315         return 0;
316 }
317
318 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
319                                        union perf_event *event,
320                                        pid_t pid, pid_t tgid,
321                                        perf_event__handler_t process,
322                                        struct machine *machine,
323                                        bool mmap_data,
324                                        unsigned int proc_map_timeout)
325 {
326         char filename[PATH_MAX];
327         FILE *fp;
328         unsigned long long t;
329         bool truncation = false;
330         unsigned long long timeout = proc_map_timeout * 1000000ULL;
331         int rc = 0;
332         const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
333         int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
334
335         if (machine__is_default_guest(machine))
336                 return 0;
337
338         snprintf(filename, sizeof(filename), "%s/proc/%d/task/%d/maps",
339                  machine->root_dir, pid, pid);
340
341         fp = fopen(filename, "r");
342         if (fp == NULL) {
343                 /*
344                  * We raced with a task exiting - just return:
345                  */
346                 pr_debug("couldn't open %s\n", filename);
347                 return -1;
348         }
349
350         event->header.type = PERF_RECORD_MMAP2;
351         t = rdclock();
352
353         while (1) {
354                 char bf[BUFSIZ];
355                 char prot[5];
356                 char execname[PATH_MAX];
357                 char anonstr[] = "//anon";
358                 unsigned int ino;
359                 size_t size;
360                 ssize_t n;
361
362                 if (fgets(bf, sizeof(bf), fp) == NULL)
363                         break;
364
365                 if ((rdclock() - t) > timeout) {
366                         pr_warning("Reading %s time out. "
367                                    "You may want to increase "
368                                    "the time limit by --proc-map-timeout\n",
369                                    filename);
370                         truncation = true;
371                         goto out;
372                 }
373
374                 /* ensure null termination since stack will be reused. */
375                 strcpy(execname, "");
376
377                 /* 00400000-0040c000 r-xp 00000000 fd:01 41038  /bin/cat */
378                 n = sscanf(bf, "%"PRIx64"-%"PRIx64" %s %"PRIx64" %x:%x %u %[^\n]\n",
379                        &event->mmap2.start, &event->mmap2.len, prot,
380                        &event->mmap2.pgoff, &event->mmap2.maj,
381                        &event->mmap2.min,
382                        &ino, execname);
383
384                 /*
385                  * Anon maps don't have the execname.
386                  */
387                 if (n < 7)
388                         continue;
389
390                 event->mmap2.ino = (u64)ino;
391
392                 /*
393                  * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
394                  */
395                 if (machine__is_host(machine))
396                         event->header.misc = PERF_RECORD_MISC_USER;
397                 else
398                         event->header.misc = PERF_RECORD_MISC_GUEST_USER;
399
400                 /* map protection and flags bits */
401                 event->mmap2.prot = 0;
402                 event->mmap2.flags = 0;
403                 if (prot[0] == 'r')
404                         event->mmap2.prot |= PROT_READ;
405                 if (prot[1] == 'w')
406                         event->mmap2.prot |= PROT_WRITE;
407                 if (prot[2] == 'x')
408                         event->mmap2.prot |= PROT_EXEC;
409
410                 if (prot[3] == 's')
411                         event->mmap2.flags |= MAP_SHARED;
412                 else
413                         event->mmap2.flags |= MAP_PRIVATE;
414
415                 if (prot[2] != 'x') {
416                         if (!mmap_data || prot[0] != 'r')
417                                 continue;
418
419                         event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
420                 }
421
422 out:
423                 if (truncation)
424                         event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
425
426                 if (!strcmp(execname, ""))
427                         strcpy(execname, anonstr);
428
429                 if (hugetlbfs_mnt_len &&
430                     !strncmp(execname, hugetlbfs_mnt, hugetlbfs_mnt_len)) {
431                         strcpy(execname, anonstr);
432                         event->mmap2.flags |= MAP_HUGETLB;
433                 }
434
435                 size = strlen(execname) + 1;
436                 memcpy(event->mmap2.filename, execname, size);
437                 size = PERF_ALIGN(size, sizeof(u64));
438                 event->mmap2.len -= event->mmap.start;
439                 event->mmap2.header.size = (sizeof(event->mmap2) -
440                                         (sizeof(event->mmap2.filename) - size));
441                 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
442                 event->mmap2.header.size += machine->id_hdr_size;
443                 event->mmap2.pid = tgid;
444                 event->mmap2.tid = pid;
445
446                 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
447                         rc = -1;
448                         break;
449                 }
450
451                 if (truncation)
452                         break;
453         }
454
455         fclose(fp);
456         return rc;
457 }
458
459 int perf_event__synthesize_modules(struct perf_tool *tool,
460                                    perf_event__handler_t process,
461                                    struct machine *machine)
462 {
463         int rc = 0;
464         struct map *pos;
465         struct map_groups *kmaps = &machine->kmaps;
466         struct maps *maps = &kmaps->maps[MAP__FUNCTION];
467         union perf_event *event = zalloc((sizeof(event->mmap) +
468                                           machine->id_hdr_size));
469         if (event == NULL) {
470                 pr_debug("Not enough memory synthesizing mmap event "
471                          "for kernel modules\n");
472                 return -1;
473         }
474
475         event->header.type = PERF_RECORD_MMAP;
476
477         /*
478          * kernel uses 0 for user space maps, see kernel/perf_event.c
479          * __perf_event_mmap
480          */
481         if (machine__is_host(machine))
482                 event->header.misc = PERF_RECORD_MISC_KERNEL;
483         else
484                 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
485
486         for (pos = maps__first(maps); pos; pos = map__next(pos)) {
487                 size_t size;
488
489                 if (__map__is_kernel(pos))
490                         continue;
491
492                 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
493                 event->mmap.header.type = PERF_RECORD_MMAP;
494                 event->mmap.header.size = (sizeof(event->mmap) -
495                                         (sizeof(event->mmap.filename) - size));
496                 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
497                 event->mmap.header.size += machine->id_hdr_size;
498                 event->mmap.start = pos->start;
499                 event->mmap.len   = pos->end - pos->start;
500                 event->mmap.pid   = machine->pid;
501
502                 memcpy(event->mmap.filename, pos->dso->long_name,
503                        pos->dso->long_name_len + 1);
504                 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
505                         rc = -1;
506                         break;
507                 }
508         }
509
510         free(event);
511         return rc;
512 }
513
514 static int __event__synthesize_thread(union perf_event *comm_event,
515                                       union perf_event *mmap_event,
516                                       union perf_event *fork_event,
517                                       union perf_event *namespaces_event,
518                                       pid_t pid, int full,
519                                       perf_event__handler_t process,
520                                       struct perf_tool *tool,
521                                       struct machine *machine,
522                                       bool mmap_data,
523                                       unsigned int proc_map_timeout)
524 {
525         char filename[PATH_MAX];
526         DIR *tasks;
527         struct dirent *dirent;
528         pid_t tgid, ppid;
529         int rc = 0;
530
531         /* special case: only send one comm event using passed in pid */
532         if (!full) {
533                 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
534                                                    process, machine);
535
536                 if (tgid == -1)
537                         return -1;
538
539                 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
540                                                       tgid, process, machine) < 0)
541                         return -1;
542
543
544                 return perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
545                                                           process, machine, mmap_data,
546                                                           proc_map_timeout);
547         }
548
549         if (machine__is_default_guest(machine))
550                 return 0;
551
552         snprintf(filename, sizeof(filename), "%s/proc/%d/task",
553                  machine->root_dir, pid);
554
555         tasks = opendir(filename);
556         if (tasks == NULL) {
557                 pr_debug("couldn't open %s\n", filename);
558                 return 0;
559         }
560
561         while ((dirent = readdir(tasks)) != NULL) {
562                 char *end;
563                 pid_t _pid;
564
565                 _pid = strtol(dirent->d_name, &end, 10);
566                 if (*end)
567                         continue;
568
569                 rc = -1;
570                 if (perf_event__prepare_comm(comm_event, _pid, machine,
571                                              &tgid, &ppid) != 0)
572                         break;
573
574                 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
575                                                 ppid, process, machine) < 0)
576                         break;
577
578                 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
579                                                       tgid, process, machine) < 0)
580                         break;
581
582                 /*
583                  * Send the prepared comm event
584                  */
585                 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
586                         break;
587
588                 rc = 0;
589                 if (_pid == pid) {
590                         /* process the parent's maps too */
591                         rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
592                                                 process, machine, mmap_data, proc_map_timeout);
593                         if (rc)
594                                 break;
595                 }
596         }
597
598         closedir(tasks);
599         return rc;
600 }
601
602 int perf_event__synthesize_thread_map(struct perf_tool *tool,
603                                       struct thread_map *threads,
604                                       perf_event__handler_t process,
605                                       struct machine *machine,
606                                       bool mmap_data,
607                                       unsigned int proc_map_timeout)
608 {
609         union perf_event *comm_event, *mmap_event, *fork_event;
610         union perf_event *namespaces_event;
611         int err = -1, thread, j;
612
613         comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
614         if (comm_event == NULL)
615                 goto out;
616
617         mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
618         if (mmap_event == NULL)
619                 goto out_free_comm;
620
621         fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
622         if (fork_event == NULL)
623                 goto out_free_mmap;
624
625         namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
626                                   (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
627                                   machine->id_hdr_size);
628         if (namespaces_event == NULL)
629                 goto out_free_fork;
630
631         err = 0;
632         for (thread = 0; thread < threads->nr; ++thread) {
633                 if (__event__synthesize_thread(comm_event, mmap_event,
634                                                fork_event, namespaces_event,
635                                                thread_map__pid(threads, thread), 0,
636                                                process, tool, machine,
637                                                mmap_data, proc_map_timeout)) {
638                         err = -1;
639                         break;
640                 }
641
642                 /*
643                  * comm.pid is set to thread group id by
644                  * perf_event__synthesize_comm
645                  */
646                 if ((int) comm_event->comm.pid != thread_map__pid(threads, thread)) {
647                         bool need_leader = true;
648
649                         /* is thread group leader in thread_map? */
650                         for (j = 0; j < threads->nr; ++j) {
651                                 if ((int) comm_event->comm.pid == thread_map__pid(threads, j)) {
652                                         need_leader = false;
653                                         break;
654                                 }
655                         }
656
657                         /* if not, generate events for it */
658                         if (need_leader &&
659                             __event__synthesize_thread(comm_event, mmap_event,
660                                                        fork_event, namespaces_event,
661                                                        comm_event->comm.pid, 0,
662                                                        process, tool, machine,
663                                                        mmap_data, proc_map_timeout)) {
664                                 err = -1;
665                                 break;
666                         }
667                 }
668         }
669         free(namespaces_event);
670 out_free_fork:
671         free(fork_event);
672 out_free_mmap:
673         free(mmap_event);
674 out_free_comm:
675         free(comm_event);
676 out:
677         return err;
678 }
679
680 int perf_event__synthesize_threads(struct perf_tool *tool,
681                                    perf_event__handler_t process,
682                                    struct machine *machine,
683                                    bool mmap_data,
684                                    unsigned int proc_map_timeout)
685 {
686         DIR *proc;
687         char proc_path[PATH_MAX];
688         struct dirent *dirent;
689         union perf_event *comm_event, *mmap_event, *fork_event;
690         union perf_event *namespaces_event;
691         int err = -1;
692
693         if (machine__is_default_guest(machine))
694                 return 0;
695
696         comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
697         if (comm_event == NULL)
698                 goto out;
699
700         mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
701         if (mmap_event == NULL)
702                 goto out_free_comm;
703
704         fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
705         if (fork_event == NULL)
706                 goto out_free_mmap;
707
708         namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
709                                   (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
710                                   machine->id_hdr_size);
711         if (namespaces_event == NULL)
712                 goto out_free_fork;
713
714         snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
715         proc = opendir(proc_path);
716
717         if (proc == NULL)
718                 goto out_free_namespaces;
719
720         while ((dirent = readdir(proc)) != NULL) {
721                 char *end;
722                 pid_t pid = strtol(dirent->d_name, &end, 10);
723
724                 if (*end) /* only interested in proper numerical dirents */
725                         continue;
726                 /*
727                  * We may race with exiting thread, so don't stop just because
728                  * one thread couldn't be synthesized.
729                  */
730                 __event__synthesize_thread(comm_event, mmap_event, fork_event,
731                                            namespaces_event, pid, 1, process,
732                                            tool, machine, mmap_data,
733                                            proc_map_timeout);
734         }
735
736         err = 0;
737         closedir(proc);
738 out_free_namespaces:
739         free(namespaces_event);
740 out_free_fork:
741         free(fork_event);
742 out_free_mmap:
743         free(mmap_event);
744 out_free_comm:
745         free(comm_event);
746 out:
747         return err;
748 }
749
750 struct process_symbol_args {
751         const char *name;
752         u64        start;
753 };
754
755 static int find_symbol_cb(void *arg, const char *name, char type,
756                           u64 start)
757 {
758         struct process_symbol_args *args = arg;
759
760         /*
761          * Must be a function or at least an alias, as in PARISC64, where "_text" is
762          * an 'A' to the same address as "_stext".
763          */
764         if (!(symbol_type__is_a(type, MAP__FUNCTION) ||
765               type == 'A') || strcmp(name, args->name))
766                 return 0;
767
768         args->start = start;
769         return 1;
770 }
771
772 int kallsyms__get_function_start(const char *kallsyms_filename,
773                                  const char *symbol_name, u64 *addr)
774 {
775         struct process_symbol_args args = { .name = symbol_name, };
776
777         if (kallsyms__parse(kallsyms_filename, &args, find_symbol_cb) <= 0)
778                 return -1;
779
780         *addr = args.start;
781         return 0;
782 }
783
784 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
785                                        perf_event__handler_t process,
786                                        struct machine *machine)
787 {
788         size_t size;
789         const char *mmap_name;
790         char name_buff[PATH_MAX];
791         struct map *map = machine__kernel_map(machine);
792         struct kmap *kmap;
793         int err;
794         union perf_event *event;
795
796         if (symbol_conf.kptr_restrict)
797                 return -1;
798         if (map == NULL)
799                 return -1;
800
801         /*
802          * We should get this from /sys/kernel/sections/.text, but till that is
803          * available use this, and after it is use this as a fallback for older
804          * kernels.
805          */
806         event = zalloc((sizeof(event->mmap) + machine->id_hdr_size));
807         if (event == NULL) {
808                 pr_debug("Not enough memory synthesizing mmap event "
809                          "for kernel modules\n");
810                 return -1;
811         }
812
813         mmap_name = machine__mmap_name(machine, name_buff, sizeof(name_buff));
814         if (machine__is_host(machine)) {
815                 /*
816                  * kernel uses PERF_RECORD_MISC_USER for user space maps,
817                  * see kernel/perf_event.c __perf_event_mmap
818                  */
819                 event->header.misc = PERF_RECORD_MISC_KERNEL;
820         } else {
821                 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
822         }
823
824         kmap = map__kmap(map);
825         size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
826                         "%s%s", mmap_name, kmap->ref_reloc_sym->name) + 1;
827         size = PERF_ALIGN(size, sizeof(u64));
828         event->mmap.header.type = PERF_RECORD_MMAP;
829         event->mmap.header.size = (sizeof(event->mmap) -
830                         (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
831         event->mmap.pgoff = kmap->ref_reloc_sym->addr;
832         event->mmap.start = map->start;
833         event->mmap.len   = map->end - event->mmap.start;
834         event->mmap.pid   = machine->pid;
835
836         err = perf_tool__process_synth_event(tool, event, machine, process);
837         free(event);
838
839         return err;
840 }
841
842 int perf_event__synthesize_thread_map2(struct perf_tool *tool,
843                                       struct thread_map *threads,
844                                       perf_event__handler_t process,
845                                       struct machine *machine)
846 {
847         union perf_event *event;
848         int i, err, size;
849
850         size  = sizeof(event->thread_map);
851         size += threads->nr * sizeof(event->thread_map.entries[0]);
852
853         event = zalloc(size);
854         if (!event)
855                 return -ENOMEM;
856
857         event->header.type = PERF_RECORD_THREAD_MAP;
858         event->header.size = size;
859         event->thread_map.nr = threads->nr;
860
861         for (i = 0; i < threads->nr; i++) {
862                 struct thread_map_event_entry *entry = &event->thread_map.entries[i];
863                 char *comm = thread_map__comm(threads, i);
864
865                 if (!comm)
866                         comm = (char *) "";
867
868                 entry->pid = thread_map__pid(threads, i);
869                 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
870         }
871
872         err = process(tool, event, NULL, machine);
873
874         free(event);
875         return err;
876 }
877
878 static void synthesize_cpus(struct cpu_map_entries *cpus,
879                             struct cpu_map *map)
880 {
881         int i;
882
883         cpus->nr = map->nr;
884
885         for (i = 0; i < map->nr; i++)
886                 cpus->cpu[i] = map->map[i];
887 }
888
889 static void synthesize_mask(struct cpu_map_mask *mask,
890                             struct cpu_map *map, int max)
891 {
892         int i;
893
894         mask->nr = BITS_TO_LONGS(max);
895         mask->long_size = sizeof(long);
896
897         for (i = 0; i < map->nr; i++)
898                 set_bit(map->map[i], mask->mask);
899 }
900
901 static size_t cpus_size(struct cpu_map *map)
902 {
903         return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
904 }
905
906 static size_t mask_size(struct cpu_map *map, int *max)
907 {
908         int i;
909
910         *max = 0;
911
912         for (i = 0; i < map->nr; i++) {
913                 /* bit possition of the cpu is + 1 */
914                 int bit = map->map[i] + 1;
915
916                 if (bit > *max)
917                         *max = bit;
918         }
919
920         return sizeof(struct cpu_map_mask) + BITS_TO_LONGS(*max) * sizeof(long);
921 }
922
923 void *cpu_map_data__alloc(struct cpu_map *map, size_t *size, u16 *type, int *max)
924 {
925         size_t size_cpus, size_mask;
926         bool is_dummy = cpu_map__empty(map);
927
928         /*
929          * Both array and mask data have variable size based
930          * on the number of cpus and their actual values.
931          * The size of the 'struct cpu_map_data' is:
932          *
933          *   array = size of 'struct cpu_map_entries' +
934          *           number of cpus * sizeof(u64)
935          *
936          *   mask  = size of 'struct cpu_map_mask' +
937          *           maximum cpu bit converted to size of longs
938          *
939          * and finaly + the size of 'struct cpu_map_data'.
940          */
941         size_cpus = cpus_size(map);
942         size_mask = mask_size(map, max);
943
944         if (is_dummy || (size_cpus < size_mask)) {
945                 *size += size_cpus;
946                 *type  = PERF_CPU_MAP__CPUS;
947         } else {
948                 *size += size_mask;
949                 *type  = PERF_CPU_MAP__MASK;
950         }
951
952         *size += sizeof(struct cpu_map_data);
953         return zalloc(*size);
954 }
955
956 void cpu_map_data__synthesize(struct cpu_map_data *data, struct cpu_map *map,
957                               u16 type, int max)
958 {
959         data->type = type;
960
961         switch (type) {
962         case PERF_CPU_MAP__CPUS:
963                 synthesize_cpus((struct cpu_map_entries *) data->data, map);
964                 break;
965         case PERF_CPU_MAP__MASK:
966                 synthesize_mask((struct cpu_map_mask *) data->data, map, max);
967         default:
968                 break;
969         };
970 }
971
972 static struct cpu_map_event* cpu_map_event__new(struct cpu_map *map)
973 {
974         size_t size = sizeof(struct cpu_map_event);
975         struct cpu_map_event *event;
976         int max;
977         u16 type;
978
979         event = cpu_map_data__alloc(map, &size, &type, &max);
980         if (!event)
981                 return NULL;
982
983         event->header.type = PERF_RECORD_CPU_MAP;
984         event->header.size = size;
985         event->data.type   = type;
986
987         cpu_map_data__synthesize(&event->data, map, type, max);
988         return event;
989 }
990
991 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
992                                    struct cpu_map *map,
993                                    perf_event__handler_t process,
994                                    struct machine *machine)
995 {
996         struct cpu_map_event *event;
997         int err;
998
999         event = cpu_map_event__new(map);
1000         if (!event)
1001                 return -ENOMEM;
1002
1003         err = process(tool, (union perf_event *) event, NULL, machine);
1004
1005         free(event);
1006         return err;
1007 }
1008
1009 int perf_event__synthesize_stat_config(struct perf_tool *tool,
1010                                        struct perf_stat_config *config,
1011                                        perf_event__handler_t process,
1012                                        struct machine *machine)
1013 {
1014         struct stat_config_event *event;
1015         int size, i = 0, err;
1016
1017         size  = sizeof(*event);
1018         size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1019
1020         event = zalloc(size);
1021         if (!event)
1022                 return -ENOMEM;
1023
1024         event->header.type = PERF_RECORD_STAT_CONFIG;
1025         event->header.size = size;
1026         event->nr          = PERF_STAT_CONFIG_TERM__MAX;
1027
1028 #define ADD(__term, __val)                                      \
1029         event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term;   \
1030         event->data[i].val = __val;                             \
1031         i++;
1032
1033         ADD(AGGR_MODE,  config->aggr_mode)
1034         ADD(INTERVAL,   config->interval)
1035         ADD(SCALE,      config->scale)
1036
1037         WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1038                   "stat config terms unbalanced\n");
1039 #undef ADD
1040
1041         err = process(tool, (union perf_event *) event, NULL, machine);
1042
1043         free(event);
1044         return err;
1045 }
1046
1047 int perf_event__synthesize_stat(struct perf_tool *tool,
1048                                 u32 cpu, u32 thread, u64 id,
1049                                 struct perf_counts_values *count,
1050                                 perf_event__handler_t process,
1051                                 struct machine *machine)
1052 {
1053         struct stat_event event;
1054
1055         event.header.type = PERF_RECORD_STAT;
1056         event.header.size = sizeof(event);
1057         event.header.misc = 0;
1058
1059         event.id        = id;
1060         event.cpu       = cpu;
1061         event.thread    = thread;
1062         event.val       = count->val;
1063         event.ena       = count->ena;
1064         event.run       = count->run;
1065
1066         return process(tool, (union perf_event *) &event, NULL, machine);
1067 }
1068
1069 int perf_event__synthesize_stat_round(struct perf_tool *tool,
1070                                       u64 evtime, u64 type,
1071                                       perf_event__handler_t process,
1072                                       struct machine *machine)
1073 {
1074         struct stat_round_event event;
1075
1076         event.header.type = PERF_RECORD_STAT_ROUND;
1077         event.header.size = sizeof(event);
1078         event.header.misc = 0;
1079
1080         event.time = evtime;
1081         event.type = type;
1082
1083         return process(tool, (union perf_event *) &event, NULL, machine);
1084 }
1085
1086 void perf_event__read_stat_config(struct perf_stat_config *config,
1087                                   struct stat_config_event *event)
1088 {
1089         unsigned i;
1090
1091         for (i = 0; i < event->nr; i++) {
1092
1093                 switch (event->data[i].tag) {
1094 #define CASE(__term, __val)                                     \
1095                 case PERF_STAT_CONFIG_TERM__##__term:           \
1096                         config->__val = event->data[i].val;     \
1097                         break;
1098
1099                 CASE(AGGR_MODE, aggr_mode)
1100                 CASE(SCALE,     scale)
1101                 CASE(INTERVAL,  interval)
1102 #undef CASE
1103                 default:
1104                         pr_warning("unknown stat config term %" PRIu64 "\n",
1105                                    event->data[i].tag);
1106                 }
1107         }
1108 }
1109
1110 size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp)
1111 {
1112         const char *s;
1113
1114         if (event->header.misc & PERF_RECORD_MISC_COMM_EXEC)
1115                 s = " exec";
1116         else
1117                 s = "";
1118
1119         return fprintf(fp, "%s: %s:%d/%d\n", s, event->comm.comm, event->comm.pid, event->comm.tid);
1120 }
1121
1122 size_t perf_event__fprintf_namespaces(union perf_event *event, FILE *fp)
1123 {
1124         size_t ret = 0;
1125         struct perf_ns_link_info *ns_link_info;
1126         u32 nr_namespaces, idx;
1127
1128         ns_link_info = event->namespaces.link_info;
1129         nr_namespaces = event->namespaces.nr_namespaces;
1130
1131         ret += fprintf(fp, " %d/%d - nr_namespaces: %u\n\t\t[",
1132                        event->namespaces.pid,
1133                        event->namespaces.tid,
1134                        nr_namespaces);
1135
1136         for (idx = 0; idx < nr_namespaces; idx++) {
1137                 if (idx && (idx % 4 == 0))
1138                         ret += fprintf(fp, "\n\t\t ");
1139
1140                 ret  += fprintf(fp, "%u/%s: %" PRIu64 "/%#" PRIx64 "%s", idx,
1141                                 perf_ns__name(idx), (u64)ns_link_info[idx].dev,
1142                                 (u64)ns_link_info[idx].ino,
1143                                 ((idx + 1) != nr_namespaces) ? ", " : "]\n");
1144         }
1145
1146         return ret;
1147 }
1148
1149 int perf_event__process_comm(struct perf_tool *tool __maybe_unused,
1150                              union perf_event *event,
1151                              struct perf_sample *sample,
1152                              struct machine *machine)
1153 {
1154         return machine__process_comm_event(machine, event, sample);
1155 }
1156
1157 int perf_event__process_namespaces(struct perf_tool *tool __maybe_unused,
1158                                    union perf_event *event,
1159                                    struct perf_sample *sample,
1160                                    struct machine *machine)
1161 {
1162         return machine__process_namespaces_event(machine, event, sample);
1163 }
1164
1165 int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
1166                              union perf_event *event,
1167                              struct perf_sample *sample,
1168                              struct machine *machine)
1169 {
1170         return machine__process_lost_event(machine, event, sample);
1171 }
1172
1173 int perf_event__process_aux(struct perf_tool *tool __maybe_unused,
1174                             union perf_event *event,
1175                             struct perf_sample *sample __maybe_unused,
1176                             struct machine *machine)
1177 {
1178         return machine__process_aux_event(machine, event);
1179 }
1180
1181 int perf_event__process_itrace_start(struct perf_tool *tool __maybe_unused,
1182                                      union perf_event *event,
1183                                      struct perf_sample *sample __maybe_unused,
1184                                      struct machine *machine)
1185 {
1186         return machine__process_itrace_start_event(machine, event);
1187 }
1188
1189 int perf_event__process_lost_samples(struct perf_tool *tool __maybe_unused,
1190                                      union perf_event *event,
1191                                      struct perf_sample *sample,
1192                                      struct machine *machine)
1193 {
1194         return machine__process_lost_samples_event(machine, event, sample);
1195 }
1196
1197 int perf_event__process_switch(struct perf_tool *tool __maybe_unused,
1198                                union perf_event *event,
1199                                struct perf_sample *sample __maybe_unused,
1200                                struct machine *machine)
1201 {
1202         return machine__process_switch_event(machine, event);
1203 }
1204
1205 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
1206 {
1207         return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
1208                        event->mmap.pid, event->mmap.tid, event->mmap.start,
1209                        event->mmap.len, event->mmap.pgoff,
1210                        (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) ? 'r' : 'x',
1211                        event->mmap.filename);
1212 }
1213
1214 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp)
1215 {
1216         return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64
1217                            " %02x:%02x %"PRIu64" %"PRIu64"]: %c%c%c%c %s\n",
1218                        event->mmap2.pid, event->mmap2.tid, event->mmap2.start,
1219                        event->mmap2.len, event->mmap2.pgoff, event->mmap2.maj,
1220                        event->mmap2.min, event->mmap2.ino,
1221                        event->mmap2.ino_generation,
1222                        (event->mmap2.prot & PROT_READ) ? 'r' : '-',
1223                        (event->mmap2.prot & PROT_WRITE) ? 'w' : '-',
1224                        (event->mmap2.prot & PROT_EXEC) ? 'x' : '-',
1225                        (event->mmap2.flags & MAP_SHARED) ? 's' : 'p',
1226                        event->mmap2.filename);
1227 }
1228
1229 size_t perf_event__fprintf_thread_map(union perf_event *event, FILE *fp)
1230 {
1231         struct thread_map *threads = thread_map__new_event(&event->thread_map);
1232         size_t ret;
1233
1234         ret = fprintf(fp, " nr: ");
1235
1236         if (threads)
1237                 ret += thread_map__fprintf(threads, fp);
1238         else
1239                 ret += fprintf(fp, "failed to get threads from event\n");
1240
1241         thread_map__put(threads);
1242         return ret;
1243 }
1244
1245 size_t perf_event__fprintf_cpu_map(union perf_event *event, FILE *fp)
1246 {
1247         struct cpu_map *cpus = cpu_map__new_data(&event->cpu_map.data);
1248         size_t ret;
1249
1250         ret = fprintf(fp, ": ");
1251
1252         if (cpus)
1253                 ret += cpu_map__fprintf(cpus, fp);
1254         else
1255                 ret += fprintf(fp, "failed to get cpumap from event\n");
1256
1257         cpu_map__put(cpus);
1258         return ret;
1259 }
1260
1261 int perf_event__process_mmap(struct perf_tool *tool __maybe_unused,
1262                              union perf_event *event,
1263                              struct perf_sample *sample,
1264                              struct machine *machine)
1265 {
1266         return machine__process_mmap_event(machine, event, sample);
1267 }
1268
1269 int perf_event__process_mmap2(struct perf_tool *tool __maybe_unused,
1270                              union perf_event *event,
1271                              struct perf_sample *sample,
1272                              struct machine *machine)
1273 {
1274         return machine__process_mmap2_event(machine, event, sample);
1275 }
1276
1277 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp)
1278 {
1279         return fprintf(fp, "(%d:%d):(%d:%d)\n",
1280                        event->fork.pid, event->fork.tid,
1281                        event->fork.ppid, event->fork.ptid);
1282 }
1283
1284 int perf_event__process_fork(struct perf_tool *tool __maybe_unused,
1285                              union perf_event *event,
1286                              struct perf_sample *sample,
1287                              struct machine *machine)
1288 {
1289         return machine__process_fork_event(machine, event, sample);
1290 }
1291
1292 int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
1293                              union perf_event *event,
1294                              struct perf_sample *sample,
1295                              struct machine *machine)
1296 {
1297         return machine__process_exit_event(machine, event, sample);
1298 }
1299
1300 size_t perf_event__fprintf_aux(union perf_event *event, FILE *fp)
1301 {
1302         return fprintf(fp, " offset: %#"PRIx64" size: %#"PRIx64" flags: %#"PRIx64" [%s%s%s]\n",
1303                        event->aux.aux_offset, event->aux.aux_size,
1304                        event->aux.flags,
1305                        event->aux.flags & PERF_AUX_FLAG_TRUNCATED ? "T" : "",
1306                        event->aux.flags & PERF_AUX_FLAG_OVERWRITE ? "O" : "",
1307                        event->aux.flags & PERF_AUX_FLAG_PARTIAL   ? "P" : "");
1308 }
1309
1310 size_t perf_event__fprintf_itrace_start(union perf_event *event, FILE *fp)
1311 {
1312         return fprintf(fp, " pid: %u tid: %u\n",
1313                        event->itrace_start.pid, event->itrace_start.tid);
1314 }
1315
1316 size_t perf_event__fprintf_switch(union perf_event *event, FILE *fp)
1317 {
1318         bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1319         const char *in_out = out ? "OUT" : "IN ";
1320
1321         if (event->header.type == PERF_RECORD_SWITCH)
1322                 return fprintf(fp, " %s\n", in_out);
1323
1324         return fprintf(fp, " %s  %s pid/tid: %5u/%-5u\n",
1325                        in_out, out ? "next" : "prev",
1326                        event->context_switch.next_prev_pid,
1327                        event->context_switch.next_prev_tid);
1328 }
1329
1330 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
1331 {
1332         size_t ret = fprintf(fp, "PERF_RECORD_%s",
1333                              perf_event__name(event->header.type));
1334
1335         switch (event->header.type) {
1336         case PERF_RECORD_COMM:
1337                 ret += perf_event__fprintf_comm(event, fp);
1338                 break;
1339         case PERF_RECORD_FORK:
1340         case PERF_RECORD_EXIT:
1341                 ret += perf_event__fprintf_task(event, fp);
1342                 break;
1343         case PERF_RECORD_MMAP:
1344                 ret += perf_event__fprintf_mmap(event, fp);
1345                 break;
1346         case PERF_RECORD_NAMESPACES:
1347                 ret += perf_event__fprintf_namespaces(event, fp);
1348                 break;
1349         case PERF_RECORD_MMAP2:
1350                 ret += perf_event__fprintf_mmap2(event, fp);
1351                 break;
1352         case PERF_RECORD_AUX:
1353                 ret += perf_event__fprintf_aux(event, fp);
1354                 break;
1355         case PERF_RECORD_ITRACE_START:
1356                 ret += perf_event__fprintf_itrace_start(event, fp);
1357                 break;
1358         case PERF_RECORD_SWITCH:
1359         case PERF_RECORD_SWITCH_CPU_WIDE:
1360                 ret += perf_event__fprintf_switch(event, fp);
1361                 break;
1362         default:
1363                 ret += fprintf(fp, "\n");
1364         }
1365
1366         return ret;
1367 }
1368
1369 int perf_event__process(struct perf_tool *tool __maybe_unused,
1370                         union perf_event *event,
1371                         struct perf_sample *sample,
1372                         struct machine *machine)
1373 {
1374         return machine__process_event(machine, event, sample);
1375 }
1376
1377 void thread__find_addr_map(struct thread *thread, u8 cpumode,
1378                            enum map_type type, u64 addr,
1379                            struct addr_location *al)
1380 {
1381         struct map_groups *mg = thread->mg;
1382         struct machine *machine = mg->machine;
1383         bool load_map = false;
1384
1385         al->machine = machine;
1386         al->thread = thread;
1387         al->addr = addr;
1388         al->cpumode = cpumode;
1389         al->filtered = 0;
1390
1391         if (machine == NULL) {
1392                 al->map = NULL;
1393                 return;
1394         }
1395
1396         if (cpumode == PERF_RECORD_MISC_KERNEL && perf_host) {
1397                 al->level = 'k';
1398                 mg = &machine->kmaps;
1399                 load_map = true;
1400         } else if (cpumode == PERF_RECORD_MISC_USER && perf_host) {
1401                 al->level = '.';
1402         } else if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL && perf_guest) {
1403                 al->level = 'g';
1404                 mg = &machine->kmaps;
1405                 load_map = true;
1406         } else if (cpumode == PERF_RECORD_MISC_GUEST_USER && perf_guest) {
1407                 al->level = 'u';
1408         } else {
1409                 al->level = 'H';
1410                 al->map = NULL;
1411
1412                 if ((cpumode == PERF_RECORD_MISC_GUEST_USER ||
1413                         cpumode == PERF_RECORD_MISC_GUEST_KERNEL) &&
1414                         !perf_guest)
1415                         al->filtered |= (1 << HIST_FILTER__GUEST);
1416                 if ((cpumode == PERF_RECORD_MISC_USER ||
1417                         cpumode == PERF_RECORD_MISC_KERNEL) &&
1418                         !perf_host)
1419                         al->filtered |= (1 << HIST_FILTER__HOST);
1420
1421                 return;
1422         }
1423 try_again:
1424         al->map = map_groups__find(mg, type, al->addr);
1425         if (al->map == NULL) {
1426                 /*
1427                  * If this is outside of all known maps, and is a negative
1428                  * address, try to look it up in the kernel dso, as it might be
1429                  * a vsyscall or vdso (which executes in user-mode).
1430                  *
1431                  * XXX This is nasty, we should have a symbol list in the
1432                  * "[vdso]" dso, but for now lets use the old trick of looking
1433                  * in the whole kernel symbol list.
1434                  */
1435                 if (cpumode == PERF_RECORD_MISC_USER && machine &&
1436                     mg != &machine->kmaps &&
1437                     machine__kernel_ip(machine, al->addr)) {
1438                         mg = &machine->kmaps;
1439                         load_map = true;
1440                         goto try_again;
1441                 }
1442         } else {
1443                 /*
1444                  * Kernel maps might be changed when loading symbols so loading
1445                  * must be done prior to using kernel maps.
1446                  */
1447                 if (load_map)
1448                         map__load(al->map);
1449                 al->addr = al->map->map_ip(al->map, al->addr);
1450         }
1451 }
1452
1453 void thread__find_addr_location(struct thread *thread,
1454                                 u8 cpumode, enum map_type type, u64 addr,
1455                                 struct addr_location *al)
1456 {
1457         thread__find_addr_map(thread, cpumode, type, addr, al);
1458         if (al->map != NULL)
1459                 al->sym = map__find_symbol(al->map, al->addr);
1460         else
1461                 al->sym = NULL;
1462 }
1463
1464 /*
1465  * Callers need to drop the reference to al->thread, obtained in
1466  * machine__findnew_thread()
1467  */
1468 int machine__resolve(struct machine *machine, struct addr_location *al,
1469                      struct perf_sample *sample)
1470 {
1471         struct thread *thread = machine__findnew_thread(machine, sample->pid,
1472                                                         sample->tid);
1473
1474         if (thread == NULL)
1475                 return -1;
1476
1477         dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread->tid);
1478         /*
1479          * Have we already created the kernel maps for this machine?
1480          *
1481          * This should have happened earlier, when we processed the kernel MMAP
1482          * events, but for older perf.data files there was no such thing, so do
1483          * it now.
1484          */
1485         if (sample->cpumode == PERF_RECORD_MISC_KERNEL &&
1486             machine__kernel_map(machine) == NULL)
1487                 machine__create_kernel_maps(machine);
1488
1489         thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->ip, al);
1490         dump_printf(" ...... dso: %s\n",
1491                     al->map ? al->map->dso->long_name :
1492                         al->level == 'H' ? "[hypervisor]" : "<not found>");
1493
1494         if (thread__is_filtered(thread))
1495                 al->filtered |= (1 << HIST_FILTER__THREAD);
1496
1497         al->sym = NULL;
1498         al->cpu = sample->cpu;
1499         al->socket = -1;
1500
1501         if (al->cpu >= 0) {
1502                 struct perf_env *env = machine->env;
1503
1504                 if (env && env->cpu)
1505                         al->socket = env->cpu[al->cpu].socket_id;
1506         }
1507
1508         if (al->map) {
1509                 struct dso *dso = al->map->dso;
1510
1511                 if (symbol_conf.dso_list &&
1512                     (!dso || !(strlist__has_entry(symbol_conf.dso_list,
1513                                                   dso->short_name) ||
1514                                (dso->short_name != dso->long_name &&
1515                                 strlist__has_entry(symbol_conf.dso_list,
1516                                                    dso->long_name))))) {
1517                         al->filtered |= (1 << HIST_FILTER__DSO);
1518                 }
1519
1520                 al->sym = map__find_symbol(al->map, al->addr);
1521         }
1522
1523         if (symbol_conf.sym_list &&
1524                 (!al->sym || !strlist__has_entry(symbol_conf.sym_list,
1525                                                 al->sym->name))) {
1526                 al->filtered |= (1 << HIST_FILTER__SYMBOL);
1527         }
1528
1529         return 0;
1530 }
1531
1532 /*
1533  * The preprocess_sample method will return with reference counts for the
1534  * in it, when done using (and perhaps getting ref counts if needing to
1535  * keep a pointer to one of those entries) it must be paired with
1536  * addr_location__put(), so that the refcounts can be decremented.
1537  */
1538 void addr_location__put(struct addr_location *al)
1539 {
1540         thread__zput(al->thread);
1541 }
1542
1543 bool is_bts_event(struct perf_event_attr *attr)
1544 {
1545         return attr->type == PERF_TYPE_HARDWARE &&
1546                (attr->config & PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1547                attr->sample_period == 1;
1548 }
1549
1550 bool sample_addr_correlates_sym(struct perf_event_attr *attr)
1551 {
1552         if (attr->type == PERF_TYPE_SOFTWARE &&
1553             (attr->config == PERF_COUNT_SW_PAGE_FAULTS ||
1554              attr->config == PERF_COUNT_SW_PAGE_FAULTS_MIN ||
1555              attr->config == PERF_COUNT_SW_PAGE_FAULTS_MAJ))
1556                 return true;
1557
1558         if (is_bts_event(attr))
1559                 return true;
1560
1561         return false;
1562 }
1563
1564 void thread__resolve(struct thread *thread, struct addr_location *al,
1565                      struct perf_sample *sample)
1566 {
1567         thread__find_addr_map(thread, sample->cpumode, MAP__FUNCTION, sample->addr, al);
1568         if (!al->map)
1569                 thread__find_addr_map(thread, sample->cpumode, MAP__VARIABLE,
1570                                       sample->addr, al);
1571
1572         al->cpu = sample->cpu;
1573         al->sym = NULL;
1574
1575         if (al->map)
1576                 al->sym = map__find_symbol(al->map, al->addr);
1577 }