blob: e66c7bd4cc8808a7ba3ddb541605216eff70a8c1 [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"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010011#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010012#include "data_map.h"
13#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
61/*
Arjan van de Venec60a3f2009-09-19 13:36:30 +020062 * Create new perf.data header:
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020063 */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020064struct perf_header *perf_header__new(void)
65{
Frederic Weisbecker2ba08252009-10-17 17:12:34 +020066 struct perf_header *self = calloc(sizeof(*self), 1);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020067
Arnaldo Carvalho de Meloa9a70bb2009-11-17 01:18:11 -020068 if (self != NULL) {
69 self->size = 1;
70 self->attr = malloc(sizeof(void *));
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020071
Arnaldo Carvalho de Meloa9a70bb2009-11-17 01:18:11 -020072 if (self->attr == NULL) {
73 free(self);
74 self = NULL;
75 }
76 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020077
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020078 return self;
79}
80
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020081void 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
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020092int perf_header__add_attr(struct perf_header *self,
93 struct perf_header_attr *attr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020094{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020095 if (self->frozen)
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020096 return -1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020097
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020098 if (self->attrs == self->size) {
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020099 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;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200108 }
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200109
110 self->attr[self->attrs++] = attr;
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200111 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200112}
113
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200114#define MAX_EVENT_NAME 64
115
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200116struct perf_trace_event_type {
117 u64 event_id;
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200118 char name[MAX_EVENT_NAME];
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200119};
120
121static int event_count;
122static struct perf_trace_event_type *events;
123
124void perf_header__push_event(u64 id, const char *name)
125{
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200126 if (strlen(name) > MAX_EVENT_NAME)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200127 pr_warning("Event %s will be truncated\n", name);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200128
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;
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200140 strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200141 event_count++;
142}
143
144char *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
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200154static const char *__perf_magic = "PERFFILE";
155
156#define PERF_MAGIC (*(u64 *)__perf_magic)
157
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200158struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200159 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200160 struct perf_file_section ids;
161};
162
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200163void perf_header__set_feat(struct perf_header *self, int feat)
164{
165 set_bit(feat, self->adds_features);
166}
167
168bool perf_header__has_feat(const struct perf_header *self, int feat)
169{
170 return test_bit(feat, self->adds_features);
171}
172
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200173static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200174{
175 while (size) {
176 int ret = write(fd, buf, size);
177
178 if (ret < 0)
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200179 return -1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200180
181 size -= ret;
182 buf += ret;
183 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200184
185 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200186}
187
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200188static int dsos__write_buildid_table(int fd)
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100189{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200190 struct dso *pos;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100191
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200192 list_for_each_entry(pos, &dsos, node) {
193 struct build_id_event b;
194 size_t len;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100195
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200196 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)
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200205 return -1;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100206 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200207
208 return 0;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100209}
210
211static void
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100212perf_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;
218 int idx = 0;
219
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)
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);
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
Arnaldo Carvalho de Melo24460422009-11-18 20:20:53 -0200253 dsos__load_kernel();
Arnaldo Carvalho de Melof1617b42009-11-18 20:20:52 -0200254 /*
255 * Read the list of loaded modules with its build_ids
256 */
257 dsos__load_modules();
258
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100259 /* Write build-ids */
260 buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200261 if (dsos__write_buildid_table(fd) < 0)
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200262 die("failed to write buildid table");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100263 buildid_sec->size = lseek(fd, 0, SEEK_CUR) - buildid_sec->offset;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100264 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100265
266 lseek(fd, sec_start, SEEK_SET);
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200267 if (do_write(fd, feat_sec, sec_size) < 0)
268 die("failed to write feature section");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100269 free(feat_sec);
270}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200271
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100272void 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;
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);
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200286 if (do_write(fd, attr->id, attr->ids * sizeof(u64)) < 0)
287 die("failed to write perf header");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200288 }
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 };
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200303 if (do_write(fd, &f_attr, sizeof(f_attr)) < 0)
304 die("failed to write perf header attribute");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200305 }
306
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200307 self->event_offset = lseek(fd, 0, SEEK_CUR);
308 self->event_size = event_count * sizeof(struct perf_trace_event_type);
309 if (events)
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200310 if (do_write(fd, events, self->event_size) < 0)
311 die("failed to write perf header events");
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200312
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200313 self->data_offset = lseek(fd, 0, SEEK_CUR);
314
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100315 if (at_exit)
316 perf_header__adds_write(self, fd);
317
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200318 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 },
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200330 .event_types = {
331 .offset = self->event_offset,
332 .size = self->event_size,
333 },
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200334 };
335
Frederic Weisbeckerdb9f11e2009-10-17 17:57:18 +0200336 memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200337
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200338 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200339 if (do_write(fd, &f_header, sizeof(f_header)) < 0)
340 die("failed to write perf header");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200341 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
342
343 self->frozen = 1;
344}
345
346static 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");
Pierre Habouzit7eac7e92009-08-07 14:16:00 +0200353 if (ret == 0)
354 die("failed to read: missing data");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200355
356 size -= ret;
357 buf += ret;
358 }
359}
360
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200361int perf_header__process_sections(struct perf_header *self, int fd,
362 int (*process)(struct perf_file_section *self,
363 int feat, int fd))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200364{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100365 struct perf_file_section *feat_sec;
366 int nr_sections;
367 int sec_size;
368 int idx = 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200369 int err = 0, feat = 1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100370
371 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
372 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200373 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100374
375 feat_sec = calloc(sizeof(*feat_sec), nr_sections);
376 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200377 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100378
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
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200385 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++];
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100388
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200389 err = process(sec, feat, fd);
390 if (err < 0)
391 break;
392 }
393 ++feat;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100394 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100395
396 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200397 return err;
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200398};
399
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200400int 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
428static 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
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200453int perf_header__read(struct perf_header *self, int fd)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200454{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200455 struct perf_file_header f_header;
456 struct perf_file_attr f_attr;
457 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200458 int nr_attrs, nr_ids, i, j;
459
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200460 if (perf_file_header__read(&f_header, self, fd) < 0) {
461 pr_debug("incompatible file format\n");
462 return -EINVAL;
463 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200464
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;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200470 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200471
472 do_read(fd, &f_attr, sizeof(f_attr));
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200473 tmp = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200474
475 attr = perf_header_attr__new(&f_attr.attr);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200476 if (attr == NULL)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200477 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200478
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
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200485 if (perf_header_attr__add_id(attr, f_id) < 0) {
486 perf_header_attr__delete(attr);
487 return -ENOMEM;
488 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200489 }
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200490 if (perf_header__add_attr(self, attr) < 0) {
491 perf_header_attr__delete(attr);
492 return -ENOMEM;
493 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200494
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200495 lseek(fd, tmp, SEEK_SET);
496 }
497
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200498 if (f_header.event_types.size) {
499 lseek(fd, f_header.event_types.offset, SEEK_SET);
500 events = malloc(f_header.event_types.size);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200501 if (events == NULL)
502 return -ENOMEM;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200503 do_read(fd, events, f_header.event_types.size);
504 event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
505 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200506
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200507 perf_header__process_sections(self, fd, perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100508
Ingo Molnar2e01d172009-09-03 16:21:11 +0200509 lseek(fd, self->data_offset, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200510
511 self->frozen = 1;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200512 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200513}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200514
515u64 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
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200532struct perf_event_attr *
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200533perf_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}