xref: /linux/tools/perf/util/header.c (revision c6bd5bcc4983f1a2d2f87a3769bf309482ee8c04)
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 		str += strlen(str) + 1;
1383 	}
1384 	return;
1385 error:
1386 	fprintf(fp, "# numa topology : not available\n");
1387 }
1388 
1389 static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
1390 {
1391 	fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
1392 }
1393 
1394 static void print_branch_stack(struct perf_header *ph __maybe_unused,
1395 			       int fd __maybe_unused, FILE *fp)
1396 {
1397 	fprintf(fp, "# contains samples with branch stack\n");
1398 }
1399 
1400 static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1401 			       FILE *fp)
1402 {
1403 	const char *delimiter = "# pmu mappings: ";
1404 	char *str, *tmp;
1405 	u32 pmu_num;
1406 	u32 type;
1407 
1408 	pmu_num = ph->env.nr_pmu_mappings;
1409 	if (!pmu_num) {
1410 		fprintf(fp, "# pmu mappings: not available\n");
1411 		return;
1412 	}
1413 
1414 	str = ph->env.pmu_mappings;
1415 
1416 	while (pmu_num) {
1417 		type = strtoul(str, &tmp, 0);
1418 		if (*tmp != ':')
1419 			goto error;
1420 
1421 		str = tmp + 1;
1422 		fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1423 
1424 		delimiter = ", ";
1425 		str += strlen(str) + 1;
1426 		pmu_num--;
1427 	}
1428 
1429 	fprintf(fp, "\n");
1430 
1431 	if (!pmu_num)
1432 		return;
1433 error:
1434 	fprintf(fp, "# pmu mappings: unable to read\n");
1435 }
1436 
1437 static int __event_process_build_id(struct build_id_event *bev,
1438 				    char *filename,
1439 				    struct perf_session *session)
1440 {
1441 	int err = -1;
1442 	struct list_head *head;
1443 	struct machine *machine;
1444 	u16 misc;
1445 	struct dso *dso;
1446 	enum dso_kernel_type dso_type;
1447 
1448 	machine = perf_session__findnew_machine(session, bev->pid);
1449 	if (!machine)
1450 		goto out;
1451 
1452 	misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1453 
1454 	switch (misc) {
1455 	case PERF_RECORD_MISC_KERNEL:
1456 		dso_type = DSO_TYPE_KERNEL;
1457 		head = &machine->kernel_dsos;
1458 		break;
1459 	case PERF_RECORD_MISC_GUEST_KERNEL:
1460 		dso_type = DSO_TYPE_GUEST_KERNEL;
1461 		head = &machine->kernel_dsos;
1462 		break;
1463 	case PERF_RECORD_MISC_USER:
1464 	case PERF_RECORD_MISC_GUEST_USER:
1465 		dso_type = DSO_TYPE_USER;
1466 		head = &machine->user_dsos;
1467 		break;
1468 	default:
1469 		goto out;
1470 	}
1471 
1472 	dso = __dsos__findnew(head, filename);
1473 	if (dso != NULL) {
1474 		char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1475 
1476 		dso__set_build_id(dso, &bev->build_id);
1477 
1478 		if (filename[0] == '[')
1479 			dso->kernel = dso_type;
1480 
1481 		build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1482 				  sbuild_id);
1483 		pr_debug("build id event received for %s: %s\n",
1484 			 dso->long_name, sbuild_id);
1485 	}
1486 
1487 	err = 0;
1488 out:
1489 	return err;
1490 }
1491 
1492 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1493 						 int input, u64 offset, u64 size)
1494 {
1495 	struct perf_session *session = container_of(header, struct perf_session, header);
1496 	struct {
1497 		struct perf_event_header   header;
1498 		u8			   build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1499 		char			   filename[0];
1500 	} old_bev;
1501 	struct build_id_event bev;
1502 	char filename[PATH_MAX];
1503 	u64 limit = offset + size;
1504 
1505 	while (offset < limit) {
1506 		ssize_t len;
1507 
1508 		if (read(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1509 			return -1;
1510 
1511 		if (header->needs_swap)
1512 			perf_event_header__bswap(&old_bev.header);
1513 
1514 		len = old_bev.header.size - sizeof(old_bev);
1515 		if (read(input, filename, len) != len)
1516 			return -1;
1517 
1518 		bev.header = old_bev.header;
1519 
1520 		/*
1521 		 * As the pid is the missing value, we need to fill
1522 		 * it properly. The header.misc value give us nice hint.
1523 		 */
1524 		bev.pid	= HOST_KERNEL_ID;
1525 		if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1526 		    bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1527 			bev.pid	= DEFAULT_GUEST_KERNEL_ID;
1528 
1529 		memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1530 		__event_process_build_id(&bev, filename, session);
1531 
1532 		offset += bev.header.size;
1533 	}
1534 
1535 	return 0;
1536 }
1537 
1538 static int perf_header__read_build_ids(struct perf_header *header,
1539 				       int input, u64 offset, u64 size)
1540 {
1541 	struct perf_session *session = container_of(header, struct perf_session, header);
1542 	struct build_id_event bev;
1543 	char filename[PATH_MAX];
1544 	u64 limit = offset + size, orig_offset = offset;
1545 	int err = -1;
1546 
1547 	while (offset < limit) {
1548 		ssize_t len;
1549 
1550 		if (read(input, &bev, sizeof(bev)) != sizeof(bev))
1551 			goto out;
1552 
1553 		if (header->needs_swap)
1554 			perf_event_header__bswap(&bev.header);
1555 
1556 		len = bev.header.size - sizeof(bev);
1557 		if (read(input, filename, len) != len)
1558 			goto out;
1559 		/*
1560 		 * The a1645ce1 changeset:
1561 		 *
1562 		 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1563 		 *
1564 		 * Added a field to struct build_id_event that broke the file
1565 		 * format.
1566 		 *
1567 		 * Since the kernel build-id is the first entry, process the
1568 		 * table using the old format if the well known
1569 		 * '[kernel.kallsyms]' string for the kernel build-id has the
1570 		 * first 4 characters chopped off (where the pid_t sits).
1571 		 */
1572 		if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1573 			if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1574 				return -1;
1575 			return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1576 		}
1577 
1578 		__event_process_build_id(&bev, filename, session);
1579 
1580 		offset += bev.header.size;
1581 	}
1582 	err = 0;
1583 out:
1584 	return err;
1585 }
1586 
1587 static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1588 				struct perf_header *ph __maybe_unused,
1589 				int fd, void *data)
1590 {
1591 	trace_report(fd, data, false);
1592 	return 0;
1593 }
1594 
1595 static int process_build_id(struct perf_file_section *section,
1596 			    struct perf_header *ph, int fd,
1597 			    void *data __maybe_unused)
1598 {
1599 	if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1600 		pr_debug("Failed to read buildids, continuing...\n");
1601 	return 0;
1602 }
1603 
1604 static int process_hostname(struct perf_file_section *section __maybe_unused,
1605 			    struct perf_header *ph, int fd,
1606 			    void *data __maybe_unused)
1607 {
1608 	ph->env.hostname = do_read_string(fd, ph);
1609 	return ph->env.hostname ? 0 : -ENOMEM;
1610 }
1611 
1612 static int process_osrelease(struct perf_file_section *section __maybe_unused,
1613 			     struct perf_header *ph, int fd,
1614 			     void *data __maybe_unused)
1615 {
1616 	ph->env.os_release = do_read_string(fd, ph);
1617 	return ph->env.os_release ? 0 : -ENOMEM;
1618 }
1619 
1620 static int process_version(struct perf_file_section *section __maybe_unused,
1621 			   struct perf_header *ph, int fd,
1622 			   void *data __maybe_unused)
1623 {
1624 	ph->env.version = do_read_string(fd, ph);
1625 	return ph->env.version ? 0 : -ENOMEM;
1626 }
1627 
1628 static int process_arch(struct perf_file_section *section __maybe_unused,
1629 			struct perf_header *ph,	int fd,
1630 			void *data __maybe_unused)
1631 {
1632 	ph->env.arch = do_read_string(fd, ph);
1633 	return ph->env.arch ? 0 : -ENOMEM;
1634 }
1635 
1636 static int process_nrcpus(struct perf_file_section *section __maybe_unused,
1637 			  struct perf_header *ph, int fd,
1638 			  void *data __maybe_unused)
1639 {
1640 	size_t ret;
1641 	u32 nr;
1642 
1643 	ret = read(fd, &nr, sizeof(nr));
1644 	if (ret != sizeof(nr))
1645 		return -1;
1646 
1647 	if (ph->needs_swap)
1648 		nr = bswap_32(nr);
1649 
1650 	ph->env.nr_cpus_online = nr;
1651 
1652 	ret = read(fd, &nr, sizeof(nr));
1653 	if (ret != sizeof(nr))
1654 		return -1;
1655 
1656 	if (ph->needs_swap)
1657 		nr = bswap_32(nr);
1658 
1659 	ph->env.nr_cpus_avail = nr;
1660 	return 0;
1661 }
1662 
1663 static int process_cpudesc(struct perf_file_section *section __maybe_unused,
1664 			   struct perf_header *ph, int fd,
1665 			   void *data __maybe_unused)
1666 {
1667 	ph->env.cpu_desc = do_read_string(fd, ph);
1668 	return ph->env.cpu_desc ? 0 : -ENOMEM;
1669 }
1670 
1671 static int process_cpuid(struct perf_file_section *section __maybe_unused,
1672 			 struct perf_header *ph,  int fd,
1673 			 void *data __maybe_unused)
1674 {
1675 	ph->env.cpuid = do_read_string(fd, ph);
1676 	return ph->env.cpuid ? 0 : -ENOMEM;
1677 }
1678 
1679 static int process_total_mem(struct perf_file_section *section __maybe_unused,
1680 			     struct perf_header *ph, int fd,
1681 			     void *data __maybe_unused)
1682 {
1683 	uint64_t mem;
1684 	size_t ret;
1685 
1686 	ret = read(fd, &mem, sizeof(mem));
1687 	if (ret != sizeof(mem))
1688 		return -1;
1689 
1690 	if (ph->needs_swap)
1691 		mem = bswap_64(mem);
1692 
1693 	ph->env.total_mem = mem;
1694 	return 0;
1695 }
1696 
1697 static struct perf_evsel *
1698 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1699 {
1700 	struct perf_evsel *evsel;
1701 
1702 	list_for_each_entry(evsel, &evlist->entries, node) {
1703 		if (evsel->idx == idx)
1704 			return evsel;
1705 	}
1706 
1707 	return NULL;
1708 }
1709 
1710 static void
1711 perf_evlist__set_event_name(struct perf_evlist *evlist,
1712 			    struct perf_evsel *event)
1713 {
1714 	struct perf_evsel *evsel;
1715 
1716 	if (!event->name)
1717 		return;
1718 
1719 	evsel = perf_evlist__find_by_index(evlist, event->idx);
1720 	if (!evsel)
1721 		return;
1722 
1723 	if (evsel->name)
1724 		return;
1725 
1726 	evsel->name = strdup(event->name);
1727 }
1728 
1729 static int
1730 process_event_desc(struct perf_file_section *section __maybe_unused,
1731 		   struct perf_header *header, int fd,
1732 		   void *data __maybe_unused)
1733 {
1734 	struct perf_session *session;
1735 	struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1736 
1737 	if (!events)
1738 		return 0;
1739 
1740 	session = container_of(header, struct perf_session, header);
1741 	for (evsel = events; evsel->attr.size; evsel++)
1742 		perf_evlist__set_event_name(session->evlist, evsel);
1743 
1744 	free_event_desc(events);
1745 
1746 	return 0;
1747 }
1748 
1749 static int process_cmdline(struct perf_file_section *section __maybe_unused,
1750 			   struct perf_header *ph, int fd,
1751 			   void *data __maybe_unused)
1752 {
1753 	size_t ret;
1754 	char *str;
1755 	u32 nr, i;
1756 	struct strbuf sb;
1757 
1758 	ret = read(fd, &nr, sizeof(nr));
1759 	if (ret != sizeof(nr))
1760 		return -1;
1761 
1762 	if (ph->needs_swap)
1763 		nr = bswap_32(nr);
1764 
1765 	ph->env.nr_cmdline = nr;
1766 	strbuf_init(&sb, 128);
1767 
1768 	for (i = 0; i < nr; i++) {
1769 		str = do_read_string(fd, ph);
1770 		if (!str)
1771 			goto error;
1772 
1773 		/* include a NULL character at the end */
1774 		strbuf_add(&sb, str, strlen(str) + 1);
1775 		free(str);
1776 	}
1777 	ph->env.cmdline = strbuf_detach(&sb, NULL);
1778 	return 0;
1779 
1780 error:
1781 	strbuf_release(&sb);
1782 	return -1;
1783 }
1784 
1785 static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
1786 				struct perf_header *ph, int fd,
1787 				void *data __maybe_unused)
1788 {
1789 	size_t ret;
1790 	u32 nr, i;
1791 	char *str;
1792 	struct strbuf sb;
1793 
1794 	ret = read(fd, &nr, sizeof(nr));
1795 	if (ret != sizeof(nr))
1796 		return -1;
1797 
1798 	if (ph->needs_swap)
1799 		nr = bswap_32(nr);
1800 
1801 	ph->env.nr_sibling_cores = nr;
1802 	strbuf_init(&sb, 128);
1803 
1804 	for (i = 0; i < nr; i++) {
1805 		str = do_read_string(fd, ph);
1806 		if (!str)
1807 			goto error;
1808 
1809 		/* include a NULL character at the end */
1810 		strbuf_add(&sb, str, strlen(str) + 1);
1811 		free(str);
1812 	}
1813 	ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1814 
1815 	ret = read(fd, &nr, sizeof(nr));
1816 	if (ret != sizeof(nr))
1817 		return -1;
1818 
1819 	if (ph->needs_swap)
1820 		nr = bswap_32(nr);
1821 
1822 	ph->env.nr_sibling_threads = nr;
1823 
1824 	for (i = 0; i < nr; i++) {
1825 		str = do_read_string(fd, ph);
1826 		if (!str)
1827 			goto error;
1828 
1829 		/* include a NULL character at the end */
1830 		strbuf_add(&sb, str, strlen(str) + 1);
1831 		free(str);
1832 	}
1833 	ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1834 	return 0;
1835 
1836 error:
1837 	strbuf_release(&sb);
1838 	return -1;
1839 }
1840 
1841 static int process_numa_topology(struct perf_file_section *section __maybe_unused,
1842 				 struct perf_header *ph, int fd,
1843 				 void *data __maybe_unused)
1844 {
1845 	size_t ret;
1846 	u32 nr, node, i;
1847 	char *str;
1848 	uint64_t mem_total, mem_free;
1849 	struct strbuf sb;
1850 
1851 	/* nr nodes */
1852 	ret = read(fd, &nr, sizeof(nr));
1853 	if (ret != sizeof(nr))
1854 		goto error;
1855 
1856 	if (ph->needs_swap)
1857 		nr = bswap_32(nr);
1858 
1859 	ph->env.nr_numa_nodes = nr;
1860 	strbuf_init(&sb, 256);
1861 
1862 	for (i = 0; i < nr; i++) {
1863 		/* node number */
1864 		ret = read(fd, &node, sizeof(node));
1865 		if (ret != sizeof(node))
1866 			goto error;
1867 
1868 		ret = read(fd, &mem_total, sizeof(u64));
1869 		if (ret != sizeof(u64))
1870 			goto error;
1871 
1872 		ret = read(fd, &mem_free, sizeof(u64));
1873 		if (ret != sizeof(u64))
1874 			goto error;
1875 
1876 		if (ph->needs_swap) {
1877 			node = bswap_32(node);
1878 			mem_total = bswap_64(mem_total);
1879 			mem_free = bswap_64(mem_free);
1880 		}
1881 
1882 		strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1883 			    node, mem_total, mem_free);
1884 
1885 		str = do_read_string(fd, ph);
1886 		if (!str)
1887 			goto error;
1888 
1889 		/* include a NULL character at the end */
1890 		strbuf_add(&sb, str, strlen(str) + 1);
1891 		free(str);
1892 	}
1893 	ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1894 	return 0;
1895 
1896 error:
1897 	strbuf_release(&sb);
1898 	return -1;
1899 }
1900 
1901 static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
1902 				struct perf_header *ph, int fd,
1903 				void *data __maybe_unused)
1904 {
1905 	size_t ret;
1906 	char *name;
1907 	u32 pmu_num;
1908 	u32 type;
1909 	struct strbuf sb;
1910 
1911 	ret = read(fd, &pmu_num, sizeof(pmu_num));
1912 	if (ret != sizeof(pmu_num))
1913 		return -1;
1914 
1915 	if (ph->needs_swap)
1916 		pmu_num = bswap_32(pmu_num);
1917 
1918 	if (!pmu_num) {
1919 		pr_debug("pmu mappings not available\n");
1920 		return 0;
1921 	}
1922 
1923 	ph->env.nr_pmu_mappings = pmu_num;
1924 	strbuf_init(&sb, 128);
1925 
1926 	while (pmu_num) {
1927 		if (read(fd, &type, sizeof(type)) != sizeof(type))
1928 			goto error;
1929 		if (ph->needs_swap)
1930 			type = bswap_32(type);
1931 
1932 		name = do_read_string(fd, ph);
1933 		if (!name)
1934 			goto error;
1935 
1936 		strbuf_addf(&sb, "%u:%s", type, name);
1937 		/* include a NULL character at the end */
1938 		strbuf_add(&sb, "", 1);
1939 
1940 		free(name);
1941 		pmu_num--;
1942 	}
1943 	ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1944 	return 0;
1945 
1946 error:
1947 	strbuf_release(&sb);
1948 	return -1;
1949 }
1950 
1951 struct feature_ops {
1952 	int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1953 	void (*print)(struct perf_header *h, int fd, FILE *fp);
1954 	int (*process)(struct perf_file_section *section,
1955 		       struct perf_header *h, int fd, void *data);
1956 	const char *name;
1957 	bool full_only;
1958 };
1959 
1960 #define FEAT_OPA(n, func) \
1961 	[n] = { .name = #n, .write = write_##func, .print = print_##func }
1962 #define FEAT_OPP(n, func) \
1963 	[n] = { .name = #n, .write = write_##func, .print = print_##func, \
1964 		.process = process_##func }
1965 #define FEAT_OPF(n, func) \
1966 	[n] = { .name = #n, .write = write_##func, .print = print_##func, \
1967 		.process = process_##func, .full_only = true }
1968 
1969 /* feature_ops not implemented: */
1970 #define print_tracing_data	NULL
1971 #define print_build_id		NULL
1972 
1973 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
1974 	FEAT_OPP(HEADER_TRACING_DATA,	tracing_data),
1975 	FEAT_OPP(HEADER_BUILD_ID,	build_id),
1976 	FEAT_OPP(HEADER_HOSTNAME,	hostname),
1977 	FEAT_OPP(HEADER_OSRELEASE,	osrelease),
1978 	FEAT_OPP(HEADER_VERSION,	version),
1979 	FEAT_OPP(HEADER_ARCH,		arch),
1980 	FEAT_OPP(HEADER_NRCPUS,		nrcpus),
1981 	FEAT_OPP(HEADER_CPUDESC,	cpudesc),
1982 	FEAT_OPP(HEADER_CPUID,		cpuid),
1983 	FEAT_OPP(HEADER_TOTAL_MEM,	total_mem),
1984 	FEAT_OPP(HEADER_EVENT_DESC,	event_desc),
1985 	FEAT_OPP(HEADER_CMDLINE,	cmdline),
1986 	FEAT_OPF(HEADER_CPU_TOPOLOGY,	cpu_topology),
1987 	FEAT_OPF(HEADER_NUMA_TOPOLOGY,	numa_topology),
1988 	FEAT_OPA(HEADER_BRANCH_STACK,	branch_stack),
1989 	FEAT_OPP(HEADER_PMU_MAPPINGS,	pmu_mappings),
1990 };
1991 
1992 struct header_print_data {
1993 	FILE *fp;
1994 	bool full; /* extended list of headers */
1995 };
1996 
1997 static int perf_file_section__fprintf_info(struct perf_file_section *section,
1998 					   struct perf_header *ph,
1999 					   int feat, int fd, void *data)
2000 {
2001 	struct header_print_data *hd = data;
2002 
2003 	if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2004 		pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2005 				"%d, continuing...\n", section->offset, feat);
2006 		return 0;
2007 	}
2008 	if (feat >= HEADER_LAST_FEATURE) {
2009 		pr_warning("unknown feature %d\n", feat);
2010 		return 0;
2011 	}
2012 	if (!feat_ops[feat].print)
2013 		return 0;
2014 
2015 	if (!feat_ops[feat].full_only || hd->full)
2016 		feat_ops[feat].print(ph, fd, hd->fp);
2017 	else
2018 		fprintf(hd->fp, "# %s info available, use -I to display\n",
2019 			feat_ops[feat].name);
2020 
2021 	return 0;
2022 }
2023 
2024 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2025 {
2026 	struct header_print_data hd;
2027 	struct perf_header *header = &session->header;
2028 	int fd = session->fd;
2029 	hd.fp = fp;
2030 	hd.full = full;
2031 
2032 	perf_header__process_sections(header, fd, &hd,
2033 				      perf_file_section__fprintf_info);
2034 	return 0;
2035 }
2036 
2037 static int do_write_feat(int fd, struct perf_header *h, int type,
2038 			 struct perf_file_section **p,
2039 			 struct perf_evlist *evlist)
2040 {
2041 	int err;
2042 	int ret = 0;
2043 
2044 	if (perf_header__has_feat(h, type)) {
2045 		if (!feat_ops[type].write)
2046 			return -1;
2047 
2048 		(*p)->offset = lseek(fd, 0, SEEK_CUR);
2049 
2050 		err = feat_ops[type].write(fd, h, evlist);
2051 		if (err < 0) {
2052 			pr_debug("failed to write feature %d\n", type);
2053 
2054 			/* undo anything written */
2055 			lseek(fd, (*p)->offset, SEEK_SET);
2056 
2057 			return -1;
2058 		}
2059 		(*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2060 		(*p)++;
2061 	}
2062 	return ret;
2063 }
2064 
2065 static int perf_header__adds_write(struct perf_header *header,
2066 				   struct perf_evlist *evlist, int fd)
2067 {
2068 	int nr_sections;
2069 	struct perf_file_section *feat_sec, *p;
2070 	int sec_size;
2071 	u64 sec_start;
2072 	int feat;
2073 	int err;
2074 
2075 	nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2076 	if (!nr_sections)
2077 		return 0;
2078 
2079 	feat_sec = p = calloc(sizeof(*feat_sec), nr_sections);
2080 	if (feat_sec == NULL)
2081 		return -ENOMEM;
2082 
2083 	sec_size = sizeof(*feat_sec) * nr_sections;
2084 
2085 	sec_start = header->data_offset + header->data_size;
2086 	lseek(fd, sec_start + sec_size, SEEK_SET);
2087 
2088 	for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2089 		if (do_write_feat(fd, header, feat, &p, evlist))
2090 			perf_header__clear_feat(header, feat);
2091 	}
2092 
2093 	lseek(fd, sec_start, SEEK_SET);
2094 	/*
2095 	 * may write more than needed due to dropped feature, but
2096 	 * this is okay, reader will skip the mising entries
2097 	 */
2098 	err = do_write(fd, feat_sec, sec_size);
2099 	if (err < 0)
2100 		pr_debug("failed to write feature section\n");
2101 	free(feat_sec);
2102 	return err;
2103 }
2104 
2105 int perf_header__write_pipe(int fd)
2106 {
2107 	struct perf_pipe_file_header f_header;
2108 	int err;
2109 
2110 	f_header = (struct perf_pipe_file_header){
2111 		.magic	   = PERF_MAGIC,
2112 		.size	   = sizeof(f_header),
2113 	};
2114 
2115 	err = do_write(fd, &f_header, sizeof(f_header));
2116 	if (err < 0) {
2117 		pr_debug("failed to write perf pipe header\n");
2118 		return err;
2119 	}
2120 
2121 	return 0;
2122 }
2123 
2124 int perf_session__write_header(struct perf_session *session,
2125 			       struct perf_evlist *evlist,
2126 			       int fd, bool at_exit)
2127 {
2128 	struct perf_file_header f_header;
2129 	struct perf_file_attr   f_attr;
2130 	struct perf_header *header = &session->header;
2131 	struct perf_evsel *evsel, *pair = NULL;
2132 	int err;
2133 
2134 	lseek(fd, sizeof(f_header), SEEK_SET);
2135 
2136 	if (session->evlist != evlist)
2137 		pair = perf_evlist__first(session->evlist);
2138 
2139 	list_for_each_entry(evsel, &evlist->entries, node) {
2140 		evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2141 		err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
2142 		if (err < 0) {
2143 out_err_write:
2144 			pr_debug("failed to write perf header\n");
2145 			return err;
2146 		}
2147 		if (session->evlist != evlist) {
2148 			err = do_write(fd, pair->id, pair->ids * sizeof(u64));
2149 			if (err < 0)
2150 				goto out_err_write;
2151 			evsel->ids += pair->ids;
2152 			pair = perf_evsel__next(pair);
2153 		}
2154 	}
2155 
2156 	header->attr_offset = lseek(fd, 0, SEEK_CUR);
2157 
2158 	list_for_each_entry(evsel, &evlist->entries, node) {
2159 		f_attr = (struct perf_file_attr){
2160 			.attr = evsel->attr,
2161 			.ids  = {
2162 				.offset = evsel->id_offset,
2163 				.size   = evsel->ids * sizeof(u64),
2164 			}
2165 		};
2166 		err = do_write(fd, &f_attr, sizeof(f_attr));
2167 		if (err < 0) {
2168 			pr_debug("failed to write perf header attribute\n");
2169 			return err;
2170 		}
2171 	}
2172 
2173 	header->event_offset = lseek(fd, 0, SEEK_CUR);
2174 	header->event_size = trace_event_count * sizeof(struct perf_trace_event_type);
2175 	if (trace_events) {
2176 		err = do_write(fd, trace_events, header->event_size);
2177 		if (err < 0) {
2178 			pr_debug("failed to write perf header events\n");
2179 			return err;
2180 		}
2181 	}
2182 
2183 	header->data_offset = lseek(fd, 0, SEEK_CUR);
2184 
2185 	if (at_exit) {
2186 		err = perf_header__adds_write(header, evlist, fd);
2187 		if (err < 0)
2188 			return err;
2189 	}
2190 
2191 	f_header = (struct perf_file_header){
2192 		.magic	   = PERF_MAGIC,
2193 		.size	   = sizeof(f_header),
2194 		.attr_size = sizeof(f_attr),
2195 		.attrs = {
2196 			.offset = header->attr_offset,
2197 			.size   = evlist->nr_entries * sizeof(f_attr),
2198 		},
2199 		.data = {
2200 			.offset = header->data_offset,
2201 			.size	= header->data_size,
2202 		},
2203 		.event_types = {
2204 			.offset = header->event_offset,
2205 			.size	= header->event_size,
2206 		},
2207 	};
2208 
2209 	memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2210 
2211 	lseek(fd, 0, SEEK_SET);
2212 	err = do_write(fd, &f_header, sizeof(f_header));
2213 	if (err < 0) {
2214 		pr_debug("failed to write perf header\n");
2215 		return err;
2216 	}
2217 	lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2218 
2219 	header->frozen = 1;
2220 	return 0;
2221 }
2222 
2223 static int perf_header__getbuffer64(struct perf_header *header,
2224 				    int fd, void *buf, size_t size)
2225 {
2226 	if (readn(fd, buf, size) <= 0)
2227 		return -1;
2228 
2229 	if (header->needs_swap)
2230 		mem_bswap_64(buf, size);
2231 
2232 	return 0;
2233 }
2234 
2235 int perf_header__process_sections(struct perf_header *header, int fd,
2236 				  void *data,
2237 				  int (*process)(struct perf_file_section *section,
2238 						 struct perf_header *ph,
2239 						 int feat, int fd, void *data))
2240 {
2241 	struct perf_file_section *feat_sec, *sec;
2242 	int nr_sections;
2243 	int sec_size;
2244 	int feat;
2245 	int err;
2246 
2247 	nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2248 	if (!nr_sections)
2249 		return 0;
2250 
2251 	feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections);
2252 	if (!feat_sec)
2253 		return -1;
2254 
2255 	sec_size = sizeof(*feat_sec) * nr_sections;
2256 
2257 	lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2258 
2259 	err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2260 	if (err < 0)
2261 		goto out_free;
2262 
2263 	for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2264 		err = process(sec++, header, feat, fd, data);
2265 		if (err < 0)
2266 			goto out_free;
2267 	}
2268 	err = 0;
2269 out_free:
2270 	free(feat_sec);
2271 	return err;
2272 }
2273 
2274 static const int attr_file_abi_sizes[] = {
2275 	[0] = PERF_ATTR_SIZE_VER0,
2276 	[1] = PERF_ATTR_SIZE_VER1,
2277 	[2] = PERF_ATTR_SIZE_VER2,
2278 	[3] = PERF_ATTR_SIZE_VER3,
2279 	0,
2280 };
2281 
2282 /*
2283  * In the legacy file format, the magic number is not used to encode endianness.
2284  * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2285  * on ABI revisions, we need to try all combinations for all endianness to
2286  * detect the endianness.
2287  */
2288 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2289 {
2290 	uint64_t ref_size, attr_size;
2291 	int i;
2292 
2293 	for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2294 		ref_size = attr_file_abi_sizes[i]
2295 			 + sizeof(struct perf_file_section);
2296 		if (hdr_sz != ref_size) {
2297 			attr_size = bswap_64(hdr_sz);
2298 			if (attr_size != ref_size)
2299 				continue;
2300 
2301 			ph->needs_swap = true;
2302 		}
2303 		pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2304 			 i,
2305 			 ph->needs_swap);
2306 		return 0;
2307 	}
2308 	/* could not determine endianness */
2309 	return -1;
2310 }
2311 
2312 #define PERF_PIPE_HDR_VER0	16
2313 
2314 static const size_t attr_pipe_abi_sizes[] = {
2315 	[0] = PERF_PIPE_HDR_VER0,
2316 	0,
2317 };
2318 
2319 /*
2320  * In the legacy pipe format, there is an implicit assumption that endiannesss
2321  * between host recording the samples, and host parsing the samples is the
2322  * same. This is not always the case given that the pipe output may always be
2323  * redirected into a file and analyzed on a different machine with possibly a
2324  * different endianness and perf_event ABI revsions in the perf tool itself.
2325  */
2326 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2327 {
2328 	u64 attr_size;
2329 	int i;
2330 
2331 	for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2332 		if (hdr_sz != attr_pipe_abi_sizes[i]) {
2333 			attr_size = bswap_64(hdr_sz);
2334 			if (attr_size != hdr_sz)
2335 				continue;
2336 
2337 			ph->needs_swap = true;
2338 		}
2339 		pr_debug("Pipe ABI%d perf.data file detected\n", i);
2340 		return 0;
2341 	}
2342 	return -1;
2343 }
2344 
2345 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2346 			      bool is_pipe, struct perf_header *ph)
2347 {
2348 	int ret;
2349 
2350 	/* check for legacy format */
2351 	ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2352 	if (ret == 0) {
2353 		pr_debug("legacy perf.data format\n");
2354 		if (is_pipe)
2355 			return try_all_pipe_abis(hdr_sz, ph);
2356 
2357 		return try_all_file_abis(hdr_sz, ph);
2358 	}
2359 	/*
2360 	 * the new magic number serves two purposes:
2361 	 * - unique number to identify actual perf.data files
2362 	 * - encode endianness of file
2363 	 */
2364 
2365 	/* check magic number with one endianness */
2366 	if (magic == __perf_magic2)
2367 		return 0;
2368 
2369 	/* check magic number with opposite endianness */
2370 	if (magic != __perf_magic2_sw)
2371 		return -1;
2372 
2373 	ph->needs_swap = true;
2374 
2375 	return 0;
2376 }
2377 
2378 int perf_file_header__read(struct perf_file_header *header,
2379 			   struct perf_header *ph, int fd)
2380 {
2381 	int ret;
2382 
2383 	lseek(fd, 0, SEEK_SET);
2384 
2385 	ret = readn(fd, header, sizeof(*header));
2386 	if (ret <= 0)
2387 		return -1;
2388 
2389 	if (check_magic_endian(header->magic,
2390 			       header->attr_size, false, ph) < 0) {
2391 		pr_debug("magic/endian check failed\n");
2392 		return -1;
2393 	}
2394 
2395 	if (ph->needs_swap) {
2396 		mem_bswap_64(header, offsetof(struct perf_file_header,
2397 			     adds_features));
2398 	}
2399 
2400 	if (header->size != sizeof(*header)) {
2401 		/* Support the previous format */
2402 		if (header->size == offsetof(typeof(*header), adds_features))
2403 			bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2404 		else
2405 			return -1;
2406 	} else if (ph->needs_swap) {
2407 		/*
2408 		 * feature bitmap is declared as an array of unsigned longs --
2409 		 * not good since its size can differ between the host that
2410 		 * generated the data file and the host analyzing the file.
2411 		 *
2412 		 * We need to handle endianness, but we don't know the size of
2413 		 * the unsigned long where the file was generated. Take a best
2414 		 * guess at determining it: try 64-bit swap first (ie., file
2415 		 * created on a 64-bit host), and check if the hostname feature
2416 		 * bit is set (this feature bit is forced on as of fbe96f2).
2417 		 * If the bit is not, undo the 64-bit swap and try a 32-bit
2418 		 * swap. If the hostname bit is still not set (e.g., older data
2419 		 * file), punt and fallback to the original behavior --
2420 		 * clearing all feature bits and setting buildid.
2421 		 */
2422 		mem_bswap_64(&header->adds_features,
2423 			    BITS_TO_U64(HEADER_FEAT_BITS));
2424 
2425 		if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2426 			/* unswap as u64 */
2427 			mem_bswap_64(&header->adds_features,
2428 				    BITS_TO_U64(HEADER_FEAT_BITS));
2429 
2430 			/* unswap as u32 */
2431 			mem_bswap_32(&header->adds_features,
2432 				    BITS_TO_U32(HEADER_FEAT_BITS));
2433 		}
2434 
2435 		if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2436 			bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2437 			set_bit(HEADER_BUILD_ID, header->adds_features);
2438 		}
2439 	}
2440 
2441 	memcpy(&ph->adds_features, &header->adds_features,
2442 	       sizeof(ph->adds_features));
2443 
2444 	ph->event_offset = header->event_types.offset;
2445 	ph->event_size   = header->event_types.size;
2446 	ph->data_offset  = header->data.offset;
2447 	ph->data_size	 = header->data.size;
2448 	return 0;
2449 }
2450 
2451 static int perf_file_section__process(struct perf_file_section *section,
2452 				      struct perf_header *ph,
2453 				      int feat, int fd, void *data)
2454 {
2455 	if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2456 		pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2457 			  "%d, continuing...\n", section->offset, feat);
2458 		return 0;
2459 	}
2460 
2461 	if (feat >= HEADER_LAST_FEATURE) {
2462 		pr_debug("unknown feature %d, continuing...\n", feat);
2463 		return 0;
2464 	}
2465 
2466 	if (!feat_ops[feat].process)
2467 		return 0;
2468 
2469 	return feat_ops[feat].process(section, ph, fd, data);
2470 }
2471 
2472 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2473 				       struct perf_header *ph, int fd,
2474 				       bool repipe)
2475 {
2476 	int ret;
2477 
2478 	ret = readn(fd, header, sizeof(*header));
2479 	if (ret <= 0)
2480 		return -1;
2481 
2482 	if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2483 		pr_debug("endian/magic failed\n");
2484 		return -1;
2485 	}
2486 
2487 	if (ph->needs_swap)
2488 		header->size = bswap_64(header->size);
2489 
2490 	if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
2491 		return -1;
2492 
2493 	return 0;
2494 }
2495 
2496 static int perf_header__read_pipe(struct perf_session *session, int fd)
2497 {
2498 	struct perf_header *header = &session->header;
2499 	struct perf_pipe_file_header f_header;
2500 
2501 	if (perf_file_header__read_pipe(&f_header, header, fd,
2502 					session->repipe) < 0) {
2503 		pr_debug("incompatible file format\n");
2504 		return -EINVAL;
2505 	}
2506 
2507 	session->fd = fd;
2508 
2509 	return 0;
2510 }
2511 
2512 static int read_attr(int fd, struct perf_header *ph,
2513 		     struct perf_file_attr *f_attr)
2514 {
2515 	struct perf_event_attr *attr = &f_attr->attr;
2516 	size_t sz, left;
2517 	size_t our_sz = sizeof(f_attr->attr);
2518 	int ret;
2519 
2520 	memset(f_attr, 0, sizeof(*f_attr));
2521 
2522 	/* read minimal guaranteed structure */
2523 	ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2524 	if (ret <= 0) {
2525 		pr_debug("cannot read %d bytes of header attr\n",
2526 			 PERF_ATTR_SIZE_VER0);
2527 		return -1;
2528 	}
2529 
2530 	/* on file perf_event_attr size */
2531 	sz = attr->size;
2532 
2533 	if (ph->needs_swap)
2534 		sz = bswap_32(sz);
2535 
2536 	if (sz == 0) {
2537 		/* assume ABI0 */
2538 		sz =  PERF_ATTR_SIZE_VER0;
2539 	} else if (sz > our_sz) {
2540 		pr_debug("file uses a more recent and unsupported ABI"
2541 			 " (%zu bytes extra)\n", sz - our_sz);
2542 		return -1;
2543 	}
2544 	/* what we have not yet read and that we know about */
2545 	left = sz - PERF_ATTR_SIZE_VER0;
2546 	if (left) {
2547 		void *ptr = attr;
2548 		ptr += PERF_ATTR_SIZE_VER0;
2549 
2550 		ret = readn(fd, ptr, left);
2551 	}
2552 	/* read perf_file_section, ids are read in caller */
2553 	ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2554 
2555 	return ret <= 0 ? -1 : 0;
2556 }
2557 
2558 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2559 						struct pevent *pevent)
2560 {
2561 	struct event_format *event;
2562 	char bf[128];
2563 
2564 	/* already prepared */
2565 	if (evsel->tp_format)
2566 		return 0;
2567 
2568 	event = pevent_find_event(pevent, evsel->attr.config);
2569 	if (event == NULL)
2570 		return -1;
2571 
2572 	if (!evsel->name) {
2573 		snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2574 		evsel->name = strdup(bf);
2575 		if (evsel->name == NULL)
2576 			return -1;
2577 	}
2578 
2579 	evsel->tp_format = event;
2580 	return 0;
2581 }
2582 
2583 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2584 						  struct pevent *pevent)
2585 {
2586 	struct perf_evsel *pos;
2587 
2588 	list_for_each_entry(pos, &evlist->entries, node) {
2589 		if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2590 		    perf_evsel__prepare_tracepoint_event(pos, pevent))
2591 			return -1;
2592 	}
2593 
2594 	return 0;
2595 }
2596 
2597 int perf_session__read_header(struct perf_session *session, int fd)
2598 {
2599 	struct perf_header *header = &session->header;
2600 	struct perf_file_header	f_header;
2601 	struct perf_file_attr	f_attr;
2602 	u64			f_id;
2603 	int nr_attrs, nr_ids, i, j;
2604 
2605 	session->evlist = perf_evlist__new(NULL, NULL);
2606 	if (session->evlist == NULL)
2607 		return -ENOMEM;
2608 
2609 	if (session->fd_pipe)
2610 		return perf_header__read_pipe(session, fd);
2611 
2612 	if (perf_file_header__read(&f_header, header, fd) < 0)
2613 		return -EINVAL;
2614 
2615 	nr_attrs = f_header.attrs.size / f_header.attr_size;
2616 	lseek(fd, f_header.attrs.offset, SEEK_SET);
2617 
2618 	for (i = 0; i < nr_attrs; i++) {
2619 		struct perf_evsel *evsel;
2620 		off_t tmp;
2621 
2622 		if (read_attr(fd, header, &f_attr) < 0)
2623 			goto out_errno;
2624 
2625 		if (header->needs_swap)
2626 			perf_event__attr_swap(&f_attr.attr);
2627 
2628 		tmp = lseek(fd, 0, SEEK_CUR);
2629 		evsel = perf_evsel__new(&f_attr.attr, i);
2630 
2631 		if (evsel == NULL)
2632 			goto out_delete_evlist;
2633 
2634 		evsel->needs_swap = header->needs_swap;
2635 		/*
2636 		 * Do it before so that if perf_evsel__alloc_id fails, this
2637 		 * entry gets purged too at perf_evlist__delete().
2638 		 */
2639 		perf_evlist__add(session->evlist, evsel);
2640 
2641 		nr_ids = f_attr.ids.size / sizeof(u64);
2642 		/*
2643 		 * We don't have the cpu and thread maps on the header, so
2644 		 * for allocating the perf_sample_id table we fake 1 cpu and
2645 		 * hattr->ids threads.
2646 		 */
2647 		if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2648 			goto out_delete_evlist;
2649 
2650 		lseek(fd, f_attr.ids.offset, SEEK_SET);
2651 
2652 		for (j = 0; j < nr_ids; j++) {
2653 			if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2654 				goto out_errno;
2655 
2656 			perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2657 		}
2658 
2659 		lseek(fd, tmp, SEEK_SET);
2660 	}
2661 
2662 	symbol_conf.nr_events = nr_attrs;
2663 
2664 	if (f_header.event_types.size) {
2665 		lseek(fd, f_header.event_types.offset, SEEK_SET);
2666 		trace_events = malloc(f_header.event_types.size);
2667 		if (trace_events == NULL)
2668 			return -ENOMEM;
2669 		if (perf_header__getbuffer64(header, fd, trace_events,
2670 					     f_header.event_types.size))
2671 			goto out_errno;
2672 		trace_event_count =  f_header.event_types.size / sizeof(struct perf_trace_event_type);
2673 	}
2674 
2675 	perf_header__process_sections(header, fd, &session->pevent,
2676 				      perf_file_section__process);
2677 
2678 	lseek(fd, header->data_offset, SEEK_SET);
2679 
2680 	if (perf_evlist__prepare_tracepoint_events(session->evlist,
2681 						   session->pevent))
2682 		goto out_delete_evlist;
2683 
2684 	header->frozen = 1;
2685 	return 0;
2686 out_errno:
2687 	return -errno;
2688 
2689 out_delete_evlist:
2690 	perf_evlist__delete(session->evlist);
2691 	session->evlist = NULL;
2692 	return -ENOMEM;
2693 }
2694 
2695 int perf_event__synthesize_attr(struct perf_tool *tool,
2696 				struct perf_event_attr *attr, u32 ids, u64 *id,
2697 				perf_event__handler_t process)
2698 {
2699 	union perf_event *ev;
2700 	size_t size;
2701 	int err;
2702 
2703 	size = sizeof(struct perf_event_attr);
2704 	size = PERF_ALIGN(size, sizeof(u64));
2705 	size += sizeof(struct perf_event_header);
2706 	size += ids * sizeof(u64);
2707 
2708 	ev = malloc(size);
2709 
2710 	if (ev == NULL)
2711 		return -ENOMEM;
2712 
2713 	ev->attr.attr = *attr;
2714 	memcpy(ev->attr.id, id, ids * sizeof(u64));
2715 
2716 	ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2717 	ev->attr.header.size = (u16)size;
2718 
2719 	if (ev->attr.header.size == size)
2720 		err = process(tool, ev, NULL, NULL);
2721 	else
2722 		err = -E2BIG;
2723 
2724 	free(ev);
2725 
2726 	return err;
2727 }
2728 
2729 int perf_event__synthesize_attrs(struct perf_tool *tool,
2730 				   struct perf_session *session,
2731 				   perf_event__handler_t process)
2732 {
2733 	struct perf_evsel *evsel;
2734 	int err = 0;
2735 
2736 	list_for_each_entry(evsel, &session->evlist->entries, node) {
2737 		err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2738 						  evsel->id, process);
2739 		if (err) {
2740 			pr_debug("failed to create perf header attribute\n");
2741 			return err;
2742 		}
2743 	}
2744 
2745 	return err;
2746 }
2747 
2748 int perf_event__process_attr(union perf_event *event,
2749 			     struct perf_evlist **pevlist)
2750 {
2751 	u32 i, ids, n_ids;
2752 	struct perf_evsel *evsel;
2753 	struct perf_evlist *evlist = *pevlist;
2754 
2755 	if (evlist == NULL) {
2756 		*pevlist = evlist = perf_evlist__new(NULL, NULL);
2757 		if (evlist == NULL)
2758 			return -ENOMEM;
2759 	}
2760 
2761 	evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
2762 	if (evsel == NULL)
2763 		return -ENOMEM;
2764 
2765 	perf_evlist__add(evlist, evsel);
2766 
2767 	ids = event->header.size;
2768 	ids -= (void *)&event->attr.id - (void *)event;
2769 	n_ids = ids / sizeof(u64);
2770 	/*
2771 	 * We don't have the cpu and thread maps on the header, so
2772 	 * for allocating the perf_sample_id table we fake 1 cpu and
2773 	 * hattr->ids threads.
2774 	 */
2775 	if (perf_evsel__alloc_id(evsel, 1, n_ids))
2776 		return -ENOMEM;
2777 
2778 	for (i = 0; i < n_ids; i++) {
2779 		perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2780 	}
2781 
2782 	return 0;
2783 }
2784 
2785 int perf_event__synthesize_event_type(struct perf_tool *tool,
2786 				      u64 event_id, char *name,
2787 				      perf_event__handler_t process,
2788 				      struct machine *machine)
2789 {
2790 	union perf_event ev;
2791 	size_t size = 0;
2792 	int err = 0;
2793 
2794 	memset(&ev, 0, sizeof(ev));
2795 
2796 	ev.event_type.event_type.event_id = event_id;
2797 	memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
2798 	strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
2799 
2800 	ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
2801 	size = strlen(ev.event_type.event_type.name);
2802 	size = PERF_ALIGN(size, sizeof(u64));
2803 	ev.event_type.header.size = sizeof(ev.event_type) -
2804 		(sizeof(ev.event_type.event_type.name) - size);
2805 
2806 	err = process(tool, &ev, NULL, machine);
2807 
2808 	return err;
2809 }
2810 
2811 int perf_event__synthesize_event_types(struct perf_tool *tool,
2812 				       perf_event__handler_t process,
2813 				       struct machine *machine)
2814 {
2815 	struct perf_trace_event_type *type;
2816 	int i, err = 0;
2817 
2818 	for (i = 0; i < trace_event_count; i++) {
2819 		type = &trace_events[i];
2820 
2821 		err = perf_event__synthesize_event_type(tool, type->event_id,
2822 							type->name, process,
2823 							machine);
2824 		if (err) {
2825 			pr_debug("failed to create perf header event type\n");
2826 			return err;
2827 		}
2828 	}
2829 
2830 	return err;
2831 }
2832 
2833 int perf_event__process_event_type(struct perf_tool *tool __maybe_unused,
2834 				   union perf_event *event)
2835 {
2836 	if (perf_header__push_event(event->event_type.event_type.event_id,
2837 				    event->event_type.event_type.name) < 0)
2838 		return -ENOMEM;
2839 
2840 	return 0;
2841 }
2842 
2843 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
2844 					struct perf_evlist *evlist,
2845 					perf_event__handler_t process)
2846 {
2847 	union perf_event ev;
2848 	struct tracing_data *tdata;
2849 	ssize_t size = 0, aligned_size = 0, padding;
2850 	int err __maybe_unused = 0;
2851 
2852 	/*
2853 	 * We are going to store the size of the data followed
2854 	 * by the data contents. Since the fd descriptor is a pipe,
2855 	 * we cannot seek back to store the size of the data once
2856 	 * we know it. Instead we:
2857 	 *
2858 	 * - write the tracing data to the temp file
2859 	 * - get/write the data size to pipe
2860 	 * - write the tracing data from the temp file
2861 	 *   to the pipe
2862 	 */
2863 	tdata = tracing_data_get(&evlist->entries, fd, true);
2864 	if (!tdata)
2865 		return -1;
2866 
2867 	memset(&ev, 0, sizeof(ev));
2868 
2869 	ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2870 	size = tdata->size;
2871 	aligned_size = PERF_ALIGN(size, sizeof(u64));
2872 	padding = aligned_size - size;
2873 	ev.tracing_data.header.size = sizeof(ev.tracing_data);
2874 	ev.tracing_data.size = aligned_size;
2875 
2876 	process(tool, &ev, NULL, NULL);
2877 
2878 	/*
2879 	 * The put function will copy all the tracing data
2880 	 * stored in temp file to the pipe.
2881 	 */
2882 	tracing_data_put(tdata);
2883 
2884 	write_padded(fd, NULL, 0, padding);
2885 
2886 	return aligned_size;
2887 }
2888 
2889 int perf_event__process_tracing_data(union perf_event *event,
2890 				     struct perf_session *session)
2891 {
2892 	ssize_t size_read, padding, size = event->tracing_data.size;
2893 	off_t offset = lseek(session->fd, 0, SEEK_CUR);
2894 	char buf[BUFSIZ];
2895 
2896 	/* setup for reading amidst mmap */
2897 	lseek(session->fd, offset + sizeof(struct tracing_data_event),
2898 	      SEEK_SET);
2899 
2900 	size_read = trace_report(session->fd, &session->pevent,
2901 				 session->repipe);
2902 	padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
2903 
2904 	if (read(session->fd, buf, padding) < 0)
2905 		die("reading input file");
2906 	if (session->repipe) {
2907 		int retw = write(STDOUT_FILENO, buf, padding);
2908 		if (retw <= 0 || retw != padding)
2909 			die("repiping tracing data padding");
2910 	}
2911 
2912 	if (size_read + padding != size)
2913 		die("tracing data size mismatch");
2914 
2915 	perf_evlist__prepare_tracepoint_events(session->evlist,
2916 					       session->pevent);
2917 
2918 	return size_read + padding;
2919 }
2920 
2921 int perf_event__synthesize_build_id(struct perf_tool *tool,
2922 				    struct dso *pos, u16 misc,
2923 				    perf_event__handler_t process,
2924 				    struct machine *machine)
2925 {
2926 	union perf_event ev;
2927 	size_t len;
2928 	int err = 0;
2929 
2930 	if (!pos->hit)
2931 		return err;
2932 
2933 	memset(&ev, 0, sizeof(ev));
2934 
2935 	len = pos->long_name_len + 1;
2936 	len = PERF_ALIGN(len, NAME_ALIGN);
2937 	memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2938 	ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2939 	ev.build_id.header.misc = misc;
2940 	ev.build_id.pid = machine->pid;
2941 	ev.build_id.header.size = sizeof(ev.build_id) + len;
2942 	memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2943 
2944 	err = process(tool, &ev, NULL, machine);
2945 
2946 	return err;
2947 }
2948 
2949 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
2950 				 union perf_event *event,
2951 				 struct perf_session *session)
2952 {
2953 	__event_process_build_id(&event->build_id,
2954 				 event->build_id.filename,
2955 				 session);
2956 	return 0;
2957 }
2958 
2959 void disable_buildid_cache(void)
2960 {
2961 	no_buildid_cache = true;
2962 }
2963