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