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