]> git.itanic.dy.fi Git - linux-stable/blob - tools/perf/util/synthetic-events.c
perf tools: Honor namespace when synthesizing build-ids
[linux-stable] / tools / perf / util / synthetic-events.c
1 // SPDX-License-Identifier: GPL-2.0-only 
2
3 #include "util/cgroup.h"
4 #include "util/data.h"
5 #include "util/debug.h"
6 #include "util/dso.h"
7 #include "util/event.h"
8 #include "util/evlist.h"
9 #include "util/machine.h"
10 #include "util/map.h"
11 #include "util/map_symbol.h"
12 #include "util/branch.h"
13 #include "util/memswap.h"
14 #include "util/namespaces.h"
15 #include "util/session.h"
16 #include "util/stat.h"
17 #include "util/symbol.h"
18 #include "util/synthetic-events.h"
19 #include "util/target.h"
20 #include "util/time-utils.h"
21 #include <linux/bitops.h>
22 #include <linux/kernel.h>
23 #include <linux/string.h>
24 #include <linux/zalloc.h>
25 #include <linux/perf_event.h>
26 #include <asm/bug.h>
27 #include <perf/evsel.h>
28 #include <perf/cpumap.h>
29 #include <internal/lib.h> // page_size
30 #include <internal/threadmap.h>
31 #include <perf/threadmap.h>
32 #include <symbol/kallsyms.h>
33 #include <dirent.h>
34 #include <errno.h>
35 #include <inttypes.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
39 #include <api/fs/fs.h>
40 #include <api/io.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45
46 #define DEFAULT_PROC_MAP_PARSE_TIMEOUT 500
47
48 unsigned int proc_map_timeout = DEFAULT_PROC_MAP_PARSE_TIMEOUT;
49
50 int perf_tool__process_synth_event(struct perf_tool *tool,
51                                    union perf_event *event,
52                                    struct machine *machine,
53                                    perf_event__handler_t process)
54 {
55         struct perf_sample synth_sample = {
56                 .pid       = -1,
57                 .tid       = -1,
58                 .time      = -1,
59                 .stream_id = -1,
60                 .cpu       = -1,
61                 .period    = 1,
62                 .cpumode   = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
63         };
64
65         return process(tool, event, &synth_sample, machine);
66 };
67
68 /*
69  * Assumes that the first 4095 bytes of /proc/pid/stat contains
70  * the comm, tgid and ppid.
71  */
72 static int perf_event__get_comm_ids(pid_t pid, pid_t tid, char *comm, size_t len,
73                                     pid_t *tgid, pid_t *ppid, bool *kernel)
74 {
75         char bf[4096];
76         int fd;
77         size_t size = 0;
78         ssize_t n;
79         char *name, *tgids, *ppids, *vmpeak, *threads;
80
81         *tgid = -1;
82         *ppid = -1;
83
84         if (pid)
85                 snprintf(bf, sizeof(bf), "/proc/%d/task/%d/status", pid, tid);
86         else
87                 snprintf(bf, sizeof(bf), "/proc/%d/status", tid);
88
89         fd = open(bf, O_RDONLY);
90         if (fd < 0) {
91                 pr_debug("couldn't open %s\n", bf);
92                 return -1;
93         }
94
95         n = read(fd, bf, sizeof(bf) - 1);
96         close(fd);
97         if (n <= 0) {
98                 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
99                            tid);
100                 return -1;
101         }
102         bf[n] = '\0';
103
104         name = strstr(bf, "Name:");
105         tgids = strstr(name ?: bf, "Tgid:");
106         ppids = strstr(tgids ?: bf, "PPid:");
107         vmpeak = strstr(ppids ?: bf, "VmPeak:");
108
109         if (vmpeak)
110                 threads = NULL;
111         else
112                 threads = strstr(ppids ?: bf, "Threads:");
113
114         if (name) {
115                 char *nl;
116
117                 name = skip_spaces(name + 5);  /* strlen("Name:") */
118                 nl = strchr(name, '\n');
119                 if (nl)
120                         *nl = '\0';
121
122                 size = strlen(name);
123                 if (size >= len)
124                         size = len - 1;
125                 memcpy(comm, name, size);
126                 comm[size] = '\0';
127         } else {
128                 pr_debug("Name: string not found for pid %d\n", tid);
129         }
130
131         if (tgids) {
132                 tgids += 5;  /* strlen("Tgid:") */
133                 *tgid = atoi(tgids);
134         } else {
135                 pr_debug("Tgid: string not found for pid %d\n", tid);
136         }
137
138         if (ppids) {
139                 ppids += 5;  /* strlen("PPid:") */
140                 *ppid = atoi(ppids);
141         } else {
142                 pr_debug("PPid: string not found for pid %d\n", tid);
143         }
144
145         if (!vmpeak && threads)
146                 *kernel = true;
147         else
148                 *kernel = false;
149
150         return 0;
151 }
152
153 static int perf_event__prepare_comm(union perf_event *event, pid_t pid, pid_t tid,
154                                     struct machine *machine,
155                                     pid_t *tgid, pid_t *ppid, bool *kernel)
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, tid, event->comm.comm,
165                                              sizeof(event->comm.comm),
166                                              tgid, ppid, kernel) != 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 = tid;
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         bool kernel_thread;
197
198         if (perf_event__prepare_comm(event, 0, pid, machine, &tgid, &ppid,
199                                      &kernel_thread) != 0)
200                 return -1;
201
202         if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
203                 return -1;
204
205         return tgid;
206 }
207
208 static void perf_event__get_ns_link_info(pid_t pid, const char *ns,
209                                          struct perf_ns_link_info *ns_link_info)
210 {
211         struct stat64 st;
212         char proc_ns[128];
213
214         sprintf(proc_ns, "/proc/%u/ns/%s", pid, ns);
215         if (stat64(proc_ns, &st) == 0) {
216                 ns_link_info->dev = st.st_dev;
217                 ns_link_info->ino = st.st_ino;
218         }
219 }
220
221 int perf_event__synthesize_namespaces(struct perf_tool *tool,
222                                       union perf_event *event,
223                                       pid_t pid, pid_t tgid,
224                                       perf_event__handler_t process,
225                                       struct machine *machine)
226 {
227         u32 idx;
228         struct perf_ns_link_info *ns_link_info;
229
230         if (!tool || !tool->namespace_events)
231                 return 0;
232
233         memset(&event->namespaces, 0, (sizeof(event->namespaces) +
234                (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
235                machine->id_hdr_size));
236
237         event->namespaces.pid = tgid;
238         event->namespaces.tid = pid;
239
240         event->namespaces.nr_namespaces = NR_NAMESPACES;
241
242         ns_link_info = event->namespaces.link_info;
243
244         for (idx = 0; idx < event->namespaces.nr_namespaces; idx++)
245                 perf_event__get_ns_link_info(pid, perf_ns__name(idx),
246                                              &ns_link_info[idx]);
247
248         event->namespaces.header.type = PERF_RECORD_NAMESPACES;
249
250         event->namespaces.header.size = (sizeof(event->namespaces) +
251                         (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
252                         machine->id_hdr_size);
253
254         if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
255                 return -1;
256
257         return 0;
258 }
259
260 static int perf_event__synthesize_fork(struct perf_tool *tool,
261                                        union perf_event *event,
262                                        pid_t pid, pid_t tgid, pid_t ppid,
263                                        perf_event__handler_t process,
264                                        struct machine *machine)
265 {
266         memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
267
268         /*
269          * for main thread set parent to ppid from status file. For other
270          * threads set parent pid to main thread. ie., assume main thread
271          * spawns all threads in a process
272         */
273         if (tgid == pid) {
274                 event->fork.ppid = ppid;
275                 event->fork.ptid = ppid;
276         } else {
277                 event->fork.ppid = tgid;
278                 event->fork.ptid = tgid;
279         }
280         event->fork.pid  = tgid;
281         event->fork.tid  = pid;
282         event->fork.header.type = PERF_RECORD_FORK;
283         event->fork.header.misc = PERF_RECORD_MISC_FORK_EXEC;
284
285         event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
286
287         if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
288                 return -1;
289
290         return 0;
291 }
292
293 static bool read_proc_maps_line(struct io *io, __u64 *start, __u64 *end,
294                                 u32 *prot, u32 *flags, __u64 *offset,
295                                 u32 *maj, u32 *min,
296                                 __u64 *inode,
297                                 ssize_t pathname_size, char *pathname)
298 {
299         __u64 temp;
300         int ch;
301         char *start_pathname = pathname;
302
303         if (io__get_hex(io, start) != '-')
304                 return false;
305         if (io__get_hex(io, end) != ' ')
306                 return false;
307
308         /* map protection and flags bits */
309         *prot = 0;
310         ch = io__get_char(io);
311         if (ch == 'r')
312                 *prot |= PROT_READ;
313         else if (ch != '-')
314                 return false;
315         ch = io__get_char(io);
316         if (ch == 'w')
317                 *prot |= PROT_WRITE;
318         else if (ch != '-')
319                 return false;
320         ch = io__get_char(io);
321         if (ch == 'x')
322                 *prot |= PROT_EXEC;
323         else if (ch != '-')
324                 return false;
325         ch = io__get_char(io);
326         if (ch == 's')
327                 *flags = MAP_SHARED;
328         else if (ch == 'p')
329                 *flags = MAP_PRIVATE;
330         else
331                 return false;
332         if (io__get_char(io) != ' ')
333                 return false;
334
335         if (io__get_hex(io, offset) != ' ')
336                 return false;
337
338         if (io__get_hex(io, &temp) != ':')
339                 return false;
340         *maj = temp;
341         if (io__get_hex(io, &temp) != ' ')
342                 return false;
343         *min = temp;
344
345         ch = io__get_dec(io, inode);
346         if (ch != ' ') {
347                 *pathname = '\0';
348                 return ch == '\n';
349         }
350         do {
351                 ch = io__get_char(io);
352         } while (ch == ' ');
353         while (true) {
354                 if (ch < 0)
355                         return false;
356                 if (ch == '\0' || ch == '\n' ||
357                     (pathname + 1 - start_pathname) >= pathname_size) {
358                         *pathname = '\0';
359                         return true;
360                 }
361                 *pathname++ = ch;
362                 ch = io__get_char(io);
363         }
364 }
365
366 static void perf_record_mmap2__read_build_id(struct perf_record_mmap2 *event,
367                                              bool is_kernel)
368 {
369         struct build_id bid;
370         struct nsinfo *nsi;
371         struct nscookie nc;
372         int rc;
373
374         if (is_kernel) {
375                 rc = sysfs__read_build_id("/sys/kernel/notes", &bid);
376                 goto out;
377         }
378
379         nsi = nsinfo__new(event->pid);
380         nsinfo__mountns_enter(nsi, &nc);
381
382         rc = filename__read_build_id(event->filename, &bid) > 0 ? 0 : -1;
383
384         nsinfo__mountns_exit(&nc);
385         nsinfo__put(nsi);
386
387 out:
388         if (rc == 0) {
389                 memcpy(event->build_id, bid.data, sizeof(bid.data));
390                 event->build_id_size = (u8) bid.size;
391                 event->header.misc |= PERF_RECORD_MISC_MMAP_BUILD_ID;
392                 event->__reserved_1 = 0;
393                 event->__reserved_2 = 0;
394         } else {
395                 if (event->filename[0] == '/') {
396                         pr_debug2("Failed to read build ID for %s\n",
397                                   event->filename);
398                 }
399         }
400 }
401
402 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
403                                        union perf_event *event,
404                                        pid_t pid, pid_t tgid,
405                                        perf_event__handler_t process,
406                                        struct machine *machine,
407                                        bool mmap_data)
408 {
409         unsigned long long t;
410         char bf[BUFSIZ];
411         struct io io;
412         bool truncation = false;
413         unsigned long long timeout = proc_map_timeout * 1000000ULL;
414         int rc = 0;
415         const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
416         int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
417
418         if (machine__is_default_guest(machine))
419                 return 0;
420
421         snprintf(bf, sizeof(bf), "%s/proc/%d/task/%d/maps",
422                 machine->root_dir, pid, pid);
423
424         io.fd = open(bf, O_RDONLY, 0);
425         if (io.fd < 0) {
426                 /*
427                  * We raced with a task exiting - just return:
428                  */
429                 pr_debug("couldn't open %s\n", bf);
430                 return -1;
431         }
432         io__init(&io, io.fd, bf, sizeof(bf));
433
434         event->header.type = PERF_RECORD_MMAP2;
435         t = rdclock();
436
437         while (!io.eof) {
438                 static const char anonstr[] = "//anon";
439                 size_t size, aligned_size;
440
441                 /* ensure null termination since stack will be reused. */
442                 event->mmap2.filename[0] = '\0';
443
444                 /* 00400000-0040c000 r-xp 00000000 fd:01 41038  /bin/cat */
445                 if (!read_proc_maps_line(&io,
446                                         &event->mmap2.start,
447                                         &event->mmap2.len,
448                                         &event->mmap2.prot,
449                                         &event->mmap2.flags,
450                                         &event->mmap2.pgoff,
451                                         &event->mmap2.maj,
452                                         &event->mmap2.min,
453                                         &event->mmap2.ino,
454                                         sizeof(event->mmap2.filename),
455                                         event->mmap2.filename))
456                         continue;
457
458                 if ((rdclock() - t) > timeout) {
459                         pr_warning("Reading %s/proc/%d/task/%d/maps time out. "
460                                    "You may want to increase "
461                                    "the time limit by --proc-map-timeout\n",
462                                    machine->root_dir, pid, pid);
463                         truncation = true;
464                         goto out;
465                 }
466
467                 event->mmap2.ino_generation = 0;
468
469                 /*
470                  * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
471                  */
472                 if (machine__is_host(machine))
473                         event->header.misc = PERF_RECORD_MISC_USER;
474                 else
475                         event->header.misc = PERF_RECORD_MISC_GUEST_USER;
476
477                 if ((event->mmap2.prot & PROT_EXEC) == 0) {
478                         if (!mmap_data || (event->mmap2.prot & PROT_READ) == 0)
479                                 continue;
480
481                         event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
482                 }
483
484 out:
485                 if (truncation)
486                         event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
487
488                 if (!strcmp(event->mmap2.filename, ""))
489                         strcpy(event->mmap2.filename, anonstr);
490
491                 if (hugetlbfs_mnt_len &&
492                     !strncmp(event->mmap2.filename, hugetlbfs_mnt,
493                              hugetlbfs_mnt_len)) {
494                         strcpy(event->mmap2.filename, anonstr);
495                         event->mmap2.flags |= MAP_HUGETLB;
496                 }
497
498                 size = strlen(event->mmap2.filename) + 1;
499                 aligned_size = PERF_ALIGN(size, sizeof(u64));
500                 event->mmap2.len -= event->mmap.start;
501                 event->mmap2.header.size = (sizeof(event->mmap2) -
502                                         (sizeof(event->mmap2.filename) - aligned_size));
503                 memset(event->mmap2.filename + size, 0, machine->id_hdr_size +
504                         (aligned_size - size));
505                 event->mmap2.header.size += machine->id_hdr_size;
506                 event->mmap2.pid = tgid;
507                 event->mmap2.tid = pid;
508
509                 if (symbol_conf.buildid_mmap2)
510                         perf_record_mmap2__read_build_id(&event->mmap2, false);
511
512                 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
513                         rc = -1;
514                         break;
515                 }
516
517                 if (truncation)
518                         break;
519         }
520
521         close(io.fd);
522         return rc;
523 }
524
525 #ifdef HAVE_FILE_HANDLE
526 static int perf_event__synthesize_cgroup(struct perf_tool *tool,
527                                          union perf_event *event,
528                                          char *path, size_t mount_len,
529                                          perf_event__handler_t process,
530                                          struct machine *machine)
531 {
532         size_t event_size = sizeof(event->cgroup) - sizeof(event->cgroup.path);
533         size_t path_len = strlen(path) - mount_len + 1;
534         struct {
535                 struct file_handle fh;
536                 uint64_t cgroup_id;
537         } handle;
538         int mount_id;
539
540         while (path_len % sizeof(u64))
541                 path[mount_len + path_len++] = '\0';
542
543         memset(&event->cgroup, 0, event_size);
544
545         event->cgroup.header.type = PERF_RECORD_CGROUP;
546         event->cgroup.header.size = event_size + path_len + machine->id_hdr_size;
547
548         handle.fh.handle_bytes = sizeof(handle.cgroup_id);
549         if (name_to_handle_at(AT_FDCWD, path, &handle.fh, &mount_id, 0) < 0) {
550                 pr_debug("stat failed: %s\n", path);
551                 return -1;
552         }
553
554         event->cgroup.id = handle.cgroup_id;
555         strncpy(event->cgroup.path, path + mount_len, path_len);
556         memset(event->cgroup.path + path_len, 0, machine->id_hdr_size);
557
558         if (perf_tool__process_synth_event(tool, event, machine, process) < 0) {
559                 pr_debug("process synth event failed\n");
560                 return -1;
561         }
562
563         return 0;
564 }
565
566 static int perf_event__walk_cgroup_tree(struct perf_tool *tool,
567                                         union perf_event *event,
568                                         char *path, size_t mount_len,
569                                         perf_event__handler_t process,
570                                         struct machine *machine)
571 {
572         size_t pos = strlen(path);
573         DIR *d;
574         struct dirent *dent;
575         int ret = 0;
576
577         if (perf_event__synthesize_cgroup(tool, event, path, mount_len,
578                                           process, machine) < 0)
579                 return -1;
580
581         d = opendir(path);
582         if (d == NULL) {
583                 pr_debug("failed to open directory: %s\n", path);
584                 return -1;
585         }
586
587         while ((dent = readdir(d)) != NULL) {
588                 if (dent->d_type != DT_DIR)
589                         continue;
590                 if (!strcmp(dent->d_name, ".") ||
591                     !strcmp(dent->d_name, ".."))
592                         continue;
593
594                 /* any sane path should be less than PATH_MAX */
595                 if (strlen(path) + strlen(dent->d_name) + 1 >= PATH_MAX)
596                         continue;
597
598                 if (path[pos - 1] != '/')
599                         strcat(path, "/");
600                 strcat(path, dent->d_name);
601
602                 ret = perf_event__walk_cgroup_tree(tool, event, path,
603                                                    mount_len, process, machine);
604                 if (ret < 0)
605                         break;
606
607                 path[pos] = '\0';
608         }
609
610         closedir(d);
611         return ret;
612 }
613
614 int perf_event__synthesize_cgroups(struct perf_tool *tool,
615                                    perf_event__handler_t process,
616                                    struct machine *machine)
617 {
618         union perf_event event;
619         char cgrp_root[PATH_MAX];
620         size_t mount_len;  /* length of mount point in the path */
621
622         if (!tool || !tool->cgroup_events)
623                 return 0;
624
625         if (cgroupfs_find_mountpoint(cgrp_root, PATH_MAX, "perf_event") < 0) {
626                 pr_debug("cannot find cgroup mount point\n");
627                 return -1;
628         }
629
630         mount_len = strlen(cgrp_root);
631         /* make sure the path starts with a slash (after mount point) */
632         strcat(cgrp_root, "/");
633
634         if (perf_event__walk_cgroup_tree(tool, &event, cgrp_root, mount_len,
635                                          process, machine) < 0)
636                 return -1;
637
638         return 0;
639 }
640 #else
641 int perf_event__synthesize_cgroups(struct perf_tool *tool __maybe_unused,
642                                    perf_event__handler_t process __maybe_unused,
643                                    struct machine *machine __maybe_unused)
644 {
645         return -1;
646 }
647 #endif
648
649 int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t process,
650                                    struct machine *machine)
651 {
652         int rc = 0;
653         struct map *pos;
654         struct maps *maps = machine__kernel_maps(machine);
655         union perf_event *event;
656         size_t size = symbol_conf.buildid_mmap2 ?
657                         sizeof(event->mmap2) : sizeof(event->mmap);
658
659         event = zalloc(size + machine->id_hdr_size);
660         if (event == NULL) {
661                 pr_debug("Not enough memory synthesizing mmap event "
662                          "for kernel modules\n");
663                 return -1;
664         }
665
666         /*
667          * kernel uses 0 for user space maps, see kernel/perf_event.c
668          * __perf_event_mmap
669          */
670         if (machine__is_host(machine))
671                 event->header.misc = PERF_RECORD_MISC_KERNEL;
672         else
673                 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
674
675         maps__for_each_entry(maps, pos) {
676                 if (!__map__is_kmodule(pos))
677                         continue;
678
679                 if (symbol_conf.buildid_mmap2) {
680                         size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
681                         event->mmap2.header.type = PERF_RECORD_MMAP2;
682                         event->mmap2.header.size = (sizeof(event->mmap2) -
683                                                 (sizeof(event->mmap2.filename) - size));
684                         memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
685                         event->mmap2.header.size += machine->id_hdr_size;
686                         event->mmap2.start = pos->start;
687                         event->mmap2.len   = pos->end - pos->start;
688                         event->mmap2.pid   = machine->pid;
689
690                         memcpy(event->mmap2.filename, pos->dso->long_name,
691                                pos->dso->long_name_len + 1);
692
693                         perf_record_mmap2__read_build_id(&event->mmap2, false);
694                 } else {
695                         size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
696                         event->mmap.header.type = PERF_RECORD_MMAP;
697                         event->mmap.header.size = (sizeof(event->mmap) -
698                                                 (sizeof(event->mmap.filename) - size));
699                         memset(event->mmap.filename + size, 0, machine->id_hdr_size);
700                         event->mmap.header.size += machine->id_hdr_size;
701                         event->mmap.start = pos->start;
702                         event->mmap.len   = pos->end - pos->start;
703                         event->mmap.pid   = machine->pid;
704
705                         memcpy(event->mmap.filename, pos->dso->long_name,
706                                pos->dso->long_name_len + 1);
707                 }
708
709                 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
710                         rc = -1;
711                         break;
712                 }
713         }
714
715         free(event);
716         return rc;
717 }
718
719 static int filter_task(const struct dirent *dirent)
720 {
721         return isdigit(dirent->d_name[0]);
722 }
723
724 static int __event__synthesize_thread(union perf_event *comm_event,
725                                       union perf_event *mmap_event,
726                                       union perf_event *fork_event,
727                                       union perf_event *namespaces_event,
728                                       pid_t pid, int full, perf_event__handler_t process,
729                                       struct perf_tool *tool, struct machine *machine,
730                                       bool needs_mmap, bool mmap_data)
731 {
732         char filename[PATH_MAX];
733         struct dirent **dirent;
734         pid_t tgid, ppid;
735         int rc = 0;
736         int i, n;
737
738         /* special case: only send one comm event using passed in pid */
739         if (!full) {
740                 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
741                                                    process, machine);
742
743                 if (tgid == -1)
744                         return -1;
745
746                 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
747                                                       tgid, process, machine) < 0)
748                         return -1;
749
750                 /*
751                  * send mmap only for thread group leader
752                  * see thread__init_maps()
753                  */
754                 if (pid == tgid && needs_mmap &&
755                     perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
756                                                        process, machine, mmap_data))
757                         return -1;
758
759                 return 0;
760         }
761
762         if (machine__is_default_guest(machine))
763                 return 0;
764
765         snprintf(filename, sizeof(filename), "%s/proc/%d/task",
766                  machine->root_dir, pid);
767
768         n = scandir(filename, &dirent, filter_task, NULL);
769         if (n < 0)
770                 return n;
771
772         for (i = 0; i < n; i++) {
773                 char *end;
774                 pid_t _pid;
775                 bool kernel_thread = false;
776
777                 _pid = strtol(dirent[i]->d_name, &end, 10);
778                 if (*end)
779                         continue;
780
781                 /* some threads may exit just after scan, ignore it */
782                 if (perf_event__prepare_comm(comm_event, pid, _pid, machine,
783                                              &tgid, &ppid, &kernel_thread) != 0)
784                         continue;
785
786                 rc = -1;
787                 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
788                                                 ppid, process, machine) < 0)
789                         break;
790
791                 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
792                                                       tgid, process, machine) < 0)
793                         break;
794
795                 /*
796                  * Send the prepared comm event
797                  */
798                 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
799                         break;
800
801                 rc = 0;
802                 if (_pid == pid && !kernel_thread && needs_mmap) {
803                         /* process the parent's maps too */
804                         rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
805                                                 process, machine, mmap_data);
806                         if (rc)
807                                 break;
808                 }
809         }
810
811         for (i = 0; i < n; i++)
812                 zfree(&dirent[i]);
813         free(dirent);
814
815         return rc;
816 }
817
818 int perf_event__synthesize_thread_map(struct perf_tool *tool,
819                                       struct perf_thread_map *threads,
820                                       perf_event__handler_t process,
821                                       struct machine *machine,
822                                       bool needs_mmap, bool mmap_data)
823 {
824         union perf_event *comm_event, *mmap_event, *fork_event;
825         union perf_event *namespaces_event;
826         int err = -1, thread, j;
827
828         comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
829         if (comm_event == NULL)
830                 goto out;
831
832         mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
833         if (mmap_event == NULL)
834                 goto out_free_comm;
835
836         fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
837         if (fork_event == NULL)
838                 goto out_free_mmap;
839
840         namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
841                                   (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
842                                   machine->id_hdr_size);
843         if (namespaces_event == NULL)
844                 goto out_free_fork;
845
846         err = 0;
847         for (thread = 0; thread < threads->nr; ++thread) {
848                 if (__event__synthesize_thread(comm_event, mmap_event,
849                                                fork_event, namespaces_event,
850                                                perf_thread_map__pid(threads, thread), 0,
851                                                process, tool, machine,
852                                                needs_mmap, mmap_data)) {
853                         err = -1;
854                         break;
855                 }
856
857                 /*
858                  * comm.pid is set to thread group id by
859                  * perf_event__synthesize_comm
860                  */
861                 if ((int) comm_event->comm.pid != perf_thread_map__pid(threads, thread)) {
862                         bool need_leader = true;
863
864                         /* is thread group leader in thread_map? */
865                         for (j = 0; j < threads->nr; ++j) {
866                                 if ((int) comm_event->comm.pid == perf_thread_map__pid(threads, j)) {
867                                         need_leader = false;
868                                         break;
869                                 }
870                         }
871
872                         /* if not, generate events for it */
873                         if (need_leader &&
874                             __event__synthesize_thread(comm_event, mmap_event,
875                                                        fork_event, namespaces_event,
876                                                        comm_event->comm.pid, 0,
877                                                        process, tool, machine,
878                                                        needs_mmap, mmap_data)) {
879                                 err = -1;
880                                 break;
881                         }
882                 }
883         }
884         free(namespaces_event);
885 out_free_fork:
886         free(fork_event);
887 out_free_mmap:
888         free(mmap_event);
889 out_free_comm:
890         free(comm_event);
891 out:
892         return err;
893 }
894
895 static int __perf_event__synthesize_threads(struct perf_tool *tool,
896                                             perf_event__handler_t process,
897                                             struct machine *machine,
898                                             bool needs_mmap,
899                                             bool mmap_data,
900                                             struct dirent **dirent,
901                                             int start,
902                                             int num)
903 {
904         union perf_event *comm_event, *mmap_event, *fork_event;
905         union perf_event *namespaces_event;
906         int err = -1;
907         char *end;
908         pid_t pid;
909         int i;
910
911         comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
912         if (comm_event == NULL)
913                 goto out;
914
915         mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
916         if (mmap_event == NULL)
917                 goto out_free_comm;
918
919         fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
920         if (fork_event == NULL)
921                 goto out_free_mmap;
922
923         namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
924                                   (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
925                                   machine->id_hdr_size);
926         if (namespaces_event == NULL)
927                 goto out_free_fork;
928
929         for (i = start; i < start + num; i++) {
930                 if (!isdigit(dirent[i]->d_name[0]))
931                         continue;
932
933                 pid = (pid_t)strtol(dirent[i]->d_name, &end, 10);
934                 /* only interested in proper numerical dirents */
935                 if (*end)
936                         continue;
937                 /*
938                  * We may race with exiting thread, so don't stop just because
939                  * one thread couldn't be synthesized.
940                  */
941                 __event__synthesize_thread(comm_event, mmap_event, fork_event,
942                                            namespaces_event, pid, 1, process,
943                                            tool, machine, needs_mmap, mmap_data);
944         }
945         err = 0;
946
947         free(namespaces_event);
948 out_free_fork:
949         free(fork_event);
950 out_free_mmap:
951         free(mmap_event);
952 out_free_comm:
953         free(comm_event);
954 out:
955         return err;
956 }
957
958 struct synthesize_threads_arg {
959         struct perf_tool *tool;
960         perf_event__handler_t process;
961         struct machine *machine;
962         bool needs_mmap;
963         bool mmap_data;
964         struct dirent **dirent;
965         int num;
966         int start;
967 };
968
969 static void *synthesize_threads_worker(void *arg)
970 {
971         struct synthesize_threads_arg *args = arg;
972
973         __perf_event__synthesize_threads(args->tool, args->process,
974                                          args->machine,
975                                          args->needs_mmap, args->mmap_data,
976                                          args->dirent,
977                                          args->start, args->num);
978         return NULL;
979 }
980
981 int perf_event__synthesize_threads(struct perf_tool *tool,
982                                    perf_event__handler_t process,
983                                    struct machine *machine,
984                                    bool needs_mmap, bool mmap_data,
985                                    unsigned int nr_threads_synthesize)
986 {
987         struct synthesize_threads_arg *args = NULL;
988         pthread_t *synthesize_threads = NULL;
989         char proc_path[PATH_MAX];
990         struct dirent **dirent;
991         int num_per_thread;
992         int m, n, i, j;
993         int thread_nr;
994         int base = 0;
995         int err = -1;
996
997
998         if (machine__is_default_guest(machine))
999                 return 0;
1000
1001         snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
1002         n = scandir(proc_path, &dirent, filter_task, NULL);
1003         if (n < 0)
1004                 return err;
1005
1006         if (nr_threads_synthesize == UINT_MAX)
1007                 thread_nr = sysconf(_SC_NPROCESSORS_ONLN);
1008         else
1009                 thread_nr = nr_threads_synthesize;
1010
1011         if (thread_nr <= 1) {
1012                 err = __perf_event__synthesize_threads(tool, process,
1013                                                        machine,
1014                                                        needs_mmap, mmap_data,
1015                                                        dirent, base, n);
1016                 goto free_dirent;
1017         }
1018         if (thread_nr > n)
1019                 thread_nr = n;
1020
1021         synthesize_threads = calloc(sizeof(pthread_t), thread_nr);
1022         if (synthesize_threads == NULL)
1023                 goto free_dirent;
1024
1025         args = calloc(sizeof(*args), thread_nr);
1026         if (args == NULL)
1027                 goto free_threads;
1028
1029         num_per_thread = n / thread_nr;
1030         m = n % thread_nr;
1031         for (i = 0; i < thread_nr; i++) {
1032                 args[i].tool = tool;
1033                 args[i].process = process;
1034                 args[i].machine = machine;
1035                 args[i].needs_mmap = needs_mmap;
1036                 args[i].mmap_data = mmap_data;
1037                 args[i].dirent = dirent;
1038         }
1039         for (i = 0; i < m; i++) {
1040                 args[i].num = num_per_thread + 1;
1041                 args[i].start = i * args[i].num;
1042         }
1043         if (i != 0)
1044                 base = args[i-1].start + args[i-1].num;
1045         for (j = i; j < thread_nr; j++) {
1046                 args[j].num = num_per_thread;
1047                 args[j].start = base + (j - i) * args[i].num;
1048         }
1049
1050         for (i = 0; i < thread_nr; i++) {
1051                 if (pthread_create(&synthesize_threads[i], NULL,
1052                                    synthesize_threads_worker, &args[i]))
1053                         goto out_join;
1054         }
1055         err = 0;
1056 out_join:
1057         for (i = 0; i < thread_nr; i++)
1058                 pthread_join(synthesize_threads[i], NULL);
1059         free(args);
1060 free_threads:
1061         free(synthesize_threads);
1062 free_dirent:
1063         for (i = 0; i < n; i++)
1064                 zfree(&dirent[i]);
1065         free(dirent);
1066
1067         return err;
1068 }
1069
1070 int __weak perf_event__synthesize_extra_kmaps(struct perf_tool *tool __maybe_unused,
1071                                               perf_event__handler_t process __maybe_unused,
1072                                               struct machine *machine __maybe_unused)
1073 {
1074         return 0;
1075 }
1076
1077 static int __perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
1078                                                 perf_event__handler_t process,
1079                                                 struct machine *machine)
1080 {
1081         union perf_event *event;
1082         size_t size = symbol_conf.buildid_mmap2 ?
1083                         sizeof(event->mmap2) : sizeof(event->mmap);
1084         struct map *map = machine__kernel_map(machine);
1085         struct kmap *kmap;
1086         int err;
1087
1088         if (map == NULL)
1089                 return -1;
1090
1091         kmap = map__kmap(map);
1092         if (!kmap->ref_reloc_sym)
1093                 return -1;
1094
1095         /*
1096          * We should get this from /sys/kernel/sections/.text, but till that is
1097          * available use this, and after it is use this as a fallback for older
1098          * kernels.
1099          */
1100         event = zalloc(size + machine->id_hdr_size);
1101         if (event == NULL) {
1102                 pr_debug("Not enough memory synthesizing mmap event "
1103                          "for kernel modules\n");
1104                 return -1;
1105         }
1106
1107         if (machine__is_host(machine)) {
1108                 /*
1109                  * kernel uses PERF_RECORD_MISC_USER for user space maps,
1110                  * see kernel/perf_event.c __perf_event_mmap
1111                  */
1112                 event->header.misc = PERF_RECORD_MISC_KERNEL;
1113         } else {
1114                 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
1115         }
1116
1117         if (symbol_conf.buildid_mmap2) {
1118                 size = snprintf(event->mmap2.filename, sizeof(event->mmap2.filename),
1119                                 "%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1;
1120                 size = PERF_ALIGN(size, sizeof(u64));
1121                 event->mmap2.header.type = PERF_RECORD_MMAP2;
1122                 event->mmap2.header.size = (sizeof(event->mmap2) -
1123                                 (sizeof(event->mmap2.filename) - size) + machine->id_hdr_size);
1124                 event->mmap2.pgoff = kmap->ref_reloc_sym->addr;
1125                 event->mmap2.start = map->start;
1126                 event->mmap2.len   = map->end - event->mmap.start;
1127                 event->mmap2.pid   = machine->pid;
1128
1129                 perf_record_mmap2__read_build_id(&event->mmap2, true);
1130         } else {
1131                 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
1132                                 "%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1;
1133                 size = PERF_ALIGN(size, sizeof(u64));
1134                 event->mmap.header.type = PERF_RECORD_MMAP;
1135                 event->mmap.header.size = (sizeof(event->mmap) -
1136                                 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
1137                 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
1138                 event->mmap.start = map->start;
1139                 event->mmap.len   = map->end - event->mmap.start;
1140                 event->mmap.pid   = machine->pid;
1141         }
1142
1143         err = perf_tool__process_synth_event(tool, event, machine, process);
1144         free(event);
1145
1146         return err;
1147 }
1148
1149 int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
1150                                        perf_event__handler_t process,
1151                                        struct machine *machine)
1152 {
1153         int err;
1154
1155         err = __perf_event__synthesize_kernel_mmap(tool, process, machine);
1156         if (err < 0)
1157                 return err;
1158
1159         return perf_event__synthesize_extra_kmaps(tool, process, machine);
1160 }
1161
1162 int perf_event__synthesize_thread_map2(struct perf_tool *tool,
1163                                       struct perf_thread_map *threads,
1164                                       perf_event__handler_t process,
1165                                       struct machine *machine)
1166 {
1167         union perf_event *event;
1168         int i, err, size;
1169
1170         size  = sizeof(event->thread_map);
1171         size += threads->nr * sizeof(event->thread_map.entries[0]);
1172
1173         event = zalloc(size);
1174         if (!event)
1175                 return -ENOMEM;
1176
1177         event->header.type = PERF_RECORD_THREAD_MAP;
1178         event->header.size = size;
1179         event->thread_map.nr = threads->nr;
1180
1181         for (i = 0; i < threads->nr; i++) {
1182                 struct perf_record_thread_map_entry *entry = &event->thread_map.entries[i];
1183                 char *comm = perf_thread_map__comm(threads, i);
1184
1185                 if (!comm)
1186                         comm = (char *) "";
1187
1188                 entry->pid = perf_thread_map__pid(threads, i);
1189                 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
1190         }
1191
1192         err = process(tool, event, NULL, machine);
1193
1194         free(event);
1195         return err;
1196 }
1197
1198 static void synthesize_cpus(struct cpu_map_entries *cpus,
1199                             struct perf_cpu_map *map)
1200 {
1201         int i, map_nr = perf_cpu_map__nr(map);
1202
1203         cpus->nr = map_nr;
1204
1205         for (i = 0; i < map_nr; i++)
1206                 cpus->cpu[i] = perf_cpu_map__cpu(map, i).cpu;
1207 }
1208
1209 static void synthesize_mask(struct perf_record_record_cpu_map *mask,
1210                             struct perf_cpu_map *map, int max)
1211 {
1212         int i;
1213
1214         mask->nr = BITS_TO_LONGS(max);
1215         mask->long_size = sizeof(long);
1216
1217         for (i = 0; i < perf_cpu_map__nr(map); i++)
1218                 set_bit(perf_cpu_map__cpu(map, i).cpu, mask->mask);
1219 }
1220
1221 static size_t cpus_size(struct perf_cpu_map *map)
1222 {
1223         return sizeof(struct cpu_map_entries) + perf_cpu_map__nr(map) * sizeof(u16);
1224 }
1225
1226 static size_t mask_size(struct perf_cpu_map *map, int *max)
1227 {
1228         int i;
1229
1230         *max = 0;
1231
1232         for (i = 0; i < perf_cpu_map__nr(map); i++) {
1233                 /* bit position of the cpu is + 1 */
1234                 int bit = perf_cpu_map__cpu(map, i).cpu + 1;
1235
1236                 if (bit > *max)
1237                         *max = bit;
1238         }
1239
1240         return sizeof(struct perf_record_record_cpu_map) + BITS_TO_LONGS(*max) * sizeof(long);
1241 }
1242
1243 void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max)
1244 {
1245         size_t size_cpus, size_mask;
1246         bool is_dummy = perf_cpu_map__empty(map);
1247
1248         /*
1249          * Both array and mask data have variable size based
1250          * on the number of cpus and their actual values.
1251          * The size of the 'struct perf_record_cpu_map_data' is:
1252          *
1253          *   array = size of 'struct cpu_map_entries' +
1254          *           number of cpus * sizeof(u64)
1255          *
1256          *   mask  = size of 'struct perf_record_record_cpu_map' +
1257          *           maximum cpu bit converted to size of longs
1258          *
1259          * and finally + the size of 'struct perf_record_cpu_map_data'.
1260          */
1261         size_cpus = cpus_size(map);
1262         size_mask = mask_size(map, max);
1263
1264         if (is_dummy || (size_cpus < size_mask)) {
1265                 *size += size_cpus;
1266                 *type  = PERF_CPU_MAP__CPUS;
1267         } else {
1268                 *size += size_mask;
1269                 *type  = PERF_CPU_MAP__MASK;
1270         }
1271
1272         *size += sizeof(struct perf_record_cpu_map_data);
1273         *size = PERF_ALIGN(*size, sizeof(u64));
1274         return zalloc(*size);
1275 }
1276
1277 void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
1278                               u16 type, int max)
1279 {
1280         data->type = type;
1281
1282         switch (type) {
1283         case PERF_CPU_MAP__CPUS:
1284                 synthesize_cpus((struct cpu_map_entries *) data->data, map);
1285                 break;
1286         case PERF_CPU_MAP__MASK:
1287                 synthesize_mask((struct perf_record_record_cpu_map *)data->data, map, max);
1288         default:
1289                 break;
1290         }
1291 }
1292
1293 static struct perf_record_cpu_map *cpu_map_event__new(struct perf_cpu_map *map)
1294 {
1295         size_t size = sizeof(struct perf_record_cpu_map);
1296         struct perf_record_cpu_map *event;
1297         int max;
1298         u16 type;
1299
1300         event = cpu_map_data__alloc(map, &size, &type, &max);
1301         if (!event)
1302                 return NULL;
1303
1304         event->header.type = PERF_RECORD_CPU_MAP;
1305         event->header.size = size;
1306         event->data.type   = type;
1307
1308         cpu_map_data__synthesize(&event->data, map, type, max);
1309         return event;
1310 }
1311
1312 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
1313                                    struct perf_cpu_map *map,
1314                                    perf_event__handler_t process,
1315                                    struct machine *machine)
1316 {
1317         struct perf_record_cpu_map *event;
1318         int err;
1319
1320         event = cpu_map_event__new(map);
1321         if (!event)
1322                 return -ENOMEM;
1323
1324         err = process(tool, (union perf_event *) event, NULL, machine);
1325
1326         free(event);
1327         return err;
1328 }
1329
1330 int perf_event__synthesize_stat_config(struct perf_tool *tool,
1331                                        struct perf_stat_config *config,
1332                                        perf_event__handler_t process,
1333                                        struct machine *machine)
1334 {
1335         struct perf_record_stat_config *event;
1336         int size, i = 0, err;
1337
1338         size  = sizeof(*event);
1339         size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1340
1341         event = zalloc(size);
1342         if (!event)
1343                 return -ENOMEM;
1344
1345         event->header.type = PERF_RECORD_STAT_CONFIG;
1346         event->header.size = size;
1347         event->nr          = PERF_STAT_CONFIG_TERM__MAX;
1348
1349 #define ADD(__term, __val)                                      \
1350         event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term;   \
1351         event->data[i].val = __val;                             \
1352         i++;
1353
1354         ADD(AGGR_MODE,  config->aggr_mode)
1355         ADD(INTERVAL,   config->interval)
1356         ADD(SCALE,      config->scale)
1357
1358         WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1359                   "stat config terms unbalanced\n");
1360 #undef ADD
1361
1362         err = process(tool, (union perf_event *) event, NULL, machine);
1363
1364         free(event);
1365         return err;
1366 }
1367
1368 int perf_event__synthesize_stat(struct perf_tool *tool,
1369                                 struct perf_cpu cpu, u32 thread, u64 id,
1370                                 struct perf_counts_values *count,
1371                                 perf_event__handler_t process,
1372                                 struct machine *machine)
1373 {
1374         struct perf_record_stat event;
1375
1376         event.header.type = PERF_RECORD_STAT;
1377         event.header.size = sizeof(event);
1378         event.header.misc = 0;
1379
1380         event.id        = id;
1381         event.cpu       = cpu.cpu;
1382         event.thread    = thread;
1383         event.val       = count->val;
1384         event.ena       = count->ena;
1385         event.run       = count->run;
1386
1387         return process(tool, (union perf_event *) &event, NULL, machine);
1388 }
1389
1390 int perf_event__synthesize_stat_round(struct perf_tool *tool,
1391                                       u64 evtime, u64 type,
1392                                       perf_event__handler_t process,
1393                                       struct machine *machine)
1394 {
1395         struct perf_record_stat_round event;
1396
1397         event.header.type = PERF_RECORD_STAT_ROUND;
1398         event.header.size = sizeof(event);
1399         event.header.misc = 0;
1400
1401         event.time = evtime;
1402         event.type = type;
1403
1404         return process(tool, (union perf_event *) &event, NULL, machine);
1405 }
1406
1407 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type, u64 read_format)
1408 {
1409         size_t sz, result = sizeof(struct perf_record_sample);
1410
1411         if (type & PERF_SAMPLE_IDENTIFIER)
1412                 result += sizeof(u64);
1413
1414         if (type & PERF_SAMPLE_IP)
1415                 result += sizeof(u64);
1416
1417         if (type & PERF_SAMPLE_TID)
1418                 result += sizeof(u64);
1419
1420         if (type & PERF_SAMPLE_TIME)
1421                 result += sizeof(u64);
1422
1423         if (type & PERF_SAMPLE_ADDR)
1424                 result += sizeof(u64);
1425
1426         if (type & PERF_SAMPLE_ID)
1427                 result += sizeof(u64);
1428
1429         if (type & PERF_SAMPLE_STREAM_ID)
1430                 result += sizeof(u64);
1431
1432         if (type & PERF_SAMPLE_CPU)
1433                 result += sizeof(u64);
1434
1435         if (type & PERF_SAMPLE_PERIOD)
1436                 result += sizeof(u64);
1437
1438         if (type & PERF_SAMPLE_READ) {
1439                 result += sizeof(u64);
1440                 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1441                         result += sizeof(u64);
1442                 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1443                         result += sizeof(u64);
1444                 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1445                 if (read_format & PERF_FORMAT_GROUP) {
1446                         sz = sample->read.group.nr *
1447                              sizeof(struct sample_read_value);
1448                         result += sz;
1449                 } else {
1450                         result += sizeof(u64);
1451                 }
1452         }
1453
1454         if (type & PERF_SAMPLE_CALLCHAIN) {
1455                 sz = (sample->callchain->nr + 1) * sizeof(u64);
1456                 result += sz;
1457         }
1458
1459         if (type & PERF_SAMPLE_RAW) {
1460                 result += sizeof(u32);
1461                 result += sample->raw_size;
1462         }
1463
1464         if (type & PERF_SAMPLE_BRANCH_STACK) {
1465                 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1466                 /* nr, hw_idx */
1467                 sz += 2 * sizeof(u64);
1468                 result += sz;
1469         }
1470
1471         if (type & PERF_SAMPLE_REGS_USER) {
1472                 if (sample->user_regs.abi) {
1473                         result += sizeof(u64);
1474                         sz = hweight64(sample->user_regs.mask) * sizeof(u64);
1475                         result += sz;
1476                 } else {
1477                         result += sizeof(u64);
1478                 }
1479         }
1480
1481         if (type & PERF_SAMPLE_STACK_USER) {
1482                 sz = sample->user_stack.size;
1483                 result += sizeof(u64);
1484                 if (sz) {
1485                         result += sz;
1486                         result += sizeof(u64);
1487                 }
1488         }
1489
1490         if (type & PERF_SAMPLE_WEIGHT_TYPE)
1491                 result += sizeof(u64);
1492
1493         if (type & PERF_SAMPLE_DATA_SRC)
1494                 result += sizeof(u64);
1495
1496         if (type & PERF_SAMPLE_TRANSACTION)
1497                 result += sizeof(u64);
1498
1499         if (type & PERF_SAMPLE_REGS_INTR) {
1500                 if (sample->intr_regs.abi) {
1501                         result += sizeof(u64);
1502                         sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
1503                         result += sz;
1504                 } else {
1505                         result += sizeof(u64);
1506                 }
1507         }
1508
1509         if (type & PERF_SAMPLE_PHYS_ADDR)
1510                 result += sizeof(u64);
1511
1512         if (type & PERF_SAMPLE_CGROUP)
1513                 result += sizeof(u64);
1514
1515         if (type & PERF_SAMPLE_DATA_PAGE_SIZE)
1516                 result += sizeof(u64);
1517
1518         if (type & PERF_SAMPLE_CODE_PAGE_SIZE)
1519                 result += sizeof(u64);
1520
1521         if (type & PERF_SAMPLE_AUX) {
1522                 result += sizeof(u64);
1523                 result += sample->aux_sample.size;
1524         }
1525
1526         return result;
1527 }
1528
1529 void __weak arch_perf_synthesize_sample_weight(const struct perf_sample *data,
1530                                                __u64 *array, u64 type __maybe_unused)
1531 {
1532         *array = data->weight;
1533 }
1534
1535 int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_format,
1536                                   const struct perf_sample *sample)
1537 {
1538         __u64 *array;
1539         size_t sz;
1540         /*
1541          * used for cross-endian analysis. See git commit 65014ab3
1542          * for why this goofiness is needed.
1543          */
1544         union u64_swap u;
1545
1546         array = event->sample.array;
1547
1548         if (type & PERF_SAMPLE_IDENTIFIER) {
1549                 *array = sample->id;
1550                 array++;
1551         }
1552
1553         if (type & PERF_SAMPLE_IP) {
1554                 *array = sample->ip;
1555                 array++;
1556         }
1557
1558         if (type & PERF_SAMPLE_TID) {
1559                 u.val32[0] = sample->pid;
1560                 u.val32[1] = sample->tid;
1561                 *array = u.val64;
1562                 array++;
1563         }
1564
1565         if (type & PERF_SAMPLE_TIME) {
1566                 *array = sample->time;
1567                 array++;
1568         }
1569
1570         if (type & PERF_SAMPLE_ADDR) {
1571                 *array = sample->addr;
1572                 array++;
1573         }
1574
1575         if (type & PERF_SAMPLE_ID) {
1576                 *array = sample->id;
1577                 array++;
1578         }
1579
1580         if (type & PERF_SAMPLE_STREAM_ID) {
1581                 *array = sample->stream_id;
1582                 array++;
1583         }
1584
1585         if (type & PERF_SAMPLE_CPU) {
1586                 u.val32[0] = sample->cpu;
1587                 u.val32[1] = 0;
1588                 *array = u.val64;
1589                 array++;
1590         }
1591
1592         if (type & PERF_SAMPLE_PERIOD) {
1593                 *array = sample->period;
1594                 array++;
1595         }
1596
1597         if (type & PERF_SAMPLE_READ) {
1598                 if (read_format & PERF_FORMAT_GROUP)
1599                         *array = sample->read.group.nr;
1600                 else
1601                         *array = sample->read.one.value;
1602                 array++;
1603
1604                 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1605                         *array = sample->read.time_enabled;
1606                         array++;
1607                 }
1608
1609                 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1610                         *array = sample->read.time_running;
1611                         array++;
1612                 }
1613
1614                 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1615                 if (read_format & PERF_FORMAT_GROUP) {
1616                         sz = sample->read.group.nr *
1617                              sizeof(struct sample_read_value);
1618                         memcpy(array, sample->read.group.values, sz);
1619                         array = (void *)array + sz;
1620                 } else {
1621                         *array = sample->read.one.id;
1622                         array++;
1623                 }
1624         }
1625
1626         if (type & PERF_SAMPLE_CALLCHAIN) {
1627                 sz = (sample->callchain->nr + 1) * sizeof(u64);
1628                 memcpy(array, sample->callchain, sz);
1629                 array = (void *)array + sz;
1630         }
1631
1632         if (type & PERF_SAMPLE_RAW) {
1633                 u.val32[0] = sample->raw_size;
1634                 *array = u.val64;
1635                 array = (void *)array + sizeof(u32);
1636
1637                 memcpy(array, sample->raw_data, sample->raw_size);
1638                 array = (void *)array + sample->raw_size;
1639         }
1640
1641         if (type & PERF_SAMPLE_BRANCH_STACK) {
1642                 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1643                 /* nr, hw_idx */
1644                 sz += 2 * sizeof(u64);
1645                 memcpy(array, sample->branch_stack, sz);
1646                 array = (void *)array + sz;
1647         }
1648
1649         if (type & PERF_SAMPLE_REGS_USER) {
1650                 if (sample->user_regs.abi) {
1651                         *array++ = sample->user_regs.abi;
1652                         sz = hweight64(sample->user_regs.mask) * sizeof(u64);
1653                         memcpy(array, sample->user_regs.regs, sz);
1654                         array = (void *)array + sz;
1655                 } else {
1656                         *array++ = 0;
1657                 }
1658         }
1659
1660         if (type & PERF_SAMPLE_STACK_USER) {
1661                 sz = sample->user_stack.size;
1662                 *array++ = sz;
1663                 if (sz) {
1664                         memcpy(array, sample->user_stack.data, sz);
1665                         array = (void *)array + sz;
1666                         *array++ = sz;
1667                 }
1668         }
1669
1670         if (type & PERF_SAMPLE_WEIGHT_TYPE) {
1671                 arch_perf_synthesize_sample_weight(sample, array, type);
1672                 array++;
1673         }
1674
1675         if (type & PERF_SAMPLE_DATA_SRC) {
1676                 *array = sample->data_src;
1677                 array++;
1678         }
1679
1680         if (type & PERF_SAMPLE_TRANSACTION) {
1681                 *array = sample->transaction;
1682                 array++;
1683         }
1684
1685         if (type & PERF_SAMPLE_REGS_INTR) {
1686                 if (sample->intr_regs.abi) {
1687                         *array++ = sample->intr_regs.abi;
1688                         sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
1689                         memcpy(array, sample->intr_regs.regs, sz);
1690                         array = (void *)array + sz;
1691                 } else {
1692                         *array++ = 0;
1693                 }
1694         }
1695
1696         if (type & PERF_SAMPLE_PHYS_ADDR) {
1697                 *array = sample->phys_addr;
1698                 array++;
1699         }
1700
1701         if (type & PERF_SAMPLE_CGROUP) {
1702                 *array = sample->cgroup;
1703                 array++;
1704         }
1705
1706         if (type & PERF_SAMPLE_DATA_PAGE_SIZE) {
1707                 *array = sample->data_page_size;
1708                 array++;
1709         }
1710
1711         if (type & PERF_SAMPLE_CODE_PAGE_SIZE) {
1712                 *array = sample->code_page_size;
1713                 array++;
1714         }
1715
1716         if (type & PERF_SAMPLE_AUX) {
1717                 sz = sample->aux_sample.size;
1718                 *array++ = sz;
1719                 memcpy(array, sample->aux_sample.data, sz);
1720                 array = (void *)array + sz;
1721         }
1722
1723         return 0;
1724 }
1725
1726 int perf_event__synthesize_id_index(struct perf_tool *tool, perf_event__handler_t process,
1727                                     struct evlist *evlist, struct machine *machine)
1728 {
1729         union perf_event *ev;
1730         struct evsel *evsel;
1731         size_t nr = 0, i = 0, sz, max_nr, n;
1732         int err;
1733
1734         pr_debug2("Synthesizing id index\n");
1735
1736         max_nr = (UINT16_MAX - sizeof(struct perf_record_id_index)) /
1737                  sizeof(struct id_index_entry);
1738
1739         evlist__for_each_entry(evlist, evsel)
1740                 nr += evsel->core.ids;
1741
1742         n = nr > max_nr ? max_nr : nr;
1743         sz = sizeof(struct perf_record_id_index) + n * sizeof(struct id_index_entry);
1744         ev = zalloc(sz);
1745         if (!ev)
1746                 return -ENOMEM;
1747
1748         ev->id_index.header.type = PERF_RECORD_ID_INDEX;
1749         ev->id_index.header.size = sz;
1750         ev->id_index.nr = n;
1751
1752         evlist__for_each_entry(evlist, evsel) {
1753                 u32 j;
1754
1755                 for (j = 0; j < evsel->core.ids; j++) {
1756                         struct id_index_entry *e;
1757                         struct perf_sample_id *sid;
1758
1759                         if (i >= n) {
1760                                 err = process(tool, ev, NULL, machine);
1761                                 if (err)
1762                                         goto out_err;
1763                                 nr -= n;
1764                                 i = 0;
1765                         }
1766
1767                         e = &ev->id_index.entries[i++];
1768
1769                         e->id = evsel->core.id[j];
1770
1771                         sid = evlist__id2sid(evlist, e->id);
1772                         if (!sid) {
1773                                 free(ev);
1774                                 return -ENOENT;
1775                         }
1776
1777                         e->idx = sid->idx;
1778                         e->cpu = sid->cpu.cpu;
1779                         e->tid = sid->tid;
1780                 }
1781         }
1782
1783         sz = sizeof(struct perf_record_id_index) + nr * sizeof(struct id_index_entry);
1784         ev->id_index.header.size = sz;
1785         ev->id_index.nr = nr;
1786
1787         err = process(tool, ev, NULL, machine);
1788 out_err:
1789         free(ev);
1790
1791         return err;
1792 }
1793
1794 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
1795                                   struct target *target, struct perf_thread_map *threads,
1796                                   perf_event__handler_t process, bool needs_mmap,
1797                                   bool data_mmap, unsigned int nr_threads_synthesize)
1798 {
1799         /*
1800          * When perf runs in non-root PID namespace, and the namespace's proc FS
1801          * is not mounted, nsinfo__is_in_root_namespace() returns false.
1802          * In this case, the proc FS is coming for the parent namespace, thus
1803          * perf tool will wrongly gather process info from its parent PID
1804          * namespace.
1805          *
1806          * To avoid the confusion that the perf tool runs in a child PID
1807          * namespace but it synthesizes thread info from its parent PID
1808          * namespace, returns failure with warning.
1809          */
1810         if (!nsinfo__is_in_root_namespace()) {
1811                 pr_err("Perf runs in non-root PID namespace but it tries to ");
1812                 pr_err("gather process info from its parent PID namespace.\n");
1813                 pr_err("Please mount the proc file system properly, e.g. ");
1814                 pr_err("add the option '--mount-proc' for unshare command.\n");
1815                 return -EPERM;
1816         }
1817
1818         if (target__has_task(target))
1819                 return perf_event__synthesize_thread_map(tool, threads, process, machine,
1820                                                          needs_mmap, data_mmap);
1821         else if (target__has_cpu(target))
1822                 return perf_event__synthesize_threads(tool, process, machine,
1823                                                       needs_mmap, data_mmap,
1824                                                       nr_threads_synthesize);
1825         /* command specified */
1826         return 0;
1827 }
1828
1829 int machine__synthesize_threads(struct machine *machine, struct target *target,
1830                                 struct perf_thread_map *threads, bool needs_mmap,
1831                                 bool data_mmap, unsigned int nr_threads_synthesize)
1832 {
1833         return __machine__synthesize_threads(machine, NULL, target, threads,
1834                                              perf_event__process, needs_mmap,
1835                                              data_mmap, nr_threads_synthesize);
1836 }
1837
1838 static struct perf_record_event_update *event_update_event__new(size_t size, u64 type, u64 id)
1839 {
1840         struct perf_record_event_update *ev;
1841
1842         size += sizeof(*ev);
1843         size  = PERF_ALIGN(size, sizeof(u64));
1844
1845         ev = zalloc(size);
1846         if (ev) {
1847                 ev->header.type = PERF_RECORD_EVENT_UPDATE;
1848                 ev->header.size = (u16)size;
1849                 ev->type        = type;
1850                 ev->id          = id;
1851         }
1852         return ev;
1853 }
1854
1855 int perf_event__synthesize_event_update_unit(struct perf_tool *tool, struct evsel *evsel,
1856                                              perf_event__handler_t process)
1857 {
1858         size_t size = strlen(evsel->unit);
1859         struct perf_record_event_update *ev;
1860         int err;
1861
1862         ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->core.id[0]);
1863         if (ev == NULL)
1864                 return -ENOMEM;
1865
1866         strlcpy(ev->data, evsel->unit, size + 1);
1867         err = process(tool, (union perf_event *)ev, NULL, NULL);
1868         free(ev);
1869         return err;
1870 }
1871
1872 int perf_event__synthesize_event_update_scale(struct perf_tool *tool, struct evsel *evsel,
1873                                               perf_event__handler_t process)
1874 {
1875         struct perf_record_event_update *ev;
1876         struct perf_record_event_update_scale *ev_data;
1877         int err;
1878
1879         ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->core.id[0]);
1880         if (ev == NULL)
1881                 return -ENOMEM;
1882
1883         ev_data = (struct perf_record_event_update_scale *)ev->data;
1884         ev_data->scale = evsel->scale;
1885         err = process(tool, (union perf_event *)ev, NULL, NULL);
1886         free(ev);
1887         return err;
1888 }
1889
1890 int perf_event__synthesize_event_update_name(struct perf_tool *tool, struct evsel *evsel,
1891                                              perf_event__handler_t process)
1892 {
1893         struct perf_record_event_update *ev;
1894         size_t len = strlen(evsel->name);
1895         int err;
1896
1897         ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->core.id[0]);
1898         if (ev == NULL)
1899                 return -ENOMEM;
1900
1901         strlcpy(ev->data, evsel->name, len + 1);
1902         err = process(tool, (union perf_event *)ev, NULL, NULL);
1903         free(ev);
1904         return err;
1905 }
1906
1907 int perf_event__synthesize_event_update_cpus(struct perf_tool *tool, struct evsel *evsel,
1908                                              perf_event__handler_t process)
1909 {
1910         size_t size = sizeof(struct perf_record_event_update);
1911         struct perf_record_event_update *ev;
1912         int max, err;
1913         u16 type;
1914
1915         if (!evsel->core.own_cpus)
1916                 return 0;
1917
1918         ev = cpu_map_data__alloc(evsel->core.own_cpus, &size, &type, &max);
1919         if (!ev)
1920                 return -ENOMEM;
1921
1922         ev->header.type = PERF_RECORD_EVENT_UPDATE;
1923         ev->header.size = (u16)size;
1924         ev->type        = PERF_EVENT_UPDATE__CPUS;
1925         ev->id          = evsel->core.id[0];
1926
1927         cpu_map_data__synthesize((struct perf_record_cpu_map_data *)ev->data,
1928                                  evsel->core.own_cpus, type, max);
1929
1930         err = process(tool, (union perf_event *)ev, NULL, NULL);
1931         free(ev);
1932         return err;
1933 }
1934
1935 int perf_event__synthesize_attrs(struct perf_tool *tool, struct evlist *evlist,
1936                                  perf_event__handler_t process)
1937 {
1938         struct evsel *evsel;
1939         int err = 0;
1940
1941         evlist__for_each_entry(evlist, evsel) {
1942                 err = perf_event__synthesize_attr(tool, &evsel->core.attr, evsel->core.ids,
1943                                                   evsel->core.id, process);
1944                 if (err) {
1945                         pr_debug("failed to create perf header attribute\n");
1946                         return err;
1947                 }
1948         }
1949
1950         return err;
1951 }
1952
1953 static bool has_unit(struct evsel *evsel)
1954 {
1955         return evsel->unit && *evsel->unit;
1956 }
1957
1958 static bool has_scale(struct evsel *evsel)
1959 {
1960         return evsel->scale != 1;
1961 }
1962
1963 int perf_event__synthesize_extra_attr(struct perf_tool *tool, struct evlist *evsel_list,
1964                                       perf_event__handler_t process, bool is_pipe)
1965 {
1966         struct evsel *evsel;
1967         int err;
1968
1969         /*
1970          * Synthesize other events stuff not carried within
1971          * attr event - unit, scale, name
1972          */
1973         evlist__for_each_entry(evsel_list, evsel) {
1974                 if (!evsel->supported)
1975                         continue;
1976
1977                 /*
1978                  * Synthesize unit and scale only if it's defined.
1979                  */
1980                 if (has_unit(evsel)) {
1981                         err = perf_event__synthesize_event_update_unit(tool, evsel, process);
1982                         if (err < 0) {
1983                                 pr_err("Couldn't synthesize evsel unit.\n");
1984                                 return err;
1985                         }
1986                 }
1987
1988                 if (has_scale(evsel)) {
1989                         err = perf_event__synthesize_event_update_scale(tool, evsel, process);
1990                         if (err < 0) {
1991                                 pr_err("Couldn't synthesize evsel evsel.\n");
1992                                 return err;
1993                         }
1994                 }
1995
1996                 if (evsel->core.own_cpus) {
1997                         err = perf_event__synthesize_event_update_cpus(tool, evsel, process);
1998                         if (err < 0) {
1999                                 pr_err("Couldn't synthesize evsel cpus.\n");
2000                                 return err;
2001                         }
2002                 }
2003
2004                 /*
2005                  * Name is needed only for pipe output,
2006                  * perf.data carries event names.
2007                  */
2008                 if (is_pipe) {
2009                         err = perf_event__synthesize_event_update_name(tool, evsel, process);
2010                         if (err < 0) {
2011                                 pr_err("Couldn't synthesize evsel name.\n");
2012                                 return err;
2013                         }
2014                 }
2015         }
2016         return 0;
2017 }
2018
2019 int perf_event__synthesize_attr(struct perf_tool *tool, struct perf_event_attr *attr,
2020                                 u32 ids, u64 *id, perf_event__handler_t process)
2021 {
2022         union perf_event *ev;
2023         size_t size;
2024         int err;
2025
2026         size = sizeof(struct perf_event_attr);
2027         size = PERF_ALIGN(size, sizeof(u64));
2028         size += sizeof(struct perf_event_header);
2029         size += ids * sizeof(u64);
2030
2031         ev = zalloc(size);
2032
2033         if (ev == NULL)
2034                 return -ENOMEM;
2035
2036         ev->attr.attr = *attr;
2037         memcpy(ev->attr.id, id, ids * sizeof(u64));
2038
2039         ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2040         ev->attr.header.size = (u16)size;
2041
2042         if (ev->attr.header.size == size)
2043                 err = process(tool, ev, NULL, NULL);
2044         else
2045                 err = -E2BIG;
2046
2047         free(ev);
2048
2049         return err;
2050 }
2051
2052 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, struct evlist *evlist,
2053                                         perf_event__handler_t process)
2054 {
2055         union perf_event ev;
2056         struct tracing_data *tdata;
2057         ssize_t size = 0, aligned_size = 0, padding;
2058         struct feat_fd ff;
2059
2060         /*
2061          * We are going to store the size of the data followed
2062          * by the data contents. Since the fd descriptor is a pipe,
2063          * we cannot seek back to store the size of the data once
2064          * we know it. Instead we:
2065          *
2066          * - write the tracing data to the temp file
2067          * - get/write the data size to pipe
2068          * - write the tracing data from the temp file
2069          *   to the pipe
2070          */
2071         tdata = tracing_data_get(&evlist->core.entries, fd, true);
2072         if (!tdata)
2073                 return -1;
2074
2075         memset(&ev, 0, sizeof(ev));
2076
2077         ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2078         size = tdata->size;
2079         aligned_size = PERF_ALIGN(size, sizeof(u64));
2080         padding = aligned_size - size;
2081         ev.tracing_data.header.size = sizeof(ev.tracing_data);
2082         ev.tracing_data.size = aligned_size;
2083
2084         process(tool, &ev, NULL, NULL);
2085
2086         /*
2087          * The put function will copy all the tracing data
2088          * stored in temp file to the pipe.
2089          */
2090         tracing_data_put(tdata);
2091
2092         ff = (struct feat_fd){ .fd = fd };
2093         if (write_padded(&ff, NULL, 0, padding))
2094                 return -1;
2095
2096         return aligned_size;
2097 }
2098
2099 int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16 misc,
2100                                     perf_event__handler_t process, struct machine *machine)
2101 {
2102         union perf_event ev;
2103         size_t len;
2104
2105         if (!pos->hit)
2106                 return 0;
2107
2108         memset(&ev, 0, sizeof(ev));
2109
2110         len = pos->long_name_len + 1;
2111         len = PERF_ALIGN(len, NAME_ALIGN);
2112         memcpy(&ev.build_id.build_id, pos->bid.data, sizeof(pos->bid.data));
2113         ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2114         ev.build_id.header.misc = misc;
2115         ev.build_id.pid = machine->pid;
2116         ev.build_id.header.size = sizeof(ev.build_id) + len;
2117         memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2118
2119         return process(tool, &ev, NULL, machine);
2120 }
2121
2122 int perf_event__synthesize_stat_events(struct perf_stat_config *config, struct perf_tool *tool,
2123                                        struct evlist *evlist, perf_event__handler_t process, bool attrs)
2124 {
2125         int err;
2126
2127         if (attrs) {
2128                 err = perf_event__synthesize_attrs(tool, evlist, process);
2129                 if (err < 0) {
2130                         pr_err("Couldn't synthesize attrs.\n");
2131                         return err;
2132                 }
2133         }
2134
2135         err = perf_event__synthesize_extra_attr(tool, evlist, process, attrs);
2136         err = perf_event__synthesize_thread_map2(tool, evlist->core.threads, process, NULL);
2137         if (err < 0) {
2138                 pr_err("Couldn't synthesize thread map.\n");
2139                 return err;
2140         }
2141
2142         err = perf_event__synthesize_cpu_map(tool, evlist->core.user_requested_cpus, process, NULL);
2143         if (err < 0) {
2144                 pr_err("Couldn't synthesize thread map.\n");
2145                 return err;
2146         }
2147
2148         err = perf_event__synthesize_stat_config(tool, config, process, NULL);
2149         if (err < 0) {
2150                 pr_err("Couldn't synthesize config.\n");
2151                 return err;
2152         }
2153
2154         return 0;
2155 }
2156
2157 extern const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
2158
2159 int perf_event__synthesize_features(struct perf_tool *tool, struct perf_session *session,
2160                                     struct evlist *evlist, perf_event__handler_t process)
2161 {
2162         struct perf_header *header = &session->header;
2163         struct perf_record_header_feature *fe;
2164         struct feat_fd ff;
2165         size_t sz, sz_hdr;
2166         int feat, ret;
2167
2168         sz_hdr = sizeof(fe->header);
2169         sz = sizeof(union perf_event);
2170         /* get a nice alignment */
2171         sz = PERF_ALIGN(sz, page_size);
2172
2173         memset(&ff, 0, sizeof(ff));
2174
2175         ff.buf = malloc(sz);
2176         if (!ff.buf)
2177                 return -ENOMEM;
2178
2179         ff.size = sz - sz_hdr;
2180         ff.ph = &session->header;
2181
2182         for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2183                 if (!feat_ops[feat].synthesize) {
2184                         pr_debug("No record header feature for header :%d\n", feat);
2185                         continue;
2186                 }
2187
2188                 ff.offset = sizeof(*fe);
2189
2190                 ret = feat_ops[feat].write(&ff, evlist);
2191                 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
2192                         pr_debug("Error writing feature\n");
2193                         continue;
2194                 }
2195                 /* ff.buf may have changed due to realloc in do_write() */
2196                 fe = ff.buf;
2197                 memset(fe, 0, sizeof(*fe));
2198
2199                 fe->feat_id = feat;
2200                 fe->header.type = PERF_RECORD_HEADER_FEATURE;
2201                 fe->header.size = ff.offset;
2202
2203                 ret = process(tool, ff.buf, NULL, NULL);
2204                 if (ret) {
2205                         free(ff.buf);
2206                         return ret;
2207                 }
2208         }
2209
2210         /* Send HEADER_LAST_FEATURE mark. */
2211         fe = ff.buf;
2212         fe->feat_id     = HEADER_LAST_FEATURE;
2213         fe->header.type = PERF_RECORD_HEADER_FEATURE;
2214         fe->header.size = sizeof(*fe);
2215
2216         ret = process(tool, ff.buf, NULL, NULL);
2217
2218         free(ff.buf);
2219         return ret;
2220 }
2221
2222 int perf_event__synthesize_for_pipe(struct perf_tool *tool,
2223                                     struct perf_session *session,
2224                                     struct perf_data *data,
2225                                     perf_event__handler_t process)
2226 {
2227         int err;
2228         int ret = 0;
2229         struct evlist *evlist = session->evlist;
2230
2231         /*
2232          * We need to synthesize events first, because some
2233          * features works on top of them (on report side).
2234          */
2235         err = perf_event__synthesize_attrs(tool, evlist, process);
2236         if (err < 0) {
2237                 pr_err("Couldn't synthesize attrs.\n");
2238                 return err;
2239         }
2240         ret += err;
2241
2242         err = perf_event__synthesize_features(tool, session, evlist, process);
2243         if (err < 0) {
2244                 pr_err("Couldn't synthesize features.\n");
2245                 return err;
2246         }
2247         ret += err;
2248
2249         if (have_tracepoints(&evlist->core.entries)) {
2250                 int fd = perf_data__fd(data);
2251
2252                 /*
2253                  * FIXME err <= 0 here actually means that
2254                  * there were no tracepoints so its not really
2255                  * an error, just that we don't need to
2256                  * synthesize anything.  We really have to
2257                  * return this more properly and also
2258                  * propagate errors that now are calling die()
2259                  */
2260                 err = perf_event__synthesize_tracing_data(tool, fd, evlist,
2261                                                           process);
2262                 if (err <= 0) {
2263                         pr_err("Couldn't record tracing data.\n");
2264                         return err;
2265                 }
2266                 ret += err;
2267         }
2268
2269         return ret;
2270 }
2271
2272 int parse_synth_opt(char *synth)
2273 {
2274         char *p, *q;
2275         int ret = 0;
2276
2277         if (synth == NULL)
2278                 return -1;
2279
2280         for (q = synth; (p = strsep(&q, ",")); p = q) {
2281                 if (!strcasecmp(p, "no") || !strcasecmp(p, "none"))
2282                         return 0;
2283
2284                 if (!strcasecmp(p, "all"))
2285                         return PERF_SYNTH_ALL;
2286
2287                 if (!strcasecmp(p, "task"))
2288                         ret |= PERF_SYNTH_TASK;
2289                 else if (!strcasecmp(p, "mmap"))
2290                         ret |= PERF_SYNTH_TASK | PERF_SYNTH_MMAP;
2291                 else if (!strcasecmp(p, "cgroup"))
2292                         ret |= PERF_SYNTH_CGROUP;
2293                 else
2294                         return -1;
2295         }
2296
2297         return ret;
2298 }