xref: /openbmc/linux/tools/perf/util/header.c (revision a72594ca)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <inttypes.h>
4 #include "util.h"
5 #include "string2.h"
6 #include <sys/param.h>
7 #include <sys/types.h>
8 #include <byteswap.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <linux/compiler.h>
13 #include <linux/list.h>
14 #include <linux/kernel.h>
15 #include <linux/bitops.h>
16 #include <linux/stringify.h>
17 #include <sys/stat.h>
18 #include <sys/utsname.h>
19 #include <linux/time64.h>
20 
21 #include "evlist.h"
22 #include "evsel.h"
23 #include "header.h"
24 #include "memswap.h"
25 #include "../perf.h"
26 #include "trace-event.h"
27 #include "session.h"
28 #include "symbol.h"
29 #include "debug.h"
30 #include "cpumap.h"
31 #include "pmu.h"
32 #include "vdso.h"
33 #include "strbuf.h"
34 #include "build-id.h"
35 #include "data.h"
36 #include <api/fs/fs.h>
37 #include "asm/bug.h"
38 #include "tool.h"
39 #include "time-utils.h"
40 
41 #include "sane_ctype.h"
42 
43 /*
44  * magic2 = "PERFILE2"
45  * must be a numerical value to let the endianness
46  * determine the memory layout. That way we are able
47  * to detect endianness when reading the perf.data file
48  * back.
49  *
50  * we check for legacy (PERFFILE) format.
51  */
52 static const char *__perf_magic1 = "PERFFILE";
53 static const u64 __perf_magic2    = 0x32454c4946524550ULL;
54 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
55 
56 #define PERF_MAGIC	__perf_magic2
57 
58 const char perf_version_string[] = PERF_VERSION;
59 
60 struct perf_file_attr {
61 	struct perf_event_attr	attr;
62 	struct perf_file_section	ids;
63 };
64 
65 struct feat_fd {
66 	struct perf_header	*ph;
67 	int			fd;
68 	void			*buf;	/* Either buf != NULL or fd >= 0 */
69 	ssize_t			offset;
70 	size_t			size;
71 	struct perf_evsel	*events;
72 };
73 
74 void perf_header__set_feat(struct perf_header *header, int feat)
75 {
76 	set_bit(feat, header->adds_features);
77 }
78 
79 void perf_header__clear_feat(struct perf_header *header, int feat)
80 {
81 	clear_bit(feat, header->adds_features);
82 }
83 
84 bool perf_header__has_feat(const struct perf_header *header, int feat)
85 {
86 	return test_bit(feat, header->adds_features);
87 }
88 
89 static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
90 {
91 	ssize_t ret = writen(ff->fd, buf, size);
92 
93 	if (ret != (ssize_t)size)
94 		return ret < 0 ? (int)ret : -1;
95 	return 0;
96 }
97 
98 static int __do_write_buf(struct feat_fd *ff,  const void *buf, size_t size)
99 {
100 	/* struct perf_event_header::size is u16 */
101 	const size_t max_size = 0xffff - sizeof(struct perf_event_header);
102 	size_t new_size = ff->size;
103 	void *addr;
104 
105 	if (size + ff->offset > max_size)
106 		return -E2BIG;
107 
108 	while (size > (new_size - ff->offset))
109 		new_size <<= 1;
110 	new_size = min(max_size, new_size);
111 
112 	if (ff->size < new_size) {
113 		addr = realloc(ff->buf, new_size);
114 		if (!addr)
115 			return -ENOMEM;
116 		ff->buf = addr;
117 		ff->size = new_size;
118 	}
119 
120 	memcpy(ff->buf + ff->offset, buf, size);
121 	ff->offset += size;
122 
123 	return 0;
124 }
125 
126 /* Return: 0 if succeded, -ERR if failed. */
127 int do_write(struct feat_fd *ff, const void *buf, size_t size)
128 {
129 	if (!ff->buf)
130 		return __do_write_fd(ff, buf, size);
131 	return __do_write_buf(ff, buf, size);
132 }
133 
134 /* Return: 0 if succeded, -ERR if failed. */
135 int write_padded(struct feat_fd *ff, const void *bf,
136 		 size_t count, size_t count_aligned)
137 {
138 	static const char zero_buf[NAME_ALIGN];
139 	int err = do_write(ff, bf, count);
140 
141 	if (!err)
142 		err = do_write(ff, zero_buf, count_aligned - count);
143 
144 	return err;
145 }
146 
147 #define string_size(str)						\
148 	(PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
149 
150 /* Return: 0 if succeded, -ERR if failed. */
151 static int do_write_string(struct feat_fd *ff, const char *str)
152 {
153 	u32 len, olen;
154 	int ret;
155 
156 	olen = strlen(str) + 1;
157 	len = PERF_ALIGN(olen, NAME_ALIGN);
158 
159 	/* write len, incl. \0 */
160 	ret = do_write(ff, &len, sizeof(len));
161 	if (ret < 0)
162 		return ret;
163 
164 	return write_padded(ff, str, olen, len);
165 }
166 
167 static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
168 {
169 	ssize_t ret = readn(ff->fd, addr, size);
170 
171 	if (ret != size)
172 		return ret < 0 ? (int)ret : -1;
173 	return 0;
174 }
175 
176 static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
177 {
178 	if (size > (ssize_t)ff->size - ff->offset)
179 		return -1;
180 
181 	memcpy(addr, ff->buf + ff->offset, size);
182 	ff->offset += size;
183 
184 	return 0;
185 
186 }
187 
188 static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
189 {
190 	if (!ff->buf)
191 		return __do_read_fd(ff, addr, size);
192 	return __do_read_buf(ff, addr, size);
193 }
194 
195 static int do_read_u32(struct feat_fd *ff, u32 *addr)
196 {
197 	int ret;
198 
199 	ret = __do_read(ff, addr, sizeof(*addr));
200 	if (ret)
201 		return ret;
202 
203 	if (ff->ph->needs_swap)
204 		*addr = bswap_32(*addr);
205 	return 0;
206 }
207 
208 static int do_read_u64(struct feat_fd *ff, u64 *addr)
209 {
210 	int ret;
211 
212 	ret = __do_read(ff, addr, sizeof(*addr));
213 	if (ret)
214 		return ret;
215 
216 	if (ff->ph->needs_swap)
217 		*addr = bswap_64(*addr);
218 	return 0;
219 }
220 
221 static char *do_read_string(struct feat_fd *ff)
222 {
223 	u32 len;
224 	char *buf;
225 
226 	if (do_read_u32(ff, &len))
227 		return NULL;
228 
229 	buf = malloc(len);
230 	if (!buf)
231 		return NULL;
232 
233 	if (!__do_read(ff, buf, len)) {
234 		/*
235 		 * strings are padded by zeroes
236 		 * thus the actual strlen of buf
237 		 * may be less than len
238 		 */
239 		return buf;
240 	}
241 
242 	free(buf);
243 	return NULL;
244 }
245 
246 static int write_tracing_data(struct feat_fd *ff,
247 			      struct perf_evlist *evlist)
248 {
249 	if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
250 		return -1;
251 
252 	return read_tracing_data(ff->fd, &evlist->entries);
253 }
254 
255 static int write_build_id(struct feat_fd *ff,
256 			  struct perf_evlist *evlist __maybe_unused)
257 {
258 	struct perf_session *session;
259 	int err;
260 
261 	session = container_of(ff->ph, struct perf_session, header);
262 
263 	if (!perf_session__read_build_ids(session, true))
264 		return -1;
265 
266 	if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
267 		return -1;
268 
269 	err = perf_session__write_buildid_table(session, ff);
270 	if (err < 0) {
271 		pr_debug("failed to write buildid table\n");
272 		return err;
273 	}
274 	perf_session__cache_build_ids(session);
275 
276 	return 0;
277 }
278 
279 static int write_hostname(struct feat_fd *ff,
280 			  struct perf_evlist *evlist __maybe_unused)
281 {
282 	struct utsname uts;
283 	int ret;
284 
285 	ret = uname(&uts);
286 	if (ret < 0)
287 		return -1;
288 
289 	return do_write_string(ff, uts.nodename);
290 }
291 
292 static int write_osrelease(struct feat_fd *ff,
293 			   struct perf_evlist *evlist __maybe_unused)
294 {
295 	struct utsname uts;
296 	int ret;
297 
298 	ret = uname(&uts);
299 	if (ret < 0)
300 		return -1;
301 
302 	return do_write_string(ff, uts.release);
303 }
304 
305 static int write_arch(struct feat_fd *ff,
306 		      struct perf_evlist *evlist __maybe_unused)
307 {
308 	struct utsname uts;
309 	int ret;
310 
311 	ret = uname(&uts);
312 	if (ret < 0)
313 		return -1;
314 
315 	return do_write_string(ff, uts.machine);
316 }
317 
318 static int write_version(struct feat_fd *ff,
319 			 struct perf_evlist *evlist __maybe_unused)
320 {
321 	return do_write_string(ff, perf_version_string);
322 }
323 
324 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
325 {
326 	FILE *file;
327 	char *buf = NULL;
328 	char *s, *p;
329 	const char *search = cpuinfo_proc;
330 	size_t len = 0;
331 	int ret = -1;
332 
333 	if (!search)
334 		return -1;
335 
336 	file = fopen("/proc/cpuinfo", "r");
337 	if (!file)
338 		return -1;
339 
340 	while (getline(&buf, &len, file) > 0) {
341 		ret = strncmp(buf, search, strlen(search));
342 		if (!ret)
343 			break;
344 	}
345 
346 	if (ret) {
347 		ret = -1;
348 		goto done;
349 	}
350 
351 	s = buf;
352 
353 	p = strchr(buf, ':');
354 	if (p && *(p+1) == ' ' && *(p+2))
355 		s = p + 2;
356 	p = strchr(s, '\n');
357 	if (p)
358 		*p = '\0';
359 
360 	/* squash extra space characters (branding string) */
361 	p = s;
362 	while (*p) {
363 		if (isspace(*p)) {
364 			char *r = p + 1;
365 			char *q = r;
366 			*p = ' ';
367 			while (*q && isspace(*q))
368 				q++;
369 			if (q != (p+1))
370 				while ((*r++ = *q++));
371 		}
372 		p++;
373 	}
374 	ret = do_write_string(ff, s);
375 done:
376 	free(buf);
377 	fclose(file);
378 	return ret;
379 }
380 
381 static int write_cpudesc(struct feat_fd *ff,
382 		       struct perf_evlist *evlist __maybe_unused)
383 {
384 	const char *cpuinfo_procs[] = CPUINFO_PROC;
385 	unsigned int i;
386 
387 	for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
388 		int ret;
389 		ret = __write_cpudesc(ff, cpuinfo_procs[i]);
390 		if (ret >= 0)
391 			return ret;
392 	}
393 	return -1;
394 }
395 
396 
397 static int write_nrcpus(struct feat_fd *ff,
398 			struct perf_evlist *evlist __maybe_unused)
399 {
400 	long nr;
401 	u32 nrc, nra;
402 	int ret;
403 
404 	nrc = cpu__max_present_cpu();
405 
406 	nr = sysconf(_SC_NPROCESSORS_ONLN);
407 	if (nr < 0)
408 		return -1;
409 
410 	nra = (u32)(nr & UINT_MAX);
411 
412 	ret = do_write(ff, &nrc, sizeof(nrc));
413 	if (ret < 0)
414 		return ret;
415 
416 	return do_write(ff, &nra, sizeof(nra));
417 }
418 
419 static int write_event_desc(struct feat_fd *ff,
420 			    struct perf_evlist *evlist)
421 {
422 	struct perf_evsel *evsel;
423 	u32 nre, nri, sz;
424 	int ret;
425 
426 	nre = evlist->nr_entries;
427 
428 	/*
429 	 * write number of events
430 	 */
431 	ret = do_write(ff, &nre, sizeof(nre));
432 	if (ret < 0)
433 		return ret;
434 
435 	/*
436 	 * size of perf_event_attr struct
437 	 */
438 	sz = (u32)sizeof(evsel->attr);
439 	ret = do_write(ff, &sz, sizeof(sz));
440 	if (ret < 0)
441 		return ret;
442 
443 	evlist__for_each_entry(evlist, evsel) {
444 		ret = do_write(ff, &evsel->attr, sz);
445 		if (ret < 0)
446 			return ret;
447 		/*
448 		 * write number of unique id per event
449 		 * there is one id per instance of an event
450 		 *
451 		 * copy into an nri to be independent of the
452 		 * type of ids,
453 		 */
454 		nri = evsel->ids;
455 		ret = do_write(ff, &nri, sizeof(nri));
456 		if (ret < 0)
457 			return ret;
458 
459 		/*
460 		 * write event string as passed on cmdline
461 		 */
462 		ret = do_write_string(ff, perf_evsel__name(evsel));
463 		if (ret < 0)
464 			return ret;
465 		/*
466 		 * write unique ids for this event
467 		 */
468 		ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64));
469 		if (ret < 0)
470 			return ret;
471 	}
472 	return 0;
473 }
474 
475 static int write_cmdline(struct feat_fd *ff,
476 			 struct perf_evlist *evlist __maybe_unused)
477 {
478 	char buf[MAXPATHLEN];
479 	u32 n;
480 	int i, ret;
481 
482 	/* actual path to perf binary */
483 	ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
484 	if (ret <= 0)
485 		return -1;
486 
487 	/* readlink() does not add null termination */
488 	buf[ret] = '\0';
489 
490 	/* account for binary path */
491 	n = perf_env.nr_cmdline + 1;
492 
493 	ret = do_write(ff, &n, sizeof(n));
494 	if (ret < 0)
495 		return ret;
496 
497 	ret = do_write_string(ff, buf);
498 	if (ret < 0)
499 		return ret;
500 
501 	for (i = 0 ; i < perf_env.nr_cmdline; i++) {
502 		ret = do_write_string(ff, perf_env.cmdline_argv[i]);
503 		if (ret < 0)
504 			return ret;
505 	}
506 	return 0;
507 }
508 
509 #define CORE_SIB_FMT \
510 	"/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
511 #define THRD_SIB_FMT \
512 	"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
513 
514 struct cpu_topo {
515 	u32 cpu_nr;
516 	u32 core_sib;
517 	u32 thread_sib;
518 	char **core_siblings;
519 	char **thread_siblings;
520 };
521 
522 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
523 {
524 	FILE *fp;
525 	char filename[MAXPATHLEN];
526 	char *buf = NULL, *p;
527 	size_t len = 0;
528 	ssize_t sret;
529 	u32 i = 0;
530 	int ret = -1;
531 
532 	sprintf(filename, CORE_SIB_FMT, cpu);
533 	fp = fopen(filename, "r");
534 	if (!fp)
535 		goto try_threads;
536 
537 	sret = getline(&buf, &len, fp);
538 	fclose(fp);
539 	if (sret <= 0)
540 		goto try_threads;
541 
542 	p = strchr(buf, '\n');
543 	if (p)
544 		*p = '\0';
545 
546 	for (i = 0; i < tp->core_sib; i++) {
547 		if (!strcmp(buf, tp->core_siblings[i]))
548 			break;
549 	}
550 	if (i == tp->core_sib) {
551 		tp->core_siblings[i] = buf;
552 		tp->core_sib++;
553 		buf = NULL;
554 		len = 0;
555 	}
556 	ret = 0;
557 
558 try_threads:
559 	sprintf(filename, THRD_SIB_FMT, cpu);
560 	fp = fopen(filename, "r");
561 	if (!fp)
562 		goto done;
563 
564 	if (getline(&buf, &len, fp) <= 0)
565 		goto done;
566 
567 	p = strchr(buf, '\n');
568 	if (p)
569 		*p = '\0';
570 
571 	for (i = 0; i < tp->thread_sib; i++) {
572 		if (!strcmp(buf, tp->thread_siblings[i]))
573 			break;
574 	}
575 	if (i == tp->thread_sib) {
576 		tp->thread_siblings[i] = buf;
577 		tp->thread_sib++;
578 		buf = NULL;
579 	}
580 	ret = 0;
581 done:
582 	if(fp)
583 		fclose(fp);
584 	free(buf);
585 	return ret;
586 }
587 
588 static void free_cpu_topo(struct cpu_topo *tp)
589 {
590 	u32 i;
591 
592 	if (!tp)
593 		return;
594 
595 	for (i = 0 ; i < tp->core_sib; i++)
596 		zfree(&tp->core_siblings[i]);
597 
598 	for (i = 0 ; i < tp->thread_sib; i++)
599 		zfree(&tp->thread_siblings[i]);
600 
601 	free(tp);
602 }
603 
604 static struct cpu_topo *build_cpu_topology(void)
605 {
606 	struct cpu_topo *tp = NULL;
607 	void *addr;
608 	u32 nr, i;
609 	size_t sz;
610 	long ncpus;
611 	int ret = -1;
612 	struct cpu_map *map;
613 
614 	ncpus = cpu__max_present_cpu();
615 
616 	/* build online CPU map */
617 	map = cpu_map__new(NULL);
618 	if (map == NULL) {
619 		pr_debug("failed to get system cpumap\n");
620 		return NULL;
621 	}
622 
623 	nr = (u32)(ncpus & UINT_MAX);
624 
625 	sz = nr * sizeof(char *);
626 	addr = calloc(1, sizeof(*tp) + 2 * sz);
627 	if (!addr)
628 		goto out_free;
629 
630 	tp = addr;
631 	tp->cpu_nr = nr;
632 	addr += sizeof(*tp);
633 	tp->core_siblings = addr;
634 	addr += sz;
635 	tp->thread_siblings = addr;
636 
637 	for (i = 0; i < nr; i++) {
638 		if (!cpu_map__has(map, i))
639 			continue;
640 
641 		ret = build_cpu_topo(tp, i);
642 		if (ret < 0)
643 			break;
644 	}
645 
646 out_free:
647 	cpu_map__put(map);
648 	if (ret) {
649 		free_cpu_topo(tp);
650 		tp = NULL;
651 	}
652 	return tp;
653 }
654 
655 static int write_cpu_topology(struct feat_fd *ff,
656 			      struct perf_evlist *evlist __maybe_unused)
657 {
658 	struct cpu_topo *tp;
659 	u32 i;
660 	int ret, j;
661 
662 	tp = build_cpu_topology();
663 	if (!tp)
664 		return -1;
665 
666 	ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
667 	if (ret < 0)
668 		goto done;
669 
670 	for (i = 0; i < tp->core_sib; i++) {
671 		ret = do_write_string(ff, tp->core_siblings[i]);
672 		if (ret < 0)
673 			goto done;
674 	}
675 	ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
676 	if (ret < 0)
677 		goto done;
678 
679 	for (i = 0; i < tp->thread_sib; i++) {
680 		ret = do_write_string(ff, tp->thread_siblings[i]);
681 		if (ret < 0)
682 			break;
683 	}
684 
685 	ret = perf_env__read_cpu_topology_map(&perf_env);
686 	if (ret < 0)
687 		goto done;
688 
689 	for (j = 0; j < perf_env.nr_cpus_avail; j++) {
690 		ret = do_write(ff, &perf_env.cpu[j].core_id,
691 			       sizeof(perf_env.cpu[j].core_id));
692 		if (ret < 0)
693 			return ret;
694 		ret = do_write(ff, &perf_env.cpu[j].socket_id,
695 			       sizeof(perf_env.cpu[j].socket_id));
696 		if (ret < 0)
697 			return ret;
698 	}
699 done:
700 	free_cpu_topo(tp);
701 	return ret;
702 }
703 
704 
705 
706 static int write_total_mem(struct feat_fd *ff,
707 			   struct perf_evlist *evlist __maybe_unused)
708 {
709 	char *buf = NULL;
710 	FILE *fp;
711 	size_t len = 0;
712 	int ret = -1, n;
713 	uint64_t mem;
714 
715 	fp = fopen("/proc/meminfo", "r");
716 	if (!fp)
717 		return -1;
718 
719 	while (getline(&buf, &len, fp) > 0) {
720 		ret = strncmp(buf, "MemTotal:", 9);
721 		if (!ret)
722 			break;
723 	}
724 	if (!ret) {
725 		n = sscanf(buf, "%*s %"PRIu64, &mem);
726 		if (n == 1)
727 			ret = do_write(ff, &mem, sizeof(mem));
728 	} else
729 		ret = -1;
730 	free(buf);
731 	fclose(fp);
732 	return ret;
733 }
734 
735 static int write_topo_node(struct feat_fd *ff, int node)
736 {
737 	char str[MAXPATHLEN];
738 	char field[32];
739 	char *buf = NULL, *p;
740 	size_t len = 0;
741 	FILE *fp;
742 	u64 mem_total, mem_free, mem;
743 	int ret = -1;
744 
745 	sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
746 	fp = fopen(str, "r");
747 	if (!fp)
748 		return -1;
749 
750 	while (getline(&buf, &len, fp) > 0) {
751 		/* skip over invalid lines */
752 		if (!strchr(buf, ':'))
753 			continue;
754 		if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
755 			goto done;
756 		if (!strcmp(field, "MemTotal:"))
757 			mem_total = mem;
758 		if (!strcmp(field, "MemFree:"))
759 			mem_free = mem;
760 	}
761 
762 	fclose(fp);
763 	fp = NULL;
764 
765 	ret = do_write(ff, &mem_total, sizeof(u64));
766 	if (ret)
767 		goto done;
768 
769 	ret = do_write(ff, &mem_free, sizeof(u64));
770 	if (ret)
771 		goto done;
772 
773 	ret = -1;
774 	sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
775 
776 	fp = fopen(str, "r");
777 	if (!fp)
778 		goto done;
779 
780 	if (getline(&buf, &len, fp) <= 0)
781 		goto done;
782 
783 	p = strchr(buf, '\n');
784 	if (p)
785 		*p = '\0';
786 
787 	ret = do_write_string(ff, buf);
788 done:
789 	free(buf);
790 	if (fp)
791 		fclose(fp);
792 	return ret;
793 }
794 
795 static int write_numa_topology(struct feat_fd *ff,
796 			       struct perf_evlist *evlist __maybe_unused)
797 {
798 	char *buf = NULL;
799 	size_t len = 0;
800 	FILE *fp;
801 	struct cpu_map *node_map = NULL;
802 	char *c;
803 	u32 nr, i, j;
804 	int ret = -1;
805 
806 	fp = fopen("/sys/devices/system/node/online", "r");
807 	if (!fp)
808 		return -1;
809 
810 	if (getline(&buf, &len, fp) <= 0)
811 		goto done;
812 
813 	c = strchr(buf, '\n');
814 	if (c)
815 		*c = '\0';
816 
817 	node_map = cpu_map__new(buf);
818 	if (!node_map)
819 		goto done;
820 
821 	nr = (u32)node_map->nr;
822 
823 	ret = do_write(ff, &nr, sizeof(nr));
824 	if (ret < 0)
825 		goto done;
826 
827 	for (i = 0; i < nr; i++) {
828 		j = (u32)node_map->map[i];
829 		ret = do_write(ff, &j, sizeof(j));
830 		if (ret < 0)
831 			break;
832 
833 		ret = write_topo_node(ff, i);
834 		if (ret < 0)
835 			break;
836 	}
837 done:
838 	free(buf);
839 	fclose(fp);
840 	cpu_map__put(node_map);
841 	return ret;
842 }
843 
844 /*
845  * File format:
846  *
847  * struct pmu_mappings {
848  *	u32	pmu_num;
849  *	struct pmu_map {
850  *		u32	type;
851  *		char	name[];
852  *	}[pmu_num];
853  * };
854  */
855 
856 static int write_pmu_mappings(struct feat_fd *ff,
857 			      struct perf_evlist *evlist __maybe_unused)
858 {
859 	struct perf_pmu *pmu = NULL;
860 	u32 pmu_num = 0;
861 	int ret;
862 
863 	/*
864 	 * Do a first pass to count number of pmu to avoid lseek so this
865 	 * works in pipe mode as well.
866 	 */
867 	while ((pmu = perf_pmu__scan(pmu))) {
868 		if (!pmu->name)
869 			continue;
870 		pmu_num++;
871 	}
872 
873 	ret = do_write(ff, &pmu_num, sizeof(pmu_num));
874 	if (ret < 0)
875 		return ret;
876 
877 	while ((pmu = perf_pmu__scan(pmu))) {
878 		if (!pmu->name)
879 			continue;
880 
881 		ret = do_write(ff, &pmu->type, sizeof(pmu->type));
882 		if (ret < 0)
883 			return ret;
884 
885 		ret = do_write_string(ff, pmu->name);
886 		if (ret < 0)
887 			return ret;
888 	}
889 
890 	return 0;
891 }
892 
893 /*
894  * File format:
895  *
896  * struct group_descs {
897  *	u32	nr_groups;
898  *	struct group_desc {
899  *		char	name[];
900  *		u32	leader_idx;
901  *		u32	nr_members;
902  *	}[nr_groups];
903  * };
904  */
905 static int write_group_desc(struct feat_fd *ff,
906 			    struct perf_evlist *evlist)
907 {
908 	u32 nr_groups = evlist->nr_groups;
909 	struct perf_evsel *evsel;
910 	int ret;
911 
912 	ret = do_write(ff, &nr_groups, sizeof(nr_groups));
913 	if (ret < 0)
914 		return ret;
915 
916 	evlist__for_each_entry(evlist, evsel) {
917 		if (perf_evsel__is_group_leader(evsel) &&
918 		    evsel->nr_members > 1) {
919 			const char *name = evsel->group_name ?: "{anon_group}";
920 			u32 leader_idx = evsel->idx;
921 			u32 nr_members = evsel->nr_members;
922 
923 			ret = do_write_string(ff, name);
924 			if (ret < 0)
925 				return ret;
926 
927 			ret = do_write(ff, &leader_idx, sizeof(leader_idx));
928 			if (ret < 0)
929 				return ret;
930 
931 			ret = do_write(ff, &nr_members, sizeof(nr_members));
932 			if (ret < 0)
933 				return ret;
934 		}
935 	}
936 	return 0;
937 }
938 
939 /*
940  * default get_cpuid(): nothing gets recorded
941  * actual implementation must be in arch/$(SRCARCH)/util/header.c
942  */
943 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
944 {
945 	return -1;
946 }
947 
948 static int write_cpuid(struct feat_fd *ff,
949 		       struct perf_evlist *evlist __maybe_unused)
950 {
951 	char buffer[64];
952 	int ret;
953 
954 	ret = get_cpuid(buffer, sizeof(buffer));
955 	if (!ret)
956 		goto write_it;
957 
958 	return -1;
959 write_it:
960 	return do_write_string(ff, buffer);
961 }
962 
963 static int write_branch_stack(struct feat_fd *ff __maybe_unused,
964 			      struct perf_evlist *evlist __maybe_unused)
965 {
966 	return 0;
967 }
968 
969 static int write_auxtrace(struct feat_fd *ff,
970 			  struct perf_evlist *evlist __maybe_unused)
971 {
972 	struct perf_session *session;
973 	int err;
974 
975 	if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
976 		return -1;
977 
978 	session = container_of(ff->ph, struct perf_session, header);
979 
980 	err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
981 	if (err < 0)
982 		pr_err("Failed to write auxtrace index\n");
983 	return err;
984 }
985 
986 static int cpu_cache_level__sort(const void *a, const void *b)
987 {
988 	struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
989 	struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
990 
991 	return cache_a->level - cache_b->level;
992 }
993 
994 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
995 {
996 	if (a->level != b->level)
997 		return false;
998 
999 	if (a->line_size != b->line_size)
1000 		return false;
1001 
1002 	if (a->sets != b->sets)
1003 		return false;
1004 
1005 	if (a->ways != b->ways)
1006 		return false;
1007 
1008 	if (strcmp(a->type, b->type))
1009 		return false;
1010 
1011 	if (strcmp(a->size, b->size))
1012 		return false;
1013 
1014 	if (strcmp(a->map, b->map))
1015 		return false;
1016 
1017 	return true;
1018 }
1019 
1020 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1021 {
1022 	char path[PATH_MAX], file[PATH_MAX];
1023 	struct stat st;
1024 	size_t len;
1025 
1026 	scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1027 	scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1028 
1029 	if (stat(file, &st))
1030 		return 1;
1031 
1032 	scnprintf(file, PATH_MAX, "%s/level", path);
1033 	if (sysfs__read_int(file, (int *) &cache->level))
1034 		return -1;
1035 
1036 	scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1037 	if (sysfs__read_int(file, (int *) &cache->line_size))
1038 		return -1;
1039 
1040 	scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1041 	if (sysfs__read_int(file, (int *) &cache->sets))
1042 		return -1;
1043 
1044 	scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1045 	if (sysfs__read_int(file, (int *) &cache->ways))
1046 		return -1;
1047 
1048 	scnprintf(file, PATH_MAX, "%s/type", path);
1049 	if (sysfs__read_str(file, &cache->type, &len))
1050 		return -1;
1051 
1052 	cache->type[len] = 0;
1053 	cache->type = rtrim(cache->type);
1054 
1055 	scnprintf(file, PATH_MAX, "%s/size", path);
1056 	if (sysfs__read_str(file, &cache->size, &len)) {
1057 		free(cache->type);
1058 		return -1;
1059 	}
1060 
1061 	cache->size[len] = 0;
1062 	cache->size = rtrim(cache->size);
1063 
1064 	scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1065 	if (sysfs__read_str(file, &cache->map, &len)) {
1066 		free(cache->map);
1067 		free(cache->type);
1068 		return -1;
1069 	}
1070 
1071 	cache->map[len] = 0;
1072 	cache->map = rtrim(cache->map);
1073 	return 0;
1074 }
1075 
1076 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1077 {
1078 	fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1079 }
1080 
1081 static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1082 {
1083 	u32 i, cnt = 0;
1084 	long ncpus;
1085 	u32 nr, cpu;
1086 	u16 level;
1087 
1088 	ncpus = sysconf(_SC_NPROCESSORS_CONF);
1089 	if (ncpus < 0)
1090 		return -1;
1091 
1092 	nr = (u32)(ncpus & UINT_MAX);
1093 
1094 	for (cpu = 0; cpu < nr; cpu++) {
1095 		for (level = 0; level < 10; level++) {
1096 			struct cpu_cache_level c;
1097 			int err;
1098 
1099 			err = cpu_cache_level__read(&c, cpu, level);
1100 			if (err < 0)
1101 				return err;
1102 
1103 			if (err == 1)
1104 				break;
1105 
1106 			for (i = 0; i < cnt; i++) {
1107 				if (cpu_cache_level__cmp(&c, &caches[i]))
1108 					break;
1109 			}
1110 
1111 			if (i == cnt)
1112 				caches[cnt++] = c;
1113 			else
1114 				cpu_cache_level__free(&c);
1115 
1116 			if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1117 				goto out;
1118 		}
1119 	}
1120  out:
1121 	*cntp = cnt;
1122 	return 0;
1123 }
1124 
1125 #define MAX_CACHES 2000
1126 
1127 static int write_cache(struct feat_fd *ff,
1128 		       struct perf_evlist *evlist __maybe_unused)
1129 {
1130 	struct cpu_cache_level caches[MAX_CACHES];
1131 	u32 cnt = 0, i, version = 1;
1132 	int ret;
1133 
1134 	ret = build_caches(caches, MAX_CACHES, &cnt);
1135 	if (ret)
1136 		goto out;
1137 
1138 	qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1139 
1140 	ret = do_write(ff, &version, sizeof(u32));
1141 	if (ret < 0)
1142 		goto out;
1143 
1144 	ret = do_write(ff, &cnt, sizeof(u32));
1145 	if (ret < 0)
1146 		goto out;
1147 
1148 	for (i = 0; i < cnt; i++) {
1149 		struct cpu_cache_level *c = &caches[i];
1150 
1151 		#define _W(v)					\
1152 			ret = do_write(ff, &c->v, sizeof(u32));	\
1153 			if (ret < 0)				\
1154 				goto out;
1155 
1156 		_W(level)
1157 		_W(line_size)
1158 		_W(sets)
1159 		_W(ways)
1160 		#undef _W
1161 
1162 		#define _W(v)						\
1163 			ret = do_write_string(ff, (const char *) c->v);	\
1164 			if (ret < 0)					\
1165 				goto out;
1166 
1167 		_W(type)
1168 		_W(size)
1169 		_W(map)
1170 		#undef _W
1171 	}
1172 
1173 out:
1174 	for (i = 0; i < cnt; i++)
1175 		cpu_cache_level__free(&caches[i]);
1176 	return ret;
1177 }
1178 
1179 static int write_stat(struct feat_fd *ff __maybe_unused,
1180 		      struct perf_evlist *evlist __maybe_unused)
1181 {
1182 	return 0;
1183 }
1184 
1185 static int write_sample_time(struct feat_fd *ff,
1186 			     struct perf_evlist *evlist)
1187 {
1188 	int ret;
1189 
1190 	ret = do_write(ff, &evlist->first_sample_time,
1191 		       sizeof(evlist->first_sample_time));
1192 	if (ret < 0)
1193 		return ret;
1194 
1195 	return do_write(ff, &evlist->last_sample_time,
1196 			sizeof(evlist->last_sample_time));
1197 }
1198 
1199 static void print_hostname(struct feat_fd *ff, FILE *fp)
1200 {
1201 	fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
1202 }
1203 
1204 static void print_osrelease(struct feat_fd *ff, FILE *fp)
1205 {
1206 	fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
1207 }
1208 
1209 static void print_arch(struct feat_fd *ff, FILE *fp)
1210 {
1211 	fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
1212 }
1213 
1214 static void print_cpudesc(struct feat_fd *ff, FILE *fp)
1215 {
1216 	fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
1217 }
1218 
1219 static void print_nrcpus(struct feat_fd *ff, FILE *fp)
1220 {
1221 	fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1222 	fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
1223 }
1224 
1225 static void print_version(struct feat_fd *ff, FILE *fp)
1226 {
1227 	fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
1228 }
1229 
1230 static void print_cmdline(struct feat_fd *ff, FILE *fp)
1231 {
1232 	int nr, i;
1233 
1234 	nr = ff->ph->env.nr_cmdline;
1235 
1236 	fprintf(fp, "# cmdline : ");
1237 
1238 	for (i = 0; i < nr; i++)
1239 		fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1240 	fputc('\n', fp);
1241 }
1242 
1243 static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
1244 {
1245 	struct perf_header *ph = ff->ph;
1246 	int cpu_nr = ph->env.nr_cpus_avail;
1247 	int nr, i;
1248 	char *str;
1249 
1250 	nr = ph->env.nr_sibling_cores;
1251 	str = ph->env.sibling_cores;
1252 
1253 	for (i = 0; i < nr; i++) {
1254 		fprintf(fp, "# sibling cores   : %s\n", str);
1255 		str += strlen(str) + 1;
1256 	}
1257 
1258 	nr = ph->env.nr_sibling_threads;
1259 	str = ph->env.sibling_threads;
1260 
1261 	for (i = 0; i < nr; i++) {
1262 		fprintf(fp, "# sibling threads : %s\n", str);
1263 		str += strlen(str) + 1;
1264 	}
1265 
1266 	if (ph->env.cpu != NULL) {
1267 		for (i = 0; i < cpu_nr; i++)
1268 			fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1269 				ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1270 	} else
1271 		fprintf(fp, "# Core ID and Socket ID information is not available\n");
1272 }
1273 
1274 static void free_event_desc(struct perf_evsel *events)
1275 {
1276 	struct perf_evsel *evsel;
1277 
1278 	if (!events)
1279 		return;
1280 
1281 	for (evsel = events; evsel->attr.size; evsel++) {
1282 		zfree(&evsel->name);
1283 		zfree(&evsel->id);
1284 	}
1285 
1286 	free(events);
1287 }
1288 
1289 static struct perf_evsel *read_event_desc(struct feat_fd *ff)
1290 {
1291 	struct perf_evsel *evsel, *events = NULL;
1292 	u64 *id;
1293 	void *buf = NULL;
1294 	u32 nre, sz, nr, i, j;
1295 	size_t msz;
1296 
1297 	/* number of events */
1298 	if (do_read_u32(ff, &nre))
1299 		goto error;
1300 
1301 	if (do_read_u32(ff, &sz))
1302 		goto error;
1303 
1304 	/* buffer to hold on file attr struct */
1305 	buf = malloc(sz);
1306 	if (!buf)
1307 		goto error;
1308 
1309 	/* the last event terminates with evsel->attr.size == 0: */
1310 	events = calloc(nre + 1, sizeof(*events));
1311 	if (!events)
1312 		goto error;
1313 
1314 	msz = sizeof(evsel->attr);
1315 	if (sz < msz)
1316 		msz = sz;
1317 
1318 	for (i = 0, evsel = events; i < nre; evsel++, i++) {
1319 		evsel->idx = i;
1320 
1321 		/*
1322 		 * must read entire on-file attr struct to
1323 		 * sync up with layout.
1324 		 */
1325 		if (__do_read(ff, buf, sz))
1326 			goto error;
1327 
1328 		if (ff->ph->needs_swap)
1329 			perf_event__attr_swap(buf);
1330 
1331 		memcpy(&evsel->attr, buf, msz);
1332 
1333 		if (do_read_u32(ff, &nr))
1334 			goto error;
1335 
1336 		if (ff->ph->needs_swap)
1337 			evsel->needs_swap = true;
1338 
1339 		evsel->name = do_read_string(ff);
1340 		if (!evsel->name)
1341 			goto error;
1342 
1343 		if (!nr)
1344 			continue;
1345 
1346 		id = calloc(nr, sizeof(*id));
1347 		if (!id)
1348 			goto error;
1349 		evsel->ids = nr;
1350 		evsel->id = id;
1351 
1352 		for (j = 0 ; j < nr; j++) {
1353 			if (do_read_u64(ff, id))
1354 				goto error;
1355 			id++;
1356 		}
1357 	}
1358 out:
1359 	free(buf);
1360 	return events;
1361 error:
1362 	free_event_desc(events);
1363 	events = NULL;
1364 	goto out;
1365 }
1366 
1367 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1368 				void *priv __maybe_unused)
1369 {
1370 	return fprintf(fp, ", %s = %s", name, val);
1371 }
1372 
1373 static void print_event_desc(struct feat_fd *ff, FILE *fp)
1374 {
1375 	struct perf_evsel *evsel, *events;
1376 	u32 j;
1377 	u64 *id;
1378 
1379 	if (ff->events)
1380 		events = ff->events;
1381 	else
1382 		events = read_event_desc(ff);
1383 
1384 	if (!events) {
1385 		fprintf(fp, "# event desc: not available or unable to read\n");
1386 		return;
1387 	}
1388 
1389 	for (evsel = events; evsel->attr.size; evsel++) {
1390 		fprintf(fp, "# event : name = %s, ", evsel->name);
1391 
1392 		if (evsel->ids) {
1393 			fprintf(fp, ", id = {");
1394 			for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1395 				if (j)
1396 					fputc(',', fp);
1397 				fprintf(fp, " %"PRIu64, *id);
1398 			}
1399 			fprintf(fp, " }");
1400 		}
1401 
1402 		perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
1403 
1404 		fputc('\n', fp);
1405 	}
1406 
1407 	free_event_desc(events);
1408 	ff->events = NULL;
1409 }
1410 
1411 static void print_total_mem(struct feat_fd *ff, FILE *fp)
1412 {
1413 	fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
1414 }
1415 
1416 static void print_numa_topology(struct feat_fd *ff, FILE *fp)
1417 {
1418 	int i;
1419 	struct numa_node *n;
1420 
1421 	for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1422 		n = &ff->ph->env.numa_nodes[i];
1423 
1424 		fprintf(fp, "# node%u meminfo  : total = %"PRIu64" kB,"
1425 			    " free = %"PRIu64" kB\n",
1426 			n->node, n->mem_total, n->mem_free);
1427 
1428 		fprintf(fp, "# node%u cpu list : ", n->node);
1429 		cpu_map__fprintf(n->map, fp);
1430 	}
1431 }
1432 
1433 static void print_cpuid(struct feat_fd *ff, FILE *fp)
1434 {
1435 	fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
1436 }
1437 
1438 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
1439 {
1440 	fprintf(fp, "# contains samples with branch stack\n");
1441 }
1442 
1443 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
1444 {
1445 	fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1446 }
1447 
1448 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
1449 {
1450 	fprintf(fp, "# contains stat data\n");
1451 }
1452 
1453 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
1454 {
1455 	int i;
1456 
1457 	fprintf(fp, "# CPU cache info:\n");
1458 	for (i = 0; i < ff->ph->env.caches_cnt; i++) {
1459 		fprintf(fp, "#  ");
1460 		cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
1461 	}
1462 }
1463 
1464 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
1465 {
1466 	const char *delimiter = "# pmu mappings: ";
1467 	char *str, *tmp;
1468 	u32 pmu_num;
1469 	u32 type;
1470 
1471 	pmu_num = ff->ph->env.nr_pmu_mappings;
1472 	if (!pmu_num) {
1473 		fprintf(fp, "# pmu mappings: not available\n");
1474 		return;
1475 	}
1476 
1477 	str = ff->ph->env.pmu_mappings;
1478 
1479 	while (pmu_num) {
1480 		type = strtoul(str, &tmp, 0);
1481 		if (*tmp != ':')
1482 			goto error;
1483 
1484 		str = tmp + 1;
1485 		fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1486 
1487 		delimiter = ", ";
1488 		str += strlen(str) + 1;
1489 		pmu_num--;
1490 	}
1491 
1492 	fprintf(fp, "\n");
1493 
1494 	if (!pmu_num)
1495 		return;
1496 error:
1497 	fprintf(fp, "# pmu mappings: unable to read\n");
1498 }
1499 
1500 static void print_group_desc(struct feat_fd *ff, FILE *fp)
1501 {
1502 	struct perf_session *session;
1503 	struct perf_evsel *evsel;
1504 	u32 nr = 0;
1505 
1506 	session = container_of(ff->ph, struct perf_session, header);
1507 
1508 	evlist__for_each_entry(session->evlist, evsel) {
1509 		if (perf_evsel__is_group_leader(evsel) &&
1510 		    evsel->nr_members > 1) {
1511 			fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1512 				perf_evsel__name(evsel));
1513 
1514 			nr = evsel->nr_members - 1;
1515 		} else if (nr) {
1516 			fprintf(fp, ",%s", perf_evsel__name(evsel));
1517 
1518 			if (--nr == 0)
1519 				fprintf(fp, "}\n");
1520 		}
1521 	}
1522 }
1523 
1524 static void print_sample_time(struct feat_fd *ff, FILE *fp)
1525 {
1526 	struct perf_session *session;
1527 	char time_buf[32];
1528 	double d;
1529 
1530 	session = container_of(ff->ph, struct perf_session, header);
1531 
1532 	timestamp__scnprintf_usec(session->evlist->first_sample_time,
1533 				  time_buf, sizeof(time_buf));
1534 	fprintf(fp, "# time of first sample : %s\n", time_buf);
1535 
1536 	timestamp__scnprintf_usec(session->evlist->last_sample_time,
1537 				  time_buf, sizeof(time_buf));
1538 	fprintf(fp, "# time of last sample : %s\n", time_buf);
1539 
1540 	d = (double)(session->evlist->last_sample_time -
1541 		session->evlist->first_sample_time) / NSEC_PER_MSEC;
1542 
1543 	fprintf(fp, "# sample duration : %10.3f ms\n", d);
1544 }
1545 
1546 static int __event_process_build_id(struct build_id_event *bev,
1547 				    char *filename,
1548 				    struct perf_session *session)
1549 {
1550 	int err = -1;
1551 	struct machine *machine;
1552 	u16 cpumode;
1553 	struct dso *dso;
1554 	enum dso_kernel_type dso_type;
1555 
1556 	machine = perf_session__findnew_machine(session, bev->pid);
1557 	if (!machine)
1558 		goto out;
1559 
1560 	cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1561 
1562 	switch (cpumode) {
1563 	case PERF_RECORD_MISC_KERNEL:
1564 		dso_type = DSO_TYPE_KERNEL;
1565 		break;
1566 	case PERF_RECORD_MISC_GUEST_KERNEL:
1567 		dso_type = DSO_TYPE_GUEST_KERNEL;
1568 		break;
1569 	case PERF_RECORD_MISC_USER:
1570 	case PERF_RECORD_MISC_GUEST_USER:
1571 		dso_type = DSO_TYPE_USER;
1572 		break;
1573 	default:
1574 		goto out;
1575 	}
1576 
1577 	dso = machine__findnew_dso(machine, filename);
1578 	if (dso != NULL) {
1579 		char sbuild_id[SBUILD_ID_SIZE];
1580 
1581 		dso__set_build_id(dso, &bev->build_id);
1582 
1583 		if (dso_type != DSO_TYPE_USER) {
1584 			struct kmod_path m = { .name = NULL, };
1585 
1586 			if (!kmod_path__parse_name(&m, filename) && m.kmod)
1587 				dso__set_module_info(dso, &m, machine);
1588 			else
1589 				dso->kernel = dso_type;
1590 
1591 			free(m.name);
1592 		}
1593 
1594 		build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1595 				  sbuild_id);
1596 		pr_debug("build id event received for %s: %s\n",
1597 			 dso->long_name, sbuild_id);
1598 		dso__put(dso);
1599 	}
1600 
1601 	err = 0;
1602 out:
1603 	return err;
1604 }
1605 
1606 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1607 						 int input, u64 offset, u64 size)
1608 {
1609 	struct perf_session *session = container_of(header, struct perf_session, header);
1610 	struct {
1611 		struct perf_event_header   header;
1612 		u8			   build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1613 		char			   filename[0];
1614 	} old_bev;
1615 	struct build_id_event bev;
1616 	char filename[PATH_MAX];
1617 	u64 limit = offset + size;
1618 
1619 	while (offset < limit) {
1620 		ssize_t len;
1621 
1622 		if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1623 			return -1;
1624 
1625 		if (header->needs_swap)
1626 			perf_event_header__bswap(&old_bev.header);
1627 
1628 		len = old_bev.header.size - sizeof(old_bev);
1629 		if (readn(input, filename, len) != len)
1630 			return -1;
1631 
1632 		bev.header = old_bev.header;
1633 
1634 		/*
1635 		 * As the pid is the missing value, we need to fill
1636 		 * it properly. The header.misc value give us nice hint.
1637 		 */
1638 		bev.pid	= HOST_KERNEL_ID;
1639 		if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1640 		    bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1641 			bev.pid	= DEFAULT_GUEST_KERNEL_ID;
1642 
1643 		memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1644 		__event_process_build_id(&bev, filename, session);
1645 
1646 		offset += bev.header.size;
1647 	}
1648 
1649 	return 0;
1650 }
1651 
1652 static int perf_header__read_build_ids(struct perf_header *header,
1653 				       int input, u64 offset, u64 size)
1654 {
1655 	struct perf_session *session = container_of(header, struct perf_session, header);
1656 	struct build_id_event bev;
1657 	char filename[PATH_MAX];
1658 	u64 limit = offset + size, orig_offset = offset;
1659 	int err = -1;
1660 
1661 	while (offset < limit) {
1662 		ssize_t len;
1663 
1664 		if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1665 			goto out;
1666 
1667 		if (header->needs_swap)
1668 			perf_event_header__bswap(&bev.header);
1669 
1670 		len = bev.header.size - sizeof(bev);
1671 		if (readn(input, filename, len) != len)
1672 			goto out;
1673 		/*
1674 		 * The a1645ce1 changeset:
1675 		 *
1676 		 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1677 		 *
1678 		 * Added a field to struct build_id_event that broke the file
1679 		 * format.
1680 		 *
1681 		 * Since the kernel build-id is the first entry, process the
1682 		 * table using the old format if the well known
1683 		 * '[kernel.kallsyms]' string for the kernel build-id has the
1684 		 * first 4 characters chopped off (where the pid_t sits).
1685 		 */
1686 		if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1687 			if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1688 				return -1;
1689 			return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1690 		}
1691 
1692 		__event_process_build_id(&bev, filename, session);
1693 
1694 		offset += bev.header.size;
1695 	}
1696 	err = 0;
1697 out:
1698 	return err;
1699 }
1700 
1701 /* Macro for features that simply need to read and store a string. */
1702 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
1703 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
1704 {\
1705 	ff->ph->env.__feat_env = do_read_string(ff); \
1706 	return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
1707 }
1708 
1709 FEAT_PROCESS_STR_FUN(hostname, hostname);
1710 FEAT_PROCESS_STR_FUN(osrelease, os_release);
1711 FEAT_PROCESS_STR_FUN(version, version);
1712 FEAT_PROCESS_STR_FUN(arch, arch);
1713 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
1714 FEAT_PROCESS_STR_FUN(cpuid, cpuid);
1715 
1716 static int process_tracing_data(struct feat_fd *ff, void *data)
1717 {
1718 	ssize_t ret = trace_report(ff->fd, data, false);
1719 
1720 	return ret < 0 ? -1 : 0;
1721 }
1722 
1723 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
1724 {
1725 	if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
1726 		pr_debug("Failed to read buildids, continuing...\n");
1727 	return 0;
1728 }
1729 
1730 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
1731 {
1732 	int ret;
1733 	u32 nr_cpus_avail, nr_cpus_online;
1734 
1735 	ret = do_read_u32(ff, &nr_cpus_avail);
1736 	if (ret)
1737 		return ret;
1738 
1739 	ret = do_read_u32(ff, &nr_cpus_online);
1740 	if (ret)
1741 		return ret;
1742 	ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
1743 	ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
1744 	return 0;
1745 }
1746 
1747 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
1748 {
1749 	u64 total_mem;
1750 	int ret;
1751 
1752 	ret = do_read_u64(ff, &total_mem);
1753 	if (ret)
1754 		return -1;
1755 	ff->ph->env.total_mem = (unsigned long long)total_mem;
1756 	return 0;
1757 }
1758 
1759 static struct perf_evsel *
1760 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1761 {
1762 	struct perf_evsel *evsel;
1763 
1764 	evlist__for_each_entry(evlist, evsel) {
1765 		if (evsel->idx == idx)
1766 			return evsel;
1767 	}
1768 
1769 	return NULL;
1770 }
1771 
1772 static void
1773 perf_evlist__set_event_name(struct perf_evlist *evlist,
1774 			    struct perf_evsel *event)
1775 {
1776 	struct perf_evsel *evsel;
1777 
1778 	if (!event->name)
1779 		return;
1780 
1781 	evsel = perf_evlist__find_by_index(evlist, event->idx);
1782 	if (!evsel)
1783 		return;
1784 
1785 	if (evsel->name)
1786 		return;
1787 
1788 	evsel->name = strdup(event->name);
1789 }
1790 
1791 static int
1792 process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
1793 {
1794 	struct perf_session *session;
1795 	struct perf_evsel *evsel, *events = read_event_desc(ff);
1796 
1797 	if (!events)
1798 		return 0;
1799 
1800 	session = container_of(ff->ph, struct perf_session, header);
1801 
1802 	if (session->data->is_pipe) {
1803 		/* Save events for reading later by print_event_desc,
1804 		 * since they can't be read again in pipe mode. */
1805 		ff->events = events;
1806 	}
1807 
1808 	for (evsel = events; evsel->attr.size; evsel++)
1809 		perf_evlist__set_event_name(session->evlist, evsel);
1810 
1811 	if (!session->data->is_pipe)
1812 		free_event_desc(events);
1813 
1814 	return 0;
1815 }
1816 
1817 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
1818 {
1819 	char *str, *cmdline = NULL, **argv = NULL;
1820 	u32 nr, i, len = 0;
1821 
1822 	if (do_read_u32(ff, &nr))
1823 		return -1;
1824 
1825 	ff->ph->env.nr_cmdline = nr;
1826 
1827 	cmdline = zalloc(ff->size + nr + 1);
1828 	if (!cmdline)
1829 		return -1;
1830 
1831 	argv = zalloc(sizeof(char *) * (nr + 1));
1832 	if (!argv)
1833 		goto error;
1834 
1835 	for (i = 0; i < nr; i++) {
1836 		str = do_read_string(ff);
1837 		if (!str)
1838 			goto error;
1839 
1840 		argv[i] = cmdline + len;
1841 		memcpy(argv[i], str, strlen(str) + 1);
1842 		len += strlen(str) + 1;
1843 		free(str);
1844 	}
1845 	ff->ph->env.cmdline = cmdline;
1846 	ff->ph->env.cmdline_argv = (const char **) argv;
1847 	return 0;
1848 
1849 error:
1850 	free(argv);
1851 	free(cmdline);
1852 	return -1;
1853 }
1854 
1855 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
1856 {
1857 	u32 nr, i;
1858 	char *str;
1859 	struct strbuf sb;
1860 	int cpu_nr = ff->ph->env.nr_cpus_avail;
1861 	u64 size = 0;
1862 	struct perf_header *ph = ff->ph;
1863 
1864 	ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1865 	if (!ph->env.cpu)
1866 		return -1;
1867 
1868 	if (do_read_u32(ff, &nr))
1869 		goto free_cpu;
1870 
1871 	ph->env.nr_sibling_cores = nr;
1872 	size += sizeof(u32);
1873 	if (strbuf_init(&sb, 128) < 0)
1874 		goto free_cpu;
1875 
1876 	for (i = 0; i < nr; i++) {
1877 		str = do_read_string(ff);
1878 		if (!str)
1879 			goto error;
1880 
1881 		/* include a NULL character at the end */
1882 		if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1883 			goto error;
1884 		size += string_size(str);
1885 		free(str);
1886 	}
1887 	ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1888 
1889 	if (do_read_u32(ff, &nr))
1890 		return -1;
1891 
1892 	ph->env.nr_sibling_threads = nr;
1893 	size += sizeof(u32);
1894 
1895 	for (i = 0; i < nr; i++) {
1896 		str = do_read_string(ff);
1897 		if (!str)
1898 			goto error;
1899 
1900 		/* include a NULL character at the end */
1901 		if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1902 			goto error;
1903 		size += string_size(str);
1904 		free(str);
1905 	}
1906 	ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1907 
1908 	/*
1909 	 * The header may be from old perf,
1910 	 * which doesn't include core id and socket id information.
1911 	 */
1912 	if (ff->size <= size) {
1913 		zfree(&ph->env.cpu);
1914 		return 0;
1915 	}
1916 
1917 	for (i = 0; i < (u32)cpu_nr; i++) {
1918 		if (do_read_u32(ff, &nr))
1919 			goto free_cpu;
1920 
1921 		ph->env.cpu[i].core_id = nr;
1922 
1923 		if (do_read_u32(ff, &nr))
1924 			goto free_cpu;
1925 
1926 		if (nr != (u32)-1 && nr > (u32)cpu_nr) {
1927 			pr_debug("socket_id number is too big."
1928 				 "You may need to upgrade the perf tool.\n");
1929 			goto free_cpu;
1930 		}
1931 
1932 		ph->env.cpu[i].socket_id = nr;
1933 	}
1934 
1935 	return 0;
1936 
1937 error:
1938 	strbuf_release(&sb);
1939 free_cpu:
1940 	zfree(&ph->env.cpu);
1941 	return -1;
1942 }
1943 
1944 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
1945 {
1946 	struct numa_node *nodes, *n;
1947 	u32 nr, i;
1948 	char *str;
1949 
1950 	/* nr nodes */
1951 	if (do_read_u32(ff, &nr))
1952 		return -1;
1953 
1954 	nodes = zalloc(sizeof(*nodes) * nr);
1955 	if (!nodes)
1956 		return -ENOMEM;
1957 
1958 	for (i = 0; i < nr; i++) {
1959 		n = &nodes[i];
1960 
1961 		/* node number */
1962 		if (do_read_u32(ff, &n->node))
1963 			goto error;
1964 
1965 		if (do_read_u64(ff, &n->mem_total))
1966 			goto error;
1967 
1968 		if (do_read_u64(ff, &n->mem_free))
1969 			goto error;
1970 
1971 		str = do_read_string(ff);
1972 		if (!str)
1973 			goto error;
1974 
1975 		n->map = cpu_map__new(str);
1976 		if (!n->map)
1977 			goto error;
1978 
1979 		free(str);
1980 	}
1981 	ff->ph->env.nr_numa_nodes = nr;
1982 	ff->ph->env.numa_nodes = nodes;
1983 	return 0;
1984 
1985 error:
1986 	free(nodes);
1987 	return -1;
1988 }
1989 
1990 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
1991 {
1992 	char *name;
1993 	u32 pmu_num;
1994 	u32 type;
1995 	struct strbuf sb;
1996 
1997 	if (do_read_u32(ff, &pmu_num))
1998 		return -1;
1999 
2000 	if (!pmu_num) {
2001 		pr_debug("pmu mappings not available\n");
2002 		return 0;
2003 	}
2004 
2005 	ff->ph->env.nr_pmu_mappings = pmu_num;
2006 	if (strbuf_init(&sb, 128) < 0)
2007 		return -1;
2008 
2009 	while (pmu_num) {
2010 		if (do_read_u32(ff, &type))
2011 			goto error;
2012 
2013 		name = do_read_string(ff);
2014 		if (!name)
2015 			goto error;
2016 
2017 		if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2018 			goto error;
2019 		/* include a NULL character at the end */
2020 		if (strbuf_add(&sb, "", 1) < 0)
2021 			goto error;
2022 
2023 		if (!strcmp(name, "msr"))
2024 			ff->ph->env.msr_pmu_type = type;
2025 
2026 		free(name);
2027 		pmu_num--;
2028 	}
2029 	ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2030 	return 0;
2031 
2032 error:
2033 	strbuf_release(&sb);
2034 	return -1;
2035 }
2036 
2037 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
2038 {
2039 	size_t ret = -1;
2040 	u32 i, nr, nr_groups;
2041 	struct perf_session *session;
2042 	struct perf_evsel *evsel, *leader = NULL;
2043 	struct group_desc {
2044 		char *name;
2045 		u32 leader_idx;
2046 		u32 nr_members;
2047 	} *desc;
2048 
2049 	if (do_read_u32(ff, &nr_groups))
2050 		return -1;
2051 
2052 	ff->ph->env.nr_groups = nr_groups;
2053 	if (!nr_groups) {
2054 		pr_debug("group desc not available\n");
2055 		return 0;
2056 	}
2057 
2058 	desc = calloc(nr_groups, sizeof(*desc));
2059 	if (!desc)
2060 		return -1;
2061 
2062 	for (i = 0; i < nr_groups; i++) {
2063 		desc[i].name = do_read_string(ff);
2064 		if (!desc[i].name)
2065 			goto out_free;
2066 
2067 		if (do_read_u32(ff, &desc[i].leader_idx))
2068 			goto out_free;
2069 
2070 		if (do_read_u32(ff, &desc[i].nr_members))
2071 			goto out_free;
2072 	}
2073 
2074 	/*
2075 	 * Rebuild group relationship based on the group_desc
2076 	 */
2077 	session = container_of(ff->ph, struct perf_session, header);
2078 	session->evlist->nr_groups = nr_groups;
2079 
2080 	i = nr = 0;
2081 	evlist__for_each_entry(session->evlist, evsel) {
2082 		if (evsel->idx == (int) desc[i].leader_idx) {
2083 			evsel->leader = evsel;
2084 			/* {anon_group} is a dummy name */
2085 			if (strcmp(desc[i].name, "{anon_group}")) {
2086 				evsel->group_name = desc[i].name;
2087 				desc[i].name = NULL;
2088 			}
2089 			evsel->nr_members = desc[i].nr_members;
2090 
2091 			if (i >= nr_groups || nr > 0) {
2092 				pr_debug("invalid group desc\n");
2093 				goto out_free;
2094 			}
2095 
2096 			leader = evsel;
2097 			nr = evsel->nr_members - 1;
2098 			i++;
2099 		} else if (nr) {
2100 			/* This is a group member */
2101 			evsel->leader = leader;
2102 
2103 			nr--;
2104 		}
2105 	}
2106 
2107 	if (i != nr_groups || nr != 0) {
2108 		pr_debug("invalid group desc\n");
2109 		goto out_free;
2110 	}
2111 
2112 	ret = 0;
2113 out_free:
2114 	for (i = 0; i < nr_groups; i++)
2115 		zfree(&desc[i].name);
2116 	free(desc);
2117 
2118 	return ret;
2119 }
2120 
2121 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
2122 {
2123 	struct perf_session *session;
2124 	int err;
2125 
2126 	session = container_of(ff->ph, struct perf_session, header);
2127 
2128 	err = auxtrace_index__process(ff->fd, ff->size, session,
2129 				      ff->ph->needs_swap);
2130 	if (err < 0)
2131 		pr_err("Failed to process auxtrace index\n");
2132 	return err;
2133 }
2134 
2135 static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
2136 {
2137 	struct cpu_cache_level *caches;
2138 	u32 cnt, i, version;
2139 
2140 	if (do_read_u32(ff, &version))
2141 		return -1;
2142 
2143 	if (version != 1)
2144 		return -1;
2145 
2146 	if (do_read_u32(ff, &cnt))
2147 		return -1;
2148 
2149 	caches = zalloc(sizeof(*caches) * cnt);
2150 	if (!caches)
2151 		return -1;
2152 
2153 	for (i = 0; i < cnt; i++) {
2154 		struct cpu_cache_level c;
2155 
2156 		#define _R(v)						\
2157 			if (do_read_u32(ff, &c.v))\
2158 				goto out_free_caches;			\
2159 
2160 		_R(level)
2161 		_R(line_size)
2162 		_R(sets)
2163 		_R(ways)
2164 		#undef _R
2165 
2166 		#define _R(v)					\
2167 			c.v = do_read_string(ff);		\
2168 			if (!c.v)				\
2169 				goto out_free_caches;
2170 
2171 		_R(type)
2172 		_R(size)
2173 		_R(map)
2174 		#undef _R
2175 
2176 		caches[i] = c;
2177 	}
2178 
2179 	ff->ph->env.caches = caches;
2180 	ff->ph->env.caches_cnt = cnt;
2181 	return 0;
2182 out_free_caches:
2183 	free(caches);
2184 	return -1;
2185 }
2186 
2187 static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2188 {
2189 	struct perf_session *session;
2190 	u64 first_sample_time, last_sample_time;
2191 	int ret;
2192 
2193 	session = container_of(ff->ph, struct perf_session, header);
2194 
2195 	ret = do_read_u64(ff, &first_sample_time);
2196 	if (ret)
2197 		return -1;
2198 
2199 	ret = do_read_u64(ff, &last_sample_time);
2200 	if (ret)
2201 		return -1;
2202 
2203 	session->evlist->first_sample_time = first_sample_time;
2204 	session->evlist->last_sample_time = last_sample_time;
2205 	return 0;
2206 }
2207 
2208 struct feature_ops {
2209 	int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
2210 	void (*print)(struct feat_fd *ff, FILE *fp);
2211 	int (*process)(struct feat_fd *ff, void *data);
2212 	const char *name;
2213 	bool full_only;
2214 	bool synthesize;
2215 };
2216 
2217 #define FEAT_OPR(n, func, __full_only) \
2218 	[HEADER_##n] = {					\
2219 		.name	    = __stringify(n),			\
2220 		.write	    = write_##func,			\
2221 		.print	    = print_##func,			\
2222 		.full_only  = __full_only,			\
2223 		.process    = process_##func,			\
2224 		.synthesize = true				\
2225 	}
2226 
2227 #define FEAT_OPN(n, func, __full_only) \
2228 	[HEADER_##n] = {					\
2229 		.name	    = __stringify(n),			\
2230 		.write	    = write_##func,			\
2231 		.print	    = print_##func,			\
2232 		.full_only  = __full_only,			\
2233 		.process    = process_##func			\
2234 	}
2235 
2236 /* feature_ops not implemented: */
2237 #define print_tracing_data	NULL
2238 #define print_build_id		NULL
2239 
2240 #define process_branch_stack	NULL
2241 #define process_stat		NULL
2242 
2243 
2244 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
2245 	FEAT_OPN(TRACING_DATA,	tracing_data,	false),
2246 	FEAT_OPN(BUILD_ID,	build_id,	false),
2247 	FEAT_OPR(HOSTNAME,	hostname,	false),
2248 	FEAT_OPR(OSRELEASE,	osrelease,	false),
2249 	FEAT_OPR(VERSION,	version,	false),
2250 	FEAT_OPR(ARCH,		arch,		false),
2251 	FEAT_OPR(NRCPUS,	nrcpus,		false),
2252 	FEAT_OPR(CPUDESC,	cpudesc,	false),
2253 	FEAT_OPR(CPUID,		cpuid,		false),
2254 	FEAT_OPR(TOTAL_MEM,	total_mem,	false),
2255 	FEAT_OPR(EVENT_DESC,	event_desc,	false),
2256 	FEAT_OPR(CMDLINE,	cmdline,	false),
2257 	FEAT_OPR(CPU_TOPOLOGY,	cpu_topology,	true),
2258 	FEAT_OPR(NUMA_TOPOLOGY,	numa_topology,	true),
2259 	FEAT_OPN(BRANCH_STACK,	branch_stack,	false),
2260 	FEAT_OPR(PMU_MAPPINGS,	pmu_mappings,	false),
2261 	FEAT_OPN(GROUP_DESC,	group_desc,	false),
2262 	FEAT_OPN(AUXTRACE,	auxtrace,	false),
2263 	FEAT_OPN(STAT,		stat,		false),
2264 	FEAT_OPN(CACHE,		cache,		true),
2265 	FEAT_OPR(SAMPLE_TIME,	sample_time,	false),
2266 };
2267 
2268 struct header_print_data {
2269 	FILE *fp;
2270 	bool full; /* extended list of headers */
2271 };
2272 
2273 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2274 					   struct perf_header *ph,
2275 					   int feat, int fd, void *data)
2276 {
2277 	struct header_print_data *hd = data;
2278 	struct feat_fd ff;
2279 
2280 	if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2281 		pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2282 				"%d, continuing...\n", section->offset, feat);
2283 		return 0;
2284 	}
2285 	if (feat >= HEADER_LAST_FEATURE) {
2286 		pr_warning("unknown feature %d\n", feat);
2287 		return 0;
2288 	}
2289 	if (!feat_ops[feat].print)
2290 		return 0;
2291 
2292 	ff = (struct  feat_fd) {
2293 		.fd = fd,
2294 		.ph = ph,
2295 	};
2296 
2297 	if (!feat_ops[feat].full_only || hd->full)
2298 		feat_ops[feat].print(&ff, hd->fp);
2299 	else
2300 		fprintf(hd->fp, "# %s info available, use -I to display\n",
2301 			feat_ops[feat].name);
2302 
2303 	return 0;
2304 }
2305 
2306 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2307 {
2308 	struct header_print_data hd;
2309 	struct perf_header *header = &session->header;
2310 	int fd = perf_data__fd(session->data);
2311 	struct stat st;
2312 	int ret, bit;
2313 
2314 	hd.fp = fp;
2315 	hd.full = full;
2316 
2317 	ret = fstat(fd, &st);
2318 	if (ret == -1)
2319 		return -1;
2320 
2321 	fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
2322 
2323 	perf_header__process_sections(header, fd, &hd,
2324 				      perf_file_section__fprintf_info);
2325 
2326 	if (session->data->is_pipe)
2327 		return 0;
2328 
2329 	fprintf(fp, "# missing features: ");
2330 	for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2331 		if (bit)
2332 			fprintf(fp, "%s ", feat_ops[bit].name);
2333 	}
2334 
2335 	fprintf(fp, "\n");
2336 	return 0;
2337 }
2338 
2339 static int do_write_feat(struct feat_fd *ff, int type,
2340 			 struct perf_file_section **p,
2341 			 struct perf_evlist *evlist)
2342 {
2343 	int err;
2344 	int ret = 0;
2345 
2346 	if (perf_header__has_feat(ff->ph, type)) {
2347 		if (!feat_ops[type].write)
2348 			return -1;
2349 
2350 		if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
2351 			return -1;
2352 
2353 		(*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
2354 
2355 		err = feat_ops[type].write(ff, evlist);
2356 		if (err < 0) {
2357 			pr_debug("failed to write feature %s\n", feat_ops[type].name);
2358 
2359 			/* undo anything written */
2360 			lseek(ff->fd, (*p)->offset, SEEK_SET);
2361 
2362 			return -1;
2363 		}
2364 		(*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
2365 		(*p)++;
2366 	}
2367 	return ret;
2368 }
2369 
2370 static int perf_header__adds_write(struct perf_header *header,
2371 				   struct perf_evlist *evlist, int fd)
2372 {
2373 	int nr_sections;
2374 	struct feat_fd ff;
2375 	struct perf_file_section *feat_sec, *p;
2376 	int sec_size;
2377 	u64 sec_start;
2378 	int feat;
2379 	int err;
2380 
2381 	ff = (struct feat_fd){
2382 		.fd  = fd,
2383 		.ph = header,
2384 	};
2385 
2386 	nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2387 	if (!nr_sections)
2388 		return 0;
2389 
2390 	feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
2391 	if (feat_sec == NULL)
2392 		return -ENOMEM;
2393 
2394 	sec_size = sizeof(*feat_sec) * nr_sections;
2395 
2396 	sec_start = header->feat_offset;
2397 	lseek(fd, sec_start + sec_size, SEEK_SET);
2398 
2399 	for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2400 		if (do_write_feat(&ff, feat, &p, evlist))
2401 			perf_header__clear_feat(header, feat);
2402 	}
2403 
2404 	lseek(fd, sec_start, SEEK_SET);
2405 	/*
2406 	 * may write more than needed due to dropped feature, but
2407 	 * this is okay, reader will skip the mising entries
2408 	 */
2409 	err = do_write(&ff, feat_sec, sec_size);
2410 	if (err < 0)
2411 		pr_debug("failed to write feature section\n");
2412 	free(feat_sec);
2413 	return err;
2414 }
2415 
2416 int perf_header__write_pipe(int fd)
2417 {
2418 	struct perf_pipe_file_header f_header;
2419 	struct feat_fd ff;
2420 	int err;
2421 
2422 	ff = (struct feat_fd){ .fd = fd };
2423 
2424 	f_header = (struct perf_pipe_file_header){
2425 		.magic	   = PERF_MAGIC,
2426 		.size	   = sizeof(f_header),
2427 	};
2428 
2429 	err = do_write(&ff, &f_header, sizeof(f_header));
2430 	if (err < 0) {
2431 		pr_debug("failed to write perf pipe header\n");
2432 		return err;
2433 	}
2434 
2435 	return 0;
2436 }
2437 
2438 int perf_session__write_header(struct perf_session *session,
2439 			       struct perf_evlist *evlist,
2440 			       int fd, bool at_exit)
2441 {
2442 	struct perf_file_header f_header;
2443 	struct perf_file_attr   f_attr;
2444 	struct perf_header *header = &session->header;
2445 	struct perf_evsel *evsel;
2446 	struct feat_fd ff;
2447 	u64 attr_offset;
2448 	int err;
2449 
2450 	ff = (struct feat_fd){ .fd = fd};
2451 	lseek(fd, sizeof(f_header), SEEK_SET);
2452 
2453 	evlist__for_each_entry(session->evlist, evsel) {
2454 		evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2455 		err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
2456 		if (err < 0) {
2457 			pr_debug("failed to write perf header\n");
2458 			return err;
2459 		}
2460 	}
2461 
2462 	attr_offset = lseek(ff.fd, 0, SEEK_CUR);
2463 
2464 	evlist__for_each_entry(evlist, evsel) {
2465 		f_attr = (struct perf_file_attr){
2466 			.attr = evsel->attr,
2467 			.ids  = {
2468 				.offset = evsel->id_offset,
2469 				.size   = evsel->ids * sizeof(u64),
2470 			}
2471 		};
2472 		err = do_write(&ff, &f_attr, sizeof(f_attr));
2473 		if (err < 0) {
2474 			pr_debug("failed to write perf header attribute\n");
2475 			return err;
2476 		}
2477 	}
2478 
2479 	if (!header->data_offset)
2480 		header->data_offset = lseek(fd, 0, SEEK_CUR);
2481 	header->feat_offset = header->data_offset + header->data_size;
2482 
2483 	if (at_exit) {
2484 		err = perf_header__adds_write(header, evlist, fd);
2485 		if (err < 0)
2486 			return err;
2487 	}
2488 
2489 	f_header = (struct perf_file_header){
2490 		.magic	   = PERF_MAGIC,
2491 		.size	   = sizeof(f_header),
2492 		.attr_size = sizeof(f_attr),
2493 		.attrs = {
2494 			.offset = attr_offset,
2495 			.size   = evlist->nr_entries * sizeof(f_attr),
2496 		},
2497 		.data = {
2498 			.offset = header->data_offset,
2499 			.size	= header->data_size,
2500 		},
2501 		/* event_types is ignored, store zeros */
2502 	};
2503 
2504 	memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2505 
2506 	lseek(fd, 0, SEEK_SET);
2507 	err = do_write(&ff, &f_header, sizeof(f_header));
2508 	if (err < 0) {
2509 		pr_debug("failed to write perf header\n");
2510 		return err;
2511 	}
2512 	lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2513 
2514 	return 0;
2515 }
2516 
2517 static int perf_header__getbuffer64(struct perf_header *header,
2518 				    int fd, void *buf, size_t size)
2519 {
2520 	if (readn(fd, buf, size) <= 0)
2521 		return -1;
2522 
2523 	if (header->needs_swap)
2524 		mem_bswap_64(buf, size);
2525 
2526 	return 0;
2527 }
2528 
2529 int perf_header__process_sections(struct perf_header *header, int fd,
2530 				  void *data,
2531 				  int (*process)(struct perf_file_section *section,
2532 						 struct perf_header *ph,
2533 						 int feat, int fd, void *data))
2534 {
2535 	struct perf_file_section *feat_sec, *sec;
2536 	int nr_sections;
2537 	int sec_size;
2538 	int feat;
2539 	int err;
2540 
2541 	nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2542 	if (!nr_sections)
2543 		return 0;
2544 
2545 	feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2546 	if (!feat_sec)
2547 		return -1;
2548 
2549 	sec_size = sizeof(*feat_sec) * nr_sections;
2550 
2551 	lseek(fd, header->feat_offset, SEEK_SET);
2552 
2553 	err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2554 	if (err < 0)
2555 		goto out_free;
2556 
2557 	for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2558 		err = process(sec++, header, feat, fd, data);
2559 		if (err < 0)
2560 			goto out_free;
2561 	}
2562 	err = 0;
2563 out_free:
2564 	free(feat_sec);
2565 	return err;
2566 }
2567 
2568 static const int attr_file_abi_sizes[] = {
2569 	[0] = PERF_ATTR_SIZE_VER0,
2570 	[1] = PERF_ATTR_SIZE_VER1,
2571 	[2] = PERF_ATTR_SIZE_VER2,
2572 	[3] = PERF_ATTR_SIZE_VER3,
2573 	[4] = PERF_ATTR_SIZE_VER4,
2574 	0,
2575 };
2576 
2577 /*
2578  * In the legacy file format, the magic number is not used to encode endianness.
2579  * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2580  * on ABI revisions, we need to try all combinations for all endianness to
2581  * detect the endianness.
2582  */
2583 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2584 {
2585 	uint64_t ref_size, attr_size;
2586 	int i;
2587 
2588 	for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2589 		ref_size = attr_file_abi_sizes[i]
2590 			 + sizeof(struct perf_file_section);
2591 		if (hdr_sz != ref_size) {
2592 			attr_size = bswap_64(hdr_sz);
2593 			if (attr_size != ref_size)
2594 				continue;
2595 
2596 			ph->needs_swap = true;
2597 		}
2598 		pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2599 			 i,
2600 			 ph->needs_swap);
2601 		return 0;
2602 	}
2603 	/* could not determine endianness */
2604 	return -1;
2605 }
2606 
2607 #define PERF_PIPE_HDR_VER0	16
2608 
2609 static const size_t attr_pipe_abi_sizes[] = {
2610 	[0] = PERF_PIPE_HDR_VER0,
2611 	0,
2612 };
2613 
2614 /*
2615  * In the legacy pipe format, there is an implicit assumption that endiannesss
2616  * between host recording the samples, and host parsing the samples is the
2617  * same. This is not always the case given that the pipe output may always be
2618  * redirected into a file and analyzed on a different machine with possibly a
2619  * different endianness and perf_event ABI revsions in the perf tool itself.
2620  */
2621 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2622 {
2623 	u64 attr_size;
2624 	int i;
2625 
2626 	for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2627 		if (hdr_sz != attr_pipe_abi_sizes[i]) {
2628 			attr_size = bswap_64(hdr_sz);
2629 			if (attr_size != hdr_sz)
2630 				continue;
2631 
2632 			ph->needs_swap = true;
2633 		}
2634 		pr_debug("Pipe ABI%d perf.data file detected\n", i);
2635 		return 0;
2636 	}
2637 	return -1;
2638 }
2639 
2640 bool is_perf_magic(u64 magic)
2641 {
2642 	if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2643 		|| magic == __perf_magic2
2644 		|| magic == __perf_magic2_sw)
2645 		return true;
2646 
2647 	return false;
2648 }
2649 
2650 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2651 			      bool is_pipe, struct perf_header *ph)
2652 {
2653 	int ret;
2654 
2655 	/* check for legacy format */
2656 	ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2657 	if (ret == 0) {
2658 		ph->version = PERF_HEADER_VERSION_1;
2659 		pr_debug("legacy perf.data format\n");
2660 		if (is_pipe)
2661 			return try_all_pipe_abis(hdr_sz, ph);
2662 
2663 		return try_all_file_abis(hdr_sz, ph);
2664 	}
2665 	/*
2666 	 * the new magic number serves two purposes:
2667 	 * - unique number to identify actual perf.data files
2668 	 * - encode endianness of file
2669 	 */
2670 	ph->version = PERF_HEADER_VERSION_2;
2671 
2672 	/* check magic number with one endianness */
2673 	if (magic == __perf_magic2)
2674 		return 0;
2675 
2676 	/* check magic number with opposite endianness */
2677 	if (magic != __perf_magic2_sw)
2678 		return -1;
2679 
2680 	ph->needs_swap = true;
2681 
2682 	return 0;
2683 }
2684 
2685 int perf_file_header__read(struct perf_file_header *header,
2686 			   struct perf_header *ph, int fd)
2687 {
2688 	ssize_t ret;
2689 
2690 	lseek(fd, 0, SEEK_SET);
2691 
2692 	ret = readn(fd, header, sizeof(*header));
2693 	if (ret <= 0)
2694 		return -1;
2695 
2696 	if (check_magic_endian(header->magic,
2697 			       header->attr_size, false, ph) < 0) {
2698 		pr_debug("magic/endian check failed\n");
2699 		return -1;
2700 	}
2701 
2702 	if (ph->needs_swap) {
2703 		mem_bswap_64(header, offsetof(struct perf_file_header,
2704 			     adds_features));
2705 	}
2706 
2707 	if (header->size != sizeof(*header)) {
2708 		/* Support the previous format */
2709 		if (header->size == offsetof(typeof(*header), adds_features))
2710 			bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2711 		else
2712 			return -1;
2713 	} else if (ph->needs_swap) {
2714 		/*
2715 		 * feature bitmap is declared as an array of unsigned longs --
2716 		 * not good since its size can differ between the host that
2717 		 * generated the data file and the host analyzing the file.
2718 		 *
2719 		 * We need to handle endianness, but we don't know the size of
2720 		 * the unsigned long where the file was generated. Take a best
2721 		 * guess at determining it: try 64-bit swap first (ie., file
2722 		 * created on a 64-bit host), and check if the hostname feature
2723 		 * bit is set (this feature bit is forced on as of fbe96f2).
2724 		 * If the bit is not, undo the 64-bit swap and try a 32-bit
2725 		 * swap. If the hostname bit is still not set (e.g., older data
2726 		 * file), punt and fallback to the original behavior --
2727 		 * clearing all feature bits and setting buildid.
2728 		 */
2729 		mem_bswap_64(&header->adds_features,
2730 			    BITS_TO_U64(HEADER_FEAT_BITS));
2731 
2732 		if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2733 			/* unswap as u64 */
2734 			mem_bswap_64(&header->adds_features,
2735 				    BITS_TO_U64(HEADER_FEAT_BITS));
2736 
2737 			/* unswap as u32 */
2738 			mem_bswap_32(&header->adds_features,
2739 				    BITS_TO_U32(HEADER_FEAT_BITS));
2740 		}
2741 
2742 		if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2743 			bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2744 			set_bit(HEADER_BUILD_ID, header->adds_features);
2745 		}
2746 	}
2747 
2748 	memcpy(&ph->adds_features, &header->adds_features,
2749 	       sizeof(ph->adds_features));
2750 
2751 	ph->data_offset  = header->data.offset;
2752 	ph->data_size	 = header->data.size;
2753 	ph->feat_offset  = header->data.offset + header->data.size;
2754 	return 0;
2755 }
2756 
2757 static int perf_file_section__process(struct perf_file_section *section,
2758 				      struct perf_header *ph,
2759 				      int feat, int fd, void *data)
2760 {
2761 	struct feat_fd fdd = {
2762 		.fd	= fd,
2763 		.ph	= ph,
2764 		.size	= section->size,
2765 		.offset	= section->offset,
2766 	};
2767 
2768 	if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2769 		pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2770 			  "%d, continuing...\n", section->offset, feat);
2771 		return 0;
2772 	}
2773 
2774 	if (feat >= HEADER_LAST_FEATURE) {
2775 		pr_debug("unknown feature %d, continuing...\n", feat);
2776 		return 0;
2777 	}
2778 
2779 	if (!feat_ops[feat].process)
2780 		return 0;
2781 
2782 	return feat_ops[feat].process(&fdd, data);
2783 }
2784 
2785 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2786 				       struct perf_header *ph, int fd,
2787 				       bool repipe)
2788 {
2789 	struct feat_fd ff = {
2790 		.fd = STDOUT_FILENO,
2791 		.ph = ph,
2792 	};
2793 	ssize_t ret;
2794 
2795 	ret = readn(fd, header, sizeof(*header));
2796 	if (ret <= 0)
2797 		return -1;
2798 
2799 	if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2800 		pr_debug("endian/magic failed\n");
2801 		return -1;
2802 	}
2803 
2804 	if (ph->needs_swap)
2805 		header->size = bswap_64(header->size);
2806 
2807 	if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
2808 		return -1;
2809 
2810 	return 0;
2811 }
2812 
2813 static int perf_header__read_pipe(struct perf_session *session)
2814 {
2815 	struct perf_header *header = &session->header;
2816 	struct perf_pipe_file_header f_header;
2817 
2818 	if (perf_file_header__read_pipe(&f_header, header,
2819 					perf_data__fd(session->data),
2820 					session->repipe) < 0) {
2821 		pr_debug("incompatible file format\n");
2822 		return -EINVAL;
2823 	}
2824 
2825 	return 0;
2826 }
2827 
2828 static int read_attr(int fd, struct perf_header *ph,
2829 		     struct perf_file_attr *f_attr)
2830 {
2831 	struct perf_event_attr *attr = &f_attr->attr;
2832 	size_t sz, left;
2833 	size_t our_sz = sizeof(f_attr->attr);
2834 	ssize_t ret;
2835 
2836 	memset(f_attr, 0, sizeof(*f_attr));
2837 
2838 	/* read minimal guaranteed structure */
2839 	ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2840 	if (ret <= 0) {
2841 		pr_debug("cannot read %d bytes of header attr\n",
2842 			 PERF_ATTR_SIZE_VER0);
2843 		return -1;
2844 	}
2845 
2846 	/* on file perf_event_attr size */
2847 	sz = attr->size;
2848 
2849 	if (ph->needs_swap)
2850 		sz = bswap_32(sz);
2851 
2852 	if (sz == 0) {
2853 		/* assume ABI0 */
2854 		sz =  PERF_ATTR_SIZE_VER0;
2855 	} else if (sz > our_sz) {
2856 		pr_debug("file uses a more recent and unsupported ABI"
2857 			 " (%zu bytes extra)\n", sz - our_sz);
2858 		return -1;
2859 	}
2860 	/* what we have not yet read and that we know about */
2861 	left = sz - PERF_ATTR_SIZE_VER0;
2862 	if (left) {
2863 		void *ptr = attr;
2864 		ptr += PERF_ATTR_SIZE_VER0;
2865 
2866 		ret = readn(fd, ptr, left);
2867 	}
2868 	/* read perf_file_section, ids are read in caller */
2869 	ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2870 
2871 	return ret <= 0 ? -1 : 0;
2872 }
2873 
2874 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2875 						struct pevent *pevent)
2876 {
2877 	struct event_format *event;
2878 	char bf[128];
2879 
2880 	/* already prepared */
2881 	if (evsel->tp_format)
2882 		return 0;
2883 
2884 	if (pevent == NULL) {
2885 		pr_debug("broken or missing trace data\n");
2886 		return -1;
2887 	}
2888 
2889 	event = pevent_find_event(pevent, evsel->attr.config);
2890 	if (event == NULL) {
2891 		pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
2892 		return -1;
2893 	}
2894 
2895 	if (!evsel->name) {
2896 		snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2897 		evsel->name = strdup(bf);
2898 		if (evsel->name == NULL)
2899 			return -1;
2900 	}
2901 
2902 	evsel->tp_format = event;
2903 	return 0;
2904 }
2905 
2906 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2907 						  struct pevent *pevent)
2908 {
2909 	struct perf_evsel *pos;
2910 
2911 	evlist__for_each_entry(evlist, pos) {
2912 		if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2913 		    perf_evsel__prepare_tracepoint_event(pos, pevent))
2914 			return -1;
2915 	}
2916 
2917 	return 0;
2918 }
2919 
2920 int perf_session__read_header(struct perf_session *session)
2921 {
2922 	struct perf_data *data = session->data;
2923 	struct perf_header *header = &session->header;
2924 	struct perf_file_header	f_header;
2925 	struct perf_file_attr	f_attr;
2926 	u64			f_id;
2927 	int nr_attrs, nr_ids, i, j;
2928 	int fd = perf_data__fd(data);
2929 
2930 	session->evlist = perf_evlist__new();
2931 	if (session->evlist == NULL)
2932 		return -ENOMEM;
2933 
2934 	session->evlist->env = &header->env;
2935 	session->machines.host.env = &header->env;
2936 	if (perf_data__is_pipe(data))
2937 		return perf_header__read_pipe(session);
2938 
2939 	if (perf_file_header__read(&f_header, header, fd) < 0)
2940 		return -EINVAL;
2941 
2942 	/*
2943 	 * Sanity check that perf.data was written cleanly; data size is
2944 	 * initialized to 0 and updated only if the on_exit function is run.
2945 	 * If data size is still 0 then the file contains only partial
2946 	 * information.  Just warn user and process it as much as it can.
2947 	 */
2948 	if (f_header.data.size == 0) {
2949 		pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2950 			   "Was the 'perf record' command properly terminated?\n",
2951 			   data->file.path);
2952 	}
2953 
2954 	nr_attrs = f_header.attrs.size / f_header.attr_size;
2955 	lseek(fd, f_header.attrs.offset, SEEK_SET);
2956 
2957 	for (i = 0; i < nr_attrs; i++) {
2958 		struct perf_evsel *evsel;
2959 		off_t tmp;
2960 
2961 		if (read_attr(fd, header, &f_attr) < 0)
2962 			goto out_errno;
2963 
2964 		if (header->needs_swap) {
2965 			f_attr.ids.size   = bswap_64(f_attr.ids.size);
2966 			f_attr.ids.offset = bswap_64(f_attr.ids.offset);
2967 			perf_event__attr_swap(&f_attr.attr);
2968 		}
2969 
2970 		tmp = lseek(fd, 0, SEEK_CUR);
2971 		evsel = perf_evsel__new(&f_attr.attr);
2972 
2973 		if (evsel == NULL)
2974 			goto out_delete_evlist;
2975 
2976 		evsel->needs_swap = header->needs_swap;
2977 		/*
2978 		 * Do it before so that if perf_evsel__alloc_id fails, this
2979 		 * entry gets purged too at perf_evlist__delete().
2980 		 */
2981 		perf_evlist__add(session->evlist, evsel);
2982 
2983 		nr_ids = f_attr.ids.size / sizeof(u64);
2984 		/*
2985 		 * We don't have the cpu and thread maps on the header, so
2986 		 * for allocating the perf_sample_id table we fake 1 cpu and
2987 		 * hattr->ids threads.
2988 		 */
2989 		if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2990 			goto out_delete_evlist;
2991 
2992 		lseek(fd, f_attr.ids.offset, SEEK_SET);
2993 
2994 		for (j = 0; j < nr_ids; j++) {
2995 			if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2996 				goto out_errno;
2997 
2998 			perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2999 		}
3000 
3001 		lseek(fd, tmp, SEEK_SET);
3002 	}
3003 
3004 	symbol_conf.nr_events = nr_attrs;
3005 
3006 	perf_header__process_sections(header, fd, &session->tevent,
3007 				      perf_file_section__process);
3008 
3009 	if (perf_evlist__prepare_tracepoint_events(session->evlist,
3010 						   session->tevent.pevent))
3011 		goto out_delete_evlist;
3012 
3013 	return 0;
3014 out_errno:
3015 	return -errno;
3016 
3017 out_delete_evlist:
3018 	perf_evlist__delete(session->evlist);
3019 	session->evlist = NULL;
3020 	return -ENOMEM;
3021 }
3022 
3023 int perf_event__synthesize_attr(struct perf_tool *tool,
3024 				struct perf_event_attr *attr, u32 ids, u64 *id,
3025 				perf_event__handler_t process)
3026 {
3027 	union perf_event *ev;
3028 	size_t size;
3029 	int err;
3030 
3031 	size = sizeof(struct perf_event_attr);
3032 	size = PERF_ALIGN(size, sizeof(u64));
3033 	size += sizeof(struct perf_event_header);
3034 	size += ids * sizeof(u64);
3035 
3036 	ev = malloc(size);
3037 
3038 	if (ev == NULL)
3039 		return -ENOMEM;
3040 
3041 	ev->attr.attr = *attr;
3042 	memcpy(ev->attr.id, id, ids * sizeof(u64));
3043 
3044 	ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
3045 	ev->attr.header.size = (u16)size;
3046 
3047 	if (ev->attr.header.size == size)
3048 		err = process(tool, ev, NULL, NULL);
3049 	else
3050 		err = -E2BIG;
3051 
3052 	free(ev);
3053 
3054 	return err;
3055 }
3056 
3057 int perf_event__synthesize_features(struct perf_tool *tool,
3058 				    struct perf_session *session,
3059 				    struct perf_evlist *evlist,
3060 				    perf_event__handler_t process)
3061 {
3062 	struct perf_header *header = &session->header;
3063 	struct feat_fd ff;
3064 	struct feature_event *fe;
3065 	size_t sz, sz_hdr;
3066 	int feat, ret;
3067 
3068 	sz_hdr = sizeof(fe->header);
3069 	sz = sizeof(union perf_event);
3070 	/* get a nice alignment */
3071 	sz = PERF_ALIGN(sz, page_size);
3072 
3073 	memset(&ff, 0, sizeof(ff));
3074 
3075 	ff.buf = malloc(sz);
3076 	if (!ff.buf)
3077 		return -ENOMEM;
3078 
3079 	ff.size = sz - sz_hdr;
3080 
3081 	for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3082 		if (!feat_ops[feat].synthesize) {
3083 			pr_debug("No record header feature for header :%d\n", feat);
3084 			continue;
3085 		}
3086 
3087 		ff.offset = sizeof(*fe);
3088 
3089 		ret = feat_ops[feat].write(&ff, evlist);
3090 		if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
3091 			pr_debug("Error writing feature\n");
3092 			continue;
3093 		}
3094 		/* ff.buf may have changed due to realloc in do_write() */
3095 		fe = ff.buf;
3096 		memset(fe, 0, sizeof(*fe));
3097 
3098 		fe->feat_id = feat;
3099 		fe->header.type = PERF_RECORD_HEADER_FEATURE;
3100 		fe->header.size = ff.offset;
3101 
3102 		ret = process(tool, ff.buf, NULL, NULL);
3103 		if (ret) {
3104 			free(ff.buf);
3105 			return ret;
3106 		}
3107 	}
3108 	free(ff.buf);
3109 	return 0;
3110 }
3111 
3112 int perf_event__process_feature(struct perf_tool *tool,
3113 				union perf_event *event,
3114 				struct perf_session *session __maybe_unused)
3115 {
3116 	struct feat_fd ff = { .fd = 0 };
3117 	struct feature_event *fe = (struct feature_event *)event;
3118 	int type = fe->header.type;
3119 	u64 feat = fe->feat_id;
3120 
3121 	if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3122 		pr_warning("invalid record type %d in pipe-mode\n", type);
3123 		return 0;
3124 	}
3125 	if (feat == HEADER_RESERVED || feat > HEADER_LAST_FEATURE) {
3126 		pr_warning("invalid record type %d in pipe-mode\n", type);
3127 		return -1;
3128 	}
3129 
3130 	if (!feat_ops[feat].process)
3131 		return 0;
3132 
3133 	ff.buf  = (void *)fe->data;
3134 	ff.size = event->header.size - sizeof(event->header);
3135 	ff.ph = &session->header;
3136 
3137 	if (feat_ops[feat].process(&ff, NULL))
3138 		return -1;
3139 
3140 	if (!feat_ops[feat].print || !tool->show_feat_hdr)
3141 		return 0;
3142 
3143 	if (!feat_ops[feat].full_only ||
3144 	    tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3145 		feat_ops[feat].print(&ff, stdout);
3146 	} else {
3147 		fprintf(stdout, "# %s info available, use -I to display\n",
3148 			feat_ops[feat].name);
3149 	}
3150 
3151 	return 0;
3152 }
3153 
3154 static struct event_update_event *
3155 event_update_event__new(size_t size, u64 type, u64 id)
3156 {
3157 	struct event_update_event *ev;
3158 
3159 	size += sizeof(*ev);
3160 	size  = PERF_ALIGN(size, sizeof(u64));
3161 
3162 	ev = zalloc(size);
3163 	if (ev) {
3164 		ev->header.type = PERF_RECORD_EVENT_UPDATE;
3165 		ev->header.size = (u16)size;
3166 		ev->type = type;
3167 		ev->id = id;
3168 	}
3169 	return ev;
3170 }
3171 
3172 int
3173 perf_event__synthesize_event_update_unit(struct perf_tool *tool,
3174 					 struct perf_evsel *evsel,
3175 					 perf_event__handler_t process)
3176 {
3177 	struct event_update_event *ev;
3178 	size_t size = strlen(evsel->unit);
3179 	int err;
3180 
3181 	ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
3182 	if (ev == NULL)
3183 		return -ENOMEM;
3184 
3185 	strncpy(ev->data, evsel->unit, size);
3186 	err = process(tool, (union perf_event *)ev, NULL, NULL);
3187 	free(ev);
3188 	return err;
3189 }
3190 
3191 int
3192 perf_event__synthesize_event_update_scale(struct perf_tool *tool,
3193 					  struct perf_evsel *evsel,
3194 					  perf_event__handler_t process)
3195 {
3196 	struct event_update_event *ev;
3197 	struct event_update_event_scale *ev_data;
3198 	int err;
3199 
3200 	ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
3201 	if (ev == NULL)
3202 		return -ENOMEM;
3203 
3204 	ev_data = (struct event_update_event_scale *) ev->data;
3205 	ev_data->scale = evsel->scale;
3206 	err = process(tool, (union perf_event*) ev, NULL, NULL);
3207 	free(ev);
3208 	return err;
3209 }
3210 
3211 int
3212 perf_event__synthesize_event_update_name(struct perf_tool *tool,
3213 					 struct perf_evsel *evsel,
3214 					 perf_event__handler_t process)
3215 {
3216 	struct event_update_event *ev;
3217 	size_t len = strlen(evsel->name);
3218 	int err;
3219 
3220 	ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
3221 	if (ev == NULL)
3222 		return -ENOMEM;
3223 
3224 	strncpy(ev->data, evsel->name, len);
3225 	err = process(tool, (union perf_event*) ev, NULL, NULL);
3226 	free(ev);
3227 	return err;
3228 }
3229 
3230 int
3231 perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
3232 					struct perf_evsel *evsel,
3233 					perf_event__handler_t process)
3234 {
3235 	size_t size = sizeof(struct event_update_event);
3236 	struct event_update_event *ev;
3237 	int max, err;
3238 	u16 type;
3239 
3240 	if (!evsel->own_cpus)
3241 		return 0;
3242 
3243 	ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3244 	if (!ev)
3245 		return -ENOMEM;
3246 
3247 	ev->header.type = PERF_RECORD_EVENT_UPDATE;
3248 	ev->header.size = (u16)size;
3249 	ev->type = PERF_EVENT_UPDATE__CPUS;
3250 	ev->id   = evsel->id[0];
3251 
3252 	cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3253 				 evsel->own_cpus,
3254 				 type, max);
3255 
3256 	err = process(tool, (union perf_event*) ev, NULL, NULL);
3257 	free(ev);
3258 	return err;
3259 }
3260 
3261 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3262 {
3263 	struct event_update_event *ev = &event->event_update;
3264 	struct event_update_event_scale *ev_scale;
3265 	struct event_update_event_cpus *ev_cpus;
3266 	struct cpu_map *map;
3267 	size_t ret;
3268 
3269 	ret = fprintf(fp, "\n... id:    %" PRIu64 "\n", ev->id);
3270 
3271 	switch (ev->type) {
3272 	case PERF_EVENT_UPDATE__SCALE:
3273 		ev_scale = (struct event_update_event_scale *) ev->data;
3274 		ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3275 		break;
3276 	case PERF_EVENT_UPDATE__UNIT:
3277 		ret += fprintf(fp, "... unit:  %s\n", ev->data);
3278 		break;
3279 	case PERF_EVENT_UPDATE__NAME:
3280 		ret += fprintf(fp, "... name:  %s\n", ev->data);
3281 		break;
3282 	case PERF_EVENT_UPDATE__CPUS:
3283 		ev_cpus = (struct event_update_event_cpus *) ev->data;
3284 		ret += fprintf(fp, "... ");
3285 
3286 		map = cpu_map__new_data(&ev_cpus->cpus);
3287 		if (map)
3288 			ret += cpu_map__fprintf(map, fp);
3289 		else
3290 			ret += fprintf(fp, "failed to get cpus\n");
3291 		break;
3292 	default:
3293 		ret += fprintf(fp, "... unknown type\n");
3294 		break;
3295 	}
3296 
3297 	return ret;
3298 }
3299 
3300 int perf_event__synthesize_attrs(struct perf_tool *tool,
3301 				   struct perf_session *session,
3302 				   perf_event__handler_t process)
3303 {
3304 	struct perf_evsel *evsel;
3305 	int err = 0;
3306 
3307 	evlist__for_each_entry(session->evlist, evsel) {
3308 		err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3309 						  evsel->id, process);
3310 		if (err) {
3311 			pr_debug("failed to create perf header attribute\n");
3312 			return err;
3313 		}
3314 	}
3315 
3316 	return err;
3317 }
3318 
3319 static bool has_unit(struct perf_evsel *counter)
3320 {
3321 	return counter->unit && *counter->unit;
3322 }
3323 
3324 static bool has_scale(struct perf_evsel *counter)
3325 {
3326 	return counter->scale != 1;
3327 }
3328 
3329 int perf_event__synthesize_extra_attr(struct perf_tool *tool,
3330 				      struct perf_evlist *evsel_list,
3331 				      perf_event__handler_t process,
3332 				      bool is_pipe)
3333 {
3334 	struct perf_evsel *counter;
3335 	int err;
3336 
3337 	/*
3338 	 * Synthesize other events stuff not carried within
3339 	 * attr event - unit, scale, name
3340 	 */
3341 	evlist__for_each_entry(evsel_list, counter) {
3342 		if (!counter->supported)
3343 			continue;
3344 
3345 		/*
3346 		 * Synthesize unit and scale only if it's defined.
3347 		 */
3348 		if (has_unit(counter)) {
3349 			err = perf_event__synthesize_event_update_unit(tool, counter, process);
3350 			if (err < 0) {
3351 				pr_err("Couldn't synthesize evsel unit.\n");
3352 				return err;
3353 			}
3354 		}
3355 
3356 		if (has_scale(counter)) {
3357 			err = perf_event__synthesize_event_update_scale(tool, counter, process);
3358 			if (err < 0) {
3359 				pr_err("Couldn't synthesize evsel counter.\n");
3360 				return err;
3361 			}
3362 		}
3363 
3364 		if (counter->own_cpus) {
3365 			err = perf_event__synthesize_event_update_cpus(tool, counter, process);
3366 			if (err < 0) {
3367 				pr_err("Couldn't synthesize evsel cpus.\n");
3368 				return err;
3369 			}
3370 		}
3371 
3372 		/*
3373 		 * Name is needed only for pipe output,
3374 		 * perf.data carries event names.
3375 		 */
3376 		if (is_pipe) {
3377 			err = perf_event__synthesize_event_update_name(tool, counter, process);
3378 			if (err < 0) {
3379 				pr_err("Couldn't synthesize evsel name.\n");
3380 				return err;
3381 			}
3382 		}
3383 	}
3384 	return 0;
3385 }
3386 
3387 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3388 			     union perf_event *event,
3389 			     struct perf_evlist **pevlist)
3390 {
3391 	u32 i, ids, n_ids;
3392 	struct perf_evsel *evsel;
3393 	struct perf_evlist *evlist = *pevlist;
3394 
3395 	if (evlist == NULL) {
3396 		*pevlist = evlist = perf_evlist__new();
3397 		if (evlist == NULL)
3398 			return -ENOMEM;
3399 	}
3400 
3401 	evsel = perf_evsel__new(&event->attr.attr);
3402 	if (evsel == NULL)
3403 		return -ENOMEM;
3404 
3405 	perf_evlist__add(evlist, evsel);
3406 
3407 	ids = event->header.size;
3408 	ids -= (void *)&event->attr.id - (void *)event;
3409 	n_ids = ids / sizeof(u64);
3410 	/*
3411 	 * We don't have the cpu and thread maps on the header, so
3412 	 * for allocating the perf_sample_id table we fake 1 cpu and
3413 	 * hattr->ids threads.
3414 	 */
3415 	if (perf_evsel__alloc_id(evsel, 1, n_ids))
3416 		return -ENOMEM;
3417 
3418 	for (i = 0; i < n_ids; i++) {
3419 		perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
3420 	}
3421 
3422 	symbol_conf.nr_events = evlist->nr_entries;
3423 
3424 	return 0;
3425 }
3426 
3427 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3428 				     union perf_event *event,
3429 				     struct perf_evlist **pevlist)
3430 {
3431 	struct event_update_event *ev = &event->event_update;
3432 	struct event_update_event_scale *ev_scale;
3433 	struct event_update_event_cpus *ev_cpus;
3434 	struct perf_evlist *evlist;
3435 	struct perf_evsel *evsel;
3436 	struct cpu_map *map;
3437 
3438 	if (!pevlist || *pevlist == NULL)
3439 		return -EINVAL;
3440 
3441 	evlist = *pevlist;
3442 
3443 	evsel = perf_evlist__id2evsel(evlist, ev->id);
3444 	if (evsel == NULL)
3445 		return -EINVAL;
3446 
3447 	switch (ev->type) {
3448 	case PERF_EVENT_UPDATE__UNIT:
3449 		evsel->unit = strdup(ev->data);
3450 		break;
3451 	case PERF_EVENT_UPDATE__NAME:
3452 		evsel->name = strdup(ev->data);
3453 		break;
3454 	case PERF_EVENT_UPDATE__SCALE:
3455 		ev_scale = (struct event_update_event_scale *) ev->data;
3456 		evsel->scale = ev_scale->scale;
3457 		break;
3458 	case PERF_EVENT_UPDATE__CPUS:
3459 		ev_cpus = (struct event_update_event_cpus *) ev->data;
3460 
3461 		map = cpu_map__new_data(&ev_cpus->cpus);
3462 		if (map)
3463 			evsel->own_cpus = map;
3464 		else
3465 			pr_err("failed to get event_update cpus\n");
3466 	default:
3467 		break;
3468 	}
3469 
3470 	return 0;
3471 }
3472 
3473 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
3474 					struct perf_evlist *evlist,
3475 					perf_event__handler_t process)
3476 {
3477 	union perf_event ev;
3478 	struct tracing_data *tdata;
3479 	ssize_t size = 0, aligned_size = 0, padding;
3480 	struct feat_fd ff;
3481 	int err __maybe_unused = 0;
3482 
3483 	/*
3484 	 * We are going to store the size of the data followed
3485 	 * by the data contents. Since the fd descriptor is a pipe,
3486 	 * we cannot seek back to store the size of the data once
3487 	 * we know it. Instead we:
3488 	 *
3489 	 * - write the tracing data to the temp file
3490 	 * - get/write the data size to pipe
3491 	 * - write the tracing data from the temp file
3492 	 *   to the pipe
3493 	 */
3494 	tdata = tracing_data_get(&evlist->entries, fd, true);
3495 	if (!tdata)
3496 		return -1;
3497 
3498 	memset(&ev, 0, sizeof(ev));
3499 
3500 	ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
3501 	size = tdata->size;
3502 	aligned_size = PERF_ALIGN(size, sizeof(u64));
3503 	padding = aligned_size - size;
3504 	ev.tracing_data.header.size = sizeof(ev.tracing_data);
3505 	ev.tracing_data.size = aligned_size;
3506 
3507 	process(tool, &ev, NULL, NULL);
3508 
3509 	/*
3510 	 * The put function will copy all the tracing data
3511 	 * stored in temp file to the pipe.
3512 	 */
3513 	tracing_data_put(tdata);
3514 
3515 	ff = (struct feat_fd){ .fd = fd };
3516 	if (write_padded(&ff, NULL, 0, padding))
3517 		return -1;
3518 
3519 	return aligned_size;
3520 }
3521 
3522 int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3523 				     union perf_event *event,
3524 				     struct perf_session *session)
3525 {
3526 	ssize_t size_read, padding, size = event->tracing_data.size;
3527 	int fd = perf_data__fd(session->data);
3528 	off_t offset = lseek(fd, 0, SEEK_CUR);
3529 	char buf[BUFSIZ];
3530 
3531 	/* setup for reading amidst mmap */
3532 	lseek(fd, offset + sizeof(struct tracing_data_event),
3533 	      SEEK_SET);
3534 
3535 	size_read = trace_report(fd, &session->tevent,
3536 				 session->repipe);
3537 	padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
3538 
3539 	if (readn(fd, buf, padding) < 0) {
3540 		pr_err("%s: reading input file", __func__);
3541 		return -1;
3542 	}
3543 	if (session->repipe) {
3544 		int retw = write(STDOUT_FILENO, buf, padding);
3545 		if (retw <= 0 || retw != padding) {
3546 			pr_err("%s: repiping tracing data padding", __func__);
3547 			return -1;
3548 		}
3549 	}
3550 
3551 	if (size_read + padding != size) {
3552 		pr_err("%s: tracing data size mismatch", __func__);
3553 		return -1;
3554 	}
3555 
3556 	perf_evlist__prepare_tracepoint_events(session->evlist,
3557 					       session->tevent.pevent);
3558 
3559 	return size_read + padding;
3560 }
3561 
3562 int perf_event__synthesize_build_id(struct perf_tool *tool,
3563 				    struct dso *pos, u16 misc,
3564 				    perf_event__handler_t process,
3565 				    struct machine *machine)
3566 {
3567 	union perf_event ev;
3568 	size_t len;
3569 	int err = 0;
3570 
3571 	if (!pos->hit)
3572 		return err;
3573 
3574 	memset(&ev, 0, sizeof(ev));
3575 
3576 	len = pos->long_name_len + 1;
3577 	len = PERF_ALIGN(len, NAME_ALIGN);
3578 	memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3579 	ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3580 	ev.build_id.header.misc = misc;
3581 	ev.build_id.pid = machine->pid;
3582 	ev.build_id.header.size = sizeof(ev.build_id) + len;
3583 	memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3584 
3585 	err = process(tool, &ev, NULL, machine);
3586 
3587 	return err;
3588 }
3589 
3590 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
3591 				 union perf_event *event,
3592 				 struct perf_session *session)
3593 {
3594 	__event_process_build_id(&event->build_id,
3595 				 event->build_id.filename,
3596 				 session);
3597 	return 0;
3598 }
3599