perf tools: perf_header__read() shouldn't die()
[linux-2.6.git] / tools / perf / util / header.c
1 #include <sys/types.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <linux/list.h>
6
7 #include "util.h"
8 #include "header.h"
9 #include "../perf.h"
10 #include "trace-event.h"
11 #include "symbol.h"
12 #include "data_map.h"
13 #include "debug.h"
14
15 /*
16  * Create new perf.data header attribute:
17  */
18 struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr)
19 {
20         struct perf_header_attr *self = malloc(sizeof(*self));
21
22         if (self != NULL) {
23                 self->attr = *attr;
24                 self->ids  = 0;
25                 self->size = 1;
26                 self->id   = malloc(sizeof(u64));
27                 if (self->id == NULL) {
28                         free(self);
29                         self = NULL;
30                 }
31         }
32
33         return self;
34 }
35
36 void perf_header_attr__delete(struct perf_header_attr *self)
37 {
38         free(self->id);
39         free(self);
40 }
41
42 int perf_header_attr__add_id(struct perf_header_attr *self, u64 id)
43 {
44         int pos = self->ids;
45
46         self->ids++;
47         if (self->ids > self->size) {
48                 int nsize = self->size * 2;
49                 u64 *nid = realloc(self->id, nsize * sizeof(u64));
50
51                 if (nid == NULL)
52                         return -1;
53
54                 self->size = nsize;
55                 self->id = nid;
56         }
57         self->id[pos] = id;
58         return 0;
59 }
60
61 /*
62  * Create new perf.data header:
63  */
64 struct perf_header *perf_header__new(void)
65 {
66         struct perf_header *self = calloc(sizeof(*self), 1);
67
68         if (self != NULL) {
69                 self->size = 1;
70                 self->attr = malloc(sizeof(void *));
71
72                 if (self->attr == NULL) {
73                         free(self);
74                         self = NULL;
75                 }
76         }
77
78         return self;
79 }
80
81 void perf_header__delete(struct perf_header *self)
82 {
83         int i;
84
85         for (i = 0; i < self->attrs; ++i)
86                 perf_header_attr__delete(self->attr[i]);
87
88         free(self->attr);
89         free(self);
90 }
91
92 int perf_header__add_attr(struct perf_header *self,
93                           struct perf_header_attr *attr)
94 {
95         if (self->frozen)
96                 return -1;
97
98         if (self->attrs == self->size) {
99                 int nsize = self->size * 2;
100                 struct perf_header_attr **nattr;
101
102                 nattr = realloc(self->attr, nsize * sizeof(void *));
103                 if (nattr == NULL)
104                         return -1;
105
106                 self->size = nsize;
107                 self->attr = nattr;
108         }
109
110         self->attr[self->attrs++] = attr;
111         return 0;
112 }
113
114 #define MAX_EVENT_NAME 64
115
116 struct perf_trace_event_type {
117         u64     event_id;
118         char    name[MAX_EVENT_NAME];
119 };
120
121 static int event_count;
122 static struct perf_trace_event_type *events;
123
124 void perf_header__push_event(u64 id, const char *name)
125 {
126         if (strlen(name) > MAX_EVENT_NAME)
127                 pr_warning("Event %s will be truncated\n", name);
128
129         if (!events) {
130                 events = malloc(sizeof(struct perf_trace_event_type));
131                 if (!events)
132                         die("nomem");
133         } else {
134                 events = realloc(events, (event_count + 1) * sizeof(struct perf_trace_event_type));
135                 if (!events)
136                         die("nomem");
137         }
138         memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
139         events[event_count].event_id = id;
140         strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
141         event_count++;
142 }
143
144 char *perf_header__find_event(u64 id)
145 {
146         int i;
147         for (i = 0 ; i < event_count; i++) {
148                 if (events[i].event_id == id)
149                         return events[i].name;
150         }
151         return NULL;
152 }
153
154 static const char *__perf_magic = "PERFFILE";
155
156 #define PERF_MAGIC      (*(u64 *)__perf_magic)
157
158 struct perf_file_attr {
159         struct perf_event_attr  attr;
160         struct perf_file_section        ids;
161 };
162
163 void perf_header__set_feat(struct perf_header *self, int feat)
164 {
165         set_bit(feat, self->adds_features);
166 }
167
168 bool perf_header__has_feat(const struct perf_header *self, int feat)
169 {
170         return test_bit(feat, self->adds_features);
171 }
172
173 static int do_write(int fd, const void *buf, size_t size)
174 {
175         while (size) {
176                 int ret = write(fd, buf, size);
177
178                 if (ret < 0)
179                         return -1;
180
181                 size -= ret;
182                 buf += ret;
183         }
184
185         return 0;
186 }
187
188 static int dsos__write_buildid_table(int fd)
189 {
190         struct dso *pos;
191
192         list_for_each_entry(pos, &dsos, node) {
193                 struct build_id_event b;
194                 size_t len;
195
196                 if (!pos->has_build_id)
197                         continue;
198                 len = pos->long_name_len + 1;
199                 len = ALIGN(len, 64);
200                 memset(&b, 0, sizeof(b));
201                 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
202                 b.header.size = sizeof(b) + len;
203                 if (do_write(fd, &b, sizeof(b)) < 0 ||
204                     do_write(fd, pos->long_name, len) < 0)
205                         return -1;
206         }
207
208         return 0;
209 }
210
211 static void
212 perf_header__adds_write(struct perf_header *self, int fd)
213 {
214         int nr_sections;
215         struct perf_file_section *feat_sec;
216         int sec_size;
217         u64 sec_start;
218         int idx = 0;
219
220         if (dsos__read_build_ids())
221                 perf_header__set_feat(self, HEADER_BUILD_ID);
222
223         nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
224         if (!nr_sections)
225                 return;
226
227         feat_sec = calloc(sizeof(*feat_sec), nr_sections);
228         if (!feat_sec)
229                 die("No memory");
230
231         sec_size = sizeof(*feat_sec) * nr_sections;
232
233         sec_start = self->data_offset + self->data_size;
234         lseek(fd, sec_start + sec_size, SEEK_SET);
235
236         if (perf_header__has_feat(self, HEADER_TRACE_INFO)) {
237                 struct perf_file_section *trace_sec;
238
239                 trace_sec = &feat_sec[idx++];
240
241                 /* Write trace info */
242                 trace_sec->offset = lseek(fd, 0, SEEK_CUR);
243                 read_tracing_data(fd, attrs, nr_counters);
244                 trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
245         }
246
247
248         if (perf_header__has_feat(self, HEADER_BUILD_ID)) {
249                 struct perf_file_section *buildid_sec;
250
251                 buildid_sec = &feat_sec[idx++];
252
253                 dsos__load_kernel();
254                 /*
255                  * Read the list of loaded modules with its build_ids
256                  */
257                 dsos__load_modules();
258
259                 /* Write build-ids */
260                 buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
261                 if (dsos__write_buildid_table(fd) < 0)
262                         die("failed to write buildid table");
263                 buildid_sec->size = lseek(fd, 0, SEEK_CUR) - buildid_sec->offset;
264         }
265
266         lseek(fd, sec_start, SEEK_SET);
267         if (do_write(fd, feat_sec, sec_size) < 0)
268                 die("failed to write feature section");
269         free(feat_sec);
270 }
271
272 void perf_header__write(struct perf_header *self, int fd, bool at_exit)
273 {
274         struct perf_file_header f_header;
275         struct perf_file_attr   f_attr;
276         struct perf_header_attr *attr;
277         int i;
278
279         lseek(fd, sizeof(f_header), SEEK_SET);
280
281
282         for (i = 0; i < self->attrs; i++) {
283                 attr = self->attr[i];
284
285                 attr->id_offset = lseek(fd, 0, SEEK_CUR);
286                 if (do_write(fd, attr->id, attr->ids * sizeof(u64)) < 0)
287                         die("failed to write perf header");
288         }
289
290
291         self->attr_offset = lseek(fd, 0, SEEK_CUR);
292
293         for (i = 0; i < self->attrs; i++) {
294                 attr = self->attr[i];
295
296                 f_attr = (struct perf_file_attr){
297                         .attr = attr->attr,
298                         .ids  = {
299                                 .offset = attr->id_offset,
300                                 .size   = attr->ids * sizeof(u64),
301                         }
302                 };
303                 if (do_write(fd, &f_attr, sizeof(f_attr)) < 0)
304                         die("failed to write perf header attribute");
305         }
306
307         self->event_offset = lseek(fd, 0, SEEK_CUR);
308         self->event_size = event_count * sizeof(struct perf_trace_event_type);
309         if (events)
310                 if (do_write(fd, events, self->event_size) < 0)
311                         die("failed to write perf header events");
312
313         self->data_offset = lseek(fd, 0, SEEK_CUR);
314
315         if (at_exit)
316                 perf_header__adds_write(self, fd);
317
318         f_header = (struct perf_file_header){
319                 .magic     = PERF_MAGIC,
320                 .size      = sizeof(f_header),
321                 .attr_size = sizeof(f_attr),
322                 .attrs = {
323                         .offset = self->attr_offset,
324                         .size   = self->attrs * sizeof(f_attr),
325                 },
326                 .data = {
327                         .offset = self->data_offset,
328                         .size   = self->data_size,
329                 },
330                 .event_types = {
331                         .offset = self->event_offset,
332                         .size   = self->event_size,
333                 },
334         };
335
336         memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features));
337
338         lseek(fd, 0, SEEK_SET);
339         if (do_write(fd, &f_header, sizeof(f_header)) < 0)
340                 die("failed to write perf header");
341         lseek(fd, self->data_offset + self->data_size, SEEK_SET);
342
343         self->frozen = 1;
344 }
345
346 static void do_read(int fd, void *buf, size_t size)
347 {
348         while (size) {
349                 int ret = read(fd, buf, size);
350
351                 if (ret < 0)
352                         die("failed to read");
353                 if (ret == 0)
354                         die("failed to read: missing data");
355
356                 size -= ret;
357                 buf += ret;
358         }
359 }
360
361 int perf_header__process_sections(struct perf_header *self, int fd,
362                                   int (*process)(struct perf_file_section *self,
363                                                  int feat, int fd))
364 {
365         struct perf_file_section *feat_sec;
366         int nr_sections;
367         int sec_size;
368         int idx = 0;
369         int err = 0, feat = 1;
370
371         nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
372         if (!nr_sections)
373                 return 0;
374
375         feat_sec = calloc(sizeof(*feat_sec), nr_sections);
376         if (!feat_sec)
377                 return -1;
378
379         sec_size = sizeof(*feat_sec) * nr_sections;
380
381         lseek(fd, self->data_offset + self->data_size, SEEK_SET);
382
383         do_read(fd, feat_sec, sec_size);
384
385         while (idx < nr_sections && feat < HEADER_LAST_FEATURE) {
386                 if (perf_header__has_feat(self, feat)) {
387                         struct perf_file_section *sec = &feat_sec[idx++];
388
389                         err = process(sec, feat, fd);
390                         if (err < 0)
391                                 break;
392                 }
393                 ++feat;
394         }
395
396         free(feat_sec);
397         return err;
398 };
399
400 int perf_file_header__read(struct perf_file_header *self,
401                            struct perf_header *ph, int fd)
402 {
403         lseek(fd, 0, SEEK_SET);
404         do_read(fd, self, sizeof(*self));
405
406         if (self->magic     != PERF_MAGIC ||
407             self->attr_size != sizeof(struct perf_file_attr))
408                 return -1;
409
410         if (self->size != sizeof(*self)) {
411                 /* Support the previous format */
412                 if (self->size == offsetof(typeof(*self), adds_features))
413                         bitmap_zero(self->adds_features, HEADER_FEAT_BITS);
414                 else
415                         return -1;
416         }
417
418         memcpy(&ph->adds_features, &self->adds_features,
419                sizeof(self->adds_features));
420
421         ph->event_offset = self->event_types.offset;
422         ph->event_size   = self->event_types.size;
423         ph->data_offset  = self->data.offset;
424         ph->data_size    = self->data.size;
425         return 0;
426 }
427
428 static int perf_file_section__process(struct perf_file_section *self,
429                                       int feat, int fd)
430 {
431         if (lseek(fd, self->offset, SEEK_SET) < 0) {
432                 pr_debug("Failed to lseek to %Ld offset for feature %d, "
433                          "continuing...\n", self->offset, feat);
434                 return 0;
435         }
436
437         switch (feat) {
438         case HEADER_TRACE_INFO:
439                 trace_report(fd);
440                 break;
441
442         case HEADER_BUILD_ID:
443                 if (perf_header__read_build_ids(fd, self->offset, self->size))
444                         pr_debug("Failed to read buildids, continuing...\n");
445                 break;
446         default:
447                 pr_debug("unknown feature %d, continuing...\n", feat);
448         }
449
450         return 0;
451 }
452
453 int perf_header__read(struct perf_header *self, int fd)
454 {
455         struct perf_file_header f_header;
456         struct perf_file_attr   f_attr;
457         u64                     f_id;
458         int nr_attrs, nr_ids, i, j;
459
460         if (perf_file_header__read(&f_header, self, fd) < 0) {
461                 pr_debug("incompatible file format\n");
462                 return -EINVAL;
463         }
464
465         nr_attrs = f_header.attrs.size / sizeof(f_attr);
466         lseek(fd, f_header.attrs.offset, SEEK_SET);
467
468         for (i = 0; i < nr_attrs; i++) {
469                 struct perf_header_attr *attr;
470                 off_t tmp;
471
472                 do_read(fd, &f_attr, sizeof(f_attr));
473                 tmp = lseek(fd, 0, SEEK_CUR);
474
475                 attr = perf_header_attr__new(&f_attr.attr);
476                 if (attr == NULL)
477                          return -ENOMEM;
478
479                 nr_ids = f_attr.ids.size / sizeof(u64);
480                 lseek(fd, f_attr.ids.offset, SEEK_SET);
481
482                 for (j = 0; j < nr_ids; j++) {
483                         do_read(fd, &f_id, sizeof(f_id));
484
485                         if (perf_header_attr__add_id(attr, f_id) < 0) {
486                                 perf_header_attr__delete(attr);
487                                 return -ENOMEM;
488                         }
489                 }
490                 if (perf_header__add_attr(self, attr) < 0) {
491                         perf_header_attr__delete(attr);
492                         return -ENOMEM;
493                 }
494
495                 lseek(fd, tmp, SEEK_SET);
496         }
497
498         if (f_header.event_types.size) {
499                 lseek(fd, f_header.event_types.offset, SEEK_SET);
500                 events = malloc(f_header.event_types.size);
501                 if (events == NULL)
502                         return -ENOMEM;
503                 do_read(fd, events, f_header.event_types.size);
504                 event_count =  f_header.event_types.size / sizeof(struct perf_trace_event_type);
505         }
506
507         perf_header__process_sections(self, fd, perf_file_section__process);
508
509         lseek(fd, self->data_offset, SEEK_SET);
510
511         self->frozen = 1;
512         return 0;
513 }
514
515 u64 perf_header__sample_type(struct perf_header *header)
516 {
517         u64 type = 0;
518         int i;
519
520         for (i = 0; i < header->attrs; i++) {
521                 struct perf_header_attr *attr = header->attr[i];
522
523                 if (!type)
524                         type = attr->attr.sample_type;
525                 else if (type != attr->attr.sample_type)
526                         die("non matching sample_type");
527         }
528
529         return type;
530 }
531
532 struct perf_event_attr *
533 perf_header__find_attr(u64 id, struct perf_header *header)
534 {
535         int i;
536
537         for (i = 0; i < header->attrs; i++) {
538                 struct perf_header_attr *attr = header->attr[i];
539                 int j;
540
541                 for (j = 0; j < attr->ids; j++) {
542                         if (attr->id[j] == id)
543                                 return &attr->attr;
544                 }
545         }
546
547         return NULL;
548 }