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