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