0001
0002
0003 #include "util/cgroup.h"
0004 #include "util/data.h"
0005 #include "util/debug.h"
0006 #include "util/dso.h"
0007 #include "util/event.h"
0008 #include "util/evlist.h"
0009 #include "util/machine.h"
0010 #include "util/map.h"
0011 #include "util/map_symbol.h"
0012 #include "util/branch.h"
0013 #include "util/memswap.h"
0014 #include "util/namespaces.h"
0015 #include "util/session.h"
0016 #include "util/stat.h"
0017 #include "util/symbol.h"
0018 #include "util/synthetic-events.h"
0019 #include "util/target.h"
0020 #include "util/time-utils.h"
0021 #include <linux/bitops.h>
0022 #include <linux/kernel.h>
0023 #include <linux/string.h>
0024 #include <linux/zalloc.h>
0025 #include <linux/perf_event.h>
0026 #include <asm/bug.h>
0027 #include <perf/evsel.h>
0028 #include <perf/cpumap.h>
0029 #include <internal/lib.h> // page_size
0030 #include <internal/threadmap.h>
0031 #include <perf/threadmap.h>
0032 #include <symbol/kallsyms.h>
0033 #include <dirent.h>
0034 #include <errno.h>
0035 #include <inttypes.h>
0036 #include <stdio.h>
0037 #include <string.h>
0038 #include <uapi/linux/mman.h> /* To get things like MAP_HUGETLB even on older libc headers */
0039 #include <api/fs/fs.h>
0040 #include <api/io.h>
0041 #include <sys/types.h>
0042 #include <sys/stat.h>
0043 #include <fcntl.h>
0044 #include <unistd.h>
0045
0046 #define DEFAULT_PROC_MAP_PARSE_TIMEOUT 500
0047
0048 unsigned int proc_map_timeout = DEFAULT_PROC_MAP_PARSE_TIMEOUT;
0049
0050 int perf_tool__process_synth_event(struct perf_tool *tool,
0051 union perf_event *event,
0052 struct machine *machine,
0053 perf_event__handler_t process)
0054 {
0055 struct perf_sample synth_sample = {
0056 .pid = -1,
0057 .tid = -1,
0058 .time = -1,
0059 .stream_id = -1,
0060 .cpu = -1,
0061 .period = 1,
0062 .cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
0063 };
0064
0065 return process(tool, event, &synth_sample, machine);
0066 };
0067
0068
0069
0070
0071
0072 static int perf_event__get_comm_ids(pid_t pid, pid_t tid, char *comm, size_t len,
0073 pid_t *tgid, pid_t *ppid, bool *kernel)
0074 {
0075 char bf[4096];
0076 int fd;
0077 size_t size = 0;
0078 ssize_t n;
0079 char *name, *tgids, *ppids, *vmpeak, *threads;
0080
0081 *tgid = -1;
0082 *ppid = -1;
0083
0084 if (pid)
0085 snprintf(bf, sizeof(bf), "/proc/%d/task/%d/status", pid, tid);
0086 else
0087 snprintf(bf, sizeof(bf), "/proc/%d/status", tid);
0088
0089 fd = open(bf, O_RDONLY);
0090 if (fd < 0) {
0091 pr_debug("couldn't open %s\n", bf);
0092 return -1;
0093 }
0094
0095 n = read(fd, bf, sizeof(bf) - 1);
0096 close(fd);
0097 if (n <= 0) {
0098 pr_warning("Couldn't get COMM, tigd and ppid for pid %d\n",
0099 tid);
0100 return -1;
0101 }
0102 bf[n] = '\0';
0103
0104 name = strstr(bf, "Name:");
0105 tgids = strstr(name ?: bf, "Tgid:");
0106 ppids = strstr(tgids ?: bf, "PPid:");
0107 vmpeak = strstr(ppids ?: bf, "VmPeak:");
0108
0109 if (vmpeak)
0110 threads = NULL;
0111 else
0112 threads = strstr(ppids ?: bf, "Threads:");
0113
0114 if (name) {
0115 char *nl;
0116
0117 name = skip_spaces(name + 5);
0118 nl = strchr(name, '\n');
0119 if (nl)
0120 *nl = '\0';
0121
0122 size = strlen(name);
0123 if (size >= len)
0124 size = len - 1;
0125 memcpy(comm, name, size);
0126 comm[size] = '\0';
0127 } else {
0128 pr_debug("Name: string not found for pid %d\n", tid);
0129 }
0130
0131 if (tgids) {
0132 tgids += 5;
0133 *tgid = atoi(tgids);
0134 } else {
0135 pr_debug("Tgid: string not found for pid %d\n", tid);
0136 }
0137
0138 if (ppids) {
0139 ppids += 5;
0140 *ppid = atoi(ppids);
0141 } else {
0142 pr_debug("PPid: string not found for pid %d\n", tid);
0143 }
0144
0145 if (!vmpeak && threads)
0146 *kernel = true;
0147 else
0148 *kernel = false;
0149
0150 return 0;
0151 }
0152
0153 static int perf_event__prepare_comm(union perf_event *event, pid_t pid, pid_t tid,
0154 struct machine *machine,
0155 pid_t *tgid, pid_t *ppid, bool *kernel)
0156 {
0157 size_t size;
0158
0159 *ppid = -1;
0160
0161 memset(&event->comm, 0, sizeof(event->comm));
0162
0163 if (machine__is_host(machine)) {
0164 if (perf_event__get_comm_ids(pid, tid, event->comm.comm,
0165 sizeof(event->comm.comm),
0166 tgid, ppid, kernel) != 0) {
0167 return -1;
0168 }
0169 } else {
0170 *tgid = machine->pid;
0171 }
0172
0173 if (*tgid < 0)
0174 return -1;
0175
0176 event->comm.pid = *tgid;
0177 event->comm.header.type = PERF_RECORD_COMM;
0178
0179 size = strlen(event->comm.comm) + 1;
0180 size = PERF_ALIGN(size, sizeof(u64));
0181 memset(event->comm.comm + size, 0, machine->id_hdr_size);
0182 event->comm.header.size = (sizeof(event->comm) -
0183 (sizeof(event->comm.comm) - size) +
0184 machine->id_hdr_size);
0185 event->comm.tid = tid;
0186
0187 return 0;
0188 }
0189
0190 pid_t perf_event__synthesize_comm(struct perf_tool *tool,
0191 union perf_event *event, pid_t pid,
0192 perf_event__handler_t process,
0193 struct machine *machine)
0194 {
0195 pid_t tgid, ppid;
0196 bool kernel_thread;
0197
0198 if (perf_event__prepare_comm(event, 0, pid, machine, &tgid, &ppid,
0199 &kernel_thread) != 0)
0200 return -1;
0201
0202 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
0203 return -1;
0204
0205 return tgid;
0206 }
0207
0208 static void perf_event__get_ns_link_info(pid_t pid, const char *ns,
0209 struct perf_ns_link_info *ns_link_info)
0210 {
0211 struct stat64 st;
0212 char proc_ns[128];
0213
0214 sprintf(proc_ns, "/proc/%u/ns/%s", pid, ns);
0215 if (stat64(proc_ns, &st) == 0) {
0216 ns_link_info->dev = st.st_dev;
0217 ns_link_info->ino = st.st_ino;
0218 }
0219 }
0220
0221 int perf_event__synthesize_namespaces(struct perf_tool *tool,
0222 union perf_event *event,
0223 pid_t pid, pid_t tgid,
0224 perf_event__handler_t process,
0225 struct machine *machine)
0226 {
0227 u32 idx;
0228 struct perf_ns_link_info *ns_link_info;
0229
0230 if (!tool || !tool->namespace_events)
0231 return 0;
0232
0233 memset(&event->namespaces, 0, (sizeof(event->namespaces) +
0234 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
0235 machine->id_hdr_size));
0236
0237 event->namespaces.pid = tgid;
0238 event->namespaces.tid = pid;
0239
0240 event->namespaces.nr_namespaces = NR_NAMESPACES;
0241
0242 ns_link_info = event->namespaces.link_info;
0243
0244 for (idx = 0; idx < event->namespaces.nr_namespaces; idx++)
0245 perf_event__get_ns_link_info(pid, perf_ns__name(idx),
0246 &ns_link_info[idx]);
0247
0248 event->namespaces.header.type = PERF_RECORD_NAMESPACES;
0249
0250 event->namespaces.header.size = (sizeof(event->namespaces) +
0251 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
0252 machine->id_hdr_size);
0253
0254 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
0255 return -1;
0256
0257 return 0;
0258 }
0259
0260 static int perf_event__synthesize_fork(struct perf_tool *tool,
0261 union perf_event *event,
0262 pid_t pid, pid_t tgid, pid_t ppid,
0263 perf_event__handler_t process,
0264 struct machine *machine)
0265 {
0266 memset(&event->fork, 0, sizeof(event->fork) + machine->id_hdr_size);
0267
0268
0269
0270
0271
0272
0273 if (tgid == pid) {
0274 event->fork.ppid = ppid;
0275 event->fork.ptid = ppid;
0276 } else {
0277 event->fork.ppid = tgid;
0278 event->fork.ptid = tgid;
0279 }
0280 event->fork.pid = tgid;
0281 event->fork.tid = pid;
0282 event->fork.header.type = PERF_RECORD_FORK;
0283 event->fork.header.misc = PERF_RECORD_MISC_FORK_EXEC;
0284
0285 event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
0286
0287 if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
0288 return -1;
0289
0290 return 0;
0291 }
0292
0293 static bool read_proc_maps_line(struct io *io, __u64 *start, __u64 *end,
0294 u32 *prot, u32 *flags, __u64 *offset,
0295 u32 *maj, u32 *min,
0296 __u64 *inode,
0297 ssize_t pathname_size, char *pathname)
0298 {
0299 __u64 temp;
0300 int ch;
0301 char *start_pathname = pathname;
0302
0303 if (io__get_hex(io, start) != '-')
0304 return false;
0305 if (io__get_hex(io, end) != ' ')
0306 return false;
0307
0308
0309 *prot = 0;
0310 ch = io__get_char(io);
0311 if (ch == 'r')
0312 *prot |= PROT_READ;
0313 else if (ch != '-')
0314 return false;
0315 ch = io__get_char(io);
0316 if (ch == 'w')
0317 *prot |= PROT_WRITE;
0318 else if (ch != '-')
0319 return false;
0320 ch = io__get_char(io);
0321 if (ch == 'x')
0322 *prot |= PROT_EXEC;
0323 else if (ch != '-')
0324 return false;
0325 ch = io__get_char(io);
0326 if (ch == 's')
0327 *flags = MAP_SHARED;
0328 else if (ch == 'p')
0329 *flags = MAP_PRIVATE;
0330 else
0331 return false;
0332 if (io__get_char(io) != ' ')
0333 return false;
0334
0335 if (io__get_hex(io, offset) != ' ')
0336 return false;
0337
0338 if (io__get_hex(io, &temp) != ':')
0339 return false;
0340 *maj = temp;
0341 if (io__get_hex(io, &temp) != ' ')
0342 return false;
0343 *min = temp;
0344
0345 ch = io__get_dec(io, inode);
0346 if (ch != ' ') {
0347 *pathname = '\0';
0348 return ch == '\n';
0349 }
0350 do {
0351 ch = io__get_char(io);
0352 } while (ch == ' ');
0353 while (true) {
0354 if (ch < 0)
0355 return false;
0356 if (ch == '\0' || ch == '\n' ||
0357 (pathname + 1 - start_pathname) >= pathname_size) {
0358 *pathname = '\0';
0359 return true;
0360 }
0361 *pathname++ = ch;
0362 ch = io__get_char(io);
0363 }
0364 }
0365
0366 static void perf_record_mmap2__read_build_id(struct perf_record_mmap2 *event,
0367 bool is_kernel)
0368 {
0369 struct build_id bid;
0370 struct nsinfo *nsi;
0371 struct nscookie nc;
0372 int rc;
0373
0374 if (is_kernel) {
0375 rc = sysfs__read_build_id("/sys/kernel/notes", &bid);
0376 goto out;
0377 }
0378
0379 nsi = nsinfo__new(event->pid);
0380 nsinfo__mountns_enter(nsi, &nc);
0381
0382 rc = filename__read_build_id(event->filename, &bid) > 0 ? 0 : -1;
0383
0384 nsinfo__mountns_exit(&nc);
0385 nsinfo__put(nsi);
0386
0387 out:
0388 if (rc == 0) {
0389 memcpy(event->build_id, bid.data, sizeof(bid.data));
0390 event->build_id_size = (u8) bid.size;
0391 event->header.misc |= PERF_RECORD_MISC_MMAP_BUILD_ID;
0392 event->__reserved_1 = 0;
0393 event->__reserved_2 = 0;
0394 } else {
0395 if (event->filename[0] == '/') {
0396 pr_debug2("Failed to read build ID for %s\n",
0397 event->filename);
0398 }
0399 }
0400 }
0401
0402 int perf_event__synthesize_mmap_events(struct perf_tool *tool,
0403 union perf_event *event,
0404 pid_t pid, pid_t tgid,
0405 perf_event__handler_t process,
0406 struct machine *machine,
0407 bool mmap_data)
0408 {
0409 unsigned long long t;
0410 char bf[BUFSIZ];
0411 struct io io;
0412 bool truncation = false;
0413 unsigned long long timeout = proc_map_timeout * 1000000ULL;
0414 int rc = 0;
0415 const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
0416 int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
0417
0418 if (machine__is_default_guest(machine))
0419 return 0;
0420
0421 snprintf(bf, sizeof(bf), "%s/proc/%d/task/%d/maps",
0422 machine->root_dir, pid, pid);
0423
0424 io.fd = open(bf, O_RDONLY, 0);
0425 if (io.fd < 0) {
0426
0427
0428
0429 pr_debug("couldn't open %s\n", bf);
0430 return -1;
0431 }
0432 io__init(&io, io.fd, bf, sizeof(bf));
0433
0434 event->header.type = PERF_RECORD_MMAP2;
0435 t = rdclock();
0436
0437 while (!io.eof) {
0438 static const char anonstr[] = "//anon";
0439 size_t size, aligned_size;
0440
0441
0442 event->mmap2.filename[0] = '\0';
0443
0444
0445 if (!read_proc_maps_line(&io,
0446 &event->mmap2.start,
0447 &event->mmap2.len,
0448 &event->mmap2.prot,
0449 &event->mmap2.flags,
0450 &event->mmap2.pgoff,
0451 &event->mmap2.maj,
0452 &event->mmap2.min,
0453 &event->mmap2.ino,
0454 sizeof(event->mmap2.filename),
0455 event->mmap2.filename))
0456 continue;
0457
0458 if ((rdclock() - t) > timeout) {
0459 pr_warning("Reading %s/proc/%d/task/%d/maps time out. "
0460 "You may want to increase "
0461 "the time limit by --proc-map-timeout\n",
0462 machine->root_dir, pid, pid);
0463 truncation = true;
0464 goto out;
0465 }
0466
0467 event->mmap2.ino_generation = 0;
0468
0469
0470
0471
0472 if (machine__is_host(machine))
0473 event->header.misc = PERF_RECORD_MISC_USER;
0474 else
0475 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
0476
0477 if ((event->mmap2.prot & PROT_EXEC) == 0) {
0478 if (!mmap_data || (event->mmap2.prot & PROT_READ) == 0)
0479 continue;
0480
0481 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
0482 }
0483
0484 out:
0485 if (truncation)
0486 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
0487
0488 if (!strcmp(event->mmap2.filename, ""))
0489 strcpy(event->mmap2.filename, anonstr);
0490
0491 if (hugetlbfs_mnt_len &&
0492 !strncmp(event->mmap2.filename, hugetlbfs_mnt,
0493 hugetlbfs_mnt_len)) {
0494 strcpy(event->mmap2.filename, anonstr);
0495 event->mmap2.flags |= MAP_HUGETLB;
0496 }
0497
0498 size = strlen(event->mmap2.filename) + 1;
0499 aligned_size = PERF_ALIGN(size, sizeof(u64));
0500 event->mmap2.len -= event->mmap.start;
0501 event->mmap2.header.size = (sizeof(event->mmap2) -
0502 (sizeof(event->mmap2.filename) - aligned_size));
0503 memset(event->mmap2.filename + size, 0, machine->id_hdr_size +
0504 (aligned_size - size));
0505 event->mmap2.header.size += machine->id_hdr_size;
0506 event->mmap2.pid = tgid;
0507 event->mmap2.tid = pid;
0508
0509 if (symbol_conf.buildid_mmap2)
0510 perf_record_mmap2__read_build_id(&event->mmap2, false);
0511
0512 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
0513 rc = -1;
0514 break;
0515 }
0516
0517 if (truncation)
0518 break;
0519 }
0520
0521 close(io.fd);
0522 return rc;
0523 }
0524
0525 #ifdef HAVE_FILE_HANDLE
0526 static int perf_event__synthesize_cgroup(struct perf_tool *tool,
0527 union perf_event *event,
0528 char *path, size_t mount_len,
0529 perf_event__handler_t process,
0530 struct machine *machine)
0531 {
0532 size_t event_size = sizeof(event->cgroup) - sizeof(event->cgroup.path);
0533 size_t path_len = strlen(path) - mount_len + 1;
0534 struct {
0535 struct file_handle fh;
0536 uint64_t cgroup_id;
0537 } handle;
0538 int mount_id;
0539
0540 while (path_len % sizeof(u64))
0541 path[mount_len + path_len++] = '\0';
0542
0543 memset(&event->cgroup, 0, event_size);
0544
0545 event->cgroup.header.type = PERF_RECORD_CGROUP;
0546 event->cgroup.header.size = event_size + path_len + machine->id_hdr_size;
0547
0548 handle.fh.handle_bytes = sizeof(handle.cgroup_id);
0549 if (name_to_handle_at(AT_FDCWD, path, &handle.fh, &mount_id, 0) < 0) {
0550 pr_debug("stat failed: %s\n", path);
0551 return -1;
0552 }
0553
0554 event->cgroup.id = handle.cgroup_id;
0555 strncpy(event->cgroup.path, path + mount_len, path_len);
0556 memset(event->cgroup.path + path_len, 0, machine->id_hdr_size);
0557
0558 if (perf_tool__process_synth_event(tool, event, machine, process) < 0) {
0559 pr_debug("process synth event failed\n");
0560 return -1;
0561 }
0562
0563 return 0;
0564 }
0565
0566 static int perf_event__walk_cgroup_tree(struct perf_tool *tool,
0567 union perf_event *event,
0568 char *path, size_t mount_len,
0569 perf_event__handler_t process,
0570 struct machine *machine)
0571 {
0572 size_t pos = strlen(path);
0573 DIR *d;
0574 struct dirent *dent;
0575 int ret = 0;
0576
0577 if (perf_event__synthesize_cgroup(tool, event, path, mount_len,
0578 process, machine) < 0)
0579 return -1;
0580
0581 d = opendir(path);
0582 if (d == NULL) {
0583 pr_debug("failed to open directory: %s\n", path);
0584 return -1;
0585 }
0586
0587 while ((dent = readdir(d)) != NULL) {
0588 if (dent->d_type != DT_DIR)
0589 continue;
0590 if (!strcmp(dent->d_name, ".") ||
0591 !strcmp(dent->d_name, ".."))
0592 continue;
0593
0594
0595 if (strlen(path) + strlen(dent->d_name) + 1 >= PATH_MAX)
0596 continue;
0597
0598 if (path[pos - 1] != '/')
0599 strcat(path, "/");
0600 strcat(path, dent->d_name);
0601
0602 ret = perf_event__walk_cgroup_tree(tool, event, path,
0603 mount_len, process, machine);
0604 if (ret < 0)
0605 break;
0606
0607 path[pos] = '\0';
0608 }
0609
0610 closedir(d);
0611 return ret;
0612 }
0613
0614 int perf_event__synthesize_cgroups(struct perf_tool *tool,
0615 perf_event__handler_t process,
0616 struct machine *machine)
0617 {
0618 union perf_event event;
0619 char cgrp_root[PATH_MAX];
0620 size_t mount_len;
0621
0622 if (!tool || !tool->cgroup_events)
0623 return 0;
0624
0625 if (cgroupfs_find_mountpoint(cgrp_root, PATH_MAX, "perf_event") < 0) {
0626 pr_debug("cannot find cgroup mount point\n");
0627 return -1;
0628 }
0629
0630 mount_len = strlen(cgrp_root);
0631
0632 strcat(cgrp_root, "/");
0633
0634 if (perf_event__walk_cgroup_tree(tool, &event, cgrp_root, mount_len,
0635 process, machine) < 0)
0636 return -1;
0637
0638 return 0;
0639 }
0640 #else
0641 int perf_event__synthesize_cgroups(struct perf_tool *tool __maybe_unused,
0642 perf_event__handler_t process __maybe_unused,
0643 struct machine *machine __maybe_unused)
0644 {
0645 return -1;
0646 }
0647 #endif
0648
0649 int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t process,
0650 struct machine *machine)
0651 {
0652 int rc = 0;
0653 struct map *pos;
0654 struct maps *maps = machine__kernel_maps(machine);
0655 union perf_event *event;
0656 size_t size = symbol_conf.buildid_mmap2 ?
0657 sizeof(event->mmap2) : sizeof(event->mmap);
0658
0659 event = zalloc(size + machine->id_hdr_size);
0660 if (event == NULL) {
0661 pr_debug("Not enough memory synthesizing mmap event "
0662 "for kernel modules\n");
0663 return -1;
0664 }
0665
0666
0667
0668
0669
0670 if (machine__is_host(machine))
0671 event->header.misc = PERF_RECORD_MISC_KERNEL;
0672 else
0673 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
0674
0675 maps__for_each_entry(maps, pos) {
0676 if (!__map__is_kmodule(pos))
0677 continue;
0678
0679 if (symbol_conf.buildid_mmap2) {
0680 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
0681 event->mmap2.header.type = PERF_RECORD_MMAP2;
0682 event->mmap2.header.size = (sizeof(event->mmap2) -
0683 (sizeof(event->mmap2.filename) - size));
0684 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
0685 event->mmap2.header.size += machine->id_hdr_size;
0686 event->mmap2.start = pos->start;
0687 event->mmap2.len = pos->end - pos->start;
0688 event->mmap2.pid = machine->pid;
0689
0690 memcpy(event->mmap2.filename, pos->dso->long_name,
0691 pos->dso->long_name_len + 1);
0692
0693 perf_record_mmap2__read_build_id(&event->mmap2, false);
0694 } else {
0695 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
0696 event->mmap.header.type = PERF_RECORD_MMAP;
0697 event->mmap.header.size = (sizeof(event->mmap) -
0698 (sizeof(event->mmap.filename) - size));
0699 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
0700 event->mmap.header.size += machine->id_hdr_size;
0701 event->mmap.start = pos->start;
0702 event->mmap.len = pos->end - pos->start;
0703 event->mmap.pid = machine->pid;
0704
0705 memcpy(event->mmap.filename, pos->dso->long_name,
0706 pos->dso->long_name_len + 1);
0707 }
0708
0709 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
0710 rc = -1;
0711 break;
0712 }
0713 }
0714
0715 free(event);
0716 return rc;
0717 }
0718
0719 static int filter_task(const struct dirent *dirent)
0720 {
0721 return isdigit(dirent->d_name[0]);
0722 }
0723
0724 static int __event__synthesize_thread(union perf_event *comm_event,
0725 union perf_event *mmap_event,
0726 union perf_event *fork_event,
0727 union perf_event *namespaces_event,
0728 pid_t pid, int full, perf_event__handler_t process,
0729 struct perf_tool *tool, struct machine *machine,
0730 bool needs_mmap, bool mmap_data)
0731 {
0732 char filename[PATH_MAX];
0733 struct dirent **dirent;
0734 pid_t tgid, ppid;
0735 int rc = 0;
0736 int i, n;
0737
0738
0739 if (!full) {
0740 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
0741 process, machine);
0742
0743 if (tgid == -1)
0744 return -1;
0745
0746 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
0747 tgid, process, machine) < 0)
0748 return -1;
0749
0750
0751
0752
0753
0754 if (pid == tgid && needs_mmap &&
0755 perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
0756 process, machine, mmap_data))
0757 return -1;
0758
0759 return 0;
0760 }
0761
0762 if (machine__is_default_guest(machine))
0763 return 0;
0764
0765 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
0766 machine->root_dir, pid);
0767
0768 n = scandir(filename, &dirent, filter_task, NULL);
0769 if (n < 0)
0770 return n;
0771
0772 for (i = 0; i < n; i++) {
0773 char *end;
0774 pid_t _pid;
0775 bool kernel_thread = false;
0776
0777 _pid = strtol(dirent[i]->d_name, &end, 10);
0778 if (*end)
0779 continue;
0780
0781
0782 if (perf_event__prepare_comm(comm_event, pid, _pid, machine,
0783 &tgid, &ppid, &kernel_thread) != 0)
0784 continue;
0785
0786 rc = -1;
0787 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
0788 ppid, process, machine) < 0)
0789 break;
0790
0791 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
0792 tgid, process, machine) < 0)
0793 break;
0794
0795
0796
0797
0798 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
0799 break;
0800
0801 rc = 0;
0802 if (_pid == pid && !kernel_thread && needs_mmap) {
0803
0804 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
0805 process, machine, mmap_data);
0806 if (rc)
0807 break;
0808 }
0809 }
0810
0811 for (i = 0; i < n; i++)
0812 zfree(&dirent[i]);
0813 free(dirent);
0814
0815 return rc;
0816 }
0817
0818 int perf_event__synthesize_thread_map(struct perf_tool *tool,
0819 struct perf_thread_map *threads,
0820 perf_event__handler_t process,
0821 struct machine *machine,
0822 bool needs_mmap, bool mmap_data)
0823 {
0824 union perf_event *comm_event, *mmap_event, *fork_event;
0825 union perf_event *namespaces_event;
0826 int err = -1, thread, j;
0827
0828 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
0829 if (comm_event == NULL)
0830 goto out;
0831
0832 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
0833 if (mmap_event == NULL)
0834 goto out_free_comm;
0835
0836 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
0837 if (fork_event == NULL)
0838 goto out_free_mmap;
0839
0840 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
0841 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
0842 machine->id_hdr_size);
0843 if (namespaces_event == NULL)
0844 goto out_free_fork;
0845
0846 err = 0;
0847 for (thread = 0; thread < threads->nr; ++thread) {
0848 if (__event__synthesize_thread(comm_event, mmap_event,
0849 fork_event, namespaces_event,
0850 perf_thread_map__pid(threads, thread), 0,
0851 process, tool, machine,
0852 needs_mmap, mmap_data)) {
0853 err = -1;
0854 break;
0855 }
0856
0857
0858
0859
0860
0861 if ((int) comm_event->comm.pid != perf_thread_map__pid(threads, thread)) {
0862 bool need_leader = true;
0863
0864
0865 for (j = 0; j < threads->nr; ++j) {
0866 if ((int) comm_event->comm.pid == perf_thread_map__pid(threads, j)) {
0867 need_leader = false;
0868 break;
0869 }
0870 }
0871
0872
0873 if (need_leader &&
0874 __event__synthesize_thread(comm_event, mmap_event,
0875 fork_event, namespaces_event,
0876 comm_event->comm.pid, 0,
0877 process, tool, machine,
0878 needs_mmap, mmap_data)) {
0879 err = -1;
0880 break;
0881 }
0882 }
0883 }
0884 free(namespaces_event);
0885 out_free_fork:
0886 free(fork_event);
0887 out_free_mmap:
0888 free(mmap_event);
0889 out_free_comm:
0890 free(comm_event);
0891 out:
0892 return err;
0893 }
0894
0895 static int __perf_event__synthesize_threads(struct perf_tool *tool,
0896 perf_event__handler_t process,
0897 struct machine *machine,
0898 bool needs_mmap,
0899 bool mmap_data,
0900 struct dirent **dirent,
0901 int start,
0902 int num)
0903 {
0904 union perf_event *comm_event, *mmap_event, *fork_event;
0905 union perf_event *namespaces_event;
0906 int err = -1;
0907 char *end;
0908 pid_t pid;
0909 int i;
0910
0911 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
0912 if (comm_event == NULL)
0913 goto out;
0914
0915 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
0916 if (mmap_event == NULL)
0917 goto out_free_comm;
0918
0919 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
0920 if (fork_event == NULL)
0921 goto out_free_mmap;
0922
0923 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
0924 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
0925 machine->id_hdr_size);
0926 if (namespaces_event == NULL)
0927 goto out_free_fork;
0928
0929 for (i = start; i < start + num; i++) {
0930 if (!isdigit(dirent[i]->d_name[0]))
0931 continue;
0932
0933 pid = (pid_t)strtol(dirent[i]->d_name, &end, 10);
0934
0935 if (*end)
0936 continue;
0937
0938
0939
0940
0941 __event__synthesize_thread(comm_event, mmap_event, fork_event,
0942 namespaces_event, pid, 1, process,
0943 tool, machine, needs_mmap, mmap_data);
0944 }
0945 err = 0;
0946
0947 free(namespaces_event);
0948 out_free_fork:
0949 free(fork_event);
0950 out_free_mmap:
0951 free(mmap_event);
0952 out_free_comm:
0953 free(comm_event);
0954 out:
0955 return err;
0956 }
0957
0958 struct synthesize_threads_arg {
0959 struct perf_tool *tool;
0960 perf_event__handler_t process;
0961 struct machine *machine;
0962 bool needs_mmap;
0963 bool mmap_data;
0964 struct dirent **dirent;
0965 int num;
0966 int start;
0967 };
0968
0969 static void *synthesize_threads_worker(void *arg)
0970 {
0971 struct synthesize_threads_arg *args = arg;
0972
0973 __perf_event__synthesize_threads(args->tool, args->process,
0974 args->machine,
0975 args->needs_mmap, args->mmap_data,
0976 args->dirent,
0977 args->start, args->num);
0978 return NULL;
0979 }
0980
0981 int perf_event__synthesize_threads(struct perf_tool *tool,
0982 perf_event__handler_t process,
0983 struct machine *machine,
0984 bool needs_mmap, bool mmap_data,
0985 unsigned int nr_threads_synthesize)
0986 {
0987 struct synthesize_threads_arg *args = NULL;
0988 pthread_t *synthesize_threads = NULL;
0989 char proc_path[PATH_MAX];
0990 struct dirent **dirent;
0991 int num_per_thread;
0992 int m, n, i, j;
0993 int thread_nr;
0994 int base = 0;
0995 int err = -1;
0996
0997
0998 if (machine__is_default_guest(machine))
0999 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
1097
1098
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
1110
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 perf_record_cpu_map_data *data,
1199 const struct perf_cpu_map *map)
1200 {
1201 int i, map_nr = perf_cpu_map__nr(map);
1202
1203 data->cpus_data.nr = map_nr;
1204
1205 for (i = 0; i < map_nr; i++)
1206 data->cpus_data.cpu[i] = perf_cpu_map__cpu(map, i).cpu;
1207 }
1208
1209 static void synthesize_mask(struct perf_record_cpu_map_data *data,
1210 const struct perf_cpu_map *map, int max)
1211 {
1212 int idx;
1213 struct perf_cpu cpu;
1214
1215
1216 data->mask32_data.nr = BITS_TO_U32(max);
1217 data->mask32_data.long_size = 4;
1218
1219 perf_cpu_map__for_each_cpu(cpu, idx, map) {
1220 int bit_word = cpu.cpu / 32;
1221 __u32 bit_mask = 1U << (cpu.cpu & 31);
1222
1223 data->mask32_data.mask[bit_word] |= bit_mask;
1224 }
1225 }
1226
1227 static size_t cpus_size(const struct perf_cpu_map *map)
1228 {
1229 return sizeof(struct cpu_map_entries) + perf_cpu_map__nr(map) * sizeof(u16);
1230 }
1231
1232 static size_t mask_size(const struct perf_cpu_map *map, int *max)
1233 {
1234 *max = perf_cpu_map__max(map).cpu;
1235 return sizeof(struct perf_record_mask_cpu_map32) + BITS_TO_U32(*max) * sizeof(__u32);
1236 }
1237
1238 static void *cpu_map_data__alloc(const struct perf_cpu_map *map, size_t *size,
1239 u16 *type, int *max)
1240 {
1241 size_t size_cpus, size_mask;
1242 bool is_dummy = perf_cpu_map__empty(map);
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257 size_cpus = cpus_size(map);
1258 size_mask = mask_size(map, max);
1259
1260 if (is_dummy || (size_cpus < size_mask)) {
1261 *size += size_cpus;
1262 *type = PERF_CPU_MAP__CPUS;
1263 } else {
1264 *size += size_mask;
1265 *type = PERF_CPU_MAP__MASK;
1266 }
1267
1268 *size += sizeof(__u16);
1269 *size = PERF_ALIGN(*size, sizeof(u64));
1270 return zalloc(*size);
1271 }
1272
1273 static void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data,
1274 const struct perf_cpu_map *map,
1275 u16 type, int max)
1276 {
1277 data->type = type;
1278
1279 switch (type) {
1280 case PERF_CPU_MAP__CPUS:
1281 synthesize_cpus(data, map);
1282 break;
1283 case PERF_CPU_MAP__MASK:
1284 synthesize_mask(data, map, max);
1285 default:
1286 break;
1287 }
1288 }
1289
1290 static struct perf_record_cpu_map *cpu_map_event__new(const struct perf_cpu_map *map)
1291 {
1292 size_t size = sizeof(struct perf_event_header);
1293 struct perf_record_cpu_map *event;
1294 int max;
1295 u16 type;
1296
1297 event = cpu_map_data__alloc(map, &size, &type, &max);
1298 if (!event)
1299 return NULL;
1300
1301 event->header.type = PERF_RECORD_CPU_MAP;
1302 event->header.size = size;
1303 event->data.type = type;
1304
1305 cpu_map_data__synthesize(&event->data, map, type, max);
1306 return event;
1307 }
1308
1309 int perf_event__synthesize_cpu_map(struct perf_tool *tool,
1310 const struct perf_cpu_map *map,
1311 perf_event__handler_t process,
1312 struct machine *machine)
1313 {
1314 struct perf_record_cpu_map *event;
1315 int err;
1316
1317 event = cpu_map_event__new(map);
1318 if (!event)
1319 return -ENOMEM;
1320
1321 err = process(tool, (union perf_event *) event, NULL, machine);
1322
1323 free(event);
1324 return err;
1325 }
1326
1327 int perf_event__synthesize_stat_config(struct perf_tool *tool,
1328 struct perf_stat_config *config,
1329 perf_event__handler_t process,
1330 struct machine *machine)
1331 {
1332 struct perf_record_stat_config *event;
1333 int size, i = 0, err;
1334
1335 size = sizeof(*event);
1336 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1337
1338 event = zalloc(size);
1339 if (!event)
1340 return -ENOMEM;
1341
1342 event->header.type = PERF_RECORD_STAT_CONFIG;
1343 event->header.size = size;
1344 event->nr = PERF_STAT_CONFIG_TERM__MAX;
1345
1346 #define ADD(__term, __val) \
1347 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1348 event->data[i].val = __val; \
1349 i++;
1350
1351 ADD(AGGR_MODE, config->aggr_mode)
1352 ADD(INTERVAL, config->interval)
1353 ADD(SCALE, config->scale)
1354
1355 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1356 "stat config terms unbalanced\n");
1357 #undef ADD
1358
1359 err = process(tool, (union perf_event *) event, NULL, machine);
1360
1361 free(event);
1362 return err;
1363 }
1364
1365 int perf_event__synthesize_stat(struct perf_tool *tool,
1366 struct perf_cpu cpu, u32 thread, u64 id,
1367 struct perf_counts_values *count,
1368 perf_event__handler_t process,
1369 struct machine *machine)
1370 {
1371 struct perf_record_stat event;
1372
1373 event.header.type = PERF_RECORD_STAT;
1374 event.header.size = sizeof(event);
1375 event.header.misc = 0;
1376
1377 event.id = id;
1378 event.cpu = cpu.cpu;
1379 event.thread = thread;
1380 event.val = count->val;
1381 event.ena = count->ena;
1382 event.run = count->run;
1383
1384 return process(tool, (union perf_event *) &event, NULL, machine);
1385 }
1386
1387 int perf_event__synthesize_stat_round(struct perf_tool *tool,
1388 u64 evtime, u64 type,
1389 perf_event__handler_t process,
1390 struct machine *machine)
1391 {
1392 struct perf_record_stat_round event;
1393
1394 event.header.type = PERF_RECORD_STAT_ROUND;
1395 event.header.size = sizeof(event);
1396 event.header.misc = 0;
1397
1398 event.time = evtime;
1399 event.type = type;
1400
1401 return process(tool, (union perf_event *) &event, NULL, machine);
1402 }
1403
1404 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type, u64 read_format)
1405 {
1406 size_t sz, result = sizeof(struct perf_record_sample);
1407
1408 if (type & PERF_SAMPLE_IDENTIFIER)
1409 result += sizeof(u64);
1410
1411 if (type & PERF_SAMPLE_IP)
1412 result += sizeof(u64);
1413
1414 if (type & PERF_SAMPLE_TID)
1415 result += sizeof(u64);
1416
1417 if (type & PERF_SAMPLE_TIME)
1418 result += sizeof(u64);
1419
1420 if (type & PERF_SAMPLE_ADDR)
1421 result += sizeof(u64);
1422
1423 if (type & PERF_SAMPLE_ID)
1424 result += sizeof(u64);
1425
1426 if (type & PERF_SAMPLE_STREAM_ID)
1427 result += sizeof(u64);
1428
1429 if (type & PERF_SAMPLE_CPU)
1430 result += sizeof(u64);
1431
1432 if (type & PERF_SAMPLE_PERIOD)
1433 result += sizeof(u64);
1434
1435 if (type & PERF_SAMPLE_READ) {
1436 result += sizeof(u64);
1437 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1438 result += sizeof(u64);
1439 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1440 result += sizeof(u64);
1441
1442 if (read_format & PERF_FORMAT_GROUP) {
1443 sz = sample_read_value_size(read_format);
1444 result += sz * sample->read.group.nr;
1445 } else {
1446 result += sizeof(u64);
1447 if (read_format & PERF_FORMAT_LOST)
1448 result += sizeof(u64);
1449 }
1450 }
1451
1452 if (type & PERF_SAMPLE_CALLCHAIN) {
1453 sz = (sample->callchain->nr + 1) * sizeof(u64);
1454 result += sz;
1455 }
1456
1457 if (type & PERF_SAMPLE_RAW) {
1458 result += sizeof(u32);
1459 result += sample->raw_size;
1460 }
1461
1462 if (type & PERF_SAMPLE_BRANCH_STACK) {
1463 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1464
1465 sz += 2 * sizeof(u64);
1466 result += sz;
1467 }
1468
1469 if (type & PERF_SAMPLE_REGS_USER) {
1470 if (sample->user_regs.abi) {
1471 result += sizeof(u64);
1472 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
1473 result += sz;
1474 } else {
1475 result += sizeof(u64);
1476 }
1477 }
1478
1479 if (type & PERF_SAMPLE_STACK_USER) {
1480 sz = sample->user_stack.size;
1481 result += sizeof(u64);
1482 if (sz) {
1483 result += sz;
1484 result += sizeof(u64);
1485 }
1486 }
1487
1488 if (type & PERF_SAMPLE_WEIGHT_TYPE)
1489 result += sizeof(u64);
1490
1491 if (type & PERF_SAMPLE_DATA_SRC)
1492 result += sizeof(u64);
1493
1494 if (type & PERF_SAMPLE_TRANSACTION)
1495 result += sizeof(u64);
1496
1497 if (type & PERF_SAMPLE_REGS_INTR) {
1498 if (sample->intr_regs.abi) {
1499 result += sizeof(u64);
1500 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
1501 result += sz;
1502 } else {
1503 result += sizeof(u64);
1504 }
1505 }
1506
1507 if (type & PERF_SAMPLE_PHYS_ADDR)
1508 result += sizeof(u64);
1509
1510 if (type & PERF_SAMPLE_CGROUP)
1511 result += sizeof(u64);
1512
1513 if (type & PERF_SAMPLE_DATA_PAGE_SIZE)
1514 result += sizeof(u64);
1515
1516 if (type & PERF_SAMPLE_CODE_PAGE_SIZE)
1517 result += sizeof(u64);
1518
1519 if (type & PERF_SAMPLE_AUX) {
1520 result += sizeof(u64);
1521 result += sample->aux_sample.size;
1522 }
1523
1524 return result;
1525 }
1526
1527 void __weak arch_perf_synthesize_sample_weight(const struct perf_sample *data,
1528 __u64 *array, u64 type __maybe_unused)
1529 {
1530 *array = data->weight;
1531 }
1532
1533 static __u64 *copy_read_group_values(__u64 *array, __u64 read_format,
1534 const struct perf_sample *sample)
1535 {
1536 size_t sz = sample_read_value_size(read_format);
1537 struct sample_read_value *v = sample->read.group.values;
1538
1539 sample_read_group__for_each(v, sample->read.group.nr, read_format) {
1540
1541 memcpy(array, v, sz);
1542 array = (void *)array + sz;
1543 }
1544 return array;
1545 }
1546
1547 int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_format,
1548 const struct perf_sample *sample)
1549 {
1550 __u64 *array;
1551 size_t sz;
1552
1553
1554
1555
1556 union u64_swap u;
1557
1558 array = event->sample.array;
1559
1560 if (type & PERF_SAMPLE_IDENTIFIER) {
1561 *array = sample->id;
1562 array++;
1563 }
1564
1565 if (type & PERF_SAMPLE_IP) {
1566 *array = sample->ip;
1567 array++;
1568 }
1569
1570 if (type & PERF_SAMPLE_TID) {
1571 u.val32[0] = sample->pid;
1572 u.val32[1] = sample->tid;
1573 *array = u.val64;
1574 array++;
1575 }
1576
1577 if (type & PERF_SAMPLE_TIME) {
1578 *array = sample->time;
1579 array++;
1580 }
1581
1582 if (type & PERF_SAMPLE_ADDR) {
1583 *array = sample->addr;
1584 array++;
1585 }
1586
1587 if (type & PERF_SAMPLE_ID) {
1588 *array = sample->id;
1589 array++;
1590 }
1591
1592 if (type & PERF_SAMPLE_STREAM_ID) {
1593 *array = sample->stream_id;
1594 array++;
1595 }
1596
1597 if (type & PERF_SAMPLE_CPU) {
1598 u.val32[0] = sample->cpu;
1599 u.val32[1] = 0;
1600 *array = u.val64;
1601 array++;
1602 }
1603
1604 if (type & PERF_SAMPLE_PERIOD) {
1605 *array = sample->period;
1606 array++;
1607 }
1608
1609 if (type & PERF_SAMPLE_READ) {
1610 if (read_format & PERF_FORMAT_GROUP)
1611 *array = sample->read.group.nr;
1612 else
1613 *array = sample->read.one.value;
1614 array++;
1615
1616 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1617 *array = sample->read.time_enabled;
1618 array++;
1619 }
1620
1621 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1622 *array = sample->read.time_running;
1623 array++;
1624 }
1625
1626
1627 if (read_format & PERF_FORMAT_GROUP) {
1628 array = copy_read_group_values(array, read_format,
1629 sample);
1630 } else {
1631 *array = sample->read.one.id;
1632 array++;
1633
1634 if (read_format & PERF_FORMAT_LOST) {
1635 *array = sample->read.one.lost;
1636 array++;
1637 }
1638 }
1639 }
1640
1641 if (type & PERF_SAMPLE_CALLCHAIN) {
1642 sz = (sample->callchain->nr + 1) * sizeof(u64);
1643 memcpy(array, sample->callchain, sz);
1644 array = (void *)array + sz;
1645 }
1646
1647 if (type & PERF_SAMPLE_RAW) {
1648 u.val32[0] = sample->raw_size;
1649 *array = u.val64;
1650 array = (void *)array + sizeof(u32);
1651
1652 memcpy(array, sample->raw_data, sample->raw_size);
1653 array = (void *)array + sample->raw_size;
1654 }
1655
1656 if (type & PERF_SAMPLE_BRANCH_STACK) {
1657 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1658
1659 sz += 2 * sizeof(u64);
1660 memcpy(array, sample->branch_stack, sz);
1661 array = (void *)array + sz;
1662 }
1663
1664 if (type & PERF_SAMPLE_REGS_USER) {
1665 if (sample->user_regs.abi) {
1666 *array++ = sample->user_regs.abi;
1667 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
1668 memcpy(array, sample->user_regs.regs, sz);
1669 array = (void *)array + sz;
1670 } else {
1671 *array++ = 0;
1672 }
1673 }
1674
1675 if (type & PERF_SAMPLE_STACK_USER) {
1676 sz = sample->user_stack.size;
1677 *array++ = sz;
1678 if (sz) {
1679 memcpy(array, sample->user_stack.data, sz);
1680 array = (void *)array + sz;
1681 *array++ = sz;
1682 }
1683 }
1684
1685 if (type & PERF_SAMPLE_WEIGHT_TYPE) {
1686 arch_perf_synthesize_sample_weight(sample, array, type);
1687 array++;
1688 }
1689
1690 if (type & PERF_SAMPLE_DATA_SRC) {
1691 *array = sample->data_src;
1692 array++;
1693 }
1694
1695 if (type & PERF_SAMPLE_TRANSACTION) {
1696 *array = sample->transaction;
1697 array++;
1698 }
1699
1700 if (type & PERF_SAMPLE_REGS_INTR) {
1701 if (sample->intr_regs.abi) {
1702 *array++ = sample->intr_regs.abi;
1703 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
1704 memcpy(array, sample->intr_regs.regs, sz);
1705 array = (void *)array + sz;
1706 } else {
1707 *array++ = 0;
1708 }
1709 }
1710
1711 if (type & PERF_SAMPLE_PHYS_ADDR) {
1712 *array = sample->phys_addr;
1713 array++;
1714 }
1715
1716 if (type & PERF_SAMPLE_CGROUP) {
1717 *array = sample->cgroup;
1718 array++;
1719 }
1720
1721 if (type & PERF_SAMPLE_DATA_PAGE_SIZE) {
1722 *array = sample->data_page_size;
1723 array++;
1724 }
1725
1726 if (type & PERF_SAMPLE_CODE_PAGE_SIZE) {
1727 *array = sample->code_page_size;
1728 array++;
1729 }
1730
1731 if (type & PERF_SAMPLE_AUX) {
1732 sz = sample->aux_sample.size;
1733 *array++ = sz;
1734 memcpy(array, sample->aux_sample.data, sz);
1735 array = (void *)array + sz;
1736 }
1737
1738 return 0;
1739 }
1740
1741 int perf_event__synthesize_id_sample(__u64 *array, u64 type, const struct perf_sample *sample)
1742 {
1743 __u64 *start = array;
1744
1745
1746
1747
1748
1749 union u64_swap u;
1750
1751 if (type & PERF_SAMPLE_TID) {
1752 u.val32[0] = sample->pid;
1753 u.val32[1] = sample->tid;
1754 *array = u.val64;
1755 array++;
1756 }
1757
1758 if (type & PERF_SAMPLE_TIME) {
1759 *array = sample->time;
1760 array++;
1761 }
1762
1763 if (type & PERF_SAMPLE_ID) {
1764 *array = sample->id;
1765 array++;
1766 }
1767
1768 if (type & PERF_SAMPLE_STREAM_ID) {
1769 *array = sample->stream_id;
1770 array++;
1771 }
1772
1773 if (type & PERF_SAMPLE_CPU) {
1774 u.val32[0] = sample->cpu;
1775 u.val32[1] = 0;
1776 *array = u.val64;
1777 array++;
1778 }
1779
1780 if (type & PERF_SAMPLE_IDENTIFIER) {
1781 *array = sample->id;
1782 array++;
1783 }
1784
1785 return (void *)array - (void *)start;
1786 }
1787
1788 int __perf_event__synthesize_id_index(struct perf_tool *tool, perf_event__handler_t process,
1789 struct evlist *evlist, struct machine *machine, size_t from)
1790 {
1791 union perf_event *ev;
1792 struct evsel *evsel;
1793 size_t nr = 0, i = 0, sz, max_nr, n, pos;
1794 size_t e1_sz = sizeof(struct id_index_entry);
1795 size_t e2_sz = sizeof(struct id_index_entry_2);
1796 size_t etot_sz = e1_sz + e2_sz;
1797 bool e2_needed = false;
1798 int err;
1799
1800 max_nr = (UINT16_MAX - sizeof(struct perf_record_id_index)) / etot_sz;
1801
1802 pos = 0;
1803 evlist__for_each_entry(evlist, evsel) {
1804 if (pos++ < from)
1805 continue;
1806 nr += evsel->core.ids;
1807 }
1808
1809 if (!nr)
1810 return 0;
1811
1812 pr_debug2("Synthesizing id index\n");
1813
1814 n = nr > max_nr ? max_nr : nr;
1815 sz = sizeof(struct perf_record_id_index) + n * etot_sz;
1816 ev = zalloc(sz);
1817 if (!ev)
1818 return -ENOMEM;
1819
1820 sz = sizeof(struct perf_record_id_index) + n * e1_sz;
1821
1822 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
1823 ev->id_index.nr = n;
1824
1825 pos = 0;
1826 evlist__for_each_entry(evlist, evsel) {
1827 u32 j;
1828
1829 if (pos++ < from)
1830 continue;
1831 for (j = 0; j < evsel->core.ids; j++, i++) {
1832 struct id_index_entry *e;
1833 struct id_index_entry_2 *e2;
1834 struct perf_sample_id *sid;
1835
1836 if (i >= n) {
1837 ev->id_index.header.size = sz + (e2_needed ? n * e2_sz : 0);
1838 err = process(tool, ev, NULL, machine);
1839 if (err)
1840 goto out_err;
1841 nr -= n;
1842 i = 0;
1843 e2_needed = false;
1844 }
1845
1846 e = &ev->id_index.entries[i];
1847
1848 e->id = evsel->core.id[j];
1849
1850 sid = evlist__id2sid(evlist, e->id);
1851 if (!sid) {
1852 free(ev);
1853 return -ENOENT;
1854 }
1855
1856 e->idx = sid->idx;
1857 e->cpu = sid->cpu.cpu;
1858 e->tid = sid->tid;
1859
1860 if (sid->machine_pid)
1861 e2_needed = true;
1862
1863 e2 = (void *)ev + sz;
1864 e2[i].machine_pid = sid->machine_pid;
1865 e2[i].vcpu = sid->vcpu.cpu;
1866 }
1867 }
1868
1869 sz = sizeof(struct perf_record_id_index) + nr * e1_sz;
1870 ev->id_index.header.size = sz + (e2_needed ? nr * e2_sz : 0);
1871 ev->id_index.nr = nr;
1872
1873 err = process(tool, ev, NULL, machine);
1874 out_err:
1875 free(ev);
1876
1877 return err;
1878 }
1879
1880 int perf_event__synthesize_id_index(struct perf_tool *tool, perf_event__handler_t process,
1881 struct evlist *evlist, struct machine *machine)
1882 {
1883 return __perf_event__synthesize_id_index(tool, process, evlist, machine, 0);
1884 }
1885
1886 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
1887 struct target *target, struct perf_thread_map *threads,
1888 perf_event__handler_t process, bool needs_mmap,
1889 bool data_mmap, unsigned int nr_threads_synthesize)
1890 {
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902 if (!nsinfo__is_in_root_namespace()) {
1903 pr_err("Perf runs in non-root PID namespace but it tries to ");
1904 pr_err("gather process info from its parent PID namespace.\n");
1905 pr_err("Please mount the proc file system properly, e.g. ");
1906 pr_err("add the option '--mount-proc' for unshare command.\n");
1907 return -EPERM;
1908 }
1909
1910 if (target__has_task(target))
1911 return perf_event__synthesize_thread_map(tool, threads, process, machine,
1912 needs_mmap, data_mmap);
1913 else if (target__has_cpu(target))
1914 return perf_event__synthesize_threads(tool, process, machine,
1915 needs_mmap, data_mmap,
1916 nr_threads_synthesize);
1917
1918 return 0;
1919 }
1920
1921 int machine__synthesize_threads(struct machine *machine, struct target *target,
1922 struct perf_thread_map *threads, bool needs_mmap,
1923 bool data_mmap, unsigned int nr_threads_synthesize)
1924 {
1925 return __machine__synthesize_threads(machine, NULL, target, threads,
1926 perf_event__process, needs_mmap,
1927 data_mmap, nr_threads_synthesize);
1928 }
1929
1930 static struct perf_record_event_update *event_update_event__new(size_t size, u64 type, u64 id)
1931 {
1932 struct perf_record_event_update *ev;
1933
1934 size += sizeof(*ev);
1935 size = PERF_ALIGN(size, sizeof(u64));
1936
1937 ev = zalloc(size);
1938 if (ev) {
1939 ev->header.type = PERF_RECORD_EVENT_UPDATE;
1940 ev->header.size = (u16)size;
1941 ev->type = type;
1942 ev->id = id;
1943 }
1944 return ev;
1945 }
1946
1947 int perf_event__synthesize_event_update_unit(struct perf_tool *tool, struct evsel *evsel,
1948 perf_event__handler_t process)
1949 {
1950 size_t size = strlen(evsel->unit);
1951 struct perf_record_event_update *ev;
1952 int err;
1953
1954 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->core.id[0]);
1955 if (ev == NULL)
1956 return -ENOMEM;
1957
1958 strlcpy(ev->data, evsel->unit, size + 1);
1959 err = process(tool, (union perf_event *)ev, NULL, NULL);
1960 free(ev);
1961 return err;
1962 }
1963
1964 int perf_event__synthesize_event_update_scale(struct perf_tool *tool, struct evsel *evsel,
1965 perf_event__handler_t process)
1966 {
1967 struct perf_record_event_update *ev;
1968 struct perf_record_event_update_scale *ev_data;
1969 int err;
1970
1971 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->core.id[0]);
1972 if (ev == NULL)
1973 return -ENOMEM;
1974
1975 ev_data = (struct perf_record_event_update_scale *)ev->data;
1976 ev_data->scale = evsel->scale;
1977 err = process(tool, (union perf_event *)ev, NULL, NULL);
1978 free(ev);
1979 return err;
1980 }
1981
1982 int perf_event__synthesize_event_update_name(struct perf_tool *tool, struct evsel *evsel,
1983 perf_event__handler_t process)
1984 {
1985 struct perf_record_event_update *ev;
1986 size_t len = strlen(evsel->name);
1987 int err;
1988
1989 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->core.id[0]);
1990 if (ev == NULL)
1991 return -ENOMEM;
1992
1993 strlcpy(ev->data, evsel->name, len + 1);
1994 err = process(tool, (union perf_event *)ev, NULL, NULL);
1995 free(ev);
1996 return err;
1997 }
1998
1999 int perf_event__synthesize_event_update_cpus(struct perf_tool *tool, struct evsel *evsel,
2000 perf_event__handler_t process)
2001 {
2002 size_t size = sizeof(struct perf_record_event_update);
2003 struct perf_record_event_update *ev;
2004 int max, err;
2005 u16 type;
2006
2007 if (!evsel->core.own_cpus)
2008 return 0;
2009
2010 ev = cpu_map_data__alloc(evsel->core.own_cpus, &size, &type, &max);
2011 if (!ev)
2012 return -ENOMEM;
2013
2014 ev->header.type = PERF_RECORD_EVENT_UPDATE;
2015 ev->header.size = (u16)size;
2016 ev->type = PERF_EVENT_UPDATE__CPUS;
2017 ev->id = evsel->core.id[0];
2018
2019 cpu_map_data__synthesize((struct perf_record_cpu_map_data *)ev->data,
2020 evsel->core.own_cpus, type, max);
2021
2022 err = process(tool, (union perf_event *)ev, NULL, NULL);
2023 free(ev);
2024 return err;
2025 }
2026
2027 int perf_event__synthesize_attrs(struct perf_tool *tool, struct evlist *evlist,
2028 perf_event__handler_t process)
2029 {
2030 struct evsel *evsel;
2031 int err = 0;
2032
2033 evlist__for_each_entry(evlist, evsel) {
2034 err = perf_event__synthesize_attr(tool, &evsel->core.attr, evsel->core.ids,
2035 evsel->core.id, process);
2036 if (err) {
2037 pr_debug("failed to create perf header attribute\n");
2038 return err;
2039 }
2040 }
2041
2042 return err;
2043 }
2044
2045 static bool has_unit(struct evsel *evsel)
2046 {
2047 return evsel->unit && *evsel->unit;
2048 }
2049
2050 static bool has_scale(struct evsel *evsel)
2051 {
2052 return evsel->scale != 1;
2053 }
2054
2055 int perf_event__synthesize_extra_attr(struct perf_tool *tool, struct evlist *evsel_list,
2056 perf_event__handler_t process, bool is_pipe)
2057 {
2058 struct evsel *evsel;
2059 int err;
2060
2061
2062
2063
2064
2065 evlist__for_each_entry(evsel_list, evsel) {
2066 if (!evsel->supported)
2067 continue;
2068
2069
2070
2071
2072 if (has_unit(evsel)) {
2073 err = perf_event__synthesize_event_update_unit(tool, evsel, process);
2074 if (err < 0) {
2075 pr_err("Couldn't synthesize evsel unit.\n");
2076 return err;
2077 }
2078 }
2079
2080 if (has_scale(evsel)) {
2081 err = perf_event__synthesize_event_update_scale(tool, evsel, process);
2082 if (err < 0) {
2083 pr_err("Couldn't synthesize evsel evsel.\n");
2084 return err;
2085 }
2086 }
2087
2088 if (evsel->core.own_cpus) {
2089 err = perf_event__synthesize_event_update_cpus(tool, evsel, process);
2090 if (err < 0) {
2091 pr_err("Couldn't synthesize evsel cpus.\n");
2092 return err;
2093 }
2094 }
2095
2096
2097
2098
2099
2100 if (is_pipe) {
2101 err = perf_event__synthesize_event_update_name(tool, evsel, process);
2102 if (err < 0) {
2103 pr_err("Couldn't synthesize evsel name.\n");
2104 return err;
2105 }
2106 }
2107 }
2108 return 0;
2109 }
2110
2111 int perf_event__synthesize_attr(struct perf_tool *tool, struct perf_event_attr *attr,
2112 u32 ids, u64 *id, perf_event__handler_t process)
2113 {
2114 union perf_event *ev;
2115 size_t size;
2116 int err;
2117
2118 size = sizeof(struct perf_event_attr);
2119 size = PERF_ALIGN(size, sizeof(u64));
2120 size += sizeof(struct perf_event_header);
2121 size += ids * sizeof(u64);
2122
2123 ev = zalloc(size);
2124
2125 if (ev == NULL)
2126 return -ENOMEM;
2127
2128 ev->attr.attr = *attr;
2129 memcpy(ev->attr.id, id, ids * sizeof(u64));
2130
2131 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2132 ev->attr.header.size = (u16)size;
2133
2134 if (ev->attr.header.size == size)
2135 err = process(tool, ev, NULL, NULL);
2136 else
2137 err = -E2BIG;
2138
2139 free(ev);
2140
2141 return err;
2142 }
2143
2144 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, struct evlist *evlist,
2145 perf_event__handler_t process)
2146 {
2147 union perf_event ev;
2148 struct tracing_data *tdata;
2149 ssize_t size = 0, aligned_size = 0, padding;
2150 struct feat_fd ff;
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163 tdata = tracing_data_get(&evlist->core.entries, fd, true);
2164 if (!tdata)
2165 return -1;
2166
2167 memset(&ev, 0, sizeof(ev));
2168
2169 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2170 size = tdata->size;
2171 aligned_size = PERF_ALIGN(size, sizeof(u64));
2172 padding = aligned_size - size;
2173 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2174 ev.tracing_data.size = aligned_size;
2175
2176 process(tool, &ev, NULL, NULL);
2177
2178
2179
2180
2181
2182 tracing_data_put(tdata);
2183
2184 ff = (struct feat_fd){ .fd = fd };
2185 if (write_padded(&ff, NULL, 0, padding))
2186 return -1;
2187
2188 return aligned_size;
2189 }
2190
2191 int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16 misc,
2192 perf_event__handler_t process, struct machine *machine)
2193 {
2194 union perf_event ev;
2195 size_t len;
2196
2197 if (!pos->hit)
2198 return 0;
2199
2200 memset(&ev, 0, sizeof(ev));
2201
2202 len = pos->long_name_len + 1;
2203 len = PERF_ALIGN(len, NAME_ALIGN);
2204 memcpy(&ev.build_id.build_id, pos->bid.data, sizeof(pos->bid.data));
2205 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2206 ev.build_id.header.misc = misc;
2207 ev.build_id.pid = machine->pid;
2208 ev.build_id.header.size = sizeof(ev.build_id) + len;
2209 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2210
2211 return process(tool, &ev, NULL, machine);
2212 }
2213
2214 int perf_event__synthesize_stat_events(struct perf_stat_config *config, struct perf_tool *tool,
2215 struct evlist *evlist, perf_event__handler_t process, bool attrs)
2216 {
2217 int err;
2218
2219 if (attrs) {
2220 err = perf_event__synthesize_attrs(tool, evlist, process);
2221 if (err < 0) {
2222 pr_err("Couldn't synthesize attrs.\n");
2223 return err;
2224 }
2225 }
2226
2227 err = perf_event__synthesize_extra_attr(tool, evlist, process, attrs);
2228 err = perf_event__synthesize_thread_map2(tool, evlist->core.threads, process, NULL);
2229 if (err < 0) {
2230 pr_err("Couldn't synthesize thread map.\n");
2231 return err;
2232 }
2233
2234 err = perf_event__synthesize_cpu_map(tool, evlist->core.user_requested_cpus, process, NULL);
2235 if (err < 0) {
2236 pr_err("Couldn't synthesize thread map.\n");
2237 return err;
2238 }
2239
2240 err = perf_event__synthesize_stat_config(tool, config, process, NULL);
2241 if (err < 0) {
2242 pr_err("Couldn't synthesize config.\n");
2243 return err;
2244 }
2245
2246 return 0;
2247 }
2248
2249 extern const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
2250
2251 int perf_event__synthesize_features(struct perf_tool *tool, struct perf_session *session,
2252 struct evlist *evlist, perf_event__handler_t process)
2253 {
2254 struct perf_header *header = &session->header;
2255 struct perf_record_header_feature *fe;
2256 struct feat_fd ff;
2257 size_t sz, sz_hdr;
2258 int feat, ret;
2259
2260 sz_hdr = sizeof(fe->header);
2261 sz = sizeof(union perf_event);
2262
2263 sz = PERF_ALIGN(sz, page_size);
2264
2265 memset(&ff, 0, sizeof(ff));
2266
2267 ff.buf = malloc(sz);
2268 if (!ff.buf)
2269 return -ENOMEM;
2270
2271 ff.size = sz - sz_hdr;
2272 ff.ph = &session->header;
2273
2274 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2275 if (!feat_ops[feat].synthesize) {
2276 pr_debug("No record header feature for header :%d\n", feat);
2277 continue;
2278 }
2279
2280 ff.offset = sizeof(*fe);
2281
2282 ret = feat_ops[feat].write(&ff, evlist);
2283 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
2284 pr_debug("Error writing feature\n");
2285 continue;
2286 }
2287
2288 fe = ff.buf;
2289 memset(fe, 0, sizeof(*fe));
2290
2291 fe->feat_id = feat;
2292 fe->header.type = PERF_RECORD_HEADER_FEATURE;
2293 fe->header.size = ff.offset;
2294
2295 ret = process(tool, ff.buf, NULL, NULL);
2296 if (ret) {
2297 free(ff.buf);
2298 return ret;
2299 }
2300 }
2301
2302
2303 fe = ff.buf;
2304 fe->feat_id = HEADER_LAST_FEATURE;
2305 fe->header.type = PERF_RECORD_HEADER_FEATURE;
2306 fe->header.size = sizeof(*fe);
2307
2308 ret = process(tool, ff.buf, NULL, NULL);
2309
2310 free(ff.buf);
2311 return ret;
2312 }
2313
2314 int perf_event__synthesize_for_pipe(struct perf_tool *tool,
2315 struct perf_session *session,
2316 struct perf_data *data,
2317 perf_event__handler_t process)
2318 {
2319 int err;
2320 int ret = 0;
2321 struct evlist *evlist = session->evlist;
2322
2323
2324
2325
2326
2327 err = perf_event__synthesize_attrs(tool, evlist, process);
2328 if (err < 0) {
2329 pr_err("Couldn't synthesize attrs.\n");
2330 return err;
2331 }
2332 ret += err;
2333
2334 err = perf_event__synthesize_features(tool, session, evlist, process);
2335 if (err < 0) {
2336 pr_err("Couldn't synthesize features.\n");
2337 return err;
2338 }
2339 ret += err;
2340
2341 if (have_tracepoints(&evlist->core.entries)) {
2342 int fd = perf_data__fd(data);
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352 err = perf_event__synthesize_tracing_data(tool, fd, evlist,
2353 process);
2354 if (err <= 0) {
2355 pr_err("Couldn't record tracing data.\n");
2356 return err;
2357 }
2358 ret += err;
2359 }
2360
2361 return ret;
2362 }
2363
2364 int parse_synth_opt(char *synth)
2365 {
2366 char *p, *q;
2367 int ret = 0;
2368
2369 if (synth == NULL)
2370 return -1;
2371
2372 for (q = synth; (p = strsep(&q, ",")); p = q) {
2373 if (!strcasecmp(p, "no") || !strcasecmp(p, "none"))
2374 return 0;
2375
2376 if (!strcasecmp(p, "all"))
2377 return PERF_SYNTH_ALL;
2378
2379 if (!strcasecmp(p, "task"))
2380 ret |= PERF_SYNTH_TASK;
2381 else if (!strcasecmp(p, "mmap"))
2382 ret |= PERF_SYNTH_TASK | PERF_SYNTH_MMAP;
2383 else if (!strcasecmp(p, "cgroup"))
2384 ret |= PERF_SYNTH_CGROUP;
2385 else
2386 return -1;
2387 }
2388
2389 return ret;
2390 }