blob: 8a0bca55106f8d4ddb15492404367ef5b3f3de88 [file] [log] [blame]
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001#include <sys/types.h>
2#include <unistd.h>
3#include <stdio.h>
4#include <stdlib.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +01005#include <linux/list.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02006
7#include "util.h"
8#include "header.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +02009#include "../perf.h"
10#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020011#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010012#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010013#include "debug.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020014
15/*
Arjan van de Venec60a3f2009-09-19 13:36:30 +020016 * Create new perf.data header attribute:
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020017 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020018struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020019{
20 struct perf_header_attr *self = malloc(sizeof(*self));
21
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -020022 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 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020032
33 return self;
34}
35
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020036void perf_header_attr__delete(struct perf_header_attr *self)
37{
38 free(self->id);
39 free(self);
40}
41
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -020042int perf_header_attr__add_id(struct perf_header_attr *self, u64 id)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020043{
44 int pos = self->ids;
45
46 self->ids++;
47 if (self->ids > self->size) {
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -020048 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;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020056 }
57 self->id[pos] = id;
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -020058 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020059}
60
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020061int perf_header__init(struct perf_header *self)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020062{
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020063 self->size = 1;
64 self->attr = malloc(sizeof(void *));
65 return self->attr == NULL ? -ENOMEM : 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020066}
67
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020068void perf_header__exit(struct perf_header *self)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020069{
70 int i;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020071 for (i = 0; i < self->attrs; ++i)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020072 perf_header_attr__delete(self->attr[i]);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020073 free(self->attr);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020074}
75
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020076int perf_header__add_attr(struct perf_header *self,
77 struct perf_header_attr *attr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020078{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020079 if (self->frozen)
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020080 return -1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020081
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020082 if (self->attrs == self->size) {
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020083 int nsize = self->size * 2;
84 struct perf_header_attr **nattr;
85
86 nattr = realloc(self->attr, nsize * sizeof(void *));
87 if (nattr == NULL)
88 return -1;
89
90 self->size = nsize;
91 self->attr = nattr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020092 }
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020093
94 self->attr[self->attrs++] = attr;
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020095 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020096}
97
Arjan van de Venec60a3f2009-09-19 13:36:30 +020098#define MAX_EVENT_NAME 64
99
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200100struct perf_trace_event_type {
101 u64 event_id;
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200102 char name[MAX_EVENT_NAME];
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200103};
104
105static int event_count;
106static struct perf_trace_event_type *events;
107
108void perf_header__push_event(u64 id, const char *name)
109{
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200110 if (strlen(name) > MAX_EVENT_NAME)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200111 pr_warning("Event %s will be truncated\n", name);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200112
113 if (!events) {
114 events = malloc(sizeof(struct perf_trace_event_type));
115 if (!events)
116 die("nomem");
117 } else {
118 events = realloc(events, (event_count + 1) * sizeof(struct perf_trace_event_type));
119 if (!events)
120 die("nomem");
121 }
122 memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
123 events[event_count].event_id = id;
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200124 strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200125 event_count++;
126}
127
128char *perf_header__find_event(u64 id)
129{
130 int i;
131 for (i = 0 ; i < event_count; i++) {
132 if (events[i].event_id == id)
133 return events[i].name;
134 }
135 return NULL;
136}
137
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200138static const char *__perf_magic = "PERFFILE";
139
140#define PERF_MAGIC (*(u64 *)__perf_magic)
141
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200142struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200143 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200144 struct perf_file_section ids;
145};
146
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200147void perf_header__set_feat(struct perf_header *self, int feat)
148{
149 set_bit(feat, self->adds_features);
150}
151
152bool perf_header__has_feat(const struct perf_header *self, int feat)
153{
154 return test_bit(feat, self->adds_features);
155}
156
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200157static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200158{
159 while (size) {
160 int ret = write(fd, buf, size);
161
162 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200163 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200164
165 size -= ret;
166 buf += ret;
167 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200168
169 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200170}
171
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200172static int __dsos__write_buildid_table(struct list_head *head, int fd)
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100173{
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900174#define NAME_ALIGN 64
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200175 struct dso *pos;
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900176 static const char zero_buf[NAME_ALIGN];
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100177
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200178 list_for_each_entry(pos, head, node) {
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200179 int err;
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200180 struct build_id_event b;
181 size_t len;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100182
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200183 if (!pos->has_build_id)
184 continue;
185 len = pos->long_name_len + 1;
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900186 len = ALIGN(len, NAME_ALIGN);
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200187 memset(&b, 0, sizeof(b));
188 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
189 b.header.size = sizeof(b) + len;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200190 err = do_write(fd, &b, sizeof(b));
191 if (err < 0)
192 return err;
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900193 err = do_write(fd, pos->long_name, pos->long_name_len + 1);
194 if (err < 0)
195 return err;
Xiao Guangrongd9541ed2009-12-07 12:07:15 +0800196 err = do_write(fd, zero_buf, len - pos->long_name_len - 1);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200197 if (err < 0)
198 return err;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100199 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200200
201 return 0;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100202}
203
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200204static int dsos__write_buildid_table(int fd)
205{
206 int err = __dsos__write_buildid_table(&dsos__kernel, fd);
207 if (err == 0)
208 err = __dsos__write_buildid_table(&dsos__user, fd);
209 return err;
210}
211
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200212static int perf_header__adds_write(struct perf_header *self, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200213{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100214 int nr_sections;
215 struct perf_file_section *feat_sec;
216 int sec_size;
217 u64 sec_start;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200218 int idx = 0, err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100219
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200220 if (dsos__read_build_ids())
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100221 perf_header__set_feat(self, HEADER_BUILD_ID);
222
223 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
224 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200225 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100226
227 feat_sec = calloc(sizeof(*feat_sec), nr_sections);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200228 if (feat_sec == NULL)
229 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100230
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);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200235
Frederic Weisbecker3e13ab22009-11-11 04:51:06 +0100236 if (perf_header__has_feat(self, HEADER_TRACE_INFO)) {
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100237 struct perf_file_section *trace_sec;
238
239 trace_sec = &feat_sec[idx++];
240
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200241 /* Write trace info */
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100242 trace_sec->offset = lseek(fd, 0, SEEK_CUR);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200243 read_tracing_data(fd, attrs, nr_counters);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100244 trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200245 }
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100246
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100247
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100248 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 /* Write build-ids */
254 buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200255 err = dsos__write_buildid_table(fd);
256 if (err < 0) {
257 pr_debug("failed to write buildid table\n");
258 goto out_free;
259 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100260 buildid_sec->size = lseek(fd, 0, SEEK_CUR) - buildid_sec->offset;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100261 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100262
263 lseek(fd, sec_start, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200264 err = do_write(fd, feat_sec, sec_size);
265 if (err < 0)
266 pr_debug("failed to write feature section\n");
267out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100268 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200269 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100270}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200271
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200272int perf_header__write(struct perf_header *self, int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200273{
274 struct perf_file_header f_header;
275 struct perf_file_attr f_attr;
276 struct perf_header_attr *attr;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200277 int i, err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200278
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);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200286 err = do_write(fd, attr->id, attr->ids * sizeof(u64));
287 if (err < 0) {
288 pr_debug("failed to write perf header\n");
289 return err;
290 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200291 }
292
293
294 self->attr_offset = lseek(fd, 0, SEEK_CUR);
295
296 for (i = 0; i < self->attrs; i++) {
297 attr = self->attr[i];
298
299 f_attr = (struct perf_file_attr){
300 .attr = attr->attr,
301 .ids = {
302 .offset = attr->id_offset,
303 .size = attr->ids * sizeof(u64),
304 }
305 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200306 err = do_write(fd, &f_attr, sizeof(f_attr));
307 if (err < 0) {
308 pr_debug("failed to write perf header attribute\n");
309 return err;
310 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200311 }
312
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200313 self->event_offset = lseek(fd, 0, SEEK_CUR);
314 self->event_size = event_count * sizeof(struct perf_trace_event_type);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200315 if (events) {
316 err = do_write(fd, events, self->event_size);
317 if (err < 0) {
318 pr_debug("failed to write perf header events\n");
319 return err;
320 }
321 }
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200322
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200323 self->data_offset = lseek(fd, 0, SEEK_CUR);
324
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200325 if (at_exit) {
326 err = perf_header__adds_write(self, fd);
327 if (err < 0)
328 return err;
329 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100330
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200331 f_header = (struct perf_file_header){
332 .magic = PERF_MAGIC,
333 .size = sizeof(f_header),
334 .attr_size = sizeof(f_attr),
335 .attrs = {
336 .offset = self->attr_offset,
337 .size = self->attrs * sizeof(f_attr),
338 },
339 .data = {
340 .offset = self->data_offset,
341 .size = self->data_size,
342 },
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200343 .event_types = {
344 .offset = self->event_offset,
345 .size = self->event_size,
346 },
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200347 };
348
Frederic Weisbeckerdb9f11e2009-10-17 17:57:18 +0200349 memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200350
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200351 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200352 err = do_write(fd, &f_header, sizeof(f_header));
353 if (err < 0) {
354 pr_debug("failed to write perf header\n");
355 return err;
356 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200357 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
358
359 self->frozen = 1;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200360 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200361}
362
363static void do_read(int fd, void *buf, size_t size)
364{
365 while (size) {
366 int ret = read(fd, buf, size);
367
368 if (ret < 0)
369 die("failed to read");
Pierre Habouzit7eac7e92009-08-07 14:16:00 +0200370 if (ret == 0)
371 die("failed to read: missing data");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200372
373 size -= ret;
374 buf += ret;
375 }
376}
377
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200378int perf_header__process_sections(struct perf_header *self, int fd,
379 int (*process)(struct perf_file_section *self,
380 int feat, int fd))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200381{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100382 struct perf_file_section *feat_sec;
383 int nr_sections;
384 int sec_size;
385 int idx = 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200386 int err = 0, feat = 1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100387
388 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
389 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200390 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100391
392 feat_sec = calloc(sizeof(*feat_sec), nr_sections);
393 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200394 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100395
396 sec_size = sizeof(*feat_sec) * nr_sections;
397
398 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
399
400 do_read(fd, feat_sec, sec_size);
401
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200402 while (idx < nr_sections && feat < HEADER_LAST_FEATURE) {
403 if (perf_header__has_feat(self, feat)) {
404 struct perf_file_section *sec = &feat_sec[idx++];
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100405
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200406 err = process(sec, feat, fd);
407 if (err < 0)
408 break;
409 }
410 ++feat;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100411 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100412
413 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200414 return err;
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200415};
416
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200417int perf_file_header__read(struct perf_file_header *self,
418 struct perf_header *ph, int fd)
419{
420 lseek(fd, 0, SEEK_SET);
421 do_read(fd, self, sizeof(*self));
422
423 if (self->magic != PERF_MAGIC ||
424 self->attr_size != sizeof(struct perf_file_attr))
425 return -1;
426
427 if (self->size != sizeof(*self)) {
428 /* Support the previous format */
429 if (self->size == offsetof(typeof(*self), adds_features))
430 bitmap_zero(self->adds_features, HEADER_FEAT_BITS);
431 else
432 return -1;
433 }
434
435 memcpy(&ph->adds_features, &self->adds_features,
436 sizeof(self->adds_features));
437
438 ph->event_offset = self->event_types.offset;
439 ph->event_size = self->event_types.size;
440 ph->data_offset = self->data.offset;
441 ph->data_size = self->data.size;
442 return 0;
443}
444
445static int perf_file_section__process(struct perf_file_section *self,
446 int feat, int fd)
447{
448 if (lseek(fd, self->offset, SEEK_SET) < 0) {
449 pr_debug("Failed to lseek to %Ld offset for feature %d, "
450 "continuing...\n", self->offset, feat);
451 return 0;
452 }
453
454 switch (feat) {
455 case HEADER_TRACE_INFO:
456 trace_report(fd);
457 break;
458
459 case HEADER_BUILD_ID:
460 if (perf_header__read_build_ids(fd, self->offset, self->size))
461 pr_debug("Failed to read buildids, continuing...\n");
462 break;
463 default:
464 pr_debug("unknown feature %d, continuing...\n", feat);
465 }
466
467 return 0;
468}
469
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200470int perf_header__read(struct perf_header *self, int fd)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200471{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200472 struct perf_file_header f_header;
473 struct perf_file_attr f_attr;
474 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200475 int nr_attrs, nr_ids, i, j;
476
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200477 if (perf_file_header__read(&f_header, self, fd) < 0) {
478 pr_debug("incompatible file format\n");
479 return -EINVAL;
480 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200481
482 nr_attrs = f_header.attrs.size / sizeof(f_attr);
483 lseek(fd, f_header.attrs.offset, SEEK_SET);
484
485 for (i = 0; i < nr_attrs; i++) {
486 struct perf_header_attr *attr;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200487 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200488
489 do_read(fd, &f_attr, sizeof(f_attr));
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200490 tmp = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200491
492 attr = perf_header_attr__new(&f_attr.attr);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200493 if (attr == NULL)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200494 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200495
496 nr_ids = f_attr.ids.size / sizeof(u64);
497 lseek(fd, f_attr.ids.offset, SEEK_SET);
498
499 for (j = 0; j < nr_ids; j++) {
500 do_read(fd, &f_id, sizeof(f_id));
501
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200502 if (perf_header_attr__add_id(attr, f_id) < 0) {
503 perf_header_attr__delete(attr);
504 return -ENOMEM;
505 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200506 }
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200507 if (perf_header__add_attr(self, attr) < 0) {
508 perf_header_attr__delete(attr);
509 return -ENOMEM;
510 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200511
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200512 lseek(fd, tmp, SEEK_SET);
513 }
514
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200515 if (f_header.event_types.size) {
516 lseek(fd, f_header.event_types.offset, SEEK_SET);
517 events = malloc(f_header.event_types.size);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200518 if (events == NULL)
519 return -ENOMEM;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200520 do_read(fd, events, f_header.event_types.size);
521 event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
522 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200523
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200524 perf_header__process_sections(self, fd, perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100525
Ingo Molnar2e01d172009-09-03 16:21:11 +0200526 lseek(fd, self->data_offset, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200527
528 self->frozen = 1;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200529 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200530}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200531
532u64 perf_header__sample_type(struct perf_header *header)
533{
534 u64 type = 0;
535 int i;
536
537 for (i = 0; i < header->attrs; i++) {
538 struct perf_header_attr *attr = header->attr[i];
539
540 if (!type)
541 type = attr->attr.sample_type;
542 else if (type != attr->attr.sample_type)
543 die("non matching sample_type");
544 }
545
546 return type;
547}
548
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200549struct perf_event_attr *
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200550perf_header__find_attr(u64 id, struct perf_header *header)
551{
552 int i;
553
554 for (i = 0; i < header->attrs; i++) {
555 struct perf_header_attr *attr = header->attr[i];
556 int j;
557
558 for (j = 0; j < attr->ids; j++) {
559 if (attr->id[j] == id)
560 return &attr->attr;
561 }
562 }
563
564 return NULL;
565}