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