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