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