perf tools: Introduce zalloc() for the common calloc(1, N) case
[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 = zalloc(sizeof(*self));
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 -errno;
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                 int err;
194                 struct build_id_event b;
195                 size_t len;
196
197                 if (!pos->has_build_id)
198                         continue;
199                 len = pos->long_name_len + 1;
200                 len = ALIGN(len, 64);
201                 memset(&b, 0, sizeof(b));
202                 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
203                 b.header.size = sizeof(b) + len;
204                 err = do_write(fd, &b, sizeof(b));
205                 if (err < 0)
206                         return err;
207                 err = do_write(fd, pos->long_name, len);
208                 if (err < 0)
209                         return err;
210         }
211
212         return 0;
213 }
214
215 static int perf_header__adds_write(struct perf_header *self, int fd)
216 {
217         int nr_sections;
218         struct perf_file_section *feat_sec;
219         int sec_size;
220         u64 sec_start;
221         int idx = 0, err;
222
223         if (dsos__read_build_ids())
224                 perf_header__set_feat(self, HEADER_BUILD_ID);
225
226         nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
227         if (!nr_sections)
228                 return 0;
229
230         feat_sec = calloc(sizeof(*feat_sec), nr_sections);
231         if (feat_sec == NULL)
232                 return -ENOMEM;
233
234         sec_size = sizeof(*feat_sec) * nr_sections;
235
236         sec_start = self->data_offset + self->data_size;
237         lseek(fd, sec_start + sec_size, SEEK_SET);
238
239         if (perf_header__has_feat(self, HEADER_TRACE_INFO)) {
240                 struct perf_file_section *trace_sec;
241
242                 trace_sec = &feat_sec[idx++];
243
244                 /* Write trace info */
245                 trace_sec->offset = lseek(fd, 0, SEEK_CUR);
246                 read_tracing_data(fd, attrs, nr_counters);
247                 trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
248         }
249
250
251         if (perf_header__has_feat(self, HEADER_BUILD_ID)) {
252                 struct perf_file_section *buildid_sec;
253
254                 buildid_sec = &feat_sec[idx++];
255
256                 /* Write build-ids */
257                 buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
258                 err = dsos__write_buildid_table(fd);
259                 if (err < 0) {
260                         pr_debug("failed to write buildid table\n");
261                         goto out_free;
262                 }
263                 buildid_sec->size = lseek(fd, 0, SEEK_CUR) - buildid_sec->offset;
264         }
265
266         lseek(fd, sec_start, SEEK_SET);
267         err = do_write(fd, feat_sec, sec_size);
268         if (err < 0)
269                 pr_debug("failed to write feature section\n");
270 out_free:
271         free(feat_sec);
272         return err;
273 }
274
275 int perf_header__write(struct perf_header *self, int fd, bool at_exit)
276 {
277         struct perf_file_header f_header;
278         struct perf_file_attr   f_attr;
279         struct perf_header_attr *attr;
280         int i, err;
281
282         lseek(fd, sizeof(f_header), SEEK_SET);
283
284
285         for (i = 0; i < self->attrs; i++) {
286                 attr = self->attr[i];
287
288                 attr->id_offset = lseek(fd, 0, SEEK_CUR);
289                 err = do_write(fd, attr->id, attr->ids * sizeof(u64));
290                 if (err < 0) {
291                         pr_debug("failed to write perf header\n");
292                         return err;
293                 }
294         }
295
296
297         self->attr_offset = lseek(fd, 0, SEEK_CUR);
298
299         for (i = 0; i < self->attrs; i++) {
300                 attr = self->attr[i];
301
302                 f_attr = (struct perf_file_attr){
303                         .attr = attr->attr,
304                         .ids  = {
305                                 .offset = attr->id_offset,
306                                 .size   = attr->ids * sizeof(u64),
307                         }
308                 };
309                 err = do_write(fd, &f_attr, sizeof(f_attr));
310                 if (err < 0) {
311                         pr_debug("failed to write perf header attribute\n");
312                         return err;
313                 }
314         }
315
316         self->event_offset = lseek(fd, 0, SEEK_CUR);
317         self->event_size = event_count * sizeof(struct perf_trace_event_type);
318         if (events) {
319                 err = do_write(fd, events, self->event_size);
320                 if (err < 0) {
321                         pr_debug("failed to write perf header events\n");
322                         return err;
323                 }
324         }
325
326         self->data_offset = lseek(fd, 0, SEEK_CUR);
327
328         if (at_exit) {
329                 err = perf_header__adds_write(self, fd);
330                 if (err < 0)
331                         return err;
332         }
333
334         f_header = (struct perf_file_header){
335                 .magic     = PERF_MAGIC,
336                 .size      = sizeof(f_header),
337                 .attr_size = sizeof(f_attr),
338                 .attrs = {
339                         .offset = self->attr_offset,
340                         .size   = self->attrs * sizeof(f_attr),
341                 },
342                 .data = {
343                         .offset = self->data_offset,
344                         .size   = self->data_size,
345                 },
346                 .event_types = {
347                         .offset = self->event_offset,
348                         .size   = self->event_size,
349                 },
350         };
351
352         memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features));
353
354         lseek(fd, 0, SEEK_SET);
355         err = do_write(fd, &f_header, sizeof(f_header));
356         if (err < 0) {
357                 pr_debug("failed to write perf header\n");
358                 return err;
359         }
360         lseek(fd, self->data_offset + self->data_size, SEEK_SET);
361
362         self->frozen = 1;
363         return 0;
364 }
365
366 static void do_read(int fd, void *buf, size_t size)
367 {
368         while (size) {
369                 int ret = read(fd, buf, size);
370
371                 if (ret < 0)
372                         die("failed to read");
373                 if (ret == 0)
374                         die("failed to read: missing data");
375
376                 size -= ret;
377                 buf += ret;
378         }
379 }
380
381 int perf_header__process_sections(struct perf_header *self, int fd,
382                                   int (*process)(struct perf_file_section *self,
383                                                  int feat, int fd))
384 {
385         struct perf_file_section *feat_sec;
386         int nr_sections;
387         int sec_size;
388         int idx = 0;
389         int err = 0, feat = 1;
390
391         nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
392         if (!nr_sections)
393                 return 0;
394
395         feat_sec = calloc(sizeof(*feat_sec), nr_sections);
396         if (!feat_sec)
397                 return -1;
398
399         sec_size = sizeof(*feat_sec) * nr_sections;
400
401         lseek(fd, self->data_offset + self->data_size, SEEK_SET);
402
403         do_read(fd, feat_sec, sec_size);
404
405         while (idx < nr_sections && feat < HEADER_LAST_FEATURE) {
406                 if (perf_header__has_feat(self, feat)) {
407                         struct perf_file_section *sec = &feat_sec[idx++];
408
409                         err = process(sec, feat, fd);
410                         if (err < 0)
411                                 break;
412                 }
413                 ++feat;
414         }
415
416         free(feat_sec);
417         return err;
418 };
419
420 int perf_file_header__read(struct perf_file_header *self,
421                            struct perf_header *ph, int fd)
422 {
423         lseek(fd, 0, SEEK_SET);
424         do_read(fd, self, sizeof(*self));
425
426         if (self->magic     != PERF_MAGIC ||
427             self->attr_size != sizeof(struct perf_file_attr))
428                 return -1;
429
430         if (self->size != sizeof(*self)) {
431                 /* Support the previous format */
432                 if (self->size == offsetof(typeof(*self), adds_features))
433                         bitmap_zero(self->adds_features, HEADER_FEAT_BITS);
434                 else
435                         return -1;
436         }
437
438         memcpy(&ph->adds_features, &self->adds_features,
439                sizeof(self->adds_features));
440
441         ph->event_offset = self->event_types.offset;
442         ph->event_size   = self->event_types.size;
443         ph->data_offset  = self->data.offset;
444         ph->data_size    = self->data.size;
445         return 0;
446 }
447
448 static int perf_file_section__process(struct perf_file_section *self,
449                                       int feat, int fd)
450 {
451         if (lseek(fd, self->offset, SEEK_SET) < 0) {
452                 pr_debug("Failed to lseek to %Ld offset for feature %d, "
453                          "continuing...\n", self->offset, feat);
454                 return 0;
455         }
456
457         switch (feat) {
458         case HEADER_TRACE_INFO:
459                 trace_report(fd);
460                 break;
461
462         case HEADER_BUILD_ID:
463                 if (perf_header__read_build_ids(fd, self->offset, self->size))
464                         pr_debug("Failed to read buildids, continuing...\n");
465                 break;
466         default:
467                 pr_debug("unknown feature %d, continuing...\n", feat);
468         }
469
470         return 0;
471 }
472
473 int perf_header__read(struct perf_header *self, int fd)
474 {
475         struct perf_file_header f_header;
476         struct perf_file_attr   f_attr;
477         u64                     f_id;
478         int nr_attrs, nr_ids, i, j;
479
480         if (perf_file_header__read(&f_header, self, fd) < 0) {
481                 pr_debug("incompatible file format\n");
482                 return -EINVAL;
483         }
484
485         nr_attrs = f_header.attrs.size / sizeof(f_attr);
486         lseek(fd, f_header.attrs.offset, SEEK_SET);
487
488         for (i = 0; i < nr_attrs; i++) {
489                 struct perf_header_attr *attr;
490                 off_t tmp;
491
492                 do_read(fd, &f_attr, sizeof(f_attr));
493                 tmp = lseek(fd, 0, SEEK_CUR);
494
495                 attr = perf_header_attr__new(&f_attr.attr);
496                 if (attr == NULL)
497                          return -ENOMEM;
498
499                 nr_ids = f_attr.ids.size / sizeof(u64);
500                 lseek(fd, f_attr.ids.offset, SEEK_SET);
501
502                 for (j = 0; j < nr_ids; j++) {
503                         do_read(fd, &f_id, sizeof(f_id));
504
505                         if (perf_header_attr__add_id(attr, f_id) < 0) {
506                                 perf_header_attr__delete(attr);
507                                 return -ENOMEM;
508                         }
509                 }
510                 if (perf_header__add_attr(self, attr) < 0) {
511                         perf_header_attr__delete(attr);
512                         return -ENOMEM;
513                 }
514
515                 lseek(fd, tmp, SEEK_SET);
516         }
517
518         if (f_header.event_types.size) {
519                 lseek(fd, f_header.event_types.offset, SEEK_SET);
520                 events = malloc(f_header.event_types.size);
521                 if (events == NULL)
522                         return -ENOMEM;
523                 do_read(fd, events, f_header.event_types.size);
524                 event_count =  f_header.event_types.size / sizeof(struct perf_trace_event_type);
525         }
526
527         perf_header__process_sections(self, fd, perf_file_section__process);
528
529         lseek(fd, self->data_offset, SEEK_SET);
530
531         self->frozen = 1;
532         return 0;
533 }
534
535 u64 perf_header__sample_type(struct perf_header *header)
536 {
537         u64 type = 0;
538         int i;
539
540         for (i = 0; i < header->attrs; i++) {
541                 struct perf_header_attr *attr = header->attr[i];
542
543                 if (!type)
544                         type = attr->attr.sample_type;
545                 else if (type != attr->attr.sample_type)
546                         die("non matching sample_type");
547         }
548
549         return type;
550 }
551
552 struct perf_event_attr *
553 perf_header__find_attr(u64 id, struct perf_header *header)
554 {
555         int i;
556
557         for (i = 0; i < header->attrs; i++) {
558                 struct perf_header_attr *attr = header->attr[i];
559                 int j;
560
561                 for (j = 0; j < attr->ids; j++) {
562                         if (attr->id[j] == id)
563                                 return &attr->attr;
564                 }
565         }
566
567         return NULL;
568 }