e509a9dea00bdfc2934b5a1f0ea754cf27a81e2f
[linux-2.6.git] / tools / perf / util / header.c
1 #define _FILE_OFFSET_BITS 64
2
3 #include "util.h"
4 #include <sys/types.h>
5 #include <byteswap.h>
6 #include <unistd.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <linux/list.h>
10 #include <linux/kernel.h>
11 #include <linux/bitops.h>
12 #include <sys/utsname.h>
13
14 #include "evlist.h"
15 #include "evsel.h"
16 #include "header.h"
17 #include "../perf.h"
18 #include "trace-event.h"
19 #include "session.h"
20 #include "symbol.h"
21 #include "debug.h"
22 #include "cpumap.h"
23
24 static bool no_buildid_cache = false;
25
26 static int event_count;
27 static struct perf_trace_event_type *events;
28
29 static u32 header_argc;
30 static const char **header_argv;
31
32 int perf_header__push_event(u64 id, const char *name)
33 {
34         if (strlen(name) > MAX_EVENT_NAME)
35                 pr_warning("Event %s will be truncated\n", name);
36
37         if (!events) {
38                 events = malloc(sizeof(struct perf_trace_event_type));
39                 if (events == NULL)
40                         return -ENOMEM;
41         } else {
42                 struct perf_trace_event_type *nevents;
43
44                 nevents = realloc(events, (event_count + 1) * sizeof(*events));
45                 if (nevents == NULL)
46                         return -ENOMEM;
47                 events = nevents;
48         }
49         memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
50         events[event_count].event_id = id;
51         strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
52         event_count++;
53         return 0;
54 }
55
56 char *perf_header__find_event(u64 id)
57 {
58         int i;
59         for (i = 0 ; i < event_count; i++) {
60                 if (events[i].event_id == id)
61                         return events[i].name;
62         }
63         return NULL;
64 }
65
66 static const char *__perf_magic = "PERFFILE";
67
68 #define PERF_MAGIC      (*(u64 *)__perf_magic)
69
70 struct perf_file_attr {
71         struct perf_event_attr  attr;
72         struct perf_file_section        ids;
73 };
74
75 void perf_header__set_feat(struct perf_header *header, int feat)
76 {
77         set_bit(feat, header->adds_features);
78 }
79
80 void perf_header__clear_feat(struct perf_header *header, int feat)
81 {
82         clear_bit(feat, header->adds_features);
83 }
84
85 bool perf_header__has_feat(const struct perf_header *header, int feat)
86 {
87         return test_bit(feat, header->adds_features);
88 }
89
90 static int do_write(int fd, const void *buf, size_t size)
91 {
92         while (size) {
93                 int ret = write(fd, buf, size);
94
95                 if (ret < 0)
96                         return -errno;
97
98                 size -= ret;
99                 buf += ret;
100         }
101
102         return 0;
103 }
104
105 #define NAME_ALIGN 64
106
107 static int write_padded(int fd, const void *bf, size_t count,
108                         size_t count_aligned)
109 {
110         static const char zero_buf[NAME_ALIGN];
111         int err = do_write(fd, bf, count);
112
113         if (!err)
114                 err = do_write(fd, zero_buf, count_aligned - count);
115
116         return err;
117 }
118
119 static int do_write_string(int fd, const char *str)
120 {
121         u32 len, olen;
122         int ret;
123
124         olen = strlen(str) + 1;
125         len = ALIGN(olen, NAME_ALIGN);
126
127         /* write len, incl. \0 */
128         ret = do_write(fd, &len, sizeof(len));
129         if (ret < 0)
130                 return ret;
131
132         return write_padded(fd, str, olen, len);
133 }
134
135 static char *do_read_string(int fd, struct perf_header *ph)
136 {
137         ssize_t sz, ret;
138         u32 len;
139         char *buf;
140
141         sz = read(fd, &len, sizeof(len));
142         if (sz < (ssize_t)sizeof(len))
143                 return NULL;
144
145         if (ph->needs_swap)
146                 len = bswap_32(len);
147
148         buf = malloc(len);
149         if (!buf)
150                 return NULL;
151
152         ret = read(fd, buf, len);
153         if (ret == (ssize_t)len) {
154                 /*
155                  * strings are padded by zeroes
156                  * thus the actual strlen of buf
157                  * may be less than len
158                  */
159                 return buf;
160         }
161
162         free(buf);
163         return NULL;
164 }
165
166 int
167 perf_header__set_cmdline(int argc, const char **argv)
168 {
169         int i;
170
171         header_argc = (u32)argc;
172
173         /* do not include NULL termination */
174         header_argv = calloc(argc, sizeof(char *));
175         if (!header_argv)
176                 return -ENOMEM;
177
178         /*
179          * must copy argv contents because it gets moved
180          * around during option parsing
181          */
182         for (i = 0; i < argc ; i++)
183                 header_argv[i] = argv[i];
184
185         return 0;
186 }
187
188 #define dsos__for_each_with_build_id(pos, head) \
189         list_for_each_entry(pos, head, node)    \
190                 if (!pos->has_build_id)         \
191                         continue;               \
192                 else
193
194 static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
195                                 u16 misc, int fd)
196 {
197         struct dso *pos;
198
199         dsos__for_each_with_build_id(pos, head) {
200                 int err;
201                 struct build_id_event b;
202                 size_t len;
203
204                 if (!pos->hit)
205                         continue;
206                 len = pos->long_name_len + 1;
207                 len = ALIGN(len, NAME_ALIGN);
208                 memset(&b, 0, sizeof(b));
209                 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
210                 b.pid = pid;
211                 b.header.misc = misc;
212                 b.header.size = sizeof(b) + len;
213                 err = do_write(fd, &b, sizeof(b));
214                 if (err < 0)
215                         return err;
216                 err = write_padded(fd, pos->long_name,
217                                    pos->long_name_len + 1, len);
218                 if (err < 0)
219                         return err;
220         }
221
222         return 0;
223 }
224
225 static int machine__write_buildid_table(struct machine *machine, int fd)
226 {
227         int err;
228         u16 kmisc = PERF_RECORD_MISC_KERNEL,
229             umisc = PERF_RECORD_MISC_USER;
230
231         if (!machine__is_host(machine)) {
232                 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
233                 umisc = PERF_RECORD_MISC_GUEST_USER;
234         }
235
236         err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid,
237                                           kmisc, fd);
238         if (err == 0)
239                 err = __dsos__write_buildid_table(&machine->user_dsos,
240                                                   machine->pid, umisc, fd);
241         return err;
242 }
243
244 static int dsos__write_buildid_table(struct perf_header *header, int fd)
245 {
246         struct perf_session *session = container_of(header,
247                         struct perf_session, header);
248         struct rb_node *nd;
249         int err = machine__write_buildid_table(&session->host_machine, fd);
250
251         if (err)
252                 return err;
253
254         for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
255                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
256                 err = machine__write_buildid_table(pos, fd);
257                 if (err)
258                         break;
259         }
260         return err;
261 }
262
263 int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
264                           const char *name, bool is_kallsyms)
265 {
266         const size_t size = PATH_MAX;
267         char *realname, *filename = zalloc(size),
268              *linkname = zalloc(size), *targetname;
269         int len, err = -1;
270
271         if (is_kallsyms) {
272                 if (symbol_conf.kptr_restrict) {
273                         pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
274                         return 0;
275                 }
276                 realname = (char *)name;
277         } else
278                 realname = realpath(name, NULL);
279
280         if (realname == NULL || filename == NULL || linkname == NULL)
281                 goto out_free;
282
283         len = snprintf(filename, size, "%s%s%s",
284                        debugdir, is_kallsyms ? "/" : "", realname);
285         if (mkdir_p(filename, 0755))
286                 goto out_free;
287
288         snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id);
289
290         if (access(filename, F_OK)) {
291                 if (is_kallsyms) {
292                          if (copyfile("/proc/kallsyms", filename))
293                                 goto out_free;
294                 } else if (link(realname, filename) && copyfile(name, filename))
295                         goto out_free;
296         }
297
298         len = snprintf(linkname, size, "%s/.build-id/%.2s",
299                        debugdir, sbuild_id);
300
301         if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
302                 goto out_free;
303
304         snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
305         targetname = filename + strlen(debugdir) - 5;
306         memcpy(targetname, "../..", 5);
307
308         if (symlink(targetname, linkname) == 0)
309                 err = 0;
310 out_free:
311         if (!is_kallsyms)
312                 free(realname);
313         free(filename);
314         free(linkname);
315         return err;
316 }
317
318 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
319                                  const char *name, const char *debugdir,
320                                  bool is_kallsyms)
321 {
322         char sbuild_id[BUILD_ID_SIZE * 2 + 1];
323
324         build_id__sprintf(build_id, build_id_size, sbuild_id);
325
326         return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms);
327 }
328
329 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
330 {
331         const size_t size = PATH_MAX;
332         char *filename = zalloc(size),
333              *linkname = zalloc(size);
334         int err = -1;
335
336         if (filename == NULL || linkname == NULL)
337                 goto out_free;
338
339         snprintf(linkname, size, "%s/.build-id/%.2s/%s",
340                  debugdir, sbuild_id, sbuild_id + 2);
341
342         if (access(linkname, F_OK))
343                 goto out_free;
344
345         if (readlink(linkname, filename, size - 1) < 0)
346                 goto out_free;
347
348         if (unlink(linkname))
349                 goto out_free;
350
351         /*
352          * Since the link is relative, we must make it absolute:
353          */
354         snprintf(linkname, size, "%s/.build-id/%.2s/%s",
355                  debugdir, sbuild_id, filename);
356
357         if (unlink(linkname))
358                 goto out_free;
359
360         err = 0;
361 out_free:
362         free(filename);
363         free(linkname);
364         return err;
365 }
366
367 static int dso__cache_build_id(struct dso *dso, const char *debugdir)
368 {
369         bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
370
371         return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id),
372                                      dso->long_name, debugdir, is_kallsyms);
373 }
374
375 static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
376 {
377         struct dso *pos;
378         int err = 0;
379
380         dsos__for_each_with_build_id(pos, head)
381                 if (dso__cache_build_id(pos, debugdir))
382                         err = -1;
383
384         return err;
385 }
386
387 static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
388 {
389         int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir);
390         ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir);
391         return ret;
392 }
393
394 static int perf_session__cache_build_ids(struct perf_session *session)
395 {
396         struct rb_node *nd;
397         int ret;
398         char debugdir[PATH_MAX];
399
400         snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
401
402         if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
403                 return -1;
404
405         ret = machine__cache_build_ids(&session->host_machine, debugdir);
406
407         for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
408                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
409                 ret |= machine__cache_build_ids(pos, debugdir);
410         }
411         return ret ? -1 : 0;
412 }
413
414 static bool machine__read_build_ids(struct machine *machine, bool with_hits)
415 {
416         bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
417         ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
418         return ret;
419 }
420
421 static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
422 {
423         struct rb_node *nd;
424         bool ret = machine__read_build_ids(&session->host_machine, with_hits);
425
426         for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
427                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
428                 ret |= machine__read_build_ids(pos, with_hits);
429         }
430
431         return ret;
432 }
433
434 static int write_trace_info(int fd, struct perf_header *h __used,
435                             struct perf_evlist *evlist)
436 {
437         return read_tracing_data(fd, &evlist->entries);
438 }
439
440
441 static int write_build_id(int fd, struct perf_header *h,
442                           struct perf_evlist *evlist __used)
443 {
444         struct perf_session *session;
445         int err;
446
447         session = container_of(h, struct perf_session, header);
448
449         if (!perf_session__read_build_ids(session, true))
450                 return -1;
451
452         err = dsos__write_buildid_table(h, fd);
453         if (err < 0) {
454                 pr_debug("failed to write buildid table\n");
455                 return err;
456         }
457         if (!no_buildid_cache)
458                 perf_session__cache_build_ids(session);
459
460         return 0;
461 }
462
463 static int write_hostname(int fd, struct perf_header *h __used,
464                           struct perf_evlist *evlist __used)
465 {
466         struct utsname uts;
467         int ret;
468
469         ret = uname(&uts);
470         if (ret < 0)
471                 return -1;
472
473         return do_write_string(fd, uts.nodename);
474 }
475
476 static int write_osrelease(int fd, struct perf_header *h __used,
477                            struct perf_evlist *evlist __used)
478 {
479         struct utsname uts;
480         int ret;
481
482         ret = uname(&uts);
483         if (ret < 0)
484                 return -1;
485
486         return do_write_string(fd, uts.release);
487 }
488
489 static int write_arch(int fd, struct perf_header *h __used,
490                       struct perf_evlist *evlist __used)
491 {
492         struct utsname uts;
493         int ret;
494
495         ret = uname(&uts);
496         if (ret < 0)
497                 return -1;
498
499         return do_write_string(fd, uts.machine);
500 }
501
502 static int write_version(int fd, struct perf_header *h __used,
503                          struct perf_evlist *evlist __used)
504 {
505         return do_write_string(fd, perf_version_string);
506 }
507
508 static int write_cpudesc(int fd, struct perf_header *h __used,
509                        struct perf_evlist *evlist __used)
510 {
511 #ifndef CPUINFO_PROC
512 #define CPUINFO_PROC NULL
513 #endif
514         FILE *file;
515         char *buf = NULL;
516         char *s, *p;
517         const char *search = CPUINFO_PROC;
518         size_t len = 0;
519         int ret = -1;
520
521         if (!search)
522                 return -1;
523
524         file = fopen("/proc/cpuinfo", "r");
525         if (!file)
526                 return -1;
527
528         while (getline(&buf, &len, file) > 0) {
529                 ret = strncmp(buf, search, strlen(search));
530                 if (!ret)
531                         break;
532         }
533
534         if (ret)
535                 goto done;
536
537         s = buf;
538
539         p = strchr(buf, ':');
540         if (p && *(p+1) == ' ' && *(p+2))
541                 s = p + 2;
542         p = strchr(s, '\n');
543         if (p)
544                 *p = '\0';
545
546         /* squash extra space characters (branding string) */
547         p = s;
548         while (*p) {
549                 if (isspace(*p)) {
550                         char *r = p + 1;
551                         char *q = r;
552                         *p = ' ';
553                         while (*q && isspace(*q))
554                                 q++;
555                         if (q != (p+1))
556                                 while ((*r++ = *q++));
557                 }
558                 p++;
559         }
560         ret = do_write_string(fd, s);
561 done:
562         free(buf);
563         fclose(file);
564         return ret;
565 }
566
567 static int write_nrcpus(int fd, struct perf_header *h __used,
568                         struct perf_evlist *evlist __used)
569 {
570         long nr;
571         u32 nrc, nra;
572         int ret;
573
574         nr = sysconf(_SC_NPROCESSORS_CONF);
575         if (nr < 0)
576                 return -1;
577
578         nrc = (u32)(nr & UINT_MAX);
579
580         nr = sysconf(_SC_NPROCESSORS_ONLN);
581         if (nr < 0)
582                 return -1;
583
584         nra = (u32)(nr & UINT_MAX);
585
586         ret = do_write(fd, &nrc, sizeof(nrc));
587         if (ret < 0)
588                 return ret;
589
590         return do_write(fd, &nra, sizeof(nra));
591 }
592
593 static int write_event_desc(int fd, struct perf_header *h __used,
594                             struct perf_evlist *evlist)
595 {
596         struct perf_evsel *attr;
597         u32 nre = 0, nri, sz;
598         int ret;
599
600         list_for_each_entry(attr, &evlist->entries, node)
601                 nre++;
602
603         /*
604          * write number of events
605          */
606         ret = do_write(fd, &nre, sizeof(nre));
607         if (ret < 0)
608                 return ret;
609
610         /*
611          * size of perf_event_attr struct
612          */
613         sz = (u32)sizeof(attr->attr);
614         ret = do_write(fd, &sz, sizeof(sz));
615         if (ret < 0)
616                 return ret;
617
618         list_for_each_entry(attr, &evlist->entries, node) {
619
620                 ret = do_write(fd, &attr->attr, sz);
621                 if (ret < 0)
622                         return ret;
623                 /*
624                  * write number of unique id per event
625                  * there is one id per instance of an event
626                  *
627                  * copy into an nri to be independent of the
628                  * type of ids,
629                  */
630                 nri = attr->ids;
631                 ret = do_write(fd, &nri, sizeof(nri));
632                 if (ret < 0)
633                         return ret;
634
635                 /*
636                  * write event string as passed on cmdline
637                  */
638                 ret = do_write_string(fd, event_name(attr));
639                 if (ret < 0)
640                         return ret;
641                 /*
642                  * write unique ids for this event
643                  */
644                 ret = do_write(fd, attr->id, attr->ids * sizeof(u64));
645                 if (ret < 0)
646                         return ret;
647         }
648         return 0;
649 }
650
651 static int write_cmdline(int fd, struct perf_header *h __used,
652                          struct perf_evlist *evlist __used)
653 {
654         char buf[MAXPATHLEN];
655         char proc[32];
656         u32 i, n;
657         int ret;
658
659         /*
660          * actual atual path to perf binary
661          */
662         sprintf(proc, "/proc/%d/exe", getpid());
663         ret = readlink(proc, buf, sizeof(buf));
664         if (ret <= 0)
665                 return -1;
666
667         /* readlink() does not add null termination */
668         buf[ret] = '\0';
669
670         /* account for binary path */
671         n = header_argc + 1;
672
673         ret = do_write(fd, &n, sizeof(n));
674         if (ret < 0)
675                 return ret;
676
677         ret = do_write_string(fd, buf);
678         if (ret < 0)
679                 return ret;
680
681         for (i = 0 ; i < header_argc; i++) {
682                 ret = do_write_string(fd, header_argv[i]);
683                 if (ret < 0)
684                         return ret;
685         }
686         return 0;
687 }
688
689 #define CORE_SIB_FMT \
690         "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
691 #define THRD_SIB_FMT \
692         "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
693
694 struct cpu_topo {
695         u32 core_sib;
696         u32 thread_sib;
697         char **core_siblings;
698         char **thread_siblings;
699 };
700
701 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
702 {
703         FILE *fp;
704         char filename[MAXPATHLEN];
705         char *buf = NULL, *p;
706         size_t len = 0;
707         u32 i = 0;
708         int ret = -1;
709
710         sprintf(filename, CORE_SIB_FMT, cpu);
711         fp = fopen(filename, "r");
712         if (!fp)
713                 return -1;
714
715         if (getline(&buf, &len, fp) <= 0)
716                 goto done;
717
718         fclose(fp);
719
720         p = strchr(buf, '\n');
721         if (p)
722                 *p = '\0';
723
724         for (i = 0; i < tp->core_sib; i++) {
725                 if (!strcmp(buf, tp->core_siblings[i]))
726                         break;
727         }
728         if (i == tp->core_sib) {
729                 tp->core_siblings[i] = buf;
730                 tp->core_sib++;
731                 buf = NULL;
732                 len = 0;
733         }
734
735         sprintf(filename, THRD_SIB_FMT, cpu);
736         fp = fopen(filename, "r");
737         if (!fp)
738                 goto done;
739
740         if (getline(&buf, &len, fp) <= 0)
741                 goto done;
742
743         p = strchr(buf, '\n');
744         if (p)
745                 *p = '\0';
746
747         for (i = 0; i < tp->thread_sib; i++) {
748                 if (!strcmp(buf, tp->thread_siblings[i]))
749                         break;
750         }
751         if (i == tp->thread_sib) {
752                 tp->thread_siblings[i] = buf;
753                 tp->thread_sib++;
754                 buf = NULL;
755         }
756         ret = 0;
757 done:
758         if(fp)
759                 fclose(fp);
760         free(buf);
761         return ret;
762 }
763
764 static void free_cpu_topo(struct cpu_topo *tp)
765 {
766         u32 i;
767
768         if (!tp)
769                 return;
770
771         for (i = 0 ; i < tp->core_sib; i++)
772                 free(tp->core_siblings[i]);
773
774         for (i = 0 ; i < tp->thread_sib; i++)
775                 free(tp->thread_siblings[i]);
776
777         free(tp);
778 }
779
780 static struct cpu_topo *build_cpu_topology(void)
781 {
782         struct cpu_topo *tp;
783         void *addr;
784         u32 nr, i;
785         size_t sz;
786         long ncpus;
787         int ret = -1;
788
789         ncpus = sysconf(_SC_NPROCESSORS_CONF);
790         if (ncpus < 0)
791                 return NULL;
792
793         nr = (u32)(ncpus & UINT_MAX);
794
795         sz = nr * sizeof(char *);
796
797         addr = calloc(1, sizeof(*tp) + 2 * sz);
798         if (!addr)
799                 return NULL;
800
801         tp = addr;
802
803         addr += sizeof(*tp);
804         tp->core_siblings = addr;
805         addr += sz;
806         tp->thread_siblings = addr;
807
808         for (i = 0; i < nr; i++) {
809                 ret = build_cpu_topo(tp, i);
810                 if (ret < 0)
811                         break;
812         }
813         if (ret) {
814                 free_cpu_topo(tp);
815                 tp = NULL;
816         }
817         return tp;
818 }
819
820 static int write_cpu_topology(int fd, struct perf_header *h __used,
821                           struct perf_evlist *evlist __used)
822 {
823         struct cpu_topo *tp;
824         u32 i;
825         int ret;
826
827         tp = build_cpu_topology();
828         if (!tp)
829                 return -1;
830
831         ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
832         if (ret < 0)
833                 goto done;
834
835         for (i = 0; i < tp->core_sib; i++) {
836                 ret = do_write_string(fd, tp->core_siblings[i]);
837                 if (ret < 0)
838                         goto done;
839         }
840         ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
841         if (ret < 0)
842                 goto done;
843
844         for (i = 0; i < tp->thread_sib; i++) {
845                 ret = do_write_string(fd, tp->thread_siblings[i]);
846                 if (ret < 0)
847                         break;
848         }
849 done:
850         free_cpu_topo(tp);
851         return ret;
852 }
853
854
855
856 static int write_total_mem(int fd, struct perf_header *h __used,
857                           struct perf_evlist *evlist __used)
858 {
859         char *buf = NULL;
860         FILE *fp;
861         size_t len = 0;
862         int ret = -1, n;
863         uint64_t mem;
864
865         fp = fopen("/proc/meminfo", "r");
866         if (!fp)
867                 return -1;
868
869         while (getline(&buf, &len, fp) > 0) {
870                 ret = strncmp(buf, "MemTotal:", 9);
871                 if (!ret)
872                         break;
873         }
874         if (!ret) {
875                 n = sscanf(buf, "%*s %"PRIu64, &mem);
876                 if (n == 1)
877                         ret = do_write(fd, &mem, sizeof(mem));
878         }
879         free(buf);
880         fclose(fp);
881         return ret;
882 }
883
884 static int write_topo_node(int fd, int node)
885 {
886         char str[MAXPATHLEN];
887         char field[32];
888         char *buf = NULL, *p;
889         size_t len = 0;
890         FILE *fp;
891         u64 mem_total, mem_free, mem;
892         int ret = -1;
893
894         sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
895         fp = fopen(str, "r");
896         if (!fp)
897                 return -1;
898
899         while (getline(&buf, &len, fp) > 0) {
900                 /* skip over invalid lines */
901                 if (!strchr(buf, ':'))
902                         continue;
903                 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
904                         goto done;
905                 if (!strcmp(field, "MemTotal:"))
906                         mem_total = mem;
907                 if (!strcmp(field, "MemFree:"))
908                         mem_free = mem;
909         }
910
911         fclose(fp);
912
913         ret = do_write(fd, &mem_total, sizeof(u64));
914         if (ret)
915                 goto done;
916
917         ret = do_write(fd, &mem_free, sizeof(u64));
918         if (ret)
919                 goto done;
920
921         ret = -1;
922         sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
923
924         fp = fopen(str, "r");
925         if (!fp)
926                 goto done;
927
928         if (getline(&buf, &len, fp) <= 0)
929                 goto done;
930
931         p = strchr(buf, '\n');
932         if (p)
933                 *p = '\0';
934
935         ret = do_write_string(fd, buf);
936 done:
937         free(buf);
938         fclose(fp);
939         return ret;
940 }
941
942 static int write_numa_topology(int fd, struct perf_header *h __used,
943                           struct perf_evlist *evlist __used)
944 {
945         char *buf = NULL;
946         size_t len = 0;
947         FILE *fp;
948         struct cpu_map *node_map = NULL;
949         char *c;
950         u32 nr, i, j;
951         int ret = -1;
952
953         fp = fopen("/sys/devices/system/node/online", "r");
954         if (!fp)
955                 return -1;
956
957         if (getline(&buf, &len, fp) <= 0)
958                 goto done;
959
960         c = strchr(buf, '\n');
961         if (c)
962                 *c = '\0';
963
964         node_map = cpu_map__new(buf);
965         if (!node_map)
966                 goto done;
967
968         nr = (u32)node_map->nr;
969
970         ret = do_write(fd, &nr, sizeof(nr));
971         if (ret < 0)
972                 goto done;
973
974         for (i = 0; i < nr; i++) {
975                 j = (u32)node_map->map[i];
976                 ret = do_write(fd, &j, sizeof(j));
977                 if (ret < 0)
978                         break;
979
980                 ret = write_topo_node(fd, i);
981                 if (ret < 0)
982                         break;
983         }
984 done:
985         free(buf);
986         fclose(fp);
987         free(node_map);
988         return ret;
989 }
990
991 /*
992  * default get_cpuid(): nothing gets recorded
993  * actual implementation must be in arch/$(ARCH)/util/header.c
994  */
995 int __attribute__((weak)) get_cpuid(char *buffer __used, size_t sz __used)
996 {
997         return -1;
998 }
999
1000 static int write_cpuid(int fd, struct perf_header *h __used,
1001                        struct perf_evlist *evlist __used)
1002 {
1003         char buffer[64];
1004         int ret;
1005
1006         ret = get_cpuid(buffer, sizeof(buffer));
1007         if (!ret)
1008                 goto write_it;
1009
1010         return -1;
1011 write_it:
1012         return do_write_string(fd, buffer);
1013 }
1014
1015 static void print_hostname(struct perf_header *ph, int fd, FILE *fp)
1016 {
1017         char *str = do_read_string(fd, ph);
1018         fprintf(fp, "# hostname : %s\n", str);
1019         free(str);
1020 }
1021
1022 static void print_osrelease(struct perf_header *ph, int fd, FILE *fp)
1023 {
1024         char *str = do_read_string(fd, ph);
1025         fprintf(fp, "# os release : %s\n", str);
1026         free(str);
1027 }
1028
1029 static void print_arch(struct perf_header *ph, int fd, FILE *fp)
1030 {
1031         char *str = do_read_string(fd, ph);
1032         fprintf(fp, "# arch : %s\n", str);
1033         free(str);
1034 }
1035
1036 static void print_cpudesc(struct perf_header *ph, int fd, FILE *fp)
1037 {
1038         char *str = do_read_string(fd, ph);
1039         fprintf(fp, "# cpudesc : %s\n", str);
1040         free(str);
1041 }
1042
1043 static void print_nrcpus(struct perf_header *ph, int fd, FILE *fp)
1044 {
1045         ssize_t ret;
1046         u32 nr;
1047
1048         ret = read(fd, &nr, sizeof(nr));
1049         if (ret != (ssize_t)sizeof(nr))
1050                 nr = -1; /* interpreted as error */
1051
1052         if (ph->needs_swap)
1053                 nr = bswap_32(nr);
1054
1055         fprintf(fp, "# nrcpus online : %u\n", nr);
1056
1057         ret = read(fd, &nr, sizeof(nr));
1058         if (ret != (ssize_t)sizeof(nr))
1059                 nr = -1; /* interpreted as error */
1060
1061         if (ph->needs_swap)
1062                 nr = bswap_32(nr);
1063
1064         fprintf(fp, "# nrcpus avail : %u\n", nr);
1065 }
1066
1067 static void print_version(struct perf_header *ph, int fd, FILE *fp)
1068 {
1069         char *str = do_read_string(fd, ph);
1070         fprintf(fp, "# perf version : %s\n", str);
1071         free(str);
1072 }
1073
1074 static void print_cmdline(struct perf_header *ph, int fd, FILE *fp)
1075 {
1076         ssize_t ret;
1077         char *str;
1078         u32 nr, i;
1079
1080         ret = read(fd, &nr, sizeof(nr));
1081         if (ret != (ssize_t)sizeof(nr))
1082                 return;
1083
1084         if (ph->needs_swap)
1085                 nr = bswap_32(nr);
1086
1087         fprintf(fp, "# cmdline : ");
1088
1089         for (i = 0; i < nr; i++) {
1090                 str = do_read_string(fd, ph);
1091                 fprintf(fp, "%s ", str);
1092                 free(str);
1093         }
1094         fputc('\n', fp);
1095 }
1096
1097 static void print_cpu_topology(struct perf_header *ph, int fd, FILE *fp)
1098 {
1099         ssize_t ret;
1100         u32 nr, i;
1101         char *str;
1102
1103         ret = read(fd, &nr, sizeof(nr));
1104         if (ret != (ssize_t)sizeof(nr))
1105                 return;
1106
1107         if (ph->needs_swap)
1108                 nr = bswap_32(nr);
1109
1110         for (i = 0; i < nr; i++) {
1111                 str = do_read_string(fd, ph);
1112                 fprintf(fp, "# sibling cores   : %s\n", str);
1113                 free(str);
1114         }
1115
1116         ret = read(fd, &nr, sizeof(nr));
1117         if (ret != (ssize_t)sizeof(nr))
1118                 return;
1119
1120         if (ph->needs_swap)
1121                 nr = bswap_32(nr);
1122
1123         for (i = 0; i < nr; i++) {
1124                 str = do_read_string(fd, ph);
1125                 fprintf(fp, "# sibling threads : %s\n", str);
1126                 free(str);
1127         }
1128 }
1129
1130 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1131 {
1132         struct perf_event_attr attr;
1133         uint64_t id;
1134         void *buf = NULL;
1135         char *str;
1136         u32 nre, sz, nr, i, j, msz;
1137         int ret;
1138
1139         /* number of events */
1140         ret = read(fd, &nre, sizeof(nre));
1141         if (ret != (ssize_t)sizeof(nre))
1142                 goto error;
1143
1144         if (ph->needs_swap)
1145                 nre = bswap_32(nre);
1146
1147         ret = read(fd, &sz, sizeof(sz));
1148         if (ret != (ssize_t)sizeof(sz))
1149                 goto error;
1150
1151         if (ph->needs_swap)
1152                 sz = bswap_32(sz);
1153
1154         /*
1155          * ensure it is at least to our ABI rev
1156          */
1157         if (sz < (u32)sizeof(attr))
1158                 goto error;
1159
1160         memset(&attr, 0, sizeof(attr));
1161
1162         /* read entire region to sync up to next field */
1163         buf = malloc(sz);
1164         if (!buf)
1165                 goto error;
1166
1167         msz = sizeof(attr);
1168         if (sz < msz)
1169                 msz = sz;
1170
1171         for (i = 0 ; i < nre; i++) {
1172
1173                 ret = read(fd, buf, sz);
1174                 if (ret != (ssize_t)sz)
1175                         goto error;
1176
1177                 if (ph->needs_swap)
1178                         perf_event__attr_swap(buf);
1179
1180                 memcpy(&attr, buf, msz);
1181
1182                 ret = read(fd, &nr, sizeof(nr));
1183                 if (ret != (ssize_t)sizeof(nr))
1184                         goto error;
1185
1186                 if (ph->needs_swap)
1187                         nr = bswap_32(nr);
1188
1189                 str = do_read_string(fd, ph);
1190                 fprintf(fp, "# event : name = %s, ", str);
1191                 free(str);
1192
1193                 fprintf(fp, "type = %d, config = 0x%"PRIx64
1194                             ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
1195                                 attr.type,
1196                                 (u64)attr.config,
1197                                 (u64)attr.config1,
1198                                 (u64)attr.config2);
1199
1200                 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
1201                                 attr.exclude_user,
1202                                 attr.exclude_kernel);
1203
1204                 if (nr)
1205                         fprintf(fp, ", id = {");
1206
1207                 for (j = 0 ; j < nr; j++) {
1208                         ret = read(fd, &id, sizeof(id));
1209                         if (ret != (ssize_t)sizeof(id))
1210                                 goto error;
1211
1212                         if (ph->needs_swap)
1213                                 id = bswap_64(id);
1214
1215                         if (j)
1216                                 fputc(',', fp);
1217
1218                         fprintf(fp, " %"PRIu64, id);
1219                 }
1220                 if (nr && j == nr)
1221                         fprintf(fp, " }");
1222                 fputc('\n', fp);
1223         }
1224         free(buf);
1225         return;
1226 error:
1227         fprintf(fp, "# event desc: not available or unable to read\n");
1228 }
1229
1230 static void print_total_mem(struct perf_header *h __used, int fd, FILE *fp)
1231 {
1232         uint64_t mem;
1233         ssize_t ret;
1234
1235         ret = read(fd, &mem, sizeof(mem));
1236         if (ret != sizeof(mem))
1237                 goto error;
1238
1239         if (h->needs_swap)
1240                 mem = bswap_64(mem);
1241
1242         fprintf(fp, "# total memory : %"PRIu64" kB\n", mem);
1243         return;
1244 error:
1245         fprintf(fp, "# total memory : unknown\n");
1246 }
1247
1248 static void print_numa_topology(struct perf_header *h __used, int fd, FILE *fp)
1249 {
1250         ssize_t ret;
1251         u32 nr, c, i;
1252         char *str;
1253         uint64_t mem_total, mem_free;
1254
1255         /* nr nodes */
1256         ret = read(fd, &nr, sizeof(nr));
1257         if (ret != (ssize_t)sizeof(nr))
1258                 goto error;
1259
1260         if (h->needs_swap)
1261                 nr = bswap_32(nr);
1262
1263         for (i = 0; i < nr; i++) {
1264
1265                 /* node number */
1266                 ret = read(fd, &c, sizeof(c));
1267                 if (ret != (ssize_t)sizeof(c))
1268                         goto error;
1269
1270                 if (h->needs_swap)
1271                         c = bswap_32(c);
1272
1273                 ret = read(fd, &mem_total, sizeof(u64));
1274                 if (ret != sizeof(u64))
1275                         goto error;
1276
1277                 ret = read(fd, &mem_free, sizeof(u64));
1278                 if (ret != sizeof(u64))
1279                         goto error;
1280
1281                 if (h->needs_swap) {
1282                         mem_total = bswap_64(mem_total);
1283                         mem_free = bswap_64(mem_free);
1284                 }
1285
1286                 fprintf(fp, "# node%u meminfo  : total = %"PRIu64" kB,"
1287                             " free = %"PRIu64" kB\n",
1288                         c,
1289                         mem_total,
1290                         mem_free);
1291
1292                 str = do_read_string(fd, h);
1293                 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1294                 free(str);
1295         }
1296         return;
1297 error:
1298         fprintf(fp, "# numa topology : not available\n");
1299 }
1300
1301 static void print_cpuid(struct perf_header *ph, int fd, FILE *fp)
1302 {
1303         char *str = do_read_string(fd, ph);
1304         fprintf(fp, "# cpuid : %s\n", str);
1305         free(str);
1306 }
1307
1308 struct feature_ops {
1309         int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1310         void (*print)(struct perf_header *h, int fd, FILE *fp);
1311         const char *name;
1312         bool full_only;
1313 };
1314
1315 #define FEAT_OPA(n, func) \
1316         [n] = { .name = #n, .write = write_##func, .print = print_##func }
1317 #define FEAT_OPF(n, func) \
1318         [n] = { .name = #n, .write = write_##func, .print = print_##func, .full_only = true }
1319
1320 /* feature_ops not implemented: */
1321 #define print_trace_info                NULL
1322 #define print_build_id                  NULL
1323
1324 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
1325         FEAT_OPA(HEADER_TRACE_INFO,     trace_info),
1326         FEAT_OPA(HEADER_BUILD_ID,       build_id),
1327         FEAT_OPA(HEADER_HOSTNAME,       hostname),
1328         FEAT_OPA(HEADER_OSRELEASE,      osrelease),
1329         FEAT_OPA(HEADER_VERSION,        version),
1330         FEAT_OPA(HEADER_ARCH,           arch),
1331         FEAT_OPA(HEADER_NRCPUS,         nrcpus),
1332         FEAT_OPA(HEADER_CPUDESC,        cpudesc),
1333         FEAT_OPA(HEADER_CPUID,          cpuid),
1334         FEAT_OPA(HEADER_TOTAL_MEM,      total_mem),
1335         FEAT_OPA(HEADER_EVENT_DESC,     event_desc),
1336         FEAT_OPA(HEADER_CMDLINE,        cmdline),
1337         FEAT_OPF(HEADER_CPU_TOPOLOGY,   cpu_topology),
1338         FEAT_OPF(HEADER_NUMA_TOPOLOGY,  numa_topology),
1339 };
1340
1341 struct header_print_data {
1342         FILE *fp;
1343         bool full; /* extended list of headers */
1344 };
1345
1346 static int perf_file_section__fprintf_info(struct perf_file_section *section,
1347                                            struct perf_header *ph,
1348                                            int feat, int fd, void *data)
1349 {
1350         struct header_print_data *hd = data;
1351
1352         if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1353                 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1354                                 "%d, continuing...\n", section->offset, feat);
1355                 return 0;
1356         }
1357         if (feat >= HEADER_LAST_FEATURE) {
1358                 pr_warning("unknown feature %d\n", feat);
1359                 return 0;
1360         }
1361         if (!feat_ops[feat].print)
1362                 return 0;
1363
1364         if (!feat_ops[feat].full_only || hd->full)
1365                 feat_ops[feat].print(ph, fd, hd->fp);
1366         else
1367                 fprintf(hd->fp, "# %s info available, use -I to display\n",
1368                         feat_ops[feat].name);
1369
1370         return 0;
1371 }
1372
1373 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
1374 {
1375         struct header_print_data hd;
1376         struct perf_header *header = &session->header;
1377         int fd = session->fd;
1378         hd.fp = fp;
1379         hd.full = full;
1380
1381         perf_header__process_sections(header, fd, &hd,
1382                                       perf_file_section__fprintf_info);
1383         return 0;
1384 }
1385
1386 static int do_write_feat(int fd, struct perf_header *h, int type,
1387                          struct perf_file_section **p,
1388                          struct perf_evlist *evlist)
1389 {
1390         int err;
1391         int ret = 0;
1392
1393         if (perf_header__has_feat(h, type)) {
1394                 if (!feat_ops[type].write)
1395                         return -1;
1396
1397                 (*p)->offset = lseek(fd, 0, SEEK_CUR);
1398
1399                 err = feat_ops[type].write(fd, h, evlist);
1400                 if (err < 0) {
1401                         pr_debug("failed to write feature %d\n", type);
1402
1403                         /* undo anything written */
1404                         lseek(fd, (*p)->offset, SEEK_SET);
1405
1406                         return -1;
1407                 }
1408                 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
1409                 (*p)++;
1410         }
1411         return ret;
1412 }
1413
1414 static int perf_header__adds_write(struct perf_header *header,
1415                                    struct perf_evlist *evlist, int fd)
1416 {
1417         int nr_sections;
1418         struct perf_session *session;
1419         struct perf_file_section *feat_sec, *p;
1420         int sec_size;
1421         u64 sec_start;
1422         int feat;
1423         int err;
1424
1425         session = container_of(header, struct perf_session, header);
1426
1427         nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
1428         if (!nr_sections)
1429                 return 0;
1430
1431         feat_sec = p = calloc(sizeof(*feat_sec), nr_sections);
1432         if (feat_sec == NULL)
1433                 return -ENOMEM;
1434
1435         sec_size = sizeof(*feat_sec) * nr_sections;
1436
1437         sec_start = header->data_offset + header->data_size;
1438         lseek(fd, sec_start + sec_size, SEEK_SET);
1439
1440         for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
1441                 if (do_write_feat(fd, header, feat, &p, evlist))
1442                         perf_header__clear_feat(header, feat);
1443         }
1444
1445         lseek(fd, sec_start, SEEK_SET);
1446         /*
1447          * may write more than needed due to dropped feature, but
1448          * this is okay, reader will skip the mising entries
1449          */
1450         err = do_write(fd, feat_sec, sec_size);
1451         if (err < 0)
1452                 pr_debug("failed to write feature section\n");
1453         free(feat_sec);
1454         return err;
1455 }
1456
1457 int perf_header__write_pipe(int fd)
1458 {
1459         struct perf_pipe_file_header f_header;
1460         int err;
1461
1462         f_header = (struct perf_pipe_file_header){
1463                 .magic     = PERF_MAGIC,
1464                 .size      = sizeof(f_header),
1465         };
1466
1467         err = do_write(fd, &f_header, sizeof(f_header));
1468         if (err < 0) {
1469                 pr_debug("failed to write perf pipe header\n");
1470                 return err;
1471         }
1472
1473         return 0;
1474 }
1475
1476 int perf_session__write_header(struct perf_session *session,
1477                                struct perf_evlist *evlist,
1478                                int fd, bool at_exit)
1479 {
1480         struct perf_file_header f_header;
1481         struct perf_file_attr   f_attr;
1482         struct perf_header *header = &session->header;
1483         struct perf_evsel *attr, *pair = NULL;
1484         int err;
1485
1486         lseek(fd, sizeof(f_header), SEEK_SET);
1487
1488         if (session->evlist != evlist)
1489                 pair = list_entry(session->evlist->entries.next, struct perf_evsel, node);
1490
1491         list_for_each_entry(attr, &evlist->entries, node) {
1492                 attr->id_offset = lseek(fd, 0, SEEK_CUR);
1493                 err = do_write(fd, attr->id, attr->ids * sizeof(u64));
1494                 if (err < 0) {
1495 out_err_write:
1496                         pr_debug("failed to write perf header\n");
1497                         return err;
1498                 }
1499                 if (session->evlist != evlist) {
1500                         err = do_write(fd, pair->id, pair->ids * sizeof(u64));
1501                         if (err < 0)
1502                                 goto out_err_write;
1503                         attr->ids += pair->ids;
1504                         pair = list_entry(pair->node.next, struct perf_evsel, node);
1505                 }
1506         }
1507
1508         header->attr_offset = lseek(fd, 0, SEEK_CUR);
1509
1510         list_for_each_entry(attr, &evlist->entries, node) {
1511                 f_attr = (struct perf_file_attr){
1512                         .attr = attr->attr,
1513                         .ids  = {
1514                                 .offset = attr->id_offset,
1515                                 .size   = attr->ids * sizeof(u64),
1516                         }
1517                 };
1518                 err = do_write(fd, &f_attr, sizeof(f_attr));
1519                 if (err < 0) {
1520                         pr_debug("failed to write perf header attribute\n");
1521                         return err;
1522                 }
1523         }
1524
1525         header->event_offset = lseek(fd, 0, SEEK_CUR);
1526         header->event_size = event_count * sizeof(struct perf_trace_event_type);
1527         if (events) {
1528                 err = do_write(fd, events, header->event_size);
1529                 if (err < 0) {
1530                         pr_debug("failed to write perf header events\n");
1531                         return err;
1532                 }
1533         }
1534
1535         header->data_offset = lseek(fd, 0, SEEK_CUR);
1536
1537         if (at_exit) {
1538                 err = perf_header__adds_write(header, evlist, fd);
1539                 if (err < 0)
1540                         return err;
1541         }
1542
1543         f_header = (struct perf_file_header){
1544                 .magic     = PERF_MAGIC,
1545                 .size      = sizeof(f_header),
1546                 .attr_size = sizeof(f_attr),
1547                 .attrs = {
1548                         .offset = header->attr_offset,
1549                         .size   = evlist->nr_entries * sizeof(f_attr),
1550                 },
1551                 .data = {
1552                         .offset = header->data_offset,
1553                         .size   = header->data_size,
1554                 },
1555                 .event_types = {
1556                         .offset = header->event_offset,
1557                         .size   = header->event_size,
1558                 },
1559         };
1560
1561         memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
1562
1563         lseek(fd, 0, SEEK_SET);
1564         err = do_write(fd, &f_header, sizeof(f_header));
1565         if (err < 0) {
1566                 pr_debug("failed to write perf header\n");
1567                 return err;
1568         }
1569         lseek(fd, header->data_offset + header->data_size, SEEK_SET);
1570
1571         header->frozen = 1;
1572         return 0;
1573 }
1574
1575 static int perf_header__getbuffer64(struct perf_header *header,
1576                                     int fd, void *buf, size_t size)
1577 {
1578         if (readn(fd, buf, size) <= 0)
1579                 return -1;
1580
1581         if (header->needs_swap)
1582                 mem_bswap_64(buf, size);
1583
1584         return 0;
1585 }
1586
1587 int perf_header__process_sections(struct perf_header *header, int fd,
1588                                   void *data,
1589                                   int (*process)(struct perf_file_section *section,
1590                                                  struct perf_header *ph,
1591                                                  int feat, int fd, void *data))
1592 {
1593         struct perf_file_section *feat_sec, *sec;
1594         int nr_sections;
1595         int sec_size;
1596         int feat;
1597         int err;
1598
1599         nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
1600         if (!nr_sections)
1601                 return 0;
1602
1603         feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections);
1604         if (!feat_sec)
1605                 return -1;
1606
1607         sec_size = sizeof(*feat_sec) * nr_sections;
1608
1609         lseek(fd, header->data_offset + header->data_size, SEEK_SET);
1610
1611         err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
1612         if (err < 0)
1613                 goto out_free;
1614
1615         for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
1616                 err = process(sec++, header, feat, fd, data);
1617                 if (err < 0)
1618                         goto out_free;
1619         }
1620         err = 0;
1621 out_free:
1622         free(feat_sec);
1623         return err;
1624 }
1625
1626 int perf_file_header__read(struct perf_file_header *header,
1627                            struct perf_header *ph, int fd)
1628 {
1629         lseek(fd, 0, SEEK_SET);
1630
1631         if (readn(fd, header, sizeof(*header)) <= 0 ||
1632             memcmp(&header->magic, __perf_magic, sizeof(header->magic)))
1633                 return -1;
1634
1635         if (header->attr_size != sizeof(struct perf_file_attr)) {
1636                 u64 attr_size = bswap_64(header->attr_size);
1637
1638                 if (attr_size != sizeof(struct perf_file_attr))
1639                         return -1;
1640
1641                 mem_bswap_64(header, offsetof(struct perf_file_header,
1642                                             adds_features));
1643                 ph->needs_swap = true;
1644         }
1645
1646         if (header->size != sizeof(*header)) {
1647                 /* Support the previous format */
1648                 if (header->size == offsetof(typeof(*header), adds_features))
1649                         bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
1650                 else
1651                         return -1;
1652         } else if (ph->needs_swap) {
1653                 unsigned int i;
1654                 /*
1655                  * feature bitmap is declared as an array of unsigned longs --
1656                  * not good since its size can differ between the host that
1657                  * generated the data file and the host analyzing the file.
1658                  *
1659                  * We need to handle endianness, but we don't know the size of
1660                  * the unsigned long where the file was generated. Take a best
1661                  * guess at determining it: try 64-bit swap first (ie., file
1662                  * created on a 64-bit host), and check if the hostname feature
1663                  * bit is set (this feature bit is forced on as of fbe96f2).
1664                  * If the bit is not, undo the 64-bit swap and try a 32-bit
1665                  * swap. If the hostname bit is still not set (e.g., older data
1666                  * file), punt and fallback to the original behavior --
1667                  * clearing all feature bits and setting buildid.
1668                  */
1669                 for (i = 0; i < BITS_TO_LONGS(HEADER_FEAT_BITS); ++i)
1670                         header->adds_features[i] = bswap_64(header->adds_features[i]);
1671
1672                 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
1673                         for (i = 0; i < BITS_TO_LONGS(HEADER_FEAT_BITS); ++i) {
1674                                 header->adds_features[i] = bswap_64(header->adds_features[i]);
1675                                 header->adds_features[i] = bswap_32(header->adds_features[i]);
1676                         }
1677                 }
1678
1679                 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
1680                         bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
1681                         set_bit(HEADER_BUILD_ID, header->adds_features);
1682                 }
1683         }
1684
1685         memcpy(&ph->adds_features, &header->adds_features,
1686                sizeof(ph->adds_features));
1687
1688         ph->event_offset = header->event_types.offset;
1689         ph->event_size   = header->event_types.size;
1690         ph->data_offset  = header->data.offset;
1691         ph->data_size    = header->data.size;
1692         return 0;
1693 }
1694
1695 static int __event_process_build_id(struct build_id_event *bev,
1696                                     char *filename,
1697                                     struct perf_session *session)
1698 {
1699         int err = -1;
1700         struct list_head *head;
1701         struct machine *machine;
1702         u16 misc;
1703         struct dso *dso;
1704         enum dso_kernel_type dso_type;
1705
1706         machine = perf_session__findnew_machine(session, bev->pid);
1707         if (!machine)
1708                 goto out;
1709
1710         misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1711
1712         switch (misc) {
1713         case PERF_RECORD_MISC_KERNEL:
1714                 dso_type = DSO_TYPE_KERNEL;
1715                 head = &machine->kernel_dsos;
1716                 break;
1717         case PERF_RECORD_MISC_GUEST_KERNEL:
1718                 dso_type = DSO_TYPE_GUEST_KERNEL;
1719                 head = &machine->kernel_dsos;
1720                 break;
1721         case PERF_RECORD_MISC_USER:
1722         case PERF_RECORD_MISC_GUEST_USER:
1723                 dso_type = DSO_TYPE_USER;
1724                 head = &machine->user_dsos;
1725                 break;
1726         default:
1727                 goto out;
1728         }
1729
1730         dso = __dsos__findnew(head, filename);
1731         if (dso != NULL) {
1732                 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1733
1734                 dso__set_build_id(dso, &bev->build_id);
1735
1736                 if (filename[0] == '[')
1737                         dso->kernel = dso_type;
1738
1739                 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1740                                   sbuild_id);
1741                 pr_debug("build id event received for %s: %s\n",
1742                          dso->long_name, sbuild_id);
1743         }
1744
1745         err = 0;
1746 out:
1747         return err;
1748 }
1749
1750 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1751                                                  int input, u64 offset, u64 size)
1752 {
1753         struct perf_session *session = container_of(header, struct perf_session, header);
1754         struct {
1755                 struct perf_event_header   header;
1756                 u8                         build_id[ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1757                 char                       filename[0];
1758         } old_bev;
1759         struct build_id_event bev;
1760         char filename[PATH_MAX];
1761         u64 limit = offset + size;
1762
1763         while (offset < limit) {
1764                 ssize_t len;
1765
1766                 if (read(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1767                         return -1;
1768
1769                 if (header->needs_swap)
1770                         perf_event_header__bswap(&old_bev.header);
1771
1772                 len = old_bev.header.size - sizeof(old_bev);
1773                 if (read(input, filename, len) != len)
1774                         return -1;
1775
1776                 bev.header = old_bev.header;
1777
1778                 /*
1779                  * As the pid is the missing value, we need to fill
1780                  * it properly. The header.misc value give us nice hint.
1781                  */
1782                 bev.pid = HOST_KERNEL_ID;
1783                 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1784                     bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1785                         bev.pid = DEFAULT_GUEST_KERNEL_ID;
1786
1787                 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1788                 __event_process_build_id(&bev, filename, session);
1789
1790                 offset += bev.header.size;
1791         }
1792
1793         return 0;
1794 }
1795
1796 static int perf_header__read_build_ids(struct perf_header *header,
1797                                        int input, u64 offset, u64 size)
1798 {
1799         struct perf_session *session = container_of(header, struct perf_session, header);
1800         struct build_id_event bev;
1801         char filename[PATH_MAX];
1802         u64 limit = offset + size, orig_offset = offset;
1803         int err = -1;
1804
1805         while (offset < limit) {
1806                 ssize_t len;
1807
1808                 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
1809                         goto out;
1810
1811                 if (header->needs_swap)
1812                         perf_event_header__bswap(&bev.header);
1813
1814                 len = bev.header.size - sizeof(bev);
1815                 if (read(input, filename, len) != len)
1816                         goto out;
1817                 /*
1818                  * The a1645ce1 changeset:
1819                  *
1820                  * "perf: 'perf kvm' tool for monitoring guest performance from host"
1821                  *
1822                  * Added a field to struct build_id_event that broke the file
1823                  * format.
1824                  *
1825                  * Since the kernel build-id is the first entry, process the
1826                  * table using the old format if the well known
1827                  * '[kernel.kallsyms]' string for the kernel build-id has the
1828                  * first 4 characters chopped off (where the pid_t sits).
1829                  */
1830                 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1831                         if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1832                                 return -1;
1833                         return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1834                 }
1835
1836                 __event_process_build_id(&bev, filename, session);
1837
1838                 offset += bev.header.size;
1839         }
1840         err = 0;
1841 out:
1842         return err;
1843 }
1844
1845 static int perf_file_section__process(struct perf_file_section *section,
1846                                       struct perf_header *ph,
1847                                       int feat, int fd, void *data __used)
1848 {
1849         if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1850                 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1851                           "%d, continuing...\n", section->offset, feat);
1852                 return 0;
1853         }
1854
1855         if (feat >= HEADER_LAST_FEATURE) {
1856                 pr_debug("unknown feature %d, continuing...\n", feat);
1857                 return 0;
1858         }
1859
1860         switch (feat) {
1861         case HEADER_TRACE_INFO:
1862                 trace_report(fd, false);
1863                 break;
1864         case HEADER_BUILD_ID:
1865                 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1866                         pr_debug("Failed to read buildids, continuing...\n");
1867                 break;
1868         default:
1869                 break;
1870         }
1871
1872         return 0;
1873 }
1874
1875 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
1876                                        struct perf_header *ph, int fd,
1877                                        bool repipe)
1878 {
1879         if (readn(fd, header, sizeof(*header)) <= 0 ||
1880             memcmp(&header->magic, __perf_magic, sizeof(header->magic)))
1881                 return -1;
1882
1883         if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
1884                 return -1;
1885
1886         if (header->size != sizeof(*header)) {
1887                 u64 size = bswap_64(header->size);
1888
1889                 if (size != sizeof(*header))
1890                         return -1;
1891
1892                 ph->needs_swap = true;
1893         }
1894
1895         return 0;
1896 }
1897
1898 static int perf_header__read_pipe(struct perf_session *session, int fd)
1899 {
1900         struct perf_header *header = &session->header;
1901         struct perf_pipe_file_header f_header;
1902
1903         if (perf_file_header__read_pipe(&f_header, header, fd,
1904                                         session->repipe) < 0) {
1905                 pr_debug("incompatible file format\n");
1906                 return -EINVAL;
1907         }
1908
1909         session->fd = fd;
1910
1911         return 0;
1912 }
1913
1914 int perf_session__read_header(struct perf_session *session, int fd)
1915 {
1916         struct perf_header *header = &session->header;
1917         struct perf_file_header f_header;
1918         struct perf_file_attr   f_attr;
1919         u64                     f_id;
1920         int nr_attrs, nr_ids, i, j;
1921
1922         session->evlist = perf_evlist__new(NULL, NULL);
1923         if (session->evlist == NULL)
1924                 return -ENOMEM;
1925
1926         if (session->fd_pipe)
1927                 return perf_header__read_pipe(session, fd);
1928
1929         if (perf_file_header__read(&f_header, header, fd) < 0) {
1930                 pr_debug("incompatible file format\n");
1931                 return -EINVAL;
1932         }
1933
1934         nr_attrs = f_header.attrs.size / sizeof(f_attr);
1935         lseek(fd, f_header.attrs.offset, SEEK_SET);
1936
1937         for (i = 0; i < nr_attrs; i++) {
1938                 struct perf_evsel *evsel;
1939                 off_t tmp;
1940
1941                 if (readn(fd, &f_attr, sizeof(f_attr)) <= 0)
1942                         goto out_errno;
1943
1944                 if (header->needs_swap)
1945                         perf_event__attr_swap(&f_attr.attr);
1946
1947                 tmp = lseek(fd, 0, SEEK_CUR);
1948                 evsel = perf_evsel__new(&f_attr.attr, i);
1949
1950                 if (evsel == NULL)
1951                         goto out_delete_evlist;
1952                 /*
1953                  * Do it before so that if perf_evsel__alloc_id fails, this
1954                  * entry gets purged too at perf_evlist__delete().
1955                  */
1956                 perf_evlist__add(session->evlist, evsel);
1957
1958                 nr_ids = f_attr.ids.size / sizeof(u64);
1959                 /*
1960                  * We don't have the cpu and thread maps on the header, so
1961                  * for allocating the perf_sample_id table we fake 1 cpu and
1962                  * hattr->ids threads.
1963                  */
1964                 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
1965                         goto out_delete_evlist;
1966
1967                 lseek(fd, f_attr.ids.offset, SEEK_SET);
1968
1969                 for (j = 0; j < nr_ids; j++) {
1970                         if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
1971                                 goto out_errno;
1972
1973                         perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
1974                 }
1975
1976                 lseek(fd, tmp, SEEK_SET);
1977         }
1978
1979         symbol_conf.nr_events = nr_attrs;
1980
1981         if (f_header.event_types.size) {
1982                 lseek(fd, f_header.event_types.offset, SEEK_SET);
1983                 events = malloc(f_header.event_types.size);
1984                 if (events == NULL)
1985                         return -ENOMEM;
1986                 if (perf_header__getbuffer64(header, fd, events,
1987                                              f_header.event_types.size))
1988                         goto out_errno;
1989                 event_count =  f_header.event_types.size / sizeof(struct perf_trace_event_type);
1990         }
1991
1992         perf_header__process_sections(header, fd, NULL,
1993                                       perf_file_section__process);
1994
1995         lseek(fd, header->data_offset, SEEK_SET);
1996
1997         header->frozen = 1;
1998         return 0;
1999 out_errno:
2000         return -errno;
2001
2002 out_delete_evlist:
2003         perf_evlist__delete(session->evlist);
2004         session->evlist = NULL;
2005         return -ENOMEM;
2006 }
2007
2008 int perf_event__synthesize_attr(struct perf_tool *tool,
2009                                 struct perf_event_attr *attr, u16 ids, u64 *id,
2010                                 perf_event__handler_t process)
2011 {
2012         union perf_event *ev;
2013         size_t size;
2014         int err;
2015
2016         size = sizeof(struct perf_event_attr);
2017         size = ALIGN(size, sizeof(u64));
2018         size += sizeof(struct perf_event_header);
2019         size += ids * sizeof(u64);
2020
2021         ev = malloc(size);
2022
2023         if (ev == NULL)
2024                 return -ENOMEM;
2025
2026         ev->attr.attr = *attr;
2027         memcpy(ev->attr.id, id, ids * sizeof(u64));
2028
2029         ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2030         ev->attr.header.size = size;
2031
2032         err = process(tool, ev, NULL, NULL);
2033
2034         free(ev);
2035
2036         return err;
2037 }
2038
2039 int perf_event__synthesize_attrs(struct perf_tool *tool,
2040                                    struct perf_session *session,
2041                                    perf_event__handler_t process)
2042 {
2043         struct perf_evsel *attr;
2044         int err = 0;
2045
2046         list_for_each_entry(attr, &session->evlist->entries, node) {
2047                 err = perf_event__synthesize_attr(tool, &attr->attr, attr->ids,
2048                                                   attr->id, process);
2049                 if (err) {
2050                         pr_debug("failed to create perf header attribute\n");
2051                         return err;
2052                 }
2053         }
2054
2055         return err;
2056 }
2057
2058 int perf_event__process_attr(union perf_event *event,
2059                              struct perf_evlist **pevlist)
2060 {
2061         unsigned int i, ids, n_ids;
2062         struct perf_evsel *evsel;
2063         struct perf_evlist *evlist = *pevlist;
2064
2065         if (evlist == NULL) {
2066                 *pevlist = evlist = perf_evlist__new(NULL, NULL);
2067                 if (evlist == NULL)
2068                         return -ENOMEM;
2069         }
2070
2071         evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
2072         if (evsel == NULL)
2073                 return -ENOMEM;
2074
2075         perf_evlist__add(evlist, evsel);
2076
2077         ids = event->header.size;
2078         ids -= (void *)&event->attr.id - (void *)event;
2079         n_ids = ids / sizeof(u64);
2080         /*
2081          * We don't have the cpu and thread maps on the header, so
2082          * for allocating the perf_sample_id table we fake 1 cpu and
2083          * hattr->ids threads.
2084          */
2085         if (perf_evsel__alloc_id(evsel, 1, n_ids))
2086                 return -ENOMEM;
2087
2088         for (i = 0; i < n_ids; i++) {
2089                 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2090         }
2091
2092         return 0;
2093 }
2094
2095 int perf_event__synthesize_event_type(struct perf_tool *tool,
2096                                       u64 event_id, char *name,
2097                                       perf_event__handler_t process,
2098                                       struct machine *machine)
2099 {
2100         union perf_event ev;
2101         size_t size = 0;
2102         int err = 0;
2103
2104         memset(&ev, 0, sizeof(ev));
2105
2106         ev.event_type.event_type.event_id = event_id;
2107         memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
2108         strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
2109
2110         ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
2111         size = strlen(name);
2112         size = ALIGN(size, sizeof(u64));
2113         ev.event_type.header.size = sizeof(ev.event_type) -
2114                 (sizeof(ev.event_type.event_type.name) - size);
2115
2116         err = process(tool, &ev, NULL, machine);
2117
2118         return err;
2119 }
2120
2121 int perf_event__synthesize_event_types(struct perf_tool *tool,
2122                                        perf_event__handler_t process,
2123                                        struct machine *machine)
2124 {
2125         struct perf_trace_event_type *type;
2126         int i, err = 0;
2127
2128         for (i = 0; i < event_count; i++) {
2129                 type = &events[i];
2130
2131                 err = perf_event__synthesize_event_type(tool, type->event_id,
2132                                                         type->name, process,
2133                                                         machine);
2134                 if (err) {
2135                         pr_debug("failed to create perf header event type\n");
2136                         return err;
2137                 }
2138         }
2139
2140         return err;
2141 }
2142
2143 int perf_event__process_event_type(struct perf_tool *tool __unused,
2144                                    union perf_event *event)
2145 {
2146         if (perf_header__push_event(event->event_type.event_type.event_id,
2147                                     event->event_type.event_type.name) < 0)
2148                 return -ENOMEM;
2149
2150         return 0;
2151 }
2152
2153 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
2154                                         struct perf_evlist *evlist,
2155                                         perf_event__handler_t process)
2156 {
2157         union perf_event ev;
2158         struct tracing_data *tdata;
2159         ssize_t size = 0, aligned_size = 0, padding;
2160         int err __used = 0;
2161
2162         /*
2163          * We are going to store the size of the data followed
2164          * by the data contents. Since the fd descriptor is a pipe,
2165          * we cannot seek back to store the size of the data once
2166          * we know it. Instead we:
2167          *
2168          * - write the tracing data to the temp file
2169          * - get/write the data size to pipe
2170          * - write the tracing data from the temp file
2171          *   to the pipe
2172          */
2173         tdata = tracing_data_get(&evlist->entries, fd, true);
2174         if (!tdata)
2175                 return -1;
2176
2177         memset(&ev, 0, sizeof(ev));
2178
2179         ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2180         size = tdata->size;
2181         aligned_size = ALIGN(size, sizeof(u64));
2182         padding = aligned_size - size;
2183         ev.tracing_data.header.size = sizeof(ev.tracing_data);
2184         ev.tracing_data.size = aligned_size;
2185
2186         process(tool, &ev, NULL, NULL);
2187
2188         /*
2189          * The put function will copy all the tracing data
2190          * stored in temp file to the pipe.
2191          */
2192         tracing_data_put(tdata);
2193
2194         write_padded(fd, NULL, 0, padding);
2195
2196         return aligned_size;
2197 }
2198
2199 int perf_event__process_tracing_data(union perf_event *event,
2200                                      struct perf_session *session)
2201 {
2202         ssize_t size_read, padding, size = event->tracing_data.size;
2203         off_t offset = lseek(session->fd, 0, SEEK_CUR);
2204         char buf[BUFSIZ];
2205
2206         /* setup for reading amidst mmap */
2207         lseek(session->fd, offset + sizeof(struct tracing_data_event),
2208               SEEK_SET);
2209
2210         size_read = trace_report(session->fd, session->repipe);
2211
2212         padding = ALIGN(size_read, sizeof(u64)) - size_read;
2213
2214         if (read(session->fd, buf, padding) < 0)
2215                 die("reading input file");
2216         if (session->repipe) {
2217                 int retw = write(STDOUT_FILENO, buf, padding);
2218                 if (retw <= 0 || retw != padding)
2219                         die("repiping tracing data padding");
2220         }
2221
2222         if (size_read + padding != size)
2223                 die("tracing data size mismatch");
2224
2225         return size_read + padding;
2226 }
2227
2228 int perf_event__synthesize_build_id(struct perf_tool *tool,
2229                                     struct dso *pos, u16 misc,
2230                                     perf_event__handler_t process,
2231                                     struct machine *machine)
2232 {
2233         union perf_event ev;
2234         size_t len;
2235         int err = 0;
2236
2237         if (!pos->hit)
2238                 return err;
2239
2240         memset(&ev, 0, sizeof(ev));
2241
2242         len = pos->long_name_len + 1;
2243         len = ALIGN(len, NAME_ALIGN);
2244         memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2245         ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2246         ev.build_id.header.misc = misc;
2247         ev.build_id.pid = machine->pid;
2248         ev.build_id.header.size = sizeof(ev.build_id) + len;
2249         memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2250
2251         err = process(tool, &ev, NULL, machine);
2252
2253         return err;
2254 }
2255
2256 int perf_event__process_build_id(struct perf_tool *tool __used,
2257                                  union perf_event *event,
2258                                  struct perf_session *session)
2259 {
2260         __event_process_build_id(&event->build_id,
2261                                  event->build_id.filename,
2262                                  session);
2263         return 0;
2264 }
2265
2266 void disable_buildid_cache(void)
2267 {
2268         no_buildid_cache = true;
2269 }