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