blob: 9709d38113b136b4909b8f45cc3e16c095c0e865 [file] [log] [blame]
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001#include <sys/types.h>
2#include <unistd.h>
3#include <stdio.h>
4#include <stdlib.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +01005#include <linux/list.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02006
7#include "util.h"
8#include "header.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +02009#include "../perf.h"
10#include "trace-event.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010011#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010012#include "data_map.h"
13#include "debug.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020014
15/*
Arjan van de Venec60a3f2009-09-19 13:36:30 +020016 * Create new perf.data header attribute:
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020017 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020018struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020019{
20 struct perf_header_attr *self = malloc(sizeof(*self));
21
22 if (!self)
23 die("nomem");
24
25 self->attr = *attr;
26 self->ids = 0;
27 self->size = 1;
28 self->id = malloc(sizeof(u64));
29
30 if (!self->id)
31 die("nomem");
32
33 return self;
34}
35
36void perf_header_attr__add_id(struct perf_header_attr *self, u64 id)
37{
38 int pos = self->ids;
39
40 self->ids++;
41 if (self->ids > self->size) {
42 self->size *= 2;
43 self->id = realloc(self->id, self->size * sizeof(u64));
44 if (!self->id)
45 die("nomem");
46 }
47 self->id[pos] = id;
48}
49
50/*
Arjan van de Venec60a3f2009-09-19 13:36:30 +020051 * Create new perf.data header:
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020052 */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020053struct perf_header *perf_header__new(void)
54{
Frederic Weisbecker2ba08252009-10-17 17:12:34 +020055 struct perf_header *self = calloc(sizeof(*self), 1);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020056
57 if (!self)
58 die("nomem");
59
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020060 self->size = 1;
61 self->attr = malloc(sizeof(void *));
62
63 if (!self->attr)
64 die("nomem");
65
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020066 return self;
67}
68
69void perf_header__add_attr(struct perf_header *self,
70 struct perf_header_attr *attr)
71{
72 int pos = self->attrs;
73
74 if (self->frozen)
75 die("frozen");
76
77 self->attrs++;
78 if (self->attrs > self->size) {
79 self->size *= 2;
80 self->attr = realloc(self->attr, self->size * sizeof(void *));
81 if (!self->attr)
82 die("nomem");
83 }
84 self->attr[pos] = attr;
85}
86
Arjan van de Venec60a3f2009-09-19 13:36:30 +020087#define MAX_EVENT_NAME 64
88
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020089struct perf_trace_event_type {
90 u64 event_id;
Arjan van de Venec60a3f2009-09-19 13:36:30 +020091 char name[MAX_EVENT_NAME];
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020092};
93
94static int event_count;
95static struct perf_trace_event_type *events;
96
97void perf_header__push_event(u64 id, const char *name)
98{
Arjan van de Venec60a3f2009-09-19 13:36:30 +020099 if (strlen(name) > MAX_EVENT_NAME)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200100 pr_warning("Event %s will be truncated\n", name);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200101
102 if (!events) {
103 events = malloc(sizeof(struct perf_trace_event_type));
104 if (!events)
105 die("nomem");
106 } else {
107 events = realloc(events, (event_count + 1) * sizeof(struct perf_trace_event_type));
108 if (!events)
109 die("nomem");
110 }
111 memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
112 events[event_count].event_id = id;
Arjan van de Venec60a3f2009-09-19 13:36:30 +0200113 strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200114 event_count++;
115}
116
117char *perf_header__find_event(u64 id)
118{
119 int i;
120 for (i = 0 ; i < event_count; i++) {
121 if (events[i].event_id == id)
122 return events[i].name;
123 }
124 return NULL;
125}
126
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200127static const char *__perf_magic = "PERFFILE";
128
129#define PERF_MAGIC (*(u64 *)__perf_magic)
130
131struct perf_file_section {
132 u64 offset;
133 u64 size;
134};
135
136struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200137 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200138 struct perf_file_section ids;
139};
140
141struct perf_file_header {
142 u64 magic;
143 u64 size;
144 u64 attr_size;
145 struct perf_file_section attrs;
146 struct perf_file_section data;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200147 struct perf_file_section event_types;
Frederic Weisbeckerdb9f11e2009-10-17 17:57:18 +0200148 DECLARE_BITMAP(adds_features, HEADER_FEAT_BITS);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200149};
150
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -0200151void perf_header__set_feat(struct perf_header *self, int feat)
152{
153 set_bit(feat, self->adds_features);
154}
155
156bool perf_header__has_feat(const struct perf_header *self, int feat)
157{
158 return test_bit(feat, self->adds_features);
159}
160
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200161static void do_write(int fd, void *buf, size_t size)
162{
163 while (size) {
164 int ret = write(fd, buf, size);
165
166 if (ret < 0)
167 die("failed to write");
168
169 size -= ret;
170 buf += ret;
171 }
172}
173
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100174static void write_buildid_table(int fd, struct list_head *id_head)
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100175{
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100176 struct build_id_list *iter, *next;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100177
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100178 list_for_each_entry_safe(iter, next, id_head, list) {
179 struct build_id_event *b = &iter->event;
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100180
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100181 do_write(fd, b, sizeof(*b));
182 do_write(fd, (void *)iter->dso_name, iter->len);
183 list_del(&iter->list);
184 free(iter);
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100185 }
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100186}
187
188static void
189perf_header__adds_write(struct perf_header *self, int fd, bool at_exit)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200190{
191 struct perf_file_section trace_sec;
192 u64 cur_offset = lseek(fd, 0, SEEK_CUR);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200193
Frederic Weisbecker3e13ab22009-11-11 04:51:06 +0100194 if (perf_header__has_feat(self, HEADER_TRACE_INFO)) {
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200195 /* Write trace info */
196 trace_sec.offset = lseek(fd, sizeof(trace_sec), SEEK_CUR);
197 read_tracing_data(fd, attrs, nr_counters);
198 trace_sec.size = lseek(fd, 0, SEEK_CUR) - trace_sec.offset;
199
200 /* Write trace info headers */
201 lseek(fd, cur_offset, SEEK_SET);
202 do_write(fd, &trace_sec, sizeof(trace_sec));
203
204 /*
205 * Update cur_offset. So that other (future)
206 * features can set their own infos in this place. But if we are
207 * the only feature, at least that seeks to the place the data
208 * should begin.
209 */
210 cur_offset = lseek(fd, trace_sec.offset + trace_sec.size, SEEK_SET);
211 }
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100212
213 if (at_exit) {
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100214 LIST_HEAD(id_list);
215
216 if (fetch_build_id_table(&id_list)) {
217 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100218 perf_header__set_feat(self, HEADER_BUILD_ID);
Frederic Weisbecker57f395a2009-11-11 04:51:04 +0100219 write_buildid_table(fd, &id_list);
220 lseek(fd, cur_offset, SEEK_SET);
221 }
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100222 }
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200223};
224
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100225void perf_header__write(struct perf_header *self, int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200226{
227 struct perf_file_header f_header;
228 struct perf_file_attr f_attr;
229 struct perf_header_attr *attr;
230 int i;
231
232 lseek(fd, sizeof(f_header), SEEK_SET);
233
234
235 for (i = 0; i < self->attrs; i++) {
236 attr = self->attr[i];
237
238 attr->id_offset = lseek(fd, 0, SEEK_CUR);
239 do_write(fd, attr->id, attr->ids * sizeof(u64));
240 }
241
242
243 self->attr_offset = lseek(fd, 0, SEEK_CUR);
244
245 for (i = 0; i < self->attrs; i++) {
246 attr = self->attr[i];
247
248 f_attr = (struct perf_file_attr){
249 .attr = attr->attr,
250 .ids = {
251 .offset = attr->id_offset,
252 .size = attr->ids * sizeof(u64),
253 }
254 };
255 do_write(fd, &f_attr, sizeof(f_attr));
256 }
257
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200258 self->event_offset = lseek(fd, 0, SEEK_CUR);
259 self->event_size = event_count * sizeof(struct perf_trace_event_type);
260 if (events)
261 do_write(fd, events, self->event_size);
262
Frederic Weisbecker8671dab2009-11-11 04:51:03 +0100263 perf_header__adds_write(self, fd, at_exit);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200264
265 self->data_offset = lseek(fd, 0, SEEK_CUR);
266
267 f_header = (struct perf_file_header){
268 .magic = PERF_MAGIC,
269 .size = sizeof(f_header),
270 .attr_size = sizeof(f_attr),
271 .attrs = {
272 .offset = self->attr_offset,
273 .size = self->attrs * sizeof(f_attr),
274 },
275 .data = {
276 .offset = self->data_offset,
277 .size = self->data_size,
278 },
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200279 .event_types = {
280 .offset = self->event_offset,
281 .size = self->event_size,
282 },
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200283 };
284
Frederic Weisbeckerdb9f11e2009-10-17 17:57:18 +0200285 memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200286
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200287 lseek(fd, 0, SEEK_SET);
288 do_write(fd, &f_header, sizeof(f_header));
289 lseek(fd, self->data_offset + self->data_size, SEEK_SET);
290
291 self->frozen = 1;
292}
293
294static void do_read(int fd, void *buf, size_t size)
295{
296 while (size) {
297 int ret = read(fd, buf, size);
298
299 if (ret < 0)
300 die("failed to read");
Pierre Habouzit7eac7e92009-08-07 14:16:00 +0200301 if (ret == 0)
302 die("failed to read: missing data");
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200303
304 size -= ret;
305 buf += ret;
306 }
307}
308
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200309static void perf_header__adds_read(struct perf_header *self, int fd)
310{
Frederic Weisbecker3e13ab22009-11-11 04:51:06 +0100311 if (perf_header__has_feat(self, HEADER_TRACE_INFO)) {
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200312 struct perf_file_section trace_sec;
313
314 do_read(fd, &trace_sec, sizeof(trace_sec));
315 lseek(fd, trace_sec.offset, SEEK_SET);
316 trace_report(fd);
317 lseek(fd, trace_sec.offset + trace_sec.size, SEEK_SET);
318 }
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100319
320 if (perf_header__has_feat(self, HEADER_BUILD_ID)) {
321 struct stat input_stat;
322
323 fstat(fd, &input_stat);
324 if (perf_header__read_build_ids(self, fd, input_stat.st_size))
325 pr_debug("failed to read buildids, continuing...\n");
326 }
Frederic Weisbecker2ba08252009-10-17 17:12:34 +0200327};
328
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200329struct perf_header *perf_header__read(int fd)
330{
331 struct perf_header *self = perf_header__new();
332 struct perf_file_header f_header;
333 struct perf_file_attr f_attr;
334 u64 f_id;
335
336 int nr_attrs, nr_ids, i, j;
337
338 lseek(fd, 0, SEEK_SET);
339 do_read(fd, &f_header, sizeof(f_header));
340
341 if (f_header.magic != PERF_MAGIC ||
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200342 f_header.attr_size != sizeof(f_attr))
343 die("incompatible file format");
344
Frederic Weisbecker26dd2cb2009-10-08 22:07:29 +0200345 if (f_header.size != sizeof(f_header)) {
346 /* Support the previous format */
Frederic Weisbeckerdb9f11e2009-10-17 17:57:18 +0200347 if (f_header.size == offsetof(typeof(f_header), adds_features))
348 bitmap_zero(f_header.adds_features, HEADER_FEAT_BITS);
349 else
Frederic Weisbecker26dd2cb2009-10-08 22:07:29 +0200350 die("incompatible file format");
351 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200352 nr_attrs = f_header.attrs.size / sizeof(f_attr);
353 lseek(fd, f_header.attrs.offset, SEEK_SET);
354
355 for (i = 0; i < nr_attrs; i++) {
356 struct perf_header_attr *attr;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200357 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200358
359 do_read(fd, &f_attr, sizeof(f_attr));
Peter Zijlstra1c222bc2009-08-06 20:57:41 +0200360 tmp = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200361
362 attr = perf_header_attr__new(&f_attr.attr);
363
364 nr_ids = f_attr.ids.size / sizeof(u64);
365 lseek(fd, f_attr.ids.offset, SEEK_SET);
366
367 for (j = 0; j < nr_ids; j++) {
368 do_read(fd, &f_id, sizeof(f_id));
369
370 perf_header_attr__add_id(attr, f_id);
371 }
372 perf_header__add_attr(self, attr);
373 lseek(fd, tmp, SEEK_SET);
374 }
375
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200376 if (f_header.event_types.size) {
377 lseek(fd, f_header.event_types.offset, SEEK_SET);
378 events = malloc(f_header.event_types.size);
379 if (!events)
380 die("nomem");
381 do_read(fd, events, f_header.event_types.size);
382 event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
383 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200384
Frederic Weisbeckerdb9f11e2009-10-17 17:57:18 +0200385 memcpy(&self->adds_features, &f_header.adds_features, sizeof(f_header.adds_features));
Frederic Weisbecker03456a12009-10-06 23:36:47 +0200386
Arjan van de Ven8755a8f2009-09-12 07:52:51 +0200387 self->event_offset = f_header.event_types.offset;
388 self->event_size = f_header.event_types.size;
389
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200390 self->data_offset = f_header.data.offset;
391 self->data_size = f_header.data.size;
392
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +0100393 perf_header__adds_read(self, fd);
394
Ingo Molnar2e01d172009-09-03 16:21:11 +0200395 lseek(fd, self->data_offset, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200396
397 self->frozen = 1;
398
399 return self;
400}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200401
402u64 perf_header__sample_type(struct perf_header *header)
403{
404 u64 type = 0;
405 int i;
406
407 for (i = 0; i < header->attrs; i++) {
408 struct perf_header_attr *attr = header->attr[i];
409
410 if (!type)
411 type = attr->attr.sample_type;
412 else if (type != attr->attr.sample_type)
413 die("non matching sample_type");
414 }
415
416 return type;
417}
418
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200419struct perf_event_attr *
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +0200420perf_header__find_attr(u64 id, struct perf_header *header)
421{
422 int i;
423
424 for (i = 0; i < header->attrs; i++) {
425 struct perf_header_attr *attr = header->attr[i];
426 int j;
427
428 for (j = 0; j < attr->ids; j++) {
429 if (attr->id[j] == id)
430 return &attr->attr;
431 }
432 }
433
434 return NULL;
435}