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