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