blob: a4a60b7887eeb9c96b22258afc7462c8f8caf5ab [file] [log] [blame]
Arnaldo Carvalho de Meloa9072bc2011-10-26 12:41:38 -02001#include "util.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02004#include <unistd.h>
5#include <stdio.h>
6#include <stdlib.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +01007#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02008#include <linux/kernel.h>
Robert Richterb1e5a9b2011-12-07 10:02:57 +01009#include <linux/bitops.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020010#include <sys/utsname.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020011
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020012#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030013#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020014#include "header.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020015#include "../perf.h"
16#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020017#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010018#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010019#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020020#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020021#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020022#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090023#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020024#include "build-id.h"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020025#include "data.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020026
Stephane Eraniana1ac1d32010-06-17 11:39:01 +020027static bool no_buildid_cache = false;
28
Stephane Eranianfbe96f22011-09-30 15:40:40 +020029static u32 header_argc;
30static const char **header_argv;
31
Stephane Eranian73323f52012-02-02 13:54:44 +010032/*
33 * magic2 = "PERFILE2"
34 * must be a numerical value to let the endianness
35 * determine the memory layout. That way we are able
36 * to detect endianness when reading the perf.data file
37 * back.
38 *
39 * we check for legacy (PERFFILE) format.
40 */
41static const char *__perf_magic1 = "PERFFILE";
42static const u64 __perf_magic2 = 0x32454c4946524550ULL;
43static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020044
Stephane Eranian73323f52012-02-02 13:54:44 +010045#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020046
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020047struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020048 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020049 struct perf_file_section ids;
50};
51
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030052void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020053{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030054 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020055}
56
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030057void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020058{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030059 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020060}
61
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030062bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020063{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030064 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020065}
66
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020067static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020068{
69 while (size) {
70 int ret = write(fd, buf, size);
71
72 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -020073 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020074
75 size -= ret;
76 buf += ret;
77 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020078
79 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020080}
81
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -020082#define NAME_ALIGN 64
83
84static int write_padded(int fd, const void *bf, size_t count,
85 size_t count_aligned)
86{
87 static const char zero_buf[NAME_ALIGN];
88 int err = do_write(fd, bf, count);
89
90 if (!err)
91 err = do_write(fd, zero_buf, count_aligned - count);
92
93 return err;
94}
95
Stephane Eranianfbe96f22011-09-30 15:40:40 +020096static int do_write_string(int fd, const char *str)
97{
98 u32 len, olen;
99 int ret;
100
101 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300102 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200103
104 /* write len, incl. \0 */
105 ret = do_write(fd, &len, sizeof(len));
106 if (ret < 0)
107 return ret;
108
109 return write_padded(fd, str, olen, len);
110}
111
112static char *do_read_string(int fd, struct perf_header *ph)
113{
114 ssize_t sz, ret;
115 u32 len;
116 char *buf;
117
Namhyung Kim5323f602012-12-17 15:38:54 +0900118 sz = readn(fd, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200119 if (sz < (ssize_t)sizeof(len))
120 return NULL;
121
122 if (ph->needs_swap)
123 len = bswap_32(len);
124
125 buf = malloc(len);
126 if (!buf)
127 return NULL;
128
Namhyung Kim5323f602012-12-17 15:38:54 +0900129 ret = readn(fd, buf, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200130 if (ret == (ssize_t)len) {
131 /*
132 * strings are padded by zeroes
133 * thus the actual strlen of buf
134 * may be less than len
135 */
136 return buf;
137 }
138
139 free(buf);
140 return NULL;
141}
142
143int
144perf_header__set_cmdline(int argc, const char **argv)
145{
146 int i;
147
David Ahern56e6f602012-07-29 20:53:51 -0600148 /*
149 * If header_argv has already been set, do not override it.
150 * This allows a command to set the cmdline, parse args and
151 * then call another builtin function that implements a
152 * command -- e.g, cmd_kvm calling cmd_record.
153 */
154 if (header_argv)
155 return 0;
156
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200157 header_argc = (u32)argc;
158
159 /* do not include NULL termination */
160 header_argv = calloc(argc, sizeof(char *));
161 if (!header_argv)
162 return -ENOMEM;
163
164 /*
165 * must copy argv contents because it gets moved
166 * around during option parsing
167 */
168 for (i = 0; i < argc ; i++)
169 header_argv[i] = argv[i];
170
171 return 0;
172}
173
Robert Richter1b549502011-12-07 10:02:53 +0100174#define dsos__for_each_with_build_id(pos, head) \
175 list_for_each_entry(pos, head, node) \
176 if (!pos->has_build_id) \
177 continue; \
178 else
179
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300180static int write_buildid(const char *name, size_t name_len, u8 *build_id,
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200181 pid_t pid, u16 misc, int fd)
182{
183 int err;
184 struct build_id_event b;
185 size_t len;
186
187 len = name_len + 1;
188 len = PERF_ALIGN(len, NAME_ALIGN);
189
190 memset(&b, 0, sizeof(b));
191 memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
192 b.pid = pid;
193 b.header.misc = misc;
194 b.header.size = sizeof(b) + len;
195
196 err = do_write(fd, &b, sizeof(b));
197 if (err < 0)
198 return err;
199
200 return write_padded(fd, name, name_len + 1, len);
201}
202
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300203static int __dsos__write_buildid_table(struct list_head *head,
204 struct machine *machine,
205 pid_t pid, u16 misc, int fd)
Robert Richter1b549502011-12-07 10:02:53 +0100206{
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300207 char nm[PATH_MAX];
Robert Richter1b549502011-12-07 10:02:53 +0100208 struct dso *pos;
209
210 dsos__for_each_with_build_id(pos, head) {
211 int err;
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300212 const char *name;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200213 size_t name_len;
Robert Richter1b549502011-12-07 10:02:53 +0100214
215 if (!pos->hit)
216 continue;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200217
218 if (is_vdso_map(pos->short_name)) {
219 name = (char *) VDSO__MAP_NAME;
220 name_len = sizeof(VDSO__MAP_NAME) + 1;
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300221 } else if (dso__is_kcore(pos)) {
222 machine__mmap_name(machine, nm, sizeof(nm));
223 name = nm;
224 name_len = strlen(nm) + 1;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200225 } else {
226 name = pos->long_name;
227 name_len = pos->long_name_len + 1;
228 }
229
230 err = write_buildid(name, name_len, pos->build_id,
231 pid, misc, fd);
232 if (err)
Robert Richter1b549502011-12-07 10:02:53 +0100233 return err;
234 }
235
236 return 0;
237}
238
239static int machine__write_buildid_table(struct machine *machine, int fd)
240{
241 int err;
242 u16 kmisc = PERF_RECORD_MISC_KERNEL,
243 umisc = PERF_RECORD_MISC_USER;
244
245 if (!machine__is_host(machine)) {
246 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
247 umisc = PERF_RECORD_MISC_GUEST_USER;
248 }
249
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300250 err = __dsos__write_buildid_table(&machine->kernel_dsos, machine,
251 machine->pid, kmisc, fd);
Robert Richter1b549502011-12-07 10:02:53 +0100252 if (err == 0)
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300253 err = __dsos__write_buildid_table(&machine->user_dsos, machine,
Robert Richter1b549502011-12-07 10:02:53 +0100254 machine->pid, umisc, fd);
255 return err;
256}
257
258static int dsos__write_buildid_table(struct perf_header *header, int fd)
259{
260 struct perf_session *session = container_of(header,
261 struct perf_session, header);
262 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300263 int err = machine__write_buildid_table(&session->machines.host, fd);
Robert Richter1b549502011-12-07 10:02:53 +0100264
265 if (err)
266 return err;
267
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300268 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
Robert Richter1b549502011-12-07 10:02:53 +0100269 struct machine *pos = rb_entry(nd, struct machine, rb_node);
270 err = machine__write_buildid_table(pos, fd);
271 if (err)
272 break;
273 }
274 return err;
275}
276
277int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200278 const char *name, bool is_kallsyms, bool is_vdso)
Robert Richter1b549502011-12-07 10:02:53 +0100279{
280 const size_t size = PATH_MAX;
281 char *realname, *filename = zalloc(size),
282 *linkname = zalloc(size), *targetname;
283 int len, err = -1;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200284 bool slash = is_kallsyms || is_vdso;
Robert Richter1b549502011-12-07 10:02:53 +0100285
286 if (is_kallsyms) {
287 if (symbol_conf.kptr_restrict) {
288 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
Thomas Jaroschfdae6372013-01-25 11:21:39 +0100289 err = 0;
290 goto out_free;
Robert Richter1b549502011-12-07 10:02:53 +0100291 }
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200292 realname = (char *) name;
Robert Richter1b549502011-12-07 10:02:53 +0100293 } else
294 realname = realpath(name, NULL);
295
296 if (realname == NULL || filename == NULL || linkname == NULL)
297 goto out_free;
298
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300299 len = scnprintf(filename, size, "%s%s%s",
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200300 debugdir, slash ? "/" : "",
301 is_vdso ? VDSO__MAP_NAME : realname);
Robert Richter1b549502011-12-07 10:02:53 +0100302 if (mkdir_p(filename, 0755))
303 goto out_free;
304
Namhyung Kimafda0f92012-05-01 23:19:36 +0900305 snprintf(filename + len, size - len, "/%s", sbuild_id);
Robert Richter1b549502011-12-07 10:02:53 +0100306
307 if (access(filename, F_OK)) {
308 if (is_kallsyms) {
309 if (copyfile("/proc/kallsyms", filename))
310 goto out_free;
311 } else if (link(realname, filename) && copyfile(name, filename))
312 goto out_free;
313 }
314
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300315 len = scnprintf(linkname, size, "%s/.build-id/%.2s",
Robert Richter1b549502011-12-07 10:02:53 +0100316 debugdir, sbuild_id);
317
318 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
319 goto out_free;
320
321 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
322 targetname = filename + strlen(debugdir) - 5;
323 memcpy(targetname, "../..", 5);
324
325 if (symlink(targetname, linkname) == 0)
326 err = 0;
327out_free:
328 if (!is_kallsyms)
329 free(realname);
330 free(filename);
331 free(linkname);
332 return err;
333}
334
335static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
336 const char *name, const char *debugdir,
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200337 bool is_kallsyms, bool is_vdso)
Robert Richter1b549502011-12-07 10:02:53 +0100338{
339 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
340
341 build_id__sprintf(build_id, build_id_size, sbuild_id);
342
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200343 return build_id_cache__add_s(sbuild_id, debugdir, name,
344 is_kallsyms, is_vdso);
Robert Richter1b549502011-12-07 10:02:53 +0100345}
346
347int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
348{
349 const size_t size = PATH_MAX;
350 char *filename = zalloc(size),
351 *linkname = zalloc(size);
352 int err = -1;
353
354 if (filename == NULL || linkname == NULL)
355 goto out_free;
356
357 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
358 debugdir, sbuild_id, sbuild_id + 2);
359
360 if (access(linkname, F_OK))
361 goto out_free;
362
363 if (readlink(linkname, filename, size - 1) < 0)
364 goto out_free;
365
366 if (unlink(linkname))
367 goto out_free;
368
369 /*
370 * Since the link is relative, we must make it absolute:
371 */
372 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
373 debugdir, sbuild_id, filename);
374
375 if (unlink(linkname))
376 goto out_free;
377
378 err = 0;
379out_free:
380 free(filename);
381 free(linkname);
382 return err;
383}
384
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300385static int dso__cache_build_id(struct dso *dso, struct machine *machine,
386 const char *debugdir)
Robert Richter1b549502011-12-07 10:02:53 +0100387{
388 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200389 bool is_vdso = is_vdso_map(dso->short_name);
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300390 const char *name = dso->long_name;
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300391 char nm[PATH_MAX];
Robert Richter1b549502011-12-07 10:02:53 +0100392
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300393 if (dso__is_kcore(dso)) {
394 is_kallsyms = true;
395 machine__mmap_name(machine, nm, sizeof(nm));
396 name = nm;
397 }
398 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name,
399 debugdir, is_kallsyms, is_vdso);
Robert Richter1b549502011-12-07 10:02:53 +0100400}
401
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300402static int __dsos__cache_build_ids(struct list_head *head,
403 struct machine *machine, const char *debugdir)
Robert Richter1b549502011-12-07 10:02:53 +0100404{
405 struct dso *pos;
406 int err = 0;
407
408 dsos__for_each_with_build_id(pos, head)
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300409 if (dso__cache_build_id(pos, machine, debugdir))
Robert Richter1b549502011-12-07 10:02:53 +0100410 err = -1;
411
412 return err;
413}
414
415static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
416{
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300417 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, machine,
418 debugdir);
419 ret |= __dsos__cache_build_ids(&machine->user_dsos, machine, debugdir);
Robert Richter1b549502011-12-07 10:02:53 +0100420 return ret;
421}
422
423static int perf_session__cache_build_ids(struct perf_session *session)
424{
425 struct rb_node *nd;
426 int ret;
427 char debugdir[PATH_MAX];
428
429 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
430
431 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
432 return -1;
433
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300434 ret = machine__cache_build_ids(&session->machines.host, debugdir);
Robert Richter1b549502011-12-07 10:02:53 +0100435
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300436 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
Robert Richter1b549502011-12-07 10:02:53 +0100437 struct machine *pos = rb_entry(nd, struct machine, rb_node);
438 ret |= machine__cache_build_ids(pos, debugdir);
439 }
440 return ret ? -1 : 0;
441}
442
443static bool machine__read_build_ids(struct machine *machine, bool with_hits)
444{
445 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
446 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
447 return ret;
448}
449
450static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
451{
452 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300453 bool ret = machine__read_build_ids(&session->machines.host, with_hits);
Robert Richter1b549502011-12-07 10:02:53 +0100454
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300455 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
Robert Richter1b549502011-12-07 10:02:53 +0100456 struct machine *pos = rb_entry(nd, struct machine, rb_node);
457 ret |= machine__read_build_ids(pos, with_hits);
458 }
459
460 return ret;
461}
462
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300463static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200464 struct perf_evlist *evlist)
465{
466 return read_tracing_data(fd, &evlist->entries);
467}
468
469
470static int write_build_id(int fd, struct perf_header *h,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300471 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200472{
473 struct perf_session *session;
474 int err;
475
476 session = container_of(h, struct perf_session, header);
477
Robert Richtere20960c2011-12-07 10:02:55 +0100478 if (!perf_session__read_build_ids(session, true))
479 return -1;
480
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200481 err = dsos__write_buildid_table(h, fd);
482 if (err < 0) {
483 pr_debug("failed to write buildid table\n");
484 return err;
485 }
486 if (!no_buildid_cache)
487 perf_session__cache_build_ids(session);
488
489 return 0;
490}
491
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300492static int write_hostname(int fd, struct perf_header *h __maybe_unused,
493 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200494{
495 struct utsname uts;
496 int ret;
497
498 ret = uname(&uts);
499 if (ret < 0)
500 return -1;
501
502 return do_write_string(fd, uts.nodename);
503}
504
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300505static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
506 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200507{
508 struct utsname uts;
509 int ret;
510
511 ret = uname(&uts);
512 if (ret < 0)
513 return -1;
514
515 return do_write_string(fd, uts.release);
516}
517
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300518static int write_arch(int fd, struct perf_header *h __maybe_unused,
519 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200520{
521 struct utsname uts;
522 int ret;
523
524 ret = uname(&uts);
525 if (ret < 0)
526 return -1;
527
528 return do_write_string(fd, uts.machine);
529}
530
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300531static int write_version(int fd, struct perf_header *h __maybe_unused,
532 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200533{
534 return do_write_string(fd, perf_version_string);
535}
536
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300537static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
538 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200539{
540#ifndef CPUINFO_PROC
541#define CPUINFO_PROC NULL
542#endif
543 FILE *file;
544 char *buf = NULL;
545 char *s, *p;
546 const char *search = CPUINFO_PROC;
547 size_t len = 0;
548 int ret = -1;
549
550 if (!search)
551 return -1;
552
553 file = fopen("/proc/cpuinfo", "r");
554 if (!file)
555 return -1;
556
557 while (getline(&buf, &len, file) > 0) {
558 ret = strncmp(buf, search, strlen(search));
559 if (!ret)
560 break;
561 }
562
563 if (ret)
564 goto done;
565
566 s = buf;
567
568 p = strchr(buf, ':');
569 if (p && *(p+1) == ' ' && *(p+2))
570 s = p + 2;
571 p = strchr(s, '\n');
572 if (p)
573 *p = '\0';
574
575 /* squash extra space characters (branding string) */
576 p = s;
577 while (*p) {
578 if (isspace(*p)) {
579 char *r = p + 1;
580 char *q = r;
581 *p = ' ';
582 while (*q && isspace(*q))
583 q++;
584 if (q != (p+1))
585 while ((*r++ = *q++));
586 }
587 p++;
588 }
589 ret = do_write_string(fd, s);
590done:
591 free(buf);
592 fclose(file);
593 return ret;
594}
595
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300596static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
597 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200598{
599 long nr;
600 u32 nrc, nra;
601 int ret;
602
603 nr = sysconf(_SC_NPROCESSORS_CONF);
604 if (nr < 0)
605 return -1;
606
607 nrc = (u32)(nr & UINT_MAX);
608
609 nr = sysconf(_SC_NPROCESSORS_ONLN);
610 if (nr < 0)
611 return -1;
612
613 nra = (u32)(nr & UINT_MAX);
614
615 ret = do_write(fd, &nrc, sizeof(nrc));
616 if (ret < 0)
617 return ret;
618
619 return do_write(fd, &nra, sizeof(nra));
620}
621
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300622static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200623 struct perf_evlist *evlist)
624{
Robert Richter6606f872012-08-16 21:10:19 +0200625 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900626 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200627 int ret;
628
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900629 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200630
631 /*
632 * write number of events
633 */
634 ret = do_write(fd, &nre, sizeof(nre));
635 if (ret < 0)
636 return ret;
637
638 /*
639 * size of perf_event_attr struct
640 */
Robert Richter6606f872012-08-16 21:10:19 +0200641 sz = (u32)sizeof(evsel->attr);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200642 ret = do_write(fd, &sz, sizeof(sz));
643 if (ret < 0)
644 return ret;
645
Robert Richter6606f872012-08-16 21:10:19 +0200646 list_for_each_entry(evsel, &evlist->entries, node) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200647
Robert Richter6606f872012-08-16 21:10:19 +0200648 ret = do_write(fd, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200649 if (ret < 0)
650 return ret;
651 /*
652 * write number of unique id per event
653 * there is one id per instance of an event
654 *
655 * copy into an nri to be independent of the
656 * type of ids,
657 */
Robert Richter6606f872012-08-16 21:10:19 +0200658 nri = evsel->ids;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200659 ret = do_write(fd, &nri, sizeof(nri));
660 if (ret < 0)
661 return ret;
662
663 /*
664 * write event string as passed on cmdline
665 */
Robert Richter6606f872012-08-16 21:10:19 +0200666 ret = do_write_string(fd, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200667 if (ret < 0)
668 return ret;
669 /*
670 * write unique ids for this event
671 */
Robert Richter6606f872012-08-16 21:10:19 +0200672 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200673 if (ret < 0)
674 return ret;
675 }
676 return 0;
677}
678
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300679static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
680 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200681{
682 char buf[MAXPATHLEN];
683 char proc[32];
684 u32 i, n;
685 int ret;
686
687 /*
688 * actual atual path to perf binary
689 */
690 sprintf(proc, "/proc/%d/exe", getpid());
691 ret = readlink(proc, buf, sizeof(buf));
692 if (ret <= 0)
693 return -1;
694
695 /* readlink() does not add null termination */
696 buf[ret] = '\0';
697
698 /* account for binary path */
699 n = header_argc + 1;
700
701 ret = do_write(fd, &n, sizeof(n));
702 if (ret < 0)
703 return ret;
704
705 ret = do_write_string(fd, buf);
706 if (ret < 0)
707 return ret;
708
709 for (i = 0 ; i < header_argc; i++) {
710 ret = do_write_string(fd, header_argv[i]);
711 if (ret < 0)
712 return ret;
713 }
714 return 0;
715}
716
717#define CORE_SIB_FMT \
718 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
719#define THRD_SIB_FMT \
720 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
721
722struct cpu_topo {
723 u32 core_sib;
724 u32 thread_sib;
725 char **core_siblings;
726 char **thread_siblings;
727};
728
729static int build_cpu_topo(struct cpu_topo *tp, int cpu)
730{
731 FILE *fp;
732 char filename[MAXPATHLEN];
733 char *buf = NULL, *p;
734 size_t len = 0;
Stephane Eranianc5885742013-08-14 12:04:26 +0200735 ssize_t sret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200736 u32 i = 0;
737 int ret = -1;
738
739 sprintf(filename, CORE_SIB_FMT, cpu);
740 fp = fopen(filename, "r");
741 if (!fp)
Stephane Eranianc5885742013-08-14 12:04:26 +0200742 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200743
Stephane Eranianc5885742013-08-14 12:04:26 +0200744 sret = getline(&buf, &len, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200745 fclose(fp);
Stephane Eranianc5885742013-08-14 12:04:26 +0200746 if (sret <= 0)
747 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200748
749 p = strchr(buf, '\n');
750 if (p)
751 *p = '\0';
752
753 for (i = 0; i < tp->core_sib; i++) {
754 if (!strcmp(buf, tp->core_siblings[i]))
755 break;
756 }
757 if (i == tp->core_sib) {
758 tp->core_siblings[i] = buf;
759 tp->core_sib++;
760 buf = NULL;
761 len = 0;
762 }
Stephane Eranianc5885742013-08-14 12:04:26 +0200763 ret = 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200764
Stephane Eranianc5885742013-08-14 12:04:26 +0200765try_threads:
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200766 sprintf(filename, THRD_SIB_FMT, cpu);
767 fp = fopen(filename, "r");
768 if (!fp)
769 goto done;
770
771 if (getline(&buf, &len, fp) <= 0)
772 goto done;
773
774 p = strchr(buf, '\n');
775 if (p)
776 *p = '\0';
777
778 for (i = 0; i < tp->thread_sib; i++) {
779 if (!strcmp(buf, tp->thread_siblings[i]))
780 break;
781 }
782 if (i == tp->thread_sib) {
783 tp->thread_siblings[i] = buf;
784 tp->thread_sib++;
785 buf = NULL;
786 }
787 ret = 0;
788done:
789 if(fp)
790 fclose(fp);
791 free(buf);
792 return ret;
793}
794
795static void free_cpu_topo(struct cpu_topo *tp)
796{
797 u32 i;
798
799 if (!tp)
800 return;
801
802 for (i = 0 ; i < tp->core_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300803 zfree(&tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200804
805 for (i = 0 ; i < tp->thread_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300806 zfree(&tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200807
808 free(tp);
809}
810
811static struct cpu_topo *build_cpu_topology(void)
812{
813 struct cpu_topo *tp;
814 void *addr;
815 u32 nr, i;
816 size_t sz;
817 long ncpus;
818 int ret = -1;
819
820 ncpus = sysconf(_SC_NPROCESSORS_CONF);
821 if (ncpus < 0)
822 return NULL;
823
824 nr = (u32)(ncpus & UINT_MAX);
825
826 sz = nr * sizeof(char *);
827
828 addr = calloc(1, sizeof(*tp) + 2 * sz);
829 if (!addr)
830 return NULL;
831
832 tp = addr;
833
834 addr += sizeof(*tp);
835 tp->core_siblings = addr;
836 addr += sz;
837 tp->thread_siblings = addr;
838
839 for (i = 0; i < nr; i++) {
840 ret = build_cpu_topo(tp, i);
841 if (ret < 0)
842 break;
843 }
844 if (ret) {
845 free_cpu_topo(tp);
846 tp = NULL;
847 }
848 return tp;
849}
850
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300851static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
852 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200853{
854 struct cpu_topo *tp;
855 u32 i;
856 int ret;
857
858 tp = build_cpu_topology();
859 if (!tp)
860 return -1;
861
862 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
863 if (ret < 0)
864 goto done;
865
866 for (i = 0; i < tp->core_sib; i++) {
867 ret = do_write_string(fd, tp->core_siblings[i]);
868 if (ret < 0)
869 goto done;
870 }
871 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
872 if (ret < 0)
873 goto done;
874
875 for (i = 0; i < tp->thread_sib; i++) {
876 ret = do_write_string(fd, tp->thread_siblings[i]);
877 if (ret < 0)
878 break;
879 }
880done:
881 free_cpu_topo(tp);
882 return ret;
883}
884
885
886
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300887static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
888 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200889{
890 char *buf = NULL;
891 FILE *fp;
892 size_t len = 0;
893 int ret = -1, n;
894 uint64_t mem;
895
896 fp = fopen("/proc/meminfo", "r");
897 if (!fp)
898 return -1;
899
900 while (getline(&buf, &len, fp) > 0) {
901 ret = strncmp(buf, "MemTotal:", 9);
902 if (!ret)
903 break;
904 }
905 if (!ret) {
906 n = sscanf(buf, "%*s %"PRIu64, &mem);
907 if (n == 1)
908 ret = do_write(fd, &mem, sizeof(mem));
909 }
910 free(buf);
911 fclose(fp);
912 return ret;
913}
914
915static int write_topo_node(int fd, int node)
916{
917 char str[MAXPATHLEN];
918 char field[32];
919 char *buf = NULL, *p;
920 size_t len = 0;
921 FILE *fp;
922 u64 mem_total, mem_free, mem;
923 int ret = -1;
924
925 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
926 fp = fopen(str, "r");
927 if (!fp)
928 return -1;
929
930 while (getline(&buf, &len, fp) > 0) {
931 /* skip over invalid lines */
932 if (!strchr(buf, ':'))
933 continue;
934 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
935 goto done;
936 if (!strcmp(field, "MemTotal:"))
937 mem_total = mem;
938 if (!strcmp(field, "MemFree:"))
939 mem_free = mem;
940 }
941
942 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100943 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200944
945 ret = do_write(fd, &mem_total, sizeof(u64));
946 if (ret)
947 goto done;
948
949 ret = do_write(fd, &mem_free, sizeof(u64));
950 if (ret)
951 goto done;
952
953 ret = -1;
954 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
955
956 fp = fopen(str, "r");
957 if (!fp)
958 goto done;
959
960 if (getline(&buf, &len, fp) <= 0)
961 goto done;
962
963 p = strchr(buf, '\n');
964 if (p)
965 *p = '\0';
966
967 ret = do_write_string(fd, buf);
968done:
969 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100970 if (fp)
971 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200972 return ret;
973}
974
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300975static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
976 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200977{
978 char *buf = NULL;
979 size_t len = 0;
980 FILE *fp;
981 struct cpu_map *node_map = NULL;
982 char *c;
983 u32 nr, i, j;
984 int ret = -1;
985
986 fp = fopen("/sys/devices/system/node/online", "r");
987 if (!fp)
988 return -1;
989
990 if (getline(&buf, &len, fp) <= 0)
991 goto done;
992
993 c = strchr(buf, '\n');
994 if (c)
995 *c = '\0';
996
997 node_map = cpu_map__new(buf);
998 if (!node_map)
999 goto done;
1000
1001 nr = (u32)node_map->nr;
1002
1003 ret = do_write(fd, &nr, sizeof(nr));
1004 if (ret < 0)
1005 goto done;
1006
1007 for (i = 0; i < nr; i++) {
1008 j = (u32)node_map->map[i];
1009 ret = do_write(fd, &j, sizeof(j));
1010 if (ret < 0)
1011 break;
1012
1013 ret = write_topo_node(fd, i);
1014 if (ret < 0)
1015 break;
1016 }
1017done:
1018 free(buf);
1019 fclose(fp);
1020 free(node_map);
1021 return ret;
1022}
1023
1024/*
Robert Richter50a96672012-08-16 21:10:24 +02001025 * File format:
1026 *
1027 * struct pmu_mappings {
1028 * u32 pmu_num;
1029 * struct pmu_map {
1030 * u32 type;
1031 * char name[];
1032 * }[pmu_num];
1033 * };
1034 */
1035
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001036static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
1037 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +02001038{
1039 struct perf_pmu *pmu = NULL;
1040 off_t offset = lseek(fd, 0, SEEK_CUR);
1041 __u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +09001042 int ret;
Robert Richter50a96672012-08-16 21:10:24 +02001043
1044 /* write real pmu_num later */
Namhyung Kim5323f602012-12-17 15:38:54 +09001045 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
1046 if (ret < 0)
1047 return ret;
Robert Richter50a96672012-08-16 21:10:24 +02001048
1049 while ((pmu = perf_pmu__scan(pmu))) {
1050 if (!pmu->name)
1051 continue;
1052 pmu_num++;
Namhyung Kim5323f602012-12-17 15:38:54 +09001053
1054 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
1055 if (ret < 0)
1056 return ret;
1057
1058 ret = do_write_string(fd, pmu->name);
1059 if (ret < 0)
1060 return ret;
Robert Richter50a96672012-08-16 21:10:24 +02001061 }
1062
1063 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1064 /* discard all */
1065 lseek(fd, offset, SEEK_SET);
1066 return -1;
1067 }
1068
1069 return 0;
1070}
1071
1072/*
Namhyung Kima8bb5592013-01-22 18:09:31 +09001073 * File format:
1074 *
1075 * struct group_descs {
1076 * u32 nr_groups;
1077 * struct group_desc {
1078 * char name[];
1079 * u32 leader_idx;
1080 * u32 nr_members;
1081 * }[nr_groups];
1082 * };
1083 */
1084static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
1085 struct perf_evlist *evlist)
1086{
1087 u32 nr_groups = evlist->nr_groups;
1088 struct perf_evsel *evsel;
1089 int ret;
1090
1091 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
1092 if (ret < 0)
1093 return ret;
1094
1095 list_for_each_entry(evsel, &evlist->entries, node) {
1096 if (perf_evsel__is_group_leader(evsel) &&
1097 evsel->nr_members > 1) {
1098 const char *name = evsel->group_name ?: "{anon_group}";
1099 u32 leader_idx = evsel->idx;
1100 u32 nr_members = evsel->nr_members;
1101
1102 ret = do_write_string(fd, name);
1103 if (ret < 0)
1104 return ret;
1105
1106 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
1107 if (ret < 0)
1108 return ret;
1109
1110 ret = do_write(fd, &nr_members, sizeof(nr_members));
1111 if (ret < 0)
1112 return ret;
1113 }
1114 }
1115 return 0;
1116}
1117
1118/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001119 * default get_cpuid(): nothing gets recorded
1120 * actual implementation must be in arch/$(ARCH)/util/header.c
1121 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001122int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
1123 size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001124{
1125 return -1;
1126}
1127
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001128static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
1129 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001130{
1131 char buffer[64];
1132 int ret;
1133
1134 ret = get_cpuid(buffer, sizeof(buffer));
1135 if (!ret)
1136 goto write_it;
1137
1138 return -1;
1139write_it:
1140 return do_write_string(fd, buffer);
1141}
1142
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001143static int write_branch_stack(int fd __maybe_unused,
1144 struct perf_header *h __maybe_unused,
1145 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +01001146{
1147 return 0;
1148}
1149
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001150static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1151 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001152{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001153 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001154}
1155
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001156static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1157 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001158{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001159 fprintf(fp, "# os release : %s\n", ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001160}
1161
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001162static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001163{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001164 fprintf(fp, "# arch : %s\n", ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001165}
1166
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001167static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1168 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001169{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001170 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001171}
1172
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001173static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1174 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001175{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001176 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1177 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001178}
1179
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001180static void print_version(struct perf_header *ph, int fd __maybe_unused,
1181 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001182{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001183 fprintf(fp, "# perf version : %s\n", ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001184}
1185
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001186static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1187 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001188{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001189 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001190 char *str;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001191
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001192 nr = ph->env.nr_cmdline;
1193 str = ph->env.cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001194
1195 fprintf(fp, "# cmdline : ");
1196
1197 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001198 fprintf(fp, "%s ", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001199 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001200 }
1201 fputc('\n', fp);
1202}
1203
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001204static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1205 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001206{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001207 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001208 char *str;
1209
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001210 nr = ph->env.nr_sibling_cores;
1211 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001212
1213 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001214 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001215 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001216 }
1217
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001218 nr = ph->env.nr_sibling_threads;
1219 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001220
1221 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001222 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001223 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001224 }
1225}
1226
Robert Richter4e1b9c62012-08-16 21:10:22 +02001227static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001228{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001229 struct perf_evsel *evsel;
1230
1231 if (!events)
1232 return;
1233
1234 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001235 zfree(&evsel->name);
1236 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001237 }
1238
1239 free(events);
1240}
1241
1242static struct perf_evsel *
1243read_event_desc(struct perf_header *ph, int fd)
1244{
1245 struct perf_evsel *evsel, *events = NULL;
1246 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001247 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001248 u32 nre, sz, nr, i, j;
1249 ssize_t ret;
1250 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001251
1252 /* number of events */
Namhyung Kim5323f602012-12-17 15:38:54 +09001253 ret = readn(fd, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001254 if (ret != (ssize_t)sizeof(nre))
1255 goto error;
1256
1257 if (ph->needs_swap)
1258 nre = bswap_32(nre);
1259
Namhyung Kim5323f602012-12-17 15:38:54 +09001260 ret = readn(fd, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001261 if (ret != (ssize_t)sizeof(sz))
1262 goto error;
1263
1264 if (ph->needs_swap)
1265 sz = bswap_32(sz);
1266
Stephane Eranian62db9062012-02-09 23:21:07 +01001267 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001268 buf = malloc(sz);
1269 if (!buf)
1270 goto error;
1271
Robert Richter4e1b9c62012-08-16 21:10:22 +02001272 /* the last event terminates with evsel->attr.size == 0: */
1273 events = calloc(nre + 1, sizeof(*events));
1274 if (!events)
1275 goto error;
1276
1277 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001278 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001279 msz = sz;
1280
Robert Richter4e1b9c62012-08-16 21:10:22 +02001281 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1282 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001283
Stephane Eranian62db9062012-02-09 23:21:07 +01001284 /*
1285 * must read entire on-file attr struct to
1286 * sync up with layout.
1287 */
Namhyung Kim5323f602012-12-17 15:38:54 +09001288 ret = readn(fd, buf, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001289 if (ret != (ssize_t)sz)
1290 goto error;
1291
1292 if (ph->needs_swap)
1293 perf_event__attr_swap(buf);
1294
Robert Richter4e1b9c62012-08-16 21:10:22 +02001295 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001296
Namhyung Kim5323f602012-12-17 15:38:54 +09001297 ret = readn(fd, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001298 if (ret != (ssize_t)sizeof(nr))
1299 goto error;
1300
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001301 if (ph->needs_swap) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001302 nr = bswap_32(nr);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001303 evsel->needs_swap = true;
1304 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001305
Robert Richter4e1b9c62012-08-16 21:10:22 +02001306 evsel->name = do_read_string(fd, ph);
1307
1308 if (!nr)
1309 continue;
1310
1311 id = calloc(nr, sizeof(*id));
1312 if (!id)
1313 goto error;
1314 evsel->ids = nr;
1315 evsel->id = id;
1316
1317 for (j = 0 ; j < nr; j++) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001318 ret = readn(fd, id, sizeof(*id));
Robert Richter4e1b9c62012-08-16 21:10:22 +02001319 if (ret != (ssize_t)sizeof(*id))
1320 goto error;
1321 if (ph->needs_swap)
1322 *id = bswap_64(*id);
1323 id++;
1324 }
1325 }
1326out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001327 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001328 return events;
1329error:
1330 if (events)
1331 free_event_desc(events);
1332 events = NULL;
1333 goto out;
1334}
1335
1336static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1337{
1338 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1339 u32 j;
1340 u64 *id;
1341
1342 if (!events) {
1343 fprintf(fp, "# event desc: not available or unable to read\n");
1344 return;
1345 }
1346
1347 for (evsel = events; evsel->attr.size; evsel++) {
1348 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001349
1350 fprintf(fp, "type = %d, config = 0x%"PRIx64
1351 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
Robert Richter4e1b9c62012-08-16 21:10:22 +02001352 evsel->attr.type,
1353 (u64)evsel->attr.config,
1354 (u64)evsel->attr.config1,
1355 (u64)evsel->attr.config2);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001356
1357 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001358 evsel->attr.exclude_user,
1359 evsel->attr.exclude_kernel);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001360
David Ahern78b961f2012-07-20 17:25:52 -06001361 fprintf(fp, ", excl_host = %d, excl_guest = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001362 evsel->attr.exclude_host,
1363 evsel->attr.exclude_guest);
David Ahern78b961f2012-07-20 17:25:52 -06001364
Robert Richter4e1b9c62012-08-16 21:10:22 +02001365 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
David Ahern78b961f2012-07-20 17:25:52 -06001366
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001367 fprintf(fp, ", attr_mmap2 = %d", evsel->attr.mmap2);
1368 fprintf(fp, ", attr_mmap = %d", evsel->attr.mmap);
1369 fprintf(fp, ", attr_mmap_data = %d", evsel->attr.mmap_data);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001370 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001371 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001372 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1373 if (j)
1374 fputc(',', fp);
1375 fprintf(fp, " %"PRIu64, *id);
1376 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001377 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001378 }
1379
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001380 fputc('\n', fp);
1381 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001382
1383 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001384}
1385
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001386static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001387 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001388{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001389 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001390}
1391
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001392static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001393 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001394{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001395 u32 nr, c, i;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001396 char *str, *tmp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001397 uint64_t mem_total, mem_free;
1398
1399 /* nr nodes */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001400 nr = ph->env.nr_numa_nodes;
1401 str = ph->env.numa_nodes;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001402
1403 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001404 /* node number */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001405 c = strtoul(str, &tmp, 0);
1406 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001407 goto error;
1408
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001409 str = tmp + 1;
1410 mem_total = strtoull(str, &tmp, 0);
1411 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001412 goto error;
1413
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001414 str = tmp + 1;
1415 mem_free = strtoull(str, &tmp, 0);
1416 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001417 goto error;
1418
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001419 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1420 " free = %"PRIu64" kB\n",
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001421 c, mem_total, mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001422
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001423 str = tmp + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001424 fprintf(fp, "# node%u cpu list : %s\n", c, str);
Namhyung Kim12344712012-10-23 22:44:49 +09001425
1426 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001427 }
1428 return;
1429error:
1430 fprintf(fp, "# numa topology : not available\n");
1431}
1432
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001433static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001434{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001435 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001436}
1437
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001438static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001439 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001440{
1441 fprintf(fp, "# contains samples with branch stack\n");
1442}
1443
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001444static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1445 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001446{
1447 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001448 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001449 u32 pmu_num;
1450 u32 type;
1451
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001452 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001453 if (!pmu_num) {
1454 fprintf(fp, "# pmu mappings: not available\n");
1455 return;
1456 }
1457
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001458 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001459
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001460 while (pmu_num) {
1461 type = strtoul(str, &tmp, 0);
1462 if (*tmp != ':')
1463 goto error;
1464
1465 str = tmp + 1;
1466 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1467
Robert Richter50a96672012-08-16 21:10:24 +02001468 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001469 str += strlen(str) + 1;
1470 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001471 }
1472
1473 fprintf(fp, "\n");
1474
1475 if (!pmu_num)
1476 return;
1477error:
1478 fprintf(fp, "# pmu mappings: unable to read\n");
1479}
1480
Namhyung Kima8bb5592013-01-22 18:09:31 +09001481static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1482 FILE *fp)
1483{
1484 struct perf_session *session;
1485 struct perf_evsel *evsel;
1486 u32 nr = 0;
1487
1488 session = container_of(ph, struct perf_session, header);
1489
1490 list_for_each_entry(evsel, &session->evlist->entries, node) {
1491 if (perf_evsel__is_group_leader(evsel) &&
1492 evsel->nr_members > 1) {
1493 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1494 perf_evsel__name(evsel));
1495
1496 nr = evsel->nr_members - 1;
1497 } else if (nr) {
1498 fprintf(fp, ",%s", perf_evsel__name(evsel));
1499
1500 if (--nr == 0)
1501 fprintf(fp, "}\n");
1502 }
1503 }
1504}
1505
Robert Richter08d95bd2012-02-10 15:41:55 +01001506static int __event_process_build_id(struct build_id_event *bev,
1507 char *filename,
1508 struct perf_session *session)
1509{
1510 int err = -1;
1511 struct list_head *head;
1512 struct machine *machine;
1513 u16 misc;
1514 struct dso *dso;
1515 enum dso_kernel_type dso_type;
1516
1517 machine = perf_session__findnew_machine(session, bev->pid);
1518 if (!machine)
1519 goto out;
1520
1521 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1522
1523 switch (misc) {
1524 case PERF_RECORD_MISC_KERNEL:
1525 dso_type = DSO_TYPE_KERNEL;
1526 head = &machine->kernel_dsos;
1527 break;
1528 case PERF_RECORD_MISC_GUEST_KERNEL:
1529 dso_type = DSO_TYPE_GUEST_KERNEL;
1530 head = &machine->kernel_dsos;
1531 break;
1532 case PERF_RECORD_MISC_USER:
1533 case PERF_RECORD_MISC_GUEST_USER:
1534 dso_type = DSO_TYPE_USER;
1535 head = &machine->user_dsos;
1536 break;
1537 default:
1538 goto out;
1539 }
1540
1541 dso = __dsos__findnew(head, filename);
1542 if (dso != NULL) {
1543 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1544
1545 dso__set_build_id(dso, &bev->build_id);
1546
1547 if (filename[0] == '[')
1548 dso->kernel = dso_type;
1549
1550 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1551 sbuild_id);
1552 pr_debug("build id event received for %s: %s\n",
1553 dso->long_name, sbuild_id);
1554 }
1555
1556 err = 0;
1557out:
1558 return err;
1559}
1560
1561static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1562 int input, u64 offset, u64 size)
1563{
1564 struct perf_session *session = container_of(header, struct perf_session, header);
1565 struct {
1566 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001567 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001568 char filename[0];
1569 } old_bev;
1570 struct build_id_event bev;
1571 char filename[PATH_MAX];
1572 u64 limit = offset + size;
1573
1574 while (offset < limit) {
1575 ssize_t len;
1576
Namhyung Kim5323f602012-12-17 15:38:54 +09001577 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001578 return -1;
1579
1580 if (header->needs_swap)
1581 perf_event_header__bswap(&old_bev.header);
1582
1583 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001584 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001585 return -1;
1586
1587 bev.header = old_bev.header;
1588
1589 /*
1590 * As the pid is the missing value, we need to fill
1591 * it properly. The header.misc value give us nice hint.
1592 */
1593 bev.pid = HOST_KERNEL_ID;
1594 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1595 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1596 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1597
1598 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1599 __event_process_build_id(&bev, filename, session);
1600
1601 offset += bev.header.size;
1602 }
1603
1604 return 0;
1605}
1606
1607static int perf_header__read_build_ids(struct perf_header *header,
1608 int input, u64 offset, u64 size)
1609{
1610 struct perf_session *session = container_of(header, struct perf_session, header);
1611 struct build_id_event bev;
1612 char filename[PATH_MAX];
1613 u64 limit = offset + size, orig_offset = offset;
1614 int err = -1;
1615
1616 while (offset < limit) {
1617 ssize_t len;
1618
Namhyung Kim5323f602012-12-17 15:38:54 +09001619 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001620 goto out;
1621
1622 if (header->needs_swap)
1623 perf_event_header__bswap(&bev.header);
1624
1625 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001626 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001627 goto out;
1628 /*
1629 * The a1645ce1 changeset:
1630 *
1631 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1632 *
1633 * Added a field to struct build_id_event that broke the file
1634 * format.
1635 *
1636 * Since the kernel build-id is the first entry, process the
1637 * table using the old format if the well known
1638 * '[kernel.kallsyms]' string for the kernel build-id has the
1639 * first 4 characters chopped off (where the pid_t sits).
1640 */
1641 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1642 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1643 return -1;
1644 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1645 }
1646
1647 __event_process_build_id(&bev, filename, session);
1648
1649 offset += bev.header.size;
1650 }
1651 err = 0;
1652out:
1653 return err;
1654}
1655
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001656static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1657 struct perf_header *ph __maybe_unused,
1658 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001659{
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001660 ssize_t ret = trace_report(fd, data, false);
1661 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001662}
1663
1664static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001665 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001666 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001667{
1668 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1669 pr_debug("Failed to read buildids, continuing...\n");
1670 return 0;
1671}
1672
Namhyung Kima1ae5652012-09-24 17:14:59 +09001673static int process_hostname(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001674 struct perf_header *ph, int fd,
1675 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001676{
1677 ph->env.hostname = do_read_string(fd, ph);
1678 return ph->env.hostname ? 0 : -ENOMEM;
1679}
1680
1681static int process_osrelease(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001682 struct perf_header *ph, int fd,
1683 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001684{
1685 ph->env.os_release = do_read_string(fd, ph);
1686 return ph->env.os_release ? 0 : -ENOMEM;
1687}
1688
1689static int process_version(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001690 struct perf_header *ph, int fd,
1691 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001692{
1693 ph->env.version = do_read_string(fd, ph);
1694 return ph->env.version ? 0 : -ENOMEM;
1695}
1696
1697static int process_arch(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001698 struct perf_header *ph, int fd,
1699 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001700{
1701 ph->env.arch = do_read_string(fd, ph);
1702 return ph->env.arch ? 0 : -ENOMEM;
1703}
1704
1705static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001706 struct perf_header *ph, int fd,
1707 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001708{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001709 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001710 u32 nr;
1711
Namhyung Kim5323f602012-12-17 15:38:54 +09001712 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001713 if (ret != sizeof(nr))
1714 return -1;
1715
1716 if (ph->needs_swap)
1717 nr = bswap_32(nr);
1718
1719 ph->env.nr_cpus_online = nr;
1720
Namhyung Kim5323f602012-12-17 15:38:54 +09001721 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001722 if (ret != sizeof(nr))
1723 return -1;
1724
1725 if (ph->needs_swap)
1726 nr = bswap_32(nr);
1727
1728 ph->env.nr_cpus_avail = nr;
1729 return 0;
1730}
1731
1732static int process_cpudesc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001733 struct perf_header *ph, int fd,
1734 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001735{
1736 ph->env.cpu_desc = do_read_string(fd, ph);
1737 return ph->env.cpu_desc ? 0 : -ENOMEM;
1738}
1739
1740static int process_cpuid(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001741 struct perf_header *ph, int fd,
1742 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001743{
1744 ph->env.cpuid = do_read_string(fd, ph);
1745 return ph->env.cpuid ? 0 : -ENOMEM;
1746}
1747
1748static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001749 struct perf_header *ph, int fd,
1750 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001751{
1752 uint64_t mem;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001753 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001754
Namhyung Kim5323f602012-12-17 15:38:54 +09001755 ret = readn(fd, &mem, sizeof(mem));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001756 if (ret != sizeof(mem))
1757 return -1;
1758
1759 if (ph->needs_swap)
1760 mem = bswap_64(mem);
1761
1762 ph->env.total_mem = mem;
1763 return 0;
1764}
1765
Robert Richter7c2f7af2012-08-16 21:10:23 +02001766static struct perf_evsel *
1767perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1768{
1769 struct perf_evsel *evsel;
1770
1771 list_for_each_entry(evsel, &evlist->entries, node) {
1772 if (evsel->idx == idx)
1773 return evsel;
1774 }
1775
1776 return NULL;
1777}
1778
1779static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001780perf_evlist__set_event_name(struct perf_evlist *evlist,
1781 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001782{
1783 struct perf_evsel *evsel;
1784
1785 if (!event->name)
1786 return;
1787
1788 evsel = perf_evlist__find_by_index(evlist, event->idx);
1789 if (!evsel)
1790 return;
1791
1792 if (evsel->name)
1793 return;
1794
1795 evsel->name = strdup(event->name);
1796}
1797
1798static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001799process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001800 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001801 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001802{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001803 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001804 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1805
1806 if (!events)
1807 return 0;
1808
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001809 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001810 for (evsel = events; evsel->attr.size; evsel++)
1811 perf_evlist__set_event_name(session->evlist, evsel);
1812
1813 free_event_desc(events);
1814
1815 return 0;
1816}
1817
Namhyung Kima1ae5652012-09-24 17:14:59 +09001818static int process_cmdline(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001819 struct perf_header *ph, int fd,
1820 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001821{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001822 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001823 char *str;
1824 u32 nr, i;
1825 struct strbuf sb;
1826
Namhyung Kim5323f602012-12-17 15:38:54 +09001827 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001828 if (ret != sizeof(nr))
1829 return -1;
1830
1831 if (ph->needs_swap)
1832 nr = bswap_32(nr);
1833
1834 ph->env.nr_cmdline = nr;
1835 strbuf_init(&sb, 128);
1836
1837 for (i = 0; i < nr; i++) {
1838 str = do_read_string(fd, ph);
1839 if (!str)
1840 goto error;
1841
1842 /* include a NULL character at the end */
1843 strbuf_add(&sb, str, strlen(str) + 1);
1844 free(str);
1845 }
1846 ph->env.cmdline = strbuf_detach(&sb, NULL);
1847 return 0;
1848
1849error:
1850 strbuf_release(&sb);
1851 return -1;
1852}
1853
1854static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001855 struct perf_header *ph, int fd,
1856 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001857{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001858 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001859 u32 nr, i;
1860 char *str;
1861 struct strbuf sb;
1862
Namhyung Kim5323f602012-12-17 15:38:54 +09001863 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001864 if (ret != sizeof(nr))
1865 return -1;
1866
1867 if (ph->needs_swap)
1868 nr = bswap_32(nr);
1869
1870 ph->env.nr_sibling_cores = nr;
1871 strbuf_init(&sb, 128);
1872
1873 for (i = 0; i < nr; i++) {
1874 str = do_read_string(fd, ph);
1875 if (!str)
1876 goto error;
1877
1878 /* include a NULL character at the end */
1879 strbuf_add(&sb, str, strlen(str) + 1);
1880 free(str);
1881 }
1882 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1883
Namhyung Kim5323f602012-12-17 15:38:54 +09001884 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001885 if (ret != sizeof(nr))
1886 return -1;
1887
1888 if (ph->needs_swap)
1889 nr = bswap_32(nr);
1890
1891 ph->env.nr_sibling_threads = nr;
1892
1893 for (i = 0; i < nr; i++) {
1894 str = do_read_string(fd, ph);
1895 if (!str)
1896 goto error;
1897
1898 /* include a NULL character at the end */
1899 strbuf_add(&sb, str, strlen(str) + 1);
1900 free(str);
1901 }
1902 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1903 return 0;
1904
1905error:
1906 strbuf_release(&sb);
1907 return -1;
1908}
1909
1910static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001911 struct perf_header *ph, int fd,
1912 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001913{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001914 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001915 u32 nr, node, i;
1916 char *str;
1917 uint64_t mem_total, mem_free;
1918 struct strbuf sb;
1919
1920 /* nr nodes */
Namhyung Kim5323f602012-12-17 15:38:54 +09001921 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001922 if (ret != sizeof(nr))
1923 goto error;
1924
1925 if (ph->needs_swap)
1926 nr = bswap_32(nr);
1927
1928 ph->env.nr_numa_nodes = nr;
1929 strbuf_init(&sb, 256);
1930
1931 for (i = 0; i < nr; i++) {
1932 /* node number */
Namhyung Kim5323f602012-12-17 15:38:54 +09001933 ret = readn(fd, &node, sizeof(node));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001934 if (ret != sizeof(node))
1935 goto error;
1936
Namhyung Kim5323f602012-12-17 15:38:54 +09001937 ret = readn(fd, &mem_total, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001938 if (ret != sizeof(u64))
1939 goto error;
1940
Namhyung Kim5323f602012-12-17 15:38:54 +09001941 ret = readn(fd, &mem_free, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001942 if (ret != sizeof(u64))
1943 goto error;
1944
1945 if (ph->needs_swap) {
1946 node = bswap_32(node);
1947 mem_total = bswap_64(mem_total);
1948 mem_free = bswap_64(mem_free);
1949 }
1950
1951 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1952 node, mem_total, mem_free);
1953
1954 str = do_read_string(fd, ph);
1955 if (!str)
1956 goto error;
1957
1958 /* include a NULL character at the end */
1959 strbuf_add(&sb, str, strlen(str) + 1);
1960 free(str);
1961 }
1962 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1963 return 0;
1964
1965error:
1966 strbuf_release(&sb);
1967 return -1;
1968}
1969
1970static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001971 struct perf_header *ph, int fd,
1972 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001973{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001974 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001975 char *name;
1976 u32 pmu_num;
1977 u32 type;
1978 struct strbuf sb;
1979
Namhyung Kim5323f602012-12-17 15:38:54 +09001980 ret = readn(fd, &pmu_num, sizeof(pmu_num));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001981 if (ret != sizeof(pmu_num))
1982 return -1;
1983
1984 if (ph->needs_swap)
1985 pmu_num = bswap_32(pmu_num);
1986
1987 if (!pmu_num) {
1988 pr_debug("pmu mappings not available\n");
1989 return 0;
1990 }
1991
1992 ph->env.nr_pmu_mappings = pmu_num;
1993 strbuf_init(&sb, 128);
1994
1995 while (pmu_num) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001996 if (readn(fd, &type, sizeof(type)) != sizeof(type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001997 goto error;
1998 if (ph->needs_swap)
1999 type = bswap_32(type);
2000
2001 name = do_read_string(fd, ph);
2002 if (!name)
2003 goto error;
2004
2005 strbuf_addf(&sb, "%u:%s", type, name);
2006 /* include a NULL character at the end */
2007 strbuf_add(&sb, "", 1);
2008
2009 free(name);
2010 pmu_num--;
2011 }
2012 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2013 return 0;
2014
2015error:
2016 strbuf_release(&sb);
2017 return -1;
2018}
2019
Namhyung Kima8bb5592013-01-22 18:09:31 +09002020static int process_group_desc(struct perf_file_section *section __maybe_unused,
2021 struct perf_header *ph, int fd,
2022 void *data __maybe_unused)
2023{
2024 size_t ret = -1;
2025 u32 i, nr, nr_groups;
2026 struct perf_session *session;
2027 struct perf_evsel *evsel, *leader = NULL;
2028 struct group_desc {
2029 char *name;
2030 u32 leader_idx;
2031 u32 nr_members;
2032 } *desc;
2033
2034 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
2035 return -1;
2036
2037 if (ph->needs_swap)
2038 nr_groups = bswap_32(nr_groups);
2039
2040 ph->env.nr_groups = nr_groups;
2041 if (!nr_groups) {
2042 pr_debug("group desc not available\n");
2043 return 0;
2044 }
2045
2046 desc = calloc(nr_groups, sizeof(*desc));
2047 if (!desc)
2048 return -1;
2049
2050 for (i = 0; i < nr_groups; i++) {
2051 desc[i].name = do_read_string(fd, ph);
2052 if (!desc[i].name)
2053 goto out_free;
2054
2055 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
2056 goto out_free;
2057
2058 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
2059 goto out_free;
2060
2061 if (ph->needs_swap) {
2062 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
2063 desc[i].nr_members = bswap_32(desc[i].nr_members);
2064 }
2065 }
2066
2067 /*
2068 * Rebuild group relationship based on the group_desc
2069 */
2070 session = container_of(ph, struct perf_session, header);
2071 session->evlist->nr_groups = nr_groups;
2072
2073 i = nr = 0;
2074 list_for_each_entry(evsel, &session->evlist->entries, node) {
2075 if (evsel->idx == (int) desc[i].leader_idx) {
2076 evsel->leader = evsel;
2077 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09002078 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002079 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09002080 desc[i].name = NULL;
2081 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09002082 evsel->nr_members = desc[i].nr_members;
2083
2084 if (i >= nr_groups || nr > 0) {
2085 pr_debug("invalid group desc\n");
2086 goto out_free;
2087 }
2088
2089 leader = evsel;
2090 nr = evsel->nr_members - 1;
2091 i++;
2092 } else if (nr) {
2093 /* This is a group member */
2094 evsel->leader = leader;
2095
2096 nr--;
2097 }
2098 }
2099
2100 if (i != nr_groups || nr != 0) {
2101 pr_debug("invalid group desc\n");
2102 goto out_free;
2103 }
2104
2105 ret = 0;
2106out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002107 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002108 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002109 free(desc);
2110
2111 return ret;
2112}
2113
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002114struct feature_ops {
2115 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2116 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01002117 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002118 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002119 const char *name;
2120 bool full_only;
2121};
2122
Robert Richter8cdfa782011-12-07 10:02:56 +01002123#define FEAT_OPA(n, func) \
2124 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01002125#define FEAT_OPP(n, func) \
2126 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2127 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01002128#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01002129 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09002130 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01002131
2132/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002133#define print_tracing_data NULL
2134#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002135
2136static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002137 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01002138 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002139 FEAT_OPP(HEADER_HOSTNAME, hostname),
2140 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2141 FEAT_OPP(HEADER_VERSION, version),
2142 FEAT_OPP(HEADER_ARCH, arch),
2143 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2144 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09002145 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002146 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02002147 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002148 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01002149 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2150 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01002151 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002152 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09002153 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002154};
2155
2156struct header_print_data {
2157 FILE *fp;
2158 bool full; /* extended list of headers */
2159};
2160
2161static int perf_file_section__fprintf_info(struct perf_file_section *section,
2162 struct perf_header *ph,
2163 int feat, int fd, void *data)
2164{
2165 struct header_print_data *hd = data;
2166
2167 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2168 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2169 "%d, continuing...\n", section->offset, feat);
2170 return 0;
2171 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002172 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002173 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002174 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002175 }
2176 if (!feat_ops[feat].print)
2177 return 0;
2178
2179 if (!feat_ops[feat].full_only || hd->full)
2180 feat_ops[feat].print(ph, fd, hd->fp);
2181 else
2182 fprintf(hd->fp, "# %s info available, use -I to display\n",
2183 feat_ops[feat].name);
2184
2185 return 0;
2186}
2187
2188int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2189{
2190 struct header_print_data hd;
2191 struct perf_header *header = &session->header;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002192 int fd = perf_data_file__fd(session->file);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002193 hd.fp = fp;
2194 hd.full = full;
2195
2196 perf_header__process_sections(header, fd, &hd,
2197 perf_file_section__fprintf_info);
2198 return 0;
2199}
2200
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002201static int do_write_feat(int fd, struct perf_header *h, int type,
2202 struct perf_file_section **p,
2203 struct perf_evlist *evlist)
2204{
2205 int err;
2206 int ret = 0;
2207
2208 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002209 if (!feat_ops[type].write)
2210 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002211
2212 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2213
2214 err = feat_ops[type].write(fd, h, evlist);
2215 if (err < 0) {
2216 pr_debug("failed to write feature %d\n", type);
2217
2218 /* undo anything written */
2219 lseek(fd, (*p)->offset, SEEK_SET);
2220
2221 return -1;
2222 }
2223 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2224 (*p)++;
2225 }
2226 return ret;
2227}
2228
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002229static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002230 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002231{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002232 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002233 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002234 int sec_size;
2235 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002236 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002237 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002238
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002239 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002240 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002241 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002242
Paul Gortmaker91b98802013-01-30 20:05:49 -05002243 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002244 if (feat_sec == NULL)
2245 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002246
2247 sec_size = sizeof(*feat_sec) * nr_sections;
2248
Jiri Olsa8d541e92013-07-17 19:49:44 +02002249 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002250 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002251
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002252 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2253 if (do_write_feat(fd, header, feat, &p, evlist))
2254 perf_header__clear_feat(header, feat);
2255 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002256
Xiao Guangrongf887f302010-02-04 16:46:42 +08002257 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002258 /*
2259 * may write more than needed due to dropped feature, but
2260 * this is okay, reader will skip the mising entries
2261 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002262 err = do_write(fd, feat_sec, sec_size);
2263 if (err < 0)
2264 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002265 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002266 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002267}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002268
Tom Zanussi8dc58102010-04-01 23:59:15 -05002269int perf_header__write_pipe(int fd)
2270{
2271 struct perf_pipe_file_header f_header;
2272 int err;
2273
2274 f_header = (struct perf_pipe_file_header){
2275 .magic = PERF_MAGIC,
2276 .size = sizeof(f_header),
2277 };
2278
2279 err = do_write(fd, &f_header, sizeof(f_header));
2280 if (err < 0) {
2281 pr_debug("failed to write perf pipe header\n");
2282 return err;
2283 }
2284
2285 return 0;
2286}
2287
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002288int perf_session__write_header(struct perf_session *session,
2289 struct perf_evlist *evlist,
2290 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002291{
2292 struct perf_file_header f_header;
2293 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002294 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002295 struct perf_evsel *evsel;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002296 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002297 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002298
2299 lseek(fd, sizeof(f_header), SEEK_SET);
2300
Robert Richter6606f872012-08-16 21:10:19 +02002301 list_for_each_entry(evsel, &evlist->entries, node) {
2302 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2303 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002304 if (err < 0) {
2305 pr_debug("failed to write perf header\n");
2306 return err;
2307 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002308 }
2309
Jiri Olsa944d62b2013-07-17 19:49:43 +02002310 attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002311
Robert Richter6606f872012-08-16 21:10:19 +02002312 list_for_each_entry(evsel, &evlist->entries, node) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002313 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002314 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002315 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002316 .offset = evsel->id_offset,
2317 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002318 }
2319 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002320 err = do_write(fd, &f_attr, sizeof(f_attr));
2321 if (err < 0) {
2322 pr_debug("failed to write perf header attribute\n");
2323 return err;
2324 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002325 }
2326
Adrian Hunterd645c442013-12-11 14:36:28 +02002327 if (!header->data_offset)
2328 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002329 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002330
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002331 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002332 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002333 if (err < 0)
2334 return err;
2335 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002336
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002337 f_header = (struct perf_file_header){
2338 .magic = PERF_MAGIC,
2339 .size = sizeof(f_header),
2340 .attr_size = sizeof(f_attr),
2341 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002342 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002343 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002344 },
2345 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002346 .offset = header->data_offset,
2347 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002348 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002349 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002350 };
2351
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002352 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002353
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002354 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002355 err = do_write(fd, &f_header, sizeof(f_header));
2356 if (err < 0) {
2357 pr_debug("failed to write perf header\n");
2358 return err;
2359 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002360 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002361
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002362 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002363}
2364
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002365static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002366 int fd, void *buf, size_t size)
2367{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002368 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002369 return -1;
2370
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002371 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002372 mem_bswap_64(buf, size);
2373
2374 return 0;
2375}
2376
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002377int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002378 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002379 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002380 struct perf_header *ph,
2381 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002382{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002383 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002384 int nr_sections;
2385 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002386 int feat;
2387 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002388
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002389 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002390 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002391 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002392
Paul Gortmaker91b98802013-01-30 20:05:49 -05002393 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002394 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002395 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002396
2397 sec_size = sizeof(*feat_sec) * nr_sections;
2398
Jiri Olsa8d541e92013-07-17 19:49:44 +02002399 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002400
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002401 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2402 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002403 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002404
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002405 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2406 err = process(sec++, header, feat, fd, data);
2407 if (err < 0)
2408 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002409 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002410 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002411out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002412 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002413 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002414}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002415
Stephane Eranian114382a2012-02-09 23:21:08 +01002416static const int attr_file_abi_sizes[] = {
2417 [0] = PERF_ATTR_SIZE_VER0,
2418 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002419 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002420 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian114382a2012-02-09 23:21:08 +01002421 0,
2422};
2423
2424/*
2425 * In the legacy file format, the magic number is not used to encode endianness.
2426 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2427 * on ABI revisions, we need to try all combinations for all endianness to
2428 * detect the endianness.
2429 */
2430static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2431{
2432 uint64_t ref_size, attr_size;
2433 int i;
2434
2435 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2436 ref_size = attr_file_abi_sizes[i]
2437 + sizeof(struct perf_file_section);
2438 if (hdr_sz != ref_size) {
2439 attr_size = bswap_64(hdr_sz);
2440 if (attr_size != ref_size)
2441 continue;
2442
2443 ph->needs_swap = true;
2444 }
2445 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2446 i,
2447 ph->needs_swap);
2448 return 0;
2449 }
2450 /* could not determine endianness */
2451 return -1;
2452}
2453
2454#define PERF_PIPE_HDR_VER0 16
2455
2456static const size_t attr_pipe_abi_sizes[] = {
2457 [0] = PERF_PIPE_HDR_VER0,
2458 0,
2459};
2460
2461/*
2462 * In the legacy pipe format, there is an implicit assumption that endiannesss
2463 * between host recording the samples, and host parsing the samples is the
2464 * same. This is not always the case given that the pipe output may always be
2465 * redirected into a file and analyzed on a different machine with possibly a
2466 * different endianness and perf_event ABI revsions in the perf tool itself.
2467 */
2468static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2469{
2470 u64 attr_size;
2471 int i;
2472
2473 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2474 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2475 attr_size = bswap_64(hdr_sz);
2476 if (attr_size != hdr_sz)
2477 continue;
2478
2479 ph->needs_swap = true;
2480 }
2481 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2482 return 0;
2483 }
2484 return -1;
2485}
2486
Feng Tange84ba4e2012-10-30 11:56:07 +08002487bool is_perf_magic(u64 magic)
2488{
2489 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2490 || magic == __perf_magic2
2491 || magic == __perf_magic2_sw)
2492 return true;
2493
2494 return false;
2495}
2496
Stephane Eranian114382a2012-02-09 23:21:08 +01002497static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2498 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002499{
2500 int ret;
2501
2502 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002503 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002504 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002505 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002506 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002507 if (is_pipe)
2508 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002509
Stephane Eranian114382a2012-02-09 23:21:08 +01002510 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002511 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002512 /*
2513 * the new magic number serves two purposes:
2514 * - unique number to identify actual perf.data files
2515 * - encode endianness of file
2516 */
Stephane Eranian73323f52012-02-02 13:54:44 +01002517
Stephane Eranian114382a2012-02-09 23:21:08 +01002518 /* check magic number with one endianness */
2519 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002520 return 0;
2521
Stephane Eranian114382a2012-02-09 23:21:08 +01002522 /* check magic number with opposite endianness */
2523 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002524 return -1;
2525
2526 ph->needs_swap = true;
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002527 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002528
2529 return 0;
2530}
2531
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002532int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002533 struct perf_header *ph, int fd)
2534{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002535 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002536
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002537 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002538
Stephane Eranian73323f52012-02-02 13:54:44 +01002539 ret = readn(fd, header, sizeof(*header));
2540 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002541 return -1;
2542
Stephane Eranian114382a2012-02-09 23:21:08 +01002543 if (check_magic_endian(header->magic,
2544 header->attr_size, false, ph) < 0) {
2545 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002546 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002547 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002548
Stephane Eranian73323f52012-02-02 13:54:44 +01002549 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002550 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002551 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002552 }
2553
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002554 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002555 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002556 if (header->size == offsetof(typeof(*header), adds_features))
2557 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002558 else
2559 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002560 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002561 /*
2562 * feature bitmap is declared as an array of unsigned longs --
2563 * not good since its size can differ between the host that
2564 * generated the data file and the host analyzing the file.
2565 *
2566 * We need to handle endianness, but we don't know the size of
2567 * the unsigned long where the file was generated. Take a best
2568 * guess at determining it: try 64-bit swap first (ie., file
2569 * created on a 64-bit host), and check if the hostname feature
2570 * bit is set (this feature bit is forced on as of fbe96f2).
2571 * If the bit is not, undo the 64-bit swap and try a 32-bit
2572 * swap. If the hostname bit is still not set (e.g., older data
2573 * file), punt and fallback to the original behavior --
2574 * clearing all feature bits and setting buildid.
2575 */
David Ahern80c01202012-06-08 11:47:51 -03002576 mem_bswap_64(&header->adds_features,
2577 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002578
2579 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002580 /* unswap as u64 */
2581 mem_bswap_64(&header->adds_features,
2582 BITS_TO_U64(HEADER_FEAT_BITS));
2583
2584 /* unswap as u32 */
2585 mem_bswap_32(&header->adds_features,
2586 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002587 }
2588
2589 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2590 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2591 set_bit(HEADER_BUILD_ID, header->adds_features);
2592 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002593 }
2594
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002595 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002596 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002597
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002598 ph->data_offset = header->data.offset;
2599 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002600 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002601 return 0;
2602}
2603
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002604static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002605 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002606 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002607{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002608 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002609 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002610 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002611 return 0;
2612 }
2613
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002614 if (feat >= HEADER_LAST_FEATURE) {
2615 pr_debug("unknown feature %d, continuing...\n", feat);
2616 return 0;
2617 }
2618
Robert Richterf1c67db2012-02-10 15:41:56 +01002619 if (!feat_ops[feat].process)
2620 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002621
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002622 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002623}
2624
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002625static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002626 struct perf_header *ph, int fd,
2627 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002628{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002629 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002630
2631 ret = readn(fd, header, sizeof(*header));
2632 if (ret <= 0)
2633 return -1;
2634
Stephane Eranian114382a2012-02-09 23:21:08 +01002635 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2636 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002637 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002638 }
2639
2640 if (ph->needs_swap)
2641 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002642
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002643 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002644 return -1;
2645
Tom Zanussi8dc58102010-04-01 23:59:15 -05002646 return 0;
2647}
2648
Jiri Olsad4339562013-07-17 19:49:41 +02002649static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002650{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002651 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002652 struct perf_pipe_file_header f_header;
2653
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002654 if (perf_file_header__read_pipe(&f_header, header,
2655 perf_data_file__fd(session->file),
Tom Zanussi454c4072010-05-01 01:41:20 -05002656 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002657 pr_debug("incompatible file format\n");
2658 return -EINVAL;
2659 }
2660
Tom Zanussi8dc58102010-04-01 23:59:15 -05002661 return 0;
2662}
2663
Stephane Eranian69996df2012-02-09 23:21:06 +01002664static int read_attr(int fd, struct perf_header *ph,
2665 struct perf_file_attr *f_attr)
2666{
2667 struct perf_event_attr *attr = &f_attr->attr;
2668 size_t sz, left;
2669 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002670 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002671
2672 memset(f_attr, 0, sizeof(*f_attr));
2673
2674 /* read minimal guaranteed structure */
2675 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2676 if (ret <= 0) {
2677 pr_debug("cannot read %d bytes of header attr\n",
2678 PERF_ATTR_SIZE_VER0);
2679 return -1;
2680 }
2681
2682 /* on file perf_event_attr size */
2683 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002684
Stephane Eranian69996df2012-02-09 23:21:06 +01002685 if (ph->needs_swap)
2686 sz = bswap_32(sz);
2687
2688 if (sz == 0) {
2689 /* assume ABI0 */
2690 sz = PERF_ATTR_SIZE_VER0;
2691 } else if (sz > our_sz) {
2692 pr_debug("file uses a more recent and unsupported ABI"
2693 " (%zu bytes extra)\n", sz - our_sz);
2694 return -1;
2695 }
2696 /* what we have not yet read and that we know about */
2697 left = sz - PERF_ATTR_SIZE_VER0;
2698 if (left) {
2699 void *ptr = attr;
2700 ptr += PERF_ATTR_SIZE_VER0;
2701
2702 ret = readn(fd, ptr, left);
2703 }
2704 /* read perf_file_section, ids are read in caller */
2705 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2706
2707 return ret <= 0 ? -1 : 0;
2708}
2709
Namhyung Kim831394b2012-09-06 11:10:46 +09002710static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2711 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002712{
Namhyung Kim831394b2012-09-06 11:10:46 +09002713 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002714 char bf[128];
2715
Namhyung Kim831394b2012-09-06 11:10:46 +09002716 /* already prepared */
2717 if (evsel->tp_format)
2718 return 0;
2719
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002720 if (pevent == NULL) {
2721 pr_debug("broken or missing trace data\n");
2722 return -1;
2723 }
2724
Namhyung Kim831394b2012-09-06 11:10:46 +09002725 event = pevent_find_event(pevent, evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002726 if (event == NULL)
2727 return -1;
2728
Namhyung Kim831394b2012-09-06 11:10:46 +09002729 if (!evsel->name) {
2730 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2731 evsel->name = strdup(bf);
2732 if (evsel->name == NULL)
2733 return -1;
2734 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002735
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002736 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002737 return 0;
2738}
2739
Namhyung Kim831394b2012-09-06 11:10:46 +09002740static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2741 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002742{
2743 struct perf_evsel *pos;
2744
2745 list_for_each_entry(pos, &evlist->entries, node) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002746 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2747 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002748 return -1;
2749 }
2750
2751 return 0;
2752}
2753
Jiri Olsad4339562013-07-17 19:49:41 +02002754int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002755{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002756 struct perf_data_file *file = session->file;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002757 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002758 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002759 struct perf_file_attr f_attr;
2760 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002761 int nr_attrs, nr_ids, i, j;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002762 int fd = perf_data_file__fd(file);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002763
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002764 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002765 if (session->evlist == NULL)
2766 return -ENOMEM;
2767
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002768 if (perf_data_file__is_pipe(file))
Jiri Olsad4339562013-07-17 19:49:41 +02002769 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002770
Stephane Eranian69996df2012-02-09 23:21:06 +01002771 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002772 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002773
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002774 /*
2775 * Sanity check that perf.data was written cleanly; data size is
2776 * initialized to 0 and updated only if the on_exit function is run.
2777 * If data size is still 0 then the file contains only partial
2778 * information. Just warn user and process it as much as it can.
2779 */
2780 if (f_header.data.size == 0) {
2781 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2782 "Was the 'perf record' command properly terminated?\n",
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002783 file->path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002784 }
2785
Stephane Eranian69996df2012-02-09 23:21:06 +01002786 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002787 lseek(fd, f_header.attrs.offset, SEEK_SET);
2788
2789 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002790 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002791 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002792
Stephane Eranian69996df2012-02-09 23:21:06 +01002793 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002794 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002795
David Aherneda39132011-07-15 12:34:09 -06002796 if (header->needs_swap)
2797 perf_event__attr_swap(&f_attr.attr);
2798
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002799 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002800 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002801
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002802 if (evsel == NULL)
2803 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002804
2805 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002806 /*
2807 * Do it before so that if perf_evsel__alloc_id fails, this
2808 * entry gets purged too at perf_evlist__delete().
2809 */
2810 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002811
2812 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002813 /*
2814 * We don't have the cpu and thread maps on the header, so
2815 * for allocating the perf_sample_id table we fake 1 cpu and
2816 * hattr->ids threads.
2817 */
2818 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2819 goto out_delete_evlist;
2820
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002821 lseek(fd, f_attr.ids.offset, SEEK_SET);
2822
2823 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002824 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002825 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002826
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002827 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002828 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002829
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002830 lseek(fd, tmp, SEEK_SET);
2831 }
2832
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002833 symbol_conf.nr_events = nr_attrs;
2834
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002835 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002836 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002837
Namhyung Kim831394b2012-09-06 11:10:46 +09002838 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002839 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002840 goto out_delete_evlist;
2841
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002842 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002843out_errno:
2844 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002845
2846out_delete_evlist:
2847 perf_evlist__delete(session->evlist);
2848 session->evlist = NULL;
2849 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002850}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002851
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002852int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002853 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002854 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002855{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002856 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002857 size_t size;
2858 int err;
2859
2860 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002861 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002862 size += sizeof(struct perf_event_header);
2863 size += ids * sizeof(u64);
2864
2865 ev = malloc(size);
2866
Chris Samuelce47dc52010-11-13 13:35:06 +11002867 if (ev == NULL)
2868 return -ENOMEM;
2869
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002870 ev->attr.attr = *attr;
2871 memcpy(ev->attr.id, id, ids * sizeof(u64));
2872
2873 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002874 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002875
Robert Richterf4d83432012-08-16 21:10:17 +02002876 if (ev->attr.header.size == size)
2877 err = process(tool, ev, NULL, NULL);
2878 else
2879 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002880
2881 free(ev);
2882
2883 return err;
2884}
2885
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002886int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002887 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002888 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002889{
Robert Richter6606f872012-08-16 21:10:19 +02002890 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002891 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002892
Robert Richter6606f872012-08-16 21:10:19 +02002893 list_for_each_entry(evsel, &session->evlist->entries, node) {
2894 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2895 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002896 if (err) {
2897 pr_debug("failed to create perf header attribute\n");
2898 return err;
2899 }
2900 }
2901
2902 return err;
2903}
2904
Adrian Hunter47c3d102013-07-04 16:20:21 +03002905int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2906 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002907 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002908{
Robert Richterf4d83432012-08-16 21:10:17 +02002909 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002910 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002911 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002912
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002913 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002914 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002915 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002916 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002917 }
2918
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002919 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002920 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002921 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002922
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002923 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002924
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002925 ids = event->header.size;
2926 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002927 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002928 /*
2929 * We don't have the cpu and thread maps on the header, so
2930 * for allocating the perf_sample_id table we fake 1 cpu and
2931 * hattr->ids threads.
2932 */
2933 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2934 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002935
2936 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002937 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002938 }
2939
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03002940 symbol_conf.nr_events = evlist->nr_entries;
2941
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002942 return 0;
2943}
Tom Zanussicd19a032010-04-01 23:59:20 -05002944
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002945int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002946 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002947 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05002948{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002949 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02002950 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05002951 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002952 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05002953
Jiri Olsa29208e52011-10-20 15:59:43 +02002954 /*
2955 * We are going to store the size of the data followed
2956 * by the data contents. Since the fd descriptor is a pipe,
2957 * we cannot seek back to store the size of the data once
2958 * we know it. Instead we:
2959 *
2960 * - write the tracing data to the temp file
2961 * - get/write the data size to pipe
2962 * - write the tracing data from the temp file
2963 * to the pipe
2964 */
2965 tdata = tracing_data_get(&evlist->entries, fd, true);
2966 if (!tdata)
2967 return -1;
2968
Tom Zanussi92155452010-04-01 23:59:21 -05002969 memset(&ev, 0, sizeof(ev));
2970
2971 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02002972 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002973 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05002974 padding = aligned_size - size;
2975 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2976 ev.tracing_data.size = aligned_size;
2977
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002978 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05002979
Jiri Olsa29208e52011-10-20 15:59:43 +02002980 /*
2981 * The put function will copy all the tracing data
2982 * stored in temp file to the pipe.
2983 */
2984 tracing_data_put(tdata);
2985
Tom Zanussi92155452010-04-01 23:59:21 -05002986 write_padded(fd, NULL, 0, padding);
2987
2988 return aligned_size;
2989}
2990
Adrian Hunter47c3d102013-07-04 16:20:21 +03002991int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
2992 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002993 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05002994{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002995 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002996 int fd = perf_data_file__fd(session->file);
2997 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05002998 char buf[BUFSIZ];
2999
3000 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003001 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05003002 SEEK_SET);
3003
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003004 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003005 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003006 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003007
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003008 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003009 pr_err("%s: reading input file", __func__);
3010 return -1;
3011 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003012 if (session->repipe) {
3013 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003014 if (retw <= 0 || retw != padding) {
3015 pr_err("%s: repiping tracing data padding", __func__);
3016 return -1;
3017 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003018 }
Tom Zanussi92155452010-04-01 23:59:21 -05003019
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003020 if (size_read + padding != size) {
3021 pr_err("%s: tracing data size mismatch", __func__);
3022 return -1;
3023 }
Tom Zanussi92155452010-04-01 23:59:21 -05003024
Namhyung Kim831394b2012-09-06 11:10:46 +09003025 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003026 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003027
Tom Zanussi92155452010-04-01 23:59:21 -05003028 return size_read + padding;
3029}
Tom Zanussic7929e42010-04-01 23:59:22 -05003030
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003031int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003032 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003033 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003034 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003035{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003036 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003037 size_t len;
3038 int err = 0;
3039
3040 if (!pos->hit)
3041 return err;
3042
3043 memset(&ev, 0, sizeof(ev));
3044
3045 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003046 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003047 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3048 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3049 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003050 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003051 ev.build_id.header.size = sizeof(ev.build_id) + len;
3052 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3053
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003054 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003055
3056 return err;
3057}
3058
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003059int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003060 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003061 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003062{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003063 __event_process_build_id(&event->build_id,
3064 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003065 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003066 return 0;
3067}
Stephane Eraniana1ac1d32010-06-17 11:39:01 +02003068
3069void disable_buildid_cache(void)
3070{
3071 no_buildid_cache = true;
3072}