blob: f6a929e74981e48844b0af2d21599ff5bb725ac3 [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
Stephane Eraniana1ac1d32010-06-17 11:39:01 +020019static bool no_buildid_cache = false;
20
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020021/*
Arjan van de Venec60a3f2009-09-19 13:36:30 +020022 * Create new perf.data header attribute:
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020023 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020024struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020025{
26 struct perf_header_attr *self = malloc(sizeof(*self));
27
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -020028 if (self != NULL) {
29 self->attr = *attr;
30 self->ids = 0;
31 self->size = 1;
32 self->id = malloc(sizeof(u64));
33 if (self->id == NULL) {
34 free(self);
35 self = NULL;
36 }
37 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020038
39 return self;
40}
41
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020042void perf_header_attr__delete(struct perf_header_attr *self)
43{
44 free(self->id);
45 free(self);
46}
47
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -020048int perf_header_attr__add_id(struct perf_header_attr *self, u64 id)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020049{
50 int pos = self->ids;
51
52 self->ids++;
53 if (self->ids > self->size) {
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -020054 int nsize = self->size * 2;
55 u64 *nid = realloc(self->id, nsize * sizeof(u64));
56
57 if (nid == NULL)
58 return -1;
59
60 self->size = nsize;
61 self->id = nid;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020062 }
63 self->id[pos] = id;
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -020064 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020065}
66
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020067int perf_header__init(struct perf_header *self)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020068{
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020069 self->size = 1;
70 self->attr = malloc(sizeof(void *));
71 return self->attr == NULL ? -ENOMEM : 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020072}
73
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020074void perf_header__exit(struct perf_header *self)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020075{
76 int i;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020077 for (i = 0; i < self->attrs; ++i)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020078 perf_header_attr__delete(self->attr[i]);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020079 free(self->attr);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020080}
81
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020082int perf_header__add_attr(struct perf_header *self,
83 struct perf_header_attr *attr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020084{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020085 if (self->frozen)
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020086 return -1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020087
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020088 if (self->attrs == self->size) {
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020089 int nsize = self->size * 2;
90 struct perf_header_attr **nattr;
91
92 nattr = realloc(self->attr, nsize * sizeof(void *));
93 if (nattr == NULL)
94 return -1;
95
96 self->size = nsize;
97 self->attr = nattr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020098 }
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020099
100 self->attr[self->attrs++] = attr;
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200101 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200102}
103
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200104static int event_count;
105static struct perf_trace_event_type *events;
106
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -0200107int perf_header__push_event(u64 id, const char *name)
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200108{
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200109 if (strlen(name) > MAX_EVENT_NAME)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200110 pr_warning("Event %s will be truncated\n", name);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200111
112 if (!events) {
113 events = malloc(sizeof(struct perf_trace_event_type));
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -0200114 if (events == NULL)
115 return -ENOMEM;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200116 } else {
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -0200117 struct perf_trace_event_type *nevents;
118
119 nevents = realloc(events, (event_count + 1) * sizeof(*events));
120 if (nevents == NULL)
121 return -ENOMEM;
122 events = nevents;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200123 }
124 memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
125 events[event_count].event_id = id;
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200126 strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200127 event_count++;
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -0200128 return 0;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200129}
130
131char *perf_header__find_event(u64 id)
132{
133 int i;
134 for (i = 0 ; i < event_count; i++) {
135 if (events[i].event_id == id)
136 return events[i].name;
137 }
138 return NULL;
139}
140
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200141static const char *__perf_magic = "PERFFILE";
142
143#define PERF_MAGIC (*(u64 *)__perf_magic)
144
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200145struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200146 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200147 struct perf_file_section ids;
148};
149
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200150void perf_header__set_feat(struct perf_header *self, int feat)
151{
152 set_bit(feat, self->adds_features);
153}
154
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -0200155void perf_header__clear_feat(struct perf_header *self, int feat)
156{
157 clear_bit(feat, self->adds_features);
158}
159
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200160bool 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
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800200static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
201 u16 misc, int fd)
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100202{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200203 struct dso *pos;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100204
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200205 dsos__for_each_with_build_id(pos, head) {
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200206 int err;
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200207 struct build_id_event b;
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200208 size_t len;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100209
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200210 if (!pos->hit)
211 continue;
212 len = pos->long_name_len + 1;
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900213 len = ALIGN(len, NAME_ALIGN);
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200214 memset(&b, 0, sizeof(b));
215 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800216 b.pid = pid;
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -0200217 b.header.misc = misc;
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200218 b.header.size = sizeof(b) + len;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200219 err = do_write(fd, &b, sizeof(b));
220 if (err < 0)
221 return err;
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200222 err = write_padded(fd, pos->long_name,
223 pos->long_name_len + 1, len);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200224 if (err < 0)
225 return err;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100226 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200227
228 return 0;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100229}
230
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300231static int machine__write_buildid_table(struct machine *self, int fd)
232{
233 int err;
234 u16 kmisc = PERF_RECORD_MISC_KERNEL,
235 umisc = PERF_RECORD_MISC_USER;
236
237 if (!machine__is_host(self)) {
238 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
239 umisc = PERF_RECORD_MISC_GUEST_USER;
240 }
241
242 err = __dsos__write_buildid_table(&self->kernel_dsos, self->pid,
243 kmisc, fd);
244 if (err == 0)
245 err = __dsos__write_buildid_table(&self->user_dsos,
246 self->pid, umisc, fd);
247 return err;
248}
249
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800250static int dsos__write_buildid_table(struct perf_header *header, int fd)
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200251{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800252 struct perf_session *session = container_of(header,
253 struct perf_session, header);
254 struct rb_node *nd;
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300255 int err = machine__write_buildid_table(&session->host_machine, fd);
256
257 if (err)
258 return err;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800259
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300260 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
261 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300262 err = machine__write_buildid_table(pos, fd);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800263 if (err)
264 break;
265 }
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200266 return err;
267}
268
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200269int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
270 const char *name, bool is_kallsyms)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200271{
272 const size_t size = PATH_MAX;
Franck Bui-Huu68a7a772010-12-10 22:06:26 +0100273 char *realname = realpath(name, NULL),
274 *filename = malloc(size),
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200275 *linkname = malloc(size), *targetname;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200276 int len, err = -1;
277
Franck Bui-Huu68a7a772010-12-10 22:06:26 +0100278 if (realname == NULL || filename == NULL || linkname == NULL)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200279 goto out_free;
280
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200281 len = snprintf(filename, size, "%s%s%s",
Franck Bui-Huu68a7a772010-12-10 22:06:26 +0100282 debugdir, is_kallsyms ? "/" : "", realname);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200283 if (mkdir_p(filename, 0755))
284 goto out_free;
285
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200286 snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200287
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200288 if (access(filename, F_OK)) {
289 if (is_kallsyms) {
290 if (copyfile("/proc/kallsyms", filename))
291 goto out_free;
Franck Bui-Huu68a7a772010-12-10 22:06:26 +0100292 } else if (link(realname, filename) && copyfile(name, filename))
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200293 goto out_free;
294 }
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200295
296 len = snprintf(linkname, size, "%s/.build-id/%.2s",
297 debugdir, sbuild_id);
298
299 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
300 goto out_free;
301
302 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
303 targetname = filename + strlen(debugdir) - 5;
304 memcpy(targetname, "../..", 5);
305
306 if (symlink(targetname, linkname) == 0)
307 err = 0;
308out_free:
Franck Bui-Huu68a7a772010-12-10 22:06:26 +0100309 free(realname);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200310 free(filename);
311 free(linkname);
312 return err;
313}
314
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200315static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
316 const char *name, const char *debugdir,
317 bool is_kallsyms)
318{
319 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
320
321 build_id__sprintf(build_id, build_id_size, sbuild_id);
322
323 return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms);
324}
325
326int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
327{
328 const size_t size = PATH_MAX;
329 char *filename = malloc(size),
330 *linkname = malloc(size);
331 int err = -1;
332
333 if (filename == NULL || linkname == NULL)
334 goto out_free;
335
336 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
337 debugdir, sbuild_id, sbuild_id + 2);
338
339 if (access(linkname, F_OK))
340 goto out_free;
341
342 if (readlink(linkname, filename, size) < 0)
343 goto out_free;
344
345 if (unlink(linkname))
346 goto out_free;
347
348 /*
349 * Since the link is relative, we must make it absolute:
350 */
351 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
352 debugdir, sbuild_id, filename);
353
354 if (unlink(linkname))
355 goto out_free;
356
357 err = 0;
358out_free:
359 free(filename);
360 free(linkname);
361 return err;
362}
363
364static int dso__cache_build_id(struct dso *self, const char *debugdir)
365{
366 bool is_kallsyms = self->kernel && self->long_name[0] != '/';
367
368 return build_id_cache__add_b(self->build_id, sizeof(self->build_id),
369 self->long_name, debugdir, is_kallsyms);
370}
371
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200372static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
373{
374 struct dso *pos;
375 int err = 0;
376
377 dsos__for_each_with_build_id(pos, head)
378 if (dso__cache_build_id(pos, debugdir))
379 err = -1;
380
381 return err;
382}
383
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300384static int machine__cache_build_ids(struct machine *self, const char *debugdir)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200385{
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300386 int ret = __dsos__cache_build_ids(&self->kernel_dsos, debugdir);
387 ret |= __dsos__cache_build_ids(&self->user_dsos, debugdir);
388 return ret;
389}
390
391static int perf_session__cache_build_ids(struct perf_session *self)
392{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800393 struct rb_node *nd;
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300394 int ret;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200395 char debugdir[PATH_MAX];
396
Stephane Eranian45de34b2010-06-01 21:25:01 +0200397 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200398
399 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
400 return -1;
401
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300402 ret = machine__cache_build_ids(&self->host_machine, debugdir);
403
404 for (nd = rb_first(&self->machines); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300405 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300406 ret |= machine__cache_build_ids(pos, debugdir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800407 }
408 return ret ? -1 : 0;
409}
410
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300411static bool machine__read_build_ids(struct machine *self, bool with_hits)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800412{
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300413 bool ret = __dsos__read_build_ids(&self->kernel_dsos, with_hits);
414 ret |= __dsos__read_build_ids(&self->user_dsos, with_hits);
415 return ret;
416}
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800417
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300418static bool perf_session__read_build_ids(struct perf_session *self, bool with_hits)
419{
420 struct rb_node *nd;
421 bool ret = machine__read_build_ids(&self->host_machine, with_hits);
422
423 for (nd = rb_first(&self->machines); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300424 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300425 ret |= machine__read_build_ids(pos, with_hits);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800426 }
427
428 return ret;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200429}
430
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200431static int perf_header__adds_write(struct perf_header *self, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200432{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100433 int nr_sections;
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300434 struct perf_session *session;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100435 struct perf_file_section *feat_sec;
436 int sec_size;
437 u64 sec_start;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200438 int idx = 0, err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100439
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300440 session = container_of(self, struct perf_session, header);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -0200441
442 if (perf_header__has_feat(self, HEADER_BUILD_ID &&
443 !perf_session__read_build_ids(session, true)))
444 perf_header__clear_feat(self, HEADER_BUILD_ID);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100445
446 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
447 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200448 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100449
450 feat_sec = calloc(sizeof(*feat_sec), nr_sections);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200451 if (feat_sec == NULL)
452 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100453
454 sec_size = sizeof(*feat_sec) * nr_sections;
455
456 sec_start = self->data_offset + self->data_size;
Xiao Guangrongf887f302010-02-04 16:46:42 +0800457 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200458
Frederic Weisbecker3e13ab22009-11-11 04:51:06 +0100459 if (perf_header__has_feat(self, HEADER_TRACE_INFO)) {
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100460 struct perf_file_section *trace_sec;
461
462 trace_sec = &feat_sec[idx++];
463
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200464 /* Write trace info */
Xiao Guangrongf887f302010-02-04 16:46:42 +0800465 trace_sec->offset = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200466 read_tracing_data(fd, &evsel_list);
Xiao Guangrongf887f302010-02-04 16:46:42 +0800467 trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200468 }
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100469
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100470 if (perf_header__has_feat(self, HEADER_BUILD_ID)) {
471 struct perf_file_section *buildid_sec;
472
473 buildid_sec = &feat_sec[idx++];
474
475 /* Write build-ids */
Xiao Guangrongf887f302010-02-04 16:46:42 +0800476 buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800477 err = dsos__write_buildid_table(self, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200478 if (err < 0) {
479 pr_debug("failed to write buildid table\n");
480 goto out_free;
481 }
Xiao Guangrongf887f302010-02-04 16:46:42 +0800482 buildid_sec->size = lseek(fd, 0, SEEK_CUR) -
483 buildid_sec->offset;
Stephane Eraniana1ac1d32010-06-17 11:39:01 +0200484 if (!no_buildid_cache)
485 perf_session__cache_build_ids(session);
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100486 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100487
Xiao Guangrongf887f302010-02-04 16:46:42 +0800488 lseek(fd, sec_start, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200489 err = do_write(fd, feat_sec, sec_size);
490 if (err < 0)
491 pr_debug("failed to write feature section\n");
492out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100493 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200494 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100495}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200496
Tom Zanussi8dc58102010-04-01 23:59:15 -0500497int perf_header__write_pipe(int fd)
498{
499 struct perf_pipe_file_header f_header;
500 int err;
501
502 f_header = (struct perf_pipe_file_header){
503 .magic = PERF_MAGIC,
504 .size = sizeof(f_header),
505 };
506
507 err = do_write(fd, &f_header, sizeof(f_header));
508 if (err < 0) {
509 pr_debug("failed to write perf pipe header\n");
510 return err;
511 }
512
513 return 0;
514}
515
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200516int perf_header__write(struct perf_header *self, int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200517{
518 struct perf_file_header f_header;
519 struct perf_file_attr f_attr;
520 struct perf_header_attr *attr;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200521 int i, err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200522
523 lseek(fd, sizeof(f_header), SEEK_SET);
524
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200525 for (i = 0; i < self->attrs; i++) {
526 attr = self->attr[i];
527
528 attr->id_offset = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200529 err = do_write(fd, attr->id, attr->ids * sizeof(u64));
530 if (err < 0) {
531 pr_debug("failed to write perf header\n");
532 return err;
533 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200534 }
535
536
537 self->attr_offset = lseek(fd, 0, SEEK_CUR);
538
539 for (i = 0; i < self->attrs; i++) {
540 attr = self->attr[i];
541
542 f_attr = (struct perf_file_attr){
543 .attr = attr->attr,
544 .ids = {
545 .offset = attr->id_offset,
546 .size = attr->ids * sizeof(u64),
547 }
548 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200549 err = do_write(fd, &f_attr, sizeof(f_attr));
550 if (err < 0) {
551 pr_debug("failed to write perf header attribute\n");
552 return err;
553 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200554 }
555
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200556 self->event_offset = lseek(fd, 0, SEEK_CUR);
557 self->event_size = event_count * sizeof(struct perf_trace_event_type);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200558 if (events) {
559 err = do_write(fd, events, self->event_size);
560 if (err < 0) {
561 pr_debug("failed to write perf header events\n");
562 return err;
563 }
564 }
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200565
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200566 self->data_offset = lseek(fd, 0, SEEK_CUR);
567
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200568 if (at_exit) {
569 err = perf_header__adds_write(self, fd);
570 if (err < 0)
571 return err;
572 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100573
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200574 f_header = (struct perf_file_header){
575 .magic = PERF_MAGIC,
576 .size = sizeof(f_header),
577 .attr_size = sizeof(f_attr),
578 .attrs = {
579 .offset = self->attr_offset,
580 .size = self->attrs * sizeof(f_attr),
581 },
582 .data = {
583 .offset = self->data_offset,
584 .size = self->data_size,
585 },
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200586 .event_types = {
587 .offset = self->event_offset,
588 .size = self->event_size,
589 },
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200590 };
591
Frederic Weisbeckerdb9f11e2009-10-17 17:57:18 +0200592 memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200593
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200594 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200595 err = do_write(fd, &f_header, sizeof(f_header));
596 if (err < 0) {
597 pr_debug("failed to write perf header\n");
598 return err;
599 }
Xiao Guangrongf887f302010-02-04 16:46:42 +0800600 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200601
602 self->frozen = 1;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200603 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200604}
605
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200606static int perf_header__getbuffer64(struct perf_header *self,
607 int fd, void *buf, size_t size)
608{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200609 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200610 return -1;
611
612 if (self->needs_swap)
613 mem_bswap_64(buf, size);
614
615 return 0;
616}
617
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200618int perf_header__process_sections(struct perf_header *self, int fd,
619 int (*process)(struct perf_file_section *self,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200620 struct perf_header *ph,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200621 int feat, int fd))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200622{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100623 struct perf_file_section *feat_sec;
624 int nr_sections;
625 int sec_size;
626 int idx = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200627 int err = -1, feat = 1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100628
629 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
630 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200631 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100632
633 feat_sec = calloc(sizeof(*feat_sec), nr_sections);
634 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200635 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100636
637 sec_size = sizeof(*feat_sec) * nr_sections;
638
Xiao Guangrongf887f302010-02-04 16:46:42 +0800639 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100640
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200641 if (perf_header__getbuffer64(self, fd, feat_sec, sec_size))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200642 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100643
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200644 err = 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200645 while (idx < nr_sections && feat < HEADER_LAST_FEATURE) {
646 if (perf_header__has_feat(self, feat)) {
647 struct perf_file_section *sec = &feat_sec[idx++];
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100648
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200649 err = process(sec, self, feat, fd);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200650 if (err < 0)
651 break;
652 }
653 ++feat;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100654 }
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200655out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100656 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200657 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200658}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200659
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200660int perf_file_header__read(struct perf_file_header *self,
661 struct perf_header *ph, int fd)
662{
663 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200664
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200665 if (readn(fd, self, sizeof(*self)) <= 0 ||
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200666 memcmp(&self->magic, __perf_magic, sizeof(self->magic)))
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200667 return -1;
668
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200669 if (self->attr_size != sizeof(struct perf_file_attr)) {
670 u64 attr_size = bswap_64(self->attr_size);
671
672 if (attr_size != sizeof(struct perf_file_attr))
673 return -1;
674
675 mem_bswap_64(self, offsetof(struct perf_file_header,
676 adds_features));
677 ph->needs_swap = true;
678 }
679
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200680 if (self->size != sizeof(*self)) {
681 /* Support the previous format */
682 if (self->size == offsetof(typeof(*self), adds_features))
683 bitmap_zero(self->adds_features, HEADER_FEAT_BITS);
684 else
685 return -1;
686 }
687
688 memcpy(&ph->adds_features, &self->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200689 sizeof(ph->adds_features));
690 /*
691 * FIXME: hack that assumes that if we need swap the perf.data file
692 * may be coming from an arch with a different word-size, ergo different
693 * DEFINE_BITMAP format, investigate more later, but for now its mostly
694 * safe to assume that we have a build-id section. Trace files probably
695 * have several other issues in this realm anyway...
696 */
697 if (ph->needs_swap) {
698 memset(&ph->adds_features, 0, sizeof(ph->adds_features));
699 perf_header__set_feat(ph, HEADER_BUILD_ID);
700 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200701
702 ph->event_offset = self->event_types.offset;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200703 ph->event_size = self->event_types.size;
704 ph->data_offset = self->data.offset;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200705 ph->data_size = self->data.size;
706 return 0;
707}
708
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800709static int __event_process_build_id(struct build_id_event *bev,
710 char *filename,
711 struct perf_session *session)
712{
713 int err = -1;
714 struct list_head *head;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300715 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800716 u16 misc;
717 struct dso *dso;
718 enum dso_kernel_type dso_type;
719
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300720 machine = perf_session__findnew_machine(session, bev->pid);
721 if (!machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800722 goto out;
723
724 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
725
726 switch (misc) {
727 case PERF_RECORD_MISC_KERNEL:
728 dso_type = DSO_TYPE_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300729 head = &machine->kernel_dsos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800730 break;
731 case PERF_RECORD_MISC_GUEST_KERNEL:
732 dso_type = DSO_TYPE_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300733 head = &machine->kernel_dsos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800734 break;
735 case PERF_RECORD_MISC_USER:
736 case PERF_RECORD_MISC_GUEST_USER:
737 dso_type = DSO_TYPE_USER;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300738 head = &machine->user_dsos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800739 break;
740 default:
741 goto out;
742 }
743
744 dso = __dsos__findnew(head, filename);
745 if (dso != NULL) {
Tom Zanussi454c4072010-05-01 01:41:20 -0500746 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
747
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800748 dso__set_build_id(dso, &bev->build_id);
Tom Zanussi454c4072010-05-01 01:41:20 -0500749
750 if (filename[0] == '[')
751 dso->kernel = dso_type;
752
753 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
754 sbuild_id);
755 pr_debug("build id event received for %s: %s\n",
756 dso->long_name, sbuild_id);
757 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800758
759 err = 0;
760out:
761 return err;
762}
763
764static int perf_header__read_build_ids(struct perf_header *self,
765 int input, u64 offset, u64 size)
766{
767 struct perf_session *session = container_of(self,
768 struct perf_session, header);
769 struct build_id_event bev;
770 char filename[PATH_MAX];
771 u64 limit = offset + size;
772 int err = -1;
773
774 while (offset < limit) {
775 ssize_t len;
776
777 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
778 goto out;
779
780 if (self->needs_swap)
781 perf_event_header__bswap(&bev.header);
782
783 len = bev.header.size - sizeof(bev);
784 if (read(input, filename, len) != len)
785 goto out;
786
787 __event_process_build_id(&bev, filename, session);
788
789 offset += bev.header.size;
790 }
791 err = 0;
792out:
793 return err;
794}
795
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200796static int perf_file_section__process(struct perf_file_section *self,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200797 struct perf_header *ph,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200798 int feat, int fd)
799{
Xiao Guangrongf887f302010-02-04 16:46:42 +0800800 if (lseek(fd, self->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200801 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
802 "%d, continuing...\n", self->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200803 return 0;
804 }
805
806 switch (feat) {
807 case HEADER_TRACE_INFO:
Tom Zanussi454c4072010-05-01 01:41:20 -0500808 trace_report(fd, false);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200809 break;
810
811 case HEADER_BUILD_ID:
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200812 if (perf_header__read_build_ids(ph, fd, self->offset, self->size))
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200813 pr_debug("Failed to read buildids, continuing...\n");
814 break;
815 default:
816 pr_debug("unknown feature %d, continuing...\n", feat);
817 }
818
819 return 0;
820}
821
Tom Zanussi8dc58102010-04-01 23:59:15 -0500822static int perf_file_header__read_pipe(struct perf_pipe_file_header *self,
Tom Zanussi454c4072010-05-01 01:41:20 -0500823 struct perf_header *ph, int fd,
824 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200825{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200826 if (readn(fd, self, sizeof(*self)) <= 0 ||
Tom Zanussi8dc58102010-04-01 23:59:15 -0500827 memcmp(&self->magic, __perf_magic, sizeof(self->magic)))
828 return -1;
829
Tom Zanussi454c4072010-05-01 01:41:20 -0500830 if (repipe && do_write(STDOUT_FILENO, self, sizeof(*self)) < 0)
831 return -1;
832
Tom Zanussi8dc58102010-04-01 23:59:15 -0500833 if (self->size != sizeof(*self)) {
834 u64 size = bswap_64(self->size);
835
836 if (size != sizeof(*self))
837 return -1;
838
839 ph->needs_swap = true;
840 }
841
842 return 0;
843}
844
845static int perf_header__read_pipe(struct perf_session *session, int fd)
846{
847 struct perf_header *self = &session->header;
848 struct perf_pipe_file_header f_header;
849
Tom Zanussi454c4072010-05-01 01:41:20 -0500850 if (perf_file_header__read_pipe(&f_header, self, fd,
851 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -0500852 pr_debug("incompatible file format\n");
853 return -EINVAL;
854 }
855
856 session->fd = fd;
857
858 return 0;
859}
860
861int perf_header__read(struct perf_session *session, int fd)
862{
863 struct perf_header *self = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200864 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200865 struct perf_file_attr f_attr;
866 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200867 int nr_attrs, nr_ids, i, j;
868
Tom Zanussi8dc58102010-04-01 23:59:15 -0500869 if (session->fd_pipe)
870 return perf_header__read_pipe(session, fd);
871
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200872 if (perf_file_header__read(&f_header, self, fd) < 0) {
873 pr_debug("incompatible file format\n");
874 return -EINVAL;
875 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200876
877 nr_attrs = f_header.attrs.size / sizeof(f_attr);
878 lseek(fd, f_header.attrs.offset, SEEK_SET);
879
880 for (i = 0; i < nr_attrs; i++) {
881 struct perf_header_attr *attr;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200882 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200883
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200884 if (perf_header__getbuffer64(self, fd, &f_attr, sizeof(f_attr)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200885 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200886
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200887 tmp = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200888
889 attr = perf_header_attr__new(&f_attr.attr);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200890 if (attr == NULL)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200891 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200892
893 nr_ids = f_attr.ids.size / sizeof(u64);
894 lseek(fd, f_attr.ids.offset, SEEK_SET);
895
896 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200897 if (perf_header__getbuffer64(self, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200898 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200899
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200900 if (perf_header_attr__add_id(attr, f_id) < 0) {
901 perf_header_attr__delete(attr);
902 return -ENOMEM;
903 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200904 }
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200905 if (perf_header__add_attr(self, attr) < 0) {
906 perf_header_attr__delete(attr);
907 return -ENOMEM;
908 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200909
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200910 lseek(fd, tmp, SEEK_SET);
911 }
912
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200913 if (f_header.event_types.size) {
914 lseek(fd, f_header.event_types.offset, SEEK_SET);
915 events = malloc(f_header.event_types.size);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200916 if (events == NULL)
917 return -ENOMEM;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200918 if (perf_header__getbuffer64(self, fd, events,
919 f_header.event_types.size))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200920 goto out_errno;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200921 event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
922 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200923
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200924 perf_header__process_sections(self, fd, perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100925
Ingo Molnar2e01d172009-09-03 16:21:11 +0200926 lseek(fd, self->data_offset, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200927
928 self->frozen = 1;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200929 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200930out_errno:
931 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200932}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200933
934u64 perf_header__sample_type(struct perf_header *header)
935{
936 u64 type = 0;
937 int i;
938
939 for (i = 0; i < header->attrs; i++) {
940 struct perf_header_attr *attr = header->attr[i];
941
942 if (!type)
943 type = attr->attr.sample_type;
944 else if (type != attr->attr.sample_type)
945 die("non matching sample_type");
946 }
947
948 return type;
949}
950
Arnaldo Carvalho de Melo9c90a612010-12-02 10:25:28 -0200951bool perf_header__sample_id_all(const struct perf_header *header)
952{
953 bool value = false, first = true;
954 int i;
955
956 for (i = 0; i < header->attrs; i++) {
957 struct perf_header_attr *attr = header->attr[i];
958
959 if (first) {
960 value = attr->attr.sample_id_all;
961 first = false;
962 } else if (value != attr->attr.sample_id_all)
963 die("non matching sample_id_all");
964 }
965
966 return value;
967}
968
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200969struct perf_event_attr *
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200970perf_header__find_attr(u64 id, struct perf_header *header)
971{
972 int i;
973
Anton Blanchard02bf60a2010-05-04 21:19:15 +1000974 /*
975 * We set id to -1 if the data file doesn't contain sample
Franck Bui-Huu60e67732010-11-30 12:50:14 +0100976 * ids. This can happen when the data file contains one type
977 * of event and in that case, the header can still store the
978 * event attribute information. Check for this and avoid
979 * walking through the entire list of ids which may be large.
Anton Blanchard02bf60a2010-05-04 21:19:15 +1000980 */
Franck Bui-Huu60e67732010-11-30 12:50:14 +0100981 if (id == -1ULL) {
982 if (header->attrs > 0)
983 return &header->attr[0]->attr;
Anton Blanchard02bf60a2010-05-04 21:19:15 +1000984 return NULL;
Franck Bui-Huu60e67732010-11-30 12:50:14 +0100985 }
Anton Blanchard02bf60a2010-05-04 21:19:15 +1000986
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200987 for (i = 0; i < header->attrs; i++) {
988 struct perf_header_attr *attr = header->attr[i];
989 int j;
990
991 for (j = 0; j < attr->ids; j++) {
992 if (attr->id[j] == id)
993 return &attr->attr;
994 }
995 }
996
997 return NULL;
998}
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500999
1000int event__synthesize_attr(struct perf_event_attr *attr, u16 ids, u64 *id,
1001 event__handler_t process,
1002 struct perf_session *session)
1003{
1004 event_t *ev;
1005 size_t size;
1006 int err;
1007
1008 size = sizeof(struct perf_event_attr);
1009 size = ALIGN(size, sizeof(u64));
1010 size += sizeof(struct perf_event_header);
1011 size += ids * sizeof(u64);
1012
1013 ev = malloc(size);
1014
Chris Samuelce47dc52010-11-13 13:35:06 +11001015 if (ev == NULL)
1016 return -ENOMEM;
1017
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05001018 ev->attr.attr = *attr;
1019 memcpy(ev->attr.id, id, ids * sizeof(u64));
1020
1021 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
1022 ev->attr.header.size = size;
1023
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001024 err = process(ev, NULL, session);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05001025
1026 free(ev);
1027
1028 return err;
1029}
1030
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001031int event__synthesize_attrs(struct perf_header *self, event__handler_t process,
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05001032 struct perf_session *session)
1033{
1034 struct perf_header_attr *attr;
1035 int i, err = 0;
1036
1037 for (i = 0; i < self->attrs; i++) {
1038 attr = self->attr[i];
1039
1040 err = event__synthesize_attr(&attr->attr, attr->ids, attr->id,
1041 process, session);
1042 if (err) {
1043 pr_debug("failed to create perf header attribute\n");
1044 return err;
1045 }
1046 }
1047
1048 return err;
1049}
1050
1051int event__process_attr(event_t *self, struct perf_session *session)
1052{
1053 struct perf_header_attr *attr;
1054 unsigned int i, ids, n_ids;
1055
1056 attr = perf_header_attr__new(&self->attr.attr);
1057 if (attr == NULL)
1058 return -ENOMEM;
1059
1060 ids = self->header.size;
1061 ids -= (void *)&self->attr.id - (void *)self;
1062 n_ids = ids / sizeof(u64);
1063
1064 for (i = 0; i < n_ids; i++) {
1065 if (perf_header_attr__add_id(attr, self->attr.id[i]) < 0) {
1066 perf_header_attr__delete(attr);
1067 return -ENOMEM;
1068 }
1069 }
1070
1071 if (perf_header__add_attr(&session->header, attr) < 0) {
1072 perf_header_attr__delete(attr);
1073 return -ENOMEM;
1074 }
1075
1076 perf_session__update_sample_type(session);
1077
1078 return 0;
1079}
Tom Zanussicd19a032010-04-01 23:59:20 -05001080
1081int event__synthesize_event_type(u64 event_id, char *name,
1082 event__handler_t process,
1083 struct perf_session *session)
1084{
1085 event_t ev;
1086 size_t size = 0;
1087 int err = 0;
1088
1089 memset(&ev, 0, sizeof(ev));
1090
1091 ev.event_type.event_type.event_id = event_id;
1092 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
1093 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
1094
1095 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
1096 size = strlen(name);
1097 size = ALIGN(size, sizeof(u64));
1098 ev.event_type.header.size = sizeof(ev.event_type) -
1099 (sizeof(ev.event_type.event_type.name) - size);
1100
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001101 err = process(&ev, NULL, session);
Tom Zanussicd19a032010-04-01 23:59:20 -05001102
1103 return err;
1104}
1105
1106int event__synthesize_event_types(event__handler_t process,
1107 struct perf_session *session)
1108{
1109 struct perf_trace_event_type *type;
1110 int i, err = 0;
1111
1112 for (i = 0; i < event_count; i++) {
1113 type = &events[i];
1114
1115 err = event__synthesize_event_type(type->event_id, type->name,
1116 process, session);
1117 if (err) {
1118 pr_debug("failed to create perf header event type\n");
1119 return err;
1120 }
1121 }
1122
1123 return err;
1124}
1125
1126int event__process_event_type(event_t *self,
1127 struct perf_session *session __unused)
1128{
1129 if (perf_header__push_event(self->event_type.event_type.event_id,
1130 self->event_type.event_type.name) < 0)
1131 return -ENOMEM;
1132
1133 return 0;
1134}
Tom Zanussi92155452010-04-01 23:59:21 -05001135
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001136int event__synthesize_tracing_data(int fd, struct list_head *pattrs,
Tom Zanussi92155452010-04-01 23:59:21 -05001137 event__handler_t process,
1138 struct perf_session *session __unused)
1139{
1140 event_t ev;
1141 ssize_t size = 0, aligned_size = 0, padding;
1142 int err = 0;
1143
1144 memset(&ev, 0, sizeof(ev));
1145
1146 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001147 size = read_tracing_data_size(fd, pattrs);
Tom Zanussi92155452010-04-01 23:59:21 -05001148 if (size <= 0)
1149 return size;
1150 aligned_size = ALIGN(size, sizeof(u64));
1151 padding = aligned_size - size;
1152 ev.tracing_data.header.size = sizeof(ev.tracing_data);
1153 ev.tracing_data.size = aligned_size;
1154
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001155 process(&ev, NULL, session);
Tom Zanussi92155452010-04-01 23:59:21 -05001156
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001157 err = read_tracing_data(fd, pattrs);
Tom Zanussi92155452010-04-01 23:59:21 -05001158 write_padded(fd, NULL, 0, padding);
1159
1160 return aligned_size;
1161}
1162
1163int event__process_tracing_data(event_t *self,
1164 struct perf_session *session)
1165{
1166 ssize_t size_read, padding, size = self->tracing_data.size;
1167 off_t offset = lseek(session->fd, 0, SEEK_CUR);
1168 char buf[BUFSIZ];
1169
1170 /* setup for reading amidst mmap */
1171 lseek(session->fd, offset + sizeof(struct tracing_data_event),
1172 SEEK_SET);
1173
Tom Zanussi454c4072010-05-01 01:41:20 -05001174 size_read = trace_report(session->fd, session->repipe);
Tom Zanussi92155452010-04-01 23:59:21 -05001175
1176 padding = ALIGN(size_read, sizeof(u64)) - size_read;
1177
1178 if (read(session->fd, buf, padding) < 0)
1179 die("reading input file");
Tom Zanussi454c4072010-05-01 01:41:20 -05001180 if (session->repipe) {
1181 int retw = write(STDOUT_FILENO, buf, padding);
1182 if (retw <= 0 || retw != padding)
1183 die("repiping tracing data padding");
1184 }
Tom Zanussi92155452010-04-01 23:59:21 -05001185
1186 if (size_read + padding != size)
1187 die("tracing data size mismatch");
1188
1189 return size_read + padding;
1190}
Tom Zanussic7929e42010-04-01 23:59:22 -05001191
1192int event__synthesize_build_id(struct dso *pos, u16 misc,
Tom Zanussi454c4072010-05-01 01:41:20 -05001193 event__handler_t process,
1194 struct machine *machine,
Tom Zanussic7929e42010-04-01 23:59:22 -05001195 struct perf_session *session)
1196{
1197 event_t ev;
1198 size_t len;
1199 int err = 0;
1200
1201 if (!pos->hit)
1202 return err;
1203
1204 memset(&ev, 0, sizeof(ev));
1205
1206 len = pos->long_name_len + 1;
1207 len = ALIGN(len, NAME_ALIGN);
1208 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
1209 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
1210 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001211 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05001212 ev.build_id.header.size = sizeof(ev.build_id) + len;
1213 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
1214
Arnaldo Carvalho de Melo640c03c2010-12-02 14:10:21 -02001215 err = process(&ev, NULL, session);
Tom Zanussic7929e42010-04-01 23:59:22 -05001216
1217 return err;
1218}
1219
Tom Zanussic7929e42010-04-01 23:59:22 -05001220int event__process_build_id(event_t *self,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001221 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05001222{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001223 __event_process_build_id(&self->build_id,
1224 self->build_id.filename,
1225 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05001226 return 0;
1227}
Stephane Eraniana1ac1d32010-06-17 11:39:01 +02001228
1229void disable_buildid_cache(void)
1230{
1231 no_buildid_cache = true;
1232}