perf tools: Kill event_t typedef, use 'union perf_event' instead
[linux-2.6.git] / tools / perf / util / header.c
1 #define _FILE_OFFSET_BITS 64
2
3 #include <sys/types.h>
4 #include <byteswap.h>
5 #include <unistd.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <linux/list.h>
9 #include <linux/kernel.h>
10
11 #include "evlist.h"
12 #include "util.h"
13 #include "header.h"
14 #include "../perf.h"
15 #include "trace-event.h"
16 #include "session.h"
17 #include "symbol.h"
18 #include "debug.h"
19
20 static bool no_buildid_cache = false;
21
22 /*
23  * Create new perf.data header attribute:
24  */
25 struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr)
26 {
27         struct perf_header_attr *self = malloc(sizeof(*self));
28
29         if (self != NULL) {
30                 self->attr = *attr;
31                 self->ids  = 0;
32                 self->size = 1;
33                 self->id   = malloc(sizeof(u64));
34                 if (self->id == NULL) {
35                         free(self);
36                         self = NULL;
37                 }
38         }
39
40         return self;
41 }
42
43 void perf_header_attr__delete(struct perf_header_attr *self)
44 {
45         free(self->id);
46         free(self);
47 }
48
49 int perf_header_attr__add_id(struct perf_header_attr *self, u64 id)
50 {
51         int pos = self->ids;
52
53         self->ids++;
54         if (self->ids > self->size) {
55                 int nsize = self->size * 2;
56                 u64 *nid = realloc(self->id, nsize * sizeof(u64));
57
58                 if (nid == NULL)
59                         return -1;
60
61                 self->size = nsize;
62                 self->id = nid;
63         }
64         self->id[pos] = id;
65         return 0;
66 }
67
68 int perf_header__init(struct perf_header *self)
69 {
70         self->size = 1;
71         self->attr = malloc(sizeof(void *));
72         return self->attr == NULL ? -ENOMEM : 0;
73 }
74
75 void perf_header__exit(struct perf_header *self)
76 {
77         int i;
78         for (i = 0; i < self->attrs; ++i)
79                 perf_header_attr__delete(self->attr[i]);
80         free(self->attr);
81 }
82
83 int perf_header__add_attr(struct perf_header *self,
84                           struct perf_header_attr *attr)
85 {
86         if (self->frozen)
87                 return -1;
88
89         if (self->attrs == self->size) {
90                 int nsize = self->size * 2;
91                 struct perf_header_attr **nattr;
92
93                 nattr = realloc(self->attr, nsize * sizeof(void *));
94                 if (nattr == NULL)
95                         return -1;
96
97                 self->size = nsize;
98                 self->attr = nattr;
99         }
100
101         self->attr[self->attrs++] = attr;
102         return 0;
103 }
104
105 static int event_count;
106 static struct perf_trace_event_type *events;
107
108 int perf_header__push_event(u64 id, const char *name)
109 {
110         if (strlen(name) > MAX_EVENT_NAME)
111                 pr_warning("Event %s will be truncated\n", name);
112
113         if (!events) {
114                 events = malloc(sizeof(struct perf_trace_event_type));
115                 if (events == NULL)
116                         return -ENOMEM;
117         } else {
118                 struct perf_trace_event_type *nevents;
119
120                 nevents = realloc(events, (event_count + 1) * sizeof(*events));
121                 if (nevents == NULL)
122                         return -ENOMEM;
123                 events = nevents;
124         }
125         memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
126         events[event_count].event_id = id;
127         strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
128         event_count++;
129         return 0;
130 }
131
132 char *perf_header__find_event(u64 id)
133 {
134         int i;
135         for (i = 0 ; i < event_count; i++) {
136                 if (events[i].event_id == id)
137                         return events[i].name;
138         }
139         return NULL;
140 }
141
142 static const char *__perf_magic = "PERFFILE";
143
144 #define PERF_MAGIC      (*(u64 *)__perf_magic)
145
146 struct perf_file_attr {
147         struct perf_event_attr  attr;
148         struct perf_file_section        ids;
149 };
150
151 void perf_header__set_feat(struct perf_header *self, int feat)
152 {
153         set_bit(feat, self->adds_features);
154 }
155
156 void perf_header__clear_feat(struct perf_header *self, int feat)
157 {
158         clear_bit(feat, self->adds_features);
159 }
160
161 bool perf_header__has_feat(const struct perf_header *self, int feat)
162 {
163         return test_bit(feat, self->adds_features);
164 }
165
166 static int do_write(int fd, const void *buf, size_t size)
167 {
168         while (size) {
169                 int ret = write(fd, buf, size);
170
171                 if (ret < 0)
172                         return -errno;
173
174                 size -= ret;
175                 buf += ret;
176         }
177
178         return 0;
179 }
180
181 #define NAME_ALIGN 64
182
183 static int write_padded(int fd, const void *bf, size_t count,
184                         size_t count_aligned)
185 {
186         static const char zero_buf[NAME_ALIGN];
187         int err = do_write(fd, bf, count);
188
189         if (!err)
190                 err = do_write(fd, zero_buf, count_aligned - count);
191
192         return err;
193 }
194
195 #define dsos__for_each_with_build_id(pos, head) \
196         list_for_each_entry(pos, head, node)    \
197                 if (!pos->has_build_id)         \
198                         continue;               \
199                 else
200
201 static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
202                                 u16 misc, int fd)
203 {
204         struct dso *pos;
205
206         dsos__for_each_with_build_id(pos, head) {
207                 int err;
208                 struct build_id_event b;
209                 size_t len;
210
211                 if (!pos->hit)
212                         continue;
213                 len = pos->long_name_len + 1;
214                 len = ALIGN(len, NAME_ALIGN);
215                 memset(&b, 0, sizeof(b));
216                 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
217                 b.pid = pid;
218                 b.header.misc = misc;
219                 b.header.size = sizeof(b) + len;
220                 err = do_write(fd, &b, sizeof(b));
221                 if (err < 0)
222                         return err;
223                 err = write_padded(fd, pos->long_name,
224                                    pos->long_name_len + 1, len);
225                 if (err < 0)
226                         return err;
227         }
228
229         return 0;
230 }
231
232 static int machine__write_buildid_table(struct machine *self, int fd)
233 {
234         int err;
235         u16 kmisc = PERF_RECORD_MISC_KERNEL,
236             umisc = PERF_RECORD_MISC_USER;
237
238         if (!machine__is_host(self)) {
239                 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
240                 umisc = PERF_RECORD_MISC_GUEST_USER;
241         }
242
243         err = __dsos__write_buildid_table(&self->kernel_dsos, self->pid,
244                                           kmisc, fd);
245         if (err == 0)
246                 err = __dsos__write_buildid_table(&self->user_dsos,
247                                                   self->pid, umisc, fd);
248         return err;
249 }
250
251 static int dsos__write_buildid_table(struct perf_header *header, int fd)
252 {
253         struct perf_session *session = container_of(header,
254                         struct perf_session, header);
255         struct rb_node *nd;
256         int err = machine__write_buildid_table(&session->host_machine, fd);
257
258         if (err)
259                 return err;
260
261         for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
262                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
263                 err = machine__write_buildid_table(pos, fd);
264                 if (err)
265                         break;
266         }
267         return err;
268 }
269
270 int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
271                           const char *name, bool is_kallsyms)
272 {
273         const size_t size = PATH_MAX;
274         char *realname = realpath(name, NULL),
275              *filename = malloc(size),
276              *linkname = malloc(size), *targetname;
277         int len, err = -1;
278
279         if (realname == NULL || filename == NULL || linkname == NULL)
280                 goto out_free;
281
282         len = snprintf(filename, size, "%s%s%s",
283                        debugdir, is_kallsyms ? "/" : "", realname);
284         if (mkdir_p(filename, 0755))
285                 goto out_free;
286
287         snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id);
288
289         if (access(filename, F_OK)) {
290                 if (is_kallsyms) {
291                          if (copyfile("/proc/kallsyms", filename))
292                                 goto out_free;
293                 } else if (link(realname, filename) && copyfile(name, filename))
294                         goto out_free;
295         }
296
297         len = snprintf(linkname, size, "%s/.build-id/%.2s",
298                        debugdir, sbuild_id);
299
300         if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
301                 goto out_free;
302
303         snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
304         targetname = filename + strlen(debugdir) - 5;
305         memcpy(targetname, "../..", 5);
306
307         if (symlink(targetname, linkname) == 0)
308                 err = 0;
309 out_free:
310         free(realname);
311         free(filename);
312         free(linkname);
313         return err;
314 }
315
316 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
317                                  const char *name, const char *debugdir,
318                                  bool is_kallsyms)
319 {
320         char sbuild_id[BUILD_ID_SIZE * 2 + 1];
321
322         build_id__sprintf(build_id, build_id_size, sbuild_id);
323
324         return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms);
325 }
326
327 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
328 {
329         const size_t size = PATH_MAX;
330         char *filename = malloc(size),
331              *linkname = malloc(size);
332         int err = -1;
333
334         if (filename == NULL || linkname == NULL)
335                 goto out_free;
336
337         snprintf(linkname, size, "%s/.build-id/%.2s/%s",
338                  debugdir, sbuild_id, sbuild_id + 2);
339
340         if (access(linkname, F_OK))
341                 goto out_free;
342
343         if (readlink(linkname, filename, size) < 0)
344                 goto out_free;
345
346         if (unlink(linkname))
347                 goto out_free;
348
349         /*
350          * Since the link is relative, we must make it absolute:
351          */
352         snprintf(linkname, size, "%s/.build-id/%.2s/%s",
353                  debugdir, sbuild_id, filename);
354
355         if (unlink(linkname))
356                 goto out_free;
357
358         err = 0;
359 out_free:
360         free(filename);
361         free(linkname);
362         return err;
363 }
364
365 static int dso__cache_build_id(struct dso *self, const char *debugdir)
366 {
367         bool is_kallsyms = self->kernel && self->long_name[0] != '/';
368
369         return build_id_cache__add_b(self->build_id, sizeof(self->build_id),
370                                      self->long_name, debugdir, is_kallsyms);
371 }
372
373 static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
374 {
375         struct dso *pos;
376         int err = 0;
377
378         dsos__for_each_with_build_id(pos, head)
379                 if (dso__cache_build_id(pos, debugdir))
380                         err = -1;
381
382         return err;
383 }
384
385 static int machine__cache_build_ids(struct machine *self, const char *debugdir)
386 {
387         int ret = __dsos__cache_build_ids(&self->kernel_dsos, debugdir);
388         ret |= __dsos__cache_build_ids(&self->user_dsos, debugdir);
389         return ret;
390 }
391
392 static int perf_session__cache_build_ids(struct perf_session *self)
393 {
394         struct rb_node *nd;
395         int ret;
396         char debugdir[PATH_MAX];
397
398         snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
399
400         if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
401                 return -1;
402
403         ret = machine__cache_build_ids(&self->host_machine, debugdir);
404
405         for (nd = rb_first(&self->machines); nd; nd = rb_next(nd)) {
406                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
407                 ret |= machine__cache_build_ids(pos, debugdir);
408         }
409         return ret ? -1 : 0;
410 }
411
412 static bool machine__read_build_ids(struct machine *self, bool with_hits)
413 {
414         bool ret = __dsos__read_build_ids(&self->kernel_dsos, with_hits);
415         ret |= __dsos__read_build_ids(&self->user_dsos, with_hits);
416         return ret;
417 }
418
419 static bool perf_session__read_build_ids(struct perf_session *self, bool with_hits)
420 {
421         struct rb_node *nd;
422         bool ret = machine__read_build_ids(&self->host_machine, with_hits);
423
424         for (nd = rb_first(&self->machines); nd; nd = rb_next(nd)) {
425                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
426                 ret |= machine__read_build_ids(pos, with_hits);
427         }
428
429         return ret;
430 }
431
432 static int perf_header__adds_write(struct perf_header *self,
433                                    struct perf_evlist *evlist, int fd)
434 {
435         int nr_sections;
436         struct perf_session *session;
437         struct perf_file_section *feat_sec;
438         int sec_size;
439         u64 sec_start;
440         int idx = 0, err;
441
442         session = container_of(self, struct perf_session, header);
443
444         if (perf_header__has_feat(self, HEADER_BUILD_ID &&
445             !perf_session__read_build_ids(session, true)))
446                 perf_header__clear_feat(self, HEADER_BUILD_ID);
447
448         nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
449         if (!nr_sections)
450                 return 0;
451
452         feat_sec = calloc(sizeof(*feat_sec), nr_sections);
453         if (feat_sec == NULL)
454                 return -ENOMEM;
455
456         sec_size = sizeof(*feat_sec) * nr_sections;
457
458         sec_start = self->data_offset + self->data_size;
459         lseek(fd, sec_start + sec_size, SEEK_SET);
460
461         if (perf_header__has_feat(self, HEADER_TRACE_INFO)) {
462                 struct perf_file_section *trace_sec;
463
464                 trace_sec = &feat_sec[idx++];
465
466                 /* Write trace info */
467                 trace_sec->offset = lseek(fd, 0, SEEK_CUR);
468                 read_tracing_data(fd, &evlist->entries);
469                 trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
470         }
471
472         if (perf_header__has_feat(self, HEADER_BUILD_ID)) {
473                 struct perf_file_section *buildid_sec;
474
475                 buildid_sec = &feat_sec[idx++];
476
477                 /* Write build-ids */
478                 buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
479                 err = dsos__write_buildid_table(self, fd);
480                 if (err < 0) {
481                         pr_debug("failed to write buildid table\n");
482                         goto out_free;
483                 }
484                 buildid_sec->size = lseek(fd, 0, SEEK_CUR) -
485                                           buildid_sec->offset;
486                 if (!no_buildid_cache)
487                         perf_session__cache_build_ids(session);
488         }
489
490         lseek(fd, sec_start, SEEK_SET);
491         err = do_write(fd, feat_sec, sec_size);
492         if (err < 0)
493                 pr_debug("failed to write feature section\n");
494 out_free:
495         free(feat_sec);
496         return err;
497 }
498
499 int perf_header__write_pipe(int fd)
500 {
501         struct perf_pipe_file_header f_header;
502         int err;
503
504         f_header = (struct perf_pipe_file_header){
505                 .magic     = PERF_MAGIC,
506                 .size      = sizeof(f_header),
507         };
508
509         err = do_write(fd, &f_header, sizeof(f_header));
510         if (err < 0) {
511                 pr_debug("failed to write perf pipe header\n");
512                 return err;
513         }
514
515         return 0;
516 }
517
518 int perf_header__write(struct perf_header *self, struct perf_evlist *evlist,
519                        int fd, bool at_exit)
520 {
521         struct perf_file_header f_header;
522         struct perf_file_attr   f_attr;
523         struct perf_header_attr *attr;
524         int i, err;
525
526         lseek(fd, sizeof(f_header), SEEK_SET);
527
528         for (i = 0; i < self->attrs; i++) {
529                 attr = self->attr[i];
530
531                 attr->id_offset = lseek(fd, 0, SEEK_CUR);
532                 err = do_write(fd, attr->id, attr->ids * sizeof(u64));
533                 if (err < 0) {
534                         pr_debug("failed to write perf header\n");
535                         return err;
536                 }
537         }
538
539
540         self->attr_offset = lseek(fd, 0, SEEK_CUR);
541
542         for (i = 0; i < self->attrs; i++) {
543                 attr = self->attr[i];
544
545                 f_attr = (struct perf_file_attr){
546                         .attr = attr->attr,
547                         .ids  = {
548                                 .offset = attr->id_offset,
549                                 .size   = attr->ids * sizeof(u64),
550                         }
551                 };
552                 err = do_write(fd, &f_attr, sizeof(f_attr));
553                 if (err < 0) {
554                         pr_debug("failed to write perf header attribute\n");
555                         return err;
556                 }
557         }
558
559         self->event_offset = lseek(fd, 0, SEEK_CUR);
560         self->event_size = event_count * sizeof(struct perf_trace_event_type);
561         if (events) {
562                 err = do_write(fd, events, self->event_size);
563                 if (err < 0) {
564                         pr_debug("failed to write perf header events\n");
565                         return err;
566                 }
567         }
568
569         self->data_offset = lseek(fd, 0, SEEK_CUR);
570
571         if (at_exit) {
572                 err = perf_header__adds_write(self, evlist, fd);
573                 if (err < 0)
574                         return err;
575         }
576
577         f_header = (struct perf_file_header){
578                 .magic     = PERF_MAGIC,
579                 .size      = sizeof(f_header),
580                 .attr_size = sizeof(f_attr),
581                 .attrs = {
582                         .offset = self->attr_offset,
583                         .size   = self->attrs * sizeof(f_attr),
584                 },
585                 .data = {
586                         .offset = self->data_offset,
587                         .size   = self->data_size,
588                 },
589                 .event_types = {
590                         .offset = self->event_offset,
591                         .size   = self->event_size,
592                 },
593         };
594
595         memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features));
596
597         lseek(fd, 0, SEEK_SET);
598         err = do_write(fd, &f_header, sizeof(f_header));
599         if (err < 0) {
600                 pr_debug("failed to write perf header\n");
601                 return err;
602         }
603         lseek(fd, self->data_offset + self->data_size, SEEK_SET);
604
605         self->frozen = 1;
606         return 0;
607 }
608
609 static int perf_header__getbuffer64(struct perf_header *self,
610                                     int fd, void *buf, size_t size)
611 {
612         if (readn(fd, buf, size) <= 0)
613                 return -1;
614
615         if (self->needs_swap)
616                 mem_bswap_64(buf, size);
617
618         return 0;
619 }
620
621 int perf_header__process_sections(struct perf_header *self, int fd,
622                                   int (*process)(struct perf_file_section *self,
623                                                  struct perf_header *ph,
624                                                  int feat, int fd))
625 {
626         struct perf_file_section *feat_sec;
627         int nr_sections;
628         int sec_size;
629         int idx = 0;
630         int err = -1, feat = 1;
631
632         nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
633         if (!nr_sections)
634                 return 0;
635
636         feat_sec = calloc(sizeof(*feat_sec), nr_sections);
637         if (!feat_sec)
638                 return -1;
639
640         sec_size = sizeof(*feat_sec) * nr_sections;
641
642         lseek(fd, self->data_offset + self->data_size, SEEK_SET);
643
644         if (perf_header__getbuffer64(self, fd, feat_sec, sec_size))
645                 goto out_free;
646
647         err = 0;
648         while (idx < nr_sections && feat < HEADER_LAST_FEATURE) {
649                 if (perf_header__has_feat(self, feat)) {
650                         struct perf_file_section *sec = &feat_sec[idx++];
651
652                         err = process(sec, self, feat, fd);
653                         if (err < 0)
654                                 break;
655                 }
656                 ++feat;
657         }
658 out_free:
659         free(feat_sec);
660         return err;
661 }
662
663 int perf_file_header__read(struct perf_file_header *self,
664                            struct perf_header *ph, int fd)
665 {
666         lseek(fd, 0, SEEK_SET);
667
668         if (readn(fd, self, sizeof(*self)) <= 0 ||
669             memcmp(&self->magic, __perf_magic, sizeof(self->magic)))
670                 return -1;
671
672         if (self->attr_size != sizeof(struct perf_file_attr)) {
673                 u64 attr_size = bswap_64(self->attr_size);
674
675                 if (attr_size != sizeof(struct perf_file_attr))
676                         return -1;
677
678                 mem_bswap_64(self, offsetof(struct perf_file_header,
679                                             adds_features));
680                 ph->needs_swap = true;
681         }
682
683         if (self->size != sizeof(*self)) {
684                 /* Support the previous format */
685                 if (self->size == offsetof(typeof(*self), adds_features))
686                         bitmap_zero(self->adds_features, HEADER_FEAT_BITS);
687                 else
688                         return -1;
689         }
690
691         memcpy(&ph->adds_features, &self->adds_features,
692                sizeof(ph->adds_features));
693         /*
694          * FIXME: hack that assumes that if we need swap the perf.data file
695          * may be coming from an arch with a different word-size, ergo different
696          * DEFINE_BITMAP format, investigate more later, but for now its mostly
697          * safe to assume that we have a build-id section. Trace files probably
698          * have several other issues in this realm anyway...
699          */
700         if (ph->needs_swap) {
701                 memset(&ph->adds_features, 0, sizeof(ph->adds_features));
702                 perf_header__set_feat(ph, HEADER_BUILD_ID);
703         }
704
705         ph->event_offset = self->event_types.offset;
706         ph->event_size   = self->event_types.size;
707         ph->data_offset  = self->data.offset;
708         ph->data_size    = self->data.size;
709         return 0;
710 }
711
712 static int __event_process_build_id(struct build_id_event *bev,
713                                     char *filename,
714                                     struct perf_session *session)
715 {
716         int err = -1;
717         struct list_head *head;
718         struct machine *machine;
719         u16 misc;
720         struct dso *dso;
721         enum dso_kernel_type dso_type;
722
723         machine = perf_session__findnew_machine(session, bev->pid);
724         if (!machine)
725                 goto out;
726
727         misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
728
729         switch (misc) {
730         case PERF_RECORD_MISC_KERNEL:
731                 dso_type = DSO_TYPE_KERNEL;
732                 head = &machine->kernel_dsos;
733                 break;
734         case PERF_RECORD_MISC_GUEST_KERNEL:
735                 dso_type = DSO_TYPE_GUEST_KERNEL;
736                 head = &machine->kernel_dsos;
737                 break;
738         case PERF_RECORD_MISC_USER:
739         case PERF_RECORD_MISC_GUEST_USER:
740                 dso_type = DSO_TYPE_USER;
741                 head = &machine->user_dsos;
742                 break;
743         default:
744                 goto out;
745         }
746
747         dso = __dsos__findnew(head, filename);
748         if (dso != NULL) {
749                 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
750
751                 dso__set_build_id(dso, &bev->build_id);
752
753                 if (filename[0] == '[')
754                         dso->kernel = dso_type;
755
756                 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
757                                   sbuild_id);
758                 pr_debug("build id event received for %s: %s\n",
759                          dso->long_name, sbuild_id);
760         }
761
762         err = 0;
763 out:
764         return err;
765 }
766
767 static int perf_header__read_build_ids(struct perf_header *self,
768                         int input, u64 offset, u64 size)
769 {
770         struct perf_session *session = container_of(self,
771                         struct perf_session, header);
772         struct build_id_event bev;
773         char filename[PATH_MAX];
774         u64 limit = offset + size;
775         int err = -1;
776
777         while (offset < limit) {
778                 ssize_t len;
779
780                 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
781                         goto out;
782
783                 if (self->needs_swap)
784                         perf_event_header__bswap(&bev.header);
785
786                 len = bev.header.size - sizeof(bev);
787                 if (read(input, filename, len) != len)
788                         goto out;
789
790                 __event_process_build_id(&bev, filename, session);
791
792                 offset += bev.header.size;
793         }
794         err = 0;
795 out:
796         return err;
797 }
798
799 static int perf_file_section__process(struct perf_file_section *self,
800                                       struct perf_header *ph,
801                                       int feat, int fd)
802 {
803         if (lseek(fd, self->offset, SEEK_SET) == (off_t)-1) {
804                 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
805                           "%d, continuing...\n", self->offset, feat);
806                 return 0;
807         }
808
809         switch (feat) {
810         case HEADER_TRACE_INFO:
811                 trace_report(fd, false);
812                 break;
813
814         case HEADER_BUILD_ID:
815                 if (perf_header__read_build_ids(ph, fd, self->offset, self->size))
816                         pr_debug("Failed to read buildids, continuing...\n");
817                 break;
818         default:
819                 pr_debug("unknown feature %d, continuing...\n", feat);
820         }
821
822         return 0;
823 }
824
825 static int perf_file_header__read_pipe(struct perf_pipe_file_header *self,
826                                        struct perf_header *ph, int fd,
827                                        bool repipe)
828 {
829         if (readn(fd, self, sizeof(*self)) <= 0 ||
830             memcmp(&self->magic, __perf_magic, sizeof(self->magic)))
831                 return -1;
832
833         if (repipe && do_write(STDOUT_FILENO, self, sizeof(*self)) < 0)
834                 return -1;
835
836         if (self->size != sizeof(*self)) {
837                 u64 size = bswap_64(self->size);
838
839                 if (size != sizeof(*self))
840                         return -1;
841
842                 ph->needs_swap = true;
843         }
844
845         return 0;
846 }
847
848 static int perf_header__read_pipe(struct perf_session *session, int fd)
849 {
850         struct perf_header *self = &session->header;
851         struct perf_pipe_file_header f_header;
852
853         if (perf_file_header__read_pipe(&f_header, self, fd,
854                                         session->repipe) < 0) {
855                 pr_debug("incompatible file format\n");
856                 return -EINVAL;
857         }
858
859         session->fd = fd;
860
861         return 0;
862 }
863
864 int perf_header__read(struct perf_session *session, int fd)
865 {
866         struct perf_header *self = &session->header;
867         struct perf_file_header f_header;
868         struct perf_file_attr   f_attr;
869         u64                     f_id;
870         int nr_attrs, nr_ids, i, j;
871
872         if (session->fd_pipe)
873                 return perf_header__read_pipe(session, fd);
874
875         if (perf_file_header__read(&f_header, self, fd) < 0) {
876                 pr_debug("incompatible file format\n");
877                 return -EINVAL;
878         }
879
880         nr_attrs = f_header.attrs.size / sizeof(f_attr);
881         lseek(fd, f_header.attrs.offset, SEEK_SET);
882
883         for (i = 0; i < nr_attrs; i++) {
884                 struct perf_header_attr *attr;
885                 off_t tmp;
886
887                 if (perf_header__getbuffer64(self, fd, &f_attr, sizeof(f_attr)))
888                         goto out_errno;
889
890                 tmp = lseek(fd, 0, SEEK_CUR);
891
892                 attr = perf_header_attr__new(&f_attr.attr);
893                 if (attr == NULL)
894                          return -ENOMEM;
895
896                 nr_ids = f_attr.ids.size / sizeof(u64);
897                 lseek(fd, f_attr.ids.offset, SEEK_SET);
898
899                 for (j = 0; j < nr_ids; j++) {
900                         if (perf_header__getbuffer64(self, fd, &f_id, sizeof(f_id)))
901                                 goto out_errno;
902
903                         if (perf_header_attr__add_id(attr, f_id) < 0) {
904                                 perf_header_attr__delete(attr);
905                                 return -ENOMEM;
906                         }
907                 }
908                 if (perf_header__add_attr(self, attr) < 0) {
909                         perf_header_attr__delete(attr);
910                         return -ENOMEM;
911                 }
912
913                 lseek(fd, tmp, SEEK_SET);
914         }
915
916         if (f_header.event_types.size) {
917                 lseek(fd, f_header.event_types.offset, SEEK_SET);
918                 events = malloc(f_header.event_types.size);
919                 if (events == NULL)
920                         return -ENOMEM;
921                 if (perf_header__getbuffer64(self, fd, events,
922                                              f_header.event_types.size))
923                         goto out_errno;
924                 event_count =  f_header.event_types.size / sizeof(struct perf_trace_event_type);
925         }
926
927         perf_header__process_sections(self, fd, perf_file_section__process);
928
929         lseek(fd, self->data_offset, SEEK_SET);
930
931         self->frozen = 1;
932         return 0;
933 out_errno:
934         return -errno;
935 }
936
937 u64 perf_header__sample_type(struct perf_header *header)
938 {
939         u64 type = 0;
940         int i;
941
942         for (i = 0; i < header->attrs; i++) {
943                 struct perf_header_attr *attr = header->attr[i];
944
945                 if (!type)
946                         type = attr->attr.sample_type;
947                 else if (type != attr->attr.sample_type)
948                         die("non matching sample_type");
949         }
950
951         return type;
952 }
953
954 bool perf_header__sample_id_all(const struct perf_header *header)
955 {
956         bool value = false, first = true;
957         int i;
958
959         for (i = 0; i < header->attrs; i++) {
960                 struct perf_header_attr *attr = header->attr[i];
961
962                 if (first) {
963                         value = attr->attr.sample_id_all;
964                         first = false;
965                 } else if (value != attr->attr.sample_id_all)
966                         die("non matching sample_id_all");
967         }
968
969         return value;
970 }
971
972 struct perf_event_attr *
973 perf_header__find_attr(u64 id, struct perf_header *header)
974 {
975         int i;
976
977         /*
978          * We set id to -1 if the data file doesn't contain sample
979          * ids. This can happen when the data file contains one type
980          * of event and in that case, the header can still store the
981          * event attribute information. Check for this and avoid
982          * walking through the entire list of ids which may be large.
983          */
984         if (id == -1ULL) {
985                 if (header->attrs > 0)
986                         return &header->attr[0]->attr;
987                 return NULL;
988         }
989
990         for (i = 0; i < header->attrs; i++) {
991                 struct perf_header_attr *attr = header->attr[i];
992                 int j;
993
994                 for (j = 0; j < attr->ids; j++) {
995                         if (attr->id[j] == id)
996                                 return &attr->attr;
997                 }
998         }
999
1000         return NULL;
1001 }
1002
1003 int perf_event__synthesize_attr(struct perf_event_attr *attr, u16 ids, u64 *id,
1004                                 perf_event__handler_t process,
1005                                 struct perf_session *session)
1006 {
1007         union perf_event *ev;
1008         size_t size;
1009         int err;
1010
1011         size = sizeof(struct perf_event_attr);
1012         size = ALIGN(size, sizeof(u64));
1013         size += sizeof(struct perf_event_header);
1014         size += ids * sizeof(u64);
1015
1016         ev = malloc(size);
1017
1018         if (ev == NULL)
1019                 return -ENOMEM;
1020
1021         ev->attr.attr = *attr;
1022         memcpy(ev->attr.id, id, ids * sizeof(u64));
1023
1024         ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
1025         ev->attr.header.size = size;
1026
1027         err = process(ev, NULL, session);
1028
1029         free(ev);
1030
1031         return err;
1032 }
1033
1034 int perf_event__synthesize_attrs(struct perf_header *self,
1035                                  perf_event__handler_t process,
1036                                  struct perf_session *session)
1037 {
1038         struct perf_header_attr *attr;
1039         int i, err = 0;
1040
1041         for (i = 0; i < self->attrs; i++) {
1042                 attr = self->attr[i];
1043
1044                 err = perf_event__synthesize_attr(&attr->attr, attr->ids,
1045                                                   attr->id, process, session);
1046                 if (err) {
1047                         pr_debug("failed to create perf header attribute\n");
1048                         return err;
1049                 }
1050         }
1051
1052         return err;
1053 }
1054
1055 int perf_event__process_attr(union perf_event *event,
1056                              struct perf_session *session)
1057 {
1058         struct perf_header_attr *attr;
1059         unsigned int i, ids, n_ids;
1060
1061         attr = perf_header_attr__new(&event->attr.attr);
1062         if (attr == NULL)
1063                 return -ENOMEM;
1064
1065         ids = event->header.size;
1066         ids -= (void *)&event->attr.id - (void *)event;
1067         n_ids = ids / sizeof(u64);
1068
1069         for (i = 0; i < n_ids; i++) {
1070                 if (perf_header_attr__add_id(attr, event->attr.id[i]) < 0) {
1071                         perf_header_attr__delete(attr);
1072                         return -ENOMEM;
1073                 }
1074         }
1075
1076         if (perf_header__add_attr(&session->header, attr) < 0) {
1077                 perf_header_attr__delete(attr);
1078                 return -ENOMEM;
1079         }
1080
1081         perf_session__update_sample_type(session);
1082
1083         return 0;
1084 }
1085
1086 int perf_event__synthesize_event_type(u64 event_id, char *name,
1087                                       perf_event__handler_t process,
1088                                       struct perf_session *session)
1089 {
1090         union perf_event ev;
1091         size_t size = 0;
1092         int err = 0;
1093
1094         memset(&ev, 0, sizeof(ev));
1095
1096         ev.event_type.event_type.event_id = event_id;
1097         memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
1098         strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
1099
1100         ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
1101         size = strlen(name);
1102         size = ALIGN(size, sizeof(u64));
1103         ev.event_type.header.size = sizeof(ev.event_type) -
1104                 (sizeof(ev.event_type.event_type.name) - size);
1105
1106         err = process(&ev, NULL, session);
1107
1108         return err;
1109 }
1110
1111 int perf_event__synthesize_event_types(perf_event__handler_t process,
1112                                        struct perf_session *session)
1113 {
1114         struct perf_trace_event_type *type;
1115         int i, err = 0;
1116
1117         for (i = 0; i < event_count; i++) {
1118                 type = &events[i];
1119
1120                 err = perf_event__synthesize_event_type(type->event_id,
1121                                                         type->name, process,
1122                                                         session);
1123                 if (err) {
1124                         pr_debug("failed to create perf header event type\n");
1125                         return err;
1126                 }
1127         }
1128
1129         return err;
1130 }
1131
1132 int perf_event__process_event_type(union perf_event *event,
1133                                    struct perf_session *session __unused)
1134 {
1135         if (perf_header__push_event(event->event_type.event_type.event_id,
1136                                     event->event_type.event_type.name) < 0)
1137                 return -ENOMEM;
1138
1139         return 0;
1140 }
1141
1142 int perf_event__synthesize_tracing_data(int fd, struct perf_evlist *evlist,
1143                                          perf_event__handler_t process,
1144                                    struct perf_session *session __unused)
1145 {
1146         union perf_event ev;
1147         ssize_t size = 0, aligned_size = 0, padding;
1148         int err = 0;
1149
1150         memset(&ev, 0, sizeof(ev));
1151
1152         ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
1153         size = read_tracing_data_size(fd, &evlist->entries);
1154         if (size <= 0)
1155                 return size;
1156         aligned_size = ALIGN(size, sizeof(u64));
1157         padding = aligned_size - size;
1158         ev.tracing_data.header.size = sizeof(ev.tracing_data);
1159         ev.tracing_data.size = aligned_size;
1160
1161         process(&ev, NULL, session);
1162
1163         err = read_tracing_data(fd, &evlist->entries);
1164         write_padded(fd, NULL, 0, padding);
1165
1166         return aligned_size;
1167 }
1168
1169 int perf_event__process_tracing_data(union perf_event *event,
1170                                      struct perf_session *session)
1171 {
1172         ssize_t size_read, padding, size = event->tracing_data.size;
1173         off_t offset = lseek(session->fd, 0, SEEK_CUR);
1174         char buf[BUFSIZ];
1175
1176         /* setup for reading amidst mmap */
1177         lseek(session->fd, offset + sizeof(struct tracing_data_event),
1178               SEEK_SET);
1179
1180         size_read = trace_report(session->fd, session->repipe);
1181
1182         padding = ALIGN(size_read, sizeof(u64)) - size_read;
1183
1184         if (read(session->fd, buf, padding) < 0)
1185                 die("reading input file");
1186         if (session->repipe) {
1187                 int retw = write(STDOUT_FILENO, buf, padding);
1188                 if (retw <= 0 || retw != padding)
1189                         die("repiping tracing data padding");
1190         }
1191
1192         if (size_read + padding != size)
1193                 die("tracing data size mismatch");
1194
1195         return size_read + padding;
1196 }
1197
1198 int perf_event__synthesize_build_id(struct dso *pos, u16 misc,
1199                                     perf_event__handler_t process,
1200                                     struct machine *machine,
1201                                     struct perf_session *session)
1202 {
1203         union perf_event ev;
1204         size_t len;
1205         int err = 0;
1206
1207         if (!pos->hit)
1208                 return err;
1209
1210         memset(&ev, 0, sizeof(ev));
1211
1212         len = pos->long_name_len + 1;
1213         len = ALIGN(len, NAME_ALIGN);
1214         memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
1215         ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
1216         ev.build_id.header.misc = misc;
1217         ev.build_id.pid = machine->pid;
1218         ev.build_id.header.size = sizeof(ev.build_id) + len;
1219         memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
1220
1221         err = process(&ev, NULL, session);
1222
1223         return err;
1224 }
1225
1226 int perf_event__process_build_id(union perf_event *event,
1227                                  struct perf_session *session)
1228 {
1229         __event_process_build_id(&event->build_id,
1230                                  event->build_id.filename,
1231                                  session);
1232         return 0;
1233 }
1234
1235 void disable_buildid_cache(void)
1236 {
1237         no_buildid_cache = true;
1238 }