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