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