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