1#include "util.h"
2#include <sys/types.h>
3#include <byteswap.h>
4#include <unistd.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <linux/list.h>
8#include <linux/kernel.h>
9#include <linux/bitops.h>
10#include <sys/utsname.h>
11
12#include "evlist.h"
13#include "evsel.h"
14#include "header.h"
15#include "../perf.h"
16#include "trace-event.h"
17#include "session.h"
18#include "symbol.h"
19#include "debug.h"
20#include "cpumap.h"
21#include "pmu.h"
22#include "vdso.h"
23#include "strbuf.h"
24#include "build-id.h"
25#include "data.h"
26
27static u32 header_argc;
28static const char **header_argv;
29
30/*
31 * magic2 = "PERFILE2"
32 * must be a numerical value to let the endianness
33 * determine the memory layout. That way we are able
34 * to detect endianness when reading the perf.data file
35 * back.
36 *
37 * we check for legacy (PERFFILE) format.
38 */
39static const char *__perf_magic1 = "PERFFILE";
40static const u64 __perf_magic2    = 0x32454c4946524550ULL;
41static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
42
43#define PERF_MAGIC	__perf_magic2
44
45struct perf_file_attr {
46	struct perf_event_attr	attr;
47	struct perf_file_section	ids;
48};
49
50void perf_header__set_feat(struct perf_header *header, int feat)
51{
52	set_bit(feat, header->adds_features);
53}
54
55void perf_header__clear_feat(struct perf_header *header, int feat)
56{
57	clear_bit(feat, header->adds_features);
58}
59
60bool perf_header__has_feat(const struct perf_header *header, int feat)
61{
62	return test_bit(feat, header->adds_features);
63}
64
65static int do_write(int fd, const void *buf, size_t size)
66{
67	while (size) {
68		int ret = write(fd, buf, size);
69
70		if (ret < 0)
71			return -errno;
72
73		size -= ret;
74		buf += ret;
75	}
76
77	return 0;
78}
79
80int write_padded(int fd, const void *bf, size_t count, size_t count_aligned)
81{
82	static const char zero_buf[NAME_ALIGN];
83	int err = do_write(fd, bf, count);
84
85	if (!err)
86		err = do_write(fd, zero_buf, count_aligned - count);
87
88	return err;
89}
90
91static int do_write_string(int fd, const char *str)
92{
93	u32 len, olen;
94	int ret;
95
96	olen = strlen(str) + 1;
97	len = PERF_ALIGN(olen, NAME_ALIGN);
98
99	/* write len, incl. \0 */
100	ret = do_write(fd, &len, sizeof(len));
101	if (ret < 0)
102		return ret;
103
104	return write_padded(fd, str, olen, len);
105}
106
107static char *do_read_string(int fd, struct perf_header *ph)
108{
109	ssize_t sz, ret;
110	u32 len;
111	char *buf;
112
113	sz = readn(fd, &len, sizeof(len));
114	if (sz < (ssize_t)sizeof(len))
115		return NULL;
116
117	if (ph->needs_swap)
118		len = bswap_32(len);
119
120	buf = malloc(len);
121	if (!buf)
122		return NULL;
123
124	ret = readn(fd, buf, len);
125	if (ret == (ssize_t)len) {
126		/*
127		 * strings are padded by zeroes
128		 * thus the actual strlen of buf
129		 * may be less than len
130		 */
131		return buf;
132	}
133
134	free(buf);
135	return NULL;
136}
137
138int
139perf_header__set_cmdline(int argc, const char **argv)
140{
141	int i;
142
143	/*
144	 * If header_argv has already been set, do not override it.
145	 * This allows a command to set the cmdline, parse args and
146	 * then call another builtin function that implements a
147	 * command -- e.g, cmd_kvm calling cmd_record.
148	 */
149	if (header_argv)
150		return 0;
151
152	header_argc = (u32)argc;
153
154	/* do not include NULL termination */
155	header_argv = calloc(argc, sizeof(char *));
156	if (!header_argv)
157		return -ENOMEM;
158
159	/*
160	 * must copy argv contents because it gets moved
161	 * around during option parsing
162	 */
163	for (i = 0; i < argc ; i++)
164		header_argv[i] = argv[i];
165
166	return 0;
167}
168
169static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
170			    struct perf_evlist *evlist)
171{
172	return read_tracing_data(fd, &evlist->entries);
173}
174
175
176static int write_build_id(int fd, struct perf_header *h,
177			  struct perf_evlist *evlist __maybe_unused)
178{
179	struct perf_session *session;
180	int err;
181
182	session = container_of(h, struct perf_session, header);
183
184	if (!perf_session__read_build_ids(session, true))
185		return -1;
186
187	err = perf_session__write_buildid_table(session, fd);
188	if (err < 0) {
189		pr_debug("failed to write buildid table\n");
190		return err;
191	}
192	perf_session__cache_build_ids(session);
193
194	return 0;
195}
196
197static int write_hostname(int fd, struct perf_header *h __maybe_unused,
198			  struct perf_evlist *evlist __maybe_unused)
199{
200	struct utsname uts;
201	int ret;
202
203	ret = uname(&uts);
204	if (ret < 0)
205		return -1;
206
207	return do_write_string(fd, uts.nodename);
208}
209
210static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
211			   struct perf_evlist *evlist __maybe_unused)
212{
213	struct utsname uts;
214	int ret;
215
216	ret = uname(&uts);
217	if (ret < 0)
218		return -1;
219
220	return do_write_string(fd, uts.release);
221}
222
223static int write_arch(int fd, struct perf_header *h __maybe_unused,
224		      struct perf_evlist *evlist __maybe_unused)
225{
226	struct utsname uts;
227	int ret;
228
229	ret = uname(&uts);
230	if (ret < 0)
231		return -1;
232
233	return do_write_string(fd, uts.machine);
234}
235
236static int write_version(int fd, struct perf_header *h __maybe_unused,
237			 struct perf_evlist *evlist __maybe_unused)
238{
239	return do_write_string(fd, perf_version_string);
240}
241
242static int __write_cpudesc(int fd, const char *cpuinfo_proc)
243{
244	FILE *file;
245	char *buf = NULL;
246	char *s, *p;
247	const char *search = cpuinfo_proc;
248	size_t len = 0;
249	int ret = -1;
250
251	if (!search)
252		return -1;
253
254	file = fopen("/proc/cpuinfo", "r");
255	if (!file)
256		return -1;
257
258	while (getline(&buf, &len, file) > 0) {
259		ret = strncmp(buf, search, strlen(search));
260		if (!ret)
261			break;
262	}
263
264	if (ret) {
265		ret = -1;
266		goto done;
267	}
268
269	s = buf;
270
271	p = strchr(buf, ':');
272	if (p && *(p+1) == ' ' && *(p+2))
273		s = p + 2;
274	p = strchr(s, '\n');
275	if (p)
276		*p = '\0';
277
278	/* squash extra space characters (branding string) */
279	p = s;
280	while (*p) {
281		if (isspace(*p)) {
282			char *r = p + 1;
283			char *q = r;
284			*p = ' ';
285			while (*q && isspace(*q))
286				q++;
287			if (q != (p+1))
288				while ((*r++ = *q++));
289		}
290		p++;
291	}
292	ret = do_write_string(fd, s);
293done:
294	free(buf);
295	fclose(file);
296	return ret;
297}
298
299static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
300		       struct perf_evlist *evlist __maybe_unused)
301{
302#ifndef CPUINFO_PROC
303#define CPUINFO_PROC {"model name", }
304#endif
305	const char *cpuinfo_procs[] = CPUINFO_PROC;
306	unsigned int i;
307
308	for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
309		int ret;
310		ret = __write_cpudesc(fd, cpuinfo_procs[i]);
311		if (ret >= 0)
312			return ret;
313	}
314	return -1;
315}
316
317
318static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
319			struct perf_evlist *evlist __maybe_unused)
320{
321	long nr;
322	u32 nrc, nra;
323	int ret;
324
325	nr = sysconf(_SC_NPROCESSORS_CONF);
326	if (nr < 0)
327		return -1;
328
329	nrc = (u32)(nr & UINT_MAX);
330
331	nr = sysconf(_SC_NPROCESSORS_ONLN);
332	if (nr < 0)
333		return -1;
334
335	nra = (u32)(nr & UINT_MAX);
336
337	ret = do_write(fd, &nrc, sizeof(nrc));
338	if (ret < 0)
339		return ret;
340
341	return do_write(fd, &nra, sizeof(nra));
342}
343
344static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
345			    struct perf_evlist *evlist)
346{
347	struct perf_evsel *evsel;
348	u32 nre, nri, sz;
349	int ret;
350
351	nre = evlist->nr_entries;
352
353	/*
354	 * write number of events
355	 */
356	ret = do_write(fd, &nre, sizeof(nre));
357	if (ret < 0)
358		return ret;
359
360	/*
361	 * size of perf_event_attr struct
362	 */
363	sz = (u32)sizeof(evsel->attr);
364	ret = do_write(fd, &sz, sizeof(sz));
365	if (ret < 0)
366		return ret;
367
368	evlist__for_each(evlist, evsel) {
369		ret = do_write(fd, &evsel->attr, sz);
370		if (ret < 0)
371			return ret;
372		/*
373		 * write number of unique id per event
374		 * there is one id per instance of an event
375		 *
376		 * copy into an nri to be independent of the
377		 * type of ids,
378		 */
379		nri = evsel->ids;
380		ret = do_write(fd, &nri, sizeof(nri));
381		if (ret < 0)
382			return ret;
383
384		/*
385		 * write event string as passed on cmdline
386		 */
387		ret = do_write_string(fd, perf_evsel__name(evsel));
388		if (ret < 0)
389			return ret;
390		/*
391		 * write unique ids for this event
392		 */
393		ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
394		if (ret < 0)
395			return ret;
396	}
397	return 0;
398}
399
400static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
401			 struct perf_evlist *evlist __maybe_unused)
402{
403	char buf[MAXPATHLEN];
404	char proc[32];
405	u32 i, n;
406	int ret;
407
408	/*
409	 * actual atual path to perf binary
410	 */
411	sprintf(proc, "/proc/%d/exe", getpid());
412	ret = readlink(proc, buf, sizeof(buf));
413	if (ret <= 0)
414		return -1;
415
416	/* readlink() does not add null termination */
417	buf[ret] = '\0';
418
419	/* account for binary path */
420	n = header_argc + 1;
421
422	ret = do_write(fd, &n, sizeof(n));
423	if (ret < 0)
424		return ret;
425
426	ret = do_write_string(fd, buf);
427	if (ret < 0)
428		return ret;
429
430	for (i = 0 ; i < header_argc; i++) {
431		ret = do_write_string(fd, header_argv[i]);
432		if (ret < 0)
433			return ret;
434	}
435	return 0;
436}
437
438#define CORE_SIB_FMT \
439	"/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
440#define THRD_SIB_FMT \
441	"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
442
443struct cpu_topo {
444	u32 core_sib;
445	u32 thread_sib;
446	char **core_siblings;
447	char **thread_siblings;
448};
449
450static int build_cpu_topo(struct cpu_topo *tp, int cpu)
451{
452	FILE *fp;
453	char filename[MAXPATHLEN];
454	char *buf = NULL, *p;
455	size_t len = 0;
456	ssize_t sret;
457	u32 i = 0;
458	int ret = -1;
459
460	sprintf(filename, CORE_SIB_FMT, cpu);
461	fp = fopen(filename, "r");
462	if (!fp)
463		goto try_threads;
464
465	sret = getline(&buf, &len, fp);
466	fclose(fp);
467	if (sret <= 0)
468		goto try_threads;
469
470	p = strchr(buf, '\n');
471	if (p)
472		*p = '\0';
473
474	for (i = 0; i < tp->core_sib; i++) {
475		if (!strcmp(buf, tp->core_siblings[i]))
476			break;
477	}
478	if (i == tp->core_sib) {
479		tp->core_siblings[i] = buf;
480		tp->core_sib++;
481		buf = NULL;
482		len = 0;
483	}
484	ret = 0;
485
486try_threads:
487	sprintf(filename, THRD_SIB_FMT, cpu);
488	fp = fopen(filename, "r");
489	if (!fp)
490		goto done;
491
492	if (getline(&buf, &len, fp) <= 0)
493		goto done;
494
495	p = strchr(buf, '\n');
496	if (p)
497		*p = '\0';
498
499	for (i = 0; i < tp->thread_sib; i++) {
500		if (!strcmp(buf, tp->thread_siblings[i]))
501			break;
502	}
503	if (i == tp->thread_sib) {
504		tp->thread_siblings[i] = buf;
505		tp->thread_sib++;
506		buf = NULL;
507	}
508	ret = 0;
509done:
510	if(fp)
511		fclose(fp);
512	free(buf);
513	return ret;
514}
515
516static void free_cpu_topo(struct cpu_topo *tp)
517{
518	u32 i;
519
520	if (!tp)
521		return;
522
523	for (i = 0 ; i < tp->core_sib; i++)
524		zfree(&tp->core_siblings[i]);
525
526	for (i = 0 ; i < tp->thread_sib; i++)
527		zfree(&tp->thread_siblings[i]);
528
529	free(tp);
530}
531
532static struct cpu_topo *build_cpu_topology(void)
533{
534	struct cpu_topo *tp;
535	void *addr;
536	u32 nr, i;
537	size_t sz;
538	long ncpus;
539	int ret = -1;
540
541	ncpus = sysconf(_SC_NPROCESSORS_CONF);
542	if (ncpus < 0)
543		return NULL;
544
545	nr = (u32)(ncpus & UINT_MAX);
546
547	sz = nr * sizeof(char *);
548
549	addr = calloc(1, sizeof(*tp) + 2 * sz);
550	if (!addr)
551		return NULL;
552
553	tp = addr;
554
555	addr += sizeof(*tp);
556	tp->core_siblings = addr;
557	addr += sz;
558	tp->thread_siblings = addr;
559
560	for (i = 0; i < nr; i++) {
561		ret = build_cpu_topo(tp, i);
562		if (ret < 0)
563			break;
564	}
565	if (ret) {
566		free_cpu_topo(tp);
567		tp = NULL;
568	}
569	return tp;
570}
571
572static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
573			  struct perf_evlist *evlist __maybe_unused)
574{
575	struct cpu_topo *tp;
576	u32 i;
577	int ret;
578
579	tp = build_cpu_topology();
580	if (!tp)
581		return -1;
582
583	ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
584	if (ret < 0)
585		goto done;
586
587	for (i = 0; i < tp->core_sib; i++) {
588		ret = do_write_string(fd, tp->core_siblings[i]);
589		if (ret < 0)
590			goto done;
591	}
592	ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
593	if (ret < 0)
594		goto done;
595
596	for (i = 0; i < tp->thread_sib; i++) {
597		ret = do_write_string(fd, tp->thread_siblings[i]);
598		if (ret < 0)
599			break;
600	}
601done:
602	free_cpu_topo(tp);
603	return ret;
604}
605
606
607
608static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
609			  struct perf_evlist *evlist __maybe_unused)
610{
611	char *buf = NULL;
612	FILE *fp;
613	size_t len = 0;
614	int ret = -1, n;
615	uint64_t mem;
616
617	fp = fopen("/proc/meminfo", "r");
618	if (!fp)
619		return -1;
620
621	while (getline(&buf, &len, fp) > 0) {
622		ret = strncmp(buf, "MemTotal:", 9);
623		if (!ret)
624			break;
625	}
626	if (!ret) {
627		n = sscanf(buf, "%*s %"PRIu64, &mem);
628		if (n == 1)
629			ret = do_write(fd, &mem, sizeof(mem));
630	} else
631		ret = -1;
632	free(buf);
633	fclose(fp);
634	return ret;
635}
636
637static int write_topo_node(int fd, int node)
638{
639	char str[MAXPATHLEN];
640	char field[32];
641	char *buf = NULL, *p;
642	size_t len = 0;
643	FILE *fp;
644	u64 mem_total, mem_free, mem;
645	int ret = -1;
646
647	sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
648	fp = fopen(str, "r");
649	if (!fp)
650		return -1;
651
652	while (getline(&buf, &len, fp) > 0) {
653		/* skip over invalid lines */
654		if (!strchr(buf, ':'))
655			continue;
656		if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
657			goto done;
658		if (!strcmp(field, "MemTotal:"))
659			mem_total = mem;
660		if (!strcmp(field, "MemFree:"))
661			mem_free = mem;
662	}
663
664	fclose(fp);
665	fp = NULL;
666
667	ret = do_write(fd, &mem_total, sizeof(u64));
668	if (ret)
669		goto done;
670
671	ret = do_write(fd, &mem_free, sizeof(u64));
672	if (ret)
673		goto done;
674
675	ret = -1;
676	sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
677
678	fp = fopen(str, "r");
679	if (!fp)
680		goto done;
681
682	if (getline(&buf, &len, fp) <= 0)
683		goto done;
684
685	p = strchr(buf, '\n');
686	if (p)
687		*p = '\0';
688
689	ret = do_write_string(fd, buf);
690done:
691	free(buf);
692	if (fp)
693		fclose(fp);
694	return ret;
695}
696
697static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
698			  struct perf_evlist *evlist __maybe_unused)
699{
700	char *buf = NULL;
701	size_t len = 0;
702	FILE *fp;
703	struct cpu_map *node_map = NULL;
704	char *c;
705	u32 nr, i, j;
706	int ret = -1;
707
708	fp = fopen("/sys/devices/system/node/online", "r");
709	if (!fp)
710		return -1;
711
712	if (getline(&buf, &len, fp) <= 0)
713		goto done;
714
715	c = strchr(buf, '\n');
716	if (c)
717		*c = '\0';
718
719	node_map = cpu_map__new(buf);
720	if (!node_map)
721		goto done;
722
723	nr = (u32)node_map->nr;
724
725	ret = do_write(fd, &nr, sizeof(nr));
726	if (ret < 0)
727		goto done;
728
729	for (i = 0; i < nr; i++) {
730		j = (u32)node_map->map[i];
731		ret = do_write(fd, &j, sizeof(j));
732		if (ret < 0)
733			break;
734
735		ret = write_topo_node(fd, i);
736		if (ret < 0)
737			break;
738	}
739done:
740	free(buf);
741	fclose(fp);
742	free(node_map);
743	return ret;
744}
745
746/*
747 * File format:
748 *
749 * struct pmu_mappings {
750 *	u32	pmu_num;
751 *	struct pmu_map {
752 *		u32	type;
753 *		char	name[];
754 *	}[pmu_num];
755 * };
756 */
757
758static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
759			      struct perf_evlist *evlist __maybe_unused)
760{
761	struct perf_pmu *pmu = NULL;
762	off_t offset = lseek(fd, 0, SEEK_CUR);
763	__u32 pmu_num = 0;
764	int ret;
765
766	/* write real pmu_num later */
767	ret = do_write(fd, &pmu_num, sizeof(pmu_num));
768	if (ret < 0)
769		return ret;
770
771	while ((pmu = perf_pmu__scan(pmu))) {
772		if (!pmu->name)
773			continue;
774		pmu_num++;
775
776		ret = do_write(fd, &pmu->type, sizeof(pmu->type));
777		if (ret < 0)
778			return ret;
779
780		ret = do_write_string(fd, pmu->name);
781		if (ret < 0)
782			return ret;
783	}
784
785	if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
786		/* discard all */
787		lseek(fd, offset, SEEK_SET);
788		return -1;
789	}
790
791	return 0;
792}
793
794/*
795 * File format:
796 *
797 * struct group_descs {
798 *	u32	nr_groups;
799 *	struct group_desc {
800 *		char	name[];
801 *		u32	leader_idx;
802 *		u32	nr_members;
803 *	}[nr_groups];
804 * };
805 */
806static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
807			    struct perf_evlist *evlist)
808{
809	u32 nr_groups = evlist->nr_groups;
810	struct perf_evsel *evsel;
811	int ret;
812
813	ret = do_write(fd, &nr_groups, sizeof(nr_groups));
814	if (ret < 0)
815		return ret;
816
817	evlist__for_each(evlist, evsel) {
818		if (perf_evsel__is_group_leader(evsel) &&
819		    evsel->nr_members > 1) {
820			const char *name = evsel->group_name ?: "{anon_group}";
821			u32 leader_idx = evsel->idx;
822			u32 nr_members = evsel->nr_members;
823
824			ret = do_write_string(fd, name);
825			if (ret < 0)
826				return ret;
827
828			ret = do_write(fd, &leader_idx, sizeof(leader_idx));
829			if (ret < 0)
830				return ret;
831
832			ret = do_write(fd, &nr_members, sizeof(nr_members));
833			if (ret < 0)
834				return ret;
835		}
836	}
837	return 0;
838}
839
840/*
841 * default get_cpuid(): nothing gets recorded
842 * actual implementation must be in arch/$(ARCH)/util/header.c
843 */
844int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
845				     size_t sz __maybe_unused)
846{
847	return -1;
848}
849
850static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
851		       struct perf_evlist *evlist __maybe_unused)
852{
853	char buffer[64];
854	int ret;
855
856	ret = get_cpuid(buffer, sizeof(buffer));
857	if (!ret)
858		goto write_it;
859
860	return -1;
861write_it:
862	return do_write_string(fd, buffer);
863}
864
865static int write_branch_stack(int fd __maybe_unused,
866			      struct perf_header *h __maybe_unused,
867		       struct perf_evlist *evlist __maybe_unused)
868{
869	return 0;
870}
871
872static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
873			   FILE *fp)
874{
875	fprintf(fp, "# hostname : %s\n", ph->env.hostname);
876}
877
878static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
879			    FILE *fp)
880{
881	fprintf(fp, "# os release : %s\n", ph->env.os_release);
882}
883
884static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
885{
886	fprintf(fp, "# arch : %s\n", ph->env.arch);
887}
888
889static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
890			  FILE *fp)
891{
892	fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
893}
894
895static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
896			 FILE *fp)
897{
898	fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
899	fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
900}
901
902static void print_version(struct perf_header *ph, int fd __maybe_unused,
903			  FILE *fp)
904{
905	fprintf(fp, "# perf version : %s\n", ph->env.version);
906}
907
908static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
909			  FILE *fp)
910{
911	int nr, i;
912	char *str;
913
914	nr = ph->env.nr_cmdline;
915	str = ph->env.cmdline;
916
917	fprintf(fp, "# cmdline : ");
918
919	for (i = 0; i < nr; i++) {
920		fprintf(fp, "%s ", str);
921		str += strlen(str) + 1;
922	}
923	fputc('\n', fp);
924}
925
926static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
927			       FILE *fp)
928{
929	int nr, i;
930	char *str;
931
932	nr = ph->env.nr_sibling_cores;
933	str = ph->env.sibling_cores;
934
935	for (i = 0; i < nr; i++) {
936		fprintf(fp, "# sibling cores   : %s\n", str);
937		str += strlen(str) + 1;
938	}
939
940	nr = ph->env.nr_sibling_threads;
941	str = ph->env.sibling_threads;
942
943	for (i = 0; i < nr; i++) {
944		fprintf(fp, "# sibling threads : %s\n", str);
945		str += strlen(str) + 1;
946	}
947}
948
949static void free_event_desc(struct perf_evsel *events)
950{
951	struct perf_evsel *evsel;
952
953	if (!events)
954		return;
955
956	for (evsel = events; evsel->attr.size; evsel++) {
957		zfree(&evsel->name);
958		zfree(&evsel->id);
959	}
960
961	free(events);
962}
963
964static struct perf_evsel *
965read_event_desc(struct perf_header *ph, int fd)
966{
967	struct perf_evsel *evsel, *events = NULL;
968	u64 *id;
969	void *buf = NULL;
970	u32 nre, sz, nr, i, j;
971	ssize_t ret;
972	size_t msz;
973
974	/* number of events */
975	ret = readn(fd, &nre, sizeof(nre));
976	if (ret != (ssize_t)sizeof(nre))
977		goto error;
978
979	if (ph->needs_swap)
980		nre = bswap_32(nre);
981
982	ret = readn(fd, &sz, sizeof(sz));
983	if (ret != (ssize_t)sizeof(sz))
984		goto error;
985
986	if (ph->needs_swap)
987		sz = bswap_32(sz);
988
989	/* buffer to hold on file attr struct */
990	buf = malloc(sz);
991	if (!buf)
992		goto error;
993
994	/* the last event terminates with evsel->attr.size == 0: */
995	events = calloc(nre + 1, sizeof(*events));
996	if (!events)
997		goto error;
998
999	msz = sizeof(evsel->attr);
1000	if (sz < msz)
1001		msz = sz;
1002
1003	for (i = 0, evsel = events; i < nre; evsel++, i++) {
1004		evsel->idx = i;
1005
1006		/*
1007		 * must read entire on-file attr struct to
1008		 * sync up with layout.
1009		 */
1010		ret = readn(fd, buf, sz);
1011		if (ret != (ssize_t)sz)
1012			goto error;
1013
1014		if (ph->needs_swap)
1015			perf_event__attr_swap(buf);
1016
1017		memcpy(&evsel->attr, buf, msz);
1018
1019		ret = readn(fd, &nr, sizeof(nr));
1020		if (ret != (ssize_t)sizeof(nr))
1021			goto error;
1022
1023		if (ph->needs_swap) {
1024			nr = bswap_32(nr);
1025			evsel->needs_swap = true;
1026		}
1027
1028		evsel->name = do_read_string(fd, ph);
1029
1030		if (!nr)
1031			continue;
1032
1033		id = calloc(nr, sizeof(*id));
1034		if (!id)
1035			goto error;
1036		evsel->ids = nr;
1037		evsel->id = id;
1038
1039		for (j = 0 ; j < nr; j++) {
1040			ret = readn(fd, id, sizeof(*id));
1041			if (ret != (ssize_t)sizeof(*id))
1042				goto error;
1043			if (ph->needs_swap)
1044				*id = bswap_64(*id);
1045			id++;
1046		}
1047	}
1048out:
1049	free(buf);
1050	return events;
1051error:
1052	if (events)
1053		free_event_desc(events);
1054	events = NULL;
1055	goto out;
1056}
1057
1058static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1059				void *priv __attribute__((unused)))
1060{
1061	return fprintf(fp, ", %s = %s", name, val);
1062}
1063
1064static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1065{
1066	struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1067	u32 j;
1068	u64 *id;
1069
1070	if (!events) {
1071		fprintf(fp, "# event desc: not available or unable to read\n");
1072		return;
1073	}
1074
1075	for (evsel = events; evsel->attr.size; evsel++) {
1076		fprintf(fp, "# event : name = %s, ", evsel->name);
1077
1078		if (evsel->ids) {
1079			fprintf(fp, ", id = {");
1080			for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1081				if (j)
1082					fputc(',', fp);
1083				fprintf(fp, " %"PRIu64, *id);
1084			}
1085			fprintf(fp, " }");
1086		}
1087
1088		perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
1089
1090		fputc('\n', fp);
1091	}
1092
1093	free_event_desc(events);
1094}
1095
1096static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
1097			    FILE *fp)
1098{
1099	fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
1100}
1101
1102static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
1103				FILE *fp)
1104{
1105	u32 nr, c, i;
1106	char *str, *tmp;
1107	uint64_t mem_total, mem_free;
1108
1109	/* nr nodes */
1110	nr = ph->env.nr_numa_nodes;
1111	str = ph->env.numa_nodes;
1112
1113	for (i = 0; i < nr; i++) {
1114		/* node number */
1115		c = strtoul(str, &tmp, 0);
1116		if (*tmp != ':')
1117			goto error;
1118
1119		str = tmp + 1;
1120		mem_total = strtoull(str, &tmp, 0);
1121		if (*tmp != ':')
1122			goto error;
1123
1124		str = tmp + 1;
1125		mem_free = strtoull(str, &tmp, 0);
1126		if (*tmp != ':')
1127			goto error;
1128
1129		fprintf(fp, "# node%u meminfo  : total = %"PRIu64" kB,"
1130			    " free = %"PRIu64" kB\n",
1131			c, mem_total, mem_free);
1132
1133		str = tmp + 1;
1134		fprintf(fp, "# node%u cpu list : %s\n", c, str);
1135
1136		str += strlen(str) + 1;
1137	}
1138	return;
1139error:
1140	fprintf(fp, "# numa topology : not available\n");
1141}
1142
1143static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1144{
1145	fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
1146}
1147
1148static void print_branch_stack(struct perf_header *ph __maybe_unused,
1149			       int fd __maybe_unused, FILE *fp)
1150{
1151	fprintf(fp, "# contains samples with branch stack\n");
1152}
1153
1154static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1155			       FILE *fp)
1156{
1157	const char *delimiter = "# pmu mappings: ";
1158	char *str, *tmp;
1159	u32 pmu_num;
1160	u32 type;
1161
1162	pmu_num = ph->env.nr_pmu_mappings;
1163	if (!pmu_num) {
1164		fprintf(fp, "# pmu mappings: not available\n");
1165		return;
1166	}
1167
1168	str = ph->env.pmu_mappings;
1169
1170	while (pmu_num) {
1171		type = strtoul(str, &tmp, 0);
1172		if (*tmp != ':')
1173			goto error;
1174
1175		str = tmp + 1;
1176		fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1177
1178		delimiter = ", ";
1179		str += strlen(str) + 1;
1180		pmu_num--;
1181	}
1182
1183	fprintf(fp, "\n");
1184
1185	if (!pmu_num)
1186		return;
1187error:
1188	fprintf(fp, "# pmu mappings: unable to read\n");
1189}
1190
1191static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1192			     FILE *fp)
1193{
1194	struct perf_session *session;
1195	struct perf_evsel *evsel;
1196	u32 nr = 0;
1197
1198	session = container_of(ph, struct perf_session, header);
1199
1200	evlist__for_each(session->evlist, evsel) {
1201		if (perf_evsel__is_group_leader(evsel) &&
1202		    evsel->nr_members > 1) {
1203			fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1204				perf_evsel__name(evsel));
1205
1206			nr = evsel->nr_members - 1;
1207		} else if (nr) {
1208			fprintf(fp, ",%s", perf_evsel__name(evsel));
1209
1210			if (--nr == 0)
1211				fprintf(fp, "}\n");
1212		}
1213	}
1214}
1215
1216static int __event_process_build_id(struct build_id_event *bev,
1217				    char *filename,
1218				    struct perf_session *session)
1219{
1220	int err = -1;
1221	struct dsos *dsos;
1222	struct machine *machine;
1223	u16 misc;
1224	struct dso *dso;
1225	enum dso_kernel_type dso_type;
1226
1227	machine = perf_session__findnew_machine(session, bev->pid);
1228	if (!machine)
1229		goto out;
1230
1231	misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1232
1233	switch (misc) {
1234	case PERF_RECORD_MISC_KERNEL:
1235		dso_type = DSO_TYPE_KERNEL;
1236		dsos = &machine->kernel_dsos;
1237		break;
1238	case PERF_RECORD_MISC_GUEST_KERNEL:
1239		dso_type = DSO_TYPE_GUEST_KERNEL;
1240		dsos = &machine->kernel_dsos;
1241		break;
1242	case PERF_RECORD_MISC_USER:
1243	case PERF_RECORD_MISC_GUEST_USER:
1244		dso_type = DSO_TYPE_USER;
1245		dsos = &machine->user_dsos;
1246		break;
1247	default:
1248		goto out;
1249	}
1250
1251	dso = __dsos__findnew(dsos, filename);
1252	if (dso != NULL) {
1253		char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1254
1255		dso__set_build_id(dso, &bev->build_id);
1256
1257		if (!is_kernel_module(filename))
1258			dso->kernel = dso_type;
1259
1260		build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1261				  sbuild_id);
1262		pr_debug("build id event received for %s: %s\n",
1263			 dso->long_name, sbuild_id);
1264	}
1265
1266	err = 0;
1267out:
1268	return err;
1269}
1270
1271static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1272						 int input, u64 offset, u64 size)
1273{
1274	struct perf_session *session = container_of(header, struct perf_session, header);
1275	struct {
1276		struct perf_event_header   header;
1277		u8			   build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1278		char			   filename[0];
1279	} old_bev;
1280	struct build_id_event bev;
1281	char filename[PATH_MAX];
1282	u64 limit = offset + size;
1283
1284	while (offset < limit) {
1285		ssize_t len;
1286
1287		if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1288			return -1;
1289
1290		if (header->needs_swap)
1291			perf_event_header__bswap(&old_bev.header);
1292
1293		len = old_bev.header.size - sizeof(old_bev);
1294		if (readn(input, filename, len) != len)
1295			return -1;
1296
1297		bev.header = old_bev.header;
1298
1299		/*
1300		 * As the pid is the missing value, we need to fill
1301		 * it properly. The header.misc value give us nice hint.
1302		 */
1303		bev.pid	= HOST_KERNEL_ID;
1304		if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1305		    bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1306			bev.pid	= DEFAULT_GUEST_KERNEL_ID;
1307
1308		memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1309		__event_process_build_id(&bev, filename, session);
1310
1311		offset += bev.header.size;
1312	}
1313
1314	return 0;
1315}
1316
1317static int perf_header__read_build_ids(struct perf_header *header,
1318				       int input, u64 offset, u64 size)
1319{
1320	struct perf_session *session = container_of(header, struct perf_session, header);
1321	struct build_id_event bev;
1322	char filename[PATH_MAX];
1323	u64 limit = offset + size, orig_offset = offset;
1324	int err = -1;
1325
1326	while (offset < limit) {
1327		ssize_t len;
1328
1329		if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1330			goto out;
1331
1332		if (header->needs_swap)
1333			perf_event_header__bswap(&bev.header);
1334
1335		len = bev.header.size - sizeof(bev);
1336		if (readn(input, filename, len) != len)
1337			goto out;
1338		/*
1339		 * The a1645ce1 changeset:
1340		 *
1341		 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1342		 *
1343		 * Added a field to struct build_id_event that broke the file
1344		 * format.
1345		 *
1346		 * Since the kernel build-id is the first entry, process the
1347		 * table using the old format if the well known
1348		 * '[kernel.kallsyms]' string for the kernel build-id has the
1349		 * first 4 characters chopped off (where the pid_t sits).
1350		 */
1351		if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1352			if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1353				return -1;
1354			return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1355		}
1356
1357		__event_process_build_id(&bev, filename, session);
1358
1359		offset += bev.header.size;
1360	}
1361	err = 0;
1362out:
1363	return err;
1364}
1365
1366static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1367				struct perf_header *ph __maybe_unused,
1368				int fd, void *data)
1369{
1370	ssize_t ret = trace_report(fd, data, false);
1371	return ret < 0 ? -1 : 0;
1372}
1373
1374static int process_build_id(struct perf_file_section *section,
1375			    struct perf_header *ph, int fd,
1376			    void *data __maybe_unused)
1377{
1378	if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1379		pr_debug("Failed to read buildids, continuing...\n");
1380	return 0;
1381}
1382
1383static int process_hostname(struct perf_file_section *section __maybe_unused,
1384			    struct perf_header *ph, int fd,
1385			    void *data __maybe_unused)
1386{
1387	ph->env.hostname = do_read_string(fd, ph);
1388	return ph->env.hostname ? 0 : -ENOMEM;
1389}
1390
1391static int process_osrelease(struct perf_file_section *section __maybe_unused,
1392			     struct perf_header *ph, int fd,
1393			     void *data __maybe_unused)
1394{
1395	ph->env.os_release = do_read_string(fd, ph);
1396	return ph->env.os_release ? 0 : -ENOMEM;
1397}
1398
1399static int process_version(struct perf_file_section *section __maybe_unused,
1400			   struct perf_header *ph, int fd,
1401			   void *data __maybe_unused)
1402{
1403	ph->env.version = do_read_string(fd, ph);
1404	return ph->env.version ? 0 : -ENOMEM;
1405}
1406
1407static int process_arch(struct perf_file_section *section __maybe_unused,
1408			struct perf_header *ph,	int fd,
1409			void *data __maybe_unused)
1410{
1411	ph->env.arch = do_read_string(fd, ph);
1412	return ph->env.arch ? 0 : -ENOMEM;
1413}
1414
1415static int process_nrcpus(struct perf_file_section *section __maybe_unused,
1416			  struct perf_header *ph, int fd,
1417			  void *data __maybe_unused)
1418{
1419	ssize_t ret;
1420	u32 nr;
1421
1422	ret = readn(fd, &nr, sizeof(nr));
1423	if (ret != sizeof(nr))
1424		return -1;
1425
1426	if (ph->needs_swap)
1427		nr = bswap_32(nr);
1428
1429	ph->env.nr_cpus_avail = nr;
1430
1431	ret = readn(fd, &nr, sizeof(nr));
1432	if (ret != sizeof(nr))
1433		return -1;
1434
1435	if (ph->needs_swap)
1436		nr = bswap_32(nr);
1437
1438	ph->env.nr_cpus_online = nr;
1439	return 0;
1440}
1441
1442static int process_cpudesc(struct perf_file_section *section __maybe_unused,
1443			   struct perf_header *ph, int fd,
1444			   void *data __maybe_unused)
1445{
1446	ph->env.cpu_desc = do_read_string(fd, ph);
1447	return ph->env.cpu_desc ? 0 : -ENOMEM;
1448}
1449
1450static int process_cpuid(struct perf_file_section *section __maybe_unused,
1451			 struct perf_header *ph,  int fd,
1452			 void *data __maybe_unused)
1453{
1454	ph->env.cpuid = do_read_string(fd, ph);
1455	return ph->env.cpuid ? 0 : -ENOMEM;
1456}
1457
1458static int process_total_mem(struct perf_file_section *section __maybe_unused,
1459			     struct perf_header *ph, int fd,
1460			     void *data __maybe_unused)
1461{
1462	uint64_t mem;
1463	ssize_t ret;
1464
1465	ret = readn(fd, &mem, sizeof(mem));
1466	if (ret != sizeof(mem))
1467		return -1;
1468
1469	if (ph->needs_swap)
1470		mem = bswap_64(mem);
1471
1472	ph->env.total_mem = mem;
1473	return 0;
1474}
1475
1476static struct perf_evsel *
1477perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1478{
1479	struct perf_evsel *evsel;
1480
1481	evlist__for_each(evlist, evsel) {
1482		if (evsel->idx == idx)
1483			return evsel;
1484	}
1485
1486	return NULL;
1487}
1488
1489static void
1490perf_evlist__set_event_name(struct perf_evlist *evlist,
1491			    struct perf_evsel *event)
1492{
1493	struct perf_evsel *evsel;
1494
1495	if (!event->name)
1496		return;
1497
1498	evsel = perf_evlist__find_by_index(evlist, event->idx);
1499	if (!evsel)
1500		return;
1501
1502	if (evsel->name)
1503		return;
1504
1505	evsel->name = strdup(event->name);
1506}
1507
1508static int
1509process_event_desc(struct perf_file_section *section __maybe_unused,
1510		   struct perf_header *header, int fd,
1511		   void *data __maybe_unused)
1512{
1513	struct perf_session *session;
1514	struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1515
1516	if (!events)
1517		return 0;
1518
1519	session = container_of(header, struct perf_session, header);
1520	for (evsel = events; evsel->attr.size; evsel++)
1521		perf_evlist__set_event_name(session->evlist, evsel);
1522
1523	free_event_desc(events);
1524
1525	return 0;
1526}
1527
1528static int process_cmdline(struct perf_file_section *section __maybe_unused,
1529			   struct perf_header *ph, int fd,
1530			   void *data __maybe_unused)
1531{
1532	ssize_t ret;
1533	char *str;
1534	u32 nr, i;
1535	struct strbuf sb;
1536
1537	ret = readn(fd, &nr, sizeof(nr));
1538	if (ret != sizeof(nr))
1539		return -1;
1540
1541	if (ph->needs_swap)
1542		nr = bswap_32(nr);
1543
1544	ph->env.nr_cmdline = nr;
1545	strbuf_init(&sb, 128);
1546
1547	for (i = 0; i < nr; i++) {
1548		str = do_read_string(fd, ph);
1549		if (!str)
1550			goto error;
1551
1552		/* include a NULL character at the end */
1553		strbuf_add(&sb, str, strlen(str) + 1);
1554		free(str);
1555	}
1556	ph->env.cmdline = strbuf_detach(&sb, NULL);
1557	return 0;
1558
1559error:
1560	strbuf_release(&sb);
1561	return -1;
1562}
1563
1564static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
1565				struct perf_header *ph, int fd,
1566				void *data __maybe_unused)
1567{
1568	ssize_t ret;
1569	u32 nr, i;
1570	char *str;
1571	struct strbuf sb;
1572
1573	ret = readn(fd, &nr, sizeof(nr));
1574	if (ret != sizeof(nr))
1575		return -1;
1576
1577	if (ph->needs_swap)
1578		nr = bswap_32(nr);
1579
1580	ph->env.nr_sibling_cores = nr;
1581	strbuf_init(&sb, 128);
1582
1583	for (i = 0; i < nr; i++) {
1584		str = do_read_string(fd, ph);
1585		if (!str)
1586			goto error;
1587
1588		/* include a NULL character at the end */
1589		strbuf_add(&sb, str, strlen(str) + 1);
1590		free(str);
1591	}
1592	ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1593
1594	ret = readn(fd, &nr, sizeof(nr));
1595	if (ret != sizeof(nr))
1596		return -1;
1597
1598	if (ph->needs_swap)
1599		nr = bswap_32(nr);
1600
1601	ph->env.nr_sibling_threads = nr;
1602
1603	for (i = 0; i < nr; i++) {
1604		str = do_read_string(fd, ph);
1605		if (!str)
1606			goto error;
1607
1608		/* include a NULL character at the end */
1609		strbuf_add(&sb, str, strlen(str) + 1);
1610		free(str);
1611	}
1612	ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1613	return 0;
1614
1615error:
1616	strbuf_release(&sb);
1617	return -1;
1618}
1619
1620static int process_numa_topology(struct perf_file_section *section __maybe_unused,
1621				 struct perf_header *ph, int fd,
1622				 void *data __maybe_unused)
1623{
1624	ssize_t ret;
1625	u32 nr, node, i;
1626	char *str;
1627	uint64_t mem_total, mem_free;
1628	struct strbuf sb;
1629
1630	/* nr nodes */
1631	ret = readn(fd, &nr, sizeof(nr));
1632	if (ret != sizeof(nr))
1633		goto error;
1634
1635	if (ph->needs_swap)
1636		nr = bswap_32(nr);
1637
1638	ph->env.nr_numa_nodes = nr;
1639	strbuf_init(&sb, 256);
1640
1641	for (i = 0; i < nr; i++) {
1642		/* node number */
1643		ret = readn(fd, &node, sizeof(node));
1644		if (ret != sizeof(node))
1645			goto error;
1646
1647		ret = readn(fd, &mem_total, sizeof(u64));
1648		if (ret != sizeof(u64))
1649			goto error;
1650
1651		ret = readn(fd, &mem_free, sizeof(u64));
1652		if (ret != sizeof(u64))
1653			goto error;
1654
1655		if (ph->needs_swap) {
1656			node = bswap_32(node);
1657			mem_total = bswap_64(mem_total);
1658			mem_free = bswap_64(mem_free);
1659		}
1660
1661		strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1662			    node, mem_total, mem_free);
1663
1664		str = do_read_string(fd, ph);
1665		if (!str)
1666			goto error;
1667
1668		/* include a NULL character at the end */
1669		strbuf_add(&sb, str, strlen(str) + 1);
1670		free(str);
1671	}
1672	ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1673	return 0;
1674
1675error:
1676	strbuf_release(&sb);
1677	return -1;
1678}
1679
1680static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
1681				struct perf_header *ph, int fd,
1682				void *data __maybe_unused)
1683{
1684	ssize_t ret;
1685	char *name;
1686	u32 pmu_num;
1687	u32 type;
1688	struct strbuf sb;
1689
1690	ret = readn(fd, &pmu_num, sizeof(pmu_num));
1691	if (ret != sizeof(pmu_num))
1692		return -1;
1693
1694	if (ph->needs_swap)
1695		pmu_num = bswap_32(pmu_num);
1696
1697	if (!pmu_num) {
1698		pr_debug("pmu mappings not available\n");
1699		return 0;
1700	}
1701
1702	ph->env.nr_pmu_mappings = pmu_num;
1703	strbuf_init(&sb, 128);
1704
1705	while (pmu_num) {
1706		if (readn(fd, &type, sizeof(type)) != sizeof(type))
1707			goto error;
1708		if (ph->needs_swap)
1709			type = bswap_32(type);
1710
1711		name = do_read_string(fd, ph);
1712		if (!name)
1713			goto error;
1714
1715		strbuf_addf(&sb, "%u:%s", type, name);
1716		/* include a NULL character at the end */
1717		strbuf_add(&sb, "", 1);
1718
1719		free(name);
1720		pmu_num--;
1721	}
1722	ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1723	return 0;
1724
1725error:
1726	strbuf_release(&sb);
1727	return -1;
1728}
1729
1730static int process_group_desc(struct perf_file_section *section __maybe_unused,
1731			      struct perf_header *ph, int fd,
1732			      void *data __maybe_unused)
1733{
1734	size_t ret = -1;
1735	u32 i, nr, nr_groups;
1736	struct perf_session *session;
1737	struct perf_evsel *evsel, *leader = NULL;
1738	struct group_desc {
1739		char *name;
1740		u32 leader_idx;
1741		u32 nr_members;
1742	} *desc;
1743
1744	if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
1745		return -1;
1746
1747	if (ph->needs_swap)
1748		nr_groups = bswap_32(nr_groups);
1749
1750	ph->env.nr_groups = nr_groups;
1751	if (!nr_groups) {
1752		pr_debug("group desc not available\n");
1753		return 0;
1754	}
1755
1756	desc = calloc(nr_groups, sizeof(*desc));
1757	if (!desc)
1758		return -1;
1759
1760	for (i = 0; i < nr_groups; i++) {
1761		desc[i].name = do_read_string(fd, ph);
1762		if (!desc[i].name)
1763			goto out_free;
1764
1765		if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
1766			goto out_free;
1767
1768		if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
1769			goto out_free;
1770
1771		if (ph->needs_swap) {
1772			desc[i].leader_idx = bswap_32(desc[i].leader_idx);
1773			desc[i].nr_members = bswap_32(desc[i].nr_members);
1774		}
1775	}
1776
1777	/*
1778	 * Rebuild group relationship based on the group_desc
1779	 */
1780	session = container_of(ph, struct perf_session, header);
1781	session->evlist->nr_groups = nr_groups;
1782
1783	i = nr = 0;
1784	evlist__for_each(session->evlist, evsel) {
1785		if (evsel->idx == (int) desc[i].leader_idx) {
1786			evsel->leader = evsel;
1787			/* {anon_group} is a dummy name */
1788			if (strcmp(desc[i].name, "{anon_group}")) {
1789				evsel->group_name = desc[i].name;
1790				desc[i].name = NULL;
1791			}
1792			evsel->nr_members = desc[i].nr_members;
1793
1794			if (i >= nr_groups || nr > 0) {
1795				pr_debug("invalid group desc\n");
1796				goto out_free;
1797			}
1798
1799			leader = evsel;
1800			nr = evsel->nr_members - 1;
1801			i++;
1802		} else if (nr) {
1803			/* This is a group member */
1804			evsel->leader = leader;
1805
1806			nr--;
1807		}
1808	}
1809
1810	if (i != nr_groups || nr != 0) {
1811		pr_debug("invalid group desc\n");
1812		goto out_free;
1813	}
1814
1815	ret = 0;
1816out_free:
1817	for (i = 0; i < nr_groups; i++)
1818		zfree(&desc[i].name);
1819	free(desc);
1820
1821	return ret;
1822}
1823
1824struct feature_ops {
1825	int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1826	void (*print)(struct perf_header *h, int fd, FILE *fp);
1827	int (*process)(struct perf_file_section *section,
1828		       struct perf_header *h, int fd, void *data);
1829	const char *name;
1830	bool full_only;
1831};
1832
1833#define FEAT_OPA(n, func) \
1834	[n] = { .name = #n, .write = write_##func, .print = print_##func }
1835#define FEAT_OPP(n, func) \
1836	[n] = { .name = #n, .write = write_##func, .print = print_##func, \
1837		.process = process_##func }
1838#define FEAT_OPF(n, func) \
1839	[n] = { .name = #n, .write = write_##func, .print = print_##func, \
1840		.process = process_##func, .full_only = true }
1841
1842/* feature_ops not implemented: */
1843#define print_tracing_data	NULL
1844#define print_build_id		NULL
1845
1846static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
1847	FEAT_OPP(HEADER_TRACING_DATA,	tracing_data),
1848	FEAT_OPP(HEADER_BUILD_ID,	build_id),
1849	FEAT_OPP(HEADER_HOSTNAME,	hostname),
1850	FEAT_OPP(HEADER_OSRELEASE,	osrelease),
1851	FEAT_OPP(HEADER_VERSION,	version),
1852	FEAT_OPP(HEADER_ARCH,		arch),
1853	FEAT_OPP(HEADER_NRCPUS,		nrcpus),
1854	FEAT_OPP(HEADER_CPUDESC,	cpudesc),
1855	FEAT_OPP(HEADER_CPUID,		cpuid),
1856	FEAT_OPP(HEADER_TOTAL_MEM,	total_mem),
1857	FEAT_OPP(HEADER_EVENT_DESC,	event_desc),
1858	FEAT_OPP(HEADER_CMDLINE,	cmdline),
1859	FEAT_OPF(HEADER_CPU_TOPOLOGY,	cpu_topology),
1860	FEAT_OPF(HEADER_NUMA_TOPOLOGY,	numa_topology),
1861	FEAT_OPA(HEADER_BRANCH_STACK,	branch_stack),
1862	FEAT_OPP(HEADER_PMU_MAPPINGS,	pmu_mappings),
1863	FEAT_OPP(HEADER_GROUP_DESC,	group_desc),
1864};
1865
1866struct header_print_data {
1867	FILE *fp;
1868	bool full; /* extended list of headers */
1869};
1870
1871static int perf_file_section__fprintf_info(struct perf_file_section *section,
1872					   struct perf_header *ph,
1873					   int feat, int fd, void *data)
1874{
1875	struct header_print_data *hd = data;
1876
1877	if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1878		pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1879				"%d, continuing...\n", section->offset, feat);
1880		return 0;
1881	}
1882	if (feat >= HEADER_LAST_FEATURE) {
1883		pr_warning("unknown feature %d\n", feat);
1884		return 0;
1885	}
1886	if (!feat_ops[feat].print)
1887		return 0;
1888
1889	if (!feat_ops[feat].full_only || hd->full)
1890		feat_ops[feat].print(ph, fd, hd->fp);
1891	else
1892		fprintf(hd->fp, "# %s info available, use -I to display\n",
1893			feat_ops[feat].name);
1894
1895	return 0;
1896}
1897
1898int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
1899{
1900	struct header_print_data hd;
1901	struct perf_header *header = &session->header;
1902	int fd = perf_data_file__fd(session->file);
1903	hd.fp = fp;
1904	hd.full = full;
1905
1906	perf_header__process_sections(header, fd, &hd,
1907				      perf_file_section__fprintf_info);
1908	return 0;
1909}
1910
1911static int do_write_feat(int fd, struct perf_header *h, int type,
1912			 struct perf_file_section **p,
1913			 struct perf_evlist *evlist)
1914{
1915	int err;
1916	int ret = 0;
1917
1918	if (perf_header__has_feat(h, type)) {
1919		if (!feat_ops[type].write)
1920			return -1;
1921
1922		(*p)->offset = lseek(fd, 0, SEEK_CUR);
1923
1924		err = feat_ops[type].write(fd, h, evlist);
1925		if (err < 0) {
1926			pr_debug("failed to write feature %d\n", type);
1927
1928			/* undo anything written */
1929			lseek(fd, (*p)->offset, SEEK_SET);
1930
1931			return -1;
1932		}
1933		(*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
1934		(*p)++;
1935	}
1936	return ret;
1937}
1938
1939static int perf_header__adds_write(struct perf_header *header,
1940				   struct perf_evlist *evlist, int fd)
1941{
1942	int nr_sections;
1943	struct perf_file_section *feat_sec, *p;
1944	int sec_size;
1945	u64 sec_start;
1946	int feat;
1947	int err;
1948
1949	nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
1950	if (!nr_sections)
1951		return 0;
1952
1953	feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
1954	if (feat_sec == NULL)
1955		return -ENOMEM;
1956
1957	sec_size = sizeof(*feat_sec) * nr_sections;
1958
1959	sec_start = header->feat_offset;
1960	lseek(fd, sec_start + sec_size, SEEK_SET);
1961
1962	for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
1963		if (do_write_feat(fd, header, feat, &p, evlist))
1964			perf_header__clear_feat(header, feat);
1965	}
1966
1967	lseek(fd, sec_start, SEEK_SET);
1968	/*
1969	 * may write more than needed due to dropped feature, but
1970	 * this is okay, reader will skip the mising entries
1971	 */
1972	err = do_write(fd, feat_sec, sec_size);
1973	if (err < 0)
1974		pr_debug("failed to write feature section\n");
1975	free(feat_sec);
1976	return err;
1977}
1978
1979int perf_header__write_pipe(int fd)
1980{
1981	struct perf_pipe_file_header f_header;
1982	int err;
1983
1984	f_header = (struct perf_pipe_file_header){
1985		.magic	   = PERF_MAGIC,
1986		.size	   = sizeof(f_header),
1987	};
1988
1989	err = do_write(fd, &f_header, sizeof(f_header));
1990	if (err < 0) {
1991		pr_debug("failed to write perf pipe header\n");
1992		return err;
1993	}
1994
1995	return 0;
1996}
1997
1998int perf_session__write_header(struct perf_session *session,
1999			       struct perf_evlist *evlist,
2000			       int fd, bool at_exit)
2001{
2002	struct perf_file_header f_header;
2003	struct perf_file_attr   f_attr;
2004	struct perf_header *header = &session->header;
2005	struct perf_evsel *evsel;
2006	u64 attr_offset;
2007	int err;
2008
2009	lseek(fd, sizeof(f_header), SEEK_SET);
2010
2011	evlist__for_each(session->evlist, evsel) {
2012		evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2013		err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
2014		if (err < 0) {
2015			pr_debug("failed to write perf header\n");
2016			return err;
2017		}
2018	}
2019
2020	attr_offset = lseek(fd, 0, SEEK_CUR);
2021
2022	evlist__for_each(evlist, evsel) {
2023		f_attr = (struct perf_file_attr){
2024			.attr = evsel->attr,
2025			.ids  = {
2026				.offset = evsel->id_offset,
2027				.size   = evsel->ids * sizeof(u64),
2028			}
2029		};
2030		err = do_write(fd, &f_attr, sizeof(f_attr));
2031		if (err < 0) {
2032			pr_debug("failed to write perf header attribute\n");
2033			return err;
2034		}
2035	}
2036
2037	if (!header->data_offset)
2038		header->data_offset = lseek(fd, 0, SEEK_CUR);
2039	header->feat_offset = header->data_offset + header->data_size;
2040
2041	if (at_exit) {
2042		err = perf_header__adds_write(header, evlist, fd);
2043		if (err < 0)
2044			return err;
2045	}
2046
2047	f_header = (struct perf_file_header){
2048		.magic	   = PERF_MAGIC,
2049		.size	   = sizeof(f_header),
2050		.attr_size = sizeof(f_attr),
2051		.attrs = {
2052			.offset = attr_offset,
2053			.size   = evlist->nr_entries * sizeof(f_attr),
2054		},
2055		.data = {
2056			.offset = header->data_offset,
2057			.size	= header->data_size,
2058		},
2059		/* event_types is ignored, store zeros */
2060	};
2061
2062	memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2063
2064	lseek(fd, 0, SEEK_SET);
2065	err = do_write(fd, &f_header, sizeof(f_header));
2066	if (err < 0) {
2067		pr_debug("failed to write perf header\n");
2068		return err;
2069	}
2070	lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2071
2072	return 0;
2073}
2074
2075static int perf_header__getbuffer64(struct perf_header *header,
2076				    int fd, void *buf, size_t size)
2077{
2078	if (readn(fd, buf, size) <= 0)
2079		return -1;
2080
2081	if (header->needs_swap)
2082		mem_bswap_64(buf, size);
2083
2084	return 0;
2085}
2086
2087int perf_header__process_sections(struct perf_header *header, int fd,
2088				  void *data,
2089				  int (*process)(struct perf_file_section *section,
2090						 struct perf_header *ph,
2091						 int feat, int fd, void *data))
2092{
2093	struct perf_file_section *feat_sec, *sec;
2094	int nr_sections;
2095	int sec_size;
2096	int feat;
2097	int err;
2098
2099	nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2100	if (!nr_sections)
2101		return 0;
2102
2103	feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2104	if (!feat_sec)
2105		return -1;
2106
2107	sec_size = sizeof(*feat_sec) * nr_sections;
2108
2109	lseek(fd, header->feat_offset, SEEK_SET);
2110
2111	err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2112	if (err < 0)
2113		goto out_free;
2114
2115	for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2116		err = process(sec++, header, feat, fd, data);
2117		if (err < 0)
2118			goto out_free;
2119	}
2120	err = 0;
2121out_free:
2122	free(feat_sec);
2123	return err;
2124}
2125
2126static const int attr_file_abi_sizes[] = {
2127	[0] = PERF_ATTR_SIZE_VER0,
2128	[1] = PERF_ATTR_SIZE_VER1,
2129	[2] = PERF_ATTR_SIZE_VER2,
2130	[3] = PERF_ATTR_SIZE_VER3,
2131	[4] = PERF_ATTR_SIZE_VER4,
2132	0,
2133};
2134
2135/*
2136 * In the legacy file format, the magic number is not used to encode endianness.
2137 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2138 * on ABI revisions, we need to try all combinations for all endianness to
2139 * detect the endianness.
2140 */
2141static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2142{
2143	uint64_t ref_size, attr_size;
2144	int i;
2145
2146	for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2147		ref_size = attr_file_abi_sizes[i]
2148			 + sizeof(struct perf_file_section);
2149		if (hdr_sz != ref_size) {
2150			attr_size = bswap_64(hdr_sz);
2151			if (attr_size != ref_size)
2152				continue;
2153
2154			ph->needs_swap = true;
2155		}
2156		pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2157			 i,
2158			 ph->needs_swap);
2159		return 0;
2160	}
2161	/* could not determine endianness */
2162	return -1;
2163}
2164
2165#define PERF_PIPE_HDR_VER0	16
2166
2167static const size_t attr_pipe_abi_sizes[] = {
2168	[0] = PERF_PIPE_HDR_VER0,
2169	0,
2170};
2171
2172/*
2173 * In the legacy pipe format, there is an implicit assumption that endiannesss
2174 * between host recording the samples, and host parsing the samples is the
2175 * same. This is not always the case given that the pipe output may always be
2176 * redirected into a file and analyzed on a different machine with possibly a
2177 * different endianness and perf_event ABI revsions in the perf tool itself.
2178 */
2179static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2180{
2181	u64 attr_size;
2182	int i;
2183
2184	for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2185		if (hdr_sz != attr_pipe_abi_sizes[i]) {
2186			attr_size = bswap_64(hdr_sz);
2187			if (attr_size != hdr_sz)
2188				continue;
2189
2190			ph->needs_swap = true;
2191		}
2192		pr_debug("Pipe ABI%d perf.data file detected\n", i);
2193		return 0;
2194	}
2195	return -1;
2196}
2197
2198bool is_perf_magic(u64 magic)
2199{
2200	if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2201		|| magic == __perf_magic2
2202		|| magic == __perf_magic2_sw)
2203		return true;
2204
2205	return false;
2206}
2207
2208static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2209			      bool is_pipe, struct perf_header *ph)
2210{
2211	int ret;
2212
2213	/* check for legacy format */
2214	ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2215	if (ret == 0) {
2216		ph->version = PERF_HEADER_VERSION_1;
2217		pr_debug("legacy perf.data format\n");
2218		if (is_pipe)
2219			return try_all_pipe_abis(hdr_sz, ph);
2220
2221		return try_all_file_abis(hdr_sz, ph);
2222	}
2223	/*
2224	 * the new magic number serves two purposes:
2225	 * - unique number to identify actual perf.data files
2226	 * - encode endianness of file
2227	 */
2228	ph->version = PERF_HEADER_VERSION_2;
2229
2230	/* check magic number with one endianness */
2231	if (magic == __perf_magic2)
2232		return 0;
2233
2234	/* check magic number with opposite endianness */
2235	if (magic != __perf_magic2_sw)
2236		return -1;
2237
2238	ph->needs_swap = true;
2239
2240	return 0;
2241}
2242
2243int perf_file_header__read(struct perf_file_header *header,
2244			   struct perf_header *ph, int fd)
2245{
2246	ssize_t ret;
2247
2248	lseek(fd, 0, SEEK_SET);
2249
2250	ret = readn(fd, header, sizeof(*header));
2251	if (ret <= 0)
2252		return -1;
2253
2254	if (check_magic_endian(header->magic,
2255			       header->attr_size, false, ph) < 0) {
2256		pr_debug("magic/endian check failed\n");
2257		return -1;
2258	}
2259
2260	if (ph->needs_swap) {
2261		mem_bswap_64(header, offsetof(struct perf_file_header,
2262			     adds_features));
2263	}
2264
2265	if (header->size != sizeof(*header)) {
2266		/* Support the previous format */
2267		if (header->size == offsetof(typeof(*header), adds_features))
2268			bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2269		else
2270			return -1;
2271	} else if (ph->needs_swap) {
2272		/*
2273		 * feature bitmap is declared as an array of unsigned longs --
2274		 * not good since its size can differ between the host that
2275		 * generated the data file and the host analyzing the file.
2276		 *
2277		 * We need to handle endianness, but we don't know the size of
2278		 * the unsigned long where the file was generated. Take a best
2279		 * guess at determining it: try 64-bit swap first (ie., file
2280		 * created on a 64-bit host), and check if the hostname feature
2281		 * bit is set (this feature bit is forced on as of fbe96f2).
2282		 * If the bit is not, undo the 64-bit swap and try a 32-bit
2283		 * swap. If the hostname bit is still not set (e.g., older data
2284		 * file), punt and fallback to the original behavior --
2285		 * clearing all feature bits and setting buildid.
2286		 */
2287		mem_bswap_64(&header->adds_features,
2288			    BITS_TO_U64(HEADER_FEAT_BITS));
2289
2290		if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2291			/* unswap as u64 */
2292			mem_bswap_64(&header->adds_features,
2293				    BITS_TO_U64(HEADER_FEAT_BITS));
2294
2295			/* unswap as u32 */
2296			mem_bswap_32(&header->adds_features,
2297				    BITS_TO_U32(HEADER_FEAT_BITS));
2298		}
2299
2300		if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2301			bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2302			set_bit(HEADER_BUILD_ID, header->adds_features);
2303		}
2304	}
2305
2306	memcpy(&ph->adds_features, &header->adds_features,
2307	       sizeof(ph->adds_features));
2308
2309	ph->data_offset  = header->data.offset;
2310	ph->data_size	 = header->data.size;
2311	ph->feat_offset  = header->data.offset + header->data.size;
2312	return 0;
2313}
2314
2315static int perf_file_section__process(struct perf_file_section *section,
2316				      struct perf_header *ph,
2317				      int feat, int fd, void *data)
2318{
2319	if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2320		pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2321			  "%d, continuing...\n", section->offset, feat);
2322		return 0;
2323	}
2324
2325	if (feat >= HEADER_LAST_FEATURE) {
2326		pr_debug("unknown feature %d, continuing...\n", feat);
2327		return 0;
2328	}
2329
2330	if (!feat_ops[feat].process)
2331		return 0;
2332
2333	return feat_ops[feat].process(section, ph, fd, data);
2334}
2335
2336static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2337				       struct perf_header *ph, int fd,
2338				       bool repipe)
2339{
2340	ssize_t ret;
2341
2342	ret = readn(fd, header, sizeof(*header));
2343	if (ret <= 0)
2344		return -1;
2345
2346	if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2347		pr_debug("endian/magic failed\n");
2348		return -1;
2349	}
2350
2351	if (ph->needs_swap)
2352		header->size = bswap_64(header->size);
2353
2354	if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2355		return -1;
2356
2357	return 0;
2358}
2359
2360static int perf_header__read_pipe(struct perf_session *session)
2361{
2362	struct perf_header *header = &session->header;
2363	struct perf_pipe_file_header f_header;
2364
2365	if (perf_file_header__read_pipe(&f_header, header,
2366					perf_data_file__fd(session->file),
2367					session->repipe) < 0) {
2368		pr_debug("incompatible file format\n");
2369		return -EINVAL;
2370	}
2371
2372	return 0;
2373}
2374
2375static int read_attr(int fd, struct perf_header *ph,
2376		     struct perf_file_attr *f_attr)
2377{
2378	struct perf_event_attr *attr = &f_attr->attr;
2379	size_t sz, left;
2380	size_t our_sz = sizeof(f_attr->attr);
2381	ssize_t ret;
2382
2383	memset(f_attr, 0, sizeof(*f_attr));
2384
2385	/* read minimal guaranteed structure */
2386	ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2387	if (ret <= 0) {
2388		pr_debug("cannot read %d bytes of header attr\n",
2389			 PERF_ATTR_SIZE_VER0);
2390		return -1;
2391	}
2392
2393	/* on file perf_event_attr size */
2394	sz = attr->size;
2395
2396	if (ph->needs_swap)
2397		sz = bswap_32(sz);
2398
2399	if (sz == 0) {
2400		/* assume ABI0 */
2401		sz =  PERF_ATTR_SIZE_VER0;
2402	} else if (sz > our_sz) {
2403		pr_debug("file uses a more recent and unsupported ABI"
2404			 " (%zu bytes extra)\n", sz - our_sz);
2405		return -1;
2406	}
2407	/* what we have not yet read and that we know about */
2408	left = sz - PERF_ATTR_SIZE_VER0;
2409	if (left) {
2410		void *ptr = attr;
2411		ptr += PERF_ATTR_SIZE_VER0;
2412
2413		ret = readn(fd, ptr, left);
2414	}
2415	/* read perf_file_section, ids are read in caller */
2416	ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2417
2418	return ret <= 0 ? -1 : 0;
2419}
2420
2421static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2422						struct pevent *pevent)
2423{
2424	struct event_format *event;
2425	char bf[128];
2426
2427	/* already prepared */
2428	if (evsel->tp_format)
2429		return 0;
2430
2431	if (pevent == NULL) {
2432		pr_debug("broken or missing trace data\n");
2433		return -1;
2434	}
2435
2436	event = pevent_find_event(pevent, evsel->attr.config);
2437	if (event == NULL)
2438		return -1;
2439
2440	if (!evsel->name) {
2441		snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2442		evsel->name = strdup(bf);
2443		if (evsel->name == NULL)
2444			return -1;
2445	}
2446
2447	evsel->tp_format = event;
2448	return 0;
2449}
2450
2451static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2452						  struct pevent *pevent)
2453{
2454	struct perf_evsel *pos;
2455
2456	evlist__for_each(evlist, pos) {
2457		if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2458		    perf_evsel__prepare_tracepoint_event(pos, pevent))
2459			return -1;
2460	}
2461
2462	return 0;
2463}
2464
2465int perf_session__read_header(struct perf_session *session)
2466{
2467	struct perf_data_file *file = session->file;
2468	struct perf_header *header = &session->header;
2469	struct perf_file_header	f_header;
2470	struct perf_file_attr	f_attr;
2471	u64			f_id;
2472	int nr_attrs, nr_ids, i, j;
2473	int fd = perf_data_file__fd(file);
2474
2475	session->evlist = perf_evlist__new();
2476	if (session->evlist == NULL)
2477		return -ENOMEM;
2478
2479	if (perf_data_file__is_pipe(file))
2480		return perf_header__read_pipe(session);
2481
2482	if (perf_file_header__read(&f_header, header, fd) < 0)
2483		return -EINVAL;
2484
2485	/*
2486	 * Sanity check that perf.data was written cleanly; data size is
2487	 * initialized to 0 and updated only if the on_exit function is run.
2488	 * If data size is still 0 then the file contains only partial
2489	 * information.  Just warn user and process it as much as it can.
2490	 */
2491	if (f_header.data.size == 0) {
2492		pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2493			   "Was the 'perf record' command properly terminated?\n",
2494			   file->path);
2495	}
2496
2497	nr_attrs = f_header.attrs.size / f_header.attr_size;
2498	lseek(fd, f_header.attrs.offset, SEEK_SET);
2499
2500	for (i = 0; i < nr_attrs; i++) {
2501		struct perf_evsel *evsel;
2502		off_t tmp;
2503
2504		if (read_attr(fd, header, &f_attr) < 0)
2505			goto out_errno;
2506
2507		if (header->needs_swap) {
2508			f_attr.ids.size   = bswap_64(f_attr.ids.size);
2509			f_attr.ids.offset = bswap_64(f_attr.ids.offset);
2510			perf_event__attr_swap(&f_attr.attr);
2511		}
2512
2513		tmp = lseek(fd, 0, SEEK_CUR);
2514		evsel = perf_evsel__new(&f_attr.attr);
2515
2516		if (evsel == NULL)
2517			goto out_delete_evlist;
2518
2519		evsel->needs_swap = header->needs_swap;
2520		/*
2521		 * Do it before so that if perf_evsel__alloc_id fails, this
2522		 * entry gets purged too at perf_evlist__delete().
2523		 */
2524		perf_evlist__add(session->evlist, evsel);
2525
2526		nr_ids = f_attr.ids.size / sizeof(u64);
2527		/*
2528		 * We don't have the cpu and thread maps on the header, so
2529		 * for allocating the perf_sample_id table we fake 1 cpu and
2530		 * hattr->ids threads.
2531		 */
2532		if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2533			goto out_delete_evlist;
2534
2535		lseek(fd, f_attr.ids.offset, SEEK_SET);
2536
2537		for (j = 0; j < nr_ids; j++) {
2538			if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2539				goto out_errno;
2540
2541			perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2542		}
2543
2544		lseek(fd, tmp, SEEK_SET);
2545	}
2546
2547	symbol_conf.nr_events = nr_attrs;
2548
2549	perf_header__process_sections(header, fd, &session->tevent,
2550				      perf_file_section__process);
2551
2552	if (perf_evlist__prepare_tracepoint_events(session->evlist,
2553						   session->tevent.pevent))
2554		goto out_delete_evlist;
2555
2556	return 0;
2557out_errno:
2558	return -errno;
2559
2560out_delete_evlist:
2561	perf_evlist__delete(session->evlist);
2562	session->evlist = NULL;
2563	return -ENOMEM;
2564}
2565
2566int perf_event__synthesize_attr(struct perf_tool *tool,
2567				struct perf_event_attr *attr, u32 ids, u64 *id,
2568				perf_event__handler_t process)
2569{
2570	union perf_event *ev;
2571	size_t size;
2572	int err;
2573
2574	size = sizeof(struct perf_event_attr);
2575	size = PERF_ALIGN(size, sizeof(u64));
2576	size += sizeof(struct perf_event_header);
2577	size += ids * sizeof(u64);
2578
2579	ev = malloc(size);
2580
2581	if (ev == NULL)
2582		return -ENOMEM;
2583
2584	ev->attr.attr = *attr;
2585	memcpy(ev->attr.id, id, ids * sizeof(u64));
2586
2587	ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2588	ev->attr.header.size = (u16)size;
2589
2590	if (ev->attr.header.size == size)
2591		err = process(tool, ev, NULL, NULL);
2592	else
2593		err = -E2BIG;
2594
2595	free(ev);
2596
2597	return err;
2598}
2599
2600int perf_event__synthesize_attrs(struct perf_tool *tool,
2601				   struct perf_session *session,
2602				   perf_event__handler_t process)
2603{
2604	struct perf_evsel *evsel;
2605	int err = 0;
2606
2607	evlist__for_each(session->evlist, evsel) {
2608		err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2609						  evsel->id, process);
2610		if (err) {
2611			pr_debug("failed to create perf header attribute\n");
2612			return err;
2613		}
2614	}
2615
2616	return err;
2617}
2618
2619int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2620			     union perf_event *event,
2621			     struct perf_evlist **pevlist)
2622{
2623	u32 i, ids, n_ids;
2624	struct perf_evsel *evsel;
2625	struct perf_evlist *evlist = *pevlist;
2626
2627	if (evlist == NULL) {
2628		*pevlist = evlist = perf_evlist__new();
2629		if (evlist == NULL)
2630			return -ENOMEM;
2631	}
2632
2633	evsel = perf_evsel__new(&event->attr.attr);
2634	if (evsel == NULL)
2635		return -ENOMEM;
2636
2637	perf_evlist__add(evlist, evsel);
2638
2639	ids = event->header.size;
2640	ids -= (void *)&event->attr.id - (void *)event;
2641	n_ids = ids / sizeof(u64);
2642	/*
2643	 * We don't have the cpu and thread maps on the header, so
2644	 * for allocating the perf_sample_id table we fake 1 cpu and
2645	 * hattr->ids threads.
2646	 */
2647	if (perf_evsel__alloc_id(evsel, 1, n_ids))
2648		return -ENOMEM;
2649
2650	for (i = 0; i < n_ids; i++) {
2651		perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2652	}
2653
2654	symbol_conf.nr_events = evlist->nr_entries;
2655
2656	return 0;
2657}
2658
2659int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
2660					struct perf_evlist *evlist,
2661					perf_event__handler_t process)
2662{
2663	union perf_event ev;
2664	struct tracing_data *tdata;
2665	ssize_t size = 0, aligned_size = 0, padding;
2666	int err __maybe_unused = 0;
2667
2668	/*
2669	 * We are going to store the size of the data followed
2670	 * by the data contents. Since the fd descriptor is a pipe,
2671	 * we cannot seek back to store the size of the data once
2672	 * we know it. Instead we:
2673	 *
2674	 * - write the tracing data to the temp file
2675	 * - get/write the data size to pipe
2676	 * - write the tracing data from the temp file
2677	 *   to the pipe
2678	 */
2679	tdata = tracing_data_get(&evlist->entries, fd, true);
2680	if (!tdata)
2681		return -1;
2682
2683	memset(&ev, 0, sizeof(ev));
2684
2685	ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2686	size = tdata->size;
2687	aligned_size = PERF_ALIGN(size, sizeof(u64));
2688	padding = aligned_size - size;
2689	ev.tracing_data.header.size = sizeof(ev.tracing_data);
2690	ev.tracing_data.size = aligned_size;
2691
2692	process(tool, &ev, NULL, NULL);
2693
2694	/*
2695	 * The put function will copy all the tracing data
2696	 * stored in temp file to the pipe.
2697	 */
2698	tracing_data_put(tdata);
2699
2700	write_padded(fd, NULL, 0, padding);
2701
2702	return aligned_size;
2703}
2704
2705int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
2706				     union perf_event *event,
2707				     struct perf_session *session)
2708{
2709	ssize_t size_read, padding, size = event->tracing_data.size;
2710	int fd = perf_data_file__fd(session->file);
2711	off_t offset = lseek(fd, 0, SEEK_CUR);
2712	char buf[BUFSIZ];
2713
2714	/* setup for reading amidst mmap */
2715	lseek(fd, offset + sizeof(struct tracing_data_event),
2716	      SEEK_SET);
2717
2718	size_read = trace_report(fd, &session->tevent,
2719				 session->repipe);
2720	padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
2721
2722	if (readn(fd, buf, padding) < 0) {
2723		pr_err("%s: reading input file", __func__);
2724		return -1;
2725	}
2726	if (session->repipe) {
2727		int retw = write(STDOUT_FILENO, buf, padding);
2728		if (retw <= 0 || retw != padding) {
2729			pr_err("%s: repiping tracing data padding", __func__);
2730			return -1;
2731		}
2732	}
2733
2734	if (size_read + padding != size) {
2735		pr_err("%s: tracing data size mismatch", __func__);
2736		return -1;
2737	}
2738
2739	perf_evlist__prepare_tracepoint_events(session->evlist,
2740					       session->tevent.pevent);
2741
2742	return size_read + padding;
2743}
2744
2745int perf_event__synthesize_build_id(struct perf_tool *tool,
2746				    struct dso *pos, u16 misc,
2747				    perf_event__handler_t process,
2748				    struct machine *machine)
2749{
2750	union perf_event ev;
2751	size_t len;
2752	int err = 0;
2753
2754	if (!pos->hit)
2755		return err;
2756
2757	memset(&ev, 0, sizeof(ev));
2758
2759	len = pos->long_name_len + 1;
2760	len = PERF_ALIGN(len, NAME_ALIGN);
2761	memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2762	ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2763	ev.build_id.header.misc = misc;
2764	ev.build_id.pid = machine->pid;
2765	ev.build_id.header.size = sizeof(ev.build_id) + len;
2766	memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2767
2768	err = process(tool, &ev, NULL, machine);
2769
2770	return err;
2771}
2772
2773int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
2774				 union perf_event *event,
2775				 struct perf_session *session)
2776{
2777	__event_process_build_id(&event->build_id,
2778				 event->build_id.filename,
2779				 session);
2780	return 0;
2781}
2782