blob: 6c9aa16ee51fce97747cb9c9ea493e717b45b953 [file] [log] [blame]
Xiao Guangrongb8f46c52010-02-03 11:53:14 +08001#define _FILE_OFFSET_BITS 64
2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02004#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02005#include <unistd.h>
6#include <stdio.h>
7#include <stdlib.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +01008#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02009#include <linux/kernel.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020010
11#include "util.h"
12#include "header.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020013#include "../perf.h"
14#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020015#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010016#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010017#include "debug.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020018
19/*
Arjan van de Venec60a3f2009-09-19 13:36:30 +020020 * Create new perf.data header attribute:
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020021 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020022struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020023{
24 struct perf_header_attr *self = malloc(sizeof(*self));
25
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -020026 if (self != NULL) {
27 self->attr = *attr;
28 self->ids = 0;
29 self->size = 1;
30 self->id = malloc(sizeof(u64));
31 if (self->id == NULL) {
32 free(self);
33 self = NULL;
34 }
35 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020036
37 return self;
38}
39
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020040void perf_header_attr__delete(struct perf_header_attr *self)
41{
42 free(self->id);
43 free(self);
44}
45
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -020046int perf_header_attr__add_id(struct perf_header_attr *self, u64 id)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020047{
48 int pos = self->ids;
49
50 self->ids++;
51 if (self->ids > self->size) {
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -020052 int nsize = self->size * 2;
53 u64 *nid = realloc(self->id, nsize * sizeof(u64));
54
55 if (nid == NULL)
56 return -1;
57
58 self->size = nsize;
59 self->id = nid;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020060 }
61 self->id[pos] = id;
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -020062 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020063}
64
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020065int perf_header__init(struct perf_header *self)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020066{
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020067 self->size = 1;
68 self->attr = malloc(sizeof(void *));
69 return self->attr == NULL ? -ENOMEM : 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020070}
71
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020072void perf_header__exit(struct perf_header *self)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020073{
74 int i;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020075 for (i = 0; i < self->attrs; ++i)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020076 perf_header_attr__delete(self->attr[i]);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020077 free(self->attr);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020078}
79
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020080int perf_header__add_attr(struct perf_header *self,
81 struct perf_header_attr *attr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020082{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020083 if (self->frozen)
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020084 return -1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020085
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020086 if (self->attrs == self->size) {
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020087 int nsize = self->size * 2;
88 struct perf_header_attr **nattr;
89
90 nattr = realloc(self->attr, nsize * sizeof(void *));
91 if (nattr == NULL)
92 return -1;
93
94 self->size = nsize;
95 self->attr = nattr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020096 }
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020097
98 self->attr[self->attrs++] = attr;
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020099 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200100}
101
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200102#define MAX_EVENT_NAME 64
103
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200104struct perf_trace_event_type {
105 u64 event_id;
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200106 char name[MAX_EVENT_NAME];
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200107};
108
109static int event_count;
110static struct perf_trace_event_type *events;
111
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -0200112int perf_header__push_event(u64 id, const char *name)
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200113{
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200114 if (strlen(name) > MAX_EVENT_NAME)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200115 pr_warning("Event %s will be truncated\n", name);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200116
117 if (!events) {
118 events = malloc(sizeof(struct perf_trace_event_type));
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -0200119 if (events == NULL)
120 return -ENOMEM;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200121 } else {
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -0200122 struct perf_trace_event_type *nevents;
123
124 nevents = realloc(events, (event_count + 1) * sizeof(*events));
125 if (nevents == NULL)
126 return -ENOMEM;
127 events = nevents;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200128 }
129 memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
130 events[event_count].event_id = id;
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200131 strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200132 event_count++;
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -0200133 return 0;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200134}
135
136char *perf_header__find_event(u64 id)
137{
138 int i;
139 for (i = 0 ; i < event_count; i++) {
140 if (events[i].event_id == id)
141 return events[i].name;
142 }
143 return NULL;
144}
145
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200146static const char *__perf_magic = "PERFFILE";
147
148#define PERF_MAGIC (*(u64 *)__perf_magic)
149
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200150struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200151 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200152 struct perf_file_section ids;
153};
154
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200155void perf_header__set_feat(struct perf_header *self, int feat)
156{
157 set_bit(feat, self->adds_features);
158}
159
160bool perf_header__has_feat(const struct perf_header *self, int feat)
161{
162 return test_bit(feat, self->adds_features);
163}
164
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200165static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200166{
167 while (size) {
168 int ret = write(fd, buf, size);
169
170 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200171 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200172
173 size -= ret;
174 buf += ret;
175 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200176
177 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200178}
179
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200180#define NAME_ALIGN 64
181
182static int write_padded(int fd, const void *bf, size_t count,
183 size_t count_aligned)
184{
185 static const char zero_buf[NAME_ALIGN];
186 int err = do_write(fd, bf, count);
187
188 if (!err)
189 err = do_write(fd, zero_buf, count_aligned - count);
190
191 return err;
192}
193
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200194#define dsos__for_each_with_build_id(pos, head) \
195 list_for_each_entry(pos, head, node) \
196 if (!pos->has_build_id) \
197 continue; \
198 else
199
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -0200200static int __dsos__write_buildid_table(struct list_head *head, u16 misc, int fd)
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100201{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200202 struct dso *pos;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100203
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200204 dsos__for_each_with_build_id(pos, head) {
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200205 int err;
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200206 struct build_id_event b;
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200207 size_t len;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100208
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200209 if (!pos->hit)
210 continue;
211 len = pos->long_name_len + 1;
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900212 len = ALIGN(len, NAME_ALIGN);
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200213 memset(&b, 0, sizeof(b));
214 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -0200215 b.header.misc = misc;
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200216 b.header.size = sizeof(b) + len;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200217 err = do_write(fd, &b, sizeof(b));
218 if (err < 0)
219 return err;
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200220 err = write_padded(fd, pos->long_name,
221 pos->long_name_len + 1, len);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200222 if (err < 0)
223 return err;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100224 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200225
226 return 0;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100227}
228
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200229static int dsos__write_buildid_table(int fd)
230{
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -0200231 int err = __dsos__write_buildid_table(&dsos__kernel,
232 PERF_RECORD_MISC_KERNEL, fd);
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200233 if (err == 0)
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -0200234 err = __dsos__write_buildid_table(&dsos__user,
235 PERF_RECORD_MISC_USER, fd);
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200236 return err;
237}
238
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200239int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
240 const char *name, bool is_kallsyms)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200241{
242 const size_t size = PATH_MAX;
243 char *filename = malloc(size),
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200244 *linkname = malloc(size), *targetname;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200245 int len, err = -1;
246
247 if (filename == NULL || linkname == NULL)
248 goto out_free;
249
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200250 len = snprintf(filename, size, "%s%s%s",
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200251 debugdir, is_kallsyms ? "/" : "", name);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200252 if (mkdir_p(filename, 0755))
253 goto out_free;
254
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200255 snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200256
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200257 if (access(filename, F_OK)) {
258 if (is_kallsyms) {
259 if (copyfile("/proc/kallsyms", filename))
260 goto out_free;
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200261 } else if (link(name, filename) && copyfile(name, filename))
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200262 goto out_free;
263 }
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200264
265 len = snprintf(linkname, size, "%s/.build-id/%.2s",
266 debugdir, sbuild_id);
267
268 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
269 goto out_free;
270
271 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
272 targetname = filename + strlen(debugdir) - 5;
273 memcpy(targetname, "../..", 5);
274
275 if (symlink(targetname, linkname) == 0)
276 err = 0;
277out_free:
278 free(filename);
279 free(linkname);
280 return err;
281}
282
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200283static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
284 const char *name, const char *debugdir,
285 bool is_kallsyms)
286{
287 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
288
289 build_id__sprintf(build_id, build_id_size, sbuild_id);
290
291 return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms);
292}
293
294int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
295{
296 const size_t size = PATH_MAX;
297 char *filename = malloc(size),
298 *linkname = malloc(size);
299 int err = -1;
300
301 if (filename == NULL || linkname == NULL)
302 goto out_free;
303
304 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
305 debugdir, sbuild_id, sbuild_id + 2);
306
307 if (access(linkname, F_OK))
308 goto out_free;
309
310 if (readlink(linkname, filename, size) < 0)
311 goto out_free;
312
313 if (unlink(linkname))
314 goto out_free;
315
316 /*
317 * Since the link is relative, we must make it absolute:
318 */
319 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
320 debugdir, sbuild_id, filename);
321
322 if (unlink(linkname))
323 goto out_free;
324
325 err = 0;
326out_free:
327 free(filename);
328 free(linkname);
329 return err;
330}
331
332static int dso__cache_build_id(struct dso *self, const char *debugdir)
333{
334 bool is_kallsyms = self->kernel && self->long_name[0] != '/';
335
336 return build_id_cache__add_b(self->build_id, sizeof(self->build_id),
337 self->long_name, debugdir, is_kallsyms);
338}
339
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200340static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
341{
342 struct dso *pos;
343 int err = 0;
344
345 dsos__for_each_with_build_id(pos, head)
346 if (dso__cache_build_id(pos, debugdir))
347 err = -1;
348
349 return err;
350}
351
352static int dsos__cache_build_ids(void)
353{
354 int err_kernel, err_user;
355 char debugdir[PATH_MAX];
356
357 snprintf(debugdir, sizeof(debugdir), "%s/%s", getenv("HOME"),
358 DEBUG_CACHE_DIR);
359
360 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
361 return -1;
362
363 err_kernel = __dsos__cache_build_ids(&dsos__kernel, debugdir);
364 err_user = __dsos__cache_build_ids(&dsos__user, debugdir);
365 return err_kernel || err_user ? -1 : 0;
366}
367
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200368static int perf_header__adds_write(struct perf_header *self, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200369{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100370 int nr_sections;
371 struct perf_file_section *feat_sec;
372 int sec_size;
373 u64 sec_start;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200374 int idx = 0, err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100375
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200376 if (dsos__read_build_ids(true))
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100377 perf_header__set_feat(self, HEADER_BUILD_ID);
378
379 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
380 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200381 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100382
383 feat_sec = calloc(sizeof(*feat_sec), nr_sections);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200384 if (feat_sec == NULL)
385 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100386
387 sec_size = sizeof(*feat_sec) * nr_sections;
388
389 sec_start = self->data_offset + self->data_size;
Xiao Guangrongf887f302010-02-04 16:46:42 +0800390 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200391
Frederic Weisbecker3e13ab22009-11-11 04:51:06 +0100392 if (perf_header__has_feat(self, HEADER_TRACE_INFO)) {
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100393 struct perf_file_section *trace_sec;
394
395 trace_sec = &feat_sec[idx++];
396
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200397 /* Write trace info */
Xiao Guangrongf887f302010-02-04 16:46:42 +0800398 trace_sec->offset = lseek(fd, 0, SEEK_CUR);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200399 read_tracing_data(fd, attrs, nr_counters);
Xiao Guangrongf887f302010-02-04 16:46:42 +0800400 trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200401 }
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100402
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100403
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100404 if (perf_header__has_feat(self, HEADER_BUILD_ID)) {
405 struct perf_file_section *buildid_sec;
406
407 buildid_sec = &feat_sec[idx++];
408
409 /* Write build-ids */
Xiao Guangrongf887f302010-02-04 16:46:42 +0800410 buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200411 err = dsos__write_buildid_table(fd);
412 if (err < 0) {
413 pr_debug("failed to write buildid table\n");
414 goto out_free;
415 }
Xiao Guangrongf887f302010-02-04 16:46:42 +0800416 buildid_sec->size = lseek(fd, 0, SEEK_CUR) -
417 buildid_sec->offset;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200418 dsos__cache_build_ids();
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100419 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100420
Xiao Guangrongf887f302010-02-04 16:46:42 +0800421 lseek(fd, sec_start, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200422 err = do_write(fd, feat_sec, sec_size);
423 if (err < 0)
424 pr_debug("failed to write feature section\n");
425out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100426 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200427 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100428}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200429
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200430int perf_header__write(struct perf_header *self, int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200431{
432 struct perf_file_header f_header;
433 struct perf_file_attr f_attr;
434 struct perf_header_attr *attr;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200435 int i, err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200436
437 lseek(fd, sizeof(f_header), SEEK_SET);
438
439
440 for (i = 0; i < self->attrs; i++) {
441 attr = self->attr[i];
442
443 attr->id_offset = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200444 err = do_write(fd, attr->id, attr->ids * sizeof(u64));
445 if (err < 0) {
446 pr_debug("failed to write perf header\n");
447 return err;
448 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200449 }
450
451
452 self->attr_offset = lseek(fd, 0, SEEK_CUR);
453
454 for (i = 0; i < self->attrs; i++) {
455 attr = self->attr[i];
456
457 f_attr = (struct perf_file_attr){
458 .attr = attr->attr,
459 .ids = {
460 .offset = attr->id_offset,
461 .size = attr->ids * sizeof(u64),
462 }
463 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200464 err = do_write(fd, &f_attr, sizeof(f_attr));
465 if (err < 0) {
466 pr_debug("failed to write perf header attribute\n");
467 return err;
468 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200469 }
470
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200471 self->event_offset = lseek(fd, 0, SEEK_CUR);
472 self->event_size = event_count * sizeof(struct perf_trace_event_type);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200473 if (events) {
474 err = do_write(fd, events, self->event_size);
475 if (err < 0) {
476 pr_debug("failed to write perf header events\n");
477 return err;
478 }
479 }
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200480
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200481 self->data_offset = lseek(fd, 0, SEEK_CUR);
482
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200483 if (at_exit) {
484 err = perf_header__adds_write(self, fd);
485 if (err < 0)
486 return err;
487 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100488
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200489 f_header = (struct perf_file_header){
490 .magic = PERF_MAGIC,
491 .size = sizeof(f_header),
492 .attr_size = sizeof(f_attr),
493 .attrs = {
494 .offset = self->attr_offset,
495 .size = self->attrs * sizeof(f_attr),
496 },
497 .data = {
498 .offset = self->data_offset,
499 .size = self->data_size,
500 },
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200501 .event_types = {
502 .offset = self->event_offset,
503 .size = self->event_size,
504 },
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200505 };
506
Frederic Weisbeckerdb9f11e2009-10-17 17:57:18 +0200507 memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200508
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200509 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200510 err = do_write(fd, &f_header, sizeof(f_header));
511 if (err < 0) {
512 pr_debug("failed to write perf header\n");
513 return err;
514 }
Xiao Guangrongf887f302010-02-04 16:46:42 +0800515 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200516
517 self->frozen = 1;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200518 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200519}
520
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200521static int do_read(int fd, void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200522{
523 while (size) {
524 int ret = read(fd, buf, size);
525
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200526 if (ret <= 0)
527 return -1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200528
529 size -= ret;
530 buf += ret;
531 }
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200532
533 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200534}
535
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200536static int perf_header__getbuffer64(struct perf_header *self,
537 int fd, void *buf, size_t size)
538{
539 if (do_read(fd, buf, size))
540 return -1;
541
542 if (self->needs_swap)
543 mem_bswap_64(buf, size);
544
545 return 0;
546}
547
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200548int perf_header__process_sections(struct perf_header *self, int fd,
549 int (*process)(struct perf_file_section *self,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200550 struct perf_header *ph,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200551 int feat, int fd))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200552{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100553 struct perf_file_section *feat_sec;
554 int nr_sections;
555 int sec_size;
556 int idx = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200557 int err = -1, feat = 1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100558
559 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
560 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200561 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100562
563 feat_sec = calloc(sizeof(*feat_sec), nr_sections);
564 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200565 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100566
567 sec_size = sizeof(*feat_sec) * nr_sections;
568
Xiao Guangrongf887f302010-02-04 16:46:42 +0800569 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100570
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200571 if (perf_header__getbuffer64(self, fd, feat_sec, sec_size))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200572 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100573
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200574 err = 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200575 while (idx < nr_sections && feat < HEADER_LAST_FEATURE) {
576 if (perf_header__has_feat(self, feat)) {
577 struct perf_file_section *sec = &feat_sec[idx++];
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100578
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200579 err = process(sec, self, feat, fd);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200580 if (err < 0)
581 break;
582 }
583 ++feat;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100584 }
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200585out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100586 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200587 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200588}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200589
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200590int perf_file_header__read(struct perf_file_header *self,
591 struct perf_header *ph, int fd)
592{
593 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200594
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200595 if (do_read(fd, self, sizeof(*self)) ||
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200596 memcmp(&self->magic, __perf_magic, sizeof(self->magic)))
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200597 return -1;
598
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200599 if (self->attr_size != sizeof(struct perf_file_attr)) {
600 u64 attr_size = bswap_64(self->attr_size);
601
602 if (attr_size != sizeof(struct perf_file_attr))
603 return -1;
604
605 mem_bswap_64(self, offsetof(struct perf_file_header,
606 adds_features));
607 ph->needs_swap = true;
608 }
609
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200610 if (self->size != sizeof(*self)) {
611 /* Support the previous format */
612 if (self->size == offsetof(typeof(*self), adds_features))
613 bitmap_zero(self->adds_features, HEADER_FEAT_BITS);
614 else
615 return -1;
616 }
617
618 memcpy(&ph->adds_features, &self->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200619 sizeof(ph->adds_features));
620 /*
621 * FIXME: hack that assumes that if we need swap the perf.data file
622 * may be coming from an arch with a different word-size, ergo different
623 * DEFINE_BITMAP format, investigate more later, but for now its mostly
624 * safe to assume that we have a build-id section. Trace files probably
625 * have several other issues in this realm anyway...
626 */
627 if (ph->needs_swap) {
628 memset(&ph->adds_features, 0, sizeof(ph->adds_features));
629 perf_header__set_feat(ph, HEADER_BUILD_ID);
630 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200631
632 ph->event_offset = self->event_types.offset;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200633 ph->event_size = self->event_types.size;
634 ph->data_offset = self->data.offset;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200635 ph->data_size = self->data.size;
636 return 0;
637}
638
639static int perf_file_section__process(struct perf_file_section *self,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200640 struct perf_header *ph,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200641 int feat, int fd)
642{
Xiao Guangrongf887f302010-02-04 16:46:42 +0800643 if (lseek(fd, self->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200644 pr_debug("Failed to lseek to %Ld offset for feature %d, "
645 "continuing...\n", self->offset, feat);
646 return 0;
647 }
648
649 switch (feat) {
650 case HEADER_TRACE_INFO:
651 trace_report(fd);
652 break;
653
654 case HEADER_BUILD_ID:
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200655 if (perf_header__read_build_ids(ph, fd, self->offset, self->size))
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200656 pr_debug("Failed to read buildids, continuing...\n");
657 break;
658 default:
659 pr_debug("unknown feature %d, continuing...\n", feat);
660 }
661
662 return 0;
663}
664
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200665int perf_header__read(struct perf_header *self, int fd)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200666{
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200667 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200668 struct perf_file_attr f_attr;
669 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200670 int nr_attrs, nr_ids, i, j;
671
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200672 if (perf_file_header__read(&f_header, self, fd) < 0) {
673 pr_debug("incompatible file format\n");
674 return -EINVAL;
675 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200676
677 nr_attrs = f_header.attrs.size / sizeof(f_attr);
678 lseek(fd, f_header.attrs.offset, SEEK_SET);
679
680 for (i = 0; i < nr_attrs; i++) {
681 struct perf_header_attr *attr;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200682 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200683
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200684 if (perf_header__getbuffer64(self, fd, &f_attr, sizeof(f_attr)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200685 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200686
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200687 tmp = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200688
689 attr = perf_header_attr__new(&f_attr.attr);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200690 if (attr == NULL)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200691 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200692
693 nr_ids = f_attr.ids.size / sizeof(u64);
694 lseek(fd, f_attr.ids.offset, SEEK_SET);
695
696 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200697 if (perf_header__getbuffer64(self, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200698 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200699
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200700 if (perf_header_attr__add_id(attr, f_id) < 0) {
701 perf_header_attr__delete(attr);
702 return -ENOMEM;
703 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200704 }
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200705 if (perf_header__add_attr(self, attr) < 0) {
706 perf_header_attr__delete(attr);
707 return -ENOMEM;
708 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200709
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200710 lseek(fd, tmp, SEEK_SET);
711 }
712
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200713 if (f_header.event_types.size) {
714 lseek(fd, f_header.event_types.offset, SEEK_SET);
715 events = malloc(f_header.event_types.size);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200716 if (events == NULL)
717 return -ENOMEM;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200718 if (perf_header__getbuffer64(self, fd, events,
719 f_header.event_types.size))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200720 goto out_errno;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200721 event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
722 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200723
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200724 perf_header__process_sections(self, fd, perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100725
Ingo Molnar2e01d172009-09-03 16:21:11 +0200726 lseek(fd, self->data_offset, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200727
728 self->frozen = 1;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200729 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200730out_errno:
731 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200732}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200733
734u64 perf_header__sample_type(struct perf_header *header)
735{
736 u64 type = 0;
737 int i;
738
739 for (i = 0; i < header->attrs; i++) {
740 struct perf_header_attr *attr = header->attr[i];
741
742 if (!type)
743 type = attr->attr.sample_type;
744 else if (type != attr->attr.sample_type)
745 die("non matching sample_type");
746 }
747
748 return type;
749}
750
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200751struct perf_event_attr *
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200752perf_header__find_attr(u64 id, struct perf_header *header)
753{
754 int i;
755
756 for (i = 0; i < header->attrs; i++) {
757 struct perf_header_attr *attr = header->attr[i];
758 int j;
759
760 for (j = 0; j < attr->ids; j++) {
761 if (attr->id[j] == id)
762 return &attr->attr;
763 }
764 }
765
766 return NULL;
767}