1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <inttypes.h>
4 #include "string2.h"
5 #include <sys/param.h>
6 #include <sys/types.h>
7 #include <byteswap.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <linux/compiler.h>
12 #include <linux/list.h>
13 #include <linux/kernel.h>
14 #include <linux/bitops.h>
15 #include <linux/string.h>
16 #include <linux/stringify.h>
17 #include <linux/zalloc.h>
18 #include <sys/stat.h>
19 #include <sys/utsname.h>
20 #include <linux/time64.h>
21 #include <dirent.h>
22 #ifdef HAVE_LIBBPF_SUPPORT
23 #include <bpf/libbpf.h>
24 #endif
25 #include <perf/cpumap.h>
26 
27 #include "dso.h"
28 #include "evlist.h"
29 #include "evsel.h"
30 #include "util/evsel_fprintf.h"
31 #include "header.h"
32 #include "memswap.h"
33 #include "trace-event.h"
34 #include "session.h"
35 #include "symbol.h"
36 #include "debug.h"
37 #include "cpumap.h"
38 #include "pmu.h"
39 #include "vdso.h"
40 #include "strbuf.h"
41 #include "build-id.h"
42 #include "data.h"
43 #include <api/fs/fs.h>
44 #include "asm/bug.h"
45 #include "tool.h"
46 #include "time-utils.h"
47 #include "units.h"
48 #include "util/util.h" // perf_exe()
49 #include "cputopo.h"
50 #include "bpf-event.h"
51 #include "bpf-utils.h"
52 #include "clockid.h"
53 #include "pmu-hybrid.h"
54 
55 #include <linux/ctype.h>
56 #include <internal/lib.h>
57 
58 /*
59  * magic2 = "PERFILE2"
60  * must be a numerical value to let the endianness
61  * determine the memory layout. That way we are able
62  * to detect endianness when reading the perf.data file
63  * back.
64  *
65  * we check for legacy (PERFFILE) format.
66  */
67 static const char *__perf_magic1 = "PERFFILE";
68 static const u64 __perf_magic2    = 0x32454c4946524550ULL;
69 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
70 
71 #define PERF_MAGIC	__perf_magic2
72 
73 const char perf_version_string[] = PERF_VERSION;
74 
75 struct perf_file_attr {
76 	struct perf_event_attr	attr;
77 	struct perf_file_section	ids;
78 };
79 
perf_header__set_feat(struct perf_header * header,int feat)80 void perf_header__set_feat(struct perf_header *header, int feat)
81 {
82 	set_bit(feat, header->adds_features);
83 }
84 
perf_header__clear_feat(struct perf_header * header,int feat)85 void perf_header__clear_feat(struct perf_header *header, int feat)
86 {
87 	clear_bit(feat, header->adds_features);
88 }
89 
perf_header__has_feat(const struct perf_header * header,int feat)90 bool perf_header__has_feat(const struct perf_header *header, int feat)
91 {
92 	return test_bit(feat, header->adds_features);
93 }
94 
__do_write_fd(struct feat_fd * ff,const void * buf,size_t size)95 static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
96 {
97 	ssize_t ret = writen(ff->fd, buf, size);
98 
99 	if (ret != (ssize_t)size)
100 		return ret < 0 ? (int)ret : -1;
101 	return 0;
102 }
103 
__do_write_buf(struct feat_fd * ff,const void * buf,size_t size)104 static int __do_write_buf(struct feat_fd *ff,  const void *buf, size_t size)
105 {
106 	/* struct perf_event_header::size is u16 */
107 	const size_t max_size = 0xffff - sizeof(struct perf_event_header);
108 	size_t new_size = ff->size;
109 	void *addr;
110 
111 	if (size + ff->offset > max_size)
112 		return -E2BIG;
113 
114 	while (size > (new_size - ff->offset))
115 		new_size <<= 1;
116 	new_size = min(max_size, new_size);
117 
118 	if (ff->size < new_size) {
119 		addr = realloc(ff->buf, new_size);
120 		if (!addr)
121 			return -ENOMEM;
122 		ff->buf = addr;
123 		ff->size = new_size;
124 	}
125 
126 	memcpy(ff->buf + ff->offset, buf, size);
127 	ff->offset += size;
128 
129 	return 0;
130 }
131 
132 /* Return: 0 if succeeded, -ERR if failed. */
do_write(struct feat_fd * ff,const void * buf,size_t size)133 int do_write(struct feat_fd *ff, const void *buf, size_t size)
134 {
135 	if (!ff->buf)
136 		return __do_write_fd(ff, buf, size);
137 	return __do_write_buf(ff, buf, size);
138 }
139 
140 /* Return: 0 if succeeded, -ERR if failed. */
do_write_bitmap(struct feat_fd * ff,unsigned long * set,u64 size)141 static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
142 {
143 	u64 *p = (u64 *) set;
144 	int i, ret;
145 
146 	ret = do_write(ff, &size, sizeof(size));
147 	if (ret < 0)
148 		return ret;
149 
150 	for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
151 		ret = do_write(ff, p + i, sizeof(*p));
152 		if (ret < 0)
153 			return ret;
154 	}
155 
156 	return 0;
157 }
158 
159 /* Return: 0 if succeeded, -ERR if failed. */
write_padded(struct feat_fd * ff,const void * bf,size_t count,size_t count_aligned)160 int write_padded(struct feat_fd *ff, const void *bf,
161 		 size_t count, size_t count_aligned)
162 {
163 	static const char zero_buf[NAME_ALIGN];
164 	int err = do_write(ff, bf, count);
165 
166 	if (!err)
167 		err = do_write(ff, zero_buf, count_aligned - count);
168 
169 	return err;
170 }
171 
172 #define string_size(str)						\
173 	(PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
174 
175 /* Return: 0 if succeeded, -ERR if failed. */
do_write_string(struct feat_fd * ff,const char * str)176 static int do_write_string(struct feat_fd *ff, const char *str)
177 {
178 	u32 len, olen;
179 	int ret;
180 
181 	olen = strlen(str) + 1;
182 	len = PERF_ALIGN(olen, NAME_ALIGN);
183 
184 	/* write len, incl. \0 */
185 	ret = do_write(ff, &len, sizeof(len));
186 	if (ret < 0)
187 		return ret;
188 
189 	return write_padded(ff, str, olen, len);
190 }
191 
__do_read_fd(struct feat_fd * ff,void * addr,ssize_t size)192 static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
193 {
194 	ssize_t ret = readn(ff->fd, addr, size);
195 
196 	if (ret != size)
197 		return ret < 0 ? (int)ret : -1;
198 	return 0;
199 }
200 
__do_read_buf(struct feat_fd * ff,void * addr,ssize_t size)201 static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
202 {
203 	if (size > (ssize_t)ff->size - ff->offset)
204 		return -1;
205 
206 	memcpy(addr, ff->buf + ff->offset, size);
207 	ff->offset += size;
208 
209 	return 0;
210 
211 }
212 
__do_read(struct feat_fd * ff,void * addr,ssize_t size)213 static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
214 {
215 	if (!ff->buf)
216 		return __do_read_fd(ff, addr, size);
217 	return __do_read_buf(ff, addr, size);
218 }
219 
do_read_u32(struct feat_fd * ff,u32 * addr)220 static int do_read_u32(struct feat_fd *ff, u32 *addr)
221 {
222 	int ret;
223 
224 	ret = __do_read(ff, addr, sizeof(*addr));
225 	if (ret)
226 		return ret;
227 
228 	if (ff->ph->needs_swap)
229 		*addr = bswap_32(*addr);
230 	return 0;
231 }
232 
do_read_u64(struct feat_fd * ff,u64 * addr)233 static int do_read_u64(struct feat_fd *ff, u64 *addr)
234 {
235 	int ret;
236 
237 	ret = __do_read(ff, addr, sizeof(*addr));
238 	if (ret)
239 		return ret;
240 
241 	if (ff->ph->needs_swap)
242 		*addr = bswap_64(*addr);
243 	return 0;
244 }
245 
do_read_string(struct feat_fd * ff)246 static char *do_read_string(struct feat_fd *ff)
247 {
248 	u32 len;
249 	char *buf;
250 
251 	if (do_read_u32(ff, &len))
252 		return NULL;
253 
254 	buf = malloc(len);
255 	if (!buf)
256 		return NULL;
257 
258 	if (!__do_read(ff, buf, len)) {
259 		/*
260 		 * strings are padded by zeroes
261 		 * thus the actual strlen of buf
262 		 * may be less than len
263 		 */
264 		return buf;
265 	}
266 
267 	free(buf);
268 	return NULL;
269 }
270 
271 /* Return: 0 if succeeded, -ERR if failed. */
do_read_bitmap(struct feat_fd * ff,unsigned long ** pset,u64 * psize)272 static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
273 {
274 	unsigned long *set;
275 	u64 size, *p;
276 	int i, ret;
277 
278 	ret = do_read_u64(ff, &size);
279 	if (ret)
280 		return ret;
281 
282 	set = bitmap_zalloc(size);
283 	if (!set)
284 		return -ENOMEM;
285 
286 	p = (u64 *) set;
287 
288 	for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
289 		ret = do_read_u64(ff, p + i);
290 		if (ret < 0) {
291 			free(set);
292 			return ret;
293 		}
294 	}
295 
296 	*pset  = set;
297 	*psize = size;
298 	return 0;
299 }
300 
write_tracing_data(struct feat_fd * ff,struct evlist * evlist)301 static int write_tracing_data(struct feat_fd *ff,
302 			      struct evlist *evlist)
303 {
304 	if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
305 		return -1;
306 
307 	return read_tracing_data(ff->fd, &evlist->core.entries);
308 }
309 
write_build_id(struct feat_fd * ff,struct evlist * evlist __maybe_unused)310 static int write_build_id(struct feat_fd *ff,
311 			  struct evlist *evlist __maybe_unused)
312 {
313 	struct perf_session *session;
314 	int err;
315 
316 	session = container_of(ff->ph, struct perf_session, header);
317 
318 	if (!perf_session__read_build_ids(session, true))
319 		return -1;
320 
321 	if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
322 		return -1;
323 
324 	err = perf_session__write_buildid_table(session, ff);
325 	if (err < 0) {
326 		pr_debug("failed to write buildid table\n");
327 		return err;
328 	}
329 	perf_session__cache_build_ids(session);
330 
331 	return 0;
332 }
333 
write_hostname(struct feat_fd * ff,struct evlist * evlist __maybe_unused)334 static int write_hostname(struct feat_fd *ff,
335 			  struct evlist *evlist __maybe_unused)
336 {
337 	struct utsname uts;
338 	int ret;
339 
340 	ret = uname(&uts);
341 	if (ret < 0)
342 		return -1;
343 
344 	return do_write_string(ff, uts.nodename);
345 }
346 
write_osrelease(struct feat_fd * ff,struct evlist * evlist __maybe_unused)347 static int write_osrelease(struct feat_fd *ff,
348 			   struct evlist *evlist __maybe_unused)
349 {
350 	struct utsname uts;
351 	int ret;
352 
353 	ret = uname(&uts);
354 	if (ret < 0)
355 		return -1;
356 
357 	return do_write_string(ff, uts.release);
358 }
359 
write_arch(struct feat_fd * ff,struct evlist * evlist __maybe_unused)360 static int write_arch(struct feat_fd *ff,
361 		      struct evlist *evlist __maybe_unused)
362 {
363 	struct utsname uts;
364 	int ret;
365 
366 	ret = uname(&uts);
367 	if (ret < 0)
368 		return -1;
369 
370 	return do_write_string(ff, uts.machine);
371 }
372 
write_version(struct feat_fd * ff,struct evlist * evlist __maybe_unused)373 static int write_version(struct feat_fd *ff,
374 			 struct evlist *evlist __maybe_unused)
375 {
376 	return do_write_string(ff, perf_version_string);
377 }
378 
__write_cpudesc(struct feat_fd * ff,const char * cpuinfo_proc)379 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
380 {
381 	FILE *file;
382 	char *buf = NULL;
383 	char *s, *p;
384 	const char *search = cpuinfo_proc;
385 	size_t len = 0;
386 	int ret = -1;
387 
388 	if (!search)
389 		return -1;
390 
391 	file = fopen("/proc/cpuinfo", "r");
392 	if (!file)
393 		return -1;
394 
395 	while (getline(&buf, &len, file) > 0) {
396 		ret = strncmp(buf, search, strlen(search));
397 		if (!ret)
398 			break;
399 	}
400 
401 	if (ret) {
402 		ret = -1;
403 		goto done;
404 	}
405 
406 	s = buf;
407 
408 	p = strchr(buf, ':');
409 	if (p && *(p+1) == ' ' && *(p+2))
410 		s = p + 2;
411 	p = strchr(s, '\n');
412 	if (p)
413 		*p = '\0';
414 
415 	/* squash extra space characters (branding string) */
416 	p = s;
417 	while (*p) {
418 		if (isspace(*p)) {
419 			char *r = p + 1;
420 			char *q = skip_spaces(r);
421 			*p = ' ';
422 			if (q != (p+1))
423 				while ((*r++ = *q++));
424 		}
425 		p++;
426 	}
427 	ret = do_write_string(ff, s);
428 done:
429 	free(buf);
430 	fclose(file);
431 	return ret;
432 }
433 
write_cpudesc(struct feat_fd * ff,struct evlist * evlist __maybe_unused)434 static int write_cpudesc(struct feat_fd *ff,
435 		       struct evlist *evlist __maybe_unused)
436 {
437 #if defined(__powerpc__) || defined(__hppa__) || defined(__sparc__)
438 #define CPUINFO_PROC	{ "cpu", }
439 #elif defined(__s390__)
440 #define CPUINFO_PROC	{ "vendor_id", }
441 #elif defined(__sh__)
442 #define CPUINFO_PROC	{ "cpu type", }
443 #elif defined(__alpha__) || defined(__mips__)
444 #define CPUINFO_PROC	{ "cpu model", }
445 #elif defined(__arm__)
446 #define CPUINFO_PROC	{ "model name", "Processor", }
447 #elif defined(__arc__)
448 #define CPUINFO_PROC	{ "Processor", }
449 #elif defined(__xtensa__)
450 #define CPUINFO_PROC	{ "core ID", }
451 #else
452 #define CPUINFO_PROC	{ "model name", }
453 #endif
454 	const char *cpuinfo_procs[] = CPUINFO_PROC;
455 #undef CPUINFO_PROC
456 	unsigned int i;
457 
458 	for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
459 		int ret;
460 		ret = __write_cpudesc(ff, cpuinfo_procs[i]);
461 		if (ret >= 0)
462 			return ret;
463 	}
464 	return -1;
465 }
466 
467 
write_nrcpus(struct feat_fd * ff,struct evlist * evlist __maybe_unused)468 static int write_nrcpus(struct feat_fd *ff,
469 			struct evlist *evlist __maybe_unused)
470 {
471 	long nr;
472 	u32 nrc, nra;
473 	int ret;
474 
475 	nrc = cpu__max_present_cpu().cpu;
476 
477 	nr = sysconf(_SC_NPROCESSORS_ONLN);
478 	if (nr < 0)
479 		return -1;
480 
481 	nra = (u32)(nr & UINT_MAX);
482 
483 	ret = do_write(ff, &nrc, sizeof(nrc));
484 	if (ret < 0)
485 		return ret;
486 
487 	return do_write(ff, &nra, sizeof(nra));
488 }
489 
write_event_desc(struct feat_fd * ff,struct evlist * evlist)490 static int write_event_desc(struct feat_fd *ff,
491 			    struct evlist *evlist)
492 {
493 	struct evsel *evsel;
494 	u32 nre, nri, sz;
495 	int ret;
496 
497 	nre = evlist->core.nr_entries;
498 
499 	/*
500 	 * write number of events
501 	 */
502 	ret = do_write(ff, &nre, sizeof(nre));
503 	if (ret < 0)
504 		return ret;
505 
506 	/*
507 	 * size of perf_event_attr struct
508 	 */
509 	sz = (u32)sizeof(evsel->core.attr);
510 	ret = do_write(ff, &sz, sizeof(sz));
511 	if (ret < 0)
512 		return ret;
513 
514 	evlist__for_each_entry(evlist, evsel) {
515 		ret = do_write(ff, &evsel->core.attr, sz);
516 		if (ret < 0)
517 			return ret;
518 		/*
519 		 * write number of unique id per event
520 		 * there is one id per instance of an event
521 		 *
522 		 * copy into an nri to be independent of the
523 		 * type of ids,
524 		 */
525 		nri = evsel->core.ids;
526 		ret = do_write(ff, &nri, sizeof(nri));
527 		if (ret < 0)
528 			return ret;
529 
530 		/*
531 		 * write event string as passed on cmdline
532 		 */
533 		ret = do_write_string(ff, evsel__name(evsel));
534 		if (ret < 0)
535 			return ret;
536 		/*
537 		 * write unique ids for this event
538 		 */
539 		ret = do_write(ff, evsel->core.id, evsel->core.ids * sizeof(u64));
540 		if (ret < 0)
541 			return ret;
542 	}
543 	return 0;
544 }
545 
write_cmdline(struct feat_fd * ff,struct evlist * evlist __maybe_unused)546 static int write_cmdline(struct feat_fd *ff,
547 			 struct evlist *evlist __maybe_unused)
548 {
549 	char pbuf[MAXPATHLEN], *buf;
550 	int i, ret, n;
551 
552 	/* actual path to perf binary */
553 	buf = perf_exe(pbuf, MAXPATHLEN);
554 
555 	/* account for binary path */
556 	n = perf_env.nr_cmdline + 1;
557 
558 	ret = do_write(ff, &n, sizeof(n));
559 	if (ret < 0)
560 		return ret;
561 
562 	ret = do_write_string(ff, buf);
563 	if (ret < 0)
564 		return ret;
565 
566 	for (i = 0 ; i < perf_env.nr_cmdline; i++) {
567 		ret = do_write_string(ff, perf_env.cmdline_argv[i]);
568 		if (ret < 0)
569 			return ret;
570 	}
571 	return 0;
572 }
573 
574 
write_cpu_topology(struct feat_fd * ff,struct evlist * evlist __maybe_unused)575 static int write_cpu_topology(struct feat_fd *ff,
576 			      struct evlist *evlist __maybe_unused)
577 {
578 	struct cpu_topology *tp;
579 	u32 i;
580 	int ret, j;
581 
582 	tp = cpu_topology__new();
583 	if (!tp)
584 		return -1;
585 
586 	ret = do_write(ff, &tp->package_cpus_lists, sizeof(tp->package_cpus_lists));
587 	if (ret < 0)
588 		goto done;
589 
590 	for (i = 0; i < tp->package_cpus_lists; i++) {
591 		ret = do_write_string(ff, tp->package_cpus_list[i]);
592 		if (ret < 0)
593 			goto done;
594 	}
595 	ret = do_write(ff, &tp->core_cpus_lists, sizeof(tp->core_cpus_lists));
596 	if (ret < 0)
597 		goto done;
598 
599 	for (i = 0; i < tp->core_cpus_lists; i++) {
600 		ret = do_write_string(ff, tp->core_cpus_list[i]);
601 		if (ret < 0)
602 			break;
603 	}
604 
605 	ret = perf_env__read_cpu_topology_map(&perf_env);
606 	if (ret < 0)
607 		goto done;
608 
609 	for (j = 0; j < perf_env.nr_cpus_avail; j++) {
610 		ret = do_write(ff, &perf_env.cpu[j].core_id,
611 			       sizeof(perf_env.cpu[j].core_id));
612 		if (ret < 0)
613 			return ret;
614 		ret = do_write(ff, &perf_env.cpu[j].socket_id,
615 			       sizeof(perf_env.cpu[j].socket_id));
616 		if (ret < 0)
617 			return ret;
618 	}
619 
620 	if (!tp->die_cpus_lists)
621 		goto done;
622 
623 	ret = do_write(ff, &tp->die_cpus_lists, sizeof(tp->die_cpus_lists));
624 	if (ret < 0)
625 		goto done;
626 
627 	for (i = 0; i < tp->die_cpus_lists; i++) {
628 		ret = do_write_string(ff, tp->die_cpus_list[i]);
629 		if (ret < 0)
630 			goto done;
631 	}
632 
633 	for (j = 0; j < perf_env.nr_cpus_avail; j++) {
634 		ret = do_write(ff, &perf_env.cpu[j].die_id,
635 			       sizeof(perf_env.cpu[j].die_id));
636 		if (ret < 0)
637 			return ret;
638 	}
639 
640 done:
641 	cpu_topology__delete(tp);
642 	return ret;
643 }
644 
645 
646 
write_total_mem(struct feat_fd * ff,struct evlist * evlist __maybe_unused)647 static int write_total_mem(struct feat_fd *ff,
648 			   struct evlist *evlist __maybe_unused)
649 {
650 	char *buf = NULL;
651 	FILE *fp;
652 	size_t len = 0;
653 	int ret = -1, n;
654 	uint64_t mem;
655 
656 	fp = fopen("/proc/meminfo", "r");
657 	if (!fp)
658 		return -1;
659 
660 	while (getline(&buf, &len, fp) > 0) {
661 		ret = strncmp(buf, "MemTotal:", 9);
662 		if (!ret)
663 			break;
664 	}
665 	if (!ret) {
666 		n = sscanf(buf, "%*s %"PRIu64, &mem);
667 		if (n == 1)
668 			ret = do_write(ff, &mem, sizeof(mem));
669 	} else
670 		ret = -1;
671 	free(buf);
672 	fclose(fp);
673 	return ret;
674 }
675 
write_numa_topology(struct feat_fd * ff,struct evlist * evlist __maybe_unused)676 static int write_numa_topology(struct feat_fd *ff,
677 			       struct evlist *evlist __maybe_unused)
678 {
679 	struct numa_topology *tp;
680 	int ret = -1;
681 	u32 i;
682 
683 	tp = numa_topology__new();
684 	if (!tp)
685 		return -ENOMEM;
686 
687 	ret = do_write(ff, &tp->nr, sizeof(u32));
688 	if (ret < 0)
689 		goto err;
690 
691 	for (i = 0; i < tp->nr; i++) {
692 		struct numa_topology_node *n = &tp->nodes[i];
693 
694 		ret = do_write(ff, &n->node, sizeof(u32));
695 		if (ret < 0)
696 			goto err;
697 
698 		ret = do_write(ff, &n->mem_total, sizeof(u64));
699 		if (ret)
700 			goto err;
701 
702 		ret = do_write(ff, &n->mem_free, sizeof(u64));
703 		if (ret)
704 			goto err;
705 
706 		ret = do_write_string(ff, n->cpus);
707 		if (ret < 0)
708 			goto err;
709 	}
710 
711 	ret = 0;
712 
713 err:
714 	numa_topology__delete(tp);
715 	return ret;
716 }
717 
718 /*
719  * File format:
720  *
721  * struct pmu_mappings {
722  *	u32	pmu_num;
723  *	struct pmu_map {
724  *		u32	type;
725  *		char	name[];
726  *	}[pmu_num];
727  * };
728  */
729 
write_pmu_mappings(struct feat_fd * ff,struct evlist * evlist __maybe_unused)730 static int write_pmu_mappings(struct feat_fd *ff,
731 			      struct evlist *evlist __maybe_unused)
732 {
733 	struct perf_pmu *pmu = NULL;
734 	u32 pmu_num = 0;
735 	int ret;
736 
737 	/*
738 	 * Do a first pass to count number of pmu to avoid lseek so this
739 	 * works in pipe mode as well.
740 	 */
741 	while ((pmu = perf_pmu__scan(pmu))) {
742 		if (!pmu->name)
743 			continue;
744 		pmu_num++;
745 	}
746 
747 	ret = do_write(ff, &pmu_num, sizeof(pmu_num));
748 	if (ret < 0)
749 		return ret;
750 
751 	while ((pmu = perf_pmu__scan(pmu))) {
752 		if (!pmu->name)
753 			continue;
754 
755 		ret = do_write(ff, &pmu->type, sizeof(pmu->type));
756 		if (ret < 0)
757 			return ret;
758 
759 		ret = do_write_string(ff, pmu->name);
760 		if (ret < 0)
761 			return ret;
762 	}
763 
764 	return 0;
765 }
766 
767 /*
768  * File format:
769  *
770  * struct group_descs {
771  *	u32	nr_groups;
772  *	struct group_desc {
773  *		char	name[];
774  *		u32	leader_idx;
775  *		u32	nr_members;
776  *	}[nr_groups];
777  * };
778  */
write_group_desc(struct feat_fd * ff,struct evlist * evlist)779 static int write_group_desc(struct feat_fd *ff,
780 			    struct evlist *evlist)
781 {
782 	u32 nr_groups = evlist->core.nr_groups;
783 	struct evsel *evsel;
784 	int ret;
785 
786 	ret = do_write(ff, &nr_groups, sizeof(nr_groups));
787 	if (ret < 0)
788 		return ret;
789 
790 	evlist__for_each_entry(evlist, evsel) {
791 		if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) {
792 			const char *name = evsel->group_name ?: "{anon_group}";
793 			u32 leader_idx = evsel->core.idx;
794 			u32 nr_members = evsel->core.nr_members;
795 
796 			ret = do_write_string(ff, name);
797 			if (ret < 0)
798 				return ret;
799 
800 			ret = do_write(ff, &leader_idx, sizeof(leader_idx));
801 			if (ret < 0)
802 				return ret;
803 
804 			ret = do_write(ff, &nr_members, sizeof(nr_members));
805 			if (ret < 0)
806 				return ret;
807 		}
808 	}
809 	return 0;
810 }
811 
812 /*
813  * Return the CPU id as a raw string.
814  *
815  * Each architecture should provide a more precise id string that
816  * can be use to match the architecture's "mapfile".
817  */
get_cpuid_str(struct perf_pmu * pmu __maybe_unused)818 char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
819 {
820 	return NULL;
821 }
822 
823 /* Return zero when the cpuid from the mapfile.csv matches the
824  * cpuid string generated on this platform.
825  * Otherwise return non-zero.
826  */
strcmp_cpuid_str(const char * mapcpuid,const char * cpuid)827 int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
828 {
829 	regex_t re;
830 	regmatch_t pmatch[1];
831 	int match;
832 
833 	if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
834 		/* Warn unable to generate match particular string. */
835 		pr_info("Invalid regular expression %s\n", mapcpuid);
836 		return 1;
837 	}
838 
839 	match = !regexec(&re, cpuid, 1, pmatch, 0);
840 	regfree(&re);
841 	if (match) {
842 		size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
843 
844 		/* Verify the entire string matched. */
845 		if (match_len == strlen(cpuid))
846 			return 0;
847 	}
848 	return 1;
849 }
850 
851 /*
852  * default get_cpuid(): nothing gets recorded
853  * actual implementation must be in arch/$(SRCARCH)/util/header.c
854  */
get_cpuid(char * buffer __maybe_unused,size_t sz __maybe_unused)855 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
856 {
857 	return ENOSYS; /* Not implemented */
858 }
859 
write_cpuid(struct feat_fd * ff,struct evlist * evlist __maybe_unused)860 static int write_cpuid(struct feat_fd *ff,
861 		       struct evlist *evlist __maybe_unused)
862 {
863 	char buffer[64];
864 	int ret;
865 
866 	ret = get_cpuid(buffer, sizeof(buffer));
867 	if (ret)
868 		return -1;
869 
870 	return do_write_string(ff, buffer);
871 }
872 
write_branch_stack(struct feat_fd * ff __maybe_unused,struct evlist * evlist __maybe_unused)873 static int write_branch_stack(struct feat_fd *ff __maybe_unused,
874 			      struct evlist *evlist __maybe_unused)
875 {
876 	return 0;
877 }
878 
write_auxtrace(struct feat_fd * ff,struct evlist * evlist __maybe_unused)879 static int write_auxtrace(struct feat_fd *ff,
880 			  struct evlist *evlist __maybe_unused)
881 {
882 	struct perf_session *session;
883 	int err;
884 
885 	if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
886 		return -1;
887 
888 	session = container_of(ff->ph, struct perf_session, header);
889 
890 	err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
891 	if (err < 0)
892 		pr_err("Failed to write auxtrace index\n");
893 	return err;
894 }
895 
write_clockid(struct feat_fd * ff,struct evlist * evlist __maybe_unused)896 static int write_clockid(struct feat_fd *ff,
897 			 struct evlist *evlist __maybe_unused)
898 {
899 	return do_write(ff, &ff->ph->env.clock.clockid_res_ns,
900 			sizeof(ff->ph->env.clock.clockid_res_ns));
901 }
902 
write_clock_data(struct feat_fd * ff,struct evlist * evlist __maybe_unused)903 static int write_clock_data(struct feat_fd *ff,
904 			    struct evlist *evlist __maybe_unused)
905 {
906 	u64 *data64;
907 	u32 data32;
908 	int ret;
909 
910 	/* version */
911 	data32 = 1;
912 
913 	ret = do_write(ff, &data32, sizeof(data32));
914 	if (ret < 0)
915 		return ret;
916 
917 	/* clockid */
918 	data32 = ff->ph->env.clock.clockid;
919 
920 	ret = do_write(ff, &data32, sizeof(data32));
921 	if (ret < 0)
922 		return ret;
923 
924 	/* TOD ref time */
925 	data64 = &ff->ph->env.clock.tod_ns;
926 
927 	ret = do_write(ff, data64, sizeof(*data64));
928 	if (ret < 0)
929 		return ret;
930 
931 	/* clockid ref time */
932 	data64 = &ff->ph->env.clock.clockid_ns;
933 
934 	return do_write(ff, data64, sizeof(*data64));
935 }
936 
write_hybrid_topology(struct feat_fd * ff,struct evlist * evlist __maybe_unused)937 static int write_hybrid_topology(struct feat_fd *ff,
938 				 struct evlist *evlist __maybe_unused)
939 {
940 	struct hybrid_topology *tp;
941 	int ret;
942 	u32 i;
943 
944 	tp = hybrid_topology__new();
945 	if (!tp)
946 		return -ENOENT;
947 
948 	ret = do_write(ff, &tp->nr, sizeof(u32));
949 	if (ret < 0)
950 		goto err;
951 
952 	for (i = 0; i < tp->nr; i++) {
953 		struct hybrid_topology_node *n = &tp->nodes[i];
954 
955 		ret = do_write_string(ff, n->pmu_name);
956 		if (ret < 0)
957 			goto err;
958 
959 		ret = do_write_string(ff, n->cpus);
960 		if (ret < 0)
961 			goto err;
962 	}
963 
964 	ret = 0;
965 
966 err:
967 	hybrid_topology__delete(tp);
968 	return ret;
969 }
970 
write_dir_format(struct feat_fd * ff,struct evlist * evlist __maybe_unused)971 static int write_dir_format(struct feat_fd *ff,
972 			    struct evlist *evlist __maybe_unused)
973 {
974 	struct perf_session *session;
975 	struct perf_data *data;
976 
977 	session = container_of(ff->ph, struct perf_session, header);
978 	data = session->data;
979 
980 	if (WARN_ON(!perf_data__is_dir(data)))
981 		return -1;
982 
983 	return do_write(ff, &data->dir.version, sizeof(data->dir.version));
984 }
985 
986 /*
987  * Check whether a CPU is online
988  *
989  * Returns:
990  *     1 -> if CPU is online
991  *     0 -> if CPU is offline
992  *    -1 -> error case
993  */
is_cpu_online(unsigned int cpu)994 int is_cpu_online(unsigned int cpu)
995 {
996 	char *str;
997 	size_t strlen;
998 	char buf[256];
999 	int status = -1;
1000 	struct stat statbuf;
1001 
1002 	snprintf(buf, sizeof(buf),
1003 		"/sys/devices/system/cpu/cpu%d", cpu);
1004 	if (stat(buf, &statbuf) != 0)
1005 		return 0;
1006 
1007 	/*
1008 	 * Check if /sys/devices/system/cpu/cpux/online file
1009 	 * exists. Some cases cpu0 won't have online file since
1010 	 * it is not expected to be turned off generally.
1011 	 * In kernels without CONFIG_HOTPLUG_CPU, this
1012 	 * file won't exist
1013 	 */
1014 	snprintf(buf, sizeof(buf),
1015 		"/sys/devices/system/cpu/cpu%d/online", cpu);
1016 	if (stat(buf, &statbuf) != 0)
1017 		return 1;
1018 
1019 	/*
1020 	 * Read online file using sysfs__read_str.
1021 	 * If read or open fails, return -1.
1022 	 * If read succeeds, return value from file
1023 	 * which gets stored in "str"
1024 	 */
1025 	snprintf(buf, sizeof(buf),
1026 		"devices/system/cpu/cpu%d/online", cpu);
1027 
1028 	if (sysfs__read_str(buf, &str, &strlen) < 0)
1029 		return status;
1030 
1031 	status = atoi(str);
1032 
1033 	free(str);
1034 	return status;
1035 }
1036 
1037 #ifdef HAVE_LIBBPF_SUPPORT
write_bpf_prog_info(struct feat_fd * ff,struct evlist * evlist __maybe_unused)1038 static int write_bpf_prog_info(struct feat_fd *ff,
1039 			       struct evlist *evlist __maybe_unused)
1040 {
1041 	struct perf_env *env = &ff->ph->env;
1042 	struct rb_root *root;
1043 	struct rb_node *next;
1044 	int ret;
1045 
1046 	down_read(&env->bpf_progs.lock);
1047 
1048 	ret = do_write(ff, &env->bpf_progs.infos_cnt,
1049 		       sizeof(env->bpf_progs.infos_cnt));
1050 	if (ret < 0)
1051 		goto out;
1052 
1053 	root = &env->bpf_progs.infos;
1054 	next = rb_first(root);
1055 	while (next) {
1056 		struct bpf_prog_info_node *node;
1057 		size_t len;
1058 
1059 		node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1060 		next = rb_next(&node->rb_node);
1061 		len = sizeof(struct perf_bpil) +
1062 			node->info_linear->data_len;
1063 
1064 		/* before writing to file, translate address to offset */
1065 		bpil_addr_to_offs(node->info_linear);
1066 		ret = do_write(ff, node->info_linear, len);
1067 		/*
1068 		 * translate back to address even when do_write() fails,
1069 		 * so that this function never changes the data.
1070 		 */
1071 		bpil_offs_to_addr(node->info_linear);
1072 		if (ret < 0)
1073 			goto out;
1074 	}
1075 out:
1076 	up_read(&env->bpf_progs.lock);
1077 	return ret;
1078 }
1079 
write_bpf_btf(struct feat_fd * ff,struct evlist * evlist __maybe_unused)1080 static int write_bpf_btf(struct feat_fd *ff,
1081 			 struct evlist *evlist __maybe_unused)
1082 {
1083 	struct perf_env *env = &ff->ph->env;
1084 	struct rb_root *root;
1085 	struct rb_node *next;
1086 	int ret;
1087 
1088 	down_read(&env->bpf_progs.lock);
1089 
1090 	ret = do_write(ff, &env->bpf_progs.btfs_cnt,
1091 		       sizeof(env->bpf_progs.btfs_cnt));
1092 
1093 	if (ret < 0)
1094 		goto out;
1095 
1096 	root = &env->bpf_progs.btfs;
1097 	next = rb_first(root);
1098 	while (next) {
1099 		struct btf_node *node;
1100 
1101 		node = rb_entry(next, struct btf_node, rb_node);
1102 		next = rb_next(&node->rb_node);
1103 		ret = do_write(ff, &node->id,
1104 			       sizeof(u32) * 2 + node->data_size);
1105 		if (ret < 0)
1106 			goto out;
1107 	}
1108 out:
1109 	up_read(&env->bpf_progs.lock);
1110 	return ret;
1111 }
1112 #endif // HAVE_LIBBPF_SUPPORT
1113 
cpu_cache_level__sort(const void * a,const void * b)1114 static int cpu_cache_level__sort(const void *a, const void *b)
1115 {
1116 	struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
1117 	struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
1118 
1119 	return cache_a->level - cache_b->level;
1120 }
1121 
cpu_cache_level__cmp(struct cpu_cache_level * a,struct cpu_cache_level * b)1122 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
1123 {
1124 	if (a->level != b->level)
1125 		return false;
1126 
1127 	if (a->line_size != b->line_size)
1128 		return false;
1129 
1130 	if (a->sets != b->sets)
1131 		return false;
1132 
1133 	if (a->ways != b->ways)
1134 		return false;
1135 
1136 	if (strcmp(a->type, b->type))
1137 		return false;
1138 
1139 	if (strcmp(a->size, b->size))
1140 		return false;
1141 
1142 	if (strcmp(a->map, b->map))
1143 		return false;
1144 
1145 	return true;
1146 }
1147 
cpu_cache_level__read(struct cpu_cache_level * cache,u32 cpu,u16 level)1148 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1149 {
1150 	char path[PATH_MAX], file[PATH_MAX];
1151 	struct stat st;
1152 	size_t len;
1153 
1154 	scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1155 	scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1156 
1157 	if (stat(file, &st))
1158 		return 1;
1159 
1160 	scnprintf(file, PATH_MAX, "%s/level", path);
1161 	if (sysfs__read_int(file, (int *) &cache->level))
1162 		return -1;
1163 
1164 	scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1165 	if (sysfs__read_int(file, (int *) &cache->line_size))
1166 		return -1;
1167 
1168 	scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1169 	if (sysfs__read_int(file, (int *) &cache->sets))
1170 		return -1;
1171 
1172 	scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1173 	if (sysfs__read_int(file, (int *) &cache->ways))
1174 		return -1;
1175 
1176 	scnprintf(file, PATH_MAX, "%s/type", path);
1177 	if (sysfs__read_str(file, &cache->type, &len))
1178 		return -1;
1179 
1180 	cache->type[len] = 0;
1181 	cache->type = strim(cache->type);
1182 
1183 	scnprintf(file, PATH_MAX, "%s/size", path);
1184 	if (sysfs__read_str(file, &cache->size, &len)) {
1185 		zfree(&cache->type);
1186 		return -1;
1187 	}
1188 
1189 	cache->size[len] = 0;
1190 	cache->size = strim(cache->size);
1191 
1192 	scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1193 	if (sysfs__read_str(file, &cache->map, &len)) {
1194 		zfree(&cache->size);
1195 		zfree(&cache->type);
1196 		return -1;
1197 	}
1198 
1199 	cache->map[len] = 0;
1200 	cache->map = strim(cache->map);
1201 	return 0;
1202 }
1203 
cpu_cache_level__fprintf(FILE * out,struct cpu_cache_level * c)1204 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1205 {
1206 	fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1207 }
1208 
1209 #define MAX_CACHE_LVL 4
1210 
build_caches(struct cpu_cache_level caches[],u32 * cntp)1211 static int build_caches(struct cpu_cache_level caches[], u32 *cntp)
1212 {
1213 	u32 i, cnt = 0;
1214 	u32 nr, cpu;
1215 	u16 level;
1216 
1217 	nr = cpu__max_cpu().cpu;
1218 
1219 	for (cpu = 0; cpu < nr; cpu++) {
1220 		for (level = 0; level < MAX_CACHE_LVL; level++) {
1221 			struct cpu_cache_level c;
1222 			int err;
1223 
1224 			err = cpu_cache_level__read(&c, cpu, level);
1225 			if (err < 0)
1226 				return err;
1227 
1228 			if (err == 1)
1229 				break;
1230 
1231 			for (i = 0; i < cnt; i++) {
1232 				if (cpu_cache_level__cmp(&c, &caches[i]))
1233 					break;
1234 			}
1235 
1236 			if (i == cnt)
1237 				caches[cnt++] = c;
1238 			else
1239 				cpu_cache_level__free(&c);
1240 		}
1241 	}
1242 	*cntp = cnt;
1243 	return 0;
1244 }
1245 
write_cache(struct feat_fd * ff,struct evlist * evlist __maybe_unused)1246 static int write_cache(struct feat_fd *ff,
1247 		       struct evlist *evlist __maybe_unused)
1248 {
1249 	u32 max_caches = cpu__max_cpu().cpu * MAX_CACHE_LVL;
1250 	struct cpu_cache_level caches[max_caches];
1251 	u32 cnt = 0, i, version = 1;
1252 	int ret;
1253 
1254 	ret = build_caches(caches, &cnt);
1255 	if (ret)
1256 		goto out;
1257 
1258 	qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1259 
1260 	ret = do_write(ff, &version, sizeof(u32));
1261 	if (ret < 0)
1262 		goto out;
1263 
1264 	ret = do_write(ff, &cnt, sizeof(u32));
1265 	if (ret < 0)
1266 		goto out;
1267 
1268 	for (i = 0; i < cnt; i++) {
1269 		struct cpu_cache_level *c = &caches[i];
1270 
1271 		#define _W(v)					\
1272 			ret = do_write(ff, &c->v, sizeof(u32));	\
1273 			if (ret < 0)				\
1274 				goto out;
1275 
1276 		_W(level)
1277 		_W(line_size)
1278 		_W(sets)
1279 		_W(ways)
1280 		#undef _W
1281 
1282 		#define _W(v)						\
1283 			ret = do_write_string(ff, (const char *) c->v);	\
1284 			if (ret < 0)					\
1285 				goto out;
1286 
1287 		_W(type)
1288 		_W(size)
1289 		_W(map)
1290 		#undef _W
1291 	}
1292 
1293 out:
1294 	for (i = 0; i < cnt; i++)
1295 		cpu_cache_level__free(&caches[i]);
1296 	return ret;
1297 }
1298 
write_stat(struct feat_fd * ff __maybe_unused,struct evlist * evlist __maybe_unused)1299 static int write_stat(struct feat_fd *ff __maybe_unused,
1300 		      struct evlist *evlist __maybe_unused)
1301 {
1302 	return 0;
1303 }
1304 
write_sample_time(struct feat_fd * ff,struct evlist * evlist)1305 static int write_sample_time(struct feat_fd *ff,
1306 			     struct evlist *evlist)
1307 {
1308 	int ret;
1309 
1310 	ret = do_write(ff, &evlist->first_sample_time,
1311 		       sizeof(evlist->first_sample_time));
1312 	if (ret < 0)
1313 		return ret;
1314 
1315 	return do_write(ff, &evlist->last_sample_time,
1316 			sizeof(evlist->last_sample_time));
1317 }
1318 
1319 
memory_node__read(struct memory_node * n,unsigned long idx)1320 static int memory_node__read(struct memory_node *n, unsigned long idx)
1321 {
1322 	unsigned int phys, size = 0;
1323 	char path[PATH_MAX];
1324 	struct dirent *ent;
1325 	DIR *dir;
1326 
1327 #define for_each_memory(mem, dir)					\
1328 	while ((ent = readdir(dir)))					\
1329 		if (strcmp(ent->d_name, ".") &&				\
1330 		    strcmp(ent->d_name, "..") &&			\
1331 		    sscanf(ent->d_name, "memory%u", &mem) == 1)
1332 
1333 	scnprintf(path, PATH_MAX,
1334 		  "%s/devices/system/node/node%lu",
1335 		  sysfs__mountpoint(), idx);
1336 
1337 	dir = opendir(path);
1338 	if (!dir) {
1339 		pr_warning("failed: can't open memory sysfs data\n");
1340 		return -1;
1341 	}
1342 
1343 	for_each_memory(phys, dir) {
1344 		size = max(phys, size);
1345 	}
1346 
1347 	size++;
1348 
1349 	n->set = bitmap_zalloc(size);
1350 	if (!n->set) {
1351 		closedir(dir);
1352 		return -ENOMEM;
1353 	}
1354 
1355 	n->node = idx;
1356 	n->size = size;
1357 
1358 	rewinddir(dir);
1359 
1360 	for_each_memory(phys, dir) {
1361 		set_bit(phys, n->set);
1362 	}
1363 
1364 	closedir(dir);
1365 	return 0;
1366 }
1367 
memory_node__sort(const void * a,const void * b)1368 static int memory_node__sort(const void *a, const void *b)
1369 {
1370 	const struct memory_node *na = a;
1371 	const struct memory_node *nb = b;
1372 
1373 	return na->node - nb->node;
1374 }
1375 
build_mem_topology(struct memory_node * nodes,u64 size,u64 * cntp)1376 static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
1377 {
1378 	char path[PATH_MAX];
1379 	struct dirent *ent;
1380 	DIR *dir;
1381 	u64 cnt = 0;
1382 	int ret = 0;
1383 
1384 	scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1385 		  sysfs__mountpoint());
1386 
1387 	dir = opendir(path);
1388 	if (!dir) {
1389 		pr_debug2("%s: couldn't read %s, does this arch have topology information?\n",
1390 			  __func__, path);
1391 		return -1;
1392 	}
1393 
1394 	while (!ret && (ent = readdir(dir))) {
1395 		unsigned int idx;
1396 		int r;
1397 
1398 		if (!strcmp(ent->d_name, ".") ||
1399 		    !strcmp(ent->d_name, ".."))
1400 			continue;
1401 
1402 		r = sscanf(ent->d_name, "node%u", &idx);
1403 		if (r != 1)
1404 			continue;
1405 
1406 		if (WARN_ONCE(cnt >= size,
1407 			"failed to write MEM_TOPOLOGY, way too many nodes\n")) {
1408 			closedir(dir);
1409 			return -1;
1410 		}
1411 
1412 		ret = memory_node__read(&nodes[cnt++], idx);
1413 	}
1414 
1415 	*cntp = cnt;
1416 	closedir(dir);
1417 
1418 	if (!ret)
1419 		qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1420 
1421 	return ret;
1422 }
1423 
1424 #define MAX_MEMORY_NODES 2000
1425 
1426 /*
1427  * The MEM_TOPOLOGY holds physical memory map for every
1428  * node in system. The format of data is as follows:
1429  *
1430  *  0 - version          | for future changes
1431  *  8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1432  * 16 - count            | number of nodes
1433  *
1434  * For each node we store map of physical indexes for
1435  * each node:
1436  *
1437  * 32 - node id          | node index
1438  * 40 - size             | size of bitmap
1439  * 48 - bitmap           | bitmap of memory indexes that belongs to node
1440  */
write_mem_topology(struct feat_fd * ff __maybe_unused,struct evlist * evlist __maybe_unused)1441 static int write_mem_topology(struct feat_fd *ff __maybe_unused,
1442 			      struct evlist *evlist __maybe_unused)
1443 {
1444 	static struct memory_node nodes[MAX_MEMORY_NODES];
1445 	u64 bsize, version = 1, i, nr;
1446 	int ret;
1447 
1448 	ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1449 			      (unsigned long long *) &bsize);
1450 	if (ret)
1451 		return ret;
1452 
1453 	ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
1454 	if (ret)
1455 		return ret;
1456 
1457 	ret = do_write(ff, &version, sizeof(version));
1458 	if (ret < 0)
1459 		goto out;
1460 
1461 	ret = do_write(ff, &bsize, sizeof(bsize));
1462 	if (ret < 0)
1463 		goto out;
1464 
1465 	ret = do_write(ff, &nr, sizeof(nr));
1466 	if (ret < 0)
1467 		goto out;
1468 
1469 	for (i = 0; i < nr; i++) {
1470 		struct memory_node *n = &nodes[i];
1471 
1472 		#define _W(v)						\
1473 			ret = do_write(ff, &n->v, sizeof(n->v));	\
1474 			if (ret < 0)					\
1475 				goto out;
1476 
1477 		_W(node)
1478 		_W(size)
1479 
1480 		#undef _W
1481 
1482 		ret = do_write_bitmap(ff, n->set, n->size);
1483 		if (ret < 0)
1484 			goto out;
1485 	}
1486 
1487 out:
1488 	return ret;
1489 }
1490 
write_compressed(struct feat_fd * ff __maybe_unused,struct evlist * evlist __maybe_unused)1491 static int write_compressed(struct feat_fd *ff __maybe_unused,
1492 			    struct evlist *evlist __maybe_unused)
1493 {
1494 	int ret;
1495 
1496 	ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver));
1497 	if (ret)
1498 		return ret;
1499 
1500 	ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type));
1501 	if (ret)
1502 		return ret;
1503 
1504 	ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level));
1505 	if (ret)
1506 		return ret;
1507 
1508 	ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio));
1509 	if (ret)
1510 		return ret;
1511 
1512 	return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len));
1513 }
1514 
__write_pmu_caps(struct feat_fd * ff,struct perf_pmu * pmu,bool write_pmu)1515 static int __write_pmu_caps(struct feat_fd *ff, struct perf_pmu *pmu,
1516 			    bool write_pmu)
1517 {
1518 	struct perf_pmu_caps *caps = NULL;
1519 	int ret;
1520 
1521 	ret = do_write(ff, &pmu->nr_caps, sizeof(pmu->nr_caps));
1522 	if (ret < 0)
1523 		return ret;
1524 
1525 	list_for_each_entry(caps, &pmu->caps, list) {
1526 		ret = do_write_string(ff, caps->name);
1527 		if (ret < 0)
1528 			return ret;
1529 
1530 		ret = do_write_string(ff, caps->value);
1531 		if (ret < 0)
1532 			return ret;
1533 	}
1534 
1535 	if (write_pmu) {
1536 		ret = do_write_string(ff, pmu->name);
1537 		if (ret < 0)
1538 			return ret;
1539 	}
1540 
1541 	return ret;
1542 }
1543 
write_cpu_pmu_caps(struct feat_fd * ff,struct evlist * evlist __maybe_unused)1544 static int write_cpu_pmu_caps(struct feat_fd *ff,
1545 			      struct evlist *evlist __maybe_unused)
1546 {
1547 	struct perf_pmu *cpu_pmu = perf_pmu__find("cpu");
1548 	int ret;
1549 
1550 	if (!cpu_pmu)
1551 		return -ENOENT;
1552 
1553 	ret = perf_pmu__caps_parse(cpu_pmu);
1554 	if (ret < 0)
1555 		return ret;
1556 
1557 	return __write_pmu_caps(ff, cpu_pmu, false);
1558 }
1559 
write_pmu_caps(struct feat_fd * ff,struct evlist * evlist __maybe_unused)1560 static int write_pmu_caps(struct feat_fd *ff,
1561 			  struct evlist *evlist __maybe_unused)
1562 {
1563 	struct perf_pmu *pmu = NULL;
1564 	int nr_pmu = 0;
1565 	int ret;
1566 
1567 	while ((pmu = perf_pmu__scan(pmu))) {
1568 		if (!pmu->name || !strcmp(pmu->name, "cpu") ||
1569 		    perf_pmu__caps_parse(pmu) <= 0)
1570 			continue;
1571 		nr_pmu++;
1572 	}
1573 
1574 	ret = do_write(ff, &nr_pmu, sizeof(nr_pmu));
1575 	if (ret < 0)
1576 		return ret;
1577 
1578 	if (!nr_pmu)
1579 		return 0;
1580 
1581 	/*
1582 	 * Write hybrid pmu caps first to maintain compatibility with
1583 	 * older perf tool.
1584 	 */
1585 	pmu = NULL;
1586 	perf_pmu__for_each_hybrid_pmu(pmu) {
1587 		ret = __write_pmu_caps(ff, pmu, true);
1588 		if (ret < 0)
1589 			return ret;
1590 	}
1591 
1592 	pmu = NULL;
1593 	while ((pmu = perf_pmu__scan(pmu))) {
1594 		if (!pmu->name || !strcmp(pmu->name, "cpu") ||
1595 		    !pmu->nr_caps || perf_pmu__is_hybrid(pmu->name))
1596 			continue;
1597 
1598 		ret = __write_pmu_caps(ff, pmu, true);
1599 		if (ret < 0)
1600 			return ret;
1601 	}
1602 	return 0;
1603 }
1604 
print_hostname(struct feat_fd * ff,FILE * fp)1605 static void print_hostname(struct feat_fd *ff, FILE *fp)
1606 {
1607 	fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
1608 }
1609 
print_osrelease(struct feat_fd * ff,FILE * fp)1610 static void print_osrelease(struct feat_fd *ff, FILE *fp)
1611 {
1612 	fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
1613 }
1614 
print_arch(struct feat_fd * ff,FILE * fp)1615 static void print_arch(struct feat_fd *ff, FILE *fp)
1616 {
1617 	fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
1618 }
1619 
print_cpudesc(struct feat_fd * ff,FILE * fp)1620 static void print_cpudesc(struct feat_fd *ff, FILE *fp)
1621 {
1622 	fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
1623 }
1624 
print_nrcpus(struct feat_fd * ff,FILE * fp)1625 static void print_nrcpus(struct feat_fd *ff, FILE *fp)
1626 {
1627 	fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1628 	fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
1629 }
1630 
print_version(struct feat_fd * ff,FILE * fp)1631 static void print_version(struct feat_fd *ff, FILE *fp)
1632 {
1633 	fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
1634 }
1635 
print_cmdline(struct feat_fd * ff,FILE * fp)1636 static void print_cmdline(struct feat_fd *ff, FILE *fp)
1637 {
1638 	int nr, i;
1639 
1640 	nr = ff->ph->env.nr_cmdline;
1641 
1642 	fprintf(fp, "# cmdline : ");
1643 
1644 	for (i = 0; i < nr; i++) {
1645 		char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
1646 		if (!argv_i) {
1647 			fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1648 		} else {
1649 			char *mem = argv_i;
1650 			do {
1651 				char *quote = strchr(argv_i, '\'');
1652 				if (!quote)
1653 					break;
1654 				*quote++ = '\0';
1655 				fprintf(fp, "%s\\\'", argv_i);
1656 				argv_i = quote;
1657 			} while (1);
1658 			fprintf(fp, "%s ", argv_i);
1659 			free(mem);
1660 		}
1661 	}
1662 	fputc('\n', fp);
1663 }
1664 
print_cpu_topology(struct feat_fd * ff,FILE * fp)1665 static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
1666 {
1667 	struct perf_header *ph = ff->ph;
1668 	int cpu_nr = ph->env.nr_cpus_avail;
1669 	int nr, i;
1670 	char *str;
1671 
1672 	nr = ph->env.nr_sibling_cores;
1673 	str = ph->env.sibling_cores;
1674 
1675 	for (i = 0; i < nr; i++) {
1676 		fprintf(fp, "# sibling sockets : %s\n", str);
1677 		str += strlen(str) + 1;
1678 	}
1679 
1680 	if (ph->env.nr_sibling_dies) {
1681 		nr = ph->env.nr_sibling_dies;
1682 		str = ph->env.sibling_dies;
1683 
1684 		for (i = 0; i < nr; i++) {
1685 			fprintf(fp, "# sibling dies    : %s\n", str);
1686 			str += strlen(str) + 1;
1687 		}
1688 	}
1689 
1690 	nr = ph->env.nr_sibling_threads;
1691 	str = ph->env.sibling_threads;
1692 
1693 	for (i = 0; i < nr; i++) {
1694 		fprintf(fp, "# sibling threads : %s\n", str);
1695 		str += strlen(str) + 1;
1696 	}
1697 
1698 	if (ph->env.nr_sibling_dies) {
1699 		if (ph->env.cpu != NULL) {
1700 			for (i = 0; i < cpu_nr; i++)
1701 				fprintf(fp, "# CPU %d: Core ID %d, "
1702 					    "Die ID %d, Socket ID %d\n",
1703 					    i, ph->env.cpu[i].core_id,
1704 					    ph->env.cpu[i].die_id,
1705 					    ph->env.cpu[i].socket_id);
1706 		} else
1707 			fprintf(fp, "# Core ID, Die ID and Socket ID "
1708 				    "information is not available\n");
1709 	} else {
1710 		if (ph->env.cpu != NULL) {
1711 			for (i = 0; i < cpu_nr; i++)
1712 				fprintf(fp, "# CPU %d: Core ID %d, "
1713 					    "Socket ID %d\n",
1714 					    i, ph->env.cpu[i].core_id,
1715 					    ph->env.cpu[i].socket_id);
1716 		} else
1717 			fprintf(fp, "# Core ID and Socket ID "
1718 				    "information is not available\n");
1719 	}
1720 }
1721 
print_clockid(struct feat_fd * ff,FILE * fp)1722 static void print_clockid(struct feat_fd *ff, FILE *fp)
1723 {
1724 	fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n",
1725 		ff->ph->env.clock.clockid_res_ns * 1000);
1726 }
1727 
print_clock_data(struct feat_fd * ff,FILE * fp)1728 static void print_clock_data(struct feat_fd *ff, FILE *fp)
1729 {
1730 	struct timespec clockid_ns;
1731 	char tstr[64], date[64];
1732 	struct timeval tod_ns;
1733 	clockid_t clockid;
1734 	struct tm ltime;
1735 	u64 ref;
1736 
1737 	if (!ff->ph->env.clock.enabled) {
1738 		fprintf(fp, "# reference time disabled\n");
1739 		return;
1740 	}
1741 
1742 	/* Compute TOD time. */
1743 	ref = ff->ph->env.clock.tod_ns;
1744 	tod_ns.tv_sec = ref / NSEC_PER_SEC;
1745 	ref -= tod_ns.tv_sec * NSEC_PER_SEC;
1746 	tod_ns.tv_usec = ref / NSEC_PER_USEC;
1747 
1748 	/* Compute clockid time. */
1749 	ref = ff->ph->env.clock.clockid_ns;
1750 	clockid_ns.tv_sec = ref / NSEC_PER_SEC;
1751 	ref -= clockid_ns.tv_sec * NSEC_PER_SEC;
1752 	clockid_ns.tv_nsec = ref;
1753 
1754 	clockid = ff->ph->env.clock.clockid;
1755 
1756 	if (localtime_r(&tod_ns.tv_sec, &ltime) == NULL)
1757 		snprintf(tstr, sizeof(tstr), "<error>");
1758 	else {
1759 		strftime(date, sizeof(date), "%F %T", &ltime);
1760 		scnprintf(tstr, sizeof(tstr), "%s.%06d",
1761 			  date, (int) tod_ns.tv_usec);
1762 	}
1763 
1764 	fprintf(fp, "# clockid: %s (%u)\n", clockid_name(clockid), clockid);
1765 	fprintf(fp, "# reference time: %s = %ld.%06d (TOD) = %ld.%09ld (%s)\n",
1766 		    tstr, (long) tod_ns.tv_sec, (int) tod_ns.tv_usec,
1767 		    (long) clockid_ns.tv_sec, clockid_ns.tv_nsec,
1768 		    clockid_name(clockid));
1769 }
1770 
print_hybrid_topology(struct feat_fd * ff,FILE * fp)1771 static void print_hybrid_topology(struct feat_fd *ff, FILE *fp)
1772 {
1773 	int i;
1774 	struct hybrid_node *n;
1775 
1776 	fprintf(fp, "# hybrid cpu system:\n");
1777 	for (i = 0; i < ff->ph->env.nr_hybrid_nodes; i++) {
1778 		n = &ff->ph->env.hybrid_nodes[i];
1779 		fprintf(fp, "# %s cpu list : %s\n", n->pmu_name, n->cpus);
1780 	}
1781 }
1782 
print_dir_format(struct feat_fd * ff,FILE * fp)1783 static void print_dir_format(struct feat_fd *ff, FILE *fp)
1784 {
1785 	struct perf_session *session;
1786 	struct perf_data *data;
1787 
1788 	session = container_of(ff->ph, struct perf_session, header);
1789 	data = session->data;
1790 
1791 	fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version);
1792 }
1793 
1794 #ifdef HAVE_LIBBPF_SUPPORT
print_bpf_prog_info(struct feat_fd * ff,FILE * fp)1795 static void print_bpf_prog_info(struct feat_fd *ff, FILE *fp)
1796 {
1797 	struct perf_env *env = &ff->ph->env;
1798 	struct rb_root *root;
1799 	struct rb_node *next;
1800 
1801 	down_read(&env->bpf_progs.lock);
1802 
1803 	root = &env->bpf_progs.infos;
1804 	next = rb_first(root);
1805 
1806 	while (next) {
1807 		struct bpf_prog_info_node *node;
1808 
1809 		node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1810 		next = rb_next(&node->rb_node);
1811 
1812 		bpf_event__print_bpf_prog_info(&node->info_linear->info,
1813 					       env, fp);
1814 	}
1815 
1816 	up_read(&env->bpf_progs.lock);
1817 }
1818 
print_bpf_btf(struct feat_fd * ff,FILE * fp)1819 static void print_bpf_btf(struct feat_fd *ff, FILE *fp)
1820 {
1821 	struct perf_env *env = &ff->ph->env;
1822 	struct rb_root *root;
1823 	struct rb_node *next;
1824 
1825 	down_read(&env->bpf_progs.lock);
1826 
1827 	root = &env->bpf_progs.btfs;
1828 	next = rb_first(root);
1829 
1830 	while (next) {
1831 		struct btf_node *node;
1832 
1833 		node = rb_entry(next, struct btf_node, rb_node);
1834 		next = rb_next(&node->rb_node);
1835 		fprintf(fp, "# btf info of id %u\n", node->id);
1836 	}
1837 
1838 	up_read(&env->bpf_progs.lock);
1839 }
1840 #endif // HAVE_LIBBPF_SUPPORT
1841 
free_event_desc(struct evsel * events)1842 static void free_event_desc(struct evsel *events)
1843 {
1844 	struct evsel *evsel;
1845 
1846 	if (!events)
1847 		return;
1848 
1849 	for (evsel = events; evsel->core.attr.size; evsel++) {
1850 		zfree(&evsel->name);
1851 		zfree(&evsel->core.id);
1852 	}
1853 
1854 	free(events);
1855 }
1856 
perf_attr_check(struct perf_event_attr * attr)1857 static bool perf_attr_check(struct perf_event_attr *attr)
1858 {
1859 	if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3) {
1860 		pr_warning("Reserved bits are set unexpectedly. "
1861 			   "Please update perf tool.\n");
1862 		return false;
1863 	}
1864 
1865 	if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) {
1866 		pr_warning("Unknown sample type (0x%llx) is detected. "
1867 			   "Please update perf tool.\n",
1868 			   attr->sample_type);
1869 		return false;
1870 	}
1871 
1872 	if (attr->read_format & ~(PERF_FORMAT_MAX-1)) {
1873 		pr_warning("Unknown read format (0x%llx) is detected. "
1874 			   "Please update perf tool.\n",
1875 			   attr->read_format);
1876 		return false;
1877 	}
1878 
1879 	if ((attr->sample_type & PERF_SAMPLE_BRANCH_STACK) &&
1880 	    (attr->branch_sample_type & ~(PERF_SAMPLE_BRANCH_MAX-1))) {
1881 		pr_warning("Unknown branch sample type (0x%llx) is detected. "
1882 			   "Please update perf tool.\n",
1883 			   attr->branch_sample_type);
1884 
1885 		return false;
1886 	}
1887 
1888 	return true;
1889 }
1890 
read_event_desc(struct feat_fd * ff)1891 static struct evsel *read_event_desc(struct feat_fd *ff)
1892 {
1893 	struct evsel *evsel, *events = NULL;
1894 	u64 *id;
1895 	void *buf = NULL;
1896 	u32 nre, sz, nr, i, j;
1897 	size_t msz;
1898 
1899 	/* number of events */
1900 	if (do_read_u32(ff, &nre))
1901 		goto error;
1902 
1903 	if (do_read_u32(ff, &sz))
1904 		goto error;
1905 
1906 	/* buffer to hold on file attr struct */
1907 	buf = malloc(sz);
1908 	if (!buf)
1909 		goto error;
1910 
1911 	/* the last event terminates with evsel->core.attr.size == 0: */
1912 	events = calloc(nre + 1, sizeof(*events));
1913 	if (!events)
1914 		goto error;
1915 
1916 	msz = sizeof(evsel->core.attr);
1917 	if (sz < msz)
1918 		msz = sz;
1919 
1920 	for (i = 0, evsel = events; i < nre; evsel++, i++) {
1921 		evsel->core.idx = i;
1922 
1923 		/*
1924 		 * must read entire on-file attr struct to
1925 		 * sync up with layout.
1926 		 */
1927 		if (__do_read(ff, buf, sz))
1928 			goto error;
1929 
1930 		if (ff->ph->needs_swap)
1931 			perf_event__attr_swap(buf);
1932 
1933 		memcpy(&evsel->core.attr, buf, msz);
1934 
1935 		if (!perf_attr_check(&evsel->core.attr))
1936 			goto error;
1937 
1938 		if (do_read_u32(ff, &nr))
1939 			goto error;
1940 
1941 		if (ff->ph->needs_swap)
1942 			evsel->needs_swap = true;
1943 
1944 		evsel->name = do_read_string(ff);
1945 		if (!evsel->name)
1946 			goto error;
1947 
1948 		if (!nr)
1949 			continue;
1950 
1951 		id = calloc(nr, sizeof(*id));
1952 		if (!id)
1953 			goto error;
1954 		evsel->core.ids = nr;
1955 		evsel->core.id = id;
1956 
1957 		for (j = 0 ; j < nr; j++) {
1958 			if (do_read_u64(ff, id))
1959 				goto error;
1960 			id++;
1961 		}
1962 	}
1963 out:
1964 	free(buf);
1965 	return events;
1966 error:
1967 	free_event_desc(events);
1968 	events = NULL;
1969 	goto out;
1970 }
1971 
__desc_attr__fprintf(FILE * fp,const char * name,const char * val,void * priv __maybe_unused)1972 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1973 				void *priv __maybe_unused)
1974 {
1975 	return fprintf(fp, ", %s = %s", name, val);
1976 }
1977 
print_event_desc(struct feat_fd * ff,FILE * fp)1978 static void print_event_desc(struct feat_fd *ff, FILE *fp)
1979 {
1980 	struct evsel *evsel, *events;
1981 	u32 j;
1982 	u64 *id;
1983 
1984 	if (ff->events)
1985 		events = ff->events;
1986 	else
1987 		events = read_event_desc(ff);
1988 
1989 	if (!events) {
1990 		fprintf(fp, "# event desc: not available or unable to read\n");
1991 		return;
1992 	}
1993 
1994 	for (evsel = events; evsel->core.attr.size; evsel++) {
1995 		fprintf(fp, "# event : name = %s, ", evsel->name);
1996 
1997 		if (evsel->core.ids) {
1998 			fprintf(fp, ", id = {");
1999 			for (j = 0, id = evsel->core.id; j < evsel->core.ids; j++, id++) {
2000 				if (j)
2001 					fputc(',', fp);
2002 				fprintf(fp, " %"PRIu64, *id);
2003 			}
2004 			fprintf(fp, " }");
2005 		}
2006 
2007 		perf_event_attr__fprintf(fp, &evsel->core.attr, __desc_attr__fprintf, NULL);
2008 
2009 		fputc('\n', fp);
2010 	}
2011 
2012 	free_event_desc(events);
2013 	ff->events = NULL;
2014 }
2015 
print_total_mem(struct feat_fd * ff,FILE * fp)2016 static void print_total_mem(struct feat_fd *ff, FILE *fp)
2017 {
2018 	fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
2019 }
2020 
print_numa_topology(struct feat_fd * ff,FILE * fp)2021 static void print_numa_topology(struct feat_fd *ff, FILE *fp)
2022 {
2023 	int i;
2024 	struct numa_node *n;
2025 
2026 	for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
2027 		n = &ff->ph->env.numa_nodes[i];
2028 
2029 		fprintf(fp, "# node%u meminfo  : total = %"PRIu64" kB,"
2030 			    " free = %"PRIu64" kB\n",
2031 			n->node, n->mem_total, n->mem_free);
2032 
2033 		fprintf(fp, "# node%u cpu list : ", n->node);
2034 		cpu_map__fprintf(n->map, fp);
2035 	}
2036 }
2037 
print_cpuid(struct feat_fd * ff,FILE * fp)2038 static void print_cpuid(struct feat_fd *ff, FILE *fp)
2039 {
2040 	fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
2041 }
2042 
print_branch_stack(struct feat_fd * ff __maybe_unused,FILE * fp)2043 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
2044 {
2045 	fprintf(fp, "# contains samples with branch stack\n");
2046 }
2047 
print_auxtrace(struct feat_fd * ff __maybe_unused,FILE * fp)2048 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
2049 {
2050 	fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
2051 }
2052 
print_stat(struct feat_fd * ff __maybe_unused,FILE * fp)2053 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
2054 {
2055 	fprintf(fp, "# contains stat data\n");
2056 }
2057 
print_cache(struct feat_fd * ff,FILE * fp __maybe_unused)2058 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
2059 {
2060 	int i;
2061 
2062 	fprintf(fp, "# CPU cache info:\n");
2063 	for (i = 0; i < ff->ph->env.caches_cnt; i++) {
2064 		fprintf(fp, "#  ");
2065 		cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
2066 	}
2067 }
2068 
print_compressed(struct feat_fd * ff,FILE * fp)2069 static void print_compressed(struct feat_fd *ff, FILE *fp)
2070 {
2071 	fprintf(fp, "# compressed : %s, level = %d, ratio = %d\n",
2072 		ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown",
2073 		ff->ph->env.comp_level, ff->ph->env.comp_ratio);
2074 }
2075 
__print_pmu_caps(FILE * fp,int nr_caps,char ** caps,char * pmu_name)2076 static void __print_pmu_caps(FILE *fp, int nr_caps, char **caps, char *pmu_name)
2077 {
2078 	const char *delimiter = "";
2079 	int i;
2080 
2081 	if (!nr_caps) {
2082 		fprintf(fp, "# %s pmu capabilities: not available\n", pmu_name);
2083 		return;
2084 	}
2085 
2086 	fprintf(fp, "# %s pmu capabilities: ", pmu_name);
2087 	for (i = 0; i < nr_caps; i++) {
2088 		fprintf(fp, "%s%s", delimiter, caps[i]);
2089 		delimiter = ", ";
2090 	}
2091 
2092 	fprintf(fp, "\n");
2093 }
2094 
print_cpu_pmu_caps(struct feat_fd * ff,FILE * fp)2095 static void print_cpu_pmu_caps(struct feat_fd *ff, FILE *fp)
2096 {
2097 	__print_pmu_caps(fp, ff->ph->env.nr_cpu_pmu_caps,
2098 			 ff->ph->env.cpu_pmu_caps, (char *)"cpu");
2099 }
2100 
print_pmu_caps(struct feat_fd * ff,FILE * fp)2101 static void print_pmu_caps(struct feat_fd *ff, FILE *fp)
2102 {
2103 	struct pmu_caps *pmu_caps;
2104 
2105 	for (int i = 0; i < ff->ph->env.nr_pmus_with_caps; i++) {
2106 		pmu_caps = &ff->ph->env.pmu_caps[i];
2107 		__print_pmu_caps(fp, pmu_caps->nr_caps, pmu_caps->caps,
2108 				 pmu_caps->pmu_name);
2109 	}
2110 }
2111 
print_pmu_mappings(struct feat_fd * ff,FILE * fp)2112 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
2113 {
2114 	const char *delimiter = "# pmu mappings: ";
2115 	char *str, *tmp;
2116 	u32 pmu_num;
2117 	u32 type;
2118 
2119 	pmu_num = ff->ph->env.nr_pmu_mappings;
2120 	if (!pmu_num) {
2121 		fprintf(fp, "# pmu mappings: not available\n");
2122 		return;
2123 	}
2124 
2125 	str = ff->ph->env.pmu_mappings;
2126 
2127 	while (pmu_num) {
2128 		type = strtoul(str, &tmp, 0);
2129 		if (*tmp != ':')
2130 			goto error;
2131 
2132 		str = tmp + 1;
2133 		fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
2134 
2135 		delimiter = ", ";
2136 		str += strlen(str) + 1;
2137 		pmu_num--;
2138 	}
2139 
2140 	fprintf(fp, "\n");
2141 
2142 	if (!pmu_num)
2143 		return;
2144 error:
2145 	fprintf(fp, "# pmu mappings: unable to read\n");
2146 }
2147 
print_group_desc(struct feat_fd * ff,FILE * fp)2148 static void print_group_desc(struct feat_fd *ff, FILE *fp)
2149 {
2150 	struct perf_session *session;
2151 	struct evsel *evsel;
2152 	u32 nr = 0;
2153 
2154 	session = container_of(ff->ph, struct perf_session, header);
2155 
2156 	evlist__for_each_entry(session->evlist, evsel) {
2157 		if (evsel__is_group_leader(evsel) && evsel->core.nr_members > 1) {
2158 			fprintf(fp, "# group: %s{%s", evsel->group_name ?: "", evsel__name(evsel));
2159 
2160 			nr = evsel->core.nr_members - 1;
2161 		} else if (nr) {
2162 			fprintf(fp, ",%s", evsel__name(evsel));
2163 
2164 			if (--nr == 0)
2165 				fprintf(fp, "}\n");
2166 		}
2167 	}
2168 }
2169 
print_sample_time(struct feat_fd * ff,FILE * fp)2170 static void print_sample_time(struct feat_fd *ff, FILE *fp)
2171 {
2172 	struct perf_session *session;
2173 	char time_buf[32];
2174 	double d;
2175 
2176 	session = container_of(ff->ph, struct perf_session, header);
2177 
2178 	timestamp__scnprintf_usec(session->evlist->first_sample_time,
2179 				  time_buf, sizeof(time_buf));
2180 	fprintf(fp, "# time of first sample : %s\n", time_buf);
2181 
2182 	timestamp__scnprintf_usec(session->evlist->last_sample_time,
2183 				  time_buf, sizeof(time_buf));
2184 	fprintf(fp, "# time of last sample : %s\n", time_buf);
2185 
2186 	d = (double)(session->evlist->last_sample_time -
2187 		session->evlist->first_sample_time) / NSEC_PER_MSEC;
2188 
2189 	fprintf(fp, "# sample duration : %10.3f ms\n", d);
2190 }
2191 
memory_node__fprintf(struct memory_node * n,unsigned long long bsize,FILE * fp)2192 static void memory_node__fprintf(struct memory_node *n,
2193 				 unsigned long long bsize, FILE *fp)
2194 {
2195 	char buf_map[100], buf_size[50];
2196 	unsigned long long size;
2197 
2198 	size = bsize * bitmap_weight(n->set, n->size);
2199 	unit_number__scnprintf(buf_size, 50, size);
2200 
2201 	bitmap_scnprintf(n->set, n->size, buf_map, 100);
2202 	fprintf(fp, "#  %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
2203 }
2204 
print_mem_topology(struct feat_fd * ff,FILE * fp)2205 static void print_mem_topology(struct feat_fd *ff, FILE *fp)
2206 {
2207 	struct memory_node *nodes;
2208 	int i, nr;
2209 
2210 	nodes = ff->ph->env.memory_nodes;
2211 	nr    = ff->ph->env.nr_memory_nodes;
2212 
2213 	fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
2214 		nr, ff->ph->env.memory_bsize);
2215 
2216 	for (i = 0; i < nr; i++) {
2217 		memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
2218 	}
2219 }
2220 
__event_process_build_id(struct perf_record_header_build_id * bev,char * filename,struct perf_session * session)2221 static int __event_process_build_id(struct perf_record_header_build_id *bev,
2222 				    char *filename,
2223 				    struct perf_session *session)
2224 {
2225 	int err = -1;
2226 	struct machine *machine;
2227 	u16 cpumode;
2228 	struct dso *dso;
2229 	enum dso_space_type dso_space;
2230 
2231 	machine = perf_session__findnew_machine(session, bev->pid);
2232 	if (!machine)
2233 		goto out;
2234 
2235 	cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
2236 
2237 	switch (cpumode) {
2238 	case PERF_RECORD_MISC_KERNEL:
2239 		dso_space = DSO_SPACE__KERNEL;
2240 		break;
2241 	case PERF_RECORD_MISC_GUEST_KERNEL:
2242 		dso_space = DSO_SPACE__KERNEL_GUEST;
2243 		break;
2244 	case PERF_RECORD_MISC_USER:
2245 	case PERF_RECORD_MISC_GUEST_USER:
2246 		dso_space = DSO_SPACE__USER;
2247 		break;
2248 	default:
2249 		goto out;
2250 	}
2251 
2252 	dso = machine__findnew_dso(machine, filename);
2253 	if (dso != NULL) {
2254 		char sbuild_id[SBUILD_ID_SIZE];
2255 		struct build_id bid;
2256 		size_t size = BUILD_ID_SIZE;
2257 
2258 		if (bev->header.misc & PERF_RECORD_MISC_BUILD_ID_SIZE)
2259 			size = bev->size;
2260 
2261 		build_id__init(&bid, bev->data, size);
2262 		dso__set_build_id(dso, &bid);
2263 		dso->header_build_id = 1;
2264 
2265 		if (dso_space != DSO_SPACE__USER) {
2266 			struct kmod_path m = { .name = NULL, };
2267 
2268 			if (!kmod_path__parse_name(&m, filename) && m.kmod)
2269 				dso__set_module_info(dso, &m, machine);
2270 
2271 			dso->kernel = dso_space;
2272 			free(m.name);
2273 		}
2274 
2275 		build_id__sprintf(&dso->bid, sbuild_id);
2276 		pr_debug("build id event received for %s: %s [%zu]\n",
2277 			 dso->long_name, sbuild_id, size);
2278 		dso__put(dso);
2279 	}
2280 
2281 	err = 0;
2282 out:
2283 	return err;
2284 }
2285 
perf_header__read_build_ids_abi_quirk(struct perf_header * header,int input,u64 offset,u64 size)2286 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
2287 						 int input, u64 offset, u64 size)
2288 {
2289 	struct perf_session *session = container_of(header, struct perf_session, header);
2290 	struct {
2291 		struct perf_event_header   header;
2292 		u8			   build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
2293 		char			   filename[0];
2294 	} old_bev;
2295 	struct perf_record_header_build_id bev;
2296 	char filename[PATH_MAX];
2297 	u64 limit = offset + size;
2298 
2299 	while (offset < limit) {
2300 		ssize_t len;
2301 
2302 		if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
2303 			return -1;
2304 
2305 		if (header->needs_swap)
2306 			perf_event_header__bswap(&old_bev.header);
2307 
2308 		len = old_bev.header.size - sizeof(old_bev);
2309 		if (readn(input, filename, len) != len)
2310 			return -1;
2311 
2312 		bev.header = old_bev.header;
2313 
2314 		/*
2315 		 * As the pid is the missing value, we need to fill
2316 		 * it properly. The header.misc value give us nice hint.
2317 		 */
2318 		bev.pid	= HOST_KERNEL_ID;
2319 		if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
2320 		    bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
2321 			bev.pid	= DEFAULT_GUEST_KERNEL_ID;
2322 
2323 		memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
2324 		__event_process_build_id(&bev, filename, session);
2325 
2326 		offset += bev.header.size;
2327 	}
2328 
2329 	return 0;
2330 }
2331 
perf_header__read_build_ids(struct perf_header * header,int input,u64 offset,u64 size)2332 static int perf_header__read_build_ids(struct perf_header *header,
2333 				       int input, u64 offset, u64 size)
2334 {
2335 	struct perf_session *session = container_of(header, struct perf_session, header);
2336 	struct perf_record_header_build_id bev;
2337 	char filename[PATH_MAX];
2338 	u64 limit = offset + size, orig_offset = offset;
2339 	int err = -1;
2340 
2341 	while (offset < limit) {
2342 		ssize_t len;
2343 
2344 		if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
2345 			goto out;
2346 
2347 		if (header->needs_swap)
2348 			perf_event_header__bswap(&bev.header);
2349 
2350 		len = bev.header.size - sizeof(bev);
2351 		if (readn(input, filename, len) != len)
2352 			goto out;
2353 		/*
2354 		 * The a1645ce1 changeset:
2355 		 *
2356 		 * "perf: 'perf kvm' tool for monitoring guest performance from host"
2357 		 *
2358 		 * Added a field to struct perf_record_header_build_id that broke the file
2359 		 * format.
2360 		 *
2361 		 * Since the kernel build-id is the first entry, process the
2362 		 * table using the old format if the well known
2363 		 * '[kernel.kallsyms]' string for the kernel build-id has the
2364 		 * first 4 characters chopped off (where the pid_t sits).
2365 		 */
2366 		if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
2367 			if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
2368 				return -1;
2369 			return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
2370 		}
2371 
2372 		__event_process_build_id(&bev, filename, session);
2373 
2374 		offset += bev.header.size;
2375 	}
2376 	err = 0;
2377 out:
2378 	return err;
2379 }
2380 
2381 /* Macro for features that simply need to read and store a string. */
2382 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
2383 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
2384 {\
2385 	free(ff->ph->env.__feat_env);		     \
2386 	ff->ph->env.__feat_env = do_read_string(ff); \
2387 	return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
2388 }
2389 
2390 FEAT_PROCESS_STR_FUN(hostname, hostname);
2391 FEAT_PROCESS_STR_FUN(osrelease, os_release);
2392 FEAT_PROCESS_STR_FUN(version, version);
2393 FEAT_PROCESS_STR_FUN(arch, arch);
2394 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
2395 FEAT_PROCESS_STR_FUN(cpuid, cpuid);
2396 
process_tracing_data(struct feat_fd * ff,void * data)2397 static int process_tracing_data(struct feat_fd *ff, void *data)
2398 {
2399 	ssize_t ret = trace_report(ff->fd, data, false);
2400 
2401 	return ret < 0 ? -1 : 0;
2402 }
2403 
process_build_id(struct feat_fd * ff,void * data __maybe_unused)2404 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
2405 {
2406 	if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
2407 		pr_debug("Failed to read buildids, continuing...\n");
2408 	return 0;
2409 }
2410 
process_nrcpus(struct feat_fd * ff,void * data __maybe_unused)2411 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
2412 {
2413 	int ret;
2414 	u32 nr_cpus_avail, nr_cpus_online;
2415 
2416 	ret = do_read_u32(ff, &nr_cpus_avail);
2417 	if (ret)
2418 		return ret;
2419 
2420 	ret = do_read_u32(ff, &nr_cpus_online);
2421 	if (ret)
2422 		return ret;
2423 	ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
2424 	ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
2425 	return 0;
2426 }
2427 
process_total_mem(struct feat_fd * ff,void * data __maybe_unused)2428 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
2429 {
2430 	u64 total_mem;
2431 	int ret;
2432 
2433 	ret = do_read_u64(ff, &total_mem);
2434 	if (ret)
2435 		return -1;
2436 	ff->ph->env.total_mem = (unsigned long long)total_mem;
2437 	return 0;
2438 }
2439 
evlist__find_by_index(struct evlist * evlist,int idx)2440 static struct evsel *evlist__find_by_index(struct evlist *evlist, int idx)
2441 {
2442 	struct evsel *evsel;
2443 
2444 	evlist__for_each_entry(evlist, evsel) {
2445 		if (evsel->core.idx == idx)
2446 			return evsel;
2447 	}
2448 
2449 	return NULL;
2450 }
2451 
evlist__set_event_name(struct evlist * evlist,struct evsel * event)2452 static void evlist__set_event_name(struct evlist *evlist, struct evsel *event)
2453 {
2454 	struct evsel *evsel;
2455 
2456 	if (!event->name)
2457 		return;
2458 
2459 	evsel = evlist__find_by_index(evlist, event->core.idx);
2460 	if (!evsel)
2461 		return;
2462 
2463 	if (evsel->name)
2464 		return;
2465 
2466 	evsel->name = strdup(event->name);
2467 }
2468 
2469 static int
process_event_desc(struct feat_fd * ff,void * data __maybe_unused)2470 process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
2471 {
2472 	struct perf_session *session;
2473 	struct evsel *evsel, *events = read_event_desc(ff);
2474 
2475 	if (!events)
2476 		return 0;
2477 
2478 	session = container_of(ff->ph, struct perf_session, header);
2479 
2480 	if (session->data->is_pipe) {
2481 		/* Save events for reading later by print_event_desc,
2482 		 * since they can't be read again in pipe mode. */
2483 		ff->events = events;
2484 	}
2485 
2486 	for (evsel = events; evsel->core.attr.size; evsel++)
2487 		evlist__set_event_name(session->evlist, evsel);
2488 
2489 	if (!session->data->is_pipe)
2490 		free_event_desc(events);
2491 
2492 	return 0;
2493 }
2494 
process_cmdline(struct feat_fd * ff,void * data __maybe_unused)2495 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
2496 {
2497 	char *str, *cmdline = NULL, **argv = NULL;
2498 	u32 nr, i, len = 0;
2499 
2500 	if (do_read_u32(ff, &nr))
2501 		return -1;
2502 
2503 	ff->ph->env.nr_cmdline = nr;
2504 
2505 	cmdline = zalloc(ff->size + nr + 1);
2506 	if (!cmdline)
2507 		return -1;
2508 
2509 	argv = zalloc(sizeof(char *) * (nr + 1));
2510 	if (!argv)
2511 		goto error;
2512 
2513 	for (i = 0; i < nr; i++) {
2514 		str = do_read_string(ff);
2515 		if (!str)
2516 			goto error;
2517 
2518 		argv[i] = cmdline + len;
2519 		memcpy(argv[i], str, strlen(str) + 1);
2520 		len += strlen(str) + 1;
2521 		free(str);
2522 	}
2523 	ff->ph->env.cmdline = cmdline;
2524 	ff->ph->env.cmdline_argv = (const char **) argv;
2525 	return 0;
2526 
2527 error:
2528 	free(argv);
2529 	free(cmdline);
2530 	return -1;
2531 }
2532 
process_cpu_topology(struct feat_fd * ff,void * data __maybe_unused)2533 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
2534 {
2535 	u32 nr, i;
2536 	char *str;
2537 	struct strbuf sb;
2538 	int cpu_nr = ff->ph->env.nr_cpus_avail;
2539 	u64 size = 0;
2540 	struct perf_header *ph = ff->ph;
2541 	bool do_core_id_test = true;
2542 
2543 	ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
2544 	if (!ph->env.cpu)
2545 		return -1;
2546 
2547 	if (do_read_u32(ff, &nr))
2548 		goto free_cpu;
2549 
2550 	ph->env.nr_sibling_cores = nr;
2551 	size += sizeof(u32);
2552 	if (strbuf_init(&sb, 128) < 0)
2553 		goto free_cpu;
2554 
2555 	for (i = 0; i < nr; i++) {
2556 		str = do_read_string(ff);
2557 		if (!str)
2558 			goto error;
2559 
2560 		/* include a NULL character at the end */
2561 		if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2562 			goto error;
2563 		size += string_size(str);
2564 		free(str);
2565 	}
2566 	ph->env.sibling_cores = strbuf_detach(&sb, NULL);
2567 
2568 	if (do_read_u32(ff, &nr))
2569 		return -1;
2570 
2571 	ph->env.nr_sibling_threads = nr;
2572 	size += sizeof(u32);
2573 
2574 	for (i = 0; i < nr; i++) {
2575 		str = do_read_string(ff);
2576 		if (!str)
2577 			goto error;
2578 
2579 		/* include a NULL character at the end */
2580 		if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2581 			goto error;
2582 		size += string_size(str);
2583 		free(str);
2584 	}
2585 	ph->env.sibling_threads = strbuf_detach(&sb, NULL);
2586 
2587 	/*
2588 	 * The header may be from old perf,
2589 	 * which doesn't include core id and socket id information.
2590 	 */
2591 	if (ff->size <= size) {
2592 		zfree(&ph->env.cpu);
2593 		return 0;
2594 	}
2595 
2596 	/* On s390 the socket_id number is not related to the numbers of cpus.
2597 	 * The socket_id number might be higher than the numbers of cpus.
2598 	 * This depends on the configuration.
2599 	 * AArch64 is the same.
2600 	 */
2601 	if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4)
2602 			  || !strncmp(ph->env.arch, "aarch64", 7)))
2603 		do_core_id_test = false;
2604 
2605 	for (i = 0; i < (u32)cpu_nr; i++) {
2606 		if (do_read_u32(ff, &nr))
2607 			goto free_cpu;
2608 
2609 		ph->env.cpu[i].core_id = nr;
2610 		size += sizeof(u32);
2611 
2612 		if (do_read_u32(ff, &nr))
2613 			goto free_cpu;
2614 
2615 		if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
2616 			pr_debug("socket_id number is too big."
2617 				 "You may need to upgrade the perf tool.\n");
2618 			goto free_cpu;
2619 		}
2620 
2621 		ph->env.cpu[i].socket_id = nr;
2622 		size += sizeof(u32);
2623 	}
2624 
2625 	/*
2626 	 * The header may be from old perf,
2627 	 * which doesn't include die information.
2628 	 */
2629 	if (ff->size <= size)
2630 		return 0;
2631 
2632 	if (do_read_u32(ff, &nr))
2633 		return -1;
2634 
2635 	ph->env.nr_sibling_dies = nr;
2636 	size += sizeof(u32);
2637 
2638 	for (i = 0; i < nr; i++) {
2639 		str = do_read_string(ff);
2640 		if (!str)
2641 			goto error;
2642 
2643 		/* include a NULL character at the end */
2644 		if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2645 			goto error;
2646 		size += string_size(str);
2647 		free(str);
2648 	}
2649 	ph->env.sibling_dies = strbuf_detach(&sb, NULL);
2650 
2651 	for (i = 0; i < (u32)cpu_nr; i++) {
2652 		if (do_read_u32(ff, &nr))
2653 			goto free_cpu;
2654 
2655 		ph->env.cpu[i].die_id = nr;
2656 	}
2657 
2658 	return 0;
2659 
2660 error:
2661 	strbuf_release(&sb);
2662 free_cpu:
2663 	zfree(&ph->env.cpu);
2664 	return -1;
2665 }
2666 
process_numa_topology(struct feat_fd * ff,void * data __maybe_unused)2667 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
2668 {
2669 	struct numa_node *nodes, *n;
2670 	u32 nr, i;
2671 	char *str;
2672 
2673 	/* nr nodes */
2674 	if (do_read_u32(ff, &nr))
2675 		return -1;
2676 
2677 	nodes = zalloc(sizeof(*nodes) * nr);
2678 	if (!nodes)
2679 		return -ENOMEM;
2680 
2681 	for (i = 0; i < nr; i++) {
2682 		n = &nodes[i];
2683 
2684 		/* node number */
2685 		if (do_read_u32(ff, &n->node))
2686 			goto error;
2687 
2688 		if (do_read_u64(ff, &n->mem_total))
2689 			goto error;
2690 
2691 		if (do_read_u64(ff, &n->mem_free))
2692 			goto error;
2693 
2694 		str = do_read_string(ff);
2695 		if (!str)
2696 			goto error;
2697 
2698 		n->map = perf_cpu_map__new(str);
2699 		if (!n->map)
2700 			goto error;
2701 
2702 		free(str);
2703 	}
2704 	ff->ph->env.nr_numa_nodes = nr;
2705 	ff->ph->env.numa_nodes = nodes;
2706 	return 0;
2707 
2708 error:
2709 	free(nodes);
2710 	return -1;
2711 }
2712 
process_pmu_mappings(struct feat_fd * ff,void * data __maybe_unused)2713 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
2714 {
2715 	char *name;
2716 	u32 pmu_num;
2717 	u32 type;
2718 	struct strbuf sb;
2719 
2720 	if (do_read_u32(ff, &pmu_num))
2721 		return -1;
2722 
2723 	if (!pmu_num) {
2724 		pr_debug("pmu mappings not available\n");
2725 		return 0;
2726 	}
2727 
2728 	ff->ph->env.nr_pmu_mappings = pmu_num;
2729 	if (strbuf_init(&sb, 128) < 0)
2730 		return -1;
2731 
2732 	while (pmu_num) {
2733 		if (do_read_u32(ff, &type))
2734 			goto error;
2735 
2736 		name = do_read_string(ff);
2737 		if (!name)
2738 			goto error;
2739 
2740 		if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2741 			goto error;
2742 		/* include a NULL character at the end */
2743 		if (strbuf_add(&sb, "", 1) < 0)
2744 			goto error;
2745 
2746 		if (!strcmp(name, "msr"))
2747 			ff->ph->env.msr_pmu_type = type;
2748 
2749 		free(name);
2750 		pmu_num--;
2751 	}
2752 	ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2753 	return 0;
2754 
2755 error:
2756 	strbuf_release(&sb);
2757 	return -1;
2758 }
2759 
process_group_desc(struct feat_fd * ff,void * data __maybe_unused)2760 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
2761 {
2762 	size_t ret = -1;
2763 	u32 i, nr, nr_groups;
2764 	struct perf_session *session;
2765 	struct evsel *evsel, *leader = NULL;
2766 	struct group_desc {
2767 		char *name;
2768 		u32 leader_idx;
2769 		u32 nr_members;
2770 	} *desc;
2771 
2772 	if (do_read_u32(ff, &nr_groups))
2773 		return -1;
2774 
2775 	ff->ph->env.nr_groups = nr_groups;
2776 	if (!nr_groups) {
2777 		pr_debug("group desc not available\n");
2778 		return 0;
2779 	}
2780 
2781 	desc = calloc(nr_groups, sizeof(*desc));
2782 	if (!desc)
2783 		return -1;
2784 
2785 	for (i = 0; i < nr_groups; i++) {
2786 		desc[i].name = do_read_string(ff);
2787 		if (!desc[i].name)
2788 			goto out_free;
2789 
2790 		if (do_read_u32(ff, &desc[i].leader_idx))
2791 			goto out_free;
2792 
2793 		if (do_read_u32(ff, &desc[i].nr_members))
2794 			goto out_free;
2795 	}
2796 
2797 	/*
2798 	 * Rebuild group relationship based on the group_desc
2799 	 */
2800 	session = container_of(ff->ph, struct perf_session, header);
2801 	session->evlist->core.nr_groups = nr_groups;
2802 
2803 	i = nr = 0;
2804 	evlist__for_each_entry(session->evlist, evsel) {
2805 		if (evsel->core.idx == (int) desc[i].leader_idx) {
2806 			evsel__set_leader(evsel, evsel);
2807 			/* {anon_group} is a dummy name */
2808 			if (strcmp(desc[i].name, "{anon_group}")) {
2809 				evsel->group_name = desc[i].name;
2810 				desc[i].name = NULL;
2811 			}
2812 			evsel->core.nr_members = desc[i].nr_members;
2813 
2814 			if (i >= nr_groups || nr > 0) {
2815 				pr_debug("invalid group desc\n");
2816 				goto out_free;
2817 			}
2818 
2819 			leader = evsel;
2820 			nr = evsel->core.nr_members - 1;
2821 			i++;
2822 		} else if (nr) {
2823 			/* This is a group member */
2824 			evsel__set_leader(evsel, leader);
2825 
2826 			nr--;
2827 		}
2828 	}
2829 
2830 	if (i != nr_groups || nr != 0) {
2831 		pr_debug("invalid group desc\n");
2832 		goto out_free;
2833 	}
2834 
2835 	ret = 0;
2836 out_free:
2837 	for (i = 0; i < nr_groups; i++)
2838 		zfree(&desc[i].name);
2839 	free(desc);
2840 
2841 	return ret;
2842 }
2843 
process_auxtrace(struct feat_fd * ff,void * data __maybe_unused)2844 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
2845 {
2846 	struct perf_session *session;
2847 	int err;
2848 
2849 	session = container_of(ff->ph, struct perf_session, header);
2850 
2851 	err = auxtrace_index__process(ff->fd, ff->size, session,
2852 				      ff->ph->needs_swap);
2853 	if (err < 0)
2854 		pr_err("Failed to process auxtrace index\n");
2855 	return err;
2856 }
2857 
process_cache(struct feat_fd * ff,void * data __maybe_unused)2858 static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
2859 {
2860 	struct cpu_cache_level *caches;
2861 	u32 cnt, i, version;
2862 
2863 	if (do_read_u32(ff, &version))
2864 		return -1;
2865 
2866 	if (version != 1)
2867 		return -1;
2868 
2869 	if (do_read_u32(ff, &cnt))
2870 		return -1;
2871 
2872 	caches = zalloc(sizeof(*caches) * cnt);
2873 	if (!caches)
2874 		return -1;
2875 
2876 	for (i = 0; i < cnt; i++) {
2877 		struct cpu_cache_level c;
2878 
2879 		#define _R(v)						\
2880 			if (do_read_u32(ff, &c.v))\
2881 				goto out_free_caches;			\
2882 
2883 		_R(level)
2884 		_R(line_size)
2885 		_R(sets)
2886 		_R(ways)
2887 		#undef _R
2888 
2889 		#define _R(v)					\
2890 			c.v = do_read_string(ff);		\
2891 			if (!c.v)				\
2892 				goto out_free_caches;
2893 
2894 		_R(type)
2895 		_R(size)
2896 		_R(map)
2897 		#undef _R
2898 
2899 		caches[i] = c;
2900 	}
2901 
2902 	ff->ph->env.caches = caches;
2903 	ff->ph->env.caches_cnt = cnt;
2904 	return 0;
2905 out_free_caches:
2906 	free(caches);
2907 	return -1;
2908 }
2909 
process_sample_time(struct feat_fd * ff,void * data __maybe_unused)2910 static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2911 {
2912 	struct perf_session *session;
2913 	u64 first_sample_time, last_sample_time;
2914 	int ret;
2915 
2916 	session = container_of(ff->ph, struct perf_session, header);
2917 
2918 	ret = do_read_u64(ff, &first_sample_time);
2919 	if (ret)
2920 		return -1;
2921 
2922 	ret = do_read_u64(ff, &last_sample_time);
2923 	if (ret)
2924 		return -1;
2925 
2926 	session->evlist->first_sample_time = first_sample_time;
2927 	session->evlist->last_sample_time = last_sample_time;
2928 	return 0;
2929 }
2930 
process_mem_topology(struct feat_fd * ff,void * data __maybe_unused)2931 static int process_mem_topology(struct feat_fd *ff,
2932 				void *data __maybe_unused)
2933 {
2934 	struct memory_node *nodes;
2935 	u64 version, i, nr, bsize;
2936 	int ret = -1;
2937 
2938 	if (do_read_u64(ff, &version))
2939 		return -1;
2940 
2941 	if (version != 1)
2942 		return -1;
2943 
2944 	if (do_read_u64(ff, &bsize))
2945 		return -1;
2946 
2947 	if (do_read_u64(ff, &nr))
2948 		return -1;
2949 
2950 	nodes = zalloc(sizeof(*nodes) * nr);
2951 	if (!nodes)
2952 		return -1;
2953 
2954 	for (i = 0; i < nr; i++) {
2955 		struct memory_node n;
2956 
2957 		#define _R(v)				\
2958 			if (do_read_u64(ff, &n.v))	\
2959 				goto out;		\
2960 
2961 		_R(node)
2962 		_R(size)
2963 
2964 		#undef _R
2965 
2966 		if (do_read_bitmap(ff, &n.set, &n.size))
2967 			goto out;
2968 
2969 		nodes[i] = n;
2970 	}
2971 
2972 	ff->ph->env.memory_bsize    = bsize;
2973 	ff->ph->env.memory_nodes    = nodes;
2974 	ff->ph->env.nr_memory_nodes = nr;
2975 	ret = 0;
2976 
2977 out:
2978 	if (ret)
2979 		free(nodes);
2980 	return ret;
2981 }
2982 
process_clockid(struct feat_fd * ff,void * data __maybe_unused)2983 static int process_clockid(struct feat_fd *ff,
2984 			   void *data __maybe_unused)
2985 {
2986 	if (do_read_u64(ff, &ff->ph->env.clock.clockid_res_ns))
2987 		return -1;
2988 
2989 	return 0;
2990 }
2991 
process_clock_data(struct feat_fd * ff,void * _data __maybe_unused)2992 static int process_clock_data(struct feat_fd *ff,
2993 			      void *_data __maybe_unused)
2994 {
2995 	u32 data32;
2996 	u64 data64;
2997 
2998 	/* version */
2999 	if (do_read_u32(ff, &data32))
3000 		return -1;
3001 
3002 	if (data32 != 1)
3003 		return -1;
3004 
3005 	/* clockid */
3006 	if (do_read_u32(ff, &data32))
3007 		return -1;
3008 
3009 	ff->ph->env.clock.clockid = data32;
3010 
3011 	/* TOD ref time */
3012 	if (do_read_u64(ff, &data64))
3013 		return -1;
3014 
3015 	ff->ph->env.clock.tod_ns = data64;
3016 
3017 	/* clockid ref time */
3018 	if (do_read_u64(ff, &data64))
3019 		return -1;
3020 
3021 	ff->ph->env.clock.clockid_ns = data64;
3022 	ff->ph->env.clock.enabled = true;
3023 	return 0;
3024 }
3025 
process_hybrid_topology(struct feat_fd * ff,void * data __maybe_unused)3026 static int process_hybrid_topology(struct feat_fd *ff,
3027 				   void *data __maybe_unused)
3028 {
3029 	struct hybrid_node *nodes, *n;
3030 	u32 nr, i;
3031 
3032 	/* nr nodes */
3033 	if (do_read_u32(ff, &nr))
3034 		return -1;
3035 
3036 	nodes = zalloc(sizeof(*nodes) * nr);
3037 	if (!nodes)
3038 		return -ENOMEM;
3039 
3040 	for (i = 0; i < nr; i++) {
3041 		n = &nodes[i];
3042 
3043 		n->pmu_name = do_read_string(ff);
3044 		if (!n->pmu_name)
3045 			goto error;
3046 
3047 		n->cpus = do_read_string(ff);
3048 		if (!n->cpus)
3049 			goto error;
3050 	}
3051 
3052 	ff->ph->env.nr_hybrid_nodes = nr;
3053 	ff->ph->env.hybrid_nodes = nodes;
3054 	return 0;
3055 
3056 error:
3057 	for (i = 0; i < nr; i++) {
3058 		free(nodes[i].pmu_name);
3059 		free(nodes[i].cpus);
3060 	}
3061 
3062 	free(nodes);
3063 	return -1;
3064 }
3065 
process_dir_format(struct feat_fd * ff,void * _data __maybe_unused)3066 static int process_dir_format(struct feat_fd *ff,
3067 			      void *_data __maybe_unused)
3068 {
3069 	struct perf_session *session;
3070 	struct perf_data *data;
3071 
3072 	session = container_of(ff->ph, struct perf_session, header);
3073 	data = session->data;
3074 
3075 	if (WARN_ON(!perf_data__is_dir(data)))
3076 		return -1;
3077 
3078 	return do_read_u64(ff, &data->dir.version);
3079 }
3080 
3081 #ifdef HAVE_LIBBPF_SUPPORT
process_bpf_prog_info(struct feat_fd * ff,void * data __maybe_unused)3082 static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)
3083 {
3084 	struct bpf_prog_info_node *info_node;
3085 	struct perf_env *env = &ff->ph->env;
3086 	struct perf_bpil *info_linear;
3087 	u32 count, i;
3088 	int err = -1;
3089 
3090 	if (ff->ph->needs_swap) {
3091 		pr_warning("interpreting bpf_prog_info from systems with endianness is not yet supported\n");
3092 		return 0;
3093 	}
3094 
3095 	if (do_read_u32(ff, &count))
3096 		return -1;
3097 
3098 	down_write(&env->bpf_progs.lock);
3099 
3100 	for (i = 0; i < count; ++i) {
3101 		u32 info_len, data_len;
3102 
3103 		info_linear = NULL;
3104 		info_node = NULL;
3105 		if (do_read_u32(ff, &info_len))
3106 			goto out;
3107 		if (do_read_u32(ff, &data_len))
3108 			goto out;
3109 
3110 		if (info_len > sizeof(struct bpf_prog_info)) {
3111 			pr_warning("detected invalid bpf_prog_info\n");
3112 			goto out;
3113 		}
3114 
3115 		info_linear = malloc(sizeof(struct perf_bpil) +
3116 				     data_len);
3117 		if (!info_linear)
3118 			goto out;
3119 		info_linear->info_len = sizeof(struct bpf_prog_info);
3120 		info_linear->data_len = data_len;
3121 		if (do_read_u64(ff, (u64 *)(&info_linear->arrays)))
3122 			goto out;
3123 		if (__do_read(ff, &info_linear->info, info_len))
3124 			goto out;
3125 		if (info_len < sizeof(struct bpf_prog_info))
3126 			memset(((void *)(&info_linear->info)) + info_len, 0,
3127 			       sizeof(struct bpf_prog_info) - info_len);
3128 
3129 		if (__do_read(ff, info_linear->data, data_len))
3130 			goto out;
3131 
3132 		info_node = malloc(sizeof(struct bpf_prog_info_node));
3133 		if (!info_node)
3134 			goto out;
3135 
3136 		/* after reading from file, translate offset to address */
3137 		bpil_offs_to_addr(info_linear);
3138 		info_node->info_linear = info_linear;
3139 		perf_env__insert_bpf_prog_info(env, info_node);
3140 	}
3141 
3142 	up_write(&env->bpf_progs.lock);
3143 	return 0;
3144 out:
3145 	free(info_linear);
3146 	free(info_node);
3147 	up_write(&env->bpf_progs.lock);
3148 	return err;
3149 }
3150 
process_bpf_btf(struct feat_fd * ff,void * data __maybe_unused)3151 static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)
3152 {
3153 	struct perf_env *env = &ff->ph->env;
3154 	struct btf_node *node = NULL;
3155 	u32 count, i;
3156 	int err = -1;
3157 
3158 	if (ff->ph->needs_swap) {
3159 		pr_warning("interpreting btf from systems with endianness is not yet supported\n");
3160 		return 0;
3161 	}
3162 
3163 	if (do_read_u32(ff, &count))
3164 		return -1;
3165 
3166 	down_write(&env->bpf_progs.lock);
3167 
3168 	for (i = 0; i < count; ++i) {
3169 		u32 id, data_size;
3170 
3171 		if (do_read_u32(ff, &id))
3172 			goto out;
3173 		if (do_read_u32(ff, &data_size))
3174 			goto out;
3175 
3176 		node = malloc(sizeof(struct btf_node) + data_size);
3177 		if (!node)
3178 			goto out;
3179 
3180 		node->id = id;
3181 		node->data_size = data_size;
3182 
3183 		if (__do_read(ff, node->data, data_size))
3184 			goto out;
3185 
3186 		perf_env__insert_btf(env, node);
3187 		node = NULL;
3188 	}
3189 
3190 	err = 0;
3191 out:
3192 	up_write(&env->bpf_progs.lock);
3193 	free(node);
3194 	return err;
3195 }
3196 #endif // HAVE_LIBBPF_SUPPORT
3197 
process_compressed(struct feat_fd * ff,void * data __maybe_unused)3198 static int process_compressed(struct feat_fd *ff,
3199 			      void *data __maybe_unused)
3200 {
3201 	if (do_read_u32(ff, &(ff->ph->env.comp_ver)))
3202 		return -1;
3203 
3204 	if (do_read_u32(ff, &(ff->ph->env.comp_type)))
3205 		return -1;
3206 
3207 	if (do_read_u32(ff, &(ff->ph->env.comp_level)))
3208 		return -1;
3209 
3210 	if (do_read_u32(ff, &(ff->ph->env.comp_ratio)))
3211 		return -1;
3212 
3213 	if (do_read_u32(ff, &(ff->ph->env.comp_mmap_len)))
3214 		return -1;
3215 
3216 	return 0;
3217 }
3218 
__process_pmu_caps(struct feat_fd * ff,int * nr_caps,char *** caps,unsigned int * max_branches)3219 static int __process_pmu_caps(struct feat_fd *ff, int *nr_caps,
3220 			      char ***caps, unsigned int *max_branches)
3221 {
3222 	char *name, *value, *ptr;
3223 	u32 nr_pmu_caps, i;
3224 
3225 	*nr_caps = 0;
3226 	*caps = NULL;
3227 
3228 	if (do_read_u32(ff, &nr_pmu_caps))
3229 		return -1;
3230 
3231 	if (!nr_pmu_caps)
3232 		return 0;
3233 
3234 	*caps = zalloc(sizeof(char *) * nr_pmu_caps);
3235 	if (!*caps)
3236 		return -1;
3237 
3238 	for (i = 0; i < nr_pmu_caps; i++) {
3239 		name = do_read_string(ff);
3240 		if (!name)
3241 			goto error;
3242 
3243 		value = do_read_string(ff);
3244 		if (!value)
3245 			goto free_name;
3246 
3247 		if (asprintf(&ptr, "%s=%s", name, value) < 0)
3248 			goto free_value;
3249 
3250 		(*caps)[i] = ptr;
3251 
3252 		if (!strcmp(name, "branches"))
3253 			*max_branches = atoi(value);
3254 
3255 		free(value);
3256 		free(name);
3257 	}
3258 	*nr_caps = nr_pmu_caps;
3259 	return 0;
3260 
3261 free_value:
3262 	free(value);
3263 free_name:
3264 	free(name);
3265 error:
3266 	for (; i > 0; i--)
3267 		free((*caps)[i - 1]);
3268 	free(*caps);
3269 	*caps = NULL;
3270 	*nr_caps = 0;
3271 	return -1;
3272 }
3273 
process_cpu_pmu_caps(struct feat_fd * ff,void * data __maybe_unused)3274 static int process_cpu_pmu_caps(struct feat_fd *ff,
3275 				void *data __maybe_unused)
3276 {
3277 	int ret = __process_pmu_caps(ff, &ff->ph->env.nr_cpu_pmu_caps,
3278 				     &ff->ph->env.cpu_pmu_caps,
3279 				     &ff->ph->env.max_branches);
3280 
3281 	if (!ret && !ff->ph->env.cpu_pmu_caps)
3282 		pr_debug("cpu pmu capabilities not available\n");
3283 	return ret;
3284 }
3285 
process_pmu_caps(struct feat_fd * ff,void * data __maybe_unused)3286 static int process_pmu_caps(struct feat_fd *ff, void *data __maybe_unused)
3287 {
3288 	struct pmu_caps *pmu_caps;
3289 	u32 nr_pmu, i;
3290 	int ret;
3291 	int j;
3292 
3293 	if (do_read_u32(ff, &nr_pmu))
3294 		return -1;
3295 
3296 	if (!nr_pmu) {
3297 		pr_debug("pmu capabilities not available\n");
3298 		return 0;
3299 	}
3300 
3301 	pmu_caps = zalloc(sizeof(*pmu_caps) * nr_pmu);
3302 	if (!pmu_caps)
3303 		return -ENOMEM;
3304 
3305 	for (i = 0; i < nr_pmu; i++) {
3306 		ret = __process_pmu_caps(ff, &pmu_caps[i].nr_caps,
3307 					 &pmu_caps[i].caps,
3308 					 &pmu_caps[i].max_branches);
3309 		if (ret)
3310 			goto err;
3311 
3312 		pmu_caps[i].pmu_name = do_read_string(ff);
3313 		if (!pmu_caps[i].pmu_name) {
3314 			ret = -1;
3315 			goto err;
3316 		}
3317 		if (!pmu_caps[i].nr_caps) {
3318 			pr_debug("%s pmu capabilities not available\n",
3319 				 pmu_caps[i].pmu_name);
3320 		}
3321 	}
3322 
3323 	ff->ph->env.nr_pmus_with_caps = nr_pmu;
3324 	ff->ph->env.pmu_caps = pmu_caps;
3325 	return 0;
3326 
3327 err:
3328 	for (i = 0; i < nr_pmu; i++) {
3329 		for (j = 0; j < pmu_caps[i].nr_caps; j++)
3330 			free(pmu_caps[i].caps[j]);
3331 		free(pmu_caps[i].caps);
3332 		free(pmu_caps[i].pmu_name);
3333 	}
3334 
3335 	free(pmu_caps);
3336 	return ret;
3337 }
3338 
3339 #define FEAT_OPR(n, func, __full_only) \
3340 	[HEADER_##n] = {					\
3341 		.name	    = __stringify(n),			\
3342 		.write	    = write_##func,			\
3343 		.print	    = print_##func,			\
3344 		.full_only  = __full_only,			\
3345 		.process    = process_##func,			\
3346 		.synthesize = true				\
3347 	}
3348 
3349 #define FEAT_OPN(n, func, __full_only) \
3350 	[HEADER_##n] = {					\
3351 		.name	    = __stringify(n),			\
3352 		.write	    = write_##func,			\
3353 		.print	    = print_##func,			\
3354 		.full_only  = __full_only,			\
3355 		.process    = process_##func			\
3356 	}
3357 
3358 /* feature_ops not implemented: */
3359 #define print_tracing_data	NULL
3360 #define print_build_id		NULL
3361 
3362 #define process_branch_stack	NULL
3363 #define process_stat		NULL
3364 
3365 // Only used in util/synthetic-events.c
3366 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE];
3367 
3368 const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE] = {
3369 	FEAT_OPN(TRACING_DATA,	tracing_data,	false),
3370 	FEAT_OPN(BUILD_ID,	build_id,	false),
3371 	FEAT_OPR(HOSTNAME,	hostname,	false),
3372 	FEAT_OPR(OSRELEASE,	osrelease,	false),
3373 	FEAT_OPR(VERSION,	version,	false),
3374 	FEAT_OPR(ARCH,		arch,		false),
3375 	FEAT_OPR(NRCPUS,	nrcpus,		false),
3376 	FEAT_OPR(CPUDESC,	cpudesc,	false),
3377 	FEAT_OPR(CPUID,		cpuid,		false),
3378 	FEAT_OPR(TOTAL_MEM,	total_mem,	false),
3379 	FEAT_OPR(EVENT_DESC,	event_desc,	false),
3380 	FEAT_OPR(CMDLINE,	cmdline,	false),
3381 	FEAT_OPR(CPU_TOPOLOGY,	cpu_topology,	true),
3382 	FEAT_OPR(NUMA_TOPOLOGY,	numa_topology,	true),
3383 	FEAT_OPN(BRANCH_STACK,	branch_stack,	false),
3384 	FEAT_OPR(PMU_MAPPINGS,	pmu_mappings,	false),
3385 	FEAT_OPR(GROUP_DESC,	group_desc,	false),
3386 	FEAT_OPN(AUXTRACE,	auxtrace,	false),
3387 	FEAT_OPN(STAT,		stat,		false),
3388 	FEAT_OPN(CACHE,		cache,		true),
3389 	FEAT_OPR(SAMPLE_TIME,	sample_time,	false),
3390 	FEAT_OPR(MEM_TOPOLOGY,	mem_topology,	true),
3391 	FEAT_OPR(CLOCKID,	clockid,	false),
3392 	FEAT_OPN(DIR_FORMAT,	dir_format,	false),
3393 #ifdef HAVE_LIBBPF_SUPPORT
3394 	FEAT_OPR(BPF_PROG_INFO, bpf_prog_info,  false),
3395 	FEAT_OPR(BPF_BTF,       bpf_btf,        false),
3396 #endif
3397 	FEAT_OPR(COMPRESSED,	compressed,	false),
3398 	FEAT_OPR(CPU_PMU_CAPS,	cpu_pmu_caps,	false),
3399 	FEAT_OPR(CLOCK_DATA,	clock_data,	false),
3400 	FEAT_OPN(HYBRID_TOPOLOGY,	hybrid_topology,	true),
3401 	FEAT_OPR(PMU_CAPS,	pmu_caps,	false),
3402 };
3403 
3404 struct header_print_data {
3405 	FILE *fp;
3406 	bool full; /* extended list of headers */
3407 };
3408 
perf_file_section__fprintf_info(struct perf_file_section * section,struct perf_header * ph,int feat,int fd,void * data)3409 static int perf_file_section__fprintf_info(struct perf_file_section *section,
3410 					   struct perf_header *ph,
3411 					   int feat, int fd, void *data)
3412 {
3413 	struct header_print_data *hd = data;
3414 	struct feat_fd ff;
3415 
3416 	if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
3417 		pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
3418 				"%d, continuing...\n", section->offset, feat);
3419 		return 0;
3420 	}
3421 	if (feat >= HEADER_LAST_FEATURE) {
3422 		pr_warning("unknown feature %d\n", feat);
3423 		return 0;
3424 	}
3425 	if (!feat_ops[feat].print)
3426 		return 0;
3427 
3428 	ff = (struct  feat_fd) {
3429 		.fd = fd,
3430 		.ph = ph,
3431 	};
3432 
3433 	if (!feat_ops[feat].full_only || hd->full)
3434 		feat_ops[feat].print(&ff, hd->fp);
3435 	else
3436 		fprintf(hd->fp, "# %s info available, use -I to display\n",
3437 			feat_ops[feat].name);
3438 
3439 	return 0;
3440 }
3441 
perf_header__fprintf_info(struct perf_session * session,FILE * fp,bool full)3442 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
3443 {
3444 	struct header_print_data hd;
3445 	struct perf_header *header = &session->header;
3446 	int fd = perf_data__fd(session->data);
3447 	struct stat st;
3448 	time_t stctime;
3449 	int ret, bit;
3450 
3451 	hd.fp = fp;
3452 	hd.full = full;
3453 
3454 	ret = fstat(fd, &st);
3455 	if (ret == -1)
3456 		return -1;
3457 
3458 	stctime = st.st_mtime;
3459 	fprintf(fp, "# captured on    : %s", ctime(&stctime));
3460 
3461 	fprintf(fp, "# header version : %u\n", header->version);
3462 	fprintf(fp, "# data offset    : %" PRIu64 "\n", header->data_offset);
3463 	fprintf(fp, "# data size      : %" PRIu64 "\n", header->data_size);
3464 	fprintf(fp, "# feat offset    : %" PRIu64 "\n", header->feat_offset);
3465 
3466 	perf_header__process_sections(header, fd, &hd,
3467 				      perf_file_section__fprintf_info);
3468 
3469 	if (session->data->is_pipe)
3470 		return 0;
3471 
3472 	fprintf(fp, "# missing features: ");
3473 	for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
3474 		if (bit)
3475 			fprintf(fp, "%s ", feat_ops[bit].name);
3476 	}
3477 
3478 	fprintf(fp, "\n");
3479 	return 0;
3480 }
3481 
3482 struct header_fw {
3483 	struct feat_writer	fw;
3484 	struct feat_fd		*ff;
3485 };
3486 
feat_writer_cb(struct feat_writer * fw,void * buf,size_t sz)3487 static int feat_writer_cb(struct feat_writer *fw, void *buf, size_t sz)
3488 {
3489 	struct header_fw *h = container_of(fw, struct header_fw, fw);
3490 
3491 	return do_write(h->ff, buf, sz);
3492 }
3493 
do_write_feat(struct feat_fd * ff,int type,struct perf_file_section ** p,struct evlist * evlist,struct feat_copier * fc)3494 static int do_write_feat(struct feat_fd *ff, int type,
3495 			 struct perf_file_section **p,
3496 			 struct evlist *evlist,
3497 			 struct feat_copier *fc)
3498 {
3499 	int err;
3500 	int ret = 0;
3501 
3502 	if (perf_header__has_feat(ff->ph, type)) {
3503 		if (!feat_ops[type].write)
3504 			return -1;
3505 
3506 		if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
3507 			return -1;
3508 
3509 		(*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
3510 
3511 		/*
3512 		 * Hook to let perf inject copy features sections from the input
3513 		 * file.
3514 		 */
3515 		if (fc && fc->copy) {
3516 			struct header_fw h = {
3517 				.fw.write = feat_writer_cb,
3518 				.ff = ff,
3519 			};
3520 
3521 			/* ->copy() returns 0 if the feature was not copied */
3522 			err = fc->copy(fc, type, &h.fw);
3523 		} else {
3524 			err = 0;
3525 		}
3526 		if (!err)
3527 			err = feat_ops[type].write(ff, evlist);
3528 		if (err < 0) {
3529 			pr_debug("failed to write feature %s\n", feat_ops[type].name);
3530 
3531 			/* undo anything written */
3532 			lseek(ff->fd, (*p)->offset, SEEK_SET);
3533 
3534 			return -1;
3535 		}
3536 		(*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
3537 		(*p)++;
3538 	}
3539 	return ret;
3540 }
3541 
perf_header__adds_write(struct perf_header * header,struct evlist * evlist,int fd,struct feat_copier * fc)3542 static int perf_header__adds_write(struct perf_header *header,
3543 				   struct evlist *evlist, int fd,
3544 				   struct feat_copier *fc)
3545 {
3546 	int nr_sections;
3547 	struct feat_fd ff;
3548 	struct perf_file_section *feat_sec, *p;
3549 	int sec_size;
3550 	u64 sec_start;
3551 	int feat;
3552 	int err;
3553 
3554 	ff = (struct feat_fd){
3555 		.fd  = fd,
3556 		.ph = header,
3557 	};
3558 
3559 	nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
3560 	if (!nr_sections)
3561 		return 0;
3562 
3563 	feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
3564 	if (feat_sec == NULL)
3565 		return -ENOMEM;
3566 
3567 	sec_size = sizeof(*feat_sec) * nr_sections;
3568 
3569 	sec_start = header->feat_offset;
3570 	lseek(fd, sec_start + sec_size, SEEK_SET);
3571 
3572 	for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3573 		if (do_write_feat(&ff, feat, &p, evlist, fc))
3574 			perf_header__clear_feat(header, feat);
3575 	}
3576 
3577 	lseek(fd, sec_start, SEEK_SET);
3578 	/*
3579 	 * may write more than needed due to dropped feature, but
3580 	 * this is okay, reader will skip the missing entries
3581 	 */
3582 	err = do_write(&ff, feat_sec, sec_size);
3583 	if (err < 0)
3584 		pr_debug("failed to write feature section\n");
3585 	free(feat_sec);
3586 	return err;
3587 }
3588 
perf_header__write_pipe(int fd)3589 int perf_header__write_pipe(int fd)
3590 {
3591 	struct perf_pipe_file_header f_header;
3592 	struct feat_fd ff;
3593 	int err;
3594 
3595 	ff = (struct feat_fd){ .fd = fd };
3596 
3597 	f_header = (struct perf_pipe_file_header){
3598 		.magic	   = PERF_MAGIC,
3599 		.size	   = sizeof(f_header),
3600 	};
3601 
3602 	err = do_write(&ff, &f_header, sizeof(f_header));
3603 	if (err < 0) {
3604 		pr_debug("failed to write perf pipe header\n");
3605 		return err;
3606 	}
3607 
3608 	return 0;
3609 }
3610 
perf_session__do_write_header(struct perf_session * session,struct evlist * evlist,int fd,bool at_exit,struct feat_copier * fc)3611 static int perf_session__do_write_header(struct perf_session *session,
3612 					 struct evlist *evlist,
3613 					 int fd, bool at_exit,
3614 					 struct feat_copier *fc)
3615 {
3616 	struct perf_file_header f_header;
3617 	struct perf_file_attr   f_attr;
3618 	struct perf_header *header = &session->header;
3619 	struct evsel *evsel;
3620 	struct feat_fd ff;
3621 	u64 attr_offset;
3622 	int err;
3623 
3624 	ff = (struct feat_fd){ .fd = fd};
3625 	lseek(fd, sizeof(f_header), SEEK_SET);
3626 
3627 	evlist__for_each_entry(session->evlist, evsel) {
3628 		evsel->id_offset = lseek(fd, 0, SEEK_CUR);
3629 		err = do_write(&ff, evsel->core.id, evsel->core.ids * sizeof(u64));
3630 		if (err < 0) {
3631 			pr_debug("failed to write perf header\n");
3632 			return err;
3633 		}
3634 	}
3635 
3636 	attr_offset = lseek(ff.fd, 0, SEEK_CUR);
3637 
3638 	evlist__for_each_entry(evlist, evsel) {
3639 		if (evsel->core.attr.size < sizeof(evsel->core.attr)) {
3640 			/*
3641 			 * We are likely in "perf inject" and have read
3642 			 * from an older file. Update attr size so that
3643 			 * reader gets the right offset to the ids.
3644 			 */
3645 			evsel->core.attr.size = sizeof(evsel->core.attr);
3646 		}
3647 		f_attr = (struct perf_file_attr){
3648 			.attr = evsel->core.attr,
3649 			.ids  = {
3650 				.offset = evsel->id_offset,
3651 				.size   = evsel->core.ids * sizeof(u64),
3652 			}
3653 		};
3654 		err = do_write(&ff, &f_attr, sizeof(f_attr));
3655 		if (err < 0) {
3656 			pr_debug("failed to write perf header attribute\n");
3657 			return err;
3658 		}
3659 	}
3660 
3661 	if (!header->data_offset)
3662 		header->data_offset = lseek(fd, 0, SEEK_CUR);
3663 	header->feat_offset = header->data_offset + header->data_size;
3664 
3665 	if (at_exit) {
3666 		err = perf_header__adds_write(header, evlist, fd, fc);
3667 		if (err < 0)
3668 			return err;
3669 	}
3670 
3671 	f_header = (struct perf_file_header){
3672 		.magic	   = PERF_MAGIC,
3673 		.size	   = sizeof(f_header),
3674 		.attr_size = sizeof(f_attr),
3675 		.attrs = {
3676 			.offset = attr_offset,
3677 			.size   = evlist->core.nr_entries * sizeof(f_attr),
3678 		},
3679 		.data = {
3680 			.offset = header->data_offset,
3681 			.size	= header->data_size,
3682 		},
3683 		/* event_types is ignored, store zeros */
3684 	};
3685 
3686 	memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
3687 
3688 	lseek(fd, 0, SEEK_SET);
3689 	err = do_write(&ff, &f_header, sizeof(f_header));
3690 	if (err < 0) {
3691 		pr_debug("failed to write perf header\n");
3692 		return err;
3693 	}
3694 	lseek(fd, header->data_offset + header->data_size, SEEK_SET);
3695 
3696 	return 0;
3697 }
3698 
perf_session__write_header(struct perf_session * session,struct evlist * evlist,int fd,bool at_exit)3699 int perf_session__write_header(struct perf_session *session,
3700 			       struct evlist *evlist,
3701 			       int fd, bool at_exit)
3702 {
3703 	return perf_session__do_write_header(session, evlist, fd, at_exit, NULL);
3704 }
3705 
perf_session__data_offset(const struct evlist * evlist)3706 size_t perf_session__data_offset(const struct evlist *evlist)
3707 {
3708 	struct evsel *evsel;
3709 	size_t data_offset;
3710 
3711 	data_offset = sizeof(struct perf_file_header);
3712 	evlist__for_each_entry(evlist, evsel) {
3713 		data_offset += evsel->core.ids * sizeof(u64);
3714 	}
3715 	data_offset += evlist->core.nr_entries * sizeof(struct perf_file_attr);
3716 
3717 	return data_offset;
3718 }
3719 
perf_session__inject_header(struct perf_session * session,struct evlist * evlist,int fd,struct feat_copier * fc)3720 int perf_session__inject_header(struct perf_session *session,
3721 				struct evlist *evlist,
3722 				int fd,
3723 				struct feat_copier *fc)
3724 {
3725 	return perf_session__do_write_header(session, evlist, fd, true, fc);
3726 }
3727 
perf_header__getbuffer64(struct perf_header * header,int fd,void * buf,size_t size)3728 static int perf_header__getbuffer64(struct perf_header *header,
3729 				    int fd, void *buf, size_t size)
3730 {
3731 	if (readn(fd, buf, size) <= 0)
3732 		return -1;
3733 
3734 	if (header->needs_swap)
3735 		mem_bswap_64(buf, size);
3736 
3737 	return 0;
3738 }
3739 
perf_header__process_sections(struct perf_header * header,int fd,void * data,int (* process)(struct perf_file_section * section,struct perf_header * ph,int feat,int fd,void * data))3740 int perf_header__process_sections(struct perf_header *header, int fd,
3741 				  void *data,
3742 				  int (*process)(struct perf_file_section *section,
3743 						 struct perf_header *ph,
3744 						 int feat, int fd, void *data))
3745 {
3746 	struct perf_file_section *feat_sec, *sec;
3747 	int nr_sections;
3748 	int sec_size;
3749 	int feat;
3750 	int err;
3751 
3752 	nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
3753 	if (!nr_sections)
3754 		return 0;
3755 
3756 	feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
3757 	if (!feat_sec)
3758 		return -1;
3759 
3760 	sec_size = sizeof(*feat_sec) * nr_sections;
3761 
3762 	lseek(fd, header->feat_offset, SEEK_SET);
3763 
3764 	err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
3765 	if (err < 0)
3766 		goto out_free;
3767 
3768 	for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
3769 		err = process(sec++, header, feat, fd, data);
3770 		if (err < 0)
3771 			goto out_free;
3772 	}
3773 	err = 0;
3774 out_free:
3775 	free(feat_sec);
3776 	return err;
3777 }
3778 
3779 static const int attr_file_abi_sizes[] = {
3780 	[0] = PERF_ATTR_SIZE_VER0,
3781 	[1] = PERF_ATTR_SIZE_VER1,
3782 	[2] = PERF_ATTR_SIZE_VER2,
3783 	[3] = PERF_ATTR_SIZE_VER3,
3784 	[4] = PERF_ATTR_SIZE_VER4,
3785 	0,
3786 };
3787 
3788 /*
3789  * In the legacy file format, the magic number is not used to encode endianness.
3790  * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
3791  * on ABI revisions, we need to try all combinations for all endianness to
3792  * detect the endianness.
3793  */
try_all_file_abis(uint64_t hdr_sz,struct perf_header * ph)3794 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
3795 {
3796 	uint64_t ref_size, attr_size;
3797 	int i;
3798 
3799 	for (i = 0 ; attr_file_abi_sizes[i]; i++) {
3800 		ref_size = attr_file_abi_sizes[i]
3801 			 + sizeof(struct perf_file_section);
3802 		if (hdr_sz != ref_size) {
3803 			attr_size = bswap_64(hdr_sz);
3804 			if (attr_size != ref_size)
3805 				continue;
3806 
3807 			ph->needs_swap = true;
3808 		}
3809 		pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
3810 			 i,
3811 			 ph->needs_swap);
3812 		return 0;
3813 	}
3814 	/* could not determine endianness */
3815 	return -1;
3816 }
3817 
3818 #define PERF_PIPE_HDR_VER0	16
3819 
3820 static const size_t attr_pipe_abi_sizes[] = {
3821 	[0] = PERF_PIPE_HDR_VER0,
3822 	0,
3823 };
3824 
3825 /*
3826  * In the legacy pipe format, there is an implicit assumption that endianness
3827  * between host recording the samples, and host parsing the samples is the
3828  * same. This is not always the case given that the pipe output may always be
3829  * redirected into a file and analyzed on a different machine with possibly a
3830  * different endianness and perf_event ABI revisions in the perf tool itself.
3831  */
try_all_pipe_abis(uint64_t hdr_sz,struct perf_header * ph)3832 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
3833 {
3834 	u64 attr_size;
3835 	int i;
3836 
3837 	for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
3838 		if (hdr_sz != attr_pipe_abi_sizes[i]) {
3839 			attr_size = bswap_64(hdr_sz);
3840 			if (attr_size != hdr_sz)
3841 				continue;
3842 
3843 			ph->needs_swap = true;
3844 		}
3845 		pr_debug("Pipe ABI%d perf.data file detected\n", i);
3846 		return 0;
3847 	}
3848 	return -1;
3849 }
3850 
is_perf_magic(u64 magic)3851 bool is_perf_magic(u64 magic)
3852 {
3853 	if (!memcmp(&magic, __perf_magic1, sizeof(magic))
3854 		|| magic == __perf_magic2
3855 		|| magic == __perf_magic2_sw)
3856 		return true;
3857 
3858 	return false;
3859 }
3860 
check_magic_endian(u64 magic,uint64_t hdr_sz,bool is_pipe,struct perf_header * ph)3861 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
3862 			      bool is_pipe, struct perf_header *ph)
3863 {
3864 	int ret;
3865 
3866 	/* check for legacy format */
3867 	ret = memcmp(&magic, __perf_magic1, sizeof(magic));
3868 	if (ret == 0) {
3869 		ph->version = PERF_HEADER_VERSION_1;
3870 		pr_debug("legacy perf.data format\n");
3871 		if (is_pipe)
3872 			return try_all_pipe_abis(hdr_sz, ph);
3873 
3874 		return try_all_file_abis(hdr_sz, ph);
3875 	}
3876 	/*
3877 	 * the new magic number serves two purposes:
3878 	 * - unique number to identify actual perf.data files
3879 	 * - encode endianness of file
3880 	 */
3881 	ph->version = PERF_HEADER_VERSION_2;
3882 
3883 	/* check magic number with one endianness */
3884 	if (magic == __perf_magic2)
3885 		return 0;
3886 
3887 	/* check magic number with opposite endianness */
3888 	if (magic != __perf_magic2_sw)
3889 		return -1;
3890 
3891 	ph->needs_swap = true;
3892 
3893 	return 0;
3894 }
3895 
perf_file_header__read(struct perf_file_header * header,struct perf_header * ph,int fd)3896 int perf_file_header__read(struct perf_file_header *header,
3897 			   struct perf_header *ph, int fd)
3898 {
3899 	ssize_t ret;
3900 
3901 	lseek(fd, 0, SEEK_SET);
3902 
3903 	ret = readn(fd, header, sizeof(*header));
3904 	if (ret <= 0)
3905 		return -1;
3906 
3907 	if (check_magic_endian(header->magic,
3908 			       header->attr_size, false, ph) < 0) {
3909 		pr_debug("magic/endian check failed\n");
3910 		return -1;
3911 	}
3912 
3913 	if (ph->needs_swap) {
3914 		mem_bswap_64(header, offsetof(struct perf_file_header,
3915 			     adds_features));
3916 	}
3917 
3918 	if (header->size != sizeof(*header)) {
3919 		/* Support the previous format */
3920 		if (header->size == offsetof(typeof(*header), adds_features))
3921 			bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3922 		else
3923 			return -1;
3924 	} else if (ph->needs_swap) {
3925 		/*
3926 		 * feature bitmap is declared as an array of unsigned longs --
3927 		 * not good since its size can differ between the host that
3928 		 * generated the data file and the host analyzing the file.
3929 		 *
3930 		 * We need to handle endianness, but we don't know the size of
3931 		 * the unsigned long where the file was generated. Take a best
3932 		 * guess at determining it: try 64-bit swap first (ie., file
3933 		 * created on a 64-bit host), and check if the hostname feature
3934 		 * bit is set (this feature bit is forced on as of fbe96f2).
3935 		 * If the bit is not, undo the 64-bit swap and try a 32-bit
3936 		 * swap. If the hostname bit is still not set (e.g., older data
3937 		 * file), punt and fallback to the original behavior --
3938 		 * clearing all feature bits and setting buildid.
3939 		 */
3940 		mem_bswap_64(&header->adds_features,
3941 			    BITS_TO_U64(HEADER_FEAT_BITS));
3942 
3943 		if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3944 			/* unswap as u64 */
3945 			mem_bswap_64(&header->adds_features,
3946 				    BITS_TO_U64(HEADER_FEAT_BITS));
3947 
3948 			/* unswap as u32 */
3949 			mem_bswap_32(&header->adds_features,
3950 				    BITS_TO_U32(HEADER_FEAT_BITS));
3951 		}
3952 
3953 		if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3954 			bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3955 			set_bit(HEADER_BUILD_ID, header->adds_features);
3956 		}
3957 	}
3958 
3959 	memcpy(&ph->adds_features, &header->adds_features,
3960 	       sizeof(ph->adds_features));
3961 
3962 	ph->data_offset  = header->data.offset;
3963 	ph->data_size	 = header->data.size;
3964 	ph->feat_offset  = header->data.offset + header->data.size;
3965 	return 0;
3966 }
3967 
perf_file_section__process(struct perf_file_section * section,struct perf_header * ph,int feat,int fd,void * data)3968 static int perf_file_section__process(struct perf_file_section *section,
3969 				      struct perf_header *ph,
3970 				      int feat, int fd, void *data)
3971 {
3972 	struct feat_fd fdd = {
3973 		.fd	= fd,
3974 		.ph	= ph,
3975 		.size	= section->size,
3976 		.offset	= section->offset,
3977 	};
3978 
3979 	if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
3980 		pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
3981 			  "%d, continuing...\n", section->offset, feat);
3982 		return 0;
3983 	}
3984 
3985 	if (feat >= HEADER_LAST_FEATURE) {
3986 		pr_debug("unknown feature %d, continuing...\n", feat);
3987 		return 0;
3988 	}
3989 
3990 	if (!feat_ops[feat].process)
3991 		return 0;
3992 
3993 	return feat_ops[feat].process(&fdd, data);
3994 }
3995 
perf_file_header__read_pipe(struct perf_pipe_file_header * header,struct perf_header * ph,struct perf_data * data,bool repipe,int repipe_fd)3996 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
3997 				       struct perf_header *ph,
3998 				       struct perf_data* data,
3999 				       bool repipe, int repipe_fd)
4000 {
4001 	struct feat_fd ff = {
4002 		.fd = repipe_fd,
4003 		.ph = ph,
4004 	};
4005 	ssize_t ret;
4006 
4007 	ret = perf_data__read(data, header, sizeof(*header));
4008 	if (ret <= 0)
4009 		return -1;
4010 
4011 	if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
4012 		pr_debug("endian/magic failed\n");
4013 		return -1;
4014 	}
4015 
4016 	if (ph->needs_swap)
4017 		header->size = bswap_64(header->size);
4018 
4019 	if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
4020 		return -1;
4021 
4022 	return 0;
4023 }
4024 
perf_header__read_pipe(struct perf_session * session,int repipe_fd)4025 static int perf_header__read_pipe(struct perf_session *session, int repipe_fd)
4026 {
4027 	struct perf_header *header = &session->header;
4028 	struct perf_pipe_file_header f_header;
4029 
4030 	if (perf_file_header__read_pipe(&f_header, header, session->data,
4031 					session->repipe, repipe_fd) < 0) {
4032 		pr_debug("incompatible file format\n");
4033 		return -EINVAL;
4034 	}
4035 
4036 	return f_header.size == sizeof(f_header) ? 0 : -1;
4037 }
4038 
read_attr(int fd,struct perf_header * ph,struct perf_file_attr * f_attr)4039 static int read_attr(int fd, struct perf_header *ph,
4040 		     struct perf_file_attr *f_attr)
4041 {
4042 	struct perf_event_attr *attr = &f_attr->attr;
4043 	size_t sz, left;
4044 	size_t our_sz = sizeof(f_attr->attr);
4045 	ssize_t ret;
4046 
4047 	memset(f_attr, 0, sizeof(*f_attr));
4048 
4049 	/* read minimal guaranteed structure */
4050 	ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
4051 	if (ret <= 0) {
4052 		pr_debug("cannot read %d bytes of header attr\n",
4053 			 PERF_ATTR_SIZE_VER0);
4054 		return -1;
4055 	}
4056 
4057 	/* on file perf_event_attr size */
4058 	sz = attr->size;
4059 
4060 	if (ph->needs_swap)
4061 		sz = bswap_32(sz);
4062 
4063 	if (sz == 0) {
4064 		/* assume ABI0 */
4065 		sz =  PERF_ATTR_SIZE_VER0;
4066 	} else if (sz > our_sz) {
4067 		pr_debug("file uses a more recent and unsupported ABI"
4068 			 " (%zu bytes extra)\n", sz - our_sz);
4069 		return -1;
4070 	}
4071 	/* what we have not yet read and that we know about */
4072 	left = sz - PERF_ATTR_SIZE_VER0;
4073 	if (left) {
4074 		void *ptr = attr;
4075 		ptr += PERF_ATTR_SIZE_VER0;
4076 
4077 		ret = readn(fd, ptr, left);
4078 	}
4079 	/* read perf_file_section, ids are read in caller */
4080 	ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
4081 
4082 	return ret <= 0 ? -1 : 0;
4083 }
4084 
evsel__prepare_tracepoint_event(struct evsel * evsel,struct tep_handle * pevent)4085 static int evsel__prepare_tracepoint_event(struct evsel *evsel, struct tep_handle *pevent)
4086 {
4087 	struct tep_event *event;
4088 	char bf[128];
4089 
4090 	/* already prepared */
4091 	if (evsel->tp_format)
4092 		return 0;
4093 
4094 	if (pevent == NULL) {
4095 		pr_debug("broken or missing trace data\n");
4096 		return -1;
4097 	}
4098 
4099 	event = tep_find_event(pevent, evsel->core.attr.config);
4100 	if (event == NULL) {
4101 		pr_debug("cannot find event format for %d\n", (int)evsel->core.attr.config);
4102 		return -1;
4103 	}
4104 
4105 	if (!evsel->name) {
4106 		snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
4107 		evsel->name = strdup(bf);
4108 		if (evsel->name == NULL)
4109 			return -1;
4110 	}
4111 
4112 	evsel->tp_format = event;
4113 	return 0;
4114 }
4115 
evlist__prepare_tracepoint_events(struct evlist * evlist,struct tep_handle * pevent)4116 static int evlist__prepare_tracepoint_events(struct evlist *evlist, struct tep_handle *pevent)
4117 {
4118 	struct evsel *pos;
4119 
4120 	evlist__for_each_entry(evlist, pos) {
4121 		if (pos->core.attr.type == PERF_TYPE_TRACEPOINT &&
4122 		    evsel__prepare_tracepoint_event(pos, pevent))
4123 			return -1;
4124 	}
4125 
4126 	return 0;
4127 }
4128 
perf_session__read_header(struct perf_session * session,int repipe_fd)4129 int perf_session__read_header(struct perf_session *session, int repipe_fd)
4130 {
4131 	struct perf_data *data = session->data;
4132 	struct perf_header *header = &session->header;
4133 	struct perf_file_header	f_header;
4134 	struct perf_file_attr	f_attr;
4135 	u64			f_id;
4136 	int nr_attrs, nr_ids, i, j, err;
4137 	int fd = perf_data__fd(data);
4138 
4139 	session->evlist = evlist__new();
4140 	if (session->evlist == NULL)
4141 		return -ENOMEM;
4142 
4143 	session->evlist->env = &header->env;
4144 	session->machines.host.env = &header->env;
4145 
4146 	/*
4147 	 * We can read 'pipe' data event from regular file,
4148 	 * check for the pipe header regardless of source.
4149 	 */
4150 	err = perf_header__read_pipe(session, repipe_fd);
4151 	if (!err || perf_data__is_pipe(data)) {
4152 		data->is_pipe = true;
4153 		return err;
4154 	}
4155 
4156 	if (perf_file_header__read(&f_header, header, fd) < 0)
4157 		return -EINVAL;
4158 
4159 	if (header->needs_swap && data->in_place_update) {
4160 		pr_err("In-place update not supported when byte-swapping is required\n");
4161 		return -EINVAL;
4162 	}
4163 
4164 	/*
4165 	 * Sanity check that perf.data was written cleanly; data size is
4166 	 * initialized to 0 and updated only if the on_exit function is run.
4167 	 * If data size is still 0 then the file contains only partial
4168 	 * information.  Just warn user and process it as much as it can.
4169 	 */
4170 	if (f_header.data.size == 0) {
4171 		pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
4172 			   "Was the 'perf record' command properly terminated?\n",
4173 			   data->file.path);
4174 	}
4175 
4176 	if (f_header.attr_size == 0) {
4177 		pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n"
4178 		       "Was the 'perf record' command properly terminated?\n",
4179 		       data->file.path);
4180 		return -EINVAL;
4181 	}
4182 
4183 	nr_attrs = f_header.attrs.size / f_header.attr_size;
4184 	lseek(fd, f_header.attrs.offset, SEEK_SET);
4185 
4186 	for (i = 0; i < nr_attrs; i++) {
4187 		struct evsel *evsel;
4188 		off_t tmp;
4189 
4190 		if (read_attr(fd, header, &f_attr) < 0)
4191 			goto out_errno;
4192 
4193 		if (header->needs_swap) {
4194 			f_attr.ids.size   = bswap_64(f_attr.ids.size);
4195 			f_attr.ids.offset = bswap_64(f_attr.ids.offset);
4196 			perf_event__attr_swap(&f_attr.attr);
4197 		}
4198 
4199 		tmp = lseek(fd, 0, SEEK_CUR);
4200 		evsel = evsel__new(&f_attr.attr);
4201 
4202 		if (evsel == NULL)
4203 			goto out_delete_evlist;
4204 
4205 		evsel->needs_swap = header->needs_swap;
4206 		/*
4207 		 * Do it before so that if perf_evsel__alloc_id fails, this
4208 		 * entry gets purged too at evlist__delete().
4209 		 */
4210 		evlist__add(session->evlist, evsel);
4211 
4212 		nr_ids = f_attr.ids.size / sizeof(u64);
4213 		/*
4214 		 * We don't have the cpu and thread maps on the header, so
4215 		 * for allocating the perf_sample_id table we fake 1 cpu and
4216 		 * hattr->ids threads.
4217 		 */
4218 		if (perf_evsel__alloc_id(&evsel->core, 1, nr_ids))
4219 			goto out_delete_evlist;
4220 
4221 		lseek(fd, f_attr.ids.offset, SEEK_SET);
4222 
4223 		for (j = 0; j < nr_ids; j++) {
4224 			if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
4225 				goto out_errno;
4226 
4227 			perf_evlist__id_add(&session->evlist->core, &evsel->core, 0, j, f_id);
4228 		}
4229 
4230 		lseek(fd, tmp, SEEK_SET);
4231 	}
4232 
4233 	perf_header__process_sections(header, fd, &session->tevent,
4234 				      perf_file_section__process);
4235 
4236 	if (evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent))
4237 		goto out_delete_evlist;
4238 
4239 	return 0;
4240 out_errno:
4241 	return -errno;
4242 
4243 out_delete_evlist:
4244 	evlist__delete(session->evlist);
4245 	session->evlist = NULL;
4246 	return -ENOMEM;
4247 }
4248 
perf_event__process_feature(struct perf_session * session,union perf_event * event)4249 int perf_event__process_feature(struct perf_session *session,
4250 				union perf_event *event)
4251 {
4252 	struct perf_tool *tool = session->tool;
4253 	struct feat_fd ff = { .fd = 0 };
4254 	struct perf_record_header_feature *fe = (struct perf_record_header_feature *)event;
4255 	int type = fe->header.type;
4256 	u64 feat = fe->feat_id;
4257 	int ret = 0;
4258 
4259 	if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
4260 		pr_warning("invalid record type %d in pipe-mode\n", type);
4261 		return 0;
4262 	}
4263 	if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
4264 		pr_warning("invalid record type %d in pipe-mode\n", type);
4265 		return -1;
4266 	}
4267 
4268 	if (!feat_ops[feat].process)
4269 		return 0;
4270 
4271 	ff.buf  = (void *)fe->data;
4272 	ff.size = event->header.size - sizeof(*fe);
4273 	ff.ph = &session->header;
4274 
4275 	if (feat_ops[feat].process(&ff, NULL)) {
4276 		ret = -1;
4277 		goto out;
4278 	}
4279 
4280 	if (!feat_ops[feat].print || !tool->show_feat_hdr)
4281 		goto out;
4282 
4283 	if (!feat_ops[feat].full_only ||
4284 	    tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
4285 		feat_ops[feat].print(&ff, stdout);
4286 	} else {
4287 		fprintf(stdout, "# %s info available, use -I to display\n",
4288 			feat_ops[feat].name);
4289 	}
4290 out:
4291 	free_event_desc(ff.events);
4292 	return ret;
4293 }
4294 
perf_event__fprintf_event_update(union perf_event * event,FILE * fp)4295 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
4296 {
4297 	struct perf_record_event_update *ev = &event->event_update;
4298 	struct perf_cpu_map *map;
4299 	size_t ret;
4300 
4301 	ret = fprintf(fp, "\n... id:    %" PRI_lu64 "\n", ev->id);
4302 
4303 	switch (ev->type) {
4304 	case PERF_EVENT_UPDATE__SCALE:
4305 		ret += fprintf(fp, "... scale: %f\n", ev->scale.scale);
4306 		break;
4307 	case PERF_EVENT_UPDATE__UNIT:
4308 		ret += fprintf(fp, "... unit:  %s\n", ev->unit);
4309 		break;
4310 	case PERF_EVENT_UPDATE__NAME:
4311 		ret += fprintf(fp, "... name:  %s\n", ev->name);
4312 		break;
4313 	case PERF_EVENT_UPDATE__CPUS:
4314 		ret += fprintf(fp, "... ");
4315 
4316 		map = cpu_map__new_data(&ev->cpus.cpus);
4317 		if (map)
4318 			ret += cpu_map__fprintf(map, fp);
4319 		else
4320 			ret += fprintf(fp, "failed to get cpus\n");
4321 		break;
4322 	default:
4323 		ret += fprintf(fp, "... unknown type\n");
4324 		break;
4325 	}
4326 
4327 	return ret;
4328 }
4329 
perf_event__process_attr(struct perf_tool * tool __maybe_unused,union perf_event * event,struct evlist ** pevlist)4330 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
4331 			     union perf_event *event,
4332 			     struct evlist **pevlist)
4333 {
4334 	u32 i, ids, n_ids;
4335 	struct evsel *evsel;
4336 	struct evlist *evlist = *pevlist;
4337 
4338 	if (evlist == NULL) {
4339 		*pevlist = evlist = evlist__new();
4340 		if (evlist == NULL)
4341 			return -ENOMEM;
4342 	}
4343 
4344 	evsel = evsel__new(&event->attr.attr);
4345 	if (evsel == NULL)
4346 		return -ENOMEM;
4347 
4348 	evlist__add(evlist, evsel);
4349 
4350 	ids = event->header.size;
4351 	ids -= (void *)&event->attr.id - (void *)event;
4352 	n_ids = ids / sizeof(u64);
4353 	/*
4354 	 * We don't have the cpu and thread maps on the header, so
4355 	 * for allocating the perf_sample_id table we fake 1 cpu and
4356 	 * hattr->ids threads.
4357 	 */
4358 	if (perf_evsel__alloc_id(&evsel->core, 1, n_ids))
4359 		return -ENOMEM;
4360 
4361 	for (i = 0; i < n_ids; i++) {
4362 		perf_evlist__id_add(&evlist->core, &evsel->core, 0, i, event->attr.id[i]);
4363 	}
4364 
4365 	return 0;
4366 }
4367 
perf_event__process_event_update(struct perf_tool * tool __maybe_unused,union perf_event * event,struct evlist ** pevlist)4368 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
4369 				     union perf_event *event,
4370 				     struct evlist **pevlist)
4371 {
4372 	struct perf_record_event_update *ev = &event->event_update;
4373 	struct evlist *evlist;
4374 	struct evsel *evsel;
4375 	struct perf_cpu_map *map;
4376 
4377 	if (dump_trace)
4378 		perf_event__fprintf_event_update(event, stdout);
4379 
4380 	if (!pevlist || *pevlist == NULL)
4381 		return -EINVAL;
4382 
4383 	evlist = *pevlist;
4384 
4385 	evsel = evlist__id2evsel(evlist, ev->id);
4386 	if (evsel == NULL)
4387 		return -EINVAL;
4388 
4389 	switch (ev->type) {
4390 	case PERF_EVENT_UPDATE__UNIT:
4391 		free((char *)evsel->unit);
4392 		evsel->unit = strdup(ev->unit);
4393 		break;
4394 	case PERF_EVENT_UPDATE__NAME:
4395 		free(evsel->name);
4396 		evsel->name = strdup(ev->name);
4397 		break;
4398 	case PERF_EVENT_UPDATE__SCALE:
4399 		evsel->scale = ev->scale.scale;
4400 		break;
4401 	case PERF_EVENT_UPDATE__CPUS:
4402 		map = cpu_map__new_data(&ev->cpus.cpus);
4403 		if (map) {
4404 			perf_cpu_map__put(evsel->core.own_cpus);
4405 			evsel->core.own_cpus = map;
4406 		} else
4407 			pr_err("failed to get event_update cpus\n");
4408 	default:
4409 		break;
4410 	}
4411 
4412 	return 0;
4413 }
4414 
perf_event__process_tracing_data(struct perf_session * session,union perf_event * event)4415 int perf_event__process_tracing_data(struct perf_session *session,
4416 				     union perf_event *event)
4417 {
4418 	ssize_t size_read, padding, size = event->tracing_data.size;
4419 	int fd = perf_data__fd(session->data);
4420 	char buf[BUFSIZ];
4421 
4422 	/*
4423 	 * The pipe fd is already in proper place and in any case
4424 	 * we can't move it, and we'd screw the case where we read
4425 	 * 'pipe' data from regular file. The trace_report reads
4426 	 * data from 'fd' so we need to set it directly behind the
4427 	 * event, where the tracing data starts.
4428 	 */
4429 	if (!perf_data__is_pipe(session->data)) {
4430 		off_t offset = lseek(fd, 0, SEEK_CUR);
4431 
4432 		/* setup for reading amidst mmap */
4433 		lseek(fd, offset + sizeof(struct perf_record_header_tracing_data),
4434 		      SEEK_SET);
4435 	}
4436 
4437 	size_read = trace_report(fd, &session->tevent,
4438 				 session->repipe);
4439 	padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
4440 
4441 	if (readn(fd, buf, padding) < 0) {
4442 		pr_err("%s: reading input file", __func__);
4443 		return -1;
4444 	}
4445 	if (session->repipe) {
4446 		int retw = write(STDOUT_FILENO, buf, padding);
4447 		if (retw <= 0 || retw != padding) {
4448 			pr_err("%s: repiping tracing data padding", __func__);
4449 			return -1;
4450 		}
4451 	}
4452 
4453 	if (size_read + padding != size) {
4454 		pr_err("%s: tracing data size mismatch", __func__);
4455 		return -1;
4456 	}
4457 
4458 	evlist__prepare_tracepoint_events(session->evlist, session->tevent.pevent);
4459 
4460 	return size_read + padding;
4461 }
4462 
perf_event__process_build_id(struct perf_session * session,union perf_event * event)4463 int perf_event__process_build_id(struct perf_session *session,
4464 				 union perf_event *event)
4465 {
4466 	__event_process_build_id(&event->build_id,
4467 				 event->build_id.filename,
4468 				 session);
4469 	return 0;
4470 }
4471