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