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