blob: 3a898520f05cb9f8282d5fc59922469e93b0bd79 [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
Jiri Olsa4183a8d2020-12-14 11:54:52 +0100350static void perf_record_mmap2__read_build_id(struct perf_record_mmap2 *event,
351 bool is_kernel)
352{
353 struct build_id bid;
354 int rc;
355
356 if (is_kernel)
357 rc = sysfs__read_build_id("/sys/kernel/notes", &bid);
358 else
359 rc = filename__read_build_id(event->filename, &bid) > 0 ? 0 : -1;
360
361 if (rc == 0) {
362 memcpy(event->build_id, bid.data, sizeof(bid.data));
363 event->build_id_size = (u8) bid.size;
364 event->header.misc |= PERF_RECORD_MISC_MMAP_BUILD_ID;
365 event->__reserved_1 = 0;
366 event->__reserved_2 = 0;
367 } else {
368 if (event->filename[0] == '/') {
369 pr_debug2("Failed to read build ID for %s\n",
370 event->filename);
371 }
372 }
373}
374
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300375int perf_event__synthesize_mmap_events(struct perf_tool *tool,
376 union perf_event *event,
377 pid_t pid, pid_t tgid,
378 perf_event__handler_t process,
379 struct machine *machine,
380 bool mmap_data)
381{
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300382 unsigned long long t;
Ian Rogers04ed4cc2020-04-02 08:43:55 -0700383 char bf[BUFSIZ];
Ian Rogers20694252020-04-14 22:40:50 -0700384 struct io io;
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300385 bool truncation = false;
386 unsigned long long timeout = proc_map_timeout * 1000000ULL;
387 int rc = 0;
388 const char *hugetlbfs_mnt = hugetlbfs__mountpoint();
389 int hugetlbfs_mnt_len = hugetlbfs_mnt ? strlen(hugetlbfs_mnt) : 0;
390
391 if (machine__is_default_guest(machine))
392 return 0;
393
Ian Rogers04ed4cc2020-04-02 08:43:55 -0700394 snprintf(bf, sizeof(bf), "%s/proc/%d/task/%d/maps",
395 machine->root_dir, pid, pid);
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300396
Ian Rogers20694252020-04-14 22:40:50 -0700397 io.fd = open(bf, O_RDONLY, 0);
398 if (io.fd < 0) {
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300399 /*
400 * We raced with a task exiting - just return:
401 */
Ian Rogers04ed4cc2020-04-02 08:43:55 -0700402 pr_debug("couldn't open %s\n", bf);
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300403 return -1;
404 }
Ian Rogers20694252020-04-14 22:40:50 -0700405 io__init(&io, io.fd, bf, sizeof(bf));
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300406
407 event->header.type = PERF_RECORD_MMAP2;
408 t = rdclock();
409
Ian Rogers20694252020-04-14 22:40:50 -0700410 while (!io.eof) {
411 static const char anonstr[] = "//anon";
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300412 size_t size;
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300413
Ian Rogers20694252020-04-14 22:40:50 -0700414 /* ensure null termination since stack will be reused. */
415 event->mmap2.filename[0] = '\0';
416
417 /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */
418 if (!read_proc_maps_line(&io,
419 &event->mmap2.start,
420 &event->mmap2.len,
421 &event->mmap2.prot,
422 &event->mmap2.flags,
423 &event->mmap2.pgoff,
424 &event->mmap2.maj,
425 &event->mmap2.min,
426 &event->mmap2.ino,
427 sizeof(event->mmap2.filename),
428 event->mmap2.filename))
429 continue;
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300430
431 if ((rdclock() - t) > timeout) {
Ian Rogers04ed4cc2020-04-02 08:43:55 -0700432 pr_warning("Reading %s/proc/%d/task/%d/maps time out. "
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300433 "You may want to increase "
434 "the time limit by --proc-map-timeout\n",
Ian Rogers04ed4cc2020-04-02 08:43:55 -0700435 machine->root_dir, pid, pid);
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300436 truncation = true;
437 goto out;
438 }
439
Ian Rogers3b7a15b2020-03-12 22:31:29 -0700440 event->mmap2.ino_generation = 0;
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300441
442 /*
443 * Just like the kernel, see __perf_event_mmap in kernel/perf_event.c
444 */
445 if (machine__is_host(machine))
446 event->header.misc = PERF_RECORD_MISC_USER;
447 else
448 event->header.misc = PERF_RECORD_MISC_GUEST_USER;
449
Ian Rogers20694252020-04-14 22:40:50 -0700450 if ((event->mmap2.prot & PROT_EXEC) == 0) {
451 if (!mmap_data || (event->mmap2.prot & PROT_READ) == 0)
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300452 continue;
453
454 event->header.misc |= PERF_RECORD_MISC_MMAP_DATA;
455 }
456
457out:
458 if (truncation)
459 event->header.misc |= PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT;
460
Ian Rogers20694252020-04-14 22:40:50 -0700461 if (!strcmp(event->mmap2.filename, ""))
462 strcpy(event->mmap2.filename, anonstr);
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300463
464 if (hugetlbfs_mnt_len &&
Ian Rogers20694252020-04-14 22:40:50 -0700465 !strncmp(event->mmap2.filename, hugetlbfs_mnt,
466 hugetlbfs_mnt_len)) {
467 strcpy(event->mmap2.filename, anonstr);
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300468 event->mmap2.flags |= MAP_HUGETLB;
469 }
470
Ian Rogers20694252020-04-14 22:40:50 -0700471 size = strlen(event->mmap2.filename) + 1;
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300472 size = PERF_ALIGN(size, sizeof(u64));
473 event->mmap2.len -= event->mmap.start;
474 event->mmap2.header.size = (sizeof(event->mmap2) -
475 (sizeof(event->mmap2.filename) - size));
476 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
477 event->mmap2.header.size += machine->id_hdr_size;
478 event->mmap2.pid = tgid;
479 event->mmap2.tid = pid;
480
Jiri Olsa4183a8d2020-12-14 11:54:52 +0100481 if (symbol_conf.buildid_mmap2)
482 perf_record_mmap2__read_build_id(&event->mmap2, false);
483
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300484 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
485 rc = -1;
486 break;
487 }
488
489 if (truncation)
490 break;
491 }
492
Ian Rogers20694252020-04-14 22:40:50 -0700493 close(io.fd);
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300494 return rc;
495}
496
Namhyung Kimab640692020-03-25 21:45:33 +0900497#ifdef HAVE_FILE_HANDLE
498static int perf_event__synthesize_cgroup(struct perf_tool *tool,
499 union perf_event *event,
500 char *path, size_t mount_len,
501 perf_event__handler_t process,
502 struct machine *machine)
503{
504 size_t event_size = sizeof(event->cgroup) - sizeof(event->cgroup.path);
505 size_t path_len = strlen(path) - mount_len + 1;
506 struct {
507 struct file_handle fh;
508 uint64_t cgroup_id;
509 } handle;
510 int mount_id;
511
512 while (path_len % sizeof(u64))
513 path[mount_len + path_len++] = '\0';
514
515 memset(&event->cgroup, 0, event_size);
516
517 event->cgroup.header.type = PERF_RECORD_CGROUP;
518 event->cgroup.header.size = event_size + path_len + machine->id_hdr_size;
519
520 handle.fh.handle_bytes = sizeof(handle.cgroup_id);
521 if (name_to_handle_at(AT_FDCWD, path, &handle.fh, &mount_id, 0) < 0) {
522 pr_debug("stat failed: %s\n", path);
523 return -1;
524 }
525
526 event->cgroup.id = handle.cgroup_id;
527 strncpy(event->cgroup.path, path + mount_len, path_len);
528 memset(event->cgroup.path + path_len, 0, machine->id_hdr_size);
529
530 if (perf_tool__process_synth_event(tool, event, machine, process) < 0) {
531 pr_debug("process synth event failed\n");
532 return -1;
533 }
534
535 return 0;
536}
537
538static int perf_event__walk_cgroup_tree(struct perf_tool *tool,
539 union perf_event *event,
540 char *path, size_t mount_len,
541 perf_event__handler_t process,
542 struct machine *machine)
543{
544 size_t pos = strlen(path);
545 DIR *d;
546 struct dirent *dent;
547 int ret = 0;
548
549 if (perf_event__synthesize_cgroup(tool, event, path, mount_len,
550 process, machine) < 0)
551 return -1;
552
553 d = opendir(path);
554 if (d == NULL) {
555 pr_debug("failed to open directory: %s\n", path);
556 return -1;
557 }
558
559 while ((dent = readdir(d)) != NULL) {
560 if (dent->d_type != DT_DIR)
561 continue;
562 if (!strcmp(dent->d_name, ".") ||
563 !strcmp(dent->d_name, ".."))
564 continue;
565
566 /* any sane path should be less than PATH_MAX */
567 if (strlen(path) + strlen(dent->d_name) + 1 >= PATH_MAX)
568 continue;
569
570 if (path[pos - 1] != '/')
571 strcat(path, "/");
572 strcat(path, dent->d_name);
573
574 ret = perf_event__walk_cgroup_tree(tool, event, path,
575 mount_len, process, machine);
576 if (ret < 0)
577 break;
578
579 path[pos] = '\0';
580 }
581
582 closedir(d);
583 return ret;
584}
585
586int perf_event__synthesize_cgroups(struct perf_tool *tool,
587 perf_event__handler_t process,
588 struct machine *machine)
589{
590 union perf_event event;
591 char cgrp_root[PATH_MAX];
592 size_t mount_len; /* length of mount point in the path */
593
Namhyung Kimaa50d9532020-11-27 14:43:56 +0900594 if (!tool || !tool->cgroup_events)
595 return 0;
596
Namhyung Kimab640692020-03-25 21:45:33 +0900597 if (cgroupfs_find_mountpoint(cgrp_root, PATH_MAX, "perf_event") < 0) {
598 pr_debug("cannot find cgroup mount point\n");
599 return -1;
600 }
601
602 mount_len = strlen(cgrp_root);
603 /* make sure the path starts with a slash (after mount point) */
604 strcat(cgrp_root, "/");
605
606 if (perf_event__walk_cgroup_tree(tool, &event, cgrp_root, mount_len,
607 process, machine) < 0)
608 return -1;
609
610 return 0;
611}
612#else
613int perf_event__synthesize_cgroups(struct perf_tool *tool __maybe_unused,
614 perf_event__handler_t process __maybe_unused,
615 struct machine *machine __maybe_unused)
616{
617 return -1;
618}
619#endif
620
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300621int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t process,
622 struct machine *machine)
623{
624 int rc = 0;
625 struct map *pos;
626 struct maps *maps = machine__kernel_maps(machine);
Jiri Olsae0dbf182020-12-14 11:54:51 +0100627 union perf_event *event;
628 size_t size = symbol_conf.buildid_mmap2 ?
629 sizeof(event->mmap2) : sizeof(event->mmap);
630
631 event = zalloc(size + machine->id_hdr_size);
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300632 if (event == NULL) {
633 pr_debug("Not enough memory synthesizing mmap event "
634 "for kernel modules\n");
635 return -1;
636 }
637
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300638 /*
639 * kernel uses 0 for user space maps, see kernel/perf_event.c
640 * __perf_event_mmap
641 */
642 if (machine__is_host(machine))
643 event->header.misc = PERF_RECORD_MISC_KERNEL;
644 else
645 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
646
Arnaldo Carvalho de Melo8efc4f02019-10-28 11:31:38 -0300647 maps__for_each_entry(maps, pos) {
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300648 if (!__map__is_kmodule(pos))
649 continue;
650
Jiri Olsae0dbf182020-12-14 11:54:51 +0100651 if (symbol_conf.buildid_mmap2) {
652 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
653 event->mmap2.header.type = PERF_RECORD_MMAP2;
654 event->mmap2.header.size = (sizeof(event->mmap2) -
655 (sizeof(event->mmap2.filename) - size));
656 memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
657 event->mmap2.header.size += machine->id_hdr_size;
658 event->mmap2.start = pos->start;
659 event->mmap2.len = pos->end - pos->start;
660 event->mmap2.pid = machine->pid;
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300661
Jiri Olsae0dbf182020-12-14 11:54:51 +0100662 memcpy(event->mmap2.filename, pos->dso->long_name,
663 pos->dso->long_name_len + 1);
Jiri Olsa4183a8d2020-12-14 11:54:52 +0100664
665 perf_record_mmap2__read_build_id(&event->mmap2, false);
Jiri Olsae0dbf182020-12-14 11:54:51 +0100666 } else {
667 size = PERF_ALIGN(pos->dso->long_name_len + 1, sizeof(u64));
668 event->mmap.header.type = PERF_RECORD_MMAP;
669 event->mmap.header.size = (sizeof(event->mmap) -
670 (sizeof(event->mmap.filename) - size));
671 memset(event->mmap.filename + size, 0, machine->id_hdr_size);
672 event->mmap.header.size += machine->id_hdr_size;
673 event->mmap.start = pos->start;
674 event->mmap.len = pos->end - pos->start;
675 event->mmap.pid = machine->pid;
676
677 memcpy(event->mmap.filename, pos->dso->long_name,
678 pos->dso->long_name_len + 1);
679 }
680
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300681 if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
682 rc = -1;
683 break;
684 }
685 }
686
687 free(event);
688 return rc;
689}
690
691static int __event__synthesize_thread(union perf_event *comm_event,
692 union perf_event *mmap_event,
693 union perf_event *fork_event,
694 union perf_event *namespaces_event,
695 pid_t pid, int full, perf_event__handler_t process,
696 struct perf_tool *tool, struct machine *machine, bool mmap_data)
697{
698 char filename[PATH_MAX];
699 DIR *tasks;
700 struct dirent *dirent;
701 pid_t tgid, ppid;
702 int rc = 0;
703
704 /* special case: only send one comm event using passed in pid */
705 if (!full) {
706 tgid = perf_event__synthesize_comm(tool, comm_event, pid,
707 process, machine);
708
709 if (tgid == -1)
710 return -1;
711
712 if (perf_event__synthesize_namespaces(tool, namespaces_event, pid,
713 tgid, process, machine) < 0)
714 return -1;
715
716 /*
717 * send mmap only for thread group leader
Arnaldo Carvalho de Melo79b6bb72019-11-25 21:58:33 -0300718 * see thread__init_maps()
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -0300719 */
720 if (pid == tgid &&
721 perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
722 process, machine, mmap_data))
723 return -1;
724
725 return 0;
726 }
727
728 if (machine__is_default_guest(machine))
729 return 0;
730
731 snprintf(filename, sizeof(filename), "%s/proc/%d/task",
732 machine->root_dir, pid);
733
734 tasks = opendir(filename);
735 if (tasks == NULL) {
736 pr_debug("couldn't open %s\n", filename);
737 return 0;
738 }
739
740 while ((dirent = readdir(tasks)) != NULL) {
741 char *end;
742 pid_t _pid;
743
744 _pid = strtol(dirent->d_name, &end, 10);
745 if (*end)
746 continue;
747
748 rc = -1;
749 if (perf_event__prepare_comm(comm_event, _pid, machine,
750 &tgid, &ppid) != 0)
751 break;
752
753 if (perf_event__synthesize_fork(tool, fork_event, _pid, tgid,
754 ppid, process, machine) < 0)
755 break;
756
757 if (perf_event__synthesize_namespaces(tool, namespaces_event, _pid,
758 tgid, process, machine) < 0)
759 break;
760
761 /*
762 * Send the prepared comm event
763 */
764 if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
765 break;
766
767 rc = 0;
768 if (_pid == pid) {
769 /* process the parent's maps too */
770 rc = perf_event__synthesize_mmap_events(tool, mmap_event, pid, tgid,
771 process, machine, mmap_data);
772 if (rc)
773 break;
774 }
775 }
776
777 closedir(tasks);
778 return rc;
779}
780
781int perf_event__synthesize_thread_map(struct perf_tool *tool,
782 struct perf_thread_map *threads,
783 perf_event__handler_t process,
784 struct machine *machine,
785 bool mmap_data)
786{
787 union perf_event *comm_event, *mmap_event, *fork_event;
788 union perf_event *namespaces_event;
789 int err = -1, thread, j;
790
791 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
792 if (comm_event == NULL)
793 goto out;
794
795 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
796 if (mmap_event == NULL)
797 goto out_free_comm;
798
799 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
800 if (fork_event == NULL)
801 goto out_free_mmap;
802
803 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
804 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
805 machine->id_hdr_size);
806 if (namespaces_event == NULL)
807 goto out_free_fork;
808
809 err = 0;
810 for (thread = 0; thread < threads->nr; ++thread) {
811 if (__event__synthesize_thread(comm_event, mmap_event,
812 fork_event, namespaces_event,
813 perf_thread_map__pid(threads, thread), 0,
814 process, tool, machine,
815 mmap_data)) {
816 err = -1;
817 break;
818 }
819
820 /*
821 * comm.pid is set to thread group id by
822 * perf_event__synthesize_comm
823 */
824 if ((int) comm_event->comm.pid != perf_thread_map__pid(threads, thread)) {
825 bool need_leader = true;
826
827 /* is thread group leader in thread_map? */
828 for (j = 0; j < threads->nr; ++j) {
829 if ((int) comm_event->comm.pid == perf_thread_map__pid(threads, j)) {
830 need_leader = false;
831 break;
832 }
833 }
834
835 /* if not, generate events for it */
836 if (need_leader &&
837 __event__synthesize_thread(comm_event, mmap_event,
838 fork_event, namespaces_event,
839 comm_event->comm.pid, 0,
840 process, tool, machine,
841 mmap_data)) {
842 err = -1;
843 break;
844 }
845 }
846 }
847 free(namespaces_event);
848out_free_fork:
849 free(fork_event);
850out_free_mmap:
851 free(mmap_event);
852out_free_comm:
853 free(comm_event);
854out:
855 return err;
856}
857
858static int __perf_event__synthesize_threads(struct perf_tool *tool,
859 perf_event__handler_t process,
860 struct machine *machine,
861 bool mmap_data,
862 struct dirent **dirent,
863 int start,
864 int num)
865{
866 union perf_event *comm_event, *mmap_event, *fork_event;
867 union perf_event *namespaces_event;
868 int err = -1;
869 char *end;
870 pid_t pid;
871 int i;
872
873 comm_event = malloc(sizeof(comm_event->comm) + machine->id_hdr_size);
874 if (comm_event == NULL)
875 goto out;
876
877 mmap_event = malloc(sizeof(mmap_event->mmap2) + machine->id_hdr_size);
878 if (mmap_event == NULL)
879 goto out_free_comm;
880
881 fork_event = malloc(sizeof(fork_event->fork) + machine->id_hdr_size);
882 if (fork_event == NULL)
883 goto out_free_mmap;
884
885 namespaces_event = malloc(sizeof(namespaces_event->namespaces) +
886 (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
887 machine->id_hdr_size);
888 if (namespaces_event == NULL)
889 goto out_free_fork;
890
891 for (i = start; i < start + num; i++) {
892 if (!isdigit(dirent[i]->d_name[0]))
893 continue;
894
895 pid = (pid_t)strtol(dirent[i]->d_name, &end, 10);
896 /* only interested in proper numerical dirents */
897 if (*end)
898 continue;
899 /*
900 * We may race with exiting thread, so don't stop just because
901 * one thread couldn't be synthesized.
902 */
903 __event__synthesize_thread(comm_event, mmap_event, fork_event,
904 namespaces_event, pid, 1, process,
905 tool, machine, mmap_data);
906 }
907 err = 0;
908
909 free(namespaces_event);
910out_free_fork:
911 free(fork_event);
912out_free_mmap:
913 free(mmap_event);
914out_free_comm:
915 free(comm_event);
916out:
917 return err;
918}
919
920struct synthesize_threads_arg {
921 struct perf_tool *tool;
922 perf_event__handler_t process;
923 struct machine *machine;
924 bool mmap_data;
925 struct dirent **dirent;
926 int num;
927 int start;
928};
929
930static void *synthesize_threads_worker(void *arg)
931{
932 struct synthesize_threads_arg *args = arg;
933
934 __perf_event__synthesize_threads(args->tool, args->process,
935 args->machine, args->mmap_data,
936 args->dirent,
937 args->start, args->num);
938 return NULL;
939}
940
941int perf_event__synthesize_threads(struct perf_tool *tool,
942 perf_event__handler_t process,
943 struct machine *machine,
944 bool mmap_data,
945 unsigned int nr_threads_synthesize)
946{
947 struct synthesize_threads_arg *args = NULL;
948 pthread_t *synthesize_threads = NULL;
949 char proc_path[PATH_MAX];
950 struct dirent **dirent;
951 int num_per_thread;
952 int m, n, i, j;
953 int thread_nr;
954 int base = 0;
955 int err = -1;
956
957
958 if (machine__is_default_guest(machine))
959 return 0;
960
961 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir);
962 n = scandir(proc_path, &dirent, 0, alphasort);
963 if (n < 0)
964 return err;
965
966 if (nr_threads_synthesize == UINT_MAX)
967 thread_nr = sysconf(_SC_NPROCESSORS_ONLN);
968 else
969 thread_nr = nr_threads_synthesize;
970
971 if (thread_nr <= 1) {
972 err = __perf_event__synthesize_threads(tool, process,
973 machine, mmap_data,
974 dirent, base, n);
975 goto free_dirent;
976 }
977 if (thread_nr > n)
978 thread_nr = n;
979
980 synthesize_threads = calloc(sizeof(pthread_t), thread_nr);
981 if (synthesize_threads == NULL)
982 goto free_dirent;
983
984 args = calloc(sizeof(*args), thread_nr);
985 if (args == NULL)
986 goto free_threads;
987
988 num_per_thread = n / thread_nr;
989 m = n % thread_nr;
990 for (i = 0; i < thread_nr; i++) {
991 args[i].tool = tool;
992 args[i].process = process;
993 args[i].machine = machine;
994 args[i].mmap_data = mmap_data;
995 args[i].dirent = dirent;
996 }
997 for (i = 0; i < m; i++) {
998 args[i].num = num_per_thread + 1;
999 args[i].start = i * args[i].num;
1000 }
1001 if (i != 0)
1002 base = args[i-1].start + args[i-1].num;
1003 for (j = i; j < thread_nr; j++) {
1004 args[j].num = num_per_thread;
1005 args[j].start = base + (j - i) * args[i].num;
1006 }
1007
1008 for (i = 0; i < thread_nr; i++) {
1009 if (pthread_create(&synthesize_threads[i], NULL,
1010 synthesize_threads_worker, &args[i]))
1011 goto out_join;
1012 }
1013 err = 0;
1014out_join:
1015 for (i = 0; i < thread_nr; i++)
1016 pthread_join(synthesize_threads[i], NULL);
1017 free(args);
1018free_threads:
1019 free(synthesize_threads);
1020free_dirent:
1021 for (i = 0; i < n; i++)
1022 zfree(&dirent[i]);
1023 free(dirent);
1024
1025 return err;
1026}
1027
1028int __weak perf_event__synthesize_extra_kmaps(struct perf_tool *tool __maybe_unused,
1029 perf_event__handler_t process __maybe_unused,
1030 struct machine *machine __maybe_unused)
1031{
1032 return 0;
1033}
1034
1035static int __perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
1036 perf_event__handler_t process,
1037 struct machine *machine)
1038{
Jiri Olsa978410f2020-12-14 11:54:50 +01001039 union perf_event *event;
1040 size_t size = symbol_conf.buildid_mmap2 ?
1041 sizeof(event->mmap2) : sizeof(event->mmap);
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001042 struct map *map = machine__kernel_map(machine);
1043 struct kmap *kmap;
1044 int err;
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001045
1046 if (map == NULL)
1047 return -1;
1048
1049 kmap = map__kmap(map);
1050 if (!kmap->ref_reloc_sym)
1051 return -1;
1052
1053 /*
1054 * We should get this from /sys/kernel/sections/.text, but till that is
1055 * available use this, and after it is use this as a fallback for older
1056 * kernels.
1057 */
Jiri Olsa978410f2020-12-14 11:54:50 +01001058 event = zalloc(size + machine->id_hdr_size);
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001059 if (event == NULL) {
1060 pr_debug("Not enough memory synthesizing mmap event "
1061 "for kernel modules\n");
1062 return -1;
1063 }
1064
1065 if (machine__is_host(machine)) {
1066 /*
1067 * kernel uses PERF_RECORD_MISC_USER for user space maps,
1068 * see kernel/perf_event.c __perf_event_mmap
1069 */
1070 event->header.misc = PERF_RECORD_MISC_KERNEL;
1071 } else {
1072 event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
1073 }
1074
Jiri Olsa978410f2020-12-14 11:54:50 +01001075 if (symbol_conf.buildid_mmap2) {
1076 size = snprintf(event->mmap2.filename, sizeof(event->mmap2.filename),
1077 "%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1;
1078 size = PERF_ALIGN(size, sizeof(u64));
1079 event->mmap2.header.type = PERF_RECORD_MMAP2;
1080 event->mmap2.header.size = (sizeof(event->mmap2) -
1081 (sizeof(event->mmap2.filename) - size) + machine->id_hdr_size);
1082 event->mmap2.pgoff = kmap->ref_reloc_sym->addr;
1083 event->mmap2.start = map->start;
1084 event->mmap2.len = map->end - event->mmap.start;
1085 event->mmap2.pid = machine->pid;
Jiri Olsa4183a8d2020-12-14 11:54:52 +01001086
1087 perf_record_mmap2__read_build_id(&event->mmap2, true);
Jiri Olsa978410f2020-12-14 11:54:50 +01001088 } else {
1089 size = snprintf(event->mmap.filename, sizeof(event->mmap.filename),
1090 "%s%s", machine->mmap_name, kmap->ref_reloc_sym->name) + 1;
1091 size = PERF_ALIGN(size, sizeof(u64));
1092 event->mmap.header.type = PERF_RECORD_MMAP;
1093 event->mmap.header.size = (sizeof(event->mmap) -
1094 (sizeof(event->mmap.filename) - size) + machine->id_hdr_size);
1095 event->mmap.pgoff = kmap->ref_reloc_sym->addr;
1096 event->mmap.start = map->start;
1097 event->mmap.len = map->end - event->mmap.start;
1098 event->mmap.pid = machine->pid;
1099 }
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001100
1101 err = perf_tool__process_synth_event(tool, event, machine, process);
1102 free(event);
1103
1104 return err;
1105}
1106
1107int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
1108 perf_event__handler_t process,
1109 struct machine *machine)
1110{
1111 int err;
1112
1113 err = __perf_event__synthesize_kernel_mmap(tool, process, machine);
1114 if (err < 0)
1115 return err;
1116
1117 return perf_event__synthesize_extra_kmaps(tool, process, machine);
1118}
1119
1120int perf_event__synthesize_thread_map2(struct perf_tool *tool,
1121 struct perf_thread_map *threads,
1122 perf_event__handler_t process,
1123 struct machine *machine)
1124{
1125 union perf_event *event;
1126 int i, err, size;
1127
1128 size = sizeof(event->thread_map);
1129 size += threads->nr * sizeof(event->thread_map.entries[0]);
1130
1131 event = zalloc(size);
1132 if (!event)
1133 return -ENOMEM;
1134
1135 event->header.type = PERF_RECORD_THREAD_MAP;
1136 event->header.size = size;
1137 event->thread_map.nr = threads->nr;
1138
1139 for (i = 0; i < threads->nr; i++) {
1140 struct perf_record_thread_map_entry *entry = &event->thread_map.entries[i];
1141 char *comm = perf_thread_map__comm(threads, i);
1142
1143 if (!comm)
1144 comm = (char *) "";
1145
1146 entry->pid = perf_thread_map__pid(threads, i);
1147 strncpy((char *) &entry->comm, comm, sizeof(entry->comm));
1148 }
1149
1150 err = process(tool, event, NULL, machine);
1151
1152 free(event);
1153 return err;
1154}
1155
1156static void synthesize_cpus(struct cpu_map_entries *cpus,
1157 struct perf_cpu_map *map)
1158{
1159 int i;
1160
1161 cpus->nr = map->nr;
1162
1163 for (i = 0; i < map->nr; i++)
1164 cpus->cpu[i] = map->map[i];
1165}
1166
1167static void synthesize_mask(struct perf_record_record_cpu_map *mask,
1168 struct perf_cpu_map *map, int max)
1169{
1170 int i;
1171
1172 mask->nr = BITS_TO_LONGS(max);
1173 mask->long_size = sizeof(long);
1174
1175 for (i = 0; i < map->nr; i++)
1176 set_bit(map->map[i], mask->mask);
1177}
1178
1179static size_t cpus_size(struct perf_cpu_map *map)
1180{
1181 return sizeof(struct cpu_map_entries) + map->nr * sizeof(u16);
1182}
1183
1184static size_t mask_size(struct perf_cpu_map *map, int *max)
1185{
1186 int i;
1187
1188 *max = 0;
1189
1190 for (i = 0; i < map->nr; i++) {
1191 /* bit possition of the cpu is + 1 */
1192 int bit = map->map[i] + 1;
1193
1194 if (bit > *max)
1195 *max = bit;
1196 }
1197
1198 return sizeof(struct perf_record_record_cpu_map) + BITS_TO_LONGS(*max) * sizeof(long);
1199}
1200
1201void *cpu_map_data__alloc(struct perf_cpu_map *map, size_t *size, u16 *type, int *max)
1202{
1203 size_t size_cpus, size_mask;
1204 bool is_dummy = perf_cpu_map__empty(map);
1205
1206 /*
1207 * Both array and mask data have variable size based
1208 * on the number of cpus and their actual values.
1209 * The size of the 'struct perf_record_cpu_map_data' is:
1210 *
1211 * array = size of 'struct cpu_map_entries' +
1212 * number of cpus * sizeof(u64)
1213 *
1214 * mask = size of 'struct perf_record_record_cpu_map' +
1215 * maximum cpu bit converted to size of longs
1216 *
1217 * and finaly + the size of 'struct perf_record_cpu_map_data'.
1218 */
1219 size_cpus = cpus_size(map);
1220 size_mask = mask_size(map, max);
1221
1222 if (is_dummy || (size_cpus < size_mask)) {
1223 *size += size_cpus;
1224 *type = PERF_CPU_MAP__CPUS;
1225 } else {
1226 *size += size_mask;
1227 *type = PERF_CPU_MAP__MASK;
1228 }
1229
1230 *size += sizeof(struct perf_record_cpu_map_data);
1231 *size = PERF_ALIGN(*size, sizeof(u64));
1232 return zalloc(*size);
1233}
1234
1235void cpu_map_data__synthesize(struct perf_record_cpu_map_data *data, struct perf_cpu_map *map,
1236 u16 type, int max)
1237{
1238 data->type = type;
1239
1240 switch (type) {
1241 case PERF_CPU_MAP__CPUS:
1242 synthesize_cpus((struct cpu_map_entries *) data->data, map);
1243 break;
1244 case PERF_CPU_MAP__MASK:
1245 synthesize_mask((struct perf_record_record_cpu_map *)data->data, map, max);
1246 default:
1247 break;
Zou Wei8284bbe2020-04-28 17:18:43 +08001248 }
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001249}
1250
1251static struct perf_record_cpu_map *cpu_map_event__new(struct perf_cpu_map *map)
1252{
1253 size_t size = sizeof(struct perf_record_cpu_map);
1254 struct perf_record_cpu_map *event;
1255 int max;
1256 u16 type;
1257
1258 event = cpu_map_data__alloc(map, &size, &type, &max);
1259 if (!event)
1260 return NULL;
1261
1262 event->header.type = PERF_RECORD_CPU_MAP;
1263 event->header.size = size;
1264 event->data.type = type;
1265
1266 cpu_map_data__synthesize(&event->data, map, type, max);
1267 return event;
1268}
1269
1270int perf_event__synthesize_cpu_map(struct perf_tool *tool,
1271 struct perf_cpu_map *map,
1272 perf_event__handler_t process,
1273 struct machine *machine)
1274{
1275 struct perf_record_cpu_map *event;
1276 int err;
1277
1278 event = cpu_map_event__new(map);
1279 if (!event)
1280 return -ENOMEM;
1281
1282 err = process(tool, (union perf_event *) event, NULL, machine);
1283
1284 free(event);
1285 return err;
1286}
1287
1288int perf_event__synthesize_stat_config(struct perf_tool *tool,
1289 struct perf_stat_config *config,
1290 perf_event__handler_t process,
1291 struct machine *machine)
1292{
1293 struct perf_record_stat_config *event;
1294 int size, i = 0, err;
1295
1296 size = sizeof(*event);
1297 size += (PERF_STAT_CONFIG_TERM__MAX * sizeof(event->data[0]));
1298
1299 event = zalloc(size);
1300 if (!event)
1301 return -ENOMEM;
1302
1303 event->header.type = PERF_RECORD_STAT_CONFIG;
1304 event->header.size = size;
1305 event->nr = PERF_STAT_CONFIG_TERM__MAX;
1306
1307#define ADD(__term, __val) \
1308 event->data[i].tag = PERF_STAT_CONFIG_TERM__##__term; \
1309 event->data[i].val = __val; \
1310 i++;
1311
1312 ADD(AGGR_MODE, config->aggr_mode)
1313 ADD(INTERVAL, config->interval)
1314 ADD(SCALE, config->scale)
1315
1316 WARN_ONCE(i != PERF_STAT_CONFIG_TERM__MAX,
1317 "stat config terms unbalanced\n");
1318#undef ADD
1319
1320 err = process(tool, (union perf_event *) event, NULL, machine);
1321
1322 free(event);
1323 return err;
1324}
1325
1326int perf_event__synthesize_stat(struct perf_tool *tool,
1327 u32 cpu, u32 thread, u64 id,
1328 struct perf_counts_values *count,
1329 perf_event__handler_t process,
1330 struct machine *machine)
1331{
1332 struct perf_record_stat event;
1333
1334 event.header.type = PERF_RECORD_STAT;
1335 event.header.size = sizeof(event);
1336 event.header.misc = 0;
1337
1338 event.id = id;
1339 event.cpu = cpu;
1340 event.thread = thread;
1341 event.val = count->val;
1342 event.ena = count->ena;
1343 event.run = count->run;
1344
1345 return process(tool, (union perf_event *) &event, NULL, machine);
1346}
1347
1348int perf_event__synthesize_stat_round(struct perf_tool *tool,
1349 u64 evtime, u64 type,
1350 perf_event__handler_t process,
1351 struct machine *machine)
1352{
1353 struct perf_record_stat_round event;
1354
1355 event.header.type = PERF_RECORD_STAT_ROUND;
1356 event.header.size = sizeof(event);
1357 event.header.misc = 0;
1358
1359 event.time = evtime;
1360 event.type = type;
1361
1362 return process(tool, (union perf_event *) &event, NULL, machine);
1363}
1364
1365size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type, u64 read_format)
1366{
1367 size_t sz, result = sizeof(struct perf_record_sample);
1368
1369 if (type & PERF_SAMPLE_IDENTIFIER)
1370 result += sizeof(u64);
1371
1372 if (type & PERF_SAMPLE_IP)
1373 result += sizeof(u64);
1374
1375 if (type & PERF_SAMPLE_TID)
1376 result += sizeof(u64);
1377
1378 if (type & PERF_SAMPLE_TIME)
1379 result += sizeof(u64);
1380
1381 if (type & PERF_SAMPLE_ADDR)
1382 result += sizeof(u64);
1383
1384 if (type & PERF_SAMPLE_ID)
1385 result += sizeof(u64);
1386
1387 if (type & PERF_SAMPLE_STREAM_ID)
1388 result += sizeof(u64);
1389
1390 if (type & PERF_SAMPLE_CPU)
1391 result += sizeof(u64);
1392
1393 if (type & PERF_SAMPLE_PERIOD)
1394 result += sizeof(u64);
1395
1396 if (type & PERF_SAMPLE_READ) {
1397 result += sizeof(u64);
1398 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1399 result += sizeof(u64);
1400 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1401 result += sizeof(u64);
1402 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1403 if (read_format & PERF_FORMAT_GROUP) {
1404 sz = sample->read.group.nr *
1405 sizeof(struct sample_read_value);
1406 result += sz;
1407 } else {
1408 result += sizeof(u64);
1409 }
1410 }
1411
1412 if (type & PERF_SAMPLE_CALLCHAIN) {
1413 sz = (sample->callchain->nr + 1) * sizeof(u64);
1414 result += sz;
1415 }
1416
1417 if (type & PERF_SAMPLE_RAW) {
1418 result += sizeof(u32);
1419 result += sample->raw_size;
1420 }
1421
1422 if (type & PERF_SAMPLE_BRANCH_STACK) {
1423 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
Kan Liang42bbabe2020-02-28 08:30:00 -08001424 /* nr, hw_idx */
1425 sz += 2 * sizeof(u64);
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001426 result += sz;
1427 }
1428
1429 if (type & PERF_SAMPLE_REGS_USER) {
1430 if (sample->user_regs.abi) {
1431 result += sizeof(u64);
1432 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
1433 result += sz;
1434 } else {
1435 result += sizeof(u64);
1436 }
1437 }
1438
1439 if (type & PERF_SAMPLE_STACK_USER) {
1440 sz = sample->user_stack.size;
1441 result += sizeof(u64);
1442 if (sz) {
1443 result += sz;
1444 result += sizeof(u64);
1445 }
1446 }
1447
1448 if (type & PERF_SAMPLE_WEIGHT)
1449 result += sizeof(u64);
1450
1451 if (type & PERF_SAMPLE_DATA_SRC)
1452 result += sizeof(u64);
1453
1454 if (type & PERF_SAMPLE_TRANSACTION)
1455 result += sizeof(u64);
1456
1457 if (type & PERF_SAMPLE_REGS_INTR) {
1458 if (sample->intr_regs.abi) {
1459 result += sizeof(u64);
1460 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
1461 result += sz;
1462 } else {
1463 result += sizeof(u64);
1464 }
1465 }
1466
1467 if (type & PERF_SAMPLE_PHYS_ADDR)
1468 result += sizeof(u64);
1469
Namhyung Kimba78c1c2020-03-25 21:45:30 +09001470 if (type & PERF_SAMPLE_CGROUP)
1471 result += sizeof(u64);
1472
Kan Liang542b88f2020-11-30 09:27:53 -08001473 if (type & PERF_SAMPLE_DATA_PAGE_SIZE)
1474 result += sizeof(u64);
1475
Kan Liangc1de7f32021-01-05 11:57:49 -08001476 if (type & PERF_SAMPLE_CODE_PAGE_SIZE)
1477 result += sizeof(u64);
1478
Adrian Hunter98dcf142019-11-15 14:42:11 +02001479 if (type & PERF_SAMPLE_AUX) {
1480 result += sizeof(u64);
1481 result += sample->aux_sample.size;
1482 }
1483
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001484 return result;
1485}
1486
1487int perf_event__synthesize_sample(union perf_event *event, u64 type, u64 read_format,
1488 const struct perf_sample *sample)
1489{
1490 __u64 *array;
1491 size_t sz;
1492 /*
1493 * used for cross-endian analysis. See git commit 65014ab3
1494 * for why this goofiness is needed.
1495 */
1496 union u64_swap u;
1497
1498 array = event->sample.array;
1499
1500 if (type & PERF_SAMPLE_IDENTIFIER) {
1501 *array = sample->id;
1502 array++;
1503 }
1504
1505 if (type & PERF_SAMPLE_IP) {
1506 *array = sample->ip;
1507 array++;
1508 }
1509
1510 if (type & PERF_SAMPLE_TID) {
1511 u.val32[0] = sample->pid;
1512 u.val32[1] = sample->tid;
1513 *array = u.val64;
1514 array++;
1515 }
1516
1517 if (type & PERF_SAMPLE_TIME) {
1518 *array = sample->time;
1519 array++;
1520 }
1521
1522 if (type & PERF_SAMPLE_ADDR) {
1523 *array = sample->addr;
1524 array++;
1525 }
1526
1527 if (type & PERF_SAMPLE_ID) {
1528 *array = sample->id;
1529 array++;
1530 }
1531
1532 if (type & PERF_SAMPLE_STREAM_ID) {
1533 *array = sample->stream_id;
1534 array++;
1535 }
1536
1537 if (type & PERF_SAMPLE_CPU) {
1538 u.val32[0] = sample->cpu;
1539 u.val32[1] = 0;
1540 *array = u.val64;
1541 array++;
1542 }
1543
1544 if (type & PERF_SAMPLE_PERIOD) {
1545 *array = sample->period;
1546 array++;
1547 }
1548
1549 if (type & PERF_SAMPLE_READ) {
1550 if (read_format & PERF_FORMAT_GROUP)
1551 *array = sample->read.group.nr;
1552 else
1553 *array = sample->read.one.value;
1554 array++;
1555
1556 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1557 *array = sample->read.time_enabled;
1558 array++;
1559 }
1560
1561 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1562 *array = sample->read.time_running;
1563 array++;
1564 }
1565
1566 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1567 if (read_format & PERF_FORMAT_GROUP) {
1568 sz = sample->read.group.nr *
1569 sizeof(struct sample_read_value);
1570 memcpy(array, sample->read.group.values, sz);
1571 array = (void *)array + sz;
1572 } else {
1573 *array = sample->read.one.id;
1574 array++;
1575 }
1576 }
1577
1578 if (type & PERF_SAMPLE_CALLCHAIN) {
1579 sz = (sample->callchain->nr + 1) * sizeof(u64);
1580 memcpy(array, sample->callchain, sz);
1581 array = (void *)array + sz;
1582 }
1583
1584 if (type & PERF_SAMPLE_RAW) {
1585 u.val32[0] = sample->raw_size;
1586 *array = u.val64;
1587 array = (void *)array + sizeof(u32);
1588
1589 memcpy(array, sample->raw_data, sample->raw_size);
1590 array = (void *)array + sample->raw_size;
1591 }
1592
1593 if (type & PERF_SAMPLE_BRANCH_STACK) {
1594 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
Kan Liang42bbabe2020-02-28 08:30:00 -08001595 /* nr, hw_idx */
1596 sz += 2 * sizeof(u64);
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001597 memcpy(array, sample->branch_stack, sz);
1598 array = (void *)array + sz;
1599 }
1600
1601 if (type & PERF_SAMPLE_REGS_USER) {
1602 if (sample->user_regs.abi) {
1603 *array++ = sample->user_regs.abi;
1604 sz = hweight64(sample->user_regs.mask) * sizeof(u64);
1605 memcpy(array, sample->user_regs.regs, sz);
1606 array = (void *)array + sz;
1607 } else {
1608 *array++ = 0;
1609 }
1610 }
1611
1612 if (type & PERF_SAMPLE_STACK_USER) {
1613 sz = sample->user_stack.size;
1614 *array++ = sz;
1615 if (sz) {
1616 memcpy(array, sample->user_stack.data, sz);
1617 array = (void *)array + sz;
1618 *array++ = sz;
1619 }
1620 }
1621
1622 if (type & PERF_SAMPLE_WEIGHT) {
1623 *array = sample->weight;
1624 array++;
1625 }
1626
1627 if (type & PERF_SAMPLE_DATA_SRC) {
1628 *array = sample->data_src;
1629 array++;
1630 }
1631
1632 if (type & PERF_SAMPLE_TRANSACTION) {
1633 *array = sample->transaction;
1634 array++;
1635 }
1636
1637 if (type & PERF_SAMPLE_REGS_INTR) {
1638 if (sample->intr_regs.abi) {
1639 *array++ = sample->intr_regs.abi;
1640 sz = hweight64(sample->intr_regs.mask) * sizeof(u64);
1641 memcpy(array, sample->intr_regs.regs, sz);
1642 array = (void *)array + sz;
1643 } else {
1644 *array++ = 0;
1645 }
1646 }
1647
1648 if (type & PERF_SAMPLE_PHYS_ADDR) {
1649 *array = sample->phys_addr;
1650 array++;
1651 }
1652
Namhyung Kimba78c1c2020-03-25 21:45:30 +09001653 if (type & PERF_SAMPLE_CGROUP) {
1654 *array = sample->cgroup;
1655 array++;
1656 }
1657
Kan Liang542b88f2020-11-30 09:27:53 -08001658 if (type & PERF_SAMPLE_DATA_PAGE_SIZE) {
1659 *array = sample->data_page_size;
1660 array++;
1661 }
1662
Kan Liangc1de7f32021-01-05 11:57:49 -08001663 if (type & PERF_SAMPLE_CODE_PAGE_SIZE) {
1664 *array = sample->code_page_size;
1665 array++;
1666 }
1667
Adrian Hunter98dcf142019-11-15 14:42:11 +02001668 if (type & PERF_SAMPLE_AUX) {
1669 sz = sample->aux_sample.size;
1670 *array++ = sz;
1671 memcpy(array, sample->aux_sample.data, sz);
1672 array = (void *)array + sz;
1673 }
1674
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001675 return 0;
1676}
1677
1678int perf_event__synthesize_id_index(struct perf_tool *tool, perf_event__handler_t process,
1679 struct evlist *evlist, struct machine *machine)
1680{
1681 union perf_event *ev;
1682 struct evsel *evsel;
1683 size_t nr = 0, i = 0, sz, max_nr, n;
1684 int err;
1685
1686 pr_debug2("Synthesizing id index\n");
1687
1688 max_nr = (UINT16_MAX - sizeof(struct perf_record_id_index)) /
1689 sizeof(struct id_index_entry);
1690
1691 evlist__for_each_entry(evlist, evsel)
Jiri Olsae7eb9002019-09-02 22:15:47 +02001692 nr += evsel->core.ids;
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001693
1694 n = nr > max_nr ? max_nr : nr;
1695 sz = sizeof(struct perf_record_id_index) + n * sizeof(struct id_index_entry);
1696 ev = zalloc(sz);
1697 if (!ev)
1698 return -ENOMEM;
1699
1700 ev->id_index.header.type = PERF_RECORD_ID_INDEX;
1701 ev->id_index.header.size = sz;
1702 ev->id_index.nr = n;
1703
1704 evlist__for_each_entry(evlist, evsel) {
1705 u32 j;
1706
Jiri Olsae7eb9002019-09-02 22:15:47 +02001707 for (j = 0; j < evsel->core.ids; j++) {
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001708 struct id_index_entry *e;
1709 struct perf_sample_id *sid;
1710
1711 if (i >= n) {
1712 err = process(tool, ev, NULL, machine);
1713 if (err)
1714 goto out_err;
1715 nr -= n;
1716 i = 0;
1717 }
1718
1719 e = &ev->id_index.entries[i++];
1720
Jiri Olsadeaf3212019-09-02 22:12:26 +02001721 e->id = evsel->core.id[j];
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001722
Arnaldo Carvalho de Melo3ccf8a72020-11-30 14:17:57 -03001723 sid = evlist__id2sid(evlist, e->id);
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001724 if (!sid) {
1725 free(ev);
1726 return -ENOENT;
1727 }
1728
1729 e->idx = sid->idx;
1730 e->cpu = sid->cpu;
1731 e->tid = sid->tid;
1732 }
1733 }
1734
1735 sz = sizeof(struct perf_record_id_index) + nr * sizeof(struct id_index_entry);
1736 ev->id_index.header.size = sz;
1737 ev->id_index.nr = nr;
1738
1739 err = process(tool, ev, NULL, machine);
1740out_err:
1741 free(ev);
1742
1743 return err;
1744}
1745
1746int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
1747 struct target *target, struct perf_thread_map *threads,
1748 perf_event__handler_t process, bool data_mmap,
1749 unsigned int nr_threads_synthesize)
1750{
1751 if (target__has_task(target))
1752 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
1753 else if (target__has_cpu(target))
1754 return perf_event__synthesize_threads(tool, process,
1755 machine, data_mmap,
1756 nr_threads_synthesize);
1757 /* command specified */
1758 return 0;
1759}
1760
1761int machine__synthesize_threads(struct machine *machine, struct target *target,
1762 struct perf_thread_map *threads, bool data_mmap,
1763 unsigned int nr_threads_synthesize)
1764{
1765 return __machine__synthesize_threads(machine, NULL, target, threads,
1766 perf_event__process, data_mmap,
1767 nr_threads_synthesize);
1768}
1769
1770static struct perf_record_event_update *event_update_event__new(size_t size, u64 type, u64 id)
1771{
1772 struct perf_record_event_update *ev;
1773
1774 size += sizeof(*ev);
1775 size = PERF_ALIGN(size, sizeof(u64));
1776
1777 ev = zalloc(size);
1778 if (ev) {
1779 ev->header.type = PERF_RECORD_EVENT_UPDATE;
1780 ev->header.size = (u16)size;
1781 ev->type = type;
1782 ev->id = id;
1783 }
1784 return ev;
1785}
1786
1787int perf_event__synthesize_event_update_unit(struct perf_tool *tool, struct evsel *evsel,
1788 perf_event__handler_t process)
1789{
1790 size_t size = strlen(evsel->unit);
1791 struct perf_record_event_update *ev;
1792 int err;
1793
Jiri Olsadeaf3212019-09-02 22:12:26 +02001794 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->core.id[0]);
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001795 if (ev == NULL)
1796 return -ENOMEM;
1797
1798 strlcpy(ev->data, evsel->unit, size + 1);
1799 err = process(tool, (union perf_event *)ev, NULL, NULL);
1800 free(ev);
1801 return err;
1802}
1803
1804int perf_event__synthesize_event_update_scale(struct perf_tool *tool, struct evsel *evsel,
1805 perf_event__handler_t process)
1806{
1807 struct perf_record_event_update *ev;
1808 struct perf_record_event_update_scale *ev_data;
1809 int err;
1810
Jiri Olsadeaf3212019-09-02 22:12:26 +02001811 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 -03001812 if (ev == NULL)
1813 return -ENOMEM;
1814
1815 ev_data = (struct perf_record_event_update_scale *)ev->data;
1816 ev_data->scale = evsel->scale;
1817 err = process(tool, (union perf_event *)ev, NULL, NULL);
1818 free(ev);
1819 return err;
1820}
1821
1822int perf_event__synthesize_event_update_name(struct perf_tool *tool, struct evsel *evsel,
1823 perf_event__handler_t process)
1824{
1825 struct perf_record_event_update *ev;
1826 size_t len = strlen(evsel->name);
1827 int err;
1828
Jiri Olsadeaf3212019-09-02 22:12:26 +02001829 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->core.id[0]);
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001830 if (ev == NULL)
1831 return -ENOMEM;
1832
1833 strlcpy(ev->data, evsel->name, len + 1);
1834 err = process(tool, (union perf_event *)ev, NULL, NULL);
1835 free(ev);
1836 return err;
1837}
1838
1839int perf_event__synthesize_event_update_cpus(struct perf_tool *tool, struct evsel *evsel,
1840 perf_event__handler_t process)
1841{
1842 size_t size = sizeof(struct perf_record_event_update);
1843 struct perf_record_event_update *ev;
1844 int max, err;
1845 u16 type;
1846
1847 if (!evsel->core.own_cpus)
1848 return 0;
1849
1850 ev = cpu_map_data__alloc(evsel->core.own_cpus, &size, &type, &max);
1851 if (!ev)
1852 return -ENOMEM;
1853
1854 ev->header.type = PERF_RECORD_EVENT_UPDATE;
1855 ev->header.size = (u16)size;
1856 ev->type = PERF_EVENT_UPDATE__CPUS;
Jiri Olsadeaf3212019-09-02 22:12:26 +02001857 ev->id = evsel->core.id[0];
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001858
1859 cpu_map_data__synthesize((struct perf_record_cpu_map_data *)ev->data,
1860 evsel->core.own_cpus, type, max);
1861
1862 err = process(tool, (union perf_event *)ev, NULL, NULL);
1863 free(ev);
1864 return err;
1865}
1866
1867int perf_event__synthesize_attrs(struct perf_tool *tool, struct evlist *evlist,
1868 perf_event__handler_t process)
1869{
1870 struct evsel *evsel;
1871 int err = 0;
1872
1873 evlist__for_each_entry(evlist, evsel) {
Jiri Olsae7eb9002019-09-02 22:15:47 +02001874 err = perf_event__synthesize_attr(tool, &evsel->core.attr, evsel->core.ids,
Jiri Olsadeaf3212019-09-02 22:12:26 +02001875 evsel->core.id, process);
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03001876 if (err) {
1877 pr_debug("failed to create perf header attribute\n");
1878 return err;
1879 }
1880 }
1881
1882 return err;
1883}
1884
1885static bool has_unit(struct evsel *evsel)
1886{
1887 return evsel->unit && *evsel->unit;
1888}
1889
1890static bool has_scale(struct evsel *evsel)
1891{
1892 return evsel->scale != 1;
1893}
1894
1895int perf_event__synthesize_extra_attr(struct perf_tool *tool, struct evlist *evsel_list,
1896 perf_event__handler_t process, bool is_pipe)
1897{
1898 struct evsel *evsel;
1899 int err;
1900
1901 /*
1902 * Synthesize other events stuff not carried within
1903 * attr event - unit, scale, name
1904 */
1905 evlist__for_each_entry(evsel_list, evsel) {
1906 if (!evsel->supported)
1907 continue;
1908
1909 /*
1910 * Synthesize unit and scale only if it's defined.
1911 */
1912 if (has_unit(evsel)) {
1913 err = perf_event__synthesize_event_update_unit(tool, evsel, process);
1914 if (err < 0) {
1915 pr_err("Couldn't synthesize evsel unit.\n");
1916 return err;
1917 }
1918 }
1919
1920 if (has_scale(evsel)) {
1921 err = perf_event__synthesize_event_update_scale(tool, evsel, process);
1922 if (err < 0) {
1923 pr_err("Couldn't synthesize evsel evsel.\n");
1924 return err;
1925 }
1926 }
1927
1928 if (evsel->core.own_cpus) {
1929 err = perf_event__synthesize_event_update_cpus(tool, evsel, process);
1930 if (err < 0) {
1931 pr_err("Couldn't synthesize evsel cpus.\n");
1932 return err;
1933 }
1934 }
1935
1936 /*
1937 * Name is needed only for pipe output,
1938 * perf.data carries event names.
1939 */
1940 if (is_pipe) {
1941 err = perf_event__synthesize_event_update_name(tool, evsel, process);
1942 if (err < 0) {
1943 pr_err("Couldn't synthesize evsel name.\n");
1944 return err;
1945 }
1946 }
1947 }
1948 return 0;
1949}
1950
1951int perf_event__synthesize_attr(struct perf_tool *tool, struct perf_event_attr *attr,
1952 u32 ids, u64 *id, perf_event__handler_t process)
1953{
1954 union perf_event *ev;
1955 size_t size;
1956 int err;
1957
1958 size = sizeof(struct perf_event_attr);
1959 size = PERF_ALIGN(size, sizeof(u64));
1960 size += sizeof(struct perf_event_header);
1961 size += ids * sizeof(u64);
1962
1963 ev = zalloc(size);
1964
1965 if (ev == NULL)
1966 return -ENOMEM;
1967
1968 ev->attr.attr = *attr;
1969 memcpy(ev->attr.id, id, ids * sizeof(u64));
1970
1971 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
1972 ev->attr.header.size = (u16)size;
1973
1974 if (ev->attr.header.size == size)
1975 err = process(tool, ev, NULL, NULL);
1976 else
1977 err = -E2BIG;
1978
1979 free(ev);
1980
1981 return err;
1982}
1983
1984int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, struct evlist *evlist,
1985 perf_event__handler_t process)
1986{
1987 union perf_event ev;
1988 struct tracing_data *tdata;
1989 ssize_t size = 0, aligned_size = 0, padding;
1990 struct feat_fd ff;
1991
1992 /*
1993 * We are going to store the size of the data followed
1994 * by the data contents. Since the fd descriptor is a pipe,
1995 * we cannot seek back to store the size of the data once
1996 * we know it. Instead we:
1997 *
1998 * - write the tracing data to the temp file
1999 * - get/write the data size to pipe
2000 * - write the tracing data from the temp file
2001 * to the pipe
2002 */
2003 tdata = tracing_data_get(&evlist->core.entries, fd, true);
2004 if (!tdata)
2005 return -1;
2006
2007 memset(&ev, 0, sizeof(ev));
2008
2009 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2010 size = tdata->size;
2011 aligned_size = PERF_ALIGN(size, sizeof(u64));
2012 padding = aligned_size - size;
2013 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2014 ev.tracing_data.size = aligned_size;
2015
2016 process(tool, &ev, NULL, NULL);
2017
2018 /*
2019 * The put function will copy all the tracing data
2020 * stored in temp file to the pipe.
2021 */
2022 tracing_data_put(tdata);
2023
2024 ff = (struct feat_fd){ .fd = fd };
2025 if (write_padded(&ff, NULL, 0, padding))
2026 return -1;
2027
2028 return aligned_size;
2029}
2030
2031int perf_event__synthesize_build_id(struct perf_tool *tool, struct dso *pos, u16 misc,
2032 perf_event__handler_t process, struct machine *machine)
2033{
2034 union perf_event ev;
2035 size_t len;
2036
2037 if (!pos->hit)
2038 return 0;
2039
2040 memset(&ev, 0, sizeof(ev));
2041
2042 len = pos->long_name_len + 1;
2043 len = PERF_ALIGN(len, NAME_ALIGN);
Jiri Olsa0aba7f02020-10-13 21:24:33 +02002044 memcpy(&ev.build_id.build_id, pos->bid.data, sizeof(pos->bid.data));
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03002045 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2046 ev.build_id.header.misc = misc;
2047 ev.build_id.pid = machine->pid;
2048 ev.build_id.header.size = sizeof(ev.build_id) + len;
2049 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2050
2051 return process(tool, &ev, NULL, machine);
2052}
2053
2054int perf_event__synthesize_stat_events(struct perf_stat_config *config, struct perf_tool *tool,
2055 struct evlist *evlist, perf_event__handler_t process, bool attrs)
2056{
2057 int err;
2058
2059 if (attrs) {
2060 err = perf_event__synthesize_attrs(tool, evlist, process);
2061 if (err < 0) {
2062 pr_err("Couldn't synthesize attrs.\n");
2063 return err;
2064 }
2065 }
2066
2067 err = perf_event__synthesize_extra_attr(tool, evlist, process, attrs);
2068 err = perf_event__synthesize_thread_map2(tool, evlist->core.threads, process, NULL);
2069 if (err < 0) {
2070 pr_err("Couldn't synthesize thread map.\n");
2071 return err;
2072 }
2073
2074 err = perf_event__synthesize_cpu_map(tool, evlist->core.cpus, process, NULL);
2075 if (err < 0) {
2076 pr_err("Couldn't synthesize thread map.\n");
2077 return err;
2078 }
2079
2080 err = perf_event__synthesize_stat_config(tool, config, process, NULL);
2081 if (err < 0) {
2082 pr_err("Couldn't synthesize config.\n");
2083 return err;
2084 }
2085
2086 return 0;
2087}
2088
Arnaldo Carvalho de Melo055c67e2019-09-18 16:08:52 -03002089extern const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
2090
2091int perf_event__synthesize_features(struct perf_tool *tool, struct perf_session *session,
2092 struct evlist *evlist, perf_event__handler_t process)
2093{
2094 struct perf_header *header = &session->header;
2095 struct perf_record_header_feature *fe;
2096 struct feat_fd ff;
2097 size_t sz, sz_hdr;
2098 int feat, ret;
2099
2100 sz_hdr = sizeof(fe->header);
2101 sz = sizeof(union perf_event);
2102 /* get a nice alignment */
2103 sz = PERF_ALIGN(sz, page_size);
2104
2105 memset(&ff, 0, sizeof(ff));
2106
2107 ff.buf = malloc(sz);
2108 if (!ff.buf)
2109 return -ENOMEM;
2110
2111 ff.size = sz - sz_hdr;
2112 ff.ph = &session->header;
2113
2114 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2115 if (!feat_ops[feat].synthesize) {
2116 pr_debug("No record header feature for header :%d\n", feat);
2117 continue;
2118 }
2119
2120 ff.offset = sizeof(*fe);
2121
2122 ret = feat_ops[feat].write(&ff, evlist);
2123 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
2124 pr_debug("Error writing feature\n");
2125 continue;
2126 }
2127 /* ff.buf may have changed due to realloc in do_write() */
2128 fe = ff.buf;
2129 memset(fe, 0, sizeof(*fe));
2130
2131 fe->feat_id = feat;
2132 fe->header.type = PERF_RECORD_HEADER_FEATURE;
2133 fe->header.size = ff.offset;
2134
2135 ret = process(tool, ff.buf, NULL, NULL);
2136 if (ret) {
2137 free(ff.buf);
2138 return ret;
2139 }
2140 }
2141
2142 /* Send HEADER_LAST_FEATURE mark. */
2143 fe = ff.buf;
2144 fe->feat_id = HEADER_LAST_FEATURE;
2145 fe->header.type = PERF_RECORD_HEADER_FEATURE;
2146 fe->header.size = sizeof(*fe);
2147
2148 ret = process(tool, ff.buf, NULL, NULL);
2149
2150 free(ff.buf);
2151 return ret;
2152}