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