blob: f0138d47233977973f7df65e71c6792bd7d1ea3a [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
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020011#include "evlist.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020012#include "util.h"
13#include "header.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020014#include "../perf.h"
15#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020016#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010017#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010018#include "debug.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020019
Stephane Eraniana1ac1d32010-06-17 11:39:01 +020020static bool no_buildid_cache = false;
21
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020022/*
Arjan van de Venec60a3f2009-09-19 13:36:30 +020023 * Create new perf.data header attribute:
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020024 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020025struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020026{
27 struct perf_header_attr *self = malloc(sizeof(*self));
28
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -020029 if (self != NULL) {
30 self->attr = *attr;
31 self->ids = 0;
32 self->size = 1;
33 self->id = malloc(sizeof(u64));
34 if (self->id == NULL) {
35 free(self);
36 self = NULL;
37 }
38 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020039
40 return self;
41}
42
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020043void perf_header_attr__delete(struct perf_header_attr *self)
44{
45 free(self->id);
46 free(self);
47}
48
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -020049int perf_header_attr__add_id(struct perf_header_attr *self, u64 id)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020050{
51 int pos = self->ids;
52
53 self->ids++;
54 if (self->ids > self->size) {
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -020055 int nsize = self->size * 2;
56 u64 *nid = realloc(self->id, nsize * sizeof(u64));
57
58 if (nid == NULL)
59 return -1;
60
61 self->size = nsize;
62 self->id = nid;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020063 }
64 self->id[pos] = id;
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -020065 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020066}
67
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020068int perf_header__init(struct perf_header *self)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020069{
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020070 self->size = 1;
71 self->attr = malloc(sizeof(void *));
72 return self->attr == NULL ? -ENOMEM : 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020073}
74
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020075void perf_header__exit(struct perf_header *self)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020076{
77 int i;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020078 for (i = 0; i < self->attrs; ++i)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020079 perf_header_attr__delete(self->attr[i]);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020080 free(self->attr);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020081}
82
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020083int perf_header__add_attr(struct perf_header *self,
84 struct perf_header_attr *attr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020085{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020086 if (self->frozen)
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020087 return -1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020088
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020089 if (self->attrs == self->size) {
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020090 int nsize = self->size * 2;
91 struct perf_header_attr **nattr;
92
93 nattr = realloc(self->attr, nsize * sizeof(void *));
94 if (nattr == NULL)
95 return -1;
96
97 self->size = nsize;
98 self->attr = nattr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020099 }
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200100
101 self->attr[self->attrs++] = attr;
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200102 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200103}
104
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200105static int event_count;
106static struct perf_trace_event_type *events;
107
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -0200108int perf_header__push_event(u64 id, const char *name)
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200109{
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));
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -0200115 if (events == NULL)
116 return -ENOMEM;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200117 } else {
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -0200118 struct perf_trace_event_type *nevents;
119
120 nevents = realloc(events, (event_count + 1) * sizeof(*events));
121 if (nevents == NULL)
122 return -ENOMEM;
123 events = nevents;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200124 }
125 memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
126 events[event_count].event_id = id;
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200127 strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200128 event_count++;
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -0200129 return 0;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200130}
131
132char *perf_header__find_event(u64 id)
133{
134 int i;
135 for (i = 0 ; i < event_count; i++) {
136 if (events[i].event_id == id)
137 return events[i].name;
138 }
139 return NULL;
140}
141
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200142static const char *__perf_magic = "PERFFILE";
143
144#define PERF_MAGIC (*(u64 *)__perf_magic)
145
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200146struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200147 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200148 struct perf_file_section ids;
149};
150
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200151void perf_header__set_feat(struct perf_header *self, int feat)
152{
153 set_bit(feat, self->adds_features);
154}
155
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -0200156void perf_header__clear_feat(struct perf_header *self, int feat)
157{
158 clear_bit(feat, self->adds_features);
159}
160
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200161bool perf_header__has_feat(const struct perf_header *self, int feat)
162{
163 return test_bit(feat, self->adds_features);
164}
165
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200166static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200167{
168 while (size) {
169 int ret = write(fd, buf, size);
170
171 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200172 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200173
174 size -= ret;
175 buf += ret;
176 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200177
178 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200179}
180
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200181#define NAME_ALIGN 64
182
183static int write_padded(int fd, const void *bf, size_t count,
184 size_t count_aligned)
185{
186 static const char zero_buf[NAME_ALIGN];
187 int err = do_write(fd, bf, count);
188
189 if (!err)
190 err = do_write(fd, zero_buf, count_aligned - count);
191
192 return err;
193}
194
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200195#define dsos__for_each_with_build_id(pos, head) \
196 list_for_each_entry(pos, head, node) \
197 if (!pos->has_build_id) \
198 continue; \
199 else
200
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800201static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
202 u16 misc, int fd)
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100203{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200204 struct dso *pos;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100205
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200206 dsos__for_each_with_build_id(pos, head) {
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200207 int err;
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200208 struct build_id_event b;
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200209 size_t len;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100210
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200211 if (!pos->hit)
212 continue;
213 len = pos->long_name_len + 1;
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900214 len = ALIGN(len, NAME_ALIGN);
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200215 memset(&b, 0, sizeof(b));
216 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800217 b.pid = pid;
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -0200218 b.header.misc = misc;
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200219 b.header.size = sizeof(b) + len;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200220 err = do_write(fd, &b, sizeof(b));
221 if (err < 0)
222 return err;
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200223 err = write_padded(fd, pos->long_name,
224 pos->long_name_len + 1, len);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200225 if (err < 0)
226 return err;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100227 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200228
229 return 0;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100230}
231
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300232static int machine__write_buildid_table(struct machine *self, int fd)
233{
234 int err;
235 u16 kmisc = PERF_RECORD_MISC_KERNEL,
236 umisc = PERF_RECORD_MISC_USER;
237
238 if (!machine__is_host(self)) {
239 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
240 umisc = PERF_RECORD_MISC_GUEST_USER;
241 }
242
243 err = __dsos__write_buildid_table(&self->kernel_dsos, self->pid,
244 kmisc, fd);
245 if (err == 0)
246 err = __dsos__write_buildid_table(&self->user_dsos,
247 self->pid, umisc, fd);
248 return err;
249}
250
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800251static int dsos__write_buildid_table(struct perf_header *header, int fd)
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200252{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800253 struct perf_session *session = container_of(header,
254 struct perf_session, header);
255 struct rb_node *nd;
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300256 int err = machine__write_buildid_table(&session->host_machine, fd);
257
258 if (err)
259 return err;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800260
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300261 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
262 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300263 err = machine__write_buildid_table(pos, fd);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800264 if (err)
265 break;
266 }
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200267 return err;
268}
269
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200270int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
271 const char *name, bool is_kallsyms)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200272{
273 const size_t size = PATH_MAX;
Franck Bui-Huu68a7a772010-12-10 22:06:26 +0100274 char *realname = realpath(name, NULL),
275 *filename = malloc(size),
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200276 *linkname = malloc(size), *targetname;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200277 int len, err = -1;
278
Franck Bui-Huu68a7a772010-12-10 22:06:26 +0100279 if (realname == NULL || filename == NULL || linkname == NULL)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200280 goto out_free;
281
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200282 len = snprintf(filename, size, "%s%s%s",
Franck Bui-Huu68a7a772010-12-10 22:06:26 +0100283 debugdir, is_kallsyms ? "/" : "", realname);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200284 if (mkdir_p(filename, 0755))
285 goto out_free;
286
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200287 snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200288
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200289 if (access(filename, F_OK)) {
290 if (is_kallsyms) {
291 if (copyfile("/proc/kallsyms", filename))
292 goto out_free;
Franck Bui-Huu68a7a772010-12-10 22:06:26 +0100293 } else if (link(realname, filename) && copyfile(name, filename))
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200294 goto out_free;
295 }
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200296
297 len = snprintf(linkname, size, "%s/.build-id/%.2s",
298 debugdir, sbuild_id);
299
300 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
301 goto out_free;
302
303 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
304 targetname = filename + strlen(debugdir) - 5;
305 memcpy(targetname, "../..", 5);
306
307 if (symlink(targetname, linkname) == 0)
308 err = 0;
309out_free:
Franck Bui-Huu68a7a772010-12-10 22:06:26 +0100310 free(realname);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200311 free(filename);
312 free(linkname);
313 return err;
314}
315
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200316static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
317 const char *name, const char *debugdir,
318 bool is_kallsyms)
319{
320 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
321
322 build_id__sprintf(build_id, build_id_size, sbuild_id);
323
324 return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms);
325}
326
327int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
328{
329 const size_t size = PATH_MAX;
330 char *filename = malloc(size),
331 *linkname = malloc(size);
332 int err = -1;
333
334 if (filename == NULL || linkname == NULL)
335 goto out_free;
336
337 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
338 debugdir, sbuild_id, sbuild_id + 2);
339
340 if (access(linkname, F_OK))
341 goto out_free;
342
343 if (readlink(linkname, filename, size) < 0)
344 goto out_free;
345
346 if (unlink(linkname))
347 goto out_free;
348
349 /*
350 * Since the link is relative, we must make it absolute:
351 */
352 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
353 debugdir, sbuild_id, filename);
354
355 if (unlink(linkname))
356 goto out_free;
357
358 err = 0;
359out_free:
360 free(filename);
361 free(linkname);
362 return err;
363}
364
365static int dso__cache_build_id(struct dso *self, const char *debugdir)
366{
367 bool is_kallsyms = self->kernel && self->long_name[0] != '/';
368
369 return build_id_cache__add_b(self->build_id, sizeof(self->build_id),
370 self->long_name, debugdir, is_kallsyms);
371}
372
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200373static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
374{
375 struct dso *pos;
376 int err = 0;
377
378 dsos__for_each_with_build_id(pos, head)
379 if (dso__cache_build_id(pos, debugdir))
380 err = -1;
381
382 return err;
383}
384
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300385static int machine__cache_build_ids(struct machine *self, const char *debugdir)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200386{
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300387 int ret = __dsos__cache_build_ids(&self->kernel_dsos, debugdir);
388 ret |= __dsos__cache_build_ids(&self->user_dsos, debugdir);
389 return ret;
390}
391
392static int perf_session__cache_build_ids(struct perf_session *self)
393{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800394 struct rb_node *nd;
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300395 int ret;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200396 char debugdir[PATH_MAX];
397
Stephane Eranian45de34b2010-06-01 21:25:01 +0200398 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200399
400 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
401 return -1;
402
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300403 ret = machine__cache_build_ids(&self->host_machine, debugdir);
404
405 for (nd = rb_first(&self->machines); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300406 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300407 ret |= machine__cache_build_ids(pos, debugdir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800408 }
409 return ret ? -1 : 0;
410}
411
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300412static bool machine__read_build_ids(struct machine *self, bool with_hits)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800413{
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300414 bool ret = __dsos__read_build_ids(&self->kernel_dsos, with_hits);
415 ret |= __dsos__read_build_ids(&self->user_dsos, with_hits);
416 return ret;
417}
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800418
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300419static bool perf_session__read_build_ids(struct perf_session *self, bool with_hits)
420{
421 struct rb_node *nd;
422 bool ret = machine__read_build_ids(&self->host_machine, with_hits);
423
424 for (nd = rb_first(&self->machines); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300425 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300426 ret |= machine__read_build_ids(pos, with_hits);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800427 }
428
429 return ret;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200430}
431
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200432static int perf_header__adds_write(struct perf_header *self,
433 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200434{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100435 int nr_sections;
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300436 struct perf_session *session;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100437 struct perf_file_section *feat_sec;
438 int sec_size;
439 u64 sec_start;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200440 int idx = 0, err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100441
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300442 session = container_of(self, struct perf_session, header);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -0200443
444 if (perf_header__has_feat(self, HEADER_BUILD_ID &&
445 !perf_session__read_build_ids(session, true)))
446 perf_header__clear_feat(self, HEADER_BUILD_ID);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100447
448 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
449 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200450 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100451
452 feat_sec = calloc(sizeof(*feat_sec), nr_sections);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200453 if (feat_sec == NULL)
454 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100455
456 sec_size = sizeof(*feat_sec) * nr_sections;
457
458 sec_start = self->data_offset + self->data_size;
Xiao Guangrongf887f302010-02-04 16:46:42 +0800459 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200460
Frederic Weisbecker3e13ab22009-11-11 04:51:06 +0100461 if (perf_header__has_feat(self, HEADER_TRACE_INFO)) {
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100462 struct perf_file_section *trace_sec;
463
464 trace_sec = &feat_sec[idx++];
465
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200466 /* Write trace info */
Xiao Guangrongf887f302010-02-04 16:46:42 +0800467 trace_sec->offset = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200468 read_tracing_data(fd, &evlist->entries);
Xiao Guangrongf887f302010-02-04 16:46:42 +0800469 trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200470 }
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100471
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100472 if (perf_header__has_feat(self, HEADER_BUILD_ID)) {
473 struct perf_file_section *buildid_sec;
474
475 buildid_sec = &feat_sec[idx++];
476
477 /* Write build-ids */
Xiao Guangrongf887f302010-02-04 16:46:42 +0800478 buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800479 err = dsos__write_buildid_table(self, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200480 if (err < 0) {
481 pr_debug("failed to write buildid table\n");
482 goto out_free;
483 }
Xiao Guangrongf887f302010-02-04 16:46:42 +0800484 buildid_sec->size = lseek(fd, 0, SEEK_CUR) -
485 buildid_sec->offset;
Stephane Eraniana1ac1d32010-06-17 11:39:01 +0200486 if (!no_buildid_cache)
487 perf_session__cache_build_ids(session);
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100488 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100489
Xiao Guangrongf887f302010-02-04 16:46:42 +0800490 lseek(fd, sec_start, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200491 err = do_write(fd, feat_sec, sec_size);
492 if (err < 0)
493 pr_debug("failed to write feature section\n");
494out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100495 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200496 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100497}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200498
Tom Zanussi8dc58102010-04-01 23:59:15 -0500499int perf_header__write_pipe(int fd)
500{
501 struct perf_pipe_file_header f_header;
502 int err;
503
504 f_header = (struct perf_pipe_file_header){
505 .magic = PERF_MAGIC,
506 .size = sizeof(f_header),
507 };
508
509 err = do_write(fd, &f_header, sizeof(f_header));
510 if (err < 0) {
511 pr_debug("failed to write perf pipe header\n");
512 return err;
513 }
514
515 return 0;
516}
517
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200518int perf_header__write(struct perf_header *self, struct perf_evlist *evlist,
519 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200520{
521 struct perf_file_header f_header;
522 struct perf_file_attr f_attr;
523 struct perf_header_attr *attr;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200524 int i, err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200525
526 lseek(fd, sizeof(f_header), SEEK_SET);
527
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200528 for (i = 0; i < self->attrs; i++) {
529 attr = self->attr[i];
530
531 attr->id_offset = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200532 err = do_write(fd, attr->id, attr->ids * sizeof(u64));
533 if (err < 0) {
534 pr_debug("failed to write perf header\n");
535 return err;
536 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200537 }
538
539
540 self->attr_offset = lseek(fd, 0, SEEK_CUR);
541
542 for (i = 0; i < self->attrs; i++) {
543 attr = self->attr[i];
544
545 f_attr = (struct perf_file_attr){
546 .attr = attr->attr,
547 .ids = {
548 .offset = attr->id_offset,
549 .size = attr->ids * sizeof(u64),
550 }
551 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200552 err = do_write(fd, &f_attr, sizeof(f_attr));
553 if (err < 0) {
554 pr_debug("failed to write perf header attribute\n");
555 return err;
556 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200557 }
558
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200559 self->event_offset = lseek(fd, 0, SEEK_CUR);
560 self->event_size = event_count * sizeof(struct perf_trace_event_type);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200561 if (events) {
562 err = do_write(fd, events, self->event_size);
563 if (err < 0) {
564 pr_debug("failed to write perf header events\n");
565 return err;
566 }
567 }
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200568
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200569 self->data_offset = lseek(fd, 0, SEEK_CUR);
570
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200571 if (at_exit) {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200572 err = perf_header__adds_write(self, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200573 if (err < 0)
574 return err;
575 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100576
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200577 f_header = (struct perf_file_header){
578 .magic = PERF_MAGIC,
579 .size = sizeof(f_header),
580 .attr_size = sizeof(f_attr),
581 .attrs = {
582 .offset = self->attr_offset,
583 .size = self->attrs * sizeof(f_attr),
584 },
585 .data = {
586 .offset = self->data_offset,
587 .size = self->data_size,
588 },
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200589 .event_types = {
590 .offset = self->event_offset,
591 .size = self->event_size,
592 },
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200593 };
594
Frederic Weisbeckerdb9f11e2009-10-17 17:57:18 +0200595 memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200596
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200597 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200598 err = do_write(fd, &f_header, sizeof(f_header));
599 if (err < 0) {
600 pr_debug("failed to write perf header\n");
601 return err;
602 }
Xiao Guangrongf887f302010-02-04 16:46:42 +0800603 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200604
605 self->frozen = 1;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200606 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200607}
608
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200609static int perf_header__getbuffer64(struct perf_header *self,
610 int fd, void *buf, size_t size)
611{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200612 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200613 return -1;
614
615 if (self->needs_swap)
616 mem_bswap_64(buf, size);
617
618 return 0;
619}
620
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200621int perf_header__process_sections(struct perf_header *self, int fd,
622 int (*process)(struct perf_file_section *self,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200623 struct perf_header *ph,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200624 int feat, int fd))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200625{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100626 struct perf_file_section *feat_sec;
627 int nr_sections;
628 int sec_size;
629 int idx = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200630 int err = -1, feat = 1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100631
632 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
633 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200634 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100635
636 feat_sec = calloc(sizeof(*feat_sec), nr_sections);
637 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200638 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100639
640 sec_size = sizeof(*feat_sec) * nr_sections;
641
Xiao Guangrongf887f302010-02-04 16:46:42 +0800642 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100643
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200644 if (perf_header__getbuffer64(self, fd, feat_sec, sec_size))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200645 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100646
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200647 err = 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200648 while (idx < nr_sections && feat < HEADER_LAST_FEATURE) {
649 if (perf_header__has_feat(self, feat)) {
650 struct perf_file_section *sec = &feat_sec[idx++];
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100651
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200652 err = process(sec, self, feat, fd);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200653 if (err < 0)
654 break;
655 }
656 ++feat;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100657 }
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200658out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100659 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200660 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200661}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200662
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200663int perf_file_header__read(struct perf_file_header *self,
664 struct perf_header *ph, int fd)
665{
666 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200667
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200668 if (readn(fd, self, sizeof(*self)) <= 0 ||
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200669 memcmp(&self->magic, __perf_magic, sizeof(self->magic)))
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200670 return -1;
671
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200672 if (self->attr_size != sizeof(struct perf_file_attr)) {
673 u64 attr_size = bswap_64(self->attr_size);
674
675 if (attr_size != sizeof(struct perf_file_attr))
676 return -1;
677
678 mem_bswap_64(self, offsetof(struct perf_file_header,
679 adds_features));
680 ph->needs_swap = true;
681 }
682
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200683 if (self->size != sizeof(*self)) {
684 /* Support the previous format */
685 if (self->size == offsetof(typeof(*self), adds_features))
686 bitmap_zero(self->adds_features, HEADER_FEAT_BITS);
687 else
688 return -1;
689 }
690
691 memcpy(&ph->adds_features, &self->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200692 sizeof(ph->adds_features));
693 /*
694 * FIXME: hack that assumes that if we need swap the perf.data file
695 * may be coming from an arch with a different word-size, ergo different
696 * DEFINE_BITMAP format, investigate more later, but for now its mostly
697 * safe to assume that we have a build-id section. Trace files probably
698 * have several other issues in this realm anyway...
699 */
700 if (ph->needs_swap) {
701 memset(&ph->adds_features, 0, sizeof(ph->adds_features));
702 perf_header__set_feat(ph, HEADER_BUILD_ID);
703 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200704
705 ph->event_offset = self->event_types.offset;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200706 ph->event_size = self->event_types.size;
707 ph->data_offset = self->data.offset;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200708 ph->data_size = self->data.size;
709 return 0;
710}
711
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800712static int __event_process_build_id(struct build_id_event *bev,
713 char *filename,
714 struct perf_session *session)
715{
716 int err = -1;
717 struct list_head *head;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300718 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800719 u16 misc;
720 struct dso *dso;
721 enum dso_kernel_type dso_type;
722
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300723 machine = perf_session__findnew_machine(session, bev->pid);
724 if (!machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800725 goto out;
726
727 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
728
729 switch (misc) {
730 case PERF_RECORD_MISC_KERNEL:
731 dso_type = DSO_TYPE_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300732 head = &machine->kernel_dsos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800733 break;
734 case PERF_RECORD_MISC_GUEST_KERNEL:
735 dso_type = DSO_TYPE_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300736 head = &machine->kernel_dsos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800737 break;
738 case PERF_RECORD_MISC_USER:
739 case PERF_RECORD_MISC_GUEST_USER:
740 dso_type = DSO_TYPE_USER;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300741 head = &machine->user_dsos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800742 break;
743 default:
744 goto out;
745 }
746
747 dso = __dsos__findnew(head, filename);
748 if (dso != NULL) {
Tom Zanussi454c4072010-05-01 01:41:20 -0500749 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
750
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800751 dso__set_build_id(dso, &bev->build_id);
Tom Zanussi454c4072010-05-01 01:41:20 -0500752
753 if (filename[0] == '[')
754 dso->kernel = dso_type;
755
756 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
757 sbuild_id);
758 pr_debug("build id event received for %s: %s\n",
759 dso->long_name, sbuild_id);
760 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800761
762 err = 0;
763out:
764 return err;
765}
766
767static int perf_header__read_build_ids(struct perf_header *self,
768 int input, u64 offset, u64 size)
769{
770 struct perf_session *session = container_of(self,
771 struct perf_session, header);
772 struct build_id_event bev;
773 char filename[PATH_MAX];
774 u64 limit = offset + size;
775 int err = -1;
776
777 while (offset < limit) {
778 ssize_t len;
779
780 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
781 goto out;
782
783 if (self->needs_swap)
784 perf_event_header__bswap(&bev.header);
785
786 len = bev.header.size - sizeof(bev);
787 if (read(input, filename, len) != len)
788 goto out;
789
790 __event_process_build_id(&bev, filename, session);
791
792 offset += bev.header.size;
793 }
794 err = 0;
795out:
796 return err;
797}
798
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200799static int perf_file_section__process(struct perf_file_section *self,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200800 struct perf_header *ph,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200801 int feat, int fd)
802{
Xiao Guangrongf887f302010-02-04 16:46:42 +0800803 if (lseek(fd, self->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200804 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
805 "%d, continuing...\n", self->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200806 return 0;
807 }
808
809 switch (feat) {
810 case HEADER_TRACE_INFO:
Tom Zanussi454c4072010-05-01 01:41:20 -0500811 trace_report(fd, false);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200812 break;
813
814 case HEADER_BUILD_ID:
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200815 if (perf_header__read_build_ids(ph, fd, self->offset, self->size))
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200816 pr_debug("Failed to read buildids, continuing...\n");
817 break;
818 default:
819 pr_debug("unknown feature %d, continuing...\n", feat);
820 }
821
822 return 0;
823}
824
Tom Zanussi8dc58102010-04-01 23:59:15 -0500825static int perf_file_header__read_pipe(struct perf_pipe_file_header *self,
Tom Zanussi454c4072010-05-01 01:41:20 -0500826 struct perf_header *ph, int fd,
827 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200828{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200829 if (readn(fd, self, sizeof(*self)) <= 0 ||
Tom Zanussi8dc58102010-04-01 23:59:15 -0500830 memcmp(&self->magic, __perf_magic, sizeof(self->magic)))
831 return -1;
832
Tom Zanussi454c4072010-05-01 01:41:20 -0500833 if (repipe && do_write(STDOUT_FILENO, self, sizeof(*self)) < 0)
834 return -1;
835
Tom Zanussi8dc58102010-04-01 23:59:15 -0500836 if (self->size != sizeof(*self)) {
837 u64 size = bswap_64(self->size);
838
839 if (size != sizeof(*self))
840 return -1;
841
842 ph->needs_swap = true;
843 }
844
845 return 0;
846}
847
848static int perf_header__read_pipe(struct perf_session *session, int fd)
849{
850 struct perf_header *self = &session->header;
851 struct perf_pipe_file_header f_header;
852
Tom Zanussi454c4072010-05-01 01:41:20 -0500853 if (perf_file_header__read_pipe(&f_header, self, fd,
854 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -0500855 pr_debug("incompatible file format\n");
856 return -EINVAL;
857 }
858
859 session->fd = fd;
860
861 return 0;
862}
863
864int perf_header__read(struct perf_session *session, int fd)
865{
866 struct perf_header *self = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200867 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200868 struct perf_file_attr f_attr;
869 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200870 int nr_attrs, nr_ids, i, j;
871
Tom Zanussi8dc58102010-04-01 23:59:15 -0500872 if (session->fd_pipe)
873 return perf_header__read_pipe(session, fd);
874
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200875 if (perf_file_header__read(&f_header, self, fd) < 0) {
876 pr_debug("incompatible file format\n");
877 return -EINVAL;
878 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200879
880 nr_attrs = f_header.attrs.size / sizeof(f_attr);
881 lseek(fd, f_header.attrs.offset, SEEK_SET);
882
883 for (i = 0; i < nr_attrs; i++) {
884 struct perf_header_attr *attr;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200885 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200886
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200887 if (perf_header__getbuffer64(self, fd, &f_attr, sizeof(f_attr)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200888 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200889
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200890 tmp = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200891
892 attr = perf_header_attr__new(&f_attr.attr);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200893 if (attr == NULL)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200894 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200895
896 nr_ids = f_attr.ids.size / sizeof(u64);
897 lseek(fd, f_attr.ids.offset, SEEK_SET);
898
899 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200900 if (perf_header__getbuffer64(self, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200901 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200902
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200903 if (perf_header_attr__add_id(attr, f_id) < 0) {
904 perf_header_attr__delete(attr);
905 return -ENOMEM;
906 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200907 }
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200908 if (perf_header__add_attr(self, attr) < 0) {
909 perf_header_attr__delete(attr);
910 return -ENOMEM;
911 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200912
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200913 lseek(fd, tmp, SEEK_SET);
914 }
915
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200916 if (f_header.event_types.size) {
917 lseek(fd, f_header.event_types.offset, SEEK_SET);
918 events = malloc(f_header.event_types.size);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200919 if (events == NULL)
920 return -ENOMEM;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200921 if (perf_header__getbuffer64(self, fd, events,
922 f_header.event_types.size))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200923 goto out_errno;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200924 event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
925 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200926
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200927 perf_header__process_sections(self, fd, perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100928
Ingo Molnar2e01d172009-09-03 16:21:11 +0200929 lseek(fd, self->data_offset, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200930
931 self->frozen = 1;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200932 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200933out_errno:
934 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200935}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200936
937u64 perf_header__sample_type(struct perf_header *header)
938{
939 u64 type = 0;
940 int i;
941
942 for (i = 0; i < header->attrs; i++) {
943 struct perf_header_attr *attr = header->attr[i];
944
945 if (!type)
946 type = attr->attr.sample_type;
947 else if (type != attr->attr.sample_type)
948 die("non matching sample_type");
949 }
950
951 return type;
952}
953
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200954bool perf_header__sample_id_all(const struct perf_header *header)
955{
956 bool value = false, first = true;
957 int i;
958
959 for (i = 0; i < header->attrs; i++) {
960 struct perf_header_attr *attr = header->attr[i];
961
962 if (first) {
963 value = attr->attr.sample_id_all;
964 first = false;
965 } else if (value != attr->attr.sample_id_all)
966 die("non matching sample_id_all");
967 }
968
969 return value;
970}
971
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200972struct perf_event_attr *
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200973perf_header__find_attr(u64 id, struct perf_header *header)
974{
975 int i;
976
Anton Blanchard02bf60a2010-05-04 21:19:15 +1000977 /*
978 * We set id to -1 if the data file doesn't contain sample
Franck Bui-Huu60e67732010-11-30 12:50:14 +0100979 * ids. This can happen when the data file contains one type
980 * of event and in that case, the header can still store the
981 * event attribute information. Check for this and avoid
982 * walking through the entire list of ids which may be large.
Anton Blanchard02bf60a2010-05-04 21:19:15 +1000983 */
Franck Bui-Huu60e67732010-11-30 12:50:14 +0100984 if (id == -1ULL) {
985 if (header->attrs > 0)
986 return &header->attr[0]->attr;
Anton Blanchard02bf60a2010-05-04 21:19:15 +1000987 return NULL;
Franck Bui-Huu60e67732010-11-30 12:50:14 +0100988 }
Anton Blanchard02bf60a2010-05-04 21:19:15 +1000989
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200990 for (i = 0; i < header->attrs; i++) {
991 struct perf_header_attr *attr = header->attr[i];
992 int j;
993
994 for (j = 0; j < attr->ids; j++) {
995 if (attr->id[j] == id)
996 return &attr->attr;
997 }
998 }
999
1000 return NULL;
1001}
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05001002
1003int event__synthesize_attr(struct perf_event_attr *attr, u16 ids, u64 *id,
1004 event__handler_t process,
1005 struct perf_session *session)
1006{
1007 event_t *ev;
1008 size_t size;
1009 int err;
1010
1011 size = sizeof(struct perf_event_attr);
1012 size = ALIGN(size, sizeof(u64));
1013 size += sizeof(struct perf_event_header);
1014 size += ids * sizeof(u64);
1015
1016 ev = malloc(size);
1017
Chris Samuelce47dc52010-11-13 13:35:06 +11001018 if (ev == NULL)
1019 return -ENOMEM;
1020
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05001021 ev->attr.attr = *attr;
1022 memcpy(ev->attr.id, id, ids * sizeof(u64));
1023
1024 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
1025 ev->attr.header.size = size;
1026
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001027 err = process(ev, NULL, session);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05001028
1029 free(ev);
1030
1031 return err;
1032}
1033
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001034int event__synthesize_attrs(struct perf_header *self, event__handler_t process,
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05001035 struct perf_session *session)
1036{
1037 struct perf_header_attr *attr;
1038 int i, err = 0;
1039
1040 for (i = 0; i < self->attrs; i++) {
1041 attr = self->attr[i];
1042
1043 err = event__synthesize_attr(&attr->attr, attr->ids, attr->id,
1044 process, session);
1045 if (err) {
1046 pr_debug("failed to create perf header attribute\n");
1047 return err;
1048 }
1049 }
1050
1051 return err;
1052}
1053
1054int event__process_attr(event_t *self, struct perf_session *session)
1055{
1056 struct perf_header_attr *attr;
1057 unsigned int i, ids, n_ids;
1058
1059 attr = perf_header_attr__new(&self->attr.attr);
1060 if (attr == NULL)
1061 return -ENOMEM;
1062
1063 ids = self->header.size;
1064 ids -= (void *)&self->attr.id - (void *)self;
1065 n_ids = ids / sizeof(u64);
1066
1067 for (i = 0; i < n_ids; i++) {
1068 if (perf_header_attr__add_id(attr, self->attr.id[i]) < 0) {
1069 perf_header_attr__delete(attr);
1070 return -ENOMEM;
1071 }
1072 }
1073
1074 if (perf_header__add_attr(&session->header, attr) < 0) {
1075 perf_header_attr__delete(attr);
1076 return -ENOMEM;
1077 }
1078
1079 perf_session__update_sample_type(session);
1080
1081 return 0;
1082}
Tom Zanussicd19a032010-04-01 23:59:20 -05001083
1084int event__synthesize_event_type(u64 event_id, char *name,
1085 event__handler_t process,
1086 struct perf_session *session)
1087{
1088 event_t ev;
1089 size_t size = 0;
1090 int err = 0;
1091
1092 memset(&ev, 0, sizeof(ev));
1093
1094 ev.event_type.event_type.event_id = event_id;
1095 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
1096 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
1097
1098 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
1099 size = strlen(name);
1100 size = ALIGN(size, sizeof(u64));
1101 ev.event_type.header.size = sizeof(ev.event_type) -
1102 (sizeof(ev.event_type.event_type.name) - size);
1103
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001104 err = process(&ev, NULL, session);
Tom Zanussicd19a032010-04-01 23:59:20 -05001105
1106 return err;
1107}
1108
1109int event__synthesize_event_types(event__handler_t process,
1110 struct perf_session *session)
1111{
1112 struct perf_trace_event_type *type;
1113 int i, err = 0;
1114
1115 for (i = 0; i < event_count; i++) {
1116 type = &events[i];
1117
1118 err = event__synthesize_event_type(type->event_id, type->name,
1119 process, session);
1120 if (err) {
1121 pr_debug("failed to create perf header event type\n");
1122 return err;
1123 }
1124 }
1125
1126 return err;
1127}
1128
1129int event__process_event_type(event_t *self,
1130 struct perf_session *session __unused)
1131{
1132 if (perf_header__push_event(self->event_type.event_type.event_id,
1133 self->event_type.event_type.name) < 0)
1134 return -ENOMEM;
1135
1136 return 0;
1137}
Tom Zanussi92155452010-04-01 23:59:21 -05001138
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001139int event__synthesize_tracing_data(int fd, struct perf_evlist *evlist,
Tom Zanussi92155452010-04-01 23:59:21 -05001140 event__handler_t process,
1141 struct perf_session *session __unused)
1142{
1143 event_t ev;
1144 ssize_t size = 0, aligned_size = 0, padding;
1145 int err = 0;
1146
1147 memset(&ev, 0, sizeof(ev));
1148
1149 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001150 size = read_tracing_data_size(fd, &evlist->entries);
Tom Zanussi92155452010-04-01 23:59:21 -05001151 if (size <= 0)
1152 return size;
1153 aligned_size = ALIGN(size, sizeof(u64));
1154 padding = aligned_size - size;
1155 ev.tracing_data.header.size = sizeof(ev.tracing_data);
1156 ev.tracing_data.size = aligned_size;
1157
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001158 process(&ev, NULL, session);
Tom Zanussi92155452010-04-01 23:59:21 -05001159
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001160 err = read_tracing_data(fd, &evlist->entries);
Tom Zanussi92155452010-04-01 23:59:21 -05001161 write_padded(fd, NULL, 0, padding);
1162
1163 return aligned_size;
1164}
1165
1166int event__process_tracing_data(event_t *self,
1167 struct perf_session *session)
1168{
1169 ssize_t size_read, padding, size = self->tracing_data.size;
1170 off_t offset = lseek(session->fd, 0, SEEK_CUR);
1171 char buf[BUFSIZ];
1172
1173 /* setup for reading amidst mmap */
1174 lseek(session->fd, offset + sizeof(struct tracing_data_event),
1175 SEEK_SET);
1176
Tom Zanussi454c4072010-05-01 01:41:20 -05001177 size_read = trace_report(session->fd, session->repipe);
Tom Zanussi92155452010-04-01 23:59:21 -05001178
1179 padding = ALIGN(size_read, sizeof(u64)) - size_read;
1180
1181 if (read(session->fd, buf, padding) < 0)
1182 die("reading input file");
Tom Zanussi454c4072010-05-01 01:41:20 -05001183 if (session->repipe) {
1184 int retw = write(STDOUT_FILENO, buf, padding);
1185 if (retw <= 0 || retw != padding)
1186 die("repiping tracing data padding");
1187 }
Tom Zanussi92155452010-04-01 23:59:21 -05001188
1189 if (size_read + padding != size)
1190 die("tracing data size mismatch");
1191
1192 return size_read + padding;
1193}
Tom Zanussic7929e42010-04-01 23:59:22 -05001194
1195int event__synthesize_build_id(struct dso *pos, u16 misc,
Tom Zanussi454c4072010-05-01 01:41:20 -05001196 event__handler_t process,
1197 struct machine *machine,
Tom Zanussic7929e42010-04-01 23:59:22 -05001198 struct perf_session *session)
1199{
1200 event_t ev;
1201 size_t len;
1202 int err = 0;
1203
1204 if (!pos->hit)
1205 return err;
1206
1207 memset(&ev, 0, sizeof(ev));
1208
1209 len = pos->long_name_len + 1;
1210 len = ALIGN(len, NAME_ALIGN);
1211 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
1212 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
1213 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001214 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05001215 ev.build_id.header.size = sizeof(ev.build_id) + len;
1216 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
1217
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001218 err = process(&ev, NULL, session);
Tom Zanussic7929e42010-04-01 23:59:22 -05001219
1220 return err;
1221}
1222
Tom Zanussic7929e42010-04-01 23:59:22 -05001223int event__process_build_id(event_t *self,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001224 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05001225{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001226 __event_process_build_id(&self->build_id,
1227 self->build_id.filename,
1228 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05001229 return 0;
1230}
Stephane Eraniana1ac1d32010-06-17 11:39:01 +02001231
1232void disable_buildid_cache(void)
1233{
1234 no_buildid_cache = true;
1235}