blob: 6227dc4cb2cfde74086f935515a180cb72ecc6e3 [file] [log] [blame]
Xiao Guangrongb8f46c52010-02-03 11:53:14 +08001#define _FILE_OFFSET_BITS 64
2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02004#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02005#include <unistd.h>
6#include <stdio.h>
7#include <stdlib.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +01008#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02009#include <linux/kernel.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020010
11#include "util.h"
12#include "header.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020013#include "../perf.h"
14#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020015#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010016#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010017#include "debug.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020018
19/*
Arjan van de Venec60a3f2009-09-19 13:36:30 +020020 * Create new perf.data header attribute:
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020021 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020022struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020023{
24 struct perf_header_attr *self = malloc(sizeof(*self));
25
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -020026 if (self != NULL) {
27 self->attr = *attr;
28 self->ids = 0;
29 self->size = 1;
30 self->id = malloc(sizeof(u64));
31 if (self->id == NULL) {
32 free(self);
33 self = NULL;
34 }
35 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020036
37 return self;
38}
39
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020040void perf_header_attr__delete(struct perf_header_attr *self)
41{
42 free(self->id);
43 free(self);
44}
45
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -020046int perf_header_attr__add_id(struct perf_header_attr *self, u64 id)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020047{
48 int pos = self->ids;
49
50 self->ids++;
51 if (self->ids > self->size) {
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -020052 int nsize = self->size * 2;
53 u64 *nid = realloc(self->id, nsize * sizeof(u64));
54
55 if (nid == NULL)
56 return -1;
57
58 self->size = nsize;
59 self->id = nid;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020060 }
61 self->id[pos] = id;
Arnaldo Carvalho de Melo58754122009-11-17 01:18:10 -020062 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020063}
64
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020065int perf_header__init(struct perf_header *self)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020066{
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020067 self->size = 1;
68 self->attr = malloc(sizeof(void *));
69 return self->attr == NULL ? -ENOMEM : 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020070}
71
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020072void perf_header__exit(struct perf_header *self)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020073{
74 int i;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020075 for (i = 0; i < self->attrs; ++i)
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020076 perf_header_attr__delete(self->attr[i]);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020077 free(self->attr);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020078}
79
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020080int perf_header__add_attr(struct perf_header *self,
81 struct perf_header_attr *attr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020082{
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020083 if (self->frozen)
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020084 return -1;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020085
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020086 if (self->attrs == self->size) {
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020087 int nsize = self->size * 2;
88 struct perf_header_attr **nattr;
89
90 nattr = realloc(self->attr, nsize * sizeof(void *));
91 if (nattr == NULL)
92 return -1;
93
94 self->size = nsize;
95 self->attr = nattr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020096 }
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -020097
98 self->attr[self->attrs++] = attr;
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -020099 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200100}
101
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200102static int event_count;
103static struct perf_trace_event_type *events;
104
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -0200105int perf_header__push_event(u64 id, const char *name)
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200106{
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200107 if (strlen(name) > MAX_EVENT_NAME)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200108 pr_warning("Event %s will be truncated\n", name);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200109
110 if (!events) {
111 events = malloc(sizeof(struct perf_trace_event_type));
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -0200112 if (events == NULL)
113 return -ENOMEM;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200114 } else {
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -0200115 struct perf_trace_event_type *nevents;
116
117 nevents = realloc(events, (event_count + 1) * sizeof(*events));
118 if (nevents == NULL)
119 return -ENOMEM;
120 events = nevents;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200121 }
122 memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
123 events[event_count].event_id = id;
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200124 strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200125 event_count++;
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -0200126 return 0;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200127}
128
129char *perf_header__find_event(u64 id)
130{
131 int i;
132 for (i = 0 ; i < event_count; i++) {
133 if (events[i].event_id == id)
134 return events[i].name;
135 }
136 return NULL;
137}
138
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200139static const char *__perf_magic = "PERFFILE";
140
141#define PERF_MAGIC (*(u64 *)__perf_magic)
142
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200143struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200144 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200145 struct perf_file_section ids;
146};
147
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200148void perf_header__set_feat(struct perf_header *self, int feat)
149{
150 set_bit(feat, self->adds_features);
151}
152
153bool perf_header__has_feat(const struct perf_header *self, int feat)
154{
155 return test_bit(feat, self->adds_features);
156}
157
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200158static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200159{
160 while (size) {
161 int ret = write(fd, buf, size);
162
163 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200164 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200165
166 size -= ret;
167 buf += ret;
168 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200169
170 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200171}
172
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200173#define NAME_ALIGN 64
174
175static int write_padded(int fd, const void *bf, size_t count,
176 size_t count_aligned)
177{
178 static const char zero_buf[NAME_ALIGN];
179 int err = do_write(fd, bf, count);
180
181 if (!err)
182 err = do_write(fd, zero_buf, count_aligned - count);
183
184 return err;
185}
186
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200187#define dsos__for_each_with_build_id(pos, head) \
188 list_for_each_entry(pos, head, node) \
189 if (!pos->has_build_id) \
190 continue; \
191 else
192
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800193static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
194 u16 misc, int fd)
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100195{
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200196 struct dso *pos;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100197
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200198 dsos__for_each_with_build_id(pos, head) {
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200199 int err;
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200200 struct build_id_event b;
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200201 size_t len;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100202
Arnaldo Carvalho de Melo6122e4e2010-02-03 16:52:05 -0200203 if (!pos->hit)
204 continue;
205 len = pos->long_name_len + 1;
OGAWA Hirofumi7691b1e2009-12-06 20:10:49 +0900206 len = ALIGN(len, NAME_ALIGN);
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200207 memset(&b, 0, sizeof(b));
208 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800209 b.pid = pid;
Arnaldo Carvalho de Meloa89e5abe2010-01-07 19:59:39 -0200210 b.header.misc = misc;
Arnaldo Carvalho de Meloe30a3d12009-11-18 20:20:51 -0200211 b.header.size = sizeof(b) + len;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200212 err = do_write(fd, &b, sizeof(b));
213 if (err < 0)
214 return err;
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200215 err = write_padded(fd, pos->long_name,
216 pos->long_name_len + 1, len);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200217 if (err < 0)
218 return err;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100219 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200220
221 return 0;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100222}
223
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800224static int dsos__write_buildid_table(struct perf_header *header, int fd)
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200225{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800226 struct perf_session *session = container_of(header,
227 struct perf_session, header);
228 struct rb_node *nd;
229 int err = 0;
230 u16 kmisc, umisc;
231
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300232 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
233 struct machine *pos = rb_entry(nd, struct machine, rb_node);
234 if (machine__is_host(pos)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800235 kmisc = PERF_RECORD_MISC_KERNEL;
236 umisc = PERF_RECORD_MISC_USER;
237 } else {
238 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
239 umisc = PERF_RECORD_MISC_GUEST_USER;
240 }
241
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300242 err = __dsos__write_buildid_table(&pos->kernel_dsos, pos->pid,
243 kmisc, fd);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800244 if (err == 0)
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300245 err = __dsos__write_buildid_table(&pos->user_dsos,
246 pos->pid, umisc, fd);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800247 if (err)
248 break;
249 }
Arnaldo Carvalho de Melob0da9542009-11-27 16:29:14 -0200250 return err;
251}
252
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200253int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
254 const char *name, bool is_kallsyms)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200255{
256 const size_t size = PATH_MAX;
257 char *filename = malloc(size),
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200258 *linkname = malloc(size), *targetname;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200259 int len, err = -1;
260
261 if (filename == NULL || linkname == NULL)
262 goto out_free;
263
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200264 len = snprintf(filename, size, "%s%s%s",
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200265 debugdir, is_kallsyms ? "/" : "", name);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200266 if (mkdir_p(filename, 0755))
267 goto out_free;
268
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200269 snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200270
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200271 if (access(filename, F_OK)) {
272 if (is_kallsyms) {
273 if (copyfile("/proc/kallsyms", filename))
274 goto out_free;
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200275 } else if (link(name, filename) && copyfile(name, filename))
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200276 goto out_free;
277 }
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200278
279 len = snprintf(linkname, size, "%s/.build-id/%.2s",
280 debugdir, sbuild_id);
281
282 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
283 goto out_free;
284
285 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
286 targetname = filename + strlen(debugdir) - 5;
287 memcpy(targetname, "../..", 5);
288
289 if (symlink(targetname, linkname) == 0)
290 err = 0;
291out_free:
292 free(filename);
293 free(linkname);
294 return err;
295}
296
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200297static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
298 const char *name, const char *debugdir,
299 bool is_kallsyms)
300{
301 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
302
303 build_id__sprintf(build_id, build_id_size, sbuild_id);
304
305 return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms);
306}
307
308int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
309{
310 const size_t size = PATH_MAX;
311 char *filename = malloc(size),
312 *linkname = malloc(size);
313 int err = -1;
314
315 if (filename == NULL || linkname == NULL)
316 goto out_free;
317
318 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
319 debugdir, sbuild_id, sbuild_id + 2);
320
321 if (access(linkname, F_OK))
322 goto out_free;
323
324 if (readlink(linkname, filename, size) < 0)
325 goto out_free;
326
327 if (unlink(linkname))
328 goto out_free;
329
330 /*
331 * Since the link is relative, we must make it absolute:
332 */
333 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
334 debugdir, sbuild_id, filename);
335
336 if (unlink(linkname))
337 goto out_free;
338
339 err = 0;
340out_free:
341 free(filename);
342 free(linkname);
343 return err;
344}
345
346static int dso__cache_build_id(struct dso *self, const char *debugdir)
347{
348 bool is_kallsyms = self->kernel && self->long_name[0] != '/';
349
350 return build_id_cache__add_b(self->build_id, sizeof(self->build_id),
351 self->long_name, debugdir, is_kallsyms);
352}
353
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200354static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
355{
356 struct dso *pos;
357 int err = 0;
358
359 dsos__for_each_with_build_id(pos, head)
360 if (dso__cache_build_id(pos, debugdir))
361 err = -1;
362
363 return err;
364}
365
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800366static int dsos__cache_build_ids(struct perf_header *self)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200367{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800368 struct perf_session *session = container_of(self,
369 struct perf_session, header);
370 struct rb_node *nd;
371 int ret = 0;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200372 char debugdir[PATH_MAX];
373
374 snprintf(debugdir, sizeof(debugdir), "%s/%s", getenv("HOME"),
375 DEBUG_CACHE_DIR);
376
377 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
378 return -1;
379
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300380 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
381 struct machine *pos = rb_entry(nd, struct machine, rb_node);
382 ret |= __dsos__cache_build_ids(&pos->kernel_dsos, debugdir);
383 ret |= __dsos__cache_build_ids(&pos->user_dsos, debugdir);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800384 }
385 return ret ? -1 : 0;
386}
387
388static bool dsos__read_build_ids(struct perf_header *self, bool with_hits)
389{
390 bool ret = false;
391 struct perf_session *session = container_of(self,
392 struct perf_session, header);
393 struct rb_node *nd;
394
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300395 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
396 struct machine *pos = rb_entry(nd, struct machine, rb_node);
397 ret |= __dsos__read_build_ids(&pos->kernel_dsos, with_hits);
398 ret |= __dsos__read_build_ids(&pos->user_dsos, with_hits);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800399 }
400
401 return ret;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200402}
403
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200404static int perf_header__adds_write(struct perf_header *self, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200405{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100406 int nr_sections;
407 struct perf_file_section *feat_sec;
408 int sec_size;
409 u64 sec_start;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200410 int idx = 0, err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100411
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800412 if (dsos__read_build_ids(self, true))
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100413 perf_header__set_feat(self, HEADER_BUILD_ID);
414
415 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
416 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200417 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100418
419 feat_sec = calloc(sizeof(*feat_sec), nr_sections);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200420 if (feat_sec == NULL)
421 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100422
423 sec_size = sizeof(*feat_sec) * nr_sections;
424
425 sec_start = self->data_offset + self->data_size;
Xiao Guangrongf887f302010-02-04 16:46:42 +0800426 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200427
Frederic Weisbecker3e13ab22009-11-11 04:51:06 +0100428 if (perf_header__has_feat(self, HEADER_TRACE_INFO)) {
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100429 struct perf_file_section *trace_sec;
430
431 trace_sec = &feat_sec[idx++];
432
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200433 /* Write trace info */
Xiao Guangrongf887f302010-02-04 16:46:42 +0800434 trace_sec->offset = lseek(fd, 0, SEEK_CUR);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200435 read_tracing_data(fd, attrs, nr_counters);
Xiao Guangrongf887f302010-02-04 16:46:42 +0800436 trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200437 }
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100438
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100439
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100440 if (perf_header__has_feat(self, HEADER_BUILD_ID)) {
441 struct perf_file_section *buildid_sec;
442
443 buildid_sec = &feat_sec[idx++];
444
445 /* Write build-ids */
Xiao Guangrongf887f302010-02-04 16:46:42 +0800446 buildid_sec->offset = lseek(fd, 0, SEEK_CUR);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800447 err = dsos__write_buildid_table(self, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200448 if (err < 0) {
449 pr_debug("failed to write buildid table\n");
450 goto out_free;
451 }
Xiao Guangrongf887f302010-02-04 16:46:42 +0800452 buildid_sec->size = lseek(fd, 0, SEEK_CUR) -
453 buildid_sec->offset;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800454 dsos__cache_build_ids(self);
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100455 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100456
Xiao Guangrongf887f302010-02-04 16:46:42 +0800457 lseek(fd, sec_start, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200458 err = do_write(fd, feat_sec, sec_size);
459 if (err < 0)
460 pr_debug("failed to write feature section\n");
461out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100462 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200463 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100464}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200465
Tom Zanussi8dc58102010-04-01 23:59:15 -0500466int perf_header__write_pipe(int fd)
467{
468 struct perf_pipe_file_header f_header;
469 int err;
470
471 f_header = (struct perf_pipe_file_header){
472 .magic = PERF_MAGIC,
473 .size = sizeof(f_header),
474 };
475
476 err = do_write(fd, &f_header, sizeof(f_header));
477 if (err < 0) {
478 pr_debug("failed to write perf pipe header\n");
479 return err;
480 }
481
482 return 0;
483}
484
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200485int perf_header__write(struct perf_header *self, int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200486{
487 struct perf_file_header f_header;
488 struct perf_file_attr f_attr;
489 struct perf_header_attr *attr;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200490 int i, err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200491
492 lseek(fd, sizeof(f_header), SEEK_SET);
493
494
495 for (i = 0; i < self->attrs; i++) {
496 attr = self->attr[i];
497
498 attr->id_offset = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200499 err = do_write(fd, attr->id, attr->ids * sizeof(u64));
500 if (err < 0) {
501 pr_debug("failed to write perf header\n");
502 return err;
503 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200504 }
505
506
507 self->attr_offset = lseek(fd, 0, SEEK_CUR);
508
509 for (i = 0; i < self->attrs; i++) {
510 attr = self->attr[i];
511
512 f_attr = (struct perf_file_attr){
513 .attr = attr->attr,
514 .ids = {
515 .offset = attr->id_offset,
516 .size = attr->ids * sizeof(u64),
517 }
518 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200519 err = do_write(fd, &f_attr, sizeof(f_attr));
520 if (err < 0) {
521 pr_debug("failed to write perf header attribute\n");
522 return err;
523 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200524 }
525
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200526 self->event_offset = lseek(fd, 0, SEEK_CUR);
527 self->event_size = event_count * sizeof(struct perf_trace_event_type);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200528 if (events) {
529 err = do_write(fd, events, self->event_size);
530 if (err < 0) {
531 pr_debug("failed to write perf header events\n");
532 return err;
533 }
534 }
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200535
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200536 self->data_offset = lseek(fd, 0, SEEK_CUR);
537
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200538 if (at_exit) {
539 err = perf_header__adds_write(self, fd);
540 if (err < 0)
541 return err;
542 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100543
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200544 f_header = (struct perf_file_header){
545 .magic = PERF_MAGIC,
546 .size = sizeof(f_header),
547 .attr_size = sizeof(f_attr),
548 .attrs = {
549 .offset = self->attr_offset,
550 .size = self->attrs * sizeof(f_attr),
551 },
552 .data = {
553 .offset = self->data_offset,
554 .size = self->data_size,
555 },
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200556 .event_types = {
557 .offset = self->event_offset,
558 .size = self->event_size,
559 },
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200560 };
561
Frederic Weisbeckerdb9f11e2009-10-17 17:57:18 +0200562 memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200563
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200564 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200565 err = do_write(fd, &f_header, sizeof(f_header));
566 if (err < 0) {
567 pr_debug("failed to write perf header\n");
568 return err;
569 }
Xiao Guangrongf887f302010-02-04 16:46:42 +0800570 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200571
572 self->frozen = 1;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200573 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200574}
575
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200576static int perf_header__getbuffer64(struct perf_header *self,
577 int fd, void *buf, size_t size)
578{
Tom Zanussi8dc58102010-04-01 23:59:15 -0500579 if (do_read(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200580 return -1;
581
582 if (self->needs_swap)
583 mem_bswap_64(buf, size);
584
585 return 0;
586}
587
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200588int perf_header__process_sections(struct perf_header *self, int fd,
589 int (*process)(struct perf_file_section *self,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200590 struct perf_header *ph,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200591 int feat, int fd))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200592{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100593 struct perf_file_section *feat_sec;
594 int nr_sections;
595 int sec_size;
596 int idx = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200597 int err = -1, feat = 1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100598
599 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
600 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200601 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100602
603 feat_sec = calloc(sizeof(*feat_sec), nr_sections);
604 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200605 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100606
607 sec_size = sizeof(*feat_sec) * nr_sections;
608
Xiao Guangrongf887f302010-02-04 16:46:42 +0800609 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100610
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200611 if (perf_header__getbuffer64(self, fd, feat_sec, sec_size))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200612 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100613
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200614 err = 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200615 while (idx < nr_sections && feat < HEADER_LAST_FEATURE) {
616 if (perf_header__has_feat(self, feat)) {
617 struct perf_file_section *sec = &feat_sec[idx++];
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100618
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200619 err = process(sec, self, feat, fd);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200620 if (err < 0)
621 break;
622 }
623 ++feat;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100624 }
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200625out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +0100626 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200627 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200628}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200629
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200630int perf_file_header__read(struct perf_file_header *self,
631 struct perf_header *ph, int fd)
632{
633 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200634
Tom Zanussi8dc58102010-04-01 23:59:15 -0500635 if (do_read(fd, self, sizeof(*self)) <= 0 ||
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200636 memcmp(&self->magic, __perf_magic, sizeof(self->magic)))
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200637 return -1;
638
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200639 if (self->attr_size != sizeof(struct perf_file_attr)) {
640 u64 attr_size = bswap_64(self->attr_size);
641
642 if (attr_size != sizeof(struct perf_file_attr))
643 return -1;
644
645 mem_bswap_64(self, offsetof(struct perf_file_header,
646 adds_features));
647 ph->needs_swap = true;
648 }
649
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200650 if (self->size != sizeof(*self)) {
651 /* Support the previous format */
652 if (self->size == offsetof(typeof(*self), adds_features))
653 bitmap_zero(self->adds_features, HEADER_FEAT_BITS);
654 else
655 return -1;
656 }
657
658 memcpy(&ph->adds_features, &self->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200659 sizeof(ph->adds_features));
660 /*
661 * FIXME: hack that assumes that if we need swap the perf.data file
662 * may be coming from an arch with a different word-size, ergo different
663 * DEFINE_BITMAP format, investigate more later, but for now its mostly
664 * safe to assume that we have a build-id section. Trace files probably
665 * have several other issues in this realm anyway...
666 */
667 if (ph->needs_swap) {
668 memset(&ph->adds_features, 0, sizeof(ph->adds_features));
669 perf_header__set_feat(ph, HEADER_BUILD_ID);
670 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200671
672 ph->event_offset = self->event_types.offset;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200673 ph->event_size = self->event_types.size;
674 ph->data_offset = self->data.offset;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200675 ph->data_size = self->data.size;
676 return 0;
677}
678
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800679static int __event_process_build_id(struct build_id_event *bev,
680 char *filename,
681 struct perf_session *session)
682{
683 int err = -1;
684 struct list_head *head;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300685 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800686 u16 misc;
687 struct dso *dso;
688 enum dso_kernel_type dso_type;
689
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300690 machine = perf_session__findnew_machine(session, bev->pid);
691 if (!machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800692 goto out;
693
694 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
695
696 switch (misc) {
697 case PERF_RECORD_MISC_KERNEL:
698 dso_type = DSO_TYPE_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300699 head = &machine->kernel_dsos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800700 break;
701 case PERF_RECORD_MISC_GUEST_KERNEL:
702 dso_type = DSO_TYPE_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300703 head = &machine->kernel_dsos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800704 break;
705 case PERF_RECORD_MISC_USER:
706 case PERF_RECORD_MISC_GUEST_USER:
707 dso_type = DSO_TYPE_USER;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -0300708 head = &machine->user_dsos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800709 break;
710 default:
711 goto out;
712 }
713
714 dso = __dsos__findnew(head, filename);
715 if (dso != NULL) {
716 dso__set_build_id(dso, &bev->build_id);
717 if (filename[0] == '[')
718 dso->kernel = dso_type;
719 }
720
721 err = 0;
722out:
723 return err;
724}
725
726static int perf_header__read_build_ids(struct perf_header *self,
727 int input, u64 offset, u64 size)
728{
729 struct perf_session *session = container_of(self,
730 struct perf_session, header);
731 struct build_id_event bev;
732 char filename[PATH_MAX];
733 u64 limit = offset + size;
734 int err = -1;
735
736 while (offset < limit) {
737 ssize_t len;
738
739 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
740 goto out;
741
742 if (self->needs_swap)
743 perf_event_header__bswap(&bev.header);
744
745 len = bev.header.size - sizeof(bev);
746 if (read(input, filename, len) != len)
747 goto out;
748
749 __event_process_build_id(&bev, filename, session);
750
751 offset += bev.header.size;
752 }
753 err = 0;
754out:
755 return err;
756}
757
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200758static int perf_file_section__process(struct perf_file_section *self,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200759 struct perf_header *ph,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200760 int feat, int fd)
761{
Xiao Guangrongf887f302010-02-04 16:46:42 +0800762 if (lseek(fd, self->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200763 pr_debug("Failed to lseek to %Ld offset for feature %d, "
764 "continuing...\n", self->offset, feat);
765 return 0;
766 }
767
768 switch (feat) {
769 case HEADER_TRACE_INFO:
770 trace_report(fd);
771 break;
772
773 case HEADER_BUILD_ID:
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200774 if (perf_header__read_build_ids(ph, fd, self->offset, self->size))
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200775 pr_debug("Failed to read buildids, continuing...\n");
776 break;
777 default:
778 pr_debug("unknown feature %d, continuing...\n", feat);
779 }
780
781 return 0;
782}
783
Tom Zanussi8dc58102010-04-01 23:59:15 -0500784static int perf_file_header__read_pipe(struct perf_pipe_file_header *self,
785 struct perf_header *ph, int fd)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200786{
Tom Zanussi8dc58102010-04-01 23:59:15 -0500787 if (do_read(fd, self, sizeof(*self)) <= 0 ||
788 memcmp(&self->magic, __perf_magic, sizeof(self->magic)))
789 return -1;
790
791 if (self->size != sizeof(*self)) {
792 u64 size = bswap_64(self->size);
793
794 if (size != sizeof(*self))
795 return -1;
796
797 ph->needs_swap = true;
798 }
799
800 return 0;
801}
802
803static int perf_header__read_pipe(struct perf_session *session, int fd)
804{
805 struct perf_header *self = &session->header;
806 struct perf_pipe_file_header f_header;
807
808 if (perf_file_header__read_pipe(&f_header, self, fd) < 0) {
809 pr_debug("incompatible file format\n");
810 return -EINVAL;
811 }
812
813 session->fd = fd;
814
815 return 0;
816}
817
818int perf_header__read(struct perf_session *session, int fd)
819{
820 struct perf_header *self = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200821 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200822 struct perf_file_attr f_attr;
823 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200824 int nr_attrs, nr_ids, i, j;
825
Tom Zanussi8dc58102010-04-01 23:59:15 -0500826 if (session->fd_pipe)
827 return perf_header__read_pipe(session, fd);
828
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200829 if (perf_file_header__read(&f_header, self, fd) < 0) {
830 pr_debug("incompatible file format\n");
831 return -EINVAL;
832 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200833
834 nr_attrs = f_header.attrs.size / sizeof(f_attr);
835 lseek(fd, f_header.attrs.offset, SEEK_SET);
836
837 for (i = 0; i < nr_attrs; i++) {
838 struct perf_header_attr *attr;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200839 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200840
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200841 if (perf_header__getbuffer64(self, fd, &f_attr, sizeof(f_attr)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200842 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200843
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200844 tmp = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200845
846 attr = perf_header_attr__new(&f_attr.attr);
Arnaldo Carvalho de Melodc79c0f2009-11-16 19:30:26 -0200847 if (attr == NULL)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200848 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200849
850 nr_ids = f_attr.ids.size / sizeof(u64);
851 lseek(fd, f_attr.ids.offset, SEEK_SET);
852
853 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200854 if (perf_header__getbuffer64(self, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200855 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200856
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200857 if (perf_header_attr__add_id(attr, f_id) < 0) {
858 perf_header_attr__delete(attr);
859 return -ENOMEM;
860 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200861 }
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200862 if (perf_header__add_attr(self, attr) < 0) {
863 perf_header_attr__delete(attr);
864 return -ENOMEM;
865 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -0200866
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200867 lseek(fd, tmp, SEEK_SET);
868 }
869
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200870 if (f_header.event_types.size) {
871 lseek(fd, f_header.event_types.offset, SEEK_SET);
872 events = malloc(f_header.event_types.size);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200873 if (events == NULL)
874 return -ENOMEM;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -0200875 if (perf_header__getbuffer64(self, fd, events,
876 f_header.event_types.size))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200877 goto out_errno;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200878 event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
879 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200880
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -0200881 perf_header__process_sections(self, fd, perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100882
Ingo Molnar2e01d172009-09-03 16:21:11 +0200883 lseek(fd, self->data_offset, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200884
885 self->frozen = 1;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -0200886 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -0200887out_errno:
888 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200889}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200890
891u64 perf_header__sample_type(struct perf_header *header)
892{
893 u64 type = 0;
894 int i;
895
896 for (i = 0; i < header->attrs; i++) {
897 struct perf_header_attr *attr = header->attr[i];
898
899 if (!type)
900 type = attr->attr.sample_type;
901 else if (type != attr->attr.sample_type)
902 die("non matching sample_type");
903 }
904
905 return type;
906}
907
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200908struct perf_event_attr *
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200909perf_header__find_attr(u64 id, struct perf_header *header)
910{
911 int i;
912
913 for (i = 0; i < header->attrs; i++) {
914 struct perf_header_attr *attr = header->attr[i];
915 int j;
916
917 for (j = 0; j < attr->ids; j++) {
918 if (attr->id[j] == id)
919 return &attr->attr;
920 }
921 }
922
923 return NULL;
924}
Tom Zanussi2c46dbb2010-04-01 23:59:19 -0500925
926int event__synthesize_attr(struct perf_event_attr *attr, u16 ids, u64 *id,
927 event__handler_t process,
928 struct perf_session *session)
929{
930 event_t *ev;
931 size_t size;
932 int err;
933
934 size = sizeof(struct perf_event_attr);
935 size = ALIGN(size, sizeof(u64));
936 size += sizeof(struct perf_event_header);
937 size += ids * sizeof(u64);
938
939 ev = malloc(size);
940
941 ev->attr.attr = *attr;
942 memcpy(ev->attr.id, id, ids * sizeof(u64));
943
944 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
945 ev->attr.header.size = size;
946
947 err = process(ev, session);
948
949 free(ev);
950
951 return err;
952}
953
954int event__synthesize_attrs(struct perf_header *self,
955 event__handler_t process,
956 struct perf_session *session)
957{
958 struct perf_header_attr *attr;
959 int i, err = 0;
960
961 for (i = 0; i < self->attrs; i++) {
962 attr = self->attr[i];
963
964 err = event__synthesize_attr(&attr->attr, attr->ids, attr->id,
965 process, session);
966 if (err) {
967 pr_debug("failed to create perf header attribute\n");
968 return err;
969 }
970 }
971
972 return err;
973}
974
975int event__process_attr(event_t *self, struct perf_session *session)
976{
977 struct perf_header_attr *attr;
978 unsigned int i, ids, n_ids;
979
980 attr = perf_header_attr__new(&self->attr.attr);
981 if (attr == NULL)
982 return -ENOMEM;
983
984 ids = self->header.size;
985 ids -= (void *)&self->attr.id - (void *)self;
986 n_ids = ids / sizeof(u64);
987
988 for (i = 0; i < n_ids; i++) {
989 if (perf_header_attr__add_id(attr, self->attr.id[i]) < 0) {
990 perf_header_attr__delete(attr);
991 return -ENOMEM;
992 }
993 }
994
995 if (perf_header__add_attr(&session->header, attr) < 0) {
996 perf_header_attr__delete(attr);
997 return -ENOMEM;
998 }
999
1000 perf_session__update_sample_type(session);
1001
1002 return 0;
1003}
Tom Zanussicd19a032010-04-01 23:59:20 -05001004
1005int event__synthesize_event_type(u64 event_id, char *name,
1006 event__handler_t process,
1007 struct perf_session *session)
1008{
1009 event_t ev;
1010 size_t size = 0;
1011 int err = 0;
1012
1013 memset(&ev, 0, sizeof(ev));
1014
1015 ev.event_type.event_type.event_id = event_id;
1016 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
1017 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
1018
1019 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
1020 size = strlen(name);
1021 size = ALIGN(size, sizeof(u64));
1022 ev.event_type.header.size = sizeof(ev.event_type) -
1023 (sizeof(ev.event_type.event_type.name) - size);
1024
1025 err = process(&ev, session);
1026
1027 return err;
1028}
1029
1030int event__synthesize_event_types(event__handler_t process,
1031 struct perf_session *session)
1032{
1033 struct perf_trace_event_type *type;
1034 int i, err = 0;
1035
1036 for (i = 0; i < event_count; i++) {
1037 type = &events[i];
1038
1039 err = event__synthesize_event_type(type->event_id, type->name,
1040 process, session);
1041 if (err) {
1042 pr_debug("failed to create perf header event type\n");
1043 return err;
1044 }
1045 }
1046
1047 return err;
1048}
1049
1050int event__process_event_type(event_t *self,
1051 struct perf_session *session __unused)
1052{
1053 if (perf_header__push_event(self->event_type.event_type.event_id,
1054 self->event_type.event_type.name) < 0)
1055 return -ENOMEM;
1056
1057 return 0;
1058}
Tom Zanussi92155452010-04-01 23:59:21 -05001059
1060int event__synthesize_tracing_data(int fd, struct perf_event_attr *pattrs,
1061 int nb_events,
1062 event__handler_t process,
1063 struct perf_session *session __unused)
1064{
1065 event_t ev;
1066 ssize_t size = 0, aligned_size = 0, padding;
1067 int err = 0;
1068
1069 memset(&ev, 0, sizeof(ev));
1070
1071 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
1072 size = read_tracing_data_size(fd, pattrs, nb_events);
1073 if (size <= 0)
1074 return size;
1075 aligned_size = ALIGN(size, sizeof(u64));
1076 padding = aligned_size - size;
1077 ev.tracing_data.header.size = sizeof(ev.tracing_data);
1078 ev.tracing_data.size = aligned_size;
1079
1080 process(&ev, session);
1081
1082 err = read_tracing_data(fd, pattrs, nb_events);
1083 write_padded(fd, NULL, 0, padding);
1084
1085 return aligned_size;
1086}
1087
1088int event__process_tracing_data(event_t *self,
1089 struct perf_session *session)
1090{
1091 ssize_t size_read, padding, size = self->tracing_data.size;
1092 off_t offset = lseek(session->fd, 0, SEEK_CUR);
1093 char buf[BUFSIZ];
1094
1095 /* setup for reading amidst mmap */
1096 lseek(session->fd, offset + sizeof(struct tracing_data_event),
1097 SEEK_SET);
1098
1099 size_read = trace_report(session->fd);
1100
1101 padding = ALIGN(size_read, sizeof(u64)) - size_read;
1102
1103 if (read(session->fd, buf, padding) < 0)
1104 die("reading input file");
1105
1106 if (size_read + padding != size)
1107 die("tracing data size mismatch");
1108
1109 return size_read + padding;
1110}
Tom Zanussic7929e42010-04-01 23:59:22 -05001111
1112int event__synthesize_build_id(struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001113 event__handler_t process, struct machine *machine,
Tom Zanussic7929e42010-04-01 23:59:22 -05001114 struct perf_session *session)
1115{
1116 event_t ev;
1117 size_t len;
1118 int err = 0;
1119
1120 if (!pos->hit)
1121 return err;
1122
1123 memset(&ev, 0, sizeof(ev));
1124
1125 len = pos->long_name_len + 1;
1126 len = ALIGN(len, NAME_ALIGN);
1127 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
1128 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
1129 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001130 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05001131 ev.build_id.header.size = sizeof(ev.build_id) + len;
1132 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
1133
1134 err = process(&ev, session);
1135
1136 return err;
1137}
1138
1139static int __event_synthesize_build_ids(struct list_head *head, u16 misc,
1140 event__handler_t process,
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001141 struct machine *machine,
Tom Zanussic7929e42010-04-01 23:59:22 -05001142 struct perf_session *session)
1143{
1144 struct dso *pos;
1145
1146 dsos__for_each_with_build_id(pos, head) {
1147 int err;
1148 if (!pos->hit)
1149 continue;
1150
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001151 err = event__synthesize_build_id(pos, misc, process,
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001152 machine, session);
Tom Zanussic7929e42010-04-01 23:59:22 -05001153 if (err < 0)
1154 return err;
1155 }
1156
1157 return 0;
1158}
1159
1160int event__synthesize_build_ids(event__handler_t process,
1161 struct perf_session *session)
1162{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001163 int err = 0;
1164 u16 kmisc, umisc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001165 struct machine *pos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001166 struct rb_node *nd;
Tom Zanussic7929e42010-04-01 23:59:22 -05001167
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001168 if (!dsos__read_build_ids(&session->header, true))
Tom Zanussic7929e42010-04-01 23:59:22 -05001169 return 0;
1170
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001171 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
1172 pos = rb_entry(nd, struct machine, rb_node);
1173 if (machine__is_host(pos)) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001174 kmisc = PERF_RECORD_MISC_KERNEL;
1175 umisc = PERF_RECORD_MISC_USER;
1176 } else {
1177 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
1178 umisc = PERF_RECORD_MISC_GUEST_USER;
1179 }
1180
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001181 err = __event_synthesize_build_ids(&pos->kernel_dsos, kmisc,
1182 process, pos, session);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001183 if (err == 0)
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001184 err = __event_synthesize_build_ids(&pos->user_dsos, umisc,
1185 process, pos, session);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001186 if (err)
1187 break;
1188 }
Tom Zanussic7929e42010-04-01 23:59:22 -05001189
1190 if (err < 0) {
1191 pr_debug("failed to synthesize build ids\n");
1192 return err;
1193 }
1194
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001195 dsos__cache_build_ids(&session->header);
Tom Zanussic7929e42010-04-01 23:59:22 -05001196
1197 return 0;
1198}
1199
1200int event__process_build_id(event_t *self,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001201 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05001202{
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001203 __event_process_build_id(&self->build_id,
1204 self->build_id.filename,
1205 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05001206 return 0;
1207}