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