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