perf record: Stop intercepting events, use postprocessing to get build-ids
[linux-2.6.git] / tools / perf / util / header.c
1 #define _LARGEFILE64_SOURCE
2 #define _FILE_OFFSET_BITS 64
3
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
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 /*
21  * Create new perf.data header attribute:
22  */
23 struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr)
24 {
25         struct perf_header_attr *self = malloc(sizeof(*self));
26
27         if (self != NULL) {
28                 self->attr = *attr;
29                 self->ids  = 0;
30                 self->size = 1;
31                 self->id   = malloc(sizeof(u64));
32                 if (self->id == NULL) {
33                         free(self);
34                         self = NULL;
35                 }
36         }
37
38         return self;
39 }
40
41 void perf_header_attr__delete(struct perf_header_attr *self)
42 {
43         free(self->id);
44         free(self);
45 }
46
47 int perf_header_attr__add_id(struct perf_header_attr *self, u64 id)
48 {
49         int pos = self->ids;
50
51         self->ids++;
52         if (self->ids > self->size) {
53                 int nsize = self->size * 2;
54                 u64 *nid = realloc(self->id, nsize * sizeof(u64));
55
56                 if (nid == NULL)
57                         return -1;
58
59                 self->size = nsize;
60                 self->id = nid;
61         }
62         self->id[pos] = id;
63         return 0;
64 }
65
66 int perf_header__init(struct perf_header *self)
67 {
68         self->size = 1;
69         self->attr = malloc(sizeof(void *));
70         return self->attr == NULL ? -ENOMEM : 0;
71 }
72
73 void perf_header__exit(struct perf_header *self)
74 {
75         int i;
76         for (i = 0; i < self->attrs; ++i)
77                 perf_header_attr__delete(self->attr[i]);
78         free(self->attr);
79 }
80
81 int perf_header__add_attr(struct perf_header *self,
82                           struct perf_header_attr *attr)
83 {
84         if (self->frozen)
85                 return -1;
86
87         if (self->attrs == self->size) {
88                 int nsize = self->size * 2;
89                 struct perf_header_attr **nattr;
90
91                 nattr = realloc(self->attr, nsize * sizeof(void *));
92                 if (nattr == NULL)
93                         return -1;
94
95                 self->size = nsize;
96                 self->attr = nattr;
97         }
98
99         self->attr[self->attrs++] = attr;
100         return 0;
101 }
102
103 #define MAX_EVENT_NAME 64
104
105 struct perf_trace_event_type {
106         u64     event_id;
107         char    name[MAX_EVENT_NAME];
108 };
109
110 static int event_count;
111 static struct perf_trace_event_type *events;
112
113 int perf_header__push_event(u64 id, const char *name)
114 {
115         if (strlen(name) > MAX_EVENT_NAME)
116                 pr_warning("Event %s will be truncated\n", name);
117
118         if (!events) {
119                 events = malloc(sizeof(struct perf_trace_event_type));
120                 if (events == NULL)
121                         return -ENOMEM;
122         } else {
123                 struct perf_trace_event_type *nevents;
124
125                 nevents = realloc(events, (event_count + 1) * sizeof(*events));
126                 if (nevents == NULL)
127                         return -ENOMEM;
128                 events = nevents;
129         }
130         memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
131         events[event_count].event_id = id;
132         strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
133         event_count++;
134         return 0;
135 }
136
137 char *perf_header__find_event(u64 id)
138 {
139         int i;
140         for (i = 0 ; i < event_count; i++) {
141                 if (events[i].event_id == id)
142                         return events[i].name;
143         }
144         return NULL;
145 }
146
147 static const char *__perf_magic = "PERFFILE";
148
149 #define PERF_MAGIC      (*(u64 *)__perf_magic)
150
151 struct perf_file_attr {
152         struct perf_event_attr  attr;
153         struct perf_file_section        ids;
154 };
155
156 void perf_header__set_feat(struct perf_header *self, int feat)
157 {
158         set_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, u16 misc, int fd)
202 {
203         struct dso *pos;
204
205         dsos__for_each_with_build_id(pos, head) {
206                 int err;
207                 struct build_id_event b;
208                 size_t len;
209
210                 if (!pos->hit)
211                         continue;
212                 len = pos->long_name_len + 1;
213                 len = ALIGN(len, NAME_ALIGN);
214                 memset(&b, 0, sizeof(b));
215                 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
216                 b.header.misc = misc;
217                 b.header.size = sizeof(b) + len;
218                 err = do_write(fd, &b, sizeof(b));
219                 if (err < 0)
220                         return err;
221                 err = write_padded(fd, pos->long_name,
222                                    pos->long_name_len + 1, len);
223                 if (err < 0)
224                         return err;
225         }
226
227         return 0;
228 }
229
230 static int dsos__write_buildid_table(int fd)
231 {
232         int err = __dsos__write_buildid_table(&dsos__kernel,
233                                               PERF_RECORD_MISC_KERNEL, fd);
234         if (err == 0)
235                 err = __dsos__write_buildid_table(&dsos__user,
236                                                   PERF_RECORD_MISC_USER, fd);
237         return err;
238 }
239
240 int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
241                           const char *name, bool is_kallsyms)
242 {
243         const size_t size = PATH_MAX;
244         char *filename = malloc(size),
245              *linkname = malloc(size), *targetname;
246         int len, err = -1;
247
248         if (filename == NULL || linkname == NULL)
249                 goto out_free;
250
251         len = snprintf(filename, size, "%s%s%s",
252                        debugdir, is_kallsyms ? "/" : "", name);
253         if (mkdir_p(filename, 0755))
254                 goto out_free;
255
256         snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id);
257
258         if (access(filename, F_OK)) {
259                 if (is_kallsyms) {
260                          if (copyfile("/proc/kallsyms", filename))
261                                 goto out_free;
262                 } else if (link(name, filename) && copyfile(name, filename))
263                         goto out_free;
264         }
265
266         len = snprintf(linkname, size, "%s/.build-id/%.2s",
267                        debugdir, sbuild_id);
268
269         if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
270                 goto out_free;
271
272         snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
273         targetname = filename + strlen(debugdir) - 5;
274         memcpy(targetname, "../..", 5);
275
276         if (symlink(targetname, linkname) == 0)
277                 err = 0;
278 out_free:
279         free(filename);
280         free(linkname);
281         return err;
282 }
283
284 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
285                                  const char *name, const char *debugdir,
286                                  bool is_kallsyms)
287 {
288         char sbuild_id[BUILD_ID_SIZE * 2 + 1];
289
290         build_id__sprintf(build_id, build_id_size, sbuild_id);
291
292         return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms);
293 }
294
295 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
296 {
297         const size_t size = PATH_MAX;
298         char *filename = malloc(size),
299              *linkname = malloc(size);
300         int err = -1;
301
302         if (filename == NULL || linkname == NULL)
303                 goto out_free;
304
305         snprintf(linkname, size, "%s/.build-id/%.2s/%s",
306                  debugdir, sbuild_id, sbuild_id + 2);
307
308         if (access(linkname, F_OK))
309                 goto out_free;
310
311         if (readlink(linkname, filename, size) < 0)
312                 goto out_free;
313
314         if (unlink(linkname))
315                 goto out_free;
316
317         /*
318          * Since the link is relative, we must make it absolute:
319          */
320         snprintf(linkname, size, "%s/.build-id/%.2s/%s",
321                  debugdir, sbuild_id, filename);
322
323         if (unlink(linkname))
324                 goto out_free;
325
326         err = 0;
327 out_free:
328         free(filename);
329         free(linkname);
330         return err;
331 }
332
333 static int dso__cache_build_id(struct dso *self, const char *debugdir)
334 {
335         bool is_kallsyms = self->kernel && self->long_name[0] != '/';
336
337         return build_id_cache__add_b(self->build_id, sizeof(self->build_id),
338                                      self->long_name, debugdir, is_kallsyms);
339 }
340
341 static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
342 {
343         struct dso *pos;
344         int err = 0;
345
346         dsos__for_each_with_build_id(pos, head)
347                 if (dso__cache_build_id(pos, debugdir))
348                         err = -1;
349
350         return err;
351 }
352
353 static int dsos__cache_build_ids(void)
354 {
355         int err_kernel, err_user;
356         char debugdir[PATH_MAX];
357
358         snprintf(debugdir, sizeof(debugdir), "%s/%s", getenv("HOME"),
359                  DEBUG_CACHE_DIR);
360
361         if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
362                 return -1;
363
364         err_kernel = __dsos__cache_build_ids(&dsos__kernel, debugdir);
365         err_user   = __dsos__cache_build_ids(&dsos__user, debugdir);
366         return err_kernel || err_user ? -1 : 0;
367 }
368
369 static int perf_header__adds_write(struct perf_header *self, int fd)
370 {
371         int nr_sections;
372         struct perf_file_section *feat_sec;
373         int sec_size;
374         u64 sec_start;
375         int idx = 0, err;
376
377         if (dsos__read_build_ids(true))
378                 perf_header__set_feat(self, HEADER_BUILD_ID);
379
380         nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
381         if (!nr_sections)
382                 return 0;
383
384         feat_sec = calloc(sizeof(*feat_sec), nr_sections);
385         if (feat_sec == NULL)
386                 return -ENOMEM;
387
388         sec_size = sizeof(*feat_sec) * nr_sections;
389
390         sec_start = self->data_offset + self->data_size;
391         lseek64(fd, sec_start + sec_size, SEEK_SET);
392
393         if (perf_header__has_feat(self, HEADER_TRACE_INFO)) {
394                 struct perf_file_section *trace_sec;
395
396                 trace_sec = &feat_sec[idx++];
397
398                 /* Write trace info */
399                 trace_sec->offset = lseek64(fd, 0, SEEK_CUR);
400                 read_tracing_data(fd, attrs, nr_counters);
401                 trace_sec->size = lseek64(fd, 0, SEEK_CUR) - trace_sec->offset;
402         }
403
404
405         if (perf_header__has_feat(self, HEADER_BUILD_ID)) {
406                 struct perf_file_section *buildid_sec;
407
408                 buildid_sec = &feat_sec[idx++];
409
410                 /* Write build-ids */
411                 buildid_sec->offset = lseek64(fd, 0, SEEK_CUR);
412                 err = dsos__write_buildid_table(fd);
413                 if (err < 0) {
414                         pr_debug("failed to write buildid table\n");
415                         goto out_free;
416                 }
417                 buildid_sec->size = lseek64(fd, 0, SEEK_CUR) -
418                                             buildid_sec->offset;
419                 dsos__cache_build_ids();
420         }
421
422         lseek64(fd, sec_start, SEEK_SET);
423         err = do_write(fd, feat_sec, sec_size);
424         if (err < 0)
425                 pr_debug("failed to write feature section\n");
426 out_free:
427         free(feat_sec);
428         return err;
429 }
430
431 int perf_header__write(struct perf_header *self, int fd, bool at_exit)
432 {
433         struct perf_file_header f_header;
434         struct perf_file_attr   f_attr;
435         struct perf_header_attr *attr;
436         int i, err;
437
438         lseek(fd, sizeof(f_header), SEEK_SET);
439
440
441         for (i = 0; i < self->attrs; i++) {
442                 attr = self->attr[i];
443
444                 attr->id_offset = lseek(fd, 0, SEEK_CUR);
445                 err = do_write(fd, attr->id, attr->ids * sizeof(u64));
446                 if (err < 0) {
447                         pr_debug("failed to write perf header\n");
448                         return err;
449                 }
450         }
451
452
453         self->attr_offset = lseek(fd, 0, SEEK_CUR);
454
455         for (i = 0; i < self->attrs; i++) {
456                 attr = self->attr[i];
457
458                 f_attr = (struct perf_file_attr){
459                         .attr = attr->attr,
460                         .ids  = {
461                                 .offset = attr->id_offset,
462                                 .size   = attr->ids * sizeof(u64),
463                         }
464                 };
465                 err = do_write(fd, &f_attr, sizeof(f_attr));
466                 if (err < 0) {
467                         pr_debug("failed to write perf header attribute\n");
468                         return err;
469                 }
470         }
471
472         self->event_offset = lseek(fd, 0, SEEK_CUR);
473         self->event_size = event_count * sizeof(struct perf_trace_event_type);
474         if (events) {
475                 err = do_write(fd, events, self->event_size);
476                 if (err < 0) {
477                         pr_debug("failed to write perf header events\n");
478                         return err;
479                 }
480         }
481
482         self->data_offset = lseek(fd, 0, SEEK_CUR);
483
484         if (at_exit) {
485                 err = perf_header__adds_write(self, fd);
486                 if (err < 0)
487                         return err;
488         }
489
490         f_header = (struct perf_file_header){
491                 .magic     = PERF_MAGIC,
492                 .size      = sizeof(f_header),
493                 .attr_size = sizeof(f_attr),
494                 .attrs = {
495                         .offset = self->attr_offset,
496                         .size   = self->attrs * sizeof(f_attr),
497                 },
498                 .data = {
499                         .offset = self->data_offset,
500                         .size   = self->data_size,
501                 },
502                 .event_types = {
503                         .offset = self->event_offset,
504                         .size   = self->event_size,
505                 },
506         };
507
508         memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features));
509
510         lseek(fd, 0, SEEK_SET);
511         err = do_write(fd, &f_header, sizeof(f_header));
512         if (err < 0) {
513                 pr_debug("failed to write perf header\n");
514                 return err;
515         }
516         lseek64(fd, self->data_offset + self->data_size, SEEK_SET);
517
518         self->frozen = 1;
519         return 0;
520 }
521
522 static int do_read(int fd, void *buf, size_t size)
523 {
524         while (size) {
525                 int ret = read(fd, buf, size);
526
527                 if (ret <= 0)
528                         return -1;
529
530                 size -= ret;
531                 buf += ret;
532         }
533
534         return 0;
535 }
536
537 static int perf_header__getbuffer64(struct perf_header *self,
538                                     int fd, void *buf, size_t size)
539 {
540         if (do_read(fd, buf, size))
541                 return -1;
542
543         if (self->needs_swap)
544                 mem_bswap_64(buf, size);
545
546         return 0;
547 }
548
549 int perf_header__process_sections(struct perf_header *self, int fd,
550                                   int (*process)(struct perf_file_section *self,
551                                                  struct perf_header *ph,
552                                                  int feat, int fd))
553 {
554         struct perf_file_section *feat_sec;
555         int nr_sections;
556         int sec_size;
557         int idx = 0;
558         int err = -1, feat = 1;
559
560         nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
561         if (!nr_sections)
562                 return 0;
563
564         feat_sec = calloc(sizeof(*feat_sec), nr_sections);
565         if (!feat_sec)
566                 return -1;
567
568         sec_size = sizeof(*feat_sec) * nr_sections;
569
570         lseek64(fd, self->data_offset + self->data_size, SEEK_SET);
571
572         if (perf_header__getbuffer64(self, fd, feat_sec, sec_size))
573                 goto out_free;
574
575         err = 0;
576         while (idx < nr_sections && feat < HEADER_LAST_FEATURE) {
577                 if (perf_header__has_feat(self, feat)) {
578                         struct perf_file_section *sec = &feat_sec[idx++];
579
580                         err = process(sec, self, feat, fd);
581                         if (err < 0)
582                                 break;
583                 }
584                 ++feat;
585         }
586 out_free:
587         free(feat_sec);
588         return err;
589 }
590
591 int perf_file_header__read(struct perf_file_header *self,
592                            struct perf_header *ph, int fd)
593 {
594         lseek(fd, 0, SEEK_SET);
595
596         if (do_read(fd, self, sizeof(*self)) ||
597             memcmp(&self->magic, __perf_magic, sizeof(self->magic)))
598                 return -1;
599
600         if (self->attr_size != sizeof(struct perf_file_attr)) {
601                 u64 attr_size = bswap_64(self->attr_size);
602
603                 if (attr_size != sizeof(struct perf_file_attr))
604                         return -1;
605
606                 mem_bswap_64(self, offsetof(struct perf_file_header,
607                                             adds_features));
608                 ph->needs_swap = true;
609         }
610
611         if (self->size != sizeof(*self)) {
612                 /* Support the previous format */
613                 if (self->size == offsetof(typeof(*self), adds_features))
614                         bitmap_zero(self->adds_features, HEADER_FEAT_BITS);
615                 else
616                         return -1;
617         }
618
619         memcpy(&ph->adds_features, &self->adds_features,
620                sizeof(ph->adds_features));
621         /*
622          * FIXME: hack that assumes that if we need swap the perf.data file
623          * may be coming from an arch with a different word-size, ergo different
624          * DEFINE_BITMAP format, investigate more later, but for now its mostly
625          * safe to assume that we have a build-id section. Trace files probably
626          * have several other issues in this realm anyway...
627          */
628         if (ph->needs_swap) {
629                 memset(&ph->adds_features, 0, sizeof(ph->adds_features));
630                 perf_header__set_feat(ph, HEADER_BUILD_ID);
631         }
632
633         ph->event_offset = self->event_types.offset;
634         ph->event_size   = self->event_types.size;
635         ph->data_offset  = self->data.offset;
636         ph->data_size    = self->data.size;
637         return 0;
638 }
639
640 static int perf_file_section__process(struct perf_file_section *self,
641                                       struct perf_header *ph,
642                                       int feat, int fd)
643 {
644         if (lseek64(fd, self->offset, SEEK_SET) < 0) {
645                 pr_debug("Failed to lseek to %Ld offset for feature %d, "
646                          "continuing...\n", self->offset, feat);
647                 return 0;
648         }
649
650         switch (feat) {
651         case HEADER_TRACE_INFO:
652                 trace_report(fd);
653                 break;
654
655         case HEADER_BUILD_ID:
656                 if (perf_header__read_build_ids(ph, fd, self->offset, self->size))
657                         pr_debug("Failed to read buildids, continuing...\n");
658                 break;
659         default:
660                 pr_debug("unknown feature %d, continuing...\n", feat);
661         }
662
663         return 0;
664 }
665
666 int perf_header__read(struct perf_header *self, int fd)
667 {
668         struct perf_file_header f_header;
669         struct perf_file_attr   f_attr;
670         u64                     f_id;
671         int nr_attrs, nr_ids, i, j;
672
673         if (perf_file_header__read(&f_header, self, fd) < 0) {
674                 pr_debug("incompatible file format\n");
675                 return -EINVAL;
676         }
677
678         nr_attrs = f_header.attrs.size / sizeof(f_attr);
679         lseek(fd, f_header.attrs.offset, SEEK_SET);
680
681         for (i = 0; i < nr_attrs; i++) {
682                 struct perf_header_attr *attr;
683                 off_t tmp;
684
685                 if (perf_header__getbuffer64(self, fd, &f_attr, sizeof(f_attr)))
686                         goto out_errno;
687
688                 tmp = lseek(fd, 0, SEEK_CUR);
689
690                 attr = perf_header_attr__new(&f_attr.attr);
691                 if (attr == NULL)
692                          return -ENOMEM;
693
694                 nr_ids = f_attr.ids.size / sizeof(u64);
695                 lseek(fd, f_attr.ids.offset, SEEK_SET);
696
697                 for (j = 0; j < nr_ids; j++) {
698                         if (perf_header__getbuffer64(self, fd, &f_id, sizeof(f_id)))
699                                 goto out_errno;
700
701                         if (perf_header_attr__add_id(attr, f_id) < 0) {
702                                 perf_header_attr__delete(attr);
703                                 return -ENOMEM;
704                         }
705                 }
706                 if (perf_header__add_attr(self, attr) < 0) {
707                         perf_header_attr__delete(attr);
708                         return -ENOMEM;
709                 }
710
711                 lseek(fd, tmp, SEEK_SET);
712         }
713
714         if (f_header.event_types.size) {
715                 lseek(fd, f_header.event_types.offset, SEEK_SET);
716                 events = malloc(f_header.event_types.size);
717                 if (events == NULL)
718                         return -ENOMEM;
719                 if (perf_header__getbuffer64(self, fd, events,
720                                              f_header.event_types.size))
721                         goto out_errno;
722                 event_count =  f_header.event_types.size / sizeof(struct perf_trace_event_type);
723         }
724
725         perf_header__process_sections(self, fd, perf_file_section__process);
726
727         lseek(fd, self->data_offset, SEEK_SET);
728
729         self->frozen = 1;
730         return 0;
731 out_errno:
732         return -errno;
733 }
734
735 u64 perf_header__sample_type(struct perf_header *header)
736 {
737         u64 type = 0;
738         int i;
739
740         for (i = 0; i < header->attrs; i++) {
741                 struct perf_header_attr *attr = header->attr[i];
742
743                 if (!type)
744                         type = attr->attr.sample_type;
745                 else if (type != attr->attr.sample_type)
746                         die("non matching sample_type");
747         }
748
749         return type;
750 }
751
752 struct perf_event_attr *
753 perf_header__find_attr(u64 id, struct perf_header *header)
754 {
755         int i;
756
757         for (i = 0; i < header->attrs; i++) {
758                 struct perf_header_attr *attr = header->attr[i];
759                 int j;
760
761                 for (j = 0; j < attr->ids; j++) {
762                         if (attr->id[j] == id)
763                                 return &attr->attr;
764                 }
765         }
766
767         return NULL;
768 }