xref: /linux/tools/perf/util/header.c (revision 3f0a50f345f78183f6e9b39c2f45ca5dcaa511ca)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <inttypes.h>
4 #include "string2.h"
5 #include <sys/param.h>
6 #include <sys/types.h>
7 #include <byteswap.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <linux/compiler.h>
12 #include <linux/list.h>
13 #include <linux/kernel.h>
14 #include <linux/bitops.h>
15 #include <linux/string.h>
16 #include <linux/stringify.h>
17 #include <linux/zalloc.h>
18 #include <sys/stat.h>
19 #include <sys/utsname.h>
20 #include <linux/time64.h>
21 #include <dirent.h>
22 #ifdef HAVE_LIBBPF_SUPPORT
23 #include <bpf/libbpf.h>
24 #endif
25 #include <perf/cpumap.h>
26 
27 #include "dso.h"
28 #include "evlist.h"
29 #include "evsel.h"
30 #include "util/evsel_fprintf.h"
31 #include "header.h"
32 #include "memswap.h"
33 #include "trace-event.h"
34 #include "session.h"
35 #include "symbol.h"
36 #include "debug.h"
37 #include "cpumap.h"
38 #include "pmu.h"
39 #include "vdso.h"
40 #include "strbuf.h"
41 #include "build-id.h"
42 #include "data.h"
43 #include <api/fs/fs.h>
44 #include "asm/bug.h"
45 #include "tool.h"
46 #include "time-utils.h"
47 #include "units.h"
48 #include "util/util.h" // perf_exe()
49 #include "cputopo.h"
50 #include "bpf-event.h"
51 #include "bpf-utils.h"
52 #include "clockid.h"
53 #include "pmu-hybrid.h"
54 
55 #include <linux/ctype.h>
56 #include <internal/lib.h>
57 
58 /*
59  * magic2 = "PERFILE2"
60  * must be a numerical value to let the endianness
61  * determine the memory layout. That way we are able
62  * to detect endianness when reading the perf.data file
63  * back.
64  *
65  * we check for legacy (PERFFILE) format.
66  */
67 static const char *__perf_magic1 = "PERFFILE";
68 static const u64 __perf_magic2    = 0x32454c4946524550ULL;
69 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
70 
71 #define PERF_MAGIC	__perf_magic2
72 
73 const char perf_version_string[] = PERF_VERSION;
74 
75 struct perf_file_attr {
76 	struct perf_event_attr	attr;
77 	struct perf_file_section	ids;
78 };
79 
80 void perf_header__set_feat(struct perf_header *header, int feat)
81 {
82 	set_bit(feat, header->adds_features);
83 }
84 
85 void perf_header__clear_feat(struct perf_header *header, int feat)
86 {
87 	clear_bit(feat, header->adds_features);
88 }
89 
90 bool perf_header__has_feat(const struct perf_header *header, int feat)
91 {
92 	return test_bit(feat, header->adds_features);
93 }
94 
95 static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
96 {
97 	ssize_t ret = writen(ff->fd, buf, size);
98 
99 	if (ret != (ssize_t)size)
100 		return ret < 0 ? (int)ret : -1;
101 	return 0;
102 }
103 
104 static int __do_write_buf(struct feat_fd *ff,  const void *buf, size_t size)
105 {
106 	/* struct perf_event_header::size is u16 */
107 	const size_t max_size = 0xffff - sizeof(struct perf_event_header);
108 	size_t new_size = ff->size;
109 	void *addr;
110 
111 	if (size + ff->offset > max_size)
112 		return -E2BIG;
113 
114 	while (size > (new_size - ff->offset))
115 		new_size <<= 1;
116 	new_size = min(max_size, new_size);
117 
118 	if (ff->size < new_size) {
119 		addr = realloc(ff->buf, new_size);
120 		if (!addr)
121 			return -ENOMEM;
122 		ff->buf = addr;
123 		ff->size = new_size;
124 	}
125 
126 	memcpy(ff->buf + ff->offset, buf, size);
127 	ff->offset += size;
128 
129 	return 0;
130 }
131 
132 /* Return: 0 if succeeded, -ERR if failed. */
133 int do_write(struct feat_fd *ff, const void *buf, size_t size)
134 {
135 	if (!ff->buf)
136 		return __do_write_fd(ff, buf, size);
137 	return __do_write_buf(ff, buf, size);
138 }
139 
140 /* Return: 0 if succeeded, -ERR if failed. */
141 static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
142 {
143 	u64 *p = (u64 *) set;
144 	int i, ret;
145 
146 	ret = do_write(ff, &size, sizeof(size));
147 	if (ret < 0)
148 		return ret;
149 
150 	for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
151 		ret = do_write(ff, p + i, sizeof(*p));
152 		if (ret < 0)
153 			return ret;
154 	}
155 
156 	return 0;
157 }
158 
159 /* Return: 0 if succeeded, -ERR if failed. */
160 int write_padded(struct feat_fd *ff, const void *bf,
161 		 size_t count, size_t count_aligned)
162 {
163 	static const char zero_buf[NAME_ALIGN];
164 	int err = do_write(ff, bf, count);
165 
166 	if (!err)
167 		err = do_write(ff, zero_buf, count_aligned - count);
168 
169 	return err;
170 }
171 
172 #define string_size(str)						\
173 	(PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
174 
175 /* Return: 0 if succeeded, -ERR if failed. */
176 static int do_write_string(struct feat_fd *ff, const char *str)
177 {
178 	u32 len, olen;
179 	int ret;
180 
181 	olen = strlen(str) + 1;
182 	len = PERF_ALIGN(olen, NAME_ALIGN);
183 
184 	/* write len, incl. \0 */
185 	ret = do_write(ff, &len, sizeof(len));
186 	if (ret < 0)
187 		return ret;
188 
189 	return write_padded(ff, str, olen, len);
190 }
191 
192 static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
193 {
194 	ssize_t ret = readn(ff->fd, addr, size);
195 
196 	if (ret != size)
197 		return ret < 0 ? (int)ret : -1;
198 	return 0;
199 }
200 
201 static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
202 {
203 	if (size > (ssize_t)ff->size - ff->offset)
204 		return -1;
205 
206 	memcpy(addr, ff->buf + ff->offset, size);
207 	ff->offset += size;
208 
209 	return 0;
210 
211 }
212 
213 static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
214 {
215 	if (!ff->buf)
216 		return __do_read_fd(ff, addr, size);
217 	return __do_read_buf(ff, addr, size);
218 }
219 
220 static int do_read_u32(struct feat_fd *ff, u32 *addr)
221 {
222 	int ret;
223 
224 	ret = __do_read(ff, addr, sizeof(*addr));
225 	if (ret)
226 		return ret;
227 
228 	if (ff->ph->needs_swap)
229 		*addr = bswap_32(*addr);
230 	return 0;
231 }
232 
233 static int do_read_u64(struct feat_fd *ff, u64 *addr)
234 {
235 	int ret;
236 
237 	ret = __do_read(ff, addr, sizeof(*addr));
238 	if (ret)
239 		return ret;
240 
241 	if (ff->ph->needs_swap)
242 		*addr = bswap_64(*addr);
243 	return 0;
244 }
245 
246 static char *do_read_string(struct feat_fd *ff)
247 {
248 	u32 len;
249 	char *buf;
250 
251 	if (do_read_u32(ff, &len))
252 		return NULL;
253 
254 	buf = malloc(len);
255 	if (!buf)
256 		return NULL;
257 
258 	if (!__do_read(ff, buf, len)) {
259 		/*
260 		 * strings are padded by zeroes
261 		 * thus the actual strlen of buf
262 		 * may be less than len
263 		 */
264 		return buf;
265 	}
266 
267 	free(buf);
268 	return NULL;
269 }
270 
271 /* Return: 0 if succeeded, -ERR if failed. */
272 static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
273 {
274 	unsigned long *set;
275 	u64 size, *p;
276 	int i, ret;
277 
278 	ret = do_read_u64(ff, &size);
279 	if (ret)
280 		return ret;
281 
282 	set = bitmap_zalloc(size);
283 	if (!set)
284 		return -ENOMEM;
285 
286 	p = (u64 *) set;
287 
288 	for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
289 		ret = do_read_u64(ff, p + i);
290 		if (ret < 0) {
291 			free(set);
292 			return ret;
293 		}
294 	}
295 
296 	*pset  = set;
297 	*psize = size;
298 	return 0;
299 }
300 
301 static int write_tracing_data(struct feat_fd *ff,
302 			      struct evlist *evlist)
303 {
304 	if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
305 		return -1;
306 
307 	return read_tracing_data(ff->fd, &evlist->core.entries);
308 }
309 
310 static int write_build_id(struct feat_fd *ff,
311 			  struct evlist *evlist __maybe_unused)
312 {
313 	struct perf_session *session;
314 	int err;
315 
316 	session = container_of(ff->ph, struct perf_session, header);
317 
318 	if (!perf_session__read_build_ids(session, true))
319 		return -1;
320 
321 	if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
322 		return -1;
323 
324 	err = perf_session__write_buildid_table(session, ff);
325 	if (err < 0) {
326 		pr_debug("failed to write buildid table\n");
327 		return err;
328 	}
329 	perf_session__cache_build_ids(session);
330 
331 	return 0;
332 }
333 
334 static int write_hostname(struct feat_fd *ff,
335 			  struct evlist *evlist __maybe_unused)
336 {
337 	struct utsname uts;
338 	int ret;
339 
340 	ret = uname(&uts);
341 	if (ret < 0)
342 		return -1;
343 
344 	return do_write_string(ff, uts.nodename);
345 }
346 
347 static int write_osrelease(struct feat_fd *ff,
348 			   struct evlist *evlist __maybe_unused)
349 {
350 	struct utsname uts;
351 	int ret;
352 
353 	ret = uname(&uts);
354 	if (ret < 0)
355 		return -1;
356 
357 	return do_write_string(ff, uts.release);
358 }
359 
360 static int write_arch(struct feat_fd *ff,
361 		      struct evlist *evlist __maybe_unused)
362 {
363 	struct utsname uts;
364 	int ret;
365 
366 	ret = uname(&uts);
367 	if (ret < 0)
368 		return -1;
369 
370 	return do_write_string(ff, uts.machine);
371 }
372 
373 static int write_version(struct feat_fd *ff,
374 			 struct evlist *evlist __maybe_unused)
375 {
376 	return do_write_string(ff, perf_version_string);
377 }
378 
379 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
380 {
381 	FILE *file;
382 	char *buf = NULL;
383 	char *s, *p;
384 	const char *search = cpuinfo_proc;
385 	size_t len = 0;
386 	int ret = -1;
387 
388 	if (!search)
389 		return -1;
390 
391 	file = fopen("/proc/cpuinfo", "r");
392 	if (!file)
393 		return -1;
394 
395 	while (getline(&buf, &len, file) > 0) {
396 		ret = strncmp(buf, search, strlen(search));
397 		if (!ret)
398 			break;
399 	}
400 
401 	if (ret) {
402 		ret = -1;
403 		goto done;
404 	}
405 
406 	s = buf;
407 
408 	p = strchr(buf, ':');
409 	if (p && *(p+1) == ' ' && *(p+2))
410 		s = p + 2;
411 	p = strchr(s, '\n');
412 	if (p)
413 		*p = '\0';
414 
415 	/* squash extra space characters (branding string) */
416 	p = s;
417 	while (*p) {
418 		if (isspace(*p)) {
419 			char *r = p + 1;
420 			char *q = skip_spaces(r);
421 			*p = ' ';
422 			if (q != (p+1))
423 				while ((*r++ = *q++));
424 		}
425 		p++;
426 	}
427 	ret = do_write_string(ff, s);
428 done:
429 	free(buf);
430 	fclose(file);
431 	return ret;
432 }
433 
434 static int write_cpudesc(struct feat_fd *ff,
435 		       struct evlist *evlist __maybe_unused)
436 {
437 #if defined(__powerpc__) || defined(__hppa__) || defined(__sparc__)
438 #define CPUINFO_PROC	{ "cpu", }
439 #elif defined(__s390__)
440 #define CPUINFO_PROC	{ "vendor_id", }
441 #elif defined(__sh__)
442 #define CPUINFO_PROC	{ "cpu type", }
443 #elif defined(__alpha__) || defined(__mips__)
444 #define CPUINFO_PROC	{ "cpu model", }
445 #elif defined(__arm__)
446 #define CPUINFO_PROC	{ "model name", "Processor", }
447 #elif defined(__arc__)
448 #define CPUINFO_PROC	{ "Processor", }
449 #elif defined(__xtensa__)
450 #define CPUINFO_PROC	{ "core ID", }
451 #else
452 #define CPUINFO_PROC	{ "model name", }
453 #endif
454 	const char *cpuinfo_procs[] = CPUINFO_PROC;
455 #undef CPUINFO_PROC
456 	unsigned int i;
457 
458 	for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
459 		int ret;
460 		ret = __write_cpudesc(ff, cpuinfo_procs[i]);
461 		if (ret >= 0)
462 			return ret;
463 	}
464 	return -1;
465 }
466 
467 
468 static int write_nrcpus(struct feat_fd *ff,
469 			struct evlist *evlist __maybe_unused)
470 {
471 	long nr;
472 	u32 nrc, nra;
473 	int ret;
474 
475 	nrc = cpu__max_present_cpu().cpu;
476 
477 	nr = sysconf(_SC_NPROCESSORS_ONLN);
478 	if (nr < 0)
479 		return -1;
480 
481 	nra = (u32)(nr & UINT_MAX);
482 
483 	ret = do_write(ff, &nrc, sizeof(nrc));
484 	if (ret < 0)
485 		return ret;
486 
487 	return do_write(ff, &nra, sizeof(nra));
488 }
489 
490 static int write_event_desc(struct feat_fd *ff,
491 			    struct evlist *evlist)
492 {
493 	struct evsel *evsel;
494 	u32 nre, nri, sz;
495 	int ret;
496 
497 	nre = evlist->core.nr_entries;
498 
499 	/*
500 	 * write number of events
501 	 */
502 	ret = do_write(ff, &nre, sizeof(nre));
503 	if (ret < 0)
504 		return ret;
505 
506 	/*
507 	 * size of perf_event_attr struct
508 	 */
509 	sz = (u32)sizeof(evsel->core.attr);
510 	ret = do_write(ff, &sz, sizeof(sz));
511 	if (ret < 0)
512 		return ret;
513 
514 	evlist__for_each_entry(evlist, evsel) {
515 		ret = do_write(ff, &evsel->core.attr, sz);
516 		if (ret < 0)
517 			return ret;
518 		/*
519 		 * write number of unique id per event
520 		 * there is one id per instance of an event
521 		 *
522 		 * copy into an nri to be independent of the
523 		 * type of ids,
524 		 */
525 		nri = evsel->core.ids;
526 		ret = do_write(ff, &nri, sizeof(nri));
527 		if (ret < 0)
528 			return ret;
529 
530 		/*
531 		 * write event string as passed on cmdline
532 		 */
533 		ret = do_write_string(ff, evsel__name(evsel));
534 		if (ret < 0)
535 			return ret;
536 		/*
537 		 * write unique ids for this event
538 		 */
539 		ret = do_write(ff, evsel->core.id, evsel->core.ids * sizeof(u64));
540 		if (ret < 0)
541 			return ret;
542 	}
543 	return 0;
544 }
545 
546 static int write_cmdline(struct feat_fd *ff,
547 			 struct evlist *evlist __maybe_unused)
548 {
549 	char pbuf[MAXPATHLEN], *buf;
550 	int i, ret, n;
551 
552 	/* actual path to perf binary */
553 	buf = perf_exe(pbuf, MAXPATHLEN);
554 
555 	/* account for binary path */
556 	n = perf_env.nr_cmdline + 1;
557 
558 	ret = do_write(ff, &n, sizeof(n));
559 	if (ret < 0)
560 		return ret;
561 
562 	ret = do_write_string(ff, buf);
563 	if (ret < 0)
564 		return ret;
565 
566 	for (i = 0 ; i < perf_env.nr_cmdline; i++) {
567 		ret = do_write_string(ff, perf_env.cmdline_argv[i]);
568 		if (ret < 0)
569 			return ret;
570 	}
571 	return 0;
572 }
573 
574 
575 static int write_cpu_topology(struct feat_fd *ff,
576 			      struct evlist *evlist __maybe_unused)
577 {
578 	struct cpu_topology *tp;
579 	u32 i;
580 	int ret, j;
581 
582 	tp = cpu_topology__new();
583 	if (!tp)
584 		return -1;
585 
586 	ret = do_write(ff, &tp->package_cpus_lists, sizeof(tp->package_cpus_lists));
587 	if (ret < 0)
588 		goto done;
589 
590 	for (i = 0; i < tp->package_cpus_lists; i++) {
591 		ret = do_write_string(ff, tp->package_cpus_list[i]);
592 		if (ret < 0)
593 			goto done;
594 	}
595 	ret = do_write(ff, &tp->core_cpus_lists, sizeof(tp->core_cpus_lists));
596 	if (ret < 0)
597 		goto done;
598 
599 	for (i = 0; i < tp->core_cpus_lists; i++) {
600 		ret = do_write_string(ff, tp->core_cpus_list[i]);
601 		if (ret < 0)
602 			break;
603 	}
604 
605 	ret = perf_env__read_cpu_topology_map(&perf_env);
606 	if (ret < 0)
607 		goto done;
608 
609 	for (j = 0; j < perf_env.nr_cpus_avail; j++) {
610 		ret = do_write(ff, &perf_env.cpu[j].core_id,
611 			       sizeof(perf_env.cpu[j].core_id));
612 		if (ret < 0)
613 			return ret;
614 		ret = do_write(ff, &perf_env.cpu[j].socket_id,
615 			       sizeof(perf_env.cpu[j].socket_id));
616 		if (ret < 0)
617 			return ret;
618 	}
619 
620 	if (!tp->die_cpus_lists)
621 		goto done;
622 
623 	ret = do_write(ff, &tp->die_cpus_lists, sizeof(tp->die_cpus_lists));
624 	if (ret < 0)
625 		goto done;
626 
627 	for (i = 0; i < tp->die_cpus_lists; i++) {
628 		ret = do_write_string(ff, tp->die_cpus_list[i]);
629 		if (ret < 0)
630 			goto done;
631 	}
632 
633 	for (j = 0; j < perf_env.nr_cpus_avail; j++) {
634 		ret = do_write(ff, &perf_env.cpu[j].die_id,
635 			       sizeof(perf_env.cpu[j].die_id));
636 		if (ret < 0)
637 			return ret;
638 	}
639 
640 done:
641 	cpu_topology__delete(tp);
642 	return ret;
643 }
644 
645 
646 
647 static int write_total_mem(struct feat_fd *ff,
648 			   struct evlist *evlist __maybe_unused)
649 {
650 	char *buf = NULL;
651 	FILE *fp;
652 	size_t len = 0;
653 	int ret = -1, n;
654 	uint64_t mem;
655 
656 	fp = fopen("/proc/meminfo", "r");
657 	if (!fp)
658 		return -1;
659 
660 	while (getline(&buf, &len, fp) > 0) {
661 		ret = strncmp(buf, "MemTotal:", 9);
662 		if (!ret)
663 			break;
664 	}
665 	if (!ret) {
666 		n = sscanf(buf, "%*s %"PRIu64, &mem);
667 		if (n == 1)
668 			ret = do_write(ff, &mem, sizeof(mem));
669 	} else
670 		ret = -1;
671 	free(buf);
672 	fclose(fp);
673 	return ret;
674 }
675 
676 static int write_numa_topology(struct feat_fd *ff,
677 			       struct evlist *evlist __maybe_unused)
678 {
679 	struct numa_topology *tp;
680 	int ret = -1;
681 	u32 i;
682 
683 	tp = numa_topology__new();
684 	if (!tp)
685 		return -ENOMEM;
686 
687 	ret = do_write(ff, &tp->nr, sizeof(u32));
688 	if (ret < 0)
689 		goto err;
690 
691 	for (i = 0; i < tp->nr; i++) {
692 		struct numa_topology_node *n = &tp->nodes[i];
693 
694 		ret = do_write(ff, &n->node, sizeof(u32));
695 		if (ret < 0)
696 			goto err;
697 
698 		ret = do_write(ff, &n->mem_total, sizeof(u64));
699 		if (ret)
700 			goto err;
701 
702 		ret = do_write(ff, &n->mem_free, sizeof(u64));
703 		if (ret)
704 			goto err;
705 
706 		ret = do_write_string(ff, n->cpus);
707 		if (ret < 0)
708 			goto err;
709 	}
710 
711 	ret = 0;
712 
713 err:
714 	numa_topology__delete(tp);
715 	return ret;
716 }
717 
718 /*
719  * File format:
720  *
721  * struct pmu_mappings {
722  *	u32	pmu_num;
723  *	struct pmu_map {
724  *		u32	type;
725  *		char	name[];
726  *	}[pmu_num];
727  * };
728  */
729 
730 static int write_pmu_mappings(struct feat_fd *ff,
731 			      struct evlist *evlist __maybe_unused)
732 {
733 	struct perf_pmu *pmu = NULL;
734 	u32 pmu_num = 0;
735 	int ret;
736 
737 	/*
738 	 * Do a first pass to count number of pmu to avoid lseek so this
739 	 * works in pipe mode as well.
740 	 */
741 	while ((pmu = perf_pmu__scan(pmu))) {
742 		if (!pmu->name)
743 			continue;
744 		pmu_num++;
745 	}
746 
747 	ret = do_write(ff, &pmu_num, sizeof(pmu_num));
748 	if (ret < 0)
749 		return ret;
750 
751 	while ((pmu = perf_pmu__scan(pmu))) {
752 		if (!pmu->name)
753 			continue;
754 
755 		ret = do_write(ff, &pmu->type, sizeof(pmu->type));
756 		if (ret < 0)
757 			return ret;
758 
759 		ret = do_write_string(ff, pmu->name);
760 		if (ret < 0)
761 			return ret;
762 	}
763 
764 	return 0;
765 }
766 
767 /*
768  * File format:
769  *
770  * struct group_descs {
771  *	u32	nr_groups;
772  *	struct group_desc {
773  *		char	name[];
774  *		u32	leader_idx;
775  *		u32	nr_members;
776  *	}[nr_groups];
777  * };
778  */
779 static int write_group_desc(struct feat_fd *ff,
780 			    struct evlist *evlist)
781 {
782 	u32 nr_groups = evlist->core.nr_groups;
783 	struct evsel *evsel;
784 	int ret;
785 
786 	ret = do_write(ff, &nr_groups, sizeof(nr_groups));
787 	if (ret < 0)
788 		return ret;
789 
790 	evlist__for_each_entry(evlist, evsel) {
791 		if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) {
792 			const char *name = evsel->group_name ?: "{anon_group}";
793 			u32 leader_idx = evsel->core.idx;
794 			u32 nr_members = evsel->core.nr_members;
795 
796 			ret = do_write_string(ff, name);
797 			if (ret < 0)
798 				return ret;
799 
800 			ret = do_write(ff, &leader_idx, sizeof(leader_idx));
801 			if (ret < 0)
802 				return ret;
803 
804 			ret = do_write(ff, &nr_members, sizeof(nr_members));
805 			if (ret < 0)
806 				return ret;
807 		}
808 	}
809 	return 0;
810 }
811 
812 /*
813  * Return the CPU id as a raw string.
814  *
815  * Each architecture should provide a more precise id string that
816  * can be use to match the architecture's "mapfile".
817  */
818 char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
819 {
820 	return NULL;
821 }
822 
823 /* Return zero when the cpuid from the mapfile.csv matches the
824  * cpuid string generated on this platform.
825  * Otherwise return non-zero.
826  */
827 int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
828 {
829 	regex_t re;
830 	regmatch_t pmatch[1];
831 	int match;
832 
833 	if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
834 		/* Warn unable to generate match particular string. */
835 		pr_info("Invalid regular expression %s\n", mapcpuid);
836 		return 1;
837 	}
838 
839 	match = !regexec(&re, cpuid, 1, pmatch, 0);
840 	regfree(&re);
841 	if (match) {
842 		size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
843 
844 		/* Verify the entire string matched. */
845 		if (match_len == strlen(cpuid))
846 			return 0;
847 	}
848 	return 1;
849 }
850 
851 /*
852  * default get_cpuid(): nothing gets recorded
853  * actual implementation must be in arch/$(SRCARCH)/util/header.c
854  */
855 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
856 {
857 	return ENOSYS; /* Not implemented */
858 }
859 
860 static int write_cpuid(struct feat_fd *ff,
861 		       struct evlist *evlist __maybe_unused)
862 {
863 	char buffer[64];
864 	int ret;
865 
866 	ret = get_cpuid(buffer, sizeof(buffer));
867 	if (ret)
868 		return -1;
869 
870 	return do_write_string(ff, buffer);
871 }
872 
873 static int write_branch_stack(struct feat_fd *ff __maybe_unused,
874 			      struct evlist *evlist __maybe_unused)
875 {
876 	return 0;
877 }
878 
879 static int write_auxtrace(struct feat_fd *ff,
880 			  struct evlist *evlist __maybe_unused)
881 {
882 	struct perf_session *session;
883 	int err;
884 
885 	if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
886 		return -1;
887 
888 	session = container_of(ff->ph, struct perf_session, header);
889 
890 	err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
891 	if (err < 0)
892 		pr_err("Failed to write auxtrace index\n");
893 	return err;
894 }
895 
896 static int write_clockid(struct feat_fd *ff,
897 			 struct evlist *evlist __maybe_unused)
898 {
899 	return do_write(ff, &ff->ph->env.clock.clockid_res_ns,
900 			sizeof(ff->ph->env.clock.clockid_res_ns));
901 }
902 
903 static int write_clock_data(struct feat_fd *ff,
904 			    struct evlist *evlist __maybe_unused)
905 {
906 	u64 *data64;
907 	u32 data32;
908 	int ret;
909 
910 	/* version */
911 	data32 = 1;
912 
913 	ret = do_write(ff, &data32, sizeof(data32));
914 	if (ret < 0)
915 		return ret;
916 
917 	/* clockid */
918 	data32 = ff->ph->env.clock.clockid;
919 
920 	ret = do_write(ff, &data32, sizeof(data32));
921 	if (ret < 0)
922 		return ret;
923 
924 	/* TOD ref time */
925 	data64 = &ff->ph->env.clock.tod_ns;
926 
927 	ret = do_write(ff, data64, sizeof(*data64));
928 	if (ret < 0)
929 		return ret;
930 
931 	/* clockid ref time */
932 	data64 = &ff->ph->env.clock.clockid_ns;
933 
934 	return do_write(ff, data64, sizeof(*data64));
935 }
936 
937 static int write_hybrid_topology(struct feat_fd *ff,
938 				 struct evlist *evlist __maybe_unused)
939 {
940 	struct hybrid_topology *tp;
941 	int ret;
942 	u32 i;
943 
944 	tp = hybrid_topology__new();
945 	if (!tp)
946 		return -ENOENT;
947 
948 	ret = do_write(ff, &tp->nr, sizeof(u32));
949 	if (ret < 0)
950 		goto err;
951 
952 	for (i = 0; i < tp->nr; i++) {
953 		struct hybrid_topology_node *n = &tp->nodes[i];
954 
955 		ret = do_write_string(ff, n->pmu_name);
956 		if (ret < 0)
957 			goto err;
958 
959 		ret = do_write_string(ff, n->cpus);
960 		if (ret < 0)
961 			goto err;
962 	}
963 
964 	ret = 0;
965 
966 err:
967 	hybrid_topology__delete(tp);
968 	return ret;
969 }
970 
971 static int write_dir_format(struct feat_fd *ff,
972 			    struct evlist *evlist __maybe_unused)
973 {
974 	struct perf_session *session;
975 	struct perf_data *data;
976 
977 	session = container_of(ff->ph, struct perf_session, header);
978 	data = session->data;
979 
980 	if (WARN_ON(!perf_data__is_dir(data)))
981 		return -1;
982 
983 	return do_write(ff, &data->dir.version, sizeof(data->dir.version));
984 }
985 
986 #ifdef HAVE_LIBBPF_SUPPORT
987 static int write_bpf_prog_info(struct feat_fd *ff,
988 			       struct evlist *evlist __maybe_unused)
989 {
990 	struct perf_env *env = &ff->ph->env;
991 	struct rb_root *root;
992 	struct rb_node *next;
993 	int ret;
994 
995 	down_read(&env->bpf_progs.lock);
996 
997 	ret = do_write(ff, &env->bpf_progs.infos_cnt,
998 		       sizeof(env->bpf_progs.infos_cnt));
999 	if (ret < 0)
1000 		goto out;
1001 
1002 	root = &env->bpf_progs.infos;
1003 	next = rb_first(root);
1004 	while (next) {
1005 		struct bpf_prog_info_node *node;
1006 		size_t len;
1007 
1008 		node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1009 		next = rb_next(&node->rb_node);
1010 		len = sizeof(struct perf_bpil) +
1011 			node->info_linear->data_len;
1012 
1013 		/* before writing to file, translate address to offset */
1014 		bpil_addr_to_offs(node->info_linear);
1015 		ret = do_write(ff, node->info_linear, len);
1016 		/*
1017 		 * translate back to address even when do_write() fails,
1018 		 * so that this function never changes the data.
1019 		 */
1020 		bpil_offs_to_addr(node->info_linear);
1021 		if (ret < 0)
1022 			goto out;
1023 	}
1024 out:
1025 	up_read(&env->bpf_progs.lock);
1026 	return ret;
1027 }
1028 
1029 static int write_bpf_btf(struct feat_fd *ff,
1030 			 struct evlist *evlist __maybe_unused)
1031 {
1032 	struct perf_env *env = &ff->ph->env;
1033 	struct rb_root *root;
1034 	struct rb_node *next;
1035 	int ret;
1036 
1037 	down_read(&env->bpf_progs.lock);
1038 
1039 	ret = do_write(ff, &env->bpf_progs.btfs_cnt,
1040 		       sizeof(env->bpf_progs.btfs_cnt));
1041 
1042 	if (ret < 0)
1043 		goto out;
1044 
1045 	root = &env->bpf_progs.btfs;
1046 	next = rb_first(root);
1047 	while (next) {
1048 		struct btf_node *node;
1049 
1050 		node = rb_entry(next, struct btf_node, rb_node);
1051 		next = rb_next(&node->rb_node);
1052 		ret = do_write(ff, &node->id,
1053 			       sizeof(u32) * 2 + node->data_size);
1054 		if (ret < 0)
1055 			goto out;
1056 	}
1057 out:
1058 	up_read(&env->bpf_progs.lock);
1059 	return ret;
1060 }
1061 #endif // HAVE_LIBBPF_SUPPORT
1062 
1063 static int cpu_cache_level__sort(const void *a, const void *b)
1064 {
1065 	struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
1066 	struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
1067 
1068 	return cache_a->level - cache_b->level;
1069 }
1070 
1071 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
1072 {
1073 	if (a->level != b->level)
1074 		return false;
1075 
1076 	if (a->line_size != b->line_size)
1077 		return false;
1078 
1079 	if (a->sets != b->sets)
1080 		return false;
1081 
1082 	if (a->ways != b->ways)
1083 		return false;
1084 
1085 	if (strcmp(a->type, b->type))
1086 		return false;
1087 
1088 	if (strcmp(a->size, b->size))
1089 		return false;
1090 
1091 	if (strcmp(a->map, b->map))
1092 		return false;
1093 
1094 	return true;
1095 }
1096 
1097 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1098 {
1099 	char path[PATH_MAX], file[PATH_MAX];
1100 	struct stat st;
1101 	size_t len;
1102 
1103 	scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1104 	scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1105 
1106 	if (stat(file, &st))
1107 		return 1;
1108 
1109 	scnprintf(file, PATH_MAX, "%s/level", path);
1110 	if (sysfs__read_int(file, (int *) &cache->level))
1111 		return -1;
1112 
1113 	scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1114 	if (sysfs__read_int(file, (int *) &cache->line_size))
1115 		return -1;
1116 
1117 	scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1118 	if (sysfs__read_int(file, (int *) &cache->sets))
1119 		return -1;
1120 
1121 	scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1122 	if (sysfs__read_int(file, (int *) &cache->ways))
1123 		return -1;
1124 
1125 	scnprintf(file, PATH_MAX, "%s/type", path);
1126 	if (sysfs__read_str(file, &cache->type, &len))
1127 		return -1;
1128 
1129 	cache->type[len] = 0;
1130 	cache->type = strim(cache->type);
1131 
1132 	scnprintf(file, PATH_MAX, "%s/size", path);
1133 	if (sysfs__read_str(file, &cache->size, &len)) {
1134 		zfree(&cache->type);
1135 		return -1;
1136 	}
1137 
1138 	cache->size[len] = 0;
1139 	cache->size = strim(cache->size);
1140 
1141 	scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1142 	if (sysfs__read_str(file, &cache->map, &len)) {
1143 		zfree(&cache->size);
1144 		zfree(&cache->type);
1145 		return -1;
1146 	}
1147 
1148 	cache->map[len] = 0;
1149 	cache->map = strim(cache->map);
1150 	return 0;
1151 }
1152 
1153 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1154 {
1155 	fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1156 }
1157 
1158 #define MAX_CACHE_LVL 4
1159 
1160 static int build_caches(struct cpu_cache_level caches[], u32 *cntp)
1161 {
1162 	u32 i, cnt = 0;
1163 	u32 nr, cpu;
1164 	u16 level;
1165 
1166 	nr = cpu__max_cpu().cpu;
1167 
1168 	for (cpu = 0; cpu < nr; cpu++) {
1169 		for (level = 0; level < MAX_CACHE_LVL; level++) {
1170 			struct cpu_cache_level c;
1171 			int err;
1172 
1173 			err = cpu_cache_level__read(&c, cpu, level);
1174 			if (err < 0)
1175 				return err;
1176 
1177 			if (err == 1)
1178 				break;
1179 
1180 			for (i = 0; i < cnt; i++) {
1181 				if (cpu_cache_level__cmp(&c, &caches[i]))
1182 					break;
1183 			}
1184 
1185 			if (i == cnt)
1186 				caches[cnt++] = c;
1187 			else
1188 				cpu_cache_level__free(&c);
1189 		}
1190 	}
1191 	*cntp = cnt;
1192 	return 0;
1193 }
1194 
1195 static int write_cache(struct feat_fd *ff,
1196 		       struct evlist *evlist __maybe_unused)
1197 {
1198 	u32 max_caches = cpu__max_cpu().cpu * MAX_CACHE_LVL;
1199 	struct cpu_cache_level caches[max_caches];
1200 	u32 cnt = 0, i, version = 1;
1201 	int ret;
1202 
1203 	ret = build_caches(caches, &cnt);
1204 	if (ret)
1205 		goto out;
1206 
1207 	qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1208 
1209 	ret = do_write(ff, &version, sizeof(u32));
1210 	if (ret < 0)
1211 		goto out;
1212 
1213 	ret = do_write(ff, &cnt, sizeof(u32));
1214 	if (ret < 0)
1215 		goto out;
1216 
1217 	for (i = 0; i < cnt; i++) {
1218 		struct cpu_cache_level *c = &caches[i];
1219 
1220 		#define _W(v)					\
1221 			ret = do_write(ff, &c->v, sizeof(u32));	\
1222 			if (ret < 0)				\
1223 				goto out;
1224 
1225 		_W(level)
1226 		_W(line_size)
1227 		_W(sets)
1228 		_W(ways)
1229 		#undef _W
1230 
1231 		#define _W(v)						\
1232 			ret = do_write_string(ff, (const char *) c->v);	\
1233 			if (ret < 0)					\
1234 				goto out;
1235 
1236 		_W(type)
1237 		_W(size)
1238 		_W(map)
1239 		#undef _W
1240 	}
1241 
1242 out:
1243 	for (i = 0; i < cnt; i++)
1244 		cpu_cache_level__free(&caches[i]);
1245 	return ret;
1246 }
1247 
1248 static int write_stat(struct feat_fd *ff __maybe_unused,
1249 		      struct evlist *evlist __maybe_unused)
1250 {
1251 	return 0;
1252 }
1253 
1254 static int write_sample_time(struct feat_fd *ff,
1255 			     struct evlist *evlist)
1256 {
1257 	int ret;
1258 
1259 	ret = do_write(ff, &evlist->first_sample_time,
1260 		       sizeof(evlist->first_sample_time));
1261 	if (ret < 0)
1262 		return ret;
1263 
1264 	return do_write(ff, &evlist->last_sample_time,
1265 			sizeof(evlist->last_sample_time));
1266 }
1267 
1268 
1269 static int memory_node__read(struct memory_node *n, unsigned long idx)
1270 {
1271 	unsigned int phys, size = 0;
1272 	char path[PATH_MAX];
1273 	struct dirent *ent;
1274 	DIR *dir;
1275 
1276 #define for_each_memory(mem, dir)					\
1277 	while ((ent = readdir(dir)))					\
1278 		if (strcmp(ent->d_name, ".") &&				\
1279 		    strcmp(ent->d_name, "..") &&			\
1280 		    sscanf(ent->d_name, "memory%u", &mem) == 1)
1281 
1282 	scnprintf(path, PATH_MAX,
1283 		  "%s/devices/system/node/node%lu",
1284 		  sysfs__mountpoint(), idx);
1285 
1286 	dir = opendir(path);
1287 	if (!dir) {
1288 		pr_warning("failed: can't open memory sysfs data\n");
1289 		return -1;
1290 	}
1291 
1292 	for_each_memory(phys, dir) {
1293 		size = max(phys, size);
1294 	}
1295 
1296 	size++;
1297 
1298 	n->set = bitmap_zalloc(size);
1299 	if (!n->set) {
1300 		closedir(dir);
1301 		return -ENOMEM;
1302 	}
1303 
1304 	n->node = idx;
1305 	n->size = size;
1306 
1307 	rewinddir(dir);
1308 
1309 	for_each_memory(phys, dir) {
1310 		set_bit(phys, n->set);
1311 	}
1312 
1313 	closedir(dir);
1314 	return 0;
1315 }
1316 
1317 static int memory_node__sort(const void *a, const void *b)
1318 {
1319 	const struct memory_node *na = a;
1320 	const struct memory_node *nb = b;
1321 
1322 	return na->node - nb->node;
1323 }
1324 
1325 static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
1326 {
1327 	char path[PATH_MAX];
1328 	struct dirent *ent;
1329 	DIR *dir;
1330 	u64 cnt = 0;
1331 	int ret = 0;
1332 
1333 	scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1334 		  sysfs__mountpoint());
1335 
1336 	dir = opendir(path);
1337 	if (!dir) {
1338 		pr_debug2("%s: couldn't read %s, does this arch have topology information?\n",
1339 			  __func__, path);
1340 		return -1;
1341 	}
1342 
1343 	while (!ret && (ent = readdir(dir))) {
1344 		unsigned int idx;
1345 		int r;
1346 
1347 		if (!strcmp(ent->d_name, ".") ||
1348 		    !strcmp(ent->d_name, ".."))
1349 			continue;
1350 
1351 		r = sscanf(ent->d_name, "node%u", &idx);
1352 		if (r != 1)
1353 			continue;
1354 
1355 		if (WARN_ONCE(cnt >= size,
1356 			"failed to write MEM_TOPOLOGY, way too many nodes\n")) {
1357 			closedir(dir);
1358 			return -1;
1359 		}
1360 
1361 		ret = memory_node__read(&nodes[cnt++], idx);
1362 	}
1363 
1364 	*cntp = cnt;
1365 	closedir(dir);
1366 
1367 	if (!ret)
1368 		qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1369 
1370 	return ret;
1371 }
1372 
1373 #define MAX_MEMORY_NODES 2000
1374 
1375 /*
1376  * The MEM_TOPOLOGY holds physical memory map for every
1377  * node in system. The format of data is as follows:
1378  *
1379  *  0 - version          | for future changes
1380  *  8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1381  * 16 - count            | number of nodes
1382  *
1383  * For each node we store map of physical indexes for
1384  * each node:
1385  *
1386  * 32 - node id          | node index
1387  * 40 - size             | size of bitmap
1388  * 48 - bitmap           | bitmap of memory indexes that belongs to node
1389  */
1390 static int write_mem_topology(struct feat_fd *ff __maybe_unused,
1391 			      struct evlist *evlist __maybe_unused)
1392 {
1393 	static struct memory_node nodes[MAX_MEMORY_NODES];
1394 	u64 bsize, version = 1, i, nr;
1395 	int ret;
1396 
1397 	ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1398 			      (unsigned long long *) &bsize);
1399 	if (ret)
1400 		return ret;
1401 
1402 	ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
1403 	if (ret)
1404 		return ret;
1405 
1406 	ret = do_write(ff, &version, sizeof(version));
1407 	if (ret < 0)
1408 		goto out;
1409 
1410 	ret = do_write(ff, &bsize, sizeof(bsize));
1411 	if (ret < 0)
1412 		goto out;
1413 
1414 	ret = do_write(ff, &nr, sizeof(nr));
1415 	if (ret < 0)
1416 		goto out;
1417 
1418 	for (i = 0; i < nr; i++) {
1419 		struct memory_node *n = &nodes[i];
1420 
1421 		#define _W(v)						\
1422 			ret = do_write(ff, &n->v, sizeof(n->v));	\
1423 			if (ret < 0)					\
1424 				goto out;
1425 
1426 		_W(node)
1427 		_W(size)
1428 
1429 		#undef _W
1430 
1431 		ret = do_write_bitmap(ff, n->set, n->size);
1432 		if (ret < 0)
1433 			goto out;
1434 	}
1435 
1436 out:
1437 	return ret;
1438 }
1439 
1440 static int write_compressed(struct feat_fd *ff __maybe_unused,
1441 			    struct evlist *evlist __maybe_unused)
1442 {
1443 	int ret;
1444 
1445 	ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver));
1446 	if (ret)
1447 		return ret;
1448 
1449 	ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type));
1450 	if (ret)
1451 		return ret;
1452 
1453 	ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level));
1454 	if (ret)
1455 		return ret;
1456 
1457 	ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio));
1458 	if (ret)
1459 		return ret;
1460 
1461 	return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len));
1462 }
1463 
1464 static int write_per_cpu_pmu_caps(struct feat_fd *ff, struct perf_pmu *pmu,
1465 				  bool write_pmu)
1466 {
1467 	struct perf_pmu_caps *caps = NULL;
1468 	int nr_caps;
1469 	int ret;
1470 
1471 	nr_caps = perf_pmu__caps_parse(pmu);
1472 	if (nr_caps < 0)
1473 		return nr_caps;
1474 
1475 	ret = do_write(ff, &nr_caps, sizeof(nr_caps));
1476 	if (ret < 0)
1477 		return ret;
1478 
1479 	list_for_each_entry(caps, &pmu->caps, list) {
1480 		ret = do_write_string(ff, caps->name);
1481 		if (ret < 0)
1482 			return ret;
1483 
1484 		ret = do_write_string(ff, caps->value);
1485 		if (ret < 0)
1486 			return ret;
1487 	}
1488 
1489 	if (write_pmu) {
1490 		ret = do_write_string(ff, pmu->name);
1491 		if (ret < 0)
1492 			return ret;
1493 	}
1494 
1495 	return ret;
1496 }
1497 
1498 static int write_cpu_pmu_caps(struct feat_fd *ff,
1499 			      struct evlist *evlist __maybe_unused)
1500 {
1501 	struct perf_pmu *cpu_pmu = perf_pmu__find("cpu");
1502 
1503 	if (!cpu_pmu)
1504 		return -ENOENT;
1505 
1506 	return write_per_cpu_pmu_caps(ff, cpu_pmu, false);
1507 }
1508 
1509 static int write_hybrid_cpu_pmu_caps(struct feat_fd *ff,
1510 				     struct evlist *evlist __maybe_unused)
1511 {
1512 	struct perf_pmu *pmu;
1513 	u32 nr_pmu = perf_pmu__hybrid_pmu_num();
1514 	int ret;
1515 
1516 	if (nr_pmu == 0)
1517 		return -ENOENT;
1518 
1519 	ret = do_write(ff, &nr_pmu, sizeof(nr_pmu));
1520 	if (ret < 0)
1521 		return ret;
1522 
1523 	perf_pmu__for_each_hybrid_pmu(pmu) {
1524 		ret = write_per_cpu_pmu_caps(ff, pmu, true);
1525 		if (ret < 0)
1526 			return ret;
1527 	}
1528 
1529 	return 0;
1530 }
1531 
1532 static void print_hostname(struct feat_fd *ff, FILE *fp)
1533 {
1534 	fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
1535 }
1536 
1537 static void print_osrelease(struct feat_fd *ff, FILE *fp)
1538 {
1539 	fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
1540 }
1541 
1542 static void print_arch(struct feat_fd *ff, FILE *fp)
1543 {
1544 	fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
1545 }
1546 
1547 static void print_cpudesc(struct feat_fd *ff, FILE *fp)
1548 {
1549 	fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
1550 }
1551 
1552 static void print_nrcpus(struct feat_fd *ff, FILE *fp)
1553 {
1554 	fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1555 	fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
1556 }
1557 
1558 static void print_version(struct feat_fd *ff, FILE *fp)
1559 {
1560 	fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
1561 }
1562 
1563 static void print_cmdline(struct feat_fd *ff, FILE *fp)
1564 {
1565 	int nr, i;
1566 
1567 	nr = ff->ph->env.nr_cmdline;
1568 
1569 	fprintf(fp, "# cmdline : ");
1570 
1571 	for (i = 0; i < nr; i++) {
1572 		char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
1573 		if (!argv_i) {
1574 			fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1575 		} else {
1576 			char *mem = argv_i;
1577 			do {
1578 				char *quote = strchr(argv_i, '\'');
1579 				if (!quote)
1580 					break;
1581 				*quote++ = '\0';
1582 				fprintf(fp, "%s\\\'", argv_i);
1583 				argv_i = quote;
1584 			} while (1);
1585 			fprintf(fp, "%s ", argv_i);
1586 			free(mem);
1587 		}
1588 	}
1589 	fputc('\n', fp);
1590 }
1591 
1592 static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
1593 {
1594 	struct perf_header *ph = ff->ph;
1595 	int cpu_nr = ph->env.nr_cpus_avail;
1596 	int nr, i;
1597 	char *str;
1598 
1599 	nr = ph->env.nr_sibling_cores;
1600 	str = ph->env.sibling_cores;
1601 
1602 	for (i = 0; i < nr; i++) {
1603 		fprintf(fp, "# sibling sockets : %s\n", str);
1604 		str += strlen(str) + 1;
1605 	}
1606 
1607 	if (ph->env.nr_sibling_dies) {
1608 		nr = ph->env.nr_sibling_dies;
1609 		str = ph->env.sibling_dies;
1610 
1611 		for (i = 0; i < nr; i++) {
1612 			fprintf(fp, "# sibling dies    : %s\n", str);
1613 			str += strlen(str) + 1;
1614 		}
1615 	}
1616 
1617 	nr = ph->env.nr_sibling_threads;
1618 	str = ph->env.sibling_threads;
1619 
1620 	for (i = 0; i < nr; i++) {
1621 		fprintf(fp, "# sibling threads : %s\n", str);
1622 		str += strlen(str) + 1;
1623 	}
1624 
1625 	if (ph->env.nr_sibling_dies) {
1626 		if (ph->env.cpu != NULL) {
1627 			for (i = 0; i < cpu_nr; i++)
1628 				fprintf(fp, "# CPU %d: Core ID %d, "
1629 					    "Die ID %d, Socket ID %d\n",
1630 					    i, ph->env.cpu[i].core_id,
1631 					    ph->env.cpu[i].die_id,
1632 					    ph->env.cpu[i].socket_id);
1633 		} else
1634 			fprintf(fp, "# Core ID, Die ID and Socket ID "
1635 				    "information is not available\n");
1636 	} else {
1637 		if (ph->env.cpu != NULL) {
1638 			for (i = 0; i < cpu_nr; i++)
1639 				fprintf(fp, "# CPU %d: Core ID %d, "
1640 					    "Socket ID %d\n",
1641 					    i, ph->env.cpu[i].core_id,
1642 					    ph->env.cpu[i].socket_id);
1643 		} else
1644 			fprintf(fp, "# Core ID and Socket ID "
1645 				    "information is not available\n");
1646 	}
1647 }
1648 
1649 static void print_clockid(struct feat_fd *ff, FILE *fp)
1650 {
1651 	fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n",
1652 		ff->ph->env.clock.clockid_res_ns * 1000);
1653 }
1654 
1655 static void print_clock_data(struct feat_fd *ff, FILE *fp)
1656 {
1657 	struct timespec clockid_ns;
1658 	char tstr[64], date[64];
1659 	struct timeval tod_ns;
1660 	clockid_t clockid;
1661 	struct tm ltime;
1662 	u64 ref;
1663 
1664 	if (!ff->ph->env.clock.enabled) {
1665 		fprintf(fp, "# reference time disabled\n");
1666 		return;
1667 	}
1668 
1669 	/* Compute TOD time. */
1670 	ref = ff->ph->env.clock.tod_ns;
1671 	tod_ns.tv_sec = ref / NSEC_PER_SEC;
1672 	ref -= tod_ns.tv_sec * NSEC_PER_SEC;
1673 	tod_ns.tv_usec = ref / NSEC_PER_USEC;
1674 
1675 	/* Compute clockid time. */
1676 	ref = ff->ph->env.clock.clockid_ns;
1677 	clockid_ns.tv_sec = ref / NSEC_PER_SEC;
1678 	ref -= clockid_ns.tv_sec * NSEC_PER_SEC;
1679 	clockid_ns.tv_nsec = ref;
1680 
1681 	clockid = ff->ph->env.clock.clockid;
1682 
1683 	if (localtime_r(&tod_ns.tv_sec, &ltime) == NULL)
1684 		snprintf(tstr, sizeof(tstr), "<error>");
1685 	else {
1686 		strftime(date, sizeof(date), "%F %T", &ltime);
1687 		scnprintf(tstr, sizeof(tstr), "%s.%06d",
1688 			  date, (int) tod_ns.tv_usec);
1689 	}
1690 
1691 	fprintf(fp, "# clockid: %s (%u)\n", clockid_name(clockid), clockid);
1692 	fprintf(fp, "# reference time: %s = %ld.%06d (TOD) = %ld.%09ld (%s)\n",
1693 		    tstr, (long) tod_ns.tv_sec, (int) tod_ns.tv_usec,
1694 		    (long) clockid_ns.tv_sec, clockid_ns.tv_nsec,
1695 		    clockid_name(clockid));
1696 }
1697 
1698 static void print_hybrid_topology(struct feat_fd *ff, FILE *fp)
1699 {
1700 	int i;
1701 	struct hybrid_node *n;
1702 
1703 	fprintf(fp, "# hybrid cpu system:\n");
1704 	for (i = 0; i < ff->ph->env.nr_hybrid_nodes; i++) {
1705 		n = &ff->ph->env.hybrid_nodes[i];
1706 		fprintf(fp, "# %s cpu list : %s\n", n->pmu_name, n->cpus);
1707 	}
1708 }
1709 
1710 static void print_dir_format(struct feat_fd *ff, FILE *fp)
1711 {
1712 	struct perf_session *session;
1713 	struct perf_data *data;
1714 
1715 	session = container_of(ff->ph, struct perf_session, header);
1716 	data = session->data;
1717 
1718 	fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version);
1719 }
1720 
1721 #ifdef HAVE_LIBBPF_SUPPORT
1722 static void print_bpf_prog_info(struct feat_fd *ff, FILE *fp)
1723 {
1724 	struct perf_env *env = &ff->ph->env;
1725 	struct rb_root *root;
1726 	struct rb_node *next;
1727 
1728 	down_read(&env->bpf_progs.lock);
1729 
1730 	root = &env->bpf_progs.infos;
1731 	next = rb_first(root);
1732 
1733 	while (next) {
1734 		struct bpf_prog_info_node *node;
1735 
1736 		node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1737 		next = rb_next(&node->rb_node);
1738 
1739 		bpf_event__print_bpf_prog_info(&node->info_linear->info,
1740 					       env, fp);
1741 	}
1742 
1743 	up_read(&env->bpf_progs.lock);
1744 }
1745 
1746 static void print_bpf_btf(struct feat_fd *ff, FILE *fp)
1747 {
1748 	struct perf_env *env = &ff->ph->env;
1749 	struct rb_root *root;
1750 	struct rb_node *next;
1751 
1752 	down_read(&env->bpf_progs.lock);
1753 
1754 	root = &env->bpf_progs.btfs;
1755 	next = rb_first(root);
1756 
1757 	while (next) {
1758 		struct btf_node *node;
1759 
1760 		node = rb_entry(next, struct btf_node, rb_node);
1761 		next = rb_next(&node->rb_node);
1762 		fprintf(fp, "# btf info of id %u\n", node->id);
1763 	}
1764 
1765 	up_read(&env->bpf_progs.lock);
1766 }
1767 #endif // HAVE_LIBBPF_SUPPORT
1768 
1769 static void free_event_desc(struct evsel *events)
1770 {
1771 	struct evsel *evsel;
1772 
1773 	if (!events)
1774 		return;
1775 
1776 	for (evsel = events; evsel->core.attr.size; evsel++) {
1777 		zfree(&evsel->name);
1778 		zfree(&evsel->core.id);
1779 	}
1780 
1781 	free(events);
1782 }
1783 
1784 static bool perf_attr_check(struct perf_event_attr *attr)
1785 {
1786 	if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3) {
1787 		pr_warning("Reserved bits are set unexpectedly. "
1788 			   "Please update perf tool.\n");
1789 		return false;
1790 	}
1791 
1792 	if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) {
1793 		pr_warning("Unknown sample type (0x%llx) is detected. "
1794 			   "Please update perf tool.\n",
1795 			   attr->sample_type);
1796 		return false;
1797 	}
1798 
1799 	if (attr->read_format & ~(PERF_FORMAT_MAX-1)) {
1800 		pr_warning("Unknown read format (0x%llx) is detected. "
1801 			   "Please update perf tool.\n",
1802 			   attr->read_format);
1803 		return false;
1804 	}
1805 
1806 	if ((attr->sample_type & PERF_SAMPLE_BRANCH_STACK) &&
1807 	    (attr->branch_sample_type & ~(PERF_SAMPLE_BRANCH_MAX-1))) {
1808 		pr_warning("Unknown branch sample type (0x%llx) is detected. "
1809 			   "Please update perf tool.\n",
1810 			   attr->branch_sample_type);
1811 
1812 		return false;
1813 	}
1814 
1815 	return true;
1816 }
1817 
1818 static struct evsel *read_event_desc(struct feat_fd *ff)
1819 {
1820 	struct evsel *evsel, *events = NULL;
1821 	u64 *id;
1822 	void *buf = NULL;
1823 	u32 nre, sz, nr, i, j;
1824 	size_t msz;
1825 
1826 	/* number of events */
1827 	if (do_read_u32(ff, &nre))
1828 		goto error;
1829 
1830 	if (do_read_u32(ff, &sz))
1831 		goto error;
1832 
1833 	/* buffer to hold on file attr struct */
1834 	buf = malloc(sz);
1835 	if (!buf)
1836 		goto error;
1837 
1838 	/* the last event terminates with evsel->core.attr.size == 0: */
1839 	events = calloc(nre + 1, sizeof(*events));
1840 	if (!events)
1841 		goto error;
1842 
1843 	msz = sizeof(evsel->core.attr);
1844 	if (sz < msz)
1845 		msz = sz;
1846 
1847 	for (i = 0, evsel = events; i < nre; evsel++, i++) {
1848 		evsel->core.idx = i;
1849 
1850 		/*
1851 		 * must read entire on-file attr struct to
1852 		 * sync up with layout.
1853 		 */
1854 		if (__do_read(ff, buf, sz))
1855 			goto error;
1856 
1857 		if (ff->ph->needs_swap)
1858 			perf_event__attr_swap(buf);
1859 
1860 		memcpy(&evsel->core.attr, buf, msz);
1861 
1862 		if (!perf_attr_check(&evsel->core.attr))
1863 			goto error;
1864 
1865 		if (do_read_u32(ff, &nr))
1866 			goto error;
1867 
1868 		if (ff->ph->needs_swap)
1869 			evsel->needs_swap = true;
1870 
1871 		evsel->name = do_read_string(ff);
1872 		if (!evsel->name)
1873 			goto error;
1874 
1875 		if (!nr)
1876 			continue;
1877 
1878 		id = calloc(nr, sizeof(*id));
1879 		if (!id)
1880 			goto error;
1881 		evsel->core.ids = nr;
1882 		evsel->core.id = id;
1883 
1884 		for (j = 0 ; j < nr; j++) {
1885 			if (do_read_u64(ff, id))
1886 				goto error;
1887 			id++;
1888 		}
1889 	}
1890 out:
1891 	free(buf);
1892 	return events;
1893 error:
1894 	free_event_desc(events);
1895 	events = NULL;
1896 	goto out;
1897 }
1898 
1899 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1900 				void *priv __maybe_unused)
1901 {
1902 	return fprintf(fp, ", %s = %s", name, val);
1903 }
1904 
1905 static void print_event_desc(struct feat_fd *ff, FILE *fp)
1906 {
1907 	struct evsel *evsel, *events;
1908 	u32 j;
1909 	u64 *id;
1910 
1911 	if (ff->events)
1912 		events = ff->events;
1913 	else
1914 		events = read_event_desc(ff);
1915 
1916 	if (!events) {
1917 		fprintf(fp, "# event desc: not available or unable to read\n");
1918 		return;
1919 	}
1920 
1921 	for (evsel = events; evsel->core.attr.size; evsel++) {
1922 		fprintf(fp, "# event : name = %s, ", evsel->name);
1923 
1924 		if (evsel->core.ids) {
1925 			fprintf(fp, ", id = {");
1926 			for (j = 0, id = evsel->core.id; j < evsel->core.ids; j++, id++) {
1927 				if (j)
1928 					fputc(',', fp);
1929 				fprintf(fp, " %"PRIu64, *id);
1930 			}
1931 			fprintf(fp, " }");
1932 		}
1933 
1934 		perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NULL);
1935 
1936 		fputc('\n', fp);
1937 	}
1938 
1939 	free_event_desc(events);
1940 	ff->events = NULL;
1941 }
1942 
1943 static void print_total_mem(struct feat_fd *ff, FILE *fp)
1944 {
1945 	fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
1946 }
1947 
1948 static void print_numa_topology(struct feat_fd *ff, FILE *fp)
1949 {
1950 	int i;
1951 	struct numa_node *n;
1952 
1953 	for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1954 		n = &ff->ph->env.numa_nodes[i];
1955 
1956 		fprintf(fp, "# node%u meminfo  : total = %"PRIu64" kB,"
1957 			    " free = %"PRIu64" kB\n",
1958 			n->node, n->mem_total, n->mem_free);
1959 
1960 		fprintf(fp, "# node%u cpu list : ", n->node);
1961 		cpu_map__fprintf(n->map, fp);
1962 	}
1963 }
1964 
1965 static void print_cpuid(struct feat_fd *ff, FILE *fp)
1966 {
1967 	fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
1968 }
1969 
1970 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
1971 {
1972 	fprintf(fp, "# contains samples with branch stack\n");
1973 }
1974 
1975 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
1976 {
1977 	fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1978 }
1979 
1980 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
1981 {
1982 	fprintf(fp, "# contains stat data\n");
1983 }
1984 
1985 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
1986 {
1987 	int i;
1988 
1989 	fprintf(fp, "# CPU cache info:\n");
1990 	for (i = 0; i < ff->ph->env.caches_cnt; i++) {
1991 		fprintf(fp, "#  ");
1992 		cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
1993 	}
1994 }
1995 
1996 static void print_compressed(struct feat_fd *ff, FILE *fp)
1997 {
1998 	fprintf(fp, "# compressed : %s, level = %d, ratio = %d\n",
1999 		ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown",
2000 		ff->ph->env.comp_level, ff->ph->env.comp_ratio);
2001 }
2002 
2003 static void print_per_cpu_pmu_caps(FILE *fp, int nr_caps, char *cpu_pmu_caps,
2004 				   char *pmu_name)
2005 {
2006 	const char *delimiter;
2007 	char *str, buf[128];
2008 
2009 	if (!nr_caps) {
2010 		if (!pmu_name)
2011 			fprintf(fp, "# cpu pmu capabilities: not available\n");
2012 		else
2013 			fprintf(fp, "# %s pmu capabilities: not available\n", pmu_name);
2014 		return;
2015 	}
2016 
2017 	if (!pmu_name)
2018 		scnprintf(buf, sizeof(buf), "# cpu pmu capabilities: ");
2019 	else
2020 		scnprintf(buf, sizeof(buf), "# %s pmu capabilities: ", pmu_name);
2021 
2022 	delimiter = buf;
2023 
2024 	str = cpu_pmu_caps;
2025 	while (nr_caps--) {
2026 		fprintf(fp, "%s%s", delimiter, str);
2027 		delimiter = ", ";
2028 		str += strlen(str) + 1;
2029 	}
2030 
2031 	fprintf(fp, "\n");
2032 }
2033 
2034 static void print_cpu_pmu_caps(struct feat_fd *ff, FILE *fp)
2035 {
2036 	print_per_cpu_pmu_caps(fp, ff->ph->env.nr_cpu_pmu_caps,
2037 			       ff->ph->env.cpu_pmu_caps, NULL);
2038 }
2039 
2040 static void print_hybrid_cpu_pmu_caps(struct feat_fd *ff, FILE *fp)
2041 {
2042 	struct hybrid_cpc_node *n;
2043 
2044 	for (int i = 0; i < ff->ph->env.nr_hybrid_cpc_nodes; i++) {
2045 		n = &ff->ph->env.hybrid_cpc_nodes[i];
2046 		print_per_cpu_pmu_caps(fp, n->nr_cpu_pmu_caps,
2047 				       n->cpu_pmu_caps,
2048 				       n->pmu_name);
2049 	}
2050 }
2051 
2052 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
2053 {
2054 	const char *delimiter = "# pmu mappings: ";
2055 	char *str, *tmp;
2056 	u32 pmu_num;
2057 	u32 type;
2058 
2059 	pmu_num = ff->ph->env.nr_pmu_mappings;
2060 	if (!pmu_num) {
2061 		fprintf(fp, "# pmu mappings: not available\n");
2062 		return;
2063 	}
2064 
2065 	str = ff->ph->env.pmu_mappings;
2066 
2067 	while (pmu_num) {
2068 		type = strtoul(str, &tmp, 0);
2069 		if (*tmp != ':')
2070 			goto error;
2071 
2072 		str = tmp + 1;
2073 		fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
2074 
2075 		delimiter = ", ";
2076 		str += strlen(str) + 1;
2077 		pmu_num--;
2078 	}
2079 
2080 	fprintf(fp, "\n");
2081 
2082 	if (!pmu_num)
2083 		return;
2084 error:
2085 	fprintf(fp, "# pmu mappings: unable to read\n");
2086 }
2087 
2088 static void print_group_desc(struct feat_fd *ff, FILE *fp)
2089 {
2090 	struct perf_session *session;
2091 	struct evsel *evsel;
2092 	u32 nr = 0;
2093 
2094 	session = container_of(ff->ph, struct perf_session, header);
2095 
2096 	evlist__for_each_entry(session->evlist, evsel) {
2097 		if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) {
2098 			fprintf(fp, "# group: %s{%s", evsel->group_name ?: "", evsel__name(evsel));
2099 
2100 			nr = evsel->core.nr_members - 1;
2101 		} else if (nr) {
2102 			fprintf(fp, ",%s", evsel__name(evsel));
2103 
2104 			if (--nr == 0)
2105 				fprintf(fp, "}\n");
2106 		}
2107 	}
2108 }
2109 
2110 static void print_sample_time(struct feat_fd *ff, FILE *fp)
2111 {
2112 	struct perf_session *session;
2113 	char time_buf[32];
2114 	double d;
2115 
2116 	session = container_of(ff->ph, struct perf_session, header);
2117 
2118 	timestamp__scnprintf_usec(session->evlist->first_sample_time,
2119 				  time_buf, sizeof(time_buf));
2120 	fprintf(fp, "# time of first sample : %s\n", time_buf);
2121 
2122 	timestamp__scnprintf_usec(session->evlist->last_sample_time,
2123 				  time_buf, sizeof(time_buf));
2124 	fprintf(fp, "# time of last sample : %s\n", time_buf);
2125 
2126 	d = (double)(session->evlist->last_sample_time -
2127 		session->evlist->first_sample_time) / NSEC_PER_MSEC;
2128 
2129 	fprintf(fp, "# sample duration : %10.3f ms\n", d);
2130 }
2131 
2132 static void memory_node__fprintf(struct memory_node *n,
2133 				 unsigned long long bsize, FILE *fp)
2134 {
2135 	char buf_map[100], buf_size[50];
2136 	unsigned long long size;
2137 
2138 	size = bsize * bitmap_weight(n->set, n->size);
2139 	unit_number__scnprintf(buf_size, 50, size);
2140 
2141 	bitmap_scnprintf(n->set, n->size, buf_map, 100);
2142 	fprintf(fp, "#  %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
2143 }
2144 
2145 static void print_mem_topology(struct feat_fd *ff, FILE *fp)
2146 {
2147 	struct memory_node *nodes;
2148 	int i, nr;
2149 
2150 	nodes = ff->ph->env.memory_nodes;
2151 	nr    = ff->ph->env.nr_memory_nodes;
2152 
2153 	fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
2154 		nr, ff->ph->env.memory_bsize);
2155 
2156 	for (i = 0; i < nr; i++) {
2157 		memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
2158 	}
2159 }
2160 
2161 static int __event_process_build_id(struct perf_record_header_build_id *bev,
2162 				    char *filename,
2163 				    struct perf_session *session)
2164 {
2165 	int err = -1;
2166 	struct machine *machine;
2167 	u16 cpumode;
2168 	struct dso *dso;
2169 	enum dso_space_type dso_space;
2170 
2171 	machine = perf_session__findnew_machine(session, bev->pid);
2172 	if (!machine)
2173 		goto out;
2174 
2175 	cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
2176 
2177 	switch (cpumode) {
2178 	case PERF_RECORD_MISC_KERNEL:
2179 		dso_space = DSO_SPACE__KERNEL;
2180 		break;
2181 	case PERF_RECORD_MISC_GUEST_KERNEL:
2182 		dso_space = DSO_SPACE__KERNEL_GUEST;
2183 		break;
2184 	case PERF_RECORD_MISC_USER:
2185 	case PERF_RECORD_MISC_GUEST_USER:
2186 		dso_space = DSO_SPACE__USER;
2187 		break;
2188 	default:
2189 		goto out;
2190 	}
2191 
2192 	dso = machine__findnew_dso(machine, filename);
2193 	if (dso != NULL) {
2194 		char sbuild_id[SBUILD_ID_SIZE];
2195 		struct build_id bid;
2196 		size_t size = BUILD_ID_SIZE;
2197 
2198 		if (bev->header.misc & PERF_RECORD_MISC_BUILD_ID_SIZE)
2199 			size = bev->size;
2200 
2201 		build_id__init(&bid, bev->data, size);
2202 		dso__set_build_id(dso, &bid);
2203 		dso->header_build_id = 1;
2204 
2205 		if (dso_space != DSO_SPACE__USER) {
2206 			struct kmod_path m = { .name = NULL, };
2207 
2208 			if (!kmod_path__parse_name(&m, filename) && m.kmod)
2209 				dso__set_module_info(dso, &m, machine);
2210 
2211 			dso->kernel = dso_space;
2212 			free(m.name);
2213 		}
2214 
2215 		build_id__sprintf(&dso->bid, sbuild_id);
2216 		pr_debug("build id event received for %s: %s [%zu]\n",
2217 			 dso->long_name, sbuild_id, size);
2218 		dso__put(dso);
2219 	}
2220 
2221 	err = 0;
2222 out:
2223 	return err;
2224 }
2225 
2226 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
2227 						 int input, u64 offset, u64 size)
2228 {
2229 	struct perf_session *session = container_of(header, struct perf_session, header);
2230 	struct {
2231 		struct perf_event_header   header;
2232 		u8			   build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
2233 		char			   filename[0];
2234 	} old_bev;
2235 	struct perf_record_header_build_id bev;
2236 	char filename[PATH_MAX];
2237 	u64 limit = offset + size;
2238 
2239 	while (offset < limit) {
2240 		ssize_t len;
2241 
2242 		if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
2243 			return -1;
2244 
2245 		if (header->needs_swap)
2246 			perf_event_header__bswap(&old_bev.header);
2247 
2248 		len = old_bev.header.size - sizeof(old_bev);
2249 		if (readn(input, filename, len) != len)
2250 			return -1;
2251 
2252 		bev.header = old_bev.header;
2253 
2254 		/*
2255 		 * As the pid is the missing value, we need to fill
2256 		 * it properly. The header.misc value give us nice hint.
2257 		 */
2258 		bev.pid	= HOST_KERNEL_ID;
2259 		if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
2260 		    bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
2261 			bev.pid	= DEFAULT_GUEST_KERNEL_ID;
2262 
2263 		memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
2264 		__event_process_build_id(&bev, filename, session);
2265 
2266 		offset += bev.header.size;
2267 	}
2268 
2269 	return 0;
2270 }
2271 
2272 static int perf_header__read_build_ids(struct perf_header *header,
2273 				       int input, u64 offset, u64 size)
2274 {
2275 	struct perf_session *session = container_of(header, struct perf_session, header);
2276 	struct perf_record_header_build_id bev;
2277 	char filename[PATH_MAX];
2278 	u64 limit = offset + size, orig_offset = offset;
2279 	int err = -1;
2280 
2281 	while (offset < limit) {
2282 		ssize_t len;
2283 
2284 		if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
2285 			goto out;
2286 
2287 		if (header->needs_swap)
2288 			perf_event_header__bswap(&bev.header);
2289 
2290 		len = bev.header.size - sizeof(bev);
2291 		if (readn(input, filename, len) != len)
2292 			goto out;
2293 		/*
2294 		 * The a1645ce1 changeset:
2295 		 *
2296 		 * "perf: 'perf kvm' tool for monitoring guest performance from host"
2297 		 *
2298 		 * Added a field to struct perf_record_header_build_id that broke the file
2299 		 * format.
2300 		 *
2301 		 * Since the kernel build-id is the first entry, process the
2302 		 * table using the old format if the well known
2303 		 * '[kernel.kallsyms]' string for the kernel build-id has the
2304 		 * first 4 characters chopped off (where the pid_t sits).
2305 		 */
2306 		if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
2307 			if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
2308 				return -1;
2309 			return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
2310 		}
2311 
2312 		__event_process_build_id(&bev, filename, session);
2313 
2314 		offset += bev.header.size;
2315 	}
2316 	err = 0;
2317 out:
2318 	return err;
2319 }
2320 
2321 /* Macro for features that simply need to read and store a string. */
2322 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
2323 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
2324 {\
2325 	free(ff->ph->env.__feat_env);		     \
2326 	ff->ph->env.__feat_env = do_read_string(ff); \
2327 	return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
2328 }
2329 
2330 FEAT_PROCESS_STR_FUN(hostname, hostname);
2331 FEAT_PROCESS_STR_FUN(osrelease, os_release);
2332 FEAT_PROCESS_STR_FUN(version, version);
2333 FEAT_PROCESS_STR_FUN(arch, arch);
2334 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
2335 FEAT_PROCESS_STR_FUN(cpuid, cpuid);
2336 
2337 static int process_tracing_data(struct feat_fd *ff, void *data)
2338 {
2339 	ssize_t ret = trace_report(ff->fd, data, false);
2340 
2341 	return ret < 0 ? -1 : 0;
2342 }
2343 
2344 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
2345 {
2346 	if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
2347 		pr_debug("Failed to read buildids, continuing...\n");
2348 	return 0;
2349 }
2350 
2351 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
2352 {
2353 	int ret;
2354 	u32 nr_cpus_avail, nr_cpus_online;
2355 
2356 	ret = do_read_u32(ff, &nr_cpus_avail);
2357 	if (ret)
2358 		return ret;
2359 
2360 	ret = do_read_u32(ff, &nr_cpus_online);
2361 	if (ret)
2362 		return ret;
2363 	ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
2364 	ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
2365 	return 0;
2366 }
2367 
2368 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
2369 {
2370 	u64 total_mem;
2371 	int ret;
2372 
2373 	ret = do_read_u64(ff, &total_mem);
2374 	if (ret)
2375 		return -1;
2376 	ff->ph->env.total_mem = (unsigned long long)total_mem;
2377 	return 0;
2378 }
2379 
2380 static struct evsel *evlist__find_by_index(struct evlist *evlist, int idx)
2381 {
2382 	struct evsel *evsel;
2383 
2384 	evlist__for_each_entry(evlist, evsel) {
2385 		if (evsel->core.idx == idx)
2386 			return evsel;
2387 	}
2388 
2389 	return NULL;
2390 }
2391 
2392 static void evlist__set_event_name(struct evlist *evlist, struct evsel *event)
2393 {
2394 	struct evsel *evsel;
2395 
2396 	if (!event->name)
2397 		return;
2398 
2399 	evsel = evlist__find_by_index(evlist, event->core.idx);
2400 	if (!evsel)
2401 		return;
2402 
2403 	if (evsel->name)
2404 		return;
2405 
2406 	evsel->name = strdup(event->name);
2407 }
2408 
2409 static int
2410 process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
2411 {
2412 	struct perf_session *session;
2413 	struct evsel *evsel, *events = read_event_desc(ff);
2414 
2415 	if (!events)
2416 		return 0;
2417 
2418 	session = container_of(ff->ph, struct perf_session, header);
2419 
2420 	if (session->data->is_pipe) {
2421 		/* Save events for reading later by print_event_desc,
2422 		 * since they can't be read again in pipe mode. */
2423 		ff->events = events;
2424 	}
2425 
2426 	for (evsel = events; evsel->core.attr.size; evsel++)
2427 		evlist__set_event_name(session->evlist, evsel);
2428 
2429 	if (!session->data->is_pipe)
2430 		free_event_desc(events);
2431 
2432 	return 0;
2433 }
2434 
2435 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
2436 {
2437 	char *str, *cmdline = NULL, **argv = NULL;
2438 	u32 nr, i, len = 0;
2439 
2440 	if (do_read_u32(ff, &nr))
2441 		return -1;
2442 
2443 	ff->ph->env.nr_cmdline = nr;
2444 
2445 	cmdline = zalloc(ff->size + nr + 1);
2446 	if (!cmdline)
2447 		return -1;
2448 
2449 	argv = zalloc(sizeof(char *) * (nr + 1));
2450 	if (!argv)
2451 		goto error;
2452 
2453 	for (i = 0; i < nr; i++) {
2454 		str = do_read_string(ff);
2455 		if (!str)
2456 			goto error;
2457 
2458 		argv[i] = cmdline + len;
2459 		memcpy(argv[i], str, strlen(str) + 1);
2460 		len += strlen(str) + 1;
2461 		free(str);
2462 	}
2463 	ff->ph->env.cmdline = cmdline;
2464 	ff->ph->env.cmdline_argv = (const char **) argv;
2465 	return 0;
2466 
2467 error:
2468 	free(argv);
2469 	free(cmdline);
2470 	return -1;
2471 }
2472 
2473 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
2474 {
2475 	u32 nr, i;
2476 	char *str;
2477 	struct strbuf sb;
2478 	int cpu_nr = ff->ph->env.nr_cpus_avail;
2479 	u64 size = 0;
2480 	struct perf_header *ph = ff->ph;
2481 	bool do_core_id_test = true;
2482 
2483 	ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
2484 	if (!ph->env.cpu)
2485 		return -1;
2486 
2487 	if (do_read_u32(ff, &nr))
2488 		goto free_cpu;
2489 
2490 	ph->env.nr_sibling_cores = nr;
2491 	size += sizeof(u32);
2492 	if (strbuf_init(&sb, 128) < 0)
2493 		goto free_cpu;
2494 
2495 	for (i = 0; i < nr; i++) {
2496 		str = do_read_string(ff);
2497 		if (!str)
2498 			goto error;
2499 
2500 		/* include a NULL character at the end */
2501 		if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2502 			goto error;
2503 		size += string_size(str);
2504 		free(str);
2505 	}
2506 	ph->env.sibling_cores = strbuf_detach(&sb, NULL);
2507 
2508 	if (do_read_u32(ff, &nr))
2509 		return -1;
2510 
2511 	ph->env.nr_sibling_threads = nr;
2512 	size += sizeof(u32);
2513 
2514 	for (i = 0; i < nr; i++) {
2515 		str = do_read_string(ff);
2516 		if (!str)
2517 			goto error;
2518 
2519 		/* include a NULL character at the end */
2520 		if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2521 			goto error;
2522 		size += string_size(str);
2523 		free(str);
2524 	}
2525 	ph->env.sibling_threads = strbuf_detach(&sb, NULL);
2526 
2527 	/*
2528 	 * The header may be from old perf,
2529 	 * which doesn't include core id and socket id information.
2530 	 */
2531 	if (ff->size <= size) {
2532 		zfree(&ph->env.cpu);
2533 		return 0;
2534 	}
2535 
2536 	/* On s390 the socket_id number is not related to the numbers of cpus.
2537 	 * The socket_id number might be higher than the numbers of cpus.
2538 	 * This depends on the configuration.
2539 	 * AArch64 is the same.
2540 	 */
2541 	if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4)
2542 			  || !strncmp(ph->env.arch, "aarch64", 7)))
2543 		do_core_id_test = false;
2544 
2545 	for (i = 0; i < (u32)cpu_nr; i++) {
2546 		if (do_read_u32(ff, &nr))
2547 			goto free_cpu;
2548 
2549 		ph->env.cpu[i].core_id = nr;
2550 		size += sizeof(u32);
2551 
2552 		if (do_read_u32(ff, &nr))
2553 			goto free_cpu;
2554 
2555 		if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
2556 			pr_debug("socket_id number is too big."
2557 				 "You may need to upgrade the perf tool.\n");
2558 			goto free_cpu;
2559 		}
2560 
2561 		ph->env.cpu[i].socket_id = nr;
2562 		size += sizeof(u32);
2563 	}
2564 
2565 	/*
2566 	 * The header may be from old perf,
2567 	 * which doesn't include die information.
2568 	 */
2569 	if (ff->size <= size)
2570 		return 0;
2571 
2572 	if (do_read_u32(ff, &nr))
2573 		return -1;
2574 
2575 	ph->env.nr_sibling_dies = nr;
2576 	size += sizeof(u32);
2577 
2578 	for (i = 0; i < nr; i++) {
2579 		str = do_read_string(ff);
2580 		if (!str)
2581 			goto error;
2582 
2583 		/* include a NULL character at the end */
2584 		if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2585 			goto error;
2586 		size += string_size(str);
2587 		free(str);
2588 	}
2589 	ph->env.sibling_dies = strbuf_detach(&sb, NULL);
2590 
2591 	for (i = 0; i < (u32)cpu_nr; i++) {
2592 		if (do_read_u32(ff, &nr))
2593 			goto free_cpu;
2594 
2595 		ph->env.cpu[i].die_id = nr;
2596 	}
2597 
2598 	return 0;
2599 
2600 error:
2601 	strbuf_release(&sb);
2602 free_cpu:
2603 	zfree(&ph->env.cpu);
2604 	return -1;
2605 }
2606 
2607 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
2608 {
2609 	struct numa_node *nodes, *n;
2610 	u32 nr, i;
2611 	char *str;
2612 
2613 	/* nr nodes */
2614 	if (do_read_u32(ff, &nr))
2615 		return -1;
2616 
2617 	nodes = zalloc(sizeof(*nodes) * nr);
2618 	if (!nodes)
2619 		return -ENOMEM;
2620 
2621 	for (i = 0; i < nr; i++) {
2622 		n = &nodes[i];
2623 
2624 		/* node number */
2625 		if (do_read_u32(ff, &n->node))
2626 			goto error;
2627 
2628 		if (do_read_u64(ff, &n->mem_total))
2629 			goto error;
2630 
2631 		if (do_read_u64(ff, &n->mem_free))
2632 			goto error;
2633 
2634 		str = do_read_string(ff);
2635 		if (!str)
2636 			goto error;
2637 
2638 		n->map = perf_cpu_map__new(str);
2639 		if (!n->map)
2640 			goto error;
2641 
2642 		free(str);
2643 	}
2644 	ff->ph->env.nr_numa_nodes = nr;
2645 	ff->ph->env.numa_nodes = nodes;
2646 	return 0;
2647 
2648 error:
2649 	free(nodes);
2650 	return -1;
2651 }
2652 
2653 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
2654 {
2655 	char *name;
2656 	u32 pmu_num;
2657 	u32 type;
2658 	struct strbuf sb;
2659 
2660 	if (do_read_u32(ff, &pmu_num))
2661 		return -1;
2662 
2663 	if (!pmu_num) {
2664 		pr_debug("pmu mappings not available\n");
2665 		return 0;
2666 	}
2667 
2668 	ff->ph->env.nr_pmu_mappings = pmu_num;
2669 	if (strbuf_init(&sb, 128) < 0)
2670 		return -1;
2671 
2672 	while (pmu_num) {
2673 		if (do_read_u32(ff, &type))
2674 			goto error;
2675 
2676 		name = do_read_string(ff);
2677 		if (!name)
2678 			goto error;
2679 
2680 		if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2681 			goto error;
2682 		/* include a NULL character at the end */
2683 		if (strbuf_add(&sb, "", 1) < 0)
2684 			goto error;
2685 
2686 		if (!strcmp(name, "msr"))
2687 			ff->ph->env.msr_pmu_type = type;
2688 
2689 		free(name);
2690 		pmu_num--;
2691 	}
2692 	ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2693 	return 0;
2694 
2695 error:
2696 	strbuf_release(&sb);
2697 	return -1;
2698 }
2699 
2700 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
2701 {
2702 	size_t ret = -1;
2703 	u32 i, nr, nr_groups;
2704 	struct perf_session *session;
2705 	struct evsel *evsel, *leader = NULL;
2706 	struct group_desc {
2707 		char *name;
2708 		u32 leader_idx;
2709 		u32 nr_members;
2710 	} *desc;
2711 
2712 	if (do_read_u32(ff, &nr_groups))
2713 		return -1;
2714 
2715 	ff->ph->env.nr_groups = nr_groups;
2716 	if (!nr_groups) {
2717 		pr_debug("group desc not available\n");
2718 		return 0;
2719 	}
2720 
2721 	desc = calloc(nr_groups, sizeof(*desc));
2722 	if (!desc)
2723 		return -1;
2724 
2725 	for (i = 0; i < nr_groups; i++) {
2726 		desc[i].name = do_read_string(ff);
2727 		if (!desc[i].name)
2728 			goto out_free;
2729 
2730 		if (do_read_u32(ff, &desc[i].leader_idx))
2731 			goto out_free;
2732 
2733 		if (do_read_u32(ff, &desc[i].nr_members))
2734 			goto out_free;
2735 	}
2736 
2737 	/*
2738 	 * Rebuild group relationship based on the group_desc
2739 	 */
2740 	session = container_of(ff->ph, struct perf_session, header);
2741 	session->evlist->core.nr_groups = nr_groups;
2742 
2743 	i = nr = 0;
2744 	evlist__for_each_entry(session->evlist, evsel) {
2745 		if (evsel->core.idx == (int) desc[i].leader_idx) {
2746 			evsel__set_leader(evsel, evsel);
2747 			/* {anon_group} is a dummy name */
2748 			if (strcmp(desc[i].name, "{anon_group}")) {
2749 				evsel->group_name = desc[i].name;
2750 				desc[i].name = NULL;
2751 			}
2752 			evsel->core.nr_members = desc[i].nr_members;
2753 
2754 			if (i >= nr_groups || nr > 0) {
2755 				pr_debug("invalid group desc\n");
2756 				goto out_free;
2757 			}
2758 
2759 			leader = evsel;
2760 			nr = evsel->core.nr_members - 1;
2761 			i++;
2762 		} else if (nr) {
2763 			/* This is a group member */
2764 			evsel__set_leader(evsel, leader);
2765 
2766 			nr--;
2767 		}
2768 	}
2769 
2770 	if (i != nr_groups || nr != 0) {
2771 		pr_debug("invalid group desc\n");
2772 		goto out_free;
2773 	}
2774 
2775 	ret = 0;
2776 out_free:
2777 	for (i = 0; i < nr_groups; i++)
2778 		zfree(&desc[i].name);
2779 	free(desc);
2780 
2781 	return ret;
2782 }
2783 
2784 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
2785 {
2786 	struct perf_session *session;
2787 	int err;
2788 
2789 	session = container_of(ff->ph, struct perf_session, header);
2790 
2791 	err = auxtrace_index__process(ff->fd, ff->size, session,
2792 				      ff->ph->needs_swap);
2793 	if (err < 0)
2794 		pr_err("Failed to process auxtrace index\n");
2795 	return err;
2796 }
2797 
2798 static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
2799 {
2800 	struct cpu_cache_level *caches;
2801 	u32 cnt, i, version;
2802 
2803 	if (do_read_u32(ff, &version))
2804 		return -1;
2805 
2806 	if (version != 1)
2807 		return -1;
2808 
2809 	if (do_read_u32(ff, &cnt))
2810 		return -1;
2811 
2812 	caches = zalloc(sizeof(*caches) * cnt);
2813 	if (!caches)
2814 		return -1;
2815 
2816 	for (i = 0; i < cnt; i++) {
2817 		struct cpu_cache_level c;
2818 
2819 		#define _R(v)						\
2820 			if (do_read_u32(ff, &c.v))\
2821 				goto out_free_caches;			\
2822 
2823 		_R(level)
2824 		_R(line_size)
2825 		_R(sets)
2826 		_R(ways)
2827 		#undef _R
2828 
2829 		#define _R(v)					\
2830 			c.v = do_read_string(ff);		\
2831 			if (!c.v)				\
2832 				goto out_free_caches;
2833 
2834 		_R(type)
2835 		_R(size)
2836 		_R(map)
2837 		#undef _R
2838 
2839 		caches[i] = c;
2840 	}
2841 
2842 	ff->ph->env.caches = caches;
2843 	ff->ph->env.caches_cnt = cnt;
2844 	return 0;
2845 out_free_caches:
2846 	free(caches);
2847 	return -1;
2848 }
2849 
2850 static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2851 {
2852 	struct perf_session *session;
2853 	u64 first_sample_time, last_sample_time;
2854 	int ret;
2855 
2856 	session = container_of(ff->ph, struct perf_session, header);
2857 
2858 	ret = do_read_u64(ff, &first_sample_time);
2859 	if (ret)
2860 		return -1;
2861 
2862 	ret = do_read_u64(ff, &last_sample_time);
2863 	if (ret)
2864 		return -1;
2865 
2866 	session->evlist->first_sample_time = first_sample_time;
2867 	session->evlist->last_sample_time = last_sample_time;
2868 	return 0;
2869 }
2870 
2871 static int process_mem_topology(struct feat_fd *ff,
2872 				void *data __maybe_unused)
2873 {
2874 	struct memory_node *nodes;
2875 	u64 version, i, nr, bsize;
2876 	int ret = -1;
2877 
2878 	if (do_read_u64(ff, &version))
2879 		return -1;
2880 
2881 	if (version != 1)
2882 		return -1;
2883 
2884 	if (do_read_u64(ff, &bsize))
2885 		return -1;
2886 
2887 	if (do_read_u64(ff, &nr))
2888 		return -1;
2889 
2890 	nodes = zalloc(sizeof(*nodes) * nr);
2891 	if (!nodes)
2892 		return -1;
2893 
2894 	for (i = 0; i < nr; i++) {
2895 		struct memory_node n;
2896 
2897 		#define _R(v)				\
2898 			if (do_read_u64(ff, &n.v))	\
2899 				goto out;		\
2900 
2901 		_R(node)
2902 		_R(size)
2903 
2904 		#undef _R
2905 
2906 		if (do_read_bitmap(ff, &n.set, &n.size))
2907 			goto out;
2908 
2909 		nodes[i] = n;
2910 	}
2911 
2912 	ff->ph->env.memory_bsize    = bsize;
2913 	ff->ph->env.memory_nodes    = nodes;
2914 	ff->ph->env.nr_memory_nodes = nr;
2915 	ret = 0;
2916 
2917 out:
2918 	if (ret)
2919 		free(nodes);
2920 	return ret;
2921 }
2922 
2923 static int process_clockid(struct feat_fd *ff,
2924 			   void *data __maybe_unused)
2925 {
2926 	if (do_read_u64(ff, &ff->ph->env.clock.clockid_res_ns))
2927 		return -1;
2928 
2929 	return 0;
2930 }
2931 
2932 static int process_clock_data(struct feat_fd *ff,
2933 			      void *_data __maybe_unused)
2934 {
2935 	u32 data32;
2936 	u64 data64;
2937 
2938 	/* version */
2939 	if (do_read_u32(ff, &data32))
2940 		return -1;
2941 
2942 	if (data32 != 1)
2943 		return -1;
2944 
2945 	/* clockid */
2946 	if (do_read_u32(ff, &data32))
2947 		return -1;
2948 
2949 	ff->ph->env.clock.clockid = data32;
2950 
2951 	/* TOD ref time */
2952 	if (do_read_u64(ff, &data64))
2953 		return -1;
2954 
2955 	ff->ph->env.clock.tod_ns = data64;
2956 
2957 	/* clockid ref time */
2958 	if (do_read_u64(ff, &data64))
2959 		return -1;
2960 
2961 	ff->ph->env.clock.clockid_ns = data64;
2962 	ff->ph->env.clock.enabled = true;
2963 	return 0;
2964 }
2965 
2966 static int process_hybrid_topology(struct feat_fd *ff,
2967 				   void *data __maybe_unused)
2968 {
2969 	struct hybrid_node *nodes, *n;
2970 	u32 nr, i;
2971 
2972 	/* nr nodes */
2973 	if (do_read_u32(ff, &nr))
2974 		return -1;
2975 
2976 	nodes = zalloc(sizeof(*nodes) * nr);
2977 	if (!nodes)
2978 		return -ENOMEM;
2979 
2980 	for (i = 0; i < nr; i++) {
2981 		n = &nodes[i];
2982 
2983 		n->pmu_name = do_read_string(ff);
2984 		if (!n->pmu_name)
2985 			goto error;
2986 
2987 		n->cpus = do_read_string(ff);
2988 		if (!n->cpus)
2989 			goto error;
2990 	}
2991 
2992 	ff->ph->env.nr_hybrid_nodes = nr;
2993 	ff->ph->env.hybrid_nodes = nodes;
2994 	return 0;
2995 
2996 error:
2997 	for (i = 0; i < nr; i++) {
2998 		free(nodes[i].pmu_name);
2999 		free(nodes[i].cpus);
3000 	}
3001 
3002 	free(nodes);
3003 	return -1;
3004 }
3005 
3006 static int process_dir_format(struct feat_fd *ff,
3007 			      void *_data __maybe_unused)
3008 {
3009 	struct perf_session *session;
3010 	struct perf_data *data;
3011 
3012 	session = container_of(ff->ph, struct perf_session, header);
3013 	data = session->data;
3014 
3015 	if (WARN_ON(!perf_data__is_dir(data)))
3016 		return -1;
3017 
3018 	return do_read_u64(ff, &data->dir.version);
3019 }
3020 
3021 #ifdef HAVE_LIBBPF_SUPPORT
3022 static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)
3023 {
3024 	struct bpf_prog_info_node *info_node;
3025 	struct perf_env *env = &ff->ph->env;
3026 	struct perf_bpil *info_linear;
3027 	u32 count, i;
3028 	int err = -1;
3029 
3030 	if (ff->ph->needs_swap) {
3031 		pr_warning("interpreting bpf_prog_info from systems with endianness is not yet supported\n");
3032 		return 0;
3033 	}
3034 
3035 	if (do_read_u32(ff, &count))
3036 		return -1;
3037 
3038 	down_write(&env->bpf_progs.lock);
3039 
3040 	for (i = 0; i < count; ++i) {
3041 		u32 info_len, data_len;
3042 
3043 		info_linear = NULL;
3044 		info_node = NULL;
3045 		if (do_read_u32(ff, &info_len))
3046 			goto out;
3047 		if (do_read_u32(ff, &data_len))
3048 			goto out;
3049 
3050 		if (info_len > sizeof(struct bpf_prog_info)) {
3051 			pr_warning("detected invalid bpf_prog_info\n");
3052 			goto out;
3053 		}
3054 
3055 		info_linear = malloc(sizeof(struct perf_bpil) +
3056 				     data_len);
3057 		if (!info_linear)
3058 			goto out;
3059 		info_linear->info_len = sizeof(struct bpf_prog_info);
3060 		info_linear->data_len = data_len;
3061 		if (do_read_u64(ff, (u64 *)(&info_linear->arrays)))
3062 			goto out;
3063 		if (__do_read(ff, &info_linear->info, info_len))
3064 			goto out;
3065 		if (info_len < sizeof(struct bpf_prog_info))
3066 			memset(((void *)(&info_linear->info)) + info_len, 0,
3067 			       sizeof(struct bpf_prog_info) - info_len);
3068 
3069 		if (__do_read(ff, info_linear->data, data_len))
3070 			goto out;
3071 
3072 		info_node = malloc(sizeof(struct bpf_prog_info_node));
3073 		if (!info_node)
3074 			goto out;
3075 
3076 		/* after reading from file, translate offset to address */
3077 		bpil_offs_to_addr(info_linear);
3078 		info_node->info_linear = info_linear;
3079 		perf_env__insert_bpf_prog_info(env, info_node);
3080 	}
3081 
3082 	up_write(&env->bpf_progs.lock);
3083 	return 0;
3084 out:
3085 	free(info_linear);
3086 	free(info_node);
3087 	up_write(&env->bpf_progs.lock);
3088 	return err;
3089 }
3090 
3091 static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)
3092 {
3093 	struct perf_env *env = &ff->ph->env;
3094 	struct btf_node *node = NULL;
3095 	u32 count, i;
3096 	int err = -1;
3097 
3098 	if (ff->ph->needs_swap) {
3099 		pr_warning("interpreting btf from systems with endianness is not yet supported\n");
3100 		return 0;
3101 	}
3102 
3103 	if (do_read_u32(ff, &count))
3104 		return -1;
3105 
3106 	down_write(&env->bpf_progs.lock);
3107 
3108 	for (i = 0; i < count; ++i) {
3109 		u32 id, data_size;
3110 
3111 		if (do_read_u32(ff, &id))
3112 			goto out;
3113 		if (do_read_u32(ff, &data_size))
3114 			goto out;
3115 
3116 		node = malloc(sizeof(struct btf_node) + data_size);
3117 		if (!node)
3118 			goto out;
3119 
3120 		node->id = id;
3121 		node->data_size = data_size;
3122 
3123 		if (__do_read(ff, node->data, data_size))
3124 			goto out;
3125 
3126 		perf_env__insert_btf(env, node);
3127 		node = NULL;
3128 	}
3129 
3130 	err = 0;
3131 out:
3132 	up_write(&env->bpf_progs.lock);
3133 	free(node);
3134 	return err;
3135 }
3136 #endif // HAVE_LIBBPF_SUPPORT
3137 
3138 static int process_compressed(struct feat_fd *ff,
3139 			      void *data __maybe_unused)
3140 {
3141 	if (do_read_u32(ff, &(ff->ph->env.comp_ver)))
3142 		return -1;
3143 
3144 	if (do_read_u32(ff, &(ff->ph->env.comp_type)))
3145 		return -1;
3146 
3147 	if (do_read_u32(ff, &(ff->ph->env.comp_level)))
3148 		return -1;
3149 
3150 	if (do_read_u32(ff, &(ff->ph->env.comp_ratio)))
3151 		return -1;
3152 
3153 	if (do_read_u32(ff, &(ff->ph->env.comp_mmap_len)))
3154 		return -1;
3155 
3156 	return 0;
3157 }
3158 
3159 static int process_per_cpu_pmu_caps(struct feat_fd *ff, int *nr_cpu_pmu_caps,
3160 				    char **cpu_pmu_caps,
3161 				    unsigned int *max_branches)
3162 {
3163 	char *name, *value;
3164 	struct strbuf sb;
3165 	u32 nr_caps;
3166 
3167 	if (do_read_u32(ff, &nr_caps))
3168 		return -1;
3169 
3170 	if (!nr_caps) {
3171 		pr_debug("cpu pmu capabilities not available\n");
3172 		return 0;
3173 	}
3174 
3175 	*nr_cpu_pmu_caps = nr_caps;
3176 
3177 	if (strbuf_init(&sb, 128) < 0)
3178 		return -1;
3179 
3180 	while (nr_caps--) {
3181 		name = do_read_string(ff);
3182 		if (!name)
3183 			goto error;
3184 
3185 		value = do_read_string(ff);
3186 		if (!value)
3187 			goto free_name;
3188 
3189 		if (strbuf_addf(&sb, "%s=%s", name, value) < 0)
3190 			goto free_value;
3191 
3192 		/* include a NULL character at the end */
3193 		if (strbuf_add(&sb, "", 1) < 0)
3194 			goto free_value;
3195 
3196 		if (!strcmp(name, "branches"))
3197 			*max_branches = atoi(value);
3198 
3199 		free(value);
3200 		free(name);
3201 	}
3202 	*cpu_pmu_caps = strbuf_detach(&sb, NULL);
3203 	return 0;
3204 
3205 free_value:
3206 	free(value);
3207 free_name:
3208 	free(name);
3209 error:
3210 	strbuf_release(&sb);
3211 	return -1;
3212 }
3213 
3214 static int process_cpu_pmu_caps(struct feat_fd *ff,
3215 				void *data __maybe_unused)
3216 {
3217 	return process_per_cpu_pmu_caps(ff, &ff->ph->env.nr_cpu_pmu_caps,
3218 					&ff->ph->env.cpu_pmu_caps,
3219 					&ff->ph->env.max_branches);
3220 }
3221 
3222 static int process_hybrid_cpu_pmu_caps(struct feat_fd *ff,
3223 				       void *data __maybe_unused)
3224 {
3225 	struct hybrid_cpc_node *nodes;
3226 	u32 nr_pmu, i;
3227 	int ret;
3228 
3229 	if (do_read_u32(ff, &nr_pmu))
3230 		return -1;
3231 
3232 	if (!nr_pmu) {
3233 		pr_debug("hybrid cpu pmu capabilities not available\n");
3234 		return 0;
3235 	}
3236 
3237 	nodes = zalloc(sizeof(*nodes) * nr_pmu);
3238 	if (!nodes)
3239 		return -ENOMEM;
3240 
3241 	for (i = 0; i < nr_pmu; i++) {
3242 		struct hybrid_cpc_node *n = &nodes[i];
3243 
3244 		ret = process_per_cpu_pmu_caps(ff, &n->nr_cpu_pmu_caps,
3245 					       &n->cpu_pmu_caps,
3246 					       &n->max_branches);
3247 		if (ret)
3248 			goto err;
3249 
3250 		n->pmu_name = do_read_string(ff);
3251 		if (!n->pmu_name) {
3252 			ret = -1;
3253 			goto err;
3254 		}
3255 	}
3256 
3257 	ff->ph->env.nr_hybrid_cpc_nodes = nr_pmu;
3258 	ff->ph->env.hybrid_cpc_nodes = nodes;
3259 	return 0;
3260 
3261 err:
3262 	for (i = 0; i < nr_pmu; i++) {
3263 		free(nodes[i].cpu_pmu_caps);
3264 		free(nodes[i].pmu_name);
3265 	}
3266 
3267 	free(nodes);
3268 	return ret;
3269 }
3270 
3271 #define FEAT_OPR(n, func, __full_only) \
3272 	[HEADER_##n] = {					\
3273 		.name	    = __stringify(n),			\
3274 		.write	    = write_##func,			\
3275 		.print	    = print_##func,			\
3276 		.full_only  = __full_only,			\
3277 		.process    = process_##func,			\
3278 		.synthesize = true				\
3279 	}
3280 
3281 #define FEAT_OPN(n, func, __full_only) \
3282 	[HEADER_##n] = {					\
3283 		.name	    = __stringify(n),			\
3284 		.write	    = write_##func,			\
3285 		.print	    = print_##func,			\
3286 		.full_only  = __full_only,			\
3287 		.process    = process_##func			\
3288 	}
3289 
3290 /* feature_ops not implemented: */
3291 #define print_tracing_data	NULL
3292 #define print_build_id		NULL
3293 
3294 #define process_branch_stack	NULL
3295 #define process_stat		NULL
3296 
3297 // Only used in util/synthetic-events.c
3298 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
3299 
3300 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE] = {
3301 	FEAT_OPN(TRACING_DATA,	tracing_data,	false),
3302 	FEAT_OPN(BUILD_ID,	build_id,	false),
3303 	FEAT_OPR(HOSTNAME,	hostname,	false),
3304 	FEAT_OPR(OSRELEASE,	osrelease,	false),
3305 	FEAT_OPR(VERSION,	version,	false),
3306 	FEAT_OPR(ARCH,		arch,		false),
3307 	FEAT_OPR(NRCPUS,	nrcpus,		false),
3308 	FEAT_OPR(CPUDESC,	cpudesc,	false),
3309 	FEAT_OPR(CPUID,		cpuid,		false),
3310 	FEAT_OPR(TOTAL_MEM,	total_mem,	false),
3311 	FEAT_OPR(EVENT_DESC,	event_desc,	false),
3312 	FEAT_OPR(CMDLINE,	cmdline,	false),
3313 	FEAT_OPR(CPU_TOPOLOGY,	cpu_topology,	true),
3314 	FEAT_OPR(NUMA_TOPOLOGY,	numa_topology,	true),
3315 	FEAT_OPN(BRANCH_STACK,	branch_stack,	false),
3316 	FEAT_OPR(PMU_MAPPINGS,	pmu_mappings,	false),
3317 	FEAT_OPR(GROUP_DESC,	group_desc,	false),
3318 	FEAT_OPN(AUXTRACE,	auxtrace,	false),
3319 	FEAT_OPN(STAT,		stat,		false),
3320 	FEAT_OPN(CACHE,		cache,		true),
3321 	FEAT_OPR(SAMPLE_TIME,	sample_time,	false),
3322 	FEAT_OPR(MEM_TOPOLOGY,	mem_topology,	true),
3323 	FEAT_OPR(CLOCKID,	clockid,	false),
3324 	FEAT_OPN(DIR_FORMAT,	dir_format,	false),
3325 #ifdef HAVE_LIBBPF_SUPPORT
3326 	FEAT_OPR(BPF_PROG_INFO, bpf_prog_info,  false),
3327 	FEAT_OPR(BPF_BTF,       bpf_btf,        false),
3328 #endif
3329 	FEAT_OPR(COMPRESSED,	compressed,	false),
3330 	FEAT_OPR(CPU_PMU_CAPS,	cpu_pmu_caps,	false),
3331 	FEAT_OPR(CLOCK_DATA,	clock_data,	false),
3332 	FEAT_OPN(HYBRID_TOPOLOGY,	hybrid_topology,	true),
3333 	FEAT_OPR(HYBRID_CPU_PMU_CAPS,	hybrid_cpu_pmu_caps,	false),
3334 };
3335 
3336 struct header_print_data {
3337 	FILE *fp;
3338 	bool full; /* extended list of headers */
3339 };
3340 
3341 static int perf_file_section__fprintf_info(struct perf_file_section *section,
3342 					   struct perf_header *ph,
3343 					   int feat, int fd, void *data)
3344 {
3345 	struct header_print_data *hd = data;
3346 	struct feat_fd ff;
3347 
3348 	if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
3349 		pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
3350 				"%d, continuing...\n", section->offset, feat);
3351 		return 0;
3352 	}
3353 	if (feat >= HEADER_LAST_FEATURE) {
3354 		pr_warning("unknown feature %d\n", feat);
3355 		return 0;
3356 	}
3357 	if (!feat_ops[feat].print)
3358 		return 0;
3359 
3360 	ff = (struct  feat_fd) {
3361 		.fd = fd,
3362 		.ph = ph,
3363 	};
3364 
3365 	if (!feat_ops[feat].full_only || hd->full)
3366 		feat_ops[feat].print(&ff, hd->fp);
3367 	else
3368 		fprintf(hd->fp, "# %s info available, use -I to display\n",
3369 			feat_ops[feat].name);
3370 
3371 	return 0;
3372 }
3373 
3374 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
3375 {
3376 	struct header_print_data hd;
3377 	struct perf_header *header = &session->header;
3378 	int fd = perf_data__fd(session->data);
3379 	struct stat st;
3380 	time_t stctime;
3381 	int ret, bit;
3382 
3383 	hd.fp = fp;
3384 	hd.full = full;
3385 
3386 	ret = fstat(fd, &st);
3387 	if (ret == -1)
3388 		return -1;
3389 
3390 	stctime = st.st_mtime;
3391 	fprintf(fp, "# captured on    : %s", ctime(&stctime));
3392 
3393 	fprintf(fp, "# header version : %u\n", header->version);
3394 	fprintf(fp, "# data offset    : %" PRIu64 "\n", header->data_offset);
3395 	fprintf(fp, "# data size      : %" PRIu64 "\n", header->data_size);
3396 	fprintf(fp, "# feat offset    : %" PRIu64 "\n", header->feat_offset);
3397 
3398 	perf_header__process_sections(header, fd, &hd,
3399 				      perf_file_section__fprintf_info);
3400 
3401 	if (session->data->is_pipe)
3402 		return 0;
3403 
3404 	fprintf(fp, "# missing features: ");
3405 	for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
3406 		if (bit)
3407 			fprintf(fp, "%s ", feat_ops[bit].name);
3408 	}
3409 
3410 	fprintf(fp, "\n");
3411 	return 0;
3412 }
3413 
3414 static int do_write_feat(struct feat_fd *ff, int type,
3415 			 struct perf_file_section **p,
3416 			 struct evlist *evlist)
3417 {
3418 	int err;
3419 	int ret = 0;
3420 
3421 	if (perf_header__has_feat(ff->ph, type)) {
3422 		if (!feat_ops[type].write)
3423 			return -1;
3424 
3425 		if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
3426 			return -1;
3427 
3428 		(*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
3429 
3430 		err = feat_ops[type].write(ff, evlist);
3431 		if (err < 0) {
3432 			pr_debug("failed to write feature %s\n", feat_ops[type].name);
3433 
3434 			/* undo anything written */
3435 			lseek(ff->fd, (*p)->offset, SEEK_SET);
3436 
3437 			return -1;
3438 		}
3439 		(*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
3440 		(*p)++;
3441 	}
3442 	return ret;
3443 }
3444 
3445 static int perf_header__adds_write(struct perf_header *header,
3446 				   struct evlist *evlist, int fd)
3447 {
3448 	int nr_sections;
3449 	struct feat_fd ff;
3450 	struct perf_file_section *feat_sec, *p;
3451 	int sec_size;
3452 	u64 sec_start;
3453 	int feat;
3454 	int err;
3455 
3456 	ff = (struct feat_fd){
3457 		.fd  = fd,
3458 		.ph = header,
3459 	};
3460 
3461 	nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
3462 	if (!nr_sections)
3463 		return 0;
3464 
3465 	feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
3466 	if (feat_sec == NULL)
3467 		return -ENOMEM;
3468 
3469 	sec_size = sizeof(*feat_sec) * nr_sections;
3470 
3471 	sec_start = header->feat_offset;
3472 	lseek(fd, sec_start + sec_size, SEEK_SET);
3473 
3474 	for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3475 		if (do_write_feat(&ff, feat, &p, evlist))
3476 			perf_header__clear_feat(header, feat);
3477 	}
3478 
3479 	lseek(fd, sec_start, SEEK_SET);
3480 	/*
3481 	 * may write more than needed due to dropped feature, but
3482 	 * this is okay, reader will skip the missing entries
3483 	 */
3484 	err = do_write(&ff, feat_sec, sec_size);
3485 	if (err < 0)
3486 		pr_debug("failed to write feature section\n");
3487 	free(feat_sec);
3488 	return err;
3489 }
3490 
3491 int perf_header__write_pipe(int fd)
3492 {
3493 	struct perf_pipe_file_header f_header;
3494 	struct feat_fd ff;
3495 	int err;
3496 
3497 	ff = (struct feat_fd){ .fd = fd };
3498 
3499 	f_header = (struct perf_pipe_file_header){
3500 		.magic	   = PERF_MAGIC,
3501 		.size	   = sizeof(f_header),
3502 	};
3503 
3504 	err = do_write(&ff, &f_header, sizeof(f_header));
3505 	if (err < 0) {
3506 		pr_debug("failed to write perf pipe header\n");
3507 		return err;
3508 	}
3509 
3510 	return 0;
3511 }
3512 
3513 int perf_session__write_header(struct perf_session *session,
3514 			       struct evlist *evlist,
3515 			       int fd, bool at_exit)
3516 {
3517 	struct perf_file_header f_header;
3518 	struct perf_file_attr   f_attr;
3519 	struct perf_header *header = &session->header;
3520 	struct evsel *evsel;
3521 	struct feat_fd ff;
3522 	u64 attr_offset;
3523 	int err;
3524 
3525 	ff = (struct feat_fd){ .fd = fd};
3526 	lseek(fd, sizeof(f_header), SEEK_SET);
3527 
3528 	evlist__for_each_entry(session->evlist, evsel) {
3529 		evsel->id_offset = lseek(fd, 0, SEEK_CUR);
3530 		err = do_write(&ff, evsel->core.id, evsel->core.ids * sizeof(u64));
3531 		if (err < 0) {
3532 			pr_debug("failed to write perf header\n");
3533 			return err;
3534 		}
3535 	}
3536 
3537 	attr_offset = lseek(ff.fd, 0, SEEK_CUR);
3538 
3539 	evlist__for_each_entry(evlist, evsel) {
3540 		if (evsel->core.attr.size < sizeof(evsel->core.attr)) {
3541 			/*
3542 			 * We are likely in "perf inject" and have read
3543 			 * from an older file. Update attr size so that
3544 			 * reader gets the right offset to the ids.
3545 			 */
3546 			evsel->core.attr.size = sizeof(evsel->core.attr);
3547 		}
3548 		f_attr = (struct perf_file_attr){
3549 			.attr = evsel->core.attr,
3550 			.ids  = {
3551 				.offset = evsel->id_offset,
3552 				.size   = evsel->core.ids * sizeof(u64),
3553 			}
3554 		};
3555 		err = do_write(&ff, &f_attr, sizeof(f_attr));
3556 		if (err < 0) {
3557 			pr_debug("failed to write perf header attribute\n");
3558 			return err;
3559 		}
3560 	}
3561 
3562 	if (!header->data_offset)
3563 		header->data_offset = lseek(fd, 0, SEEK_CUR);
3564 	header->feat_offset = header->data_offset + header->data_size;
3565 
3566 	if (at_exit) {
3567 		err = perf_header__adds_write(header, evlist, fd);
3568 		if (err < 0)
3569 			return err;
3570 	}
3571 
3572 	f_header = (struct perf_file_header){
3573 		.magic	   = PERF_MAGIC,
3574 		.size	   = sizeof(f_header),
3575 		.attr_size = sizeof(f_attr),
3576 		.attrs = {
3577 			.offset = attr_offset,
3578 			.size   = evlist->core.nr_entries * sizeof(f_attr),
3579 		},
3580 		.data = {
3581 			.offset = header->data_offset,
3582 			.size	= header->data_size,
3583 		},
3584 		/* event_types is ignored, store zeros */
3585 	};
3586 
3587 	memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
3588 
3589 	lseek(fd, 0, SEEK_SET);
3590 	err = do_write(&ff, &f_header, sizeof(f_header));
3591 	if (err < 0) {
3592 		pr_debug("failed to write perf header\n");
3593 		return err;
3594 	}
3595 	lseek(fd, header->data_offset + header->data_size, SEEK_SET);
3596 
3597 	return 0;
3598 }
3599 
3600 static int perf_header__getbuffer64(struct perf_header *header,
3601 				    int fd, void *buf, size_t size)
3602 {
3603 	if (readn(fd, buf, size) <= 0)
3604 		return -1;
3605 
3606 	if (header->needs_swap)
3607 		mem_bswap_64(buf, size);
3608 
3609 	return 0;
3610 }
3611 
3612 int perf_header__process_sections(struct perf_header *header, int fd,
3613 				  void *data,
3614 				  int (*process)(struct perf_file_section *section,
3615 						 struct perf_header *ph,
3616 						 int feat, int fd, void *data))
3617 {
3618 	struct perf_file_section *feat_sec, *sec;
3619 	int nr_sections;
3620 	int sec_size;
3621 	int feat;
3622 	int err;
3623 
3624 	nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
3625 	if (!nr_sections)
3626 		return 0;
3627 
3628 	feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
3629 	if (!feat_sec)
3630 		return -1;
3631 
3632 	sec_size = sizeof(*feat_sec) * nr_sections;
3633 
3634 	lseek(fd, header->feat_offset, SEEK_SET);
3635 
3636 	err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
3637 	if (err < 0)
3638 		goto out_free;
3639 
3640 	for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
3641 		err = process(sec++, header, feat, fd, data);
3642 		if (err < 0)
3643 			goto out_free;
3644 	}
3645 	err = 0;
3646 out_free:
3647 	free(feat_sec);
3648 	return err;
3649 }
3650 
3651 static const int attr_file_abi_sizes[] = {
3652 	[0] = PERF_ATTR_SIZE_VER0,
3653 	[1] = PERF_ATTR_SIZE_VER1,
3654 	[2] = PERF_ATTR_SIZE_VER2,
3655 	[3] = PERF_ATTR_SIZE_VER3,
3656 	[4] = PERF_ATTR_SIZE_VER4,
3657 	0,
3658 };
3659 
3660 /*
3661  * In the legacy file format, the magic number is not used to encode endianness.
3662  * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
3663  * on ABI revisions, we need to try all combinations for all endianness to
3664  * detect the endianness.
3665  */
3666 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
3667 {
3668 	uint64_t ref_size, attr_size;
3669 	int i;
3670 
3671 	for (i = 0 ; attr_file_abi_sizes[i]; i++) {
3672 		ref_size = attr_file_abi_sizes[i]
3673 			 + sizeof(struct perf_file_section);
3674 		if (hdr_sz != ref_size) {
3675 			attr_size = bswap_64(hdr_sz);
3676 			if (attr_size != ref_size)
3677 				continue;
3678 
3679 			ph->needs_swap = true;
3680 		}
3681 		pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
3682 			 i,
3683 			 ph->needs_swap);
3684 		return 0;
3685 	}
3686 	/* could not determine endianness */
3687 	return -1;
3688 }
3689 
3690 #define PERF_PIPE_HDR_VER0	16
3691 
3692 static const size_t attr_pipe_abi_sizes[] = {
3693 	[0] = PERF_PIPE_HDR_VER0,
3694 	0,
3695 };
3696 
3697 /*
3698  * In the legacy pipe format, there is an implicit assumption that endianness
3699  * between host recording the samples, and host parsing the samples is the
3700  * same. This is not always the case given that the pipe output may always be
3701  * redirected into a file and analyzed on a different machine with possibly a
3702  * different endianness and perf_event ABI revisions in the perf tool itself.
3703  */
3704 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
3705 {
3706 	u64 attr_size;
3707 	int i;
3708 
3709 	for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
3710 		if (hdr_sz != attr_pipe_abi_sizes[i]) {
3711 			attr_size = bswap_64(hdr_sz);
3712 			if (attr_size != hdr_sz)
3713 				continue;
3714 
3715 			ph->needs_swap = true;
3716 		}
3717 		pr_debug("Pipe ABI%d perf.data file detected\n", i);
3718 		return 0;
3719 	}
3720 	return -1;
3721 }
3722 
3723 bool is_perf_magic(u64 magic)
3724 {
3725 	if (!memcmp(&magic, __perf_magic1, sizeof(magic))
3726 		|| magic == __perf_magic2
3727 		|| magic == __perf_magic2_sw)
3728 		return true;
3729 
3730 	return false;
3731 }
3732 
3733 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
3734 			      bool is_pipe, struct perf_header *ph)
3735 {
3736 	int ret;
3737 
3738 	/* check for legacy format */
3739 	ret = memcmp(&magic, __perf_magic1, sizeof(magic));
3740 	if (ret == 0) {
3741 		ph->version = PERF_HEADER_VERSION_1;
3742 		pr_debug("legacy perf.data format\n");
3743 		if (is_pipe)
3744 			return try_all_pipe_abis(hdr_sz, ph);
3745 
3746 		return try_all_file_abis(hdr_sz, ph);
3747 	}
3748 	/*
3749 	 * the new magic number serves two purposes:
3750 	 * - unique number to identify actual perf.data files
3751 	 * - encode endianness of file
3752 	 */
3753 	ph->version = PERF_HEADER_VERSION_2;
3754 
3755 	/* check magic number with one endianness */
3756 	if (magic == __perf_magic2)
3757 		return 0;
3758 
3759 	/* check magic number with opposite endianness */
3760 	if (magic != __perf_magic2_sw)
3761 		return -1;
3762 
3763 	ph->needs_swap = true;
3764 
3765 	return 0;
3766 }
3767 
3768 int perf_file_header__read(struct perf_file_header *header,
3769 			   struct perf_header *ph, int fd)
3770 {
3771 	ssize_t ret;
3772 
3773 	lseek(fd, 0, SEEK_SET);
3774 
3775 	ret = readn(fd, header, sizeof(*header));
3776 	if (ret <= 0)
3777 		return -1;
3778 
3779 	if (check_magic_endian(header->magic,
3780 			       header->attr_size, false, ph) < 0) {
3781 		pr_debug("magic/endian check failed\n");
3782 		return -1;
3783 	}
3784 
3785 	if (ph->needs_swap) {
3786 		mem_bswap_64(header, offsetof(struct perf_file_header,
3787 			     adds_features));
3788 	}
3789 
3790 	if (header->size != sizeof(*header)) {
3791 		/* Support the previous format */
3792 		if (header->size == offsetof(typeof(*header), adds_features))
3793 			bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3794 		else
3795 			return -1;
3796 	} else if (ph->needs_swap) {
3797 		/*
3798 		 * feature bitmap is declared as an array of unsigned longs --
3799 		 * not good since its size can differ between the host that
3800 		 * generated the data file and the host analyzing the file.
3801 		 *
3802 		 * We need to handle endianness, but we don't know the size of
3803 		 * the unsigned long where the file was generated. Take a best
3804 		 * guess at determining it: try 64-bit swap first (ie., file
3805 		 * created on a 64-bit host), and check if the hostname feature
3806 		 * bit is set (this feature bit is forced on as of fbe96f2).
3807 		 * If the bit is not, undo the 64-bit swap and try a 32-bit
3808 		 * swap. If the hostname bit is still not set (e.g., older data
3809 		 * file), punt and fallback to the original behavior --
3810 		 * clearing all feature bits and setting buildid.
3811 		 */
3812 		mem_bswap_64(&header->adds_features,
3813 			    BITS_TO_U64(HEADER_FEAT_BITS));
3814 
3815 		if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3816 			/* unswap as u64 */
3817 			mem_bswap_64(&header->adds_features,
3818 				    BITS_TO_U64(HEADER_FEAT_BITS));
3819 
3820 			/* unswap as u32 */
3821 			mem_bswap_32(&header->adds_features,
3822 				    BITS_TO_U32(HEADER_FEAT_BITS));
3823 		}
3824 
3825 		if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3826 			bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3827 			set_bit(HEADER_BUILD_ID, header->adds_features);
3828 		}
3829 	}
3830 
3831 	memcpy(&ph->adds_features, &header->adds_features,
3832 	       sizeof(ph->adds_features));
3833 
3834 	ph->data_offset  = header->data.offset;
3835 	ph->data_size	 = header->data.size;
3836 	ph->feat_offset  = header->data.offset + header->data.size;
3837 	return 0;
3838 }
3839 
3840 static int perf_file_section__process(struct perf_file_section *section,
3841 				      struct perf_header *ph,
3842 				      int feat, int fd, void *data)
3843 {
3844 	struct feat_fd fdd = {
3845 		.fd	= fd,
3846 		.ph	= ph,
3847 		.size	= section->size,
3848 		.offset	= section->offset,
3849 	};
3850 
3851 	if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
3852 		pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
3853 			  "%d, continuing...\n", section->offset, feat);
3854 		return 0;
3855 	}
3856 
3857 	if (feat >= HEADER_LAST_FEATURE) {
3858 		pr_debug("unknown feature %d, continuing...\n", feat);
3859 		return 0;
3860 	}
3861 
3862 	if (!feat_ops[feat].process)
3863 		return 0;
3864 
3865 	return feat_ops[feat].process(&fdd, data);
3866 }
3867 
3868 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
3869 				       struct perf_header *ph,
3870 				       struct perf_data* data,
3871 				       bool repipe, int repipe_fd)
3872 {
3873 	struct feat_fd ff = {
3874 		.fd = repipe_fd,
3875 		.ph = ph,
3876 	};
3877 	ssize_t ret;
3878 
3879 	ret = perf_data__read(data, header, sizeof(*header));
3880 	if (ret <= 0)
3881 		return -1;
3882 
3883 	if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
3884 		pr_debug("endian/magic failed\n");
3885 		return -1;
3886 	}
3887 
3888 	if (ph->needs_swap)
3889 		header->size = bswap_64(header->size);
3890 
3891 	if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
3892 		return -1;
3893 
3894 	return 0;
3895 }
3896 
3897 static int perf_header__read_pipe(struct perf_session *session, int repipe_fd)
3898 {
3899 	struct perf_header *header = &session->header;
3900 	struct perf_pipe_file_header f_header;
3901 
3902 	if (perf_file_header__read_pipe(&f_header, header, session->data,
3903 					session->repipe, repipe_fd) < 0) {
3904 		pr_debug("incompatible file format\n");
3905 		return -EINVAL;
3906 	}
3907 
3908 	return f_header.size == sizeof(f_header) ? 0 : -1;
3909 }
3910 
3911 static int read_attr(int fd, struct perf_header *ph,
3912 		     struct perf_file_attr *f_attr)
3913 {
3914 	struct perf_event_attr *attr = &f_attr->attr;
3915 	size_t sz, left;
3916 	size_t our_sz = sizeof(f_attr->attr);
3917 	ssize_t ret;
3918 
3919 	memset(f_attr, 0, sizeof(*f_attr));
3920 
3921 	/* read minimal guaranteed structure */
3922 	ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
3923 	if (ret <= 0) {
3924 		pr_debug("cannot read %d bytes of header attr\n",
3925 			 PERF_ATTR_SIZE_VER0);
3926 		return -1;
3927 	}
3928 
3929 	/* on file perf_event_attr size */
3930 	sz = attr->size;
3931 
3932 	if (ph->needs_swap)
3933 		sz = bswap_32(sz);
3934 
3935 	if (sz == 0) {
3936 		/* assume ABI0 */
3937 		sz =  PERF_ATTR_SIZE_VER0;
3938 	} else if (sz > our_sz) {
3939 		pr_debug("file uses a more recent and unsupported ABI"
3940 			 " (%zu bytes extra)\n", sz - our_sz);
3941 		return -1;
3942 	}
3943 	/* what we have not yet read and that we know about */
3944 	left = sz - PERF_ATTR_SIZE_VER0;
3945 	if (left) {
3946 		void *ptr = attr;
3947 		ptr += PERF_ATTR_SIZE_VER0;
3948 
3949 		ret = readn(fd, ptr, left);
3950 	}
3951 	/* read perf_file_section, ids are read in caller */
3952 	ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
3953 
3954 	return ret <= 0 ? -1 : 0;
3955 }
3956 
3957 static int evsel__prepare_tracepoint_event(struct evsel *evsel, struct tep_handle *pevent)
3958 {
3959 	struct tep_event *event;
3960 	char bf[128];
3961 
3962 	/* already prepared */
3963 	if (evsel->tp_format)
3964 		return 0;
3965 
3966 	if (pevent == NULL) {
3967 		pr_debug("broken or missing trace data\n");
3968 		return -1;
3969 	}
3970 
3971 	event = tep_find_event(pevent, evsel->core.attr.config);
3972 	if (event == NULL) {
3973 		pr_debug("cannot find event format for %d\n", (int)evsel->core.attr.config);
3974 		return -1;
3975 	}
3976 
3977 	if (!evsel->name) {
3978 		snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
3979 		evsel->name = strdup(bf);
3980 		if (evsel->name == NULL)
3981 			return -1;
3982 	}
3983 
3984 	evsel->tp_format = event;
3985 	return 0;
3986 }
3987 
3988 static int evlist__prepare_tracepoint_events(struct evlist *evlist, struct tep_handle *pevent)
3989 {
3990 	struct evsel *pos;
3991 
3992 	evlist__for_each_entry(evlist, pos) {
3993 		if (pos->core.attr.type == PERF_TYPE_TRACEPOINT &&
3994 		    evsel__prepare_tracepoint_event(pos, pevent))
3995 			return -1;
3996 	}
3997 
3998 	return 0;
3999 }
4000 
4001 int perf_session__read_header(struct perf_session *session, int repipe_fd)
4002 {
4003 	struct perf_data *data = session->data;
4004 	struct perf_header *header = &session->header;
4005 	struct perf_file_header	f_header;
4006 	struct perf_file_attr	f_attr;
4007 	u64			f_id;
4008 	int nr_attrs, nr_ids, i, j, err;
4009 	int fd = perf_data__fd(data);
4010 
4011 	session->evlist = evlist__new();
4012 	if (session->evlist == NULL)
4013 		return -ENOMEM;
4014 
4015 	session->evlist->env = &header->env;
4016 	session->machines.host.env = &header->env;
4017 
4018 	/*
4019 	 * We can read 'pipe' data event from regular file,
4020 	 * check for the pipe header regardless of source.
4021 	 */
4022 	err = perf_header__read_pipe(session, repipe_fd);
4023 	if (!err || perf_data__is_pipe(data)) {
4024 		data->is_pipe = true;
4025 		return err;
4026 	}
4027 
4028 	if (perf_file_header__read(&f_header, header, fd) < 0)
4029 		return -EINVAL;
4030 
4031 	if (header->needs_swap && data->in_place_update) {
4032 		pr_err("In-place update not supported when byte-swapping is required\n");
4033 		return -EINVAL;
4034 	}
4035 
4036 	/*
4037 	 * Sanity check that perf.data was written cleanly; data size is
4038 	 * initialized to 0 and updated only if the on_exit function is run.
4039 	 * If data size is still 0 then the file contains only partial
4040 	 * information.  Just warn user and process it as much as it can.
4041 	 */
4042 	if (f_header.data.size == 0) {
4043 		pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
4044 			   "Was the 'perf record' command properly terminated?\n",
4045 			   data->file.path);
4046 	}
4047 
4048 	if (f_header.attr_size == 0) {
4049 		pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n"
4050 		       "Was the 'perf record' command properly terminated?\n",
4051 		       data->file.path);
4052 		return -EINVAL;
4053 	}
4054 
4055 	nr_attrs = f_header.attrs.size / f_header.attr_size;
4056 	lseek(fd, f_header.attrs.offset, SEEK_SET);
4057 
4058 	for (i = 0; i < nr_attrs; i++) {
4059 		struct evsel *evsel;
4060 		off_t tmp;
4061 
4062 		if (read_attr(fd, header, &f_attr) < 0)
4063 			goto out_errno;
4064 
4065 		if (header->needs_swap) {
4066 			f_attr.ids.size   = bswap_64(f_attr.ids.size);
4067 			f_attr.ids.offset = bswap_64(f_attr.ids.offset);
4068 			perf_event__attr_swap(&f_attr.attr);
4069 		}
4070 
4071 		tmp = lseek(fd, 0, SEEK_CUR);
4072 		evsel = evsel__new(&f_attr.attr);
4073 
4074 		if (evsel == NULL)
4075 			goto out_delete_evlist;
4076 
4077 		evsel->needs_swap = header->needs_swap;
4078 		/*
4079 		 * Do it before so that if perf_evsel__alloc_id fails, this
4080 		 * entry gets purged too at evlist__delete().
4081 		 */
4082 		evlist__add(session->evlist, evsel);
4083 
4084 		nr_ids = f_attr.ids.size / sizeof(u64);
4085 		/*
4086 		 * We don't have the cpu and thread maps on the header, so
4087 		 * for allocating the perf_sample_id table we fake 1 cpu and
4088 		 * hattr->ids threads.
4089 		 */
4090 		if (perf_evsel__alloc_id(&evsel->core, 1, nr_ids))
4091 			goto out_delete_evlist;
4092 
4093 		lseek(fd, f_attr.ids.offset, SEEK_SET);
4094 
4095 		for (j = 0; j < nr_ids; j++) {
4096 			if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
4097 				goto out_errno;
4098 
4099 			perf_evlist__id_add(&session->evlist->core, &evsel->core, 0, j, f_id);
4100 		}
4101 
4102 		lseek(fd, tmp, SEEK_SET);
4103 	}
4104 
4105 	perf_header__process_sections(header, fd, &session->tevent,
4106 				      perf_file_section__process);
4107 
4108 	if (evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent))
4109 		goto out_delete_evlist;
4110 
4111 	return 0;
4112 out_errno:
4113 	return -errno;
4114 
4115 out_delete_evlist:
4116 	evlist__delete(session->evlist);
4117 	session->evlist = NULL;
4118 	return -ENOMEM;
4119 }
4120 
4121 int perf_event__process_feature(struct perf_session *session,
4122 				union perf_event *event)
4123 {
4124 	struct perf_tool *tool = session->tool;
4125 	struct feat_fd ff = { .fd = 0 };
4126 	struct perf_record_header_feature *fe = (struct perf_record_header_feature *)event;
4127 	int type = fe->header.type;
4128 	u64 feat = fe->feat_id;
4129 	int ret = 0;
4130 
4131 	if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
4132 		pr_warning("invalid record type %d in pipe-mode\n", type);
4133 		return 0;
4134 	}
4135 	if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
4136 		pr_warning("invalid record type %d in pipe-mode\n", type);
4137 		return -1;
4138 	}
4139 
4140 	if (!feat_ops[feat].process)
4141 		return 0;
4142 
4143 	ff.buf  = (void *)fe->data;
4144 	ff.size = event->header.size - sizeof(*fe);
4145 	ff.ph = &session->header;
4146 
4147 	if (feat_ops[feat].process(&ff, NULL)) {
4148 		ret = -1;
4149 		goto out;
4150 	}
4151 
4152 	if (!feat_ops[feat].print || !tool->show_feat_hdr)
4153 		goto out;
4154 
4155 	if (!feat_ops[feat].full_only ||
4156 	    tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
4157 		feat_ops[feat].print(&ff, stdout);
4158 	} else {
4159 		fprintf(stdout, "# %s info available, use -I to display\n",
4160 			feat_ops[feat].name);
4161 	}
4162 out:
4163 	free_event_desc(ff.events);
4164 	return ret;
4165 }
4166 
4167 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
4168 {
4169 	struct perf_record_event_update *ev = &event->event_update;
4170 	struct perf_record_event_update_scale *ev_scale;
4171 	struct perf_record_event_update_cpus *ev_cpus;
4172 	struct perf_cpu_map *map;
4173 	size_t ret;
4174 
4175 	ret = fprintf(fp, "\n... id:    %" PRI_lu64 "\n", ev->id);
4176 
4177 	switch (ev->type) {
4178 	case PERF_EVENT_UPDATE__SCALE:
4179 		ev_scale = (struct perf_record_event_update_scale *)ev->data;
4180 		ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
4181 		break;
4182 	case PERF_EVENT_UPDATE__UNIT:
4183 		ret += fprintf(fp, "... unit:  %s\n", ev->data);
4184 		break;
4185 	case PERF_EVENT_UPDATE__NAME:
4186 		ret += fprintf(fp, "... name:  %s\n", ev->data);
4187 		break;
4188 	case PERF_EVENT_UPDATE__CPUS:
4189 		ev_cpus = (struct perf_record_event_update_cpus *)ev->data;
4190 		ret += fprintf(fp, "... ");
4191 
4192 		map = cpu_map__new_data(&ev_cpus->cpus);
4193 		if (map)
4194 			ret += cpu_map__fprintf(map, fp);
4195 		else
4196 			ret += fprintf(fp, "failed to get cpus\n");
4197 		break;
4198 	default:
4199 		ret += fprintf(fp, "... unknown type\n");
4200 		break;
4201 	}
4202 
4203 	return ret;
4204 }
4205 
4206 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
4207 			     union perf_event *event,
4208 			     struct evlist **pevlist)
4209 {
4210 	u32 i, ids, n_ids;
4211 	struct evsel *evsel;
4212 	struct evlist *evlist = *pevlist;
4213 
4214 	if (evlist == NULL) {
4215 		*pevlist = evlist = evlist__new();
4216 		if (evlist == NULL)
4217 			return -ENOMEM;
4218 	}
4219 
4220 	evsel = evsel__new(&event->attr.attr);
4221 	if (evsel == NULL)
4222 		return -ENOMEM;
4223 
4224 	evlist__add(evlist, evsel);
4225 
4226 	ids = event->header.size;
4227 	ids -= (void *)&event->attr.id - (void *)event;
4228 	n_ids = ids / sizeof(u64);
4229 	/*
4230 	 * We don't have the cpu and thread maps on the header, so
4231 	 * for allocating the perf_sample_id table we fake 1 cpu and
4232 	 * hattr->ids threads.
4233 	 */
4234 	if (perf_evsel__alloc_id(&evsel->core, 1, n_ids))
4235 		return -ENOMEM;
4236 
4237 	for (i = 0; i < n_ids; i++) {
4238 		perf_evlist__id_add(&evlist->core, &evsel->core, 0, i, event->attr.id[i]);
4239 	}
4240 
4241 	return 0;
4242 }
4243 
4244 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
4245 				     union perf_event *event,
4246 				     struct evlist **pevlist)
4247 {
4248 	struct perf_record_event_update *ev = &event->event_update;
4249 	struct perf_record_event_update_scale *ev_scale;
4250 	struct perf_record_event_update_cpus *ev_cpus;
4251 	struct evlist *evlist;
4252 	struct evsel *evsel;
4253 	struct perf_cpu_map *map;
4254 
4255 	if (!pevlist || *pevlist == NULL)
4256 		return -EINVAL;
4257 
4258 	evlist = *pevlist;
4259 
4260 	evsel = evlist__id2evsel(evlist, ev->id);
4261 	if (evsel == NULL)
4262 		return -EINVAL;
4263 
4264 	switch (ev->type) {
4265 	case PERF_EVENT_UPDATE__UNIT:
4266 		free((char *)evsel->unit);
4267 		evsel->unit = strdup(ev->data);
4268 		break;
4269 	case PERF_EVENT_UPDATE__NAME:
4270 		free(evsel->name);
4271 		evsel->name = strdup(ev->data);
4272 		break;
4273 	case PERF_EVENT_UPDATE__SCALE:
4274 		ev_scale = (struct perf_record_event_update_scale *)ev->data;
4275 		evsel->scale = ev_scale->scale;
4276 		break;
4277 	case PERF_EVENT_UPDATE__CPUS:
4278 		ev_cpus = (struct perf_record_event_update_cpus *)ev->data;
4279 		map = cpu_map__new_data(&ev_cpus->cpus);
4280 		if (map) {
4281 			perf_cpu_map__put(evsel->core.own_cpus);
4282 			evsel->core.own_cpus = map;
4283 		} else
4284 			pr_err("failed to get event_update cpus\n");
4285 	default:
4286 		break;
4287 	}
4288 
4289 	return 0;
4290 }
4291 
4292 int perf_event__process_tracing_data(struct perf_session *session,
4293 				     union perf_event *event)
4294 {
4295 	ssize_t size_read, padding, size = event->tracing_data.size;
4296 	int fd = perf_data__fd(session->data);
4297 	char buf[BUFSIZ];
4298 
4299 	/*
4300 	 * The pipe fd is already in proper place and in any case
4301 	 * we can't move it, and we'd screw the case where we read
4302 	 * 'pipe' data from regular file. The trace_report reads
4303 	 * data from 'fd' so we need to set it directly behind the
4304 	 * event, where the tracing data starts.
4305 	 */
4306 	if (!perf_data__is_pipe(session->data)) {
4307 		off_t offset = lseek(fd, 0, SEEK_CUR);
4308 
4309 		/* setup for reading amidst mmap */
4310 		lseek(fd, offset + sizeof(struct perf_record_header_tracing_data),
4311 		      SEEK_SET);
4312 	}
4313 
4314 	size_read = trace_report(fd, &session->tevent,
4315 				 session->repipe);
4316 	padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
4317 
4318 	if (readn(fd, buf, padding) < 0) {
4319 		pr_err("%s: reading input file", __func__);
4320 		return -1;
4321 	}
4322 	if (session->repipe) {
4323 		int retw = write(STDOUT_FILENO, buf, padding);
4324 		if (retw <= 0 || retw != padding) {
4325 			pr_err("%s: repiping tracing data padding", __func__);
4326 			return -1;
4327 		}
4328 	}
4329 
4330 	if (size_read + padding != size) {
4331 		pr_err("%s: tracing data size mismatch", __func__);
4332 		return -1;
4333 	}
4334 
4335 	evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent);
4336 
4337 	return size_read + padding;
4338 }
4339 
4340 int perf_event__process_build_id(struct perf_session *session,
4341 				 union perf_event *event)
4342 {
4343 	__event_process_build_id(&event->build_id,
4344 				 event->build_id.filename,
4345 				 session);
4346 	return 0;
4347 }
4348