blob: d7e67b167ea338a28fed519260f1069dd8e92cf9 [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
155bool perf_header__has_feat(const struct perf_header *self, int feat)
156{
157 return test_bit(feat, self->adds_features);
158}
159
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200160static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200161{
162 while (size) {
163 int ret = write(fd, buf, size);
164
165 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200166 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200167
168 size -= ret;
169 buf += ret;
170 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200171
172 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200173}
174
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200175#define NAME_ALIGN 64
176
177static int write_padded(int fd, const void *bf, size_t count,
178 size_t count_aligned)
179{
180 static const char zero_buf[NAME_ALIGN];
181 int err = do_write(fd, bf, count);
182
183 if (!err)
184 err = do_write(fd, zero_buf, count_aligned - count);
185
186 return err;
187}
188
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200189#define dsos__for_each_with_build_id(pos, head) \
190 list_for_each_entry(pos, head, node) \
191 if (!pos->has_build_id) \
192 continue; \
193 else
194
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800195static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
196 u16 misc, int fd)
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100197{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200198 struct dso *pos;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100199
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200200 dsos__for_each_with_build_id(pos, head) {
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200201 int err;
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200202 struct build_id_event b;
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200203 size_t len;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100204
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200205 if (!pos->hit)
206 continue;
207 len = pos->long_name_len + 1;
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900208 len = ALIGN(len, NAME_ALIGN);
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200209 memset(&b, 0, sizeof(b));
210 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800211 b.pid = pid;
Arnaldo Carvalho de Meloa89e5ab2010-01-07 19:59:39 -0200212 b.header.misc = misc;
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200213 b.header.size = sizeof(b) + len;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200214 err = do_write(fd, &b, sizeof(b));
215 if (err < 0)
216 return err;
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200217 err = write_padded(fd, pos->long_name,
218 pos->long_name_len + 1, len);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200219 if (err < 0)
220 return err;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100221 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200222
223 return 0;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100224}
225
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300226static int machine__write_buildid_table(struct machine *self, int fd)
227{
228 int err;
229 u16 kmisc = PERF_RECORD_MISC_KERNEL,
230 umisc = PERF_RECORD_MISC_USER;
231
232 if (!machine__is_host(self)) {
233 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
234 umisc = PERF_RECORD_MISC_GUEST_USER;
235 }
236
237 err = __dsos__write_buildid_table(&self->kernel_dsos, self->pid,
238 kmisc, fd);
239 if (err == 0)
240 err = __dsos__write_buildid_table(&self->user_dsos,
241 self->pid, umisc, fd);
242 return err;
243}
244
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800245static int dsos__write_buildid_table(struct perf_header *header, int fd)
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200246{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800247 struct perf_session *session = container_of(header,
248 struct perf_session, header);
249 struct rb_node *nd;
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300250 int err = machine__write_buildid_table(&session->host_machine, fd);
251
252 if (err)
253 return err;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800254
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300255 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
256 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300257 err = machine__write_buildid_table(pos, fd);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800258 if (err)
259 break;
260 }
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200261 return err;
262}
263
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200264int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
265 const char *name, bool is_kallsyms)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200266{
267 const size_t size = PATH_MAX;
268 char *filename = malloc(size),
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200269 *linkname = malloc(size), *targetname;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200270 int len, err = -1;
271
272 if (filename == NULL || linkname == NULL)
273 goto out_free;
274
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200275 len = snprintf(filename, size, "%s%s%s",
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200276 debugdir, is_kallsyms ? "/" : "", name);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200277 if (mkdir_p(filename, 0755))
278 goto out_free;
279
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200280 snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200281
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200282 if (access(filename, F_OK)) {
283 if (is_kallsyms) {
284 if (copyfile("/proc/kallsyms", filename))
285 goto out_free;
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200286 } else if (link(name, filename) && copyfile(name, filename))
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200287 goto out_free;
288 }
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200289
290 len = snprintf(linkname, size, "%s/.build-id/%.2s",
291 debugdir, sbuild_id);
292
293 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
294 goto out_free;
295
296 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
297 targetname = filename + strlen(debugdir) - 5;
298 memcpy(targetname, "../..", 5);
299
300 if (symlink(targetname, linkname) == 0)
301 err = 0;
302out_free:
303 free(filename);
304 free(linkname);
305 return err;
306}
307
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200308static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
309 const char *name, const char *debugdir,
310 bool is_kallsyms)
311{
312 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
313
314 build_id__sprintf(build_id, build_id_size, sbuild_id);
315
316 return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms);
317}
318
319int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
320{
321 const size_t size = PATH_MAX;
322 char *filename = malloc(size),
323 *linkname = malloc(size);
324 int err = -1;
325
326 if (filename == NULL || linkname == NULL)
327 goto out_free;
328
329 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
330 debugdir, sbuild_id, sbuild_id + 2);
331
332 if (access(linkname, F_OK))
333 goto out_free;
334
335 if (readlink(linkname, filename, size) < 0)
336 goto out_free;
337
338 if (unlink(linkname))
339 goto out_free;
340
341 /*
342 * Since the link is relative, we must make it absolute:
343 */
344 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
345 debugdir, sbuild_id, filename);
346
347 if (unlink(linkname))
348 goto out_free;
349
350 err = 0;
351out_free:
352 free(filename);
353 free(linkname);
354 return err;
355}
356
357static int dso__cache_build_id(struct dso *self, const char *debugdir)
358{
359 bool is_kallsyms = self->kernel && self->long_name[0] != '/';
360
361 return build_id_cache__add_b(self->build_id, sizeof(self->build_id),
362 self->long_name, debugdir, is_kallsyms);
363}
364
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200365static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
366{
367 struct dso *pos;
368 int err = 0;
369
370 dsos__for_each_with_build_id(pos, head)
371 if (dso__cache_build_id(pos, debugdir))
372 err = -1;
373
374 return err;
375}
376
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300377static int machine__cache_build_ids(struct machine *self, const char *debugdir)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200378{
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300379 int ret = __dsos__cache_build_ids(&self->kernel_dsos, debugdir);
380 ret |= __dsos__cache_build_ids(&self->user_dsos, debugdir);
381 return ret;
382}
383
384static int perf_session__cache_build_ids(struct perf_session *self)
385{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800386 struct rb_node *nd;
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300387 int ret;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200388 char debugdir[PATH_MAX];
389
Stephane Eranian45de34b2010-06-01 21:25:01 +0200390 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200391
392 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
393 return -1;
394
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300395 ret = machine__cache_build_ids(&self->host_machine, debugdir);
396
397 for (nd = rb_first(&self->machines); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300398 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300399 ret |= machine__cache_build_ids(pos, debugdir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800400 }
401 return ret ? -1 : 0;
402}
403
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300404static bool machine__read_build_ids(struct machine *self, bool with_hits)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800405{
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300406 bool ret = __dsos__read_build_ids(&self->kernel_dsos, with_hits);
407 ret |= __dsos__read_build_ids(&self->user_dsos, with_hits);
408 return ret;
409}
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800410
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300411static bool perf_session__read_build_ids(struct perf_session *self, bool with_hits)
412{
413 struct rb_node *nd;
414 bool ret = machine__read_build_ids(&self->host_machine, with_hits);
415
416 for (nd = rb_first(&self->machines); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300417 struct machine *pos = rb_entry(nd, struct machine, rb_node);
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300418 ret |= machine__read_build_ids(pos, with_hits);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800419 }
420
421 return ret;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200422}
423
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200424static int perf_header__adds_write(struct perf_header *self, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200425{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100426 int nr_sections;
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300427 struct perf_session *session;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100428 struct perf_file_section *feat_sec;
429 int sec_size;
430 u64 sec_start;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200431 int idx = 0, err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100432
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -0300433 session = container_of(self, struct perf_session, header);
434 if (perf_session__read_build_ids(session, true))
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100435 perf_header__set_feat(self, HEADER_BUILD_ID);
436
437 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
438 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200439 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100440
441 feat_sec = calloc(sizeof(*feat_sec), nr_sections);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200442 if (feat_sec == NULL)
443 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100444
445 sec_size = sizeof(*feat_sec) * nr_sections;
446
447 sec_start = self->data_offset + self->data_size;
Xiao Guangrongf887f302010-02-04 16:46:42 +0800448 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200449
Frederic Weisbecker3e13ab22009-11-11 04:51:06 +0100450 if (perf_header__has_feat(self, HEADER_TRACE_INFO)) {
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100451 struct perf_file_section *trace_sec;
452
453 trace_sec = &feat_sec[idx++];
454
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200455 /* Write trace info */
Xiao Guangrongf887f302010-02-04 16:46:42 +0800456 trace_sec->offset = lseek(fd, 0, SEEK_CUR);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200457 read_tracing_data(fd, attrs, nr_counters);
Xiao Guangrongf887f302010-02-04 16:46:42 +0800458 trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200459 }
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100460
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100461 if (perf_header__has_feat(self, HEADER_BUILD_ID)) {
462 struct perf_file_section *buildid_sec;
463
464 buildid_sec = &feat_sec[idx++];
465
466 /* Write build-ids */
Xiao Guangrongf887f302010-02-04 16:46:42 +0800467 buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800468 err = dsos__write_buildid_table(self, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200469 if (err < 0) {
470 pr_debug("failed to write buildid table\n");
471 goto out_free;
472 }
Xiao Guangrongf887f302010-02-04 16:46:42 +0800473 buildid_sec->size = lseek(fd, 0, SEEK_CUR) -
474 buildid_sec->offset;
Stephane Eraniana1ac1d32010-06-17 11:39:01 +0200475 if (!no_buildid_cache)
476 perf_session__cache_build_ids(session);
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100477 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100478
Xiao Guangrongf887f302010-02-04 16:46:42 +0800479 lseek(fd, sec_start, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200480 err = do_write(fd, feat_sec, sec_size);
481 if (err < 0)
482 pr_debug("failed to write feature section\n");
483out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100484 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200485 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100486}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200487
Tom Zanussi8dc58102010-04-01 23:59:15 -0500488int perf_header__write_pipe(int fd)
489{
490 struct perf_pipe_file_header f_header;
491 int err;
492
493 f_header = (struct perf_pipe_file_header){
494 .magic = PERF_MAGIC,
495 .size = sizeof(f_header),
496 };
497
498 err = do_write(fd, &f_header, sizeof(f_header));
499 if (err < 0) {
500 pr_debug("failed to write perf pipe header\n");
501 return err;
502 }
503
504 return 0;
505}
506
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200507int perf_header__write(struct perf_header *self, int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200508{
509 struct perf_file_header f_header;
510 struct perf_file_attr f_attr;
511 struct perf_header_attr *attr;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200512 int i, err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200513
514 lseek(fd, sizeof(f_header), SEEK_SET);
515
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200516 for (i = 0; i < self->attrs; i++) {
517 attr = self->attr[i];
518
519 attr->id_offset = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200520 err = do_write(fd, attr->id, attr->ids * sizeof(u64));
521 if (err < 0) {
522 pr_debug("failed to write perf header\n");
523 return err;
524 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200525 }
526
527
528 self->attr_offset = lseek(fd, 0, SEEK_CUR);
529
530 for (i = 0; i < self->attrs; i++) {
531 attr = self->attr[i];
532
533 f_attr = (struct perf_file_attr){
534 .attr = attr->attr,
535 .ids = {
536 .offset = attr->id_offset,
537 .size = attr->ids * sizeof(u64),
538 }
539 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200540 err = do_write(fd, &f_attr, sizeof(f_attr));
541 if (err < 0) {
542 pr_debug("failed to write perf header attribute\n");
543 return err;
544 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200545 }
546
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200547 self->event_offset = lseek(fd, 0, SEEK_CUR);
548 self->event_size = event_count * sizeof(struct perf_trace_event_type);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200549 if (events) {
550 err = do_write(fd, events, self->event_size);
551 if (err < 0) {
552 pr_debug("failed to write perf header events\n");
553 return err;
554 }
555 }
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200556
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200557 self->data_offset = lseek(fd, 0, SEEK_CUR);
558
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200559 if (at_exit) {
560 err = perf_header__adds_write(self, fd);
561 if (err < 0)
562 return err;
563 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100564
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200565 f_header = (struct perf_file_header){
566 .magic = PERF_MAGIC,
567 .size = sizeof(f_header),
568 .attr_size = sizeof(f_attr),
569 .attrs = {
570 .offset = self->attr_offset,
571 .size = self->attrs * sizeof(f_attr),
572 },
573 .data = {
574 .offset = self->data_offset,
575 .size = self->data_size,
576 },
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200577 .event_types = {
578 .offset = self->event_offset,
579 .size = self->event_size,
580 },
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200581 };
582
Frederic Weisbeckerdb9f11e2009-10-17 17:57:18 +0200583 memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200584
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200585 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200586 err = do_write(fd, &f_header, sizeof(f_header));
587 if (err < 0) {
588 pr_debug("failed to write perf header\n");
589 return err;
590 }
Xiao Guangrongf887f302010-02-04 16:46:42 +0800591 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200592
593 self->frozen = 1;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200594 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200595}
596
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200597static int perf_header__getbuffer64(struct perf_header *self,
598 int fd, void *buf, size_t size)
599{
Tom Zanussi8dc58102010-04-01 23:59:15 -0500600 if (do_read(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200601 return -1;
602
603 if (self->needs_swap)
604 mem_bswap_64(buf, size);
605
606 return 0;
607}
608
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200609int perf_header__process_sections(struct perf_header *self, int fd,
610 int (*process)(struct perf_file_section *self,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200611 struct perf_header *ph,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200612 int feat, int fd))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200613{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100614 struct perf_file_section *feat_sec;
615 int nr_sections;
616 int sec_size;
617 int idx = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200618 int err = -1, feat = 1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100619
620 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
621 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200622 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100623
624 feat_sec = calloc(sizeof(*feat_sec), nr_sections);
625 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200626 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100627
628 sec_size = sizeof(*feat_sec) * nr_sections;
629
Xiao Guangrongf887f302010-02-04 16:46:42 +0800630 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100631
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200632 if (perf_header__getbuffer64(self, fd, feat_sec, sec_size))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200633 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100634
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200635 err = 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200636 while (idx < nr_sections && feat < HEADER_LAST_FEATURE) {
637 if (perf_header__has_feat(self, feat)) {
638 struct perf_file_section *sec = &feat_sec[idx++];
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100639
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200640 err = process(sec, self, feat, fd);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200641 if (err < 0)
642 break;
643 }
644 ++feat;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100645 }
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200646out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100647 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200648 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200649}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200650
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200651int perf_file_header__read(struct perf_file_header *self,
652 struct perf_header *ph, int fd)
653{
654 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200655
Tom Zanussi8dc58102010-04-01 23:59:15 -0500656 if (do_read(fd, self, sizeof(*self)) <= 0 ||
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200657 memcmp(&self->magic, __perf_magic, sizeof(self->magic)))
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200658 return -1;
659
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200660 if (self->attr_size != sizeof(struct perf_file_attr)) {
661 u64 attr_size = bswap_64(self->attr_size);
662
663 if (attr_size != sizeof(struct perf_file_attr))
664 return -1;
665
666 mem_bswap_64(self, offsetof(struct perf_file_header,
667 adds_features));
668 ph->needs_swap = true;
669 }
670
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200671 if (self->size != sizeof(*self)) {
672 /* Support the previous format */
673 if (self->size == offsetof(typeof(*self), adds_features))
674 bitmap_zero(self->adds_features, HEADER_FEAT_BITS);
675 else
676 return -1;
677 }
678
679 memcpy(&ph->adds_features, &self->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200680 sizeof(ph->adds_features));
681 /*
682 * FIXME: hack that assumes that if we need swap the perf.data file
683 * may be coming from an arch with a different word-size, ergo different
684 * DEFINE_BITMAP format, investigate more later, but for now its mostly
685 * safe to assume that we have a build-id section. Trace files probably
686 * have several other issues in this realm anyway...
687 */
688 if (ph->needs_swap) {
689 memset(&ph->adds_features, 0, sizeof(ph->adds_features));
690 perf_header__set_feat(ph, HEADER_BUILD_ID);
691 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200692
693 ph->event_offset = self->event_types.offset;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200694 ph->event_size = self->event_types.size;
695 ph->data_offset = self->data.offset;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200696 ph->data_size = self->data.size;
697 return 0;
698}
699
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800700static int __event_process_build_id(struct build_id_event *bev,
701 char *filename,
702 struct perf_session *session)
703{
704 int err = -1;
705 struct list_head *head;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300706 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800707 u16 misc;
708 struct dso *dso;
709 enum dso_kernel_type dso_type;
710
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300711 machine = perf_session__findnew_machine(session, bev->pid);
712 if (!machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800713 goto out;
714
715 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
716
717 switch (misc) {
718 case PERF_RECORD_MISC_KERNEL:
719 dso_type = DSO_TYPE_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300720 head = &machine->kernel_dsos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800721 break;
722 case PERF_RECORD_MISC_GUEST_KERNEL:
723 dso_type = DSO_TYPE_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300724 head = &machine->kernel_dsos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800725 break;
726 case PERF_RECORD_MISC_USER:
727 case PERF_RECORD_MISC_GUEST_USER:
728 dso_type = DSO_TYPE_USER;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300729 head = &machine->user_dsos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800730 break;
731 default:
732 goto out;
733 }
734
735 dso = __dsos__findnew(head, filename);
736 if (dso != NULL) {
Tom Zanussi454c4072010-05-01 01:41:20 -0500737 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
738
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800739 dso__set_build_id(dso, &bev->build_id);
Tom Zanussi454c4072010-05-01 01:41:20 -0500740
741 if (filename[0] == '[')
742 dso->kernel = dso_type;
743
744 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
745 sbuild_id);
746 pr_debug("build id event received for %s: %s\n",
747 dso->long_name, sbuild_id);
748 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800749
750 err = 0;
751out:
752 return err;
753}
754
755static int perf_header__read_build_ids(struct perf_header *self,
756 int input, u64 offset, u64 size)
757{
758 struct perf_session *session = container_of(self,
759 struct perf_session, header);
760 struct build_id_event bev;
761 char filename[PATH_MAX];
762 u64 limit = offset + size;
763 int err = -1;
764
765 while (offset < limit) {
766 ssize_t len;
767
768 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
769 goto out;
770
771 if (self->needs_swap)
772 perf_event_header__bswap(&bev.header);
773
774 len = bev.header.size - sizeof(bev);
775 if (read(input, filename, len) != len)
776 goto out;
777
778 __event_process_build_id(&bev, filename, session);
779
780 offset += bev.header.size;
781 }
782 err = 0;
783out:
784 return err;
785}
786
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200787static int perf_file_section__process(struct perf_file_section *self,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200788 struct perf_header *ph,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200789 int feat, int fd)
790{
Xiao Guangrongf887f302010-02-04 16:46:42 +0800791 if (lseek(fd, self->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200792 pr_debug("Failed to lseek to %Ld offset for feature %d, "
793 "continuing...\n", self->offset, feat);
794 return 0;
795 }
796
797 switch (feat) {
798 case HEADER_TRACE_INFO:
Tom Zanussi454c4072010-05-01 01:41:20 -0500799 trace_report(fd, false);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200800 break;
801
802 case HEADER_BUILD_ID:
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200803 if (perf_header__read_build_ids(ph, fd, self->offset, self->size))
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200804 pr_debug("Failed to read buildids, continuing...\n");
805 break;
806 default:
807 pr_debug("unknown feature %d, continuing...\n", feat);
808 }
809
810 return 0;
811}
812
Tom Zanussi8dc58102010-04-01 23:59:15 -0500813static int perf_file_header__read_pipe(struct perf_pipe_file_header *self,
Tom Zanussi454c4072010-05-01 01:41:20 -0500814 struct perf_header *ph, int fd,
815 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200816{
Tom Zanussi8dc58102010-04-01 23:59:15 -0500817 if (do_read(fd, self, sizeof(*self)) <= 0 ||
818 memcmp(&self->magic, __perf_magic, sizeof(self->magic)))
819 return -1;
820
Tom Zanussi454c4072010-05-01 01:41:20 -0500821 if (repipe && do_write(STDOUT_FILENO, self, sizeof(*self)) < 0)
822 return -1;
823
Tom Zanussi8dc58102010-04-01 23:59:15 -0500824 if (self->size != sizeof(*self)) {
825 u64 size = bswap_64(self->size);
826
827 if (size != sizeof(*self))
828 return -1;
829
830 ph->needs_swap = true;
831 }
832
833 return 0;
834}
835
836static int perf_header__read_pipe(struct perf_session *session, int fd)
837{
838 struct perf_header *self = &session->header;
839 struct perf_pipe_file_header f_header;
840
Tom Zanussi454c4072010-05-01 01:41:20 -0500841 if (perf_file_header__read_pipe(&f_header, self, fd,
842 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -0500843 pr_debug("incompatible file format\n");
844 return -EINVAL;
845 }
846
847 session->fd = fd;
848
849 return 0;
850}
851
852int perf_header__read(struct perf_session *session, int fd)
853{
854 struct perf_header *self = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200855 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200856 struct perf_file_attr f_attr;
857 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200858 int nr_attrs, nr_ids, i, j;
859
Tom Zanussi8dc58102010-04-01 23:59:15 -0500860 if (session->fd_pipe)
861 return perf_header__read_pipe(session, fd);
862
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200863 if (perf_file_header__read(&f_header, self, fd) < 0) {
864 pr_debug("incompatible file format\n");
865 return -EINVAL;
866 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200867
868 nr_attrs = f_header.attrs.size / sizeof(f_attr);
869 lseek(fd, f_header.attrs.offset, SEEK_SET);
870
871 for (i = 0; i < nr_attrs; i++) {
872 struct perf_header_attr *attr;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200873 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200874
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200875 if (perf_header__getbuffer64(self, fd, &f_attr, sizeof(f_attr)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200876 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200877
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200878 tmp = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200879
880 attr = perf_header_attr__new(&f_attr.attr);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200881 if (attr == NULL)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200882 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200883
884 nr_ids = f_attr.ids.size / sizeof(u64);
885 lseek(fd, f_attr.ids.offset, SEEK_SET);
886
887 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200888 if (perf_header__getbuffer64(self, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200889 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200890
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200891 if (perf_header_attr__add_id(attr, f_id) < 0) {
892 perf_header_attr__delete(attr);
893 return -ENOMEM;
894 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200895 }
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200896 if (perf_header__add_attr(self, attr) < 0) {
897 perf_header_attr__delete(attr);
898 return -ENOMEM;
899 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200900
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200901 lseek(fd, tmp, SEEK_SET);
902 }
903
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200904 if (f_header.event_types.size) {
905 lseek(fd, f_header.event_types.offset, SEEK_SET);
906 events = malloc(f_header.event_types.size);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200907 if (events == NULL)
908 return -ENOMEM;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200909 if (perf_header__getbuffer64(self, fd, events,
910 f_header.event_types.size))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200911 goto out_errno;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200912 event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
913 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200914
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200915 perf_header__process_sections(self, fd, perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100916
Ingo Molnar2e01d172009-09-03 16:21:11 +0200917 lseek(fd, self->data_offset, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200918
919 self->frozen = 1;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200920 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200921out_errno:
922 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200923}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200924
925u64 perf_header__sample_type(struct perf_header *header)
926{
927 u64 type = 0;
928 int i;
929
930 for (i = 0; i < header->attrs; i++) {
931 struct perf_header_attr *attr = header->attr[i];
932
933 if (!type)
934 type = attr->attr.sample_type;
935 else if (type != attr->attr.sample_type)
936 die("non matching sample_type");
937 }
938
939 return type;
940}
941
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200942struct perf_event_attr *
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200943perf_header__find_attr(u64 id, struct perf_header *header)
944{
945 int i;
946
Anton Blanchard02bf60a2010-05-04 21:19:15 +1000947 /*
948 * We set id to -1 if the data file doesn't contain sample
949 * ids. Check for this and avoid walking through the entire
950 * list of ids which may be large.
951 */
952 if (id == -1ULL)
953 return NULL;
954
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200955 for (i = 0; i < header->attrs; i++) {
956 struct perf_header_attr *attr = header->attr[i];
957 int j;
958
959 for (j = 0; j < attr->ids; j++) {
960 if (attr->id[j] == id)
961 return &attr->attr;
962 }
963 }
964
965 return NULL;
966}
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500967
968int event__synthesize_attr(struct perf_event_attr *attr, u16 ids, u64 *id,
969 event__handler_t process,
970 struct perf_session *session)
971{
972 event_t *ev;
973 size_t size;
974 int err;
975
976 size = sizeof(struct perf_event_attr);
977 size = ALIGN(size, sizeof(u64));
978 size += sizeof(struct perf_event_header);
979 size += ids * sizeof(u64);
980
981 ev = malloc(size);
982
983 ev->attr.attr = *attr;
984 memcpy(ev->attr.id, id, ids * sizeof(u64));
985
986 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
987 ev->attr.header.size = size;
988
989 err = process(ev, session);
990
991 free(ev);
992
993 return err;
994}
995
996int event__synthesize_attrs(struct perf_header *self,
997 event__handler_t process,
998 struct perf_session *session)
999{
1000 struct perf_header_attr *attr;
1001 int i, err = 0;
1002
1003 for (i = 0; i < self->attrs; i++) {
1004 attr = self->attr[i];
1005
1006 err = event__synthesize_attr(&attr->attr, attr->ids, attr->id,
1007 process, session);
1008 if (err) {
1009 pr_debug("failed to create perf header attribute\n");
1010 return err;
1011 }
1012 }
1013
1014 return err;
1015}
1016
1017int event__process_attr(event_t *self, struct perf_session *session)
1018{
1019 struct perf_header_attr *attr;
1020 unsigned int i, ids, n_ids;
1021
1022 attr = perf_header_attr__new(&self->attr.attr);
1023 if (attr == NULL)
1024 return -ENOMEM;
1025
1026 ids = self->header.size;
1027 ids -= (void *)&self->attr.id - (void *)self;
1028 n_ids = ids / sizeof(u64);
1029
1030 for (i = 0; i < n_ids; i++) {
1031 if (perf_header_attr__add_id(attr, self->attr.id[i]) < 0) {
1032 perf_header_attr__delete(attr);
1033 return -ENOMEM;
1034 }
1035 }
1036
1037 if (perf_header__add_attr(&session->header, attr) < 0) {
1038 perf_header_attr__delete(attr);
1039 return -ENOMEM;
1040 }
1041
1042 perf_session__update_sample_type(session);
1043
1044 return 0;
1045}
Tom Zanussicd19a032010-04-01 23:59:20 -05001046
1047int event__synthesize_event_type(u64 event_id, char *name,
1048 event__handler_t process,
1049 struct perf_session *session)
1050{
1051 event_t ev;
1052 size_t size = 0;
1053 int err = 0;
1054
1055 memset(&ev, 0, sizeof(ev));
1056
1057 ev.event_type.event_type.event_id = event_id;
1058 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
1059 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
1060
1061 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
1062 size = strlen(name);
1063 size = ALIGN(size, sizeof(u64));
1064 ev.event_type.header.size = sizeof(ev.event_type) -
1065 (sizeof(ev.event_type.event_type.name) - size);
1066
1067 err = process(&ev, session);
1068
1069 return err;
1070}
1071
1072int event__synthesize_event_types(event__handler_t process,
1073 struct perf_session *session)
1074{
1075 struct perf_trace_event_type *type;
1076 int i, err = 0;
1077
1078 for (i = 0; i < event_count; i++) {
1079 type = &events[i];
1080
1081 err = event__synthesize_event_type(type->event_id, type->name,
1082 process, session);
1083 if (err) {
1084 pr_debug("failed to create perf header event type\n");
1085 return err;
1086 }
1087 }
1088
1089 return err;
1090}
1091
1092int event__process_event_type(event_t *self,
1093 struct perf_session *session __unused)
1094{
1095 if (perf_header__push_event(self->event_type.event_type.event_id,
1096 self->event_type.event_type.name) < 0)
1097 return -ENOMEM;
1098
1099 return 0;
1100}
Tom Zanussi92155452010-04-01 23:59:21 -05001101
1102int event__synthesize_tracing_data(int fd, struct perf_event_attr *pattrs,
1103 int nb_events,
1104 event__handler_t process,
1105 struct perf_session *session __unused)
1106{
1107 event_t ev;
1108 ssize_t size = 0, aligned_size = 0, padding;
1109 int err = 0;
1110
1111 memset(&ev, 0, sizeof(ev));
1112
1113 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
1114 size = read_tracing_data_size(fd, pattrs, nb_events);
1115 if (size <= 0)
1116 return size;
1117 aligned_size = ALIGN(size, sizeof(u64));
1118 padding = aligned_size - size;
1119 ev.tracing_data.header.size = sizeof(ev.tracing_data);
1120 ev.tracing_data.size = aligned_size;
1121
1122 process(&ev, session);
1123
1124 err = read_tracing_data(fd, pattrs, nb_events);
1125 write_padded(fd, NULL, 0, padding);
1126
1127 return aligned_size;
1128}
1129
1130int event__process_tracing_data(event_t *self,
1131 struct perf_session *session)
1132{
1133 ssize_t size_read, padding, size = self->tracing_data.size;
1134 off_t offset = lseek(session->fd, 0, SEEK_CUR);
1135 char buf[BUFSIZ];
1136
1137 /* setup for reading amidst mmap */
1138 lseek(session->fd, offset + sizeof(struct tracing_data_event),
1139 SEEK_SET);
1140
Tom Zanussi454c4072010-05-01 01:41:20 -05001141 size_read = trace_report(session->fd, session->repipe);
Tom Zanussi92155452010-04-01 23:59:21 -05001142
1143 padding = ALIGN(size_read, sizeof(u64)) - size_read;
1144
1145 if (read(session->fd, buf, padding) < 0)
1146 die("reading input file");
Tom Zanussi454c4072010-05-01 01:41:20 -05001147 if (session->repipe) {
1148 int retw = write(STDOUT_FILENO, buf, padding);
1149 if (retw <= 0 || retw != padding)
1150 die("repiping tracing data padding");
1151 }
Tom Zanussi92155452010-04-01 23:59:21 -05001152
1153 if (size_read + padding != size)
1154 die("tracing data size mismatch");
1155
1156 return size_read + padding;
1157}
Tom Zanussic7929e42010-04-01 23:59:22 -05001158
1159int event__synthesize_build_id(struct dso *pos, u16 misc,
Tom Zanussi454c4072010-05-01 01:41:20 -05001160 event__handler_t process,
1161 struct machine *machine,
Tom Zanussic7929e42010-04-01 23:59:22 -05001162 struct perf_session *session)
1163{
1164 event_t ev;
1165 size_t len;
1166 int err = 0;
1167
1168 if (!pos->hit)
1169 return err;
1170
1171 memset(&ev, 0, sizeof(ev));
1172
1173 len = pos->long_name_len + 1;
1174 len = ALIGN(len, NAME_ALIGN);
1175 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
1176 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
1177 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001178 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05001179 ev.build_id.header.size = sizeof(ev.build_id) + len;
1180 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
1181
1182 err = process(&ev, session);
1183
1184 return err;
1185}
1186
Tom Zanussic7929e42010-04-01 23:59:22 -05001187int event__process_build_id(event_t *self,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001188 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05001189{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001190 __event_process_build_id(&self->build_id,
1191 self->build_id.filename,
1192 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05001193 return 0;
1194}
Stephane Eraniana1ac1d32010-06-17 11:39:01 +02001195
1196void disable_buildid_cache(void)
1197{
1198 no_buildid_cache = true;
1199}