blob: 6b3cb94e8a2bd0e89a86fc9c28011f6b655e9544 [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 Melo4cf40132009-12-27 21:37:06 -0200172#define dsos__for_each_with_build_id(pos, head) \
173 list_for_each_entry(pos, head, node) \
174 if (!pos->has_build_id) \
175 continue; \
176 else
177
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200178static int __dsos__write_buildid_table(struct list_head *head, int fd)
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100179{
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900180#define NAME_ALIGN 64
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200181 struct dso *pos;
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900182 static const char zero_buf[NAME_ALIGN];
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100183
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200184 dsos__for_each_with_build_id(pos, head) {
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200185 int err;
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200186 struct build_id_event b;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200187 size_t len = pos->long_name_len + 1;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100188
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900189 len = ALIGN(len, NAME_ALIGN);
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200190 memset(&b, 0, sizeof(b));
191 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
192 b.header.size = sizeof(b) + len;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200193 err = do_write(fd, &b, sizeof(b));
194 if (err < 0)
195 return err;
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900196 err = do_write(fd, pos->long_name, pos->long_name_len + 1);
197 if (err < 0)
198 return err;
Xiao Guangrongd9541ed2009-12-07 12:07:15 +0800199 err = do_write(fd, zero_buf, len - pos->long_name_len - 1);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200200 if (err < 0)
201 return err;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100202 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200203
204 return 0;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100205}
206
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200207static int dsos__write_buildid_table(int fd)
208{
209 int err = __dsos__write_buildid_table(&dsos__kernel, fd);
210 if (err == 0)
211 err = __dsos__write_buildid_table(&dsos__user, fd);
212 return err;
213}
214
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200215static int dso__cache_build_id(struct dso *self, const char *debugdir)
216{
217 const size_t size = PATH_MAX;
218 char *filename = malloc(size),
219 *linkname = malloc(size), *targetname, *sbuild_id;
220 int len, err = -1;
221
222 if (filename == NULL || linkname == NULL)
223 goto out_free;
224
225 len = snprintf(filename, size, "%s%s", debugdir, self->long_name);
226 if (mkdir_p(filename, 0755))
227 goto out_free;
228
229 len += snprintf(filename + len, sizeof(filename) - len, "/");
230 sbuild_id = filename + len;
231 build_id__sprintf(self->build_id, sizeof(self->build_id), sbuild_id);
232
233 if (access(filename, F_OK) && link(self->long_name, filename) &&
234 copyfile(self->long_name, filename))
235 goto out_free;
236
237 len = snprintf(linkname, size, "%s/.build-id/%.2s",
238 debugdir, sbuild_id);
239
240 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
241 goto out_free;
242
243 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
244 targetname = filename + strlen(debugdir) - 5;
245 memcpy(targetname, "../..", 5);
246
247 if (symlink(targetname, linkname) == 0)
248 err = 0;
249out_free:
250 free(filename);
251 free(linkname);
252 return err;
253}
254
255static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
256{
257 struct dso *pos;
258 int err = 0;
259
260 dsos__for_each_with_build_id(pos, head)
261 if (dso__cache_build_id(pos, debugdir))
262 err = -1;
263
264 return err;
265}
266
267static int dsos__cache_build_ids(void)
268{
269 int err_kernel, err_user;
270 char debugdir[PATH_MAX];
271
272 snprintf(debugdir, sizeof(debugdir), "%s/%s", getenv("HOME"),
273 DEBUG_CACHE_DIR);
274
275 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
276 return -1;
277
278 err_kernel = __dsos__cache_build_ids(&dsos__kernel, debugdir);
279 err_user = __dsos__cache_build_ids(&dsos__user, debugdir);
280 return err_kernel || err_user ? -1 : 0;
281}
282
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200283static int perf_header__adds_write(struct perf_header *self, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200284{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100285 int nr_sections;
286 struct perf_file_section *feat_sec;
287 int sec_size;
288 u64 sec_start;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200289 int idx = 0, err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100290
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200291 if (dsos__read_build_ids())
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100292 perf_header__set_feat(self, HEADER_BUILD_ID);
293
294 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
295 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200296 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100297
298 feat_sec = calloc(sizeof(*feat_sec), nr_sections);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200299 if (feat_sec == NULL)
300 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100301
302 sec_size = sizeof(*feat_sec) * nr_sections;
303
304 sec_start = self->data_offset + self->data_size;
305 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200306
Frederic Weisbecker3e13ab22009-11-11 04:51:06 +0100307 if (perf_header__has_feat(self, HEADER_TRACE_INFO)) {
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100308 struct perf_file_section *trace_sec;
309
310 trace_sec = &feat_sec[idx++];
311
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200312 /* Write trace info */
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100313 trace_sec->offset = lseek(fd, 0, SEEK_CUR);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200314 read_tracing_data(fd, attrs, nr_counters);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100315 trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200316 }
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100317
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100318
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100319 if (perf_header__has_feat(self, HEADER_BUILD_ID)) {
320 struct perf_file_section *buildid_sec;
321
322 buildid_sec = &feat_sec[idx++];
323
324 /* Write build-ids */
325 buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200326 err = dsos__write_buildid_table(fd);
327 if (err < 0) {
328 pr_debug("failed to write buildid table\n");
329 goto out_free;
330 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100331 buildid_sec->size = lseek(fd, 0, SEEK_CUR) - buildid_sec->offset;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200332 dsos__cache_build_ids();
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100333 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100334
335 lseek(fd, sec_start, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200336 err = do_write(fd, feat_sec, sec_size);
337 if (err < 0)
338 pr_debug("failed to write feature section\n");
339out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100340 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200341 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100342}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200343
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200344int perf_header__write(struct perf_header *self, int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200345{
346 struct perf_file_header f_header;
347 struct perf_file_attr f_attr;
348 struct perf_header_attr *attr;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200349 int i, err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200350
351 lseek(fd, sizeof(f_header), SEEK_SET);
352
353
354 for (i = 0; i < self->attrs; i++) {
355 attr = self->attr[i];
356
357 attr->id_offset = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200358 err = do_write(fd, attr->id, attr->ids * sizeof(u64));
359 if (err < 0) {
360 pr_debug("failed to write perf header\n");
361 return err;
362 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200363 }
364
365
366 self->attr_offset = lseek(fd, 0, SEEK_CUR);
367
368 for (i = 0; i < self->attrs; i++) {
369 attr = self->attr[i];
370
371 f_attr = (struct perf_file_attr){
372 .attr = attr->attr,
373 .ids = {
374 .offset = attr->id_offset,
375 .size = attr->ids * sizeof(u64),
376 }
377 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200378 err = do_write(fd, &f_attr, sizeof(f_attr));
379 if (err < 0) {
380 pr_debug("failed to write perf header attribute\n");
381 return err;
382 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200383 }
384
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200385 self->event_offset = lseek(fd, 0, SEEK_CUR);
386 self->event_size = event_count * sizeof(struct perf_trace_event_type);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200387 if (events) {
388 err = do_write(fd, events, self->event_size);
389 if (err < 0) {
390 pr_debug("failed to write perf header events\n");
391 return err;
392 }
393 }
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200394
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200395 self->data_offset = lseek(fd, 0, SEEK_CUR);
396
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200397 if (at_exit) {
398 err = perf_header__adds_write(self, fd);
399 if (err < 0)
400 return err;
401 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100402
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200403 f_header = (struct perf_file_header){
404 .magic = PERF_MAGIC,
405 .size = sizeof(f_header),
406 .attr_size = sizeof(f_attr),
407 .attrs = {
408 .offset = self->attr_offset,
409 .size = self->attrs * sizeof(f_attr),
410 },
411 .data = {
412 .offset = self->data_offset,
413 .size = self->data_size,
414 },
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200415 .event_types = {
416 .offset = self->event_offset,
417 .size = self->event_size,
418 },
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200419 };
420
Frederic Weisbeckerdb9f11e2009-10-17 17:57:18 +0200421 memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200422
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200423 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200424 err = do_write(fd, &f_header, sizeof(f_header));
425 if (err < 0) {
426 pr_debug("failed to write perf header\n");
427 return err;
428 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200429 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
430
431 self->frozen = 1;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200432 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200433}
434
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200435static int do_read(int fd, void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200436{
437 while (size) {
438 int ret = read(fd, buf, size);
439
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200440 if (ret <= 0)
441 return -1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200442
443 size -= ret;
444 buf += ret;
445 }
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200446
447 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200448}
449
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200450int perf_header__process_sections(struct perf_header *self, int fd,
451 int (*process)(struct perf_file_section *self,
452 int feat, int fd))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200453{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100454 struct perf_file_section *feat_sec;
455 int nr_sections;
456 int sec_size;
457 int idx = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200458 int err = -1, feat = 1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100459
460 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
461 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200462 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100463
464 feat_sec = calloc(sizeof(*feat_sec), nr_sections);
465 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200466 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100467
468 sec_size = sizeof(*feat_sec) * nr_sections;
469
470 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
471
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200472 if (do_read(fd, feat_sec, sec_size))
473 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100474
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200475 err = 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200476 while (idx < nr_sections && feat < HEADER_LAST_FEATURE) {
477 if (perf_header__has_feat(self, feat)) {
478 struct perf_file_section *sec = &feat_sec[idx++];
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100479
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200480 err = process(sec, feat, fd);
481 if (err < 0)
482 break;
483 }
484 ++feat;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100485 }
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200486out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100487 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200488 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200489}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200490
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200491int perf_file_header__read(struct perf_file_header *self,
492 struct perf_header *ph, int fd)
493{
494 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200495
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200496 if (do_read(fd, self, sizeof(*self)) ||
497 self->magic != PERF_MAGIC ||
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200498 self->attr_size != sizeof(struct perf_file_attr))
499 return -1;
500
501 if (self->size != sizeof(*self)) {
502 /* Support the previous format */
503 if (self->size == offsetof(typeof(*self), adds_features))
504 bitmap_zero(self->adds_features, HEADER_FEAT_BITS);
505 else
506 return -1;
507 }
508
509 memcpy(&ph->adds_features, &self->adds_features,
510 sizeof(self->adds_features));
511
512 ph->event_offset = self->event_types.offset;
513 ph->event_size = self->event_types.size;
514 ph->data_offset = self->data.offset;
515 ph->data_size = self->data.size;
516 return 0;
517}
518
519static int perf_file_section__process(struct perf_file_section *self,
520 int feat, int fd)
521{
522 if (lseek(fd, self->offset, SEEK_SET) < 0) {
523 pr_debug("Failed to lseek to %Ld offset for feature %d, "
524 "continuing...\n", self->offset, feat);
525 return 0;
526 }
527
528 switch (feat) {
529 case HEADER_TRACE_INFO:
530 trace_report(fd);
531 break;
532
533 case HEADER_BUILD_ID:
534 if (perf_header__read_build_ids(fd, self->offset, self->size))
535 pr_debug("Failed to read buildids, continuing...\n");
536 break;
537 default:
538 pr_debug("unknown feature %d, continuing...\n", feat);
539 }
540
541 return 0;
542}
543
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200544int perf_header__read(struct perf_header *self, int fd)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200545{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200546 struct perf_file_header f_header;
547 struct perf_file_attr f_attr;
548 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200549 int nr_attrs, nr_ids, i, j;
550
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200551 if (perf_file_header__read(&f_header, self, fd) < 0) {
552 pr_debug("incompatible file format\n");
553 return -EINVAL;
554 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200555
556 nr_attrs = f_header.attrs.size / sizeof(f_attr);
557 lseek(fd, f_header.attrs.offset, SEEK_SET);
558
559 for (i = 0; i < nr_attrs; i++) {
560 struct perf_header_attr *attr;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200561 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200562
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200563 if (do_read(fd, &f_attr, sizeof(f_attr)))
564 goto out_errno;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200565 tmp = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200566
567 attr = perf_header_attr__new(&f_attr.attr);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200568 if (attr == NULL)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200569 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200570
571 nr_ids = f_attr.ids.size / sizeof(u64);
572 lseek(fd, f_attr.ids.offset, SEEK_SET);
573
574 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200575 if (do_read(fd, &f_id, sizeof(f_id)))
576 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200577
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200578 if (perf_header_attr__add_id(attr, f_id) < 0) {
579 perf_header_attr__delete(attr);
580 return -ENOMEM;
581 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200582 }
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200583 if (perf_header__add_attr(self, attr) < 0) {
584 perf_header_attr__delete(attr);
585 return -ENOMEM;
586 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200587
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200588 lseek(fd, tmp, SEEK_SET);
589 }
590
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200591 if (f_header.event_types.size) {
592 lseek(fd, f_header.event_types.offset, SEEK_SET);
593 events = malloc(f_header.event_types.size);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200594 if (events == NULL)
595 return -ENOMEM;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200596 if (do_read(fd, events, f_header.event_types.size))
597 goto out_errno;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200598 event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
599 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200600
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200601 perf_header__process_sections(self, fd, perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100602
Ingo Molnar2e01d172009-09-03 16:21:11 +0200603 lseek(fd, self->data_offset, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200604
605 self->frozen = 1;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200606 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200607out_errno:
608 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200609}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200610
611u64 perf_header__sample_type(struct perf_header *header)
612{
613 u64 type = 0;
614 int i;
615
616 for (i = 0; i < header->attrs; i++) {
617 struct perf_header_attr *attr = header->attr[i];
618
619 if (!type)
620 type = attr->attr.sample_type;
621 else if (type != attr->attr.sample_type)
622 die("non matching sample_type");
623 }
624
625 return type;
626}
627
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200628struct perf_event_attr *
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200629perf_header__find_attr(u64 id, struct perf_header *header)
630{
631 int i;
632
633 for (i = 0; i < header->attrs; i++) {
634 struct perf_header_attr *attr = header->attr[i];
635 int j;
636
637 for (j = 0; j < attr->ids; j++) {
638 if (attr->id[j] == id)
639 return &attr->attr;
640 }
641 }
642
643 return NULL;
644}