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