blob: 893f8e2df9285237b26365922adf9b1eaa88a5ec [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
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300646 evlist__for_each(evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +0200647 ret = do_write(fd, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200648 if (ret < 0)
649 return ret;
650 /*
651 * write number of unique id per event
652 * there is one id per instance of an event
653 *
654 * copy into an nri to be independent of the
655 * type of ids,
656 */
Robert Richter6606f872012-08-16 21:10:19 +0200657 nri = evsel->ids;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200658 ret = do_write(fd, &nri, sizeof(nri));
659 if (ret < 0)
660 return ret;
661
662 /*
663 * write event string as passed on cmdline
664 */
Robert Richter6606f872012-08-16 21:10:19 +0200665 ret = do_write_string(fd, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200666 if (ret < 0)
667 return ret;
668 /*
669 * write unique ids for this event
670 */
Robert Richter6606f872012-08-16 21:10:19 +0200671 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200672 if (ret < 0)
673 return ret;
674 }
675 return 0;
676}
677
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300678static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
679 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200680{
681 char buf[MAXPATHLEN];
682 char proc[32];
683 u32 i, n;
684 int ret;
685
686 /*
687 * actual atual path to perf binary
688 */
689 sprintf(proc, "/proc/%d/exe", getpid());
690 ret = readlink(proc, buf, sizeof(buf));
691 if (ret <= 0)
692 return -1;
693
694 /* readlink() does not add null termination */
695 buf[ret] = '\0';
696
697 /* account for binary path */
698 n = header_argc + 1;
699
700 ret = do_write(fd, &n, sizeof(n));
701 if (ret < 0)
702 return ret;
703
704 ret = do_write_string(fd, buf);
705 if (ret < 0)
706 return ret;
707
708 for (i = 0 ; i < header_argc; i++) {
709 ret = do_write_string(fd, header_argv[i]);
710 if (ret < 0)
711 return ret;
712 }
713 return 0;
714}
715
716#define CORE_SIB_FMT \
717 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
718#define THRD_SIB_FMT \
719 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
720
721struct cpu_topo {
722 u32 core_sib;
723 u32 thread_sib;
724 char **core_siblings;
725 char **thread_siblings;
726};
727
728static int build_cpu_topo(struct cpu_topo *tp, int cpu)
729{
730 FILE *fp;
731 char filename[MAXPATHLEN];
732 char *buf = NULL, *p;
733 size_t len = 0;
Stephane Eranianc5885742013-08-14 12:04:26 +0200734 ssize_t sret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200735 u32 i = 0;
736 int ret = -1;
737
738 sprintf(filename, CORE_SIB_FMT, cpu);
739 fp = fopen(filename, "r");
740 if (!fp)
Stephane Eranianc5885742013-08-14 12:04:26 +0200741 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200742
Stephane Eranianc5885742013-08-14 12:04:26 +0200743 sret = getline(&buf, &len, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200744 fclose(fp);
Stephane Eranianc5885742013-08-14 12:04:26 +0200745 if (sret <= 0)
746 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200747
748 p = strchr(buf, '\n');
749 if (p)
750 *p = '\0';
751
752 for (i = 0; i < tp->core_sib; i++) {
753 if (!strcmp(buf, tp->core_siblings[i]))
754 break;
755 }
756 if (i == tp->core_sib) {
757 tp->core_siblings[i] = buf;
758 tp->core_sib++;
759 buf = NULL;
760 len = 0;
761 }
Stephane Eranianc5885742013-08-14 12:04:26 +0200762 ret = 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200763
Stephane Eranianc5885742013-08-14 12:04:26 +0200764try_threads:
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200765 sprintf(filename, THRD_SIB_FMT, cpu);
766 fp = fopen(filename, "r");
767 if (!fp)
768 goto done;
769
770 if (getline(&buf, &len, fp) <= 0)
771 goto done;
772
773 p = strchr(buf, '\n');
774 if (p)
775 *p = '\0';
776
777 for (i = 0; i < tp->thread_sib; i++) {
778 if (!strcmp(buf, tp->thread_siblings[i]))
779 break;
780 }
781 if (i == tp->thread_sib) {
782 tp->thread_siblings[i] = buf;
783 tp->thread_sib++;
784 buf = NULL;
785 }
786 ret = 0;
787done:
788 if(fp)
789 fclose(fp);
790 free(buf);
791 return ret;
792}
793
794static void free_cpu_topo(struct cpu_topo *tp)
795{
796 u32 i;
797
798 if (!tp)
799 return;
800
801 for (i = 0 ; i < tp->core_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300802 zfree(&tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200803
804 for (i = 0 ; i < tp->thread_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300805 zfree(&tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200806
807 free(tp);
808}
809
810static struct cpu_topo *build_cpu_topology(void)
811{
812 struct cpu_topo *tp;
813 void *addr;
814 u32 nr, i;
815 size_t sz;
816 long ncpus;
817 int ret = -1;
818
819 ncpus = sysconf(_SC_NPROCESSORS_CONF);
820 if (ncpus < 0)
821 return NULL;
822
823 nr = (u32)(ncpus & UINT_MAX);
824
825 sz = nr * sizeof(char *);
826
827 addr = calloc(1, sizeof(*tp) + 2 * sz);
828 if (!addr)
829 return NULL;
830
831 tp = addr;
832
833 addr += sizeof(*tp);
834 tp->core_siblings = addr;
835 addr += sz;
836 tp->thread_siblings = addr;
837
838 for (i = 0; i < nr; i++) {
839 ret = build_cpu_topo(tp, i);
840 if (ret < 0)
841 break;
842 }
843 if (ret) {
844 free_cpu_topo(tp);
845 tp = NULL;
846 }
847 return tp;
848}
849
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300850static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
851 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200852{
853 struct cpu_topo *tp;
854 u32 i;
855 int ret;
856
857 tp = build_cpu_topology();
858 if (!tp)
859 return -1;
860
861 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
862 if (ret < 0)
863 goto done;
864
865 for (i = 0; i < tp->core_sib; i++) {
866 ret = do_write_string(fd, tp->core_siblings[i]);
867 if (ret < 0)
868 goto done;
869 }
870 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
871 if (ret < 0)
872 goto done;
873
874 for (i = 0; i < tp->thread_sib; i++) {
875 ret = do_write_string(fd, tp->thread_siblings[i]);
876 if (ret < 0)
877 break;
878 }
879done:
880 free_cpu_topo(tp);
881 return ret;
882}
883
884
885
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300886static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
887 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200888{
889 char *buf = NULL;
890 FILE *fp;
891 size_t len = 0;
892 int ret = -1, n;
893 uint64_t mem;
894
895 fp = fopen("/proc/meminfo", "r");
896 if (!fp)
897 return -1;
898
899 while (getline(&buf, &len, fp) > 0) {
900 ret = strncmp(buf, "MemTotal:", 9);
901 if (!ret)
902 break;
903 }
904 if (!ret) {
905 n = sscanf(buf, "%*s %"PRIu64, &mem);
906 if (n == 1)
907 ret = do_write(fd, &mem, sizeof(mem));
908 }
909 free(buf);
910 fclose(fp);
911 return ret;
912}
913
914static int write_topo_node(int fd, int node)
915{
916 char str[MAXPATHLEN];
917 char field[32];
918 char *buf = NULL, *p;
919 size_t len = 0;
920 FILE *fp;
921 u64 mem_total, mem_free, mem;
922 int ret = -1;
923
924 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
925 fp = fopen(str, "r");
926 if (!fp)
927 return -1;
928
929 while (getline(&buf, &len, fp) > 0) {
930 /* skip over invalid lines */
931 if (!strchr(buf, ':'))
932 continue;
Alan Coxa761a2d2014-01-20 19:10:11 +0100933 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200934 goto done;
935 if (!strcmp(field, "MemTotal:"))
936 mem_total = mem;
937 if (!strcmp(field, "MemFree:"))
938 mem_free = mem;
939 }
940
941 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100942 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200943
944 ret = do_write(fd, &mem_total, sizeof(u64));
945 if (ret)
946 goto done;
947
948 ret = do_write(fd, &mem_free, sizeof(u64));
949 if (ret)
950 goto done;
951
952 ret = -1;
953 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
954
955 fp = fopen(str, "r");
956 if (!fp)
957 goto done;
958
959 if (getline(&buf, &len, fp) <= 0)
960 goto done;
961
962 p = strchr(buf, '\n');
963 if (p)
964 *p = '\0';
965
966 ret = do_write_string(fd, buf);
967done:
968 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100969 if (fp)
970 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200971 return ret;
972}
973
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300974static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
975 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200976{
977 char *buf = NULL;
978 size_t len = 0;
979 FILE *fp;
980 struct cpu_map *node_map = NULL;
981 char *c;
982 u32 nr, i, j;
983 int ret = -1;
984
985 fp = fopen("/sys/devices/system/node/online", "r");
986 if (!fp)
987 return -1;
988
989 if (getline(&buf, &len, fp) <= 0)
990 goto done;
991
992 c = strchr(buf, '\n');
993 if (c)
994 *c = '\0';
995
996 node_map = cpu_map__new(buf);
997 if (!node_map)
998 goto done;
999
1000 nr = (u32)node_map->nr;
1001
1002 ret = do_write(fd, &nr, sizeof(nr));
1003 if (ret < 0)
1004 goto done;
1005
1006 for (i = 0; i < nr; i++) {
1007 j = (u32)node_map->map[i];
1008 ret = do_write(fd, &j, sizeof(j));
1009 if (ret < 0)
1010 break;
1011
1012 ret = write_topo_node(fd, i);
1013 if (ret < 0)
1014 break;
1015 }
1016done:
1017 free(buf);
1018 fclose(fp);
1019 free(node_map);
1020 return ret;
1021}
1022
1023/*
Robert Richter50a96672012-08-16 21:10:24 +02001024 * File format:
1025 *
1026 * struct pmu_mappings {
1027 * u32 pmu_num;
1028 * struct pmu_map {
1029 * u32 type;
1030 * char name[];
1031 * }[pmu_num];
1032 * };
1033 */
1034
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001035static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
1036 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +02001037{
1038 struct perf_pmu *pmu = NULL;
1039 off_t offset = lseek(fd, 0, SEEK_CUR);
1040 __u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +09001041 int ret;
Robert Richter50a96672012-08-16 21:10:24 +02001042
1043 /* write real pmu_num later */
Namhyung Kim5323f602012-12-17 15:38:54 +09001044 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
1045 if (ret < 0)
1046 return ret;
Robert Richter50a96672012-08-16 21:10:24 +02001047
1048 while ((pmu = perf_pmu__scan(pmu))) {
1049 if (!pmu->name)
1050 continue;
1051 pmu_num++;
Namhyung Kim5323f602012-12-17 15:38:54 +09001052
1053 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
1054 if (ret < 0)
1055 return ret;
1056
1057 ret = do_write_string(fd, pmu->name);
1058 if (ret < 0)
1059 return ret;
Robert Richter50a96672012-08-16 21:10:24 +02001060 }
1061
1062 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1063 /* discard all */
1064 lseek(fd, offset, SEEK_SET);
1065 return -1;
1066 }
1067
1068 return 0;
1069}
1070
1071/*
Namhyung Kima8bb5592013-01-22 18:09:31 +09001072 * File format:
1073 *
1074 * struct group_descs {
1075 * u32 nr_groups;
1076 * struct group_desc {
1077 * char name[];
1078 * u32 leader_idx;
1079 * u32 nr_members;
1080 * }[nr_groups];
1081 * };
1082 */
1083static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
1084 struct perf_evlist *evlist)
1085{
1086 u32 nr_groups = evlist->nr_groups;
1087 struct perf_evsel *evsel;
1088 int ret;
1089
1090 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
1091 if (ret < 0)
1092 return ret;
1093
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001094 evlist__for_each(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001095 if (perf_evsel__is_group_leader(evsel) &&
1096 evsel->nr_members > 1) {
1097 const char *name = evsel->group_name ?: "{anon_group}";
1098 u32 leader_idx = evsel->idx;
1099 u32 nr_members = evsel->nr_members;
1100
1101 ret = do_write_string(fd, name);
1102 if (ret < 0)
1103 return ret;
1104
1105 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
1106 if (ret < 0)
1107 return ret;
1108
1109 ret = do_write(fd, &nr_members, sizeof(nr_members));
1110 if (ret < 0)
1111 return ret;
1112 }
1113 }
1114 return 0;
1115}
1116
1117/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001118 * default get_cpuid(): nothing gets recorded
1119 * actual implementation must be in arch/$(ARCH)/util/header.c
1120 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001121int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
1122 size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001123{
1124 return -1;
1125}
1126
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001127static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
1128 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001129{
1130 char buffer[64];
1131 int ret;
1132
1133 ret = get_cpuid(buffer, sizeof(buffer));
1134 if (!ret)
1135 goto write_it;
1136
1137 return -1;
1138write_it:
1139 return do_write_string(fd, buffer);
1140}
1141
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001142static int write_branch_stack(int fd __maybe_unused,
1143 struct perf_header *h __maybe_unused,
1144 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +01001145{
1146 return 0;
1147}
1148
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001149static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1150 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001151{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001152 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001153}
1154
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001155static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1156 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001157{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001158 fprintf(fp, "# os release : %s\n", ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001159}
1160
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001161static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001162{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001163 fprintf(fp, "# arch : %s\n", ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001164}
1165
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001166static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1167 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001168{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001169 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001170}
1171
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001172static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1173 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001174{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001175 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1176 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001177}
1178
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001179static void print_version(struct perf_header *ph, int fd __maybe_unused,
1180 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001181{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001182 fprintf(fp, "# perf version : %s\n", ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001183}
1184
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001185static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1186 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001187{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001188 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001189 char *str;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001190
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001191 nr = ph->env.nr_cmdline;
1192 str = ph->env.cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001193
1194 fprintf(fp, "# cmdline : ");
1195
1196 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001197 fprintf(fp, "%s ", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001198 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001199 }
1200 fputc('\n', fp);
1201}
1202
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001203static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1204 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001205{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001206 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001207 char *str;
1208
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001209 nr = ph->env.nr_sibling_cores;
1210 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001211
1212 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001213 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001214 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001215 }
1216
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001217 nr = ph->env.nr_sibling_threads;
1218 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001219
1220 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001221 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001222 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001223 }
1224}
1225
Robert Richter4e1b9c62012-08-16 21:10:22 +02001226static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001227{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001228 struct perf_evsel *evsel;
1229
1230 if (!events)
1231 return;
1232
1233 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001234 zfree(&evsel->name);
1235 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001236 }
1237
1238 free(events);
1239}
1240
1241static struct perf_evsel *
1242read_event_desc(struct perf_header *ph, int fd)
1243{
1244 struct perf_evsel *evsel, *events = NULL;
1245 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001246 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001247 u32 nre, sz, nr, i, j;
1248 ssize_t ret;
1249 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001250
1251 /* number of events */
Namhyung Kim5323f602012-12-17 15:38:54 +09001252 ret = readn(fd, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001253 if (ret != (ssize_t)sizeof(nre))
1254 goto error;
1255
1256 if (ph->needs_swap)
1257 nre = bswap_32(nre);
1258
Namhyung Kim5323f602012-12-17 15:38:54 +09001259 ret = readn(fd, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001260 if (ret != (ssize_t)sizeof(sz))
1261 goto error;
1262
1263 if (ph->needs_swap)
1264 sz = bswap_32(sz);
1265
Stephane Eranian62db9062012-02-09 23:21:07 +01001266 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001267 buf = malloc(sz);
1268 if (!buf)
1269 goto error;
1270
Robert Richter4e1b9c62012-08-16 21:10:22 +02001271 /* the last event terminates with evsel->attr.size == 0: */
1272 events = calloc(nre + 1, sizeof(*events));
1273 if (!events)
1274 goto error;
1275
1276 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001277 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001278 msz = sz;
1279
Robert Richter4e1b9c62012-08-16 21:10:22 +02001280 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1281 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001282
Stephane Eranian62db9062012-02-09 23:21:07 +01001283 /*
1284 * must read entire on-file attr struct to
1285 * sync up with layout.
1286 */
Namhyung Kim5323f602012-12-17 15:38:54 +09001287 ret = readn(fd, buf, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001288 if (ret != (ssize_t)sz)
1289 goto error;
1290
1291 if (ph->needs_swap)
1292 perf_event__attr_swap(buf);
1293
Robert Richter4e1b9c62012-08-16 21:10:22 +02001294 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001295
Namhyung Kim5323f602012-12-17 15:38:54 +09001296 ret = readn(fd, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001297 if (ret != (ssize_t)sizeof(nr))
1298 goto error;
1299
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001300 if (ph->needs_swap) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001301 nr = bswap_32(nr);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001302 evsel->needs_swap = true;
1303 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001304
Robert Richter4e1b9c62012-08-16 21:10:22 +02001305 evsel->name = do_read_string(fd, ph);
1306
1307 if (!nr)
1308 continue;
1309
1310 id = calloc(nr, sizeof(*id));
1311 if (!id)
1312 goto error;
1313 evsel->ids = nr;
1314 evsel->id = id;
1315
1316 for (j = 0 ; j < nr; j++) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001317 ret = readn(fd, id, sizeof(*id));
Robert Richter4e1b9c62012-08-16 21:10:22 +02001318 if (ret != (ssize_t)sizeof(*id))
1319 goto error;
1320 if (ph->needs_swap)
1321 *id = bswap_64(*id);
1322 id++;
1323 }
1324 }
1325out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001326 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001327 return events;
1328error:
1329 if (events)
1330 free_event_desc(events);
1331 events = NULL;
1332 goto out;
1333}
1334
1335static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1336{
1337 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1338 u32 j;
1339 u64 *id;
1340
1341 if (!events) {
1342 fprintf(fp, "# event desc: not available or unable to read\n");
1343 return;
1344 }
1345
1346 for (evsel = events; evsel->attr.size; evsel++) {
1347 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001348
1349 fprintf(fp, "type = %d, config = 0x%"PRIx64
1350 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
Robert Richter4e1b9c62012-08-16 21:10:22 +02001351 evsel->attr.type,
1352 (u64)evsel->attr.config,
1353 (u64)evsel->attr.config1,
1354 (u64)evsel->attr.config2);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001355
1356 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001357 evsel->attr.exclude_user,
1358 evsel->attr.exclude_kernel);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001359
David Ahern78b961f2012-07-20 17:25:52 -06001360 fprintf(fp, ", excl_host = %d, excl_guest = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001361 evsel->attr.exclude_host,
1362 evsel->attr.exclude_guest);
David Ahern78b961f2012-07-20 17:25:52 -06001363
Robert Richter4e1b9c62012-08-16 21:10:22 +02001364 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
David Ahern78b961f2012-07-20 17:25:52 -06001365
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001366 fprintf(fp, ", attr_mmap2 = %d", evsel->attr.mmap2);
1367 fprintf(fp, ", attr_mmap = %d", evsel->attr.mmap);
1368 fprintf(fp, ", attr_mmap_data = %d", evsel->attr.mmap_data);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001369 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001370 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001371 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1372 if (j)
1373 fputc(',', fp);
1374 fprintf(fp, " %"PRIu64, *id);
1375 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001376 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001377 }
1378
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001379 fputc('\n', fp);
1380 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001381
1382 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001383}
1384
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001385static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001386 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001387{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001388 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001389}
1390
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001391static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001392 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001393{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001394 u32 nr, c, i;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001395 char *str, *tmp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001396 uint64_t mem_total, mem_free;
1397
1398 /* nr nodes */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001399 nr = ph->env.nr_numa_nodes;
1400 str = ph->env.numa_nodes;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001401
1402 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001403 /* node number */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001404 c = strtoul(str, &tmp, 0);
1405 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001406 goto error;
1407
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001408 str = tmp + 1;
1409 mem_total = strtoull(str, &tmp, 0);
1410 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001411 goto error;
1412
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001413 str = tmp + 1;
1414 mem_free = strtoull(str, &tmp, 0);
1415 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001416 goto error;
1417
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001418 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1419 " free = %"PRIu64" kB\n",
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001420 c, mem_total, mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001421
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001422 str = tmp + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001423 fprintf(fp, "# node%u cpu list : %s\n", c, str);
Namhyung Kim12344712012-10-23 22:44:49 +09001424
1425 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001426 }
1427 return;
1428error:
1429 fprintf(fp, "# numa topology : not available\n");
1430}
1431
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001432static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001433{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001434 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001435}
1436
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001437static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001438 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001439{
1440 fprintf(fp, "# contains samples with branch stack\n");
1441}
1442
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001443static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1444 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001445{
1446 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001447 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001448 u32 pmu_num;
1449 u32 type;
1450
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001451 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001452 if (!pmu_num) {
1453 fprintf(fp, "# pmu mappings: not available\n");
1454 return;
1455 }
1456
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001457 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001458
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001459 while (pmu_num) {
1460 type = strtoul(str, &tmp, 0);
1461 if (*tmp != ':')
1462 goto error;
1463
1464 str = tmp + 1;
1465 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1466
Robert Richter50a96672012-08-16 21:10:24 +02001467 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001468 str += strlen(str) + 1;
1469 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001470 }
1471
1472 fprintf(fp, "\n");
1473
1474 if (!pmu_num)
1475 return;
1476error:
1477 fprintf(fp, "# pmu mappings: unable to read\n");
1478}
1479
Namhyung Kima8bb5592013-01-22 18:09:31 +09001480static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1481 FILE *fp)
1482{
1483 struct perf_session *session;
1484 struct perf_evsel *evsel;
1485 u32 nr = 0;
1486
1487 session = container_of(ph, struct perf_session, header);
1488
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001489 evlist__for_each(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001490 if (perf_evsel__is_group_leader(evsel) &&
1491 evsel->nr_members > 1) {
1492 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1493 perf_evsel__name(evsel));
1494
1495 nr = evsel->nr_members - 1;
1496 } else if (nr) {
1497 fprintf(fp, ",%s", perf_evsel__name(evsel));
1498
1499 if (--nr == 0)
1500 fprintf(fp, "}\n");
1501 }
1502 }
1503}
1504
Robert Richter08d95bd2012-02-10 15:41:55 +01001505static int __event_process_build_id(struct build_id_event *bev,
1506 char *filename,
1507 struct perf_session *session)
1508{
1509 int err = -1;
1510 struct list_head *head;
1511 struct machine *machine;
1512 u16 misc;
1513 struct dso *dso;
1514 enum dso_kernel_type dso_type;
1515
1516 machine = perf_session__findnew_machine(session, bev->pid);
1517 if (!machine)
1518 goto out;
1519
1520 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1521
1522 switch (misc) {
1523 case PERF_RECORD_MISC_KERNEL:
1524 dso_type = DSO_TYPE_KERNEL;
1525 head = &machine->kernel_dsos;
1526 break;
1527 case PERF_RECORD_MISC_GUEST_KERNEL:
1528 dso_type = DSO_TYPE_GUEST_KERNEL;
1529 head = &machine->kernel_dsos;
1530 break;
1531 case PERF_RECORD_MISC_USER:
1532 case PERF_RECORD_MISC_GUEST_USER:
1533 dso_type = DSO_TYPE_USER;
1534 head = &machine->user_dsos;
1535 break;
1536 default:
1537 goto out;
1538 }
1539
1540 dso = __dsos__findnew(head, filename);
1541 if (dso != NULL) {
1542 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1543
1544 dso__set_build_id(dso, &bev->build_id);
1545
1546 if (filename[0] == '[')
1547 dso->kernel = dso_type;
1548
1549 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1550 sbuild_id);
1551 pr_debug("build id event received for %s: %s\n",
1552 dso->long_name, sbuild_id);
1553 }
1554
1555 err = 0;
1556out:
1557 return err;
1558}
1559
1560static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1561 int input, u64 offset, u64 size)
1562{
1563 struct perf_session *session = container_of(header, struct perf_session, header);
1564 struct {
1565 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001566 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001567 char filename[0];
1568 } old_bev;
1569 struct build_id_event bev;
1570 char filename[PATH_MAX];
1571 u64 limit = offset + size;
1572
1573 while (offset < limit) {
1574 ssize_t len;
1575
Namhyung Kim5323f602012-12-17 15:38:54 +09001576 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001577 return -1;
1578
1579 if (header->needs_swap)
1580 perf_event_header__bswap(&old_bev.header);
1581
1582 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001583 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001584 return -1;
1585
1586 bev.header = old_bev.header;
1587
1588 /*
1589 * As the pid is the missing value, we need to fill
1590 * it properly. The header.misc value give us nice hint.
1591 */
1592 bev.pid = HOST_KERNEL_ID;
1593 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1594 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1595 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1596
1597 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1598 __event_process_build_id(&bev, filename, session);
1599
1600 offset += bev.header.size;
1601 }
1602
1603 return 0;
1604}
1605
1606static int perf_header__read_build_ids(struct perf_header *header,
1607 int input, u64 offset, u64 size)
1608{
1609 struct perf_session *session = container_of(header, struct perf_session, header);
1610 struct build_id_event bev;
1611 char filename[PATH_MAX];
1612 u64 limit = offset + size, orig_offset = offset;
1613 int err = -1;
1614
1615 while (offset < limit) {
1616 ssize_t len;
1617
Namhyung Kim5323f602012-12-17 15:38:54 +09001618 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001619 goto out;
1620
1621 if (header->needs_swap)
1622 perf_event_header__bswap(&bev.header);
1623
1624 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001625 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001626 goto out;
1627 /*
1628 * The a1645ce1 changeset:
1629 *
1630 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1631 *
1632 * Added a field to struct build_id_event that broke the file
1633 * format.
1634 *
1635 * Since the kernel build-id is the first entry, process the
1636 * table using the old format if the well known
1637 * '[kernel.kallsyms]' string for the kernel build-id has the
1638 * first 4 characters chopped off (where the pid_t sits).
1639 */
1640 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1641 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1642 return -1;
1643 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1644 }
1645
1646 __event_process_build_id(&bev, filename, session);
1647
1648 offset += bev.header.size;
1649 }
1650 err = 0;
1651out:
1652 return err;
1653}
1654
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001655static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1656 struct perf_header *ph __maybe_unused,
1657 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001658{
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001659 ssize_t ret = trace_report(fd, data, false);
1660 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001661}
1662
1663static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001664 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001665 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001666{
1667 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1668 pr_debug("Failed to read buildids, continuing...\n");
1669 return 0;
1670}
1671
Namhyung Kima1ae5652012-09-24 17:14:59 +09001672static int process_hostname(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001673 struct perf_header *ph, int fd,
1674 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001675{
1676 ph->env.hostname = do_read_string(fd, ph);
1677 return ph->env.hostname ? 0 : -ENOMEM;
1678}
1679
1680static int process_osrelease(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001681 struct perf_header *ph, int fd,
1682 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001683{
1684 ph->env.os_release = do_read_string(fd, ph);
1685 return ph->env.os_release ? 0 : -ENOMEM;
1686}
1687
1688static int process_version(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001689 struct perf_header *ph, int fd,
1690 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001691{
1692 ph->env.version = do_read_string(fd, ph);
1693 return ph->env.version ? 0 : -ENOMEM;
1694}
1695
1696static int process_arch(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001697 struct perf_header *ph, int fd,
1698 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001699{
1700 ph->env.arch = do_read_string(fd, ph);
1701 return ph->env.arch ? 0 : -ENOMEM;
1702}
1703
1704static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001705 struct perf_header *ph, int fd,
1706 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001707{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001708 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001709 u32 nr;
1710
Namhyung Kim5323f602012-12-17 15:38:54 +09001711 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001712 if (ret != sizeof(nr))
1713 return -1;
1714
1715 if (ph->needs_swap)
1716 nr = bswap_32(nr);
1717
1718 ph->env.nr_cpus_online = nr;
1719
Namhyung Kim5323f602012-12-17 15:38:54 +09001720 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001721 if (ret != sizeof(nr))
1722 return -1;
1723
1724 if (ph->needs_swap)
1725 nr = bswap_32(nr);
1726
1727 ph->env.nr_cpus_avail = nr;
1728 return 0;
1729}
1730
1731static int process_cpudesc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001732 struct perf_header *ph, int fd,
1733 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001734{
1735 ph->env.cpu_desc = do_read_string(fd, ph);
1736 return ph->env.cpu_desc ? 0 : -ENOMEM;
1737}
1738
1739static int process_cpuid(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001740 struct perf_header *ph, int fd,
1741 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001742{
1743 ph->env.cpuid = do_read_string(fd, ph);
1744 return ph->env.cpuid ? 0 : -ENOMEM;
1745}
1746
1747static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001748 struct perf_header *ph, int fd,
1749 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001750{
1751 uint64_t mem;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001752 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001753
Namhyung Kim5323f602012-12-17 15:38:54 +09001754 ret = readn(fd, &mem, sizeof(mem));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001755 if (ret != sizeof(mem))
1756 return -1;
1757
1758 if (ph->needs_swap)
1759 mem = bswap_64(mem);
1760
1761 ph->env.total_mem = mem;
1762 return 0;
1763}
1764
Robert Richter7c2f7af2012-08-16 21:10:23 +02001765static struct perf_evsel *
1766perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1767{
1768 struct perf_evsel *evsel;
1769
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001770 evlist__for_each(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001771 if (evsel->idx == idx)
1772 return evsel;
1773 }
1774
1775 return NULL;
1776}
1777
1778static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001779perf_evlist__set_event_name(struct perf_evlist *evlist,
1780 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001781{
1782 struct perf_evsel *evsel;
1783
1784 if (!event->name)
1785 return;
1786
1787 evsel = perf_evlist__find_by_index(evlist, event->idx);
1788 if (!evsel)
1789 return;
1790
1791 if (evsel->name)
1792 return;
1793
1794 evsel->name = strdup(event->name);
1795}
1796
1797static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001798process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001799 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001800 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001801{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001802 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001803 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1804
1805 if (!events)
1806 return 0;
1807
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001808 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001809 for (evsel = events; evsel->attr.size; evsel++)
1810 perf_evlist__set_event_name(session->evlist, evsel);
1811
1812 free_event_desc(events);
1813
1814 return 0;
1815}
1816
Namhyung Kima1ae5652012-09-24 17:14:59 +09001817static int process_cmdline(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001818 struct perf_header *ph, int fd,
1819 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001820{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001821 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001822 char *str;
1823 u32 nr, i;
1824 struct strbuf sb;
1825
Namhyung Kim5323f602012-12-17 15:38:54 +09001826 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001827 if (ret != sizeof(nr))
1828 return -1;
1829
1830 if (ph->needs_swap)
1831 nr = bswap_32(nr);
1832
1833 ph->env.nr_cmdline = nr;
1834 strbuf_init(&sb, 128);
1835
1836 for (i = 0; i < nr; i++) {
1837 str = do_read_string(fd, ph);
1838 if (!str)
1839 goto error;
1840
1841 /* include a NULL character at the end */
1842 strbuf_add(&sb, str, strlen(str) + 1);
1843 free(str);
1844 }
1845 ph->env.cmdline = strbuf_detach(&sb, NULL);
1846 return 0;
1847
1848error:
1849 strbuf_release(&sb);
1850 return -1;
1851}
1852
1853static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001854 struct perf_header *ph, int fd,
1855 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001856{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001857 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001858 u32 nr, i;
1859 char *str;
1860 struct strbuf sb;
1861
Namhyung Kim5323f602012-12-17 15:38:54 +09001862 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001863 if (ret != sizeof(nr))
1864 return -1;
1865
1866 if (ph->needs_swap)
1867 nr = bswap_32(nr);
1868
1869 ph->env.nr_sibling_cores = nr;
1870 strbuf_init(&sb, 128);
1871
1872 for (i = 0; i < nr; i++) {
1873 str = do_read_string(fd, ph);
1874 if (!str)
1875 goto error;
1876
1877 /* include a NULL character at the end */
1878 strbuf_add(&sb, str, strlen(str) + 1);
1879 free(str);
1880 }
1881 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1882
Namhyung Kim5323f602012-12-17 15:38:54 +09001883 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001884 if (ret != sizeof(nr))
1885 return -1;
1886
1887 if (ph->needs_swap)
1888 nr = bswap_32(nr);
1889
1890 ph->env.nr_sibling_threads = nr;
1891
1892 for (i = 0; i < nr; i++) {
1893 str = do_read_string(fd, ph);
1894 if (!str)
1895 goto error;
1896
1897 /* include a NULL character at the end */
1898 strbuf_add(&sb, str, strlen(str) + 1);
1899 free(str);
1900 }
1901 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1902 return 0;
1903
1904error:
1905 strbuf_release(&sb);
1906 return -1;
1907}
1908
1909static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001910 struct perf_header *ph, int fd,
1911 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001912{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001913 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001914 u32 nr, node, i;
1915 char *str;
1916 uint64_t mem_total, mem_free;
1917 struct strbuf sb;
1918
1919 /* nr nodes */
Namhyung Kim5323f602012-12-17 15:38:54 +09001920 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001921 if (ret != sizeof(nr))
1922 goto error;
1923
1924 if (ph->needs_swap)
1925 nr = bswap_32(nr);
1926
1927 ph->env.nr_numa_nodes = nr;
1928 strbuf_init(&sb, 256);
1929
1930 for (i = 0; i < nr; i++) {
1931 /* node number */
Namhyung Kim5323f602012-12-17 15:38:54 +09001932 ret = readn(fd, &node, sizeof(node));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001933 if (ret != sizeof(node))
1934 goto error;
1935
Namhyung Kim5323f602012-12-17 15:38:54 +09001936 ret = readn(fd, &mem_total, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001937 if (ret != sizeof(u64))
1938 goto error;
1939
Namhyung Kim5323f602012-12-17 15:38:54 +09001940 ret = readn(fd, &mem_free, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001941 if (ret != sizeof(u64))
1942 goto error;
1943
1944 if (ph->needs_swap) {
1945 node = bswap_32(node);
1946 mem_total = bswap_64(mem_total);
1947 mem_free = bswap_64(mem_free);
1948 }
1949
1950 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1951 node, mem_total, mem_free);
1952
1953 str = do_read_string(fd, ph);
1954 if (!str)
1955 goto error;
1956
1957 /* include a NULL character at the end */
1958 strbuf_add(&sb, str, strlen(str) + 1);
1959 free(str);
1960 }
1961 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1962 return 0;
1963
1964error:
1965 strbuf_release(&sb);
1966 return -1;
1967}
1968
1969static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001970 struct perf_header *ph, int fd,
1971 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001972{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001973 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001974 char *name;
1975 u32 pmu_num;
1976 u32 type;
1977 struct strbuf sb;
1978
Namhyung Kim5323f602012-12-17 15:38:54 +09001979 ret = readn(fd, &pmu_num, sizeof(pmu_num));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001980 if (ret != sizeof(pmu_num))
1981 return -1;
1982
1983 if (ph->needs_swap)
1984 pmu_num = bswap_32(pmu_num);
1985
1986 if (!pmu_num) {
1987 pr_debug("pmu mappings not available\n");
1988 return 0;
1989 }
1990
1991 ph->env.nr_pmu_mappings = pmu_num;
1992 strbuf_init(&sb, 128);
1993
1994 while (pmu_num) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001995 if (readn(fd, &type, sizeof(type)) != sizeof(type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001996 goto error;
1997 if (ph->needs_swap)
1998 type = bswap_32(type);
1999
2000 name = do_read_string(fd, ph);
2001 if (!name)
2002 goto error;
2003
2004 strbuf_addf(&sb, "%u:%s", type, name);
2005 /* include a NULL character at the end */
2006 strbuf_add(&sb, "", 1);
2007
2008 free(name);
2009 pmu_num--;
2010 }
2011 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2012 return 0;
2013
2014error:
2015 strbuf_release(&sb);
2016 return -1;
2017}
2018
Namhyung Kima8bb5592013-01-22 18:09:31 +09002019static int process_group_desc(struct perf_file_section *section __maybe_unused,
2020 struct perf_header *ph, int fd,
2021 void *data __maybe_unused)
2022{
2023 size_t ret = -1;
2024 u32 i, nr, nr_groups;
2025 struct perf_session *session;
2026 struct perf_evsel *evsel, *leader = NULL;
2027 struct group_desc {
2028 char *name;
2029 u32 leader_idx;
2030 u32 nr_members;
2031 } *desc;
2032
2033 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
2034 return -1;
2035
2036 if (ph->needs_swap)
2037 nr_groups = bswap_32(nr_groups);
2038
2039 ph->env.nr_groups = nr_groups;
2040 if (!nr_groups) {
2041 pr_debug("group desc not available\n");
2042 return 0;
2043 }
2044
2045 desc = calloc(nr_groups, sizeof(*desc));
2046 if (!desc)
2047 return -1;
2048
2049 for (i = 0; i < nr_groups; i++) {
2050 desc[i].name = do_read_string(fd, ph);
2051 if (!desc[i].name)
2052 goto out_free;
2053
2054 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
2055 goto out_free;
2056
2057 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
2058 goto out_free;
2059
2060 if (ph->needs_swap) {
2061 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
2062 desc[i].nr_members = bswap_32(desc[i].nr_members);
2063 }
2064 }
2065
2066 /*
2067 * Rebuild group relationship based on the group_desc
2068 */
2069 session = container_of(ph, struct perf_session, header);
2070 session->evlist->nr_groups = nr_groups;
2071
2072 i = nr = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002073 evlist__for_each(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002074 if (evsel->idx == (int) desc[i].leader_idx) {
2075 evsel->leader = evsel;
2076 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09002077 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002078 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09002079 desc[i].name = NULL;
2080 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09002081 evsel->nr_members = desc[i].nr_members;
2082
2083 if (i >= nr_groups || nr > 0) {
2084 pr_debug("invalid group desc\n");
2085 goto out_free;
2086 }
2087
2088 leader = evsel;
2089 nr = evsel->nr_members - 1;
2090 i++;
2091 } else if (nr) {
2092 /* This is a group member */
2093 evsel->leader = leader;
2094
2095 nr--;
2096 }
2097 }
2098
2099 if (i != nr_groups || nr != 0) {
2100 pr_debug("invalid group desc\n");
2101 goto out_free;
2102 }
2103
2104 ret = 0;
2105out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002106 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002107 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002108 free(desc);
2109
2110 return ret;
2111}
2112
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002113struct feature_ops {
2114 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2115 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01002116 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002117 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002118 const char *name;
2119 bool full_only;
2120};
2121
Robert Richter8cdfa782011-12-07 10:02:56 +01002122#define FEAT_OPA(n, func) \
2123 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01002124#define FEAT_OPP(n, func) \
2125 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2126 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01002127#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01002128 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09002129 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01002130
2131/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002132#define print_tracing_data NULL
2133#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002134
2135static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002136 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01002137 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002138 FEAT_OPP(HEADER_HOSTNAME, hostname),
2139 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2140 FEAT_OPP(HEADER_VERSION, version),
2141 FEAT_OPP(HEADER_ARCH, arch),
2142 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2143 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09002144 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002145 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02002146 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002147 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01002148 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2149 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01002150 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002151 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09002152 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002153};
2154
2155struct header_print_data {
2156 FILE *fp;
2157 bool full; /* extended list of headers */
2158};
2159
2160static int perf_file_section__fprintf_info(struct perf_file_section *section,
2161 struct perf_header *ph,
2162 int feat, int fd, void *data)
2163{
2164 struct header_print_data *hd = data;
2165
2166 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2167 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2168 "%d, continuing...\n", section->offset, feat);
2169 return 0;
2170 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002171 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002172 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002173 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002174 }
2175 if (!feat_ops[feat].print)
2176 return 0;
2177
2178 if (!feat_ops[feat].full_only || hd->full)
2179 feat_ops[feat].print(ph, fd, hd->fp);
2180 else
2181 fprintf(hd->fp, "# %s info available, use -I to display\n",
2182 feat_ops[feat].name);
2183
2184 return 0;
2185}
2186
2187int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2188{
2189 struct header_print_data hd;
2190 struct perf_header *header = &session->header;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002191 int fd = perf_data_file__fd(session->file);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002192 hd.fp = fp;
2193 hd.full = full;
2194
2195 perf_header__process_sections(header, fd, &hd,
2196 perf_file_section__fprintf_info);
2197 return 0;
2198}
2199
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002200static int do_write_feat(int fd, struct perf_header *h, int type,
2201 struct perf_file_section **p,
2202 struct perf_evlist *evlist)
2203{
2204 int err;
2205 int ret = 0;
2206
2207 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002208 if (!feat_ops[type].write)
2209 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002210
2211 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2212
2213 err = feat_ops[type].write(fd, h, evlist);
2214 if (err < 0) {
2215 pr_debug("failed to write feature %d\n", type);
2216
2217 /* undo anything written */
2218 lseek(fd, (*p)->offset, SEEK_SET);
2219
2220 return -1;
2221 }
2222 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2223 (*p)++;
2224 }
2225 return ret;
2226}
2227
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002228static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002229 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002230{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002231 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002232 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002233 int sec_size;
2234 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002235 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002236 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002237
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002238 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002239 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002240 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002241
Paul Gortmaker91b98802013-01-30 20:05:49 -05002242 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002243 if (feat_sec == NULL)
2244 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002245
2246 sec_size = sizeof(*feat_sec) * nr_sections;
2247
Jiri Olsa8d541e92013-07-17 19:49:44 +02002248 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002249 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002250
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002251 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2252 if (do_write_feat(fd, header, feat, &p, evlist))
2253 perf_header__clear_feat(header, feat);
2254 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002255
Xiao Guangrongf887f302010-02-04 16:46:42 +08002256 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002257 /*
2258 * may write more than needed due to dropped feature, but
2259 * this is okay, reader will skip the mising entries
2260 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002261 err = do_write(fd, feat_sec, sec_size);
2262 if (err < 0)
2263 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002264 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002265 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002266}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002267
Tom Zanussi8dc58102010-04-01 23:59:15 -05002268int perf_header__write_pipe(int fd)
2269{
2270 struct perf_pipe_file_header f_header;
2271 int err;
2272
2273 f_header = (struct perf_pipe_file_header){
2274 .magic = PERF_MAGIC,
2275 .size = sizeof(f_header),
2276 };
2277
2278 err = do_write(fd, &f_header, sizeof(f_header));
2279 if (err < 0) {
2280 pr_debug("failed to write perf pipe header\n");
2281 return err;
2282 }
2283
2284 return 0;
2285}
2286
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002287int perf_session__write_header(struct perf_session *session,
2288 struct perf_evlist *evlist,
2289 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002290{
2291 struct perf_file_header f_header;
2292 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002293 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002294 struct perf_evsel *evsel;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002295 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002296 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002297
2298 lseek(fd, sizeof(f_header), SEEK_SET);
2299
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002300 evlist__for_each(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002301 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2302 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002303 if (err < 0) {
2304 pr_debug("failed to write perf header\n");
2305 return err;
2306 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002307 }
2308
Jiri Olsa944d62b2013-07-17 19:49:43 +02002309 attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002310
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002311 evlist__for_each(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002312 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002313 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002314 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002315 .offset = evsel->id_offset,
2316 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002317 }
2318 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002319 err = do_write(fd, &f_attr, sizeof(f_attr));
2320 if (err < 0) {
2321 pr_debug("failed to write perf header attribute\n");
2322 return err;
2323 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002324 }
2325
Adrian Hunterd645c442013-12-11 14:36:28 +02002326 if (!header->data_offset)
2327 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002328 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002329
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002330 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002331 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002332 if (err < 0)
2333 return err;
2334 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002335
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002336 f_header = (struct perf_file_header){
2337 .magic = PERF_MAGIC,
2338 .size = sizeof(f_header),
2339 .attr_size = sizeof(f_attr),
2340 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002341 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002342 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002343 },
2344 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002345 .offset = header->data_offset,
2346 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002347 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002348 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002349 };
2350
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002351 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002352
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002353 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002354 err = do_write(fd, &f_header, sizeof(f_header));
2355 if (err < 0) {
2356 pr_debug("failed to write perf header\n");
2357 return err;
2358 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002359 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002360
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002361 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002362}
2363
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002364static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002365 int fd, void *buf, size_t size)
2366{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002367 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002368 return -1;
2369
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002370 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002371 mem_bswap_64(buf, size);
2372
2373 return 0;
2374}
2375
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002376int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002377 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002378 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002379 struct perf_header *ph,
2380 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002381{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002382 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002383 int nr_sections;
2384 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002385 int feat;
2386 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002387
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002388 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002389 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002390 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002391
Paul Gortmaker91b98802013-01-30 20:05:49 -05002392 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002393 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002394 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002395
2396 sec_size = sizeof(*feat_sec) * nr_sections;
2397
Jiri Olsa8d541e92013-07-17 19:49:44 +02002398 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002399
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002400 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2401 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002402 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002403
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002404 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2405 err = process(sec++, header, feat, fd, data);
2406 if (err < 0)
2407 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002408 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002409 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002410out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002411 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002412 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002413}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002414
Stephane Eranian114382a2012-02-09 23:21:08 +01002415static const int attr_file_abi_sizes[] = {
2416 [0] = PERF_ATTR_SIZE_VER0,
2417 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002418 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002419 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian114382a2012-02-09 23:21:08 +01002420 0,
2421};
2422
2423/*
2424 * In the legacy file format, the magic number is not used to encode endianness.
2425 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2426 * on ABI revisions, we need to try all combinations for all endianness to
2427 * detect the endianness.
2428 */
2429static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2430{
2431 uint64_t ref_size, attr_size;
2432 int i;
2433
2434 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2435 ref_size = attr_file_abi_sizes[i]
2436 + sizeof(struct perf_file_section);
2437 if (hdr_sz != ref_size) {
2438 attr_size = bswap_64(hdr_sz);
2439 if (attr_size != ref_size)
2440 continue;
2441
2442 ph->needs_swap = true;
2443 }
2444 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2445 i,
2446 ph->needs_swap);
2447 return 0;
2448 }
2449 /* could not determine endianness */
2450 return -1;
2451}
2452
2453#define PERF_PIPE_HDR_VER0 16
2454
2455static const size_t attr_pipe_abi_sizes[] = {
2456 [0] = PERF_PIPE_HDR_VER0,
2457 0,
2458};
2459
2460/*
2461 * In the legacy pipe format, there is an implicit assumption that endiannesss
2462 * between host recording the samples, and host parsing the samples is the
2463 * same. This is not always the case given that the pipe output may always be
2464 * redirected into a file and analyzed on a different machine with possibly a
2465 * different endianness and perf_event ABI revsions in the perf tool itself.
2466 */
2467static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2468{
2469 u64 attr_size;
2470 int i;
2471
2472 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2473 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2474 attr_size = bswap_64(hdr_sz);
2475 if (attr_size != hdr_sz)
2476 continue;
2477
2478 ph->needs_swap = true;
2479 }
2480 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2481 return 0;
2482 }
2483 return -1;
2484}
2485
Feng Tange84ba4e2012-10-30 11:56:07 +08002486bool is_perf_magic(u64 magic)
2487{
2488 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2489 || magic == __perf_magic2
2490 || magic == __perf_magic2_sw)
2491 return true;
2492
2493 return false;
2494}
2495
Stephane Eranian114382a2012-02-09 23:21:08 +01002496static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2497 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002498{
2499 int ret;
2500
2501 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002502 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002503 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002504 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002505 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002506 if (is_pipe)
2507 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002508
Stephane Eranian114382a2012-02-09 23:21:08 +01002509 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002510 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002511 /*
2512 * the new magic number serves two purposes:
2513 * - unique number to identify actual perf.data files
2514 * - encode endianness of file
2515 */
Stephane Eranian73323f52012-02-02 13:54:44 +01002516
Stephane Eranian114382a2012-02-09 23:21:08 +01002517 /* check magic number with one endianness */
2518 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002519 return 0;
2520
Stephane Eranian114382a2012-02-09 23:21:08 +01002521 /* check magic number with opposite endianness */
2522 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002523 return -1;
2524
2525 ph->needs_swap = true;
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002526 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002527
2528 return 0;
2529}
2530
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002531int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002532 struct perf_header *ph, int fd)
2533{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002534 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002535
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002536 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002537
Stephane Eranian73323f52012-02-02 13:54:44 +01002538 ret = readn(fd, header, sizeof(*header));
2539 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002540 return -1;
2541
Stephane Eranian114382a2012-02-09 23:21:08 +01002542 if (check_magic_endian(header->magic,
2543 header->attr_size, false, ph) < 0) {
2544 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002545 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002546 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002547
Stephane Eranian73323f52012-02-02 13:54:44 +01002548 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002549 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002550 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002551 }
2552
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002553 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002554 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002555 if (header->size == offsetof(typeof(*header), adds_features))
2556 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002557 else
2558 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002559 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002560 /*
2561 * feature bitmap is declared as an array of unsigned longs --
2562 * not good since its size can differ between the host that
2563 * generated the data file and the host analyzing the file.
2564 *
2565 * We need to handle endianness, but we don't know the size of
2566 * the unsigned long where the file was generated. Take a best
2567 * guess at determining it: try 64-bit swap first (ie., file
2568 * created on a 64-bit host), and check if the hostname feature
2569 * bit is set (this feature bit is forced on as of fbe96f2).
2570 * If the bit is not, undo the 64-bit swap and try a 32-bit
2571 * swap. If the hostname bit is still not set (e.g., older data
2572 * file), punt and fallback to the original behavior --
2573 * clearing all feature bits and setting buildid.
2574 */
David Ahern80c01202012-06-08 11:47:51 -03002575 mem_bswap_64(&header->adds_features,
2576 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002577
2578 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002579 /* unswap as u64 */
2580 mem_bswap_64(&header->adds_features,
2581 BITS_TO_U64(HEADER_FEAT_BITS));
2582
2583 /* unswap as u32 */
2584 mem_bswap_32(&header->adds_features,
2585 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002586 }
2587
2588 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2589 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2590 set_bit(HEADER_BUILD_ID, header->adds_features);
2591 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002592 }
2593
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002594 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002595 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002596
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002597 ph->data_offset = header->data.offset;
2598 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002599 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002600 return 0;
2601}
2602
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002603static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002604 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002605 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002606{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002607 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002608 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002609 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002610 return 0;
2611 }
2612
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002613 if (feat >= HEADER_LAST_FEATURE) {
2614 pr_debug("unknown feature %d, continuing...\n", feat);
2615 return 0;
2616 }
2617
Robert Richterf1c67db2012-02-10 15:41:56 +01002618 if (!feat_ops[feat].process)
2619 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002620
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002621 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002622}
2623
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002624static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002625 struct perf_header *ph, int fd,
2626 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002627{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002628 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002629
2630 ret = readn(fd, header, sizeof(*header));
2631 if (ret <= 0)
2632 return -1;
2633
Stephane Eranian114382a2012-02-09 23:21:08 +01002634 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2635 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002636 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002637 }
2638
2639 if (ph->needs_swap)
2640 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002641
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002642 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002643 return -1;
2644
Tom Zanussi8dc58102010-04-01 23:59:15 -05002645 return 0;
2646}
2647
Jiri Olsad4339562013-07-17 19:49:41 +02002648static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002649{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002650 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002651 struct perf_pipe_file_header f_header;
2652
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002653 if (perf_file_header__read_pipe(&f_header, header,
2654 perf_data_file__fd(session->file),
Tom Zanussi454c4072010-05-01 01:41:20 -05002655 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002656 pr_debug("incompatible file format\n");
2657 return -EINVAL;
2658 }
2659
Tom Zanussi8dc58102010-04-01 23:59:15 -05002660 return 0;
2661}
2662
Stephane Eranian69996df2012-02-09 23:21:06 +01002663static int read_attr(int fd, struct perf_header *ph,
2664 struct perf_file_attr *f_attr)
2665{
2666 struct perf_event_attr *attr = &f_attr->attr;
2667 size_t sz, left;
2668 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002669 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002670
2671 memset(f_attr, 0, sizeof(*f_attr));
2672
2673 /* read minimal guaranteed structure */
2674 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2675 if (ret <= 0) {
2676 pr_debug("cannot read %d bytes of header attr\n",
2677 PERF_ATTR_SIZE_VER0);
2678 return -1;
2679 }
2680
2681 /* on file perf_event_attr size */
2682 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002683
Stephane Eranian69996df2012-02-09 23:21:06 +01002684 if (ph->needs_swap)
2685 sz = bswap_32(sz);
2686
2687 if (sz == 0) {
2688 /* assume ABI0 */
2689 sz = PERF_ATTR_SIZE_VER0;
2690 } else if (sz > our_sz) {
2691 pr_debug("file uses a more recent and unsupported ABI"
2692 " (%zu bytes extra)\n", sz - our_sz);
2693 return -1;
2694 }
2695 /* what we have not yet read and that we know about */
2696 left = sz - PERF_ATTR_SIZE_VER0;
2697 if (left) {
2698 void *ptr = attr;
2699 ptr += PERF_ATTR_SIZE_VER0;
2700
2701 ret = readn(fd, ptr, left);
2702 }
2703 /* read perf_file_section, ids are read in caller */
2704 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2705
2706 return ret <= 0 ? -1 : 0;
2707}
2708
Namhyung Kim831394b2012-09-06 11:10:46 +09002709static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2710 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002711{
Namhyung Kim831394b2012-09-06 11:10:46 +09002712 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002713 char bf[128];
2714
Namhyung Kim831394b2012-09-06 11:10:46 +09002715 /* already prepared */
2716 if (evsel->tp_format)
2717 return 0;
2718
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002719 if (pevent == NULL) {
2720 pr_debug("broken or missing trace data\n");
2721 return -1;
2722 }
2723
Namhyung Kim831394b2012-09-06 11:10:46 +09002724 event = pevent_find_event(pevent, evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002725 if (event == NULL)
2726 return -1;
2727
Namhyung Kim831394b2012-09-06 11:10:46 +09002728 if (!evsel->name) {
2729 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2730 evsel->name = strdup(bf);
2731 if (evsel->name == NULL)
2732 return -1;
2733 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002734
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002735 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002736 return 0;
2737}
2738
Namhyung Kim831394b2012-09-06 11:10:46 +09002739static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2740 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002741{
2742 struct perf_evsel *pos;
2743
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002744 evlist__for_each(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002745 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2746 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002747 return -1;
2748 }
2749
2750 return 0;
2751}
2752
Jiri Olsad4339562013-07-17 19:49:41 +02002753int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002754{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002755 struct perf_data_file *file = session->file;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002756 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002757 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002758 struct perf_file_attr f_attr;
2759 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002760 int nr_attrs, nr_ids, i, j;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002761 int fd = perf_data_file__fd(file);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002762
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002763 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002764 if (session->evlist == NULL)
2765 return -ENOMEM;
2766
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002767 if (perf_data_file__is_pipe(file))
Jiri Olsad4339562013-07-17 19:49:41 +02002768 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002769
Stephane Eranian69996df2012-02-09 23:21:06 +01002770 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002771 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002772
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002773 /*
2774 * Sanity check that perf.data was written cleanly; data size is
2775 * initialized to 0 and updated only if the on_exit function is run.
2776 * If data size is still 0 then the file contains only partial
2777 * information. Just warn user and process it as much as it can.
2778 */
2779 if (f_header.data.size == 0) {
2780 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2781 "Was the 'perf record' command properly terminated?\n",
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002782 file->path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002783 }
2784
Stephane Eranian69996df2012-02-09 23:21:06 +01002785 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002786 lseek(fd, f_header.attrs.offset, SEEK_SET);
2787
2788 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002789 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002790 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002791
Stephane Eranian69996df2012-02-09 23:21:06 +01002792 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002793 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002794
David Aherneda39132011-07-15 12:34:09 -06002795 if (header->needs_swap)
2796 perf_event__attr_swap(&f_attr.attr);
2797
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002798 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002799 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002800
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002801 if (evsel == NULL)
2802 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002803
2804 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002805 /*
2806 * Do it before so that if perf_evsel__alloc_id fails, this
2807 * entry gets purged too at perf_evlist__delete().
2808 */
2809 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002810
2811 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002812 /*
2813 * We don't have the cpu and thread maps on the header, so
2814 * for allocating the perf_sample_id table we fake 1 cpu and
2815 * hattr->ids threads.
2816 */
2817 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2818 goto out_delete_evlist;
2819
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002820 lseek(fd, f_attr.ids.offset, SEEK_SET);
2821
2822 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002823 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002824 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002825
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002826 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002827 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002828
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002829 lseek(fd, tmp, SEEK_SET);
2830 }
2831
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002832 symbol_conf.nr_events = nr_attrs;
2833
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002834 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002835 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002836
Namhyung Kim831394b2012-09-06 11:10:46 +09002837 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002838 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002839 goto out_delete_evlist;
2840
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002841 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002842out_errno:
2843 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002844
2845out_delete_evlist:
2846 perf_evlist__delete(session->evlist);
2847 session->evlist = NULL;
2848 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002849}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002850
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002851int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002852 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002853 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002854{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002855 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002856 size_t size;
2857 int err;
2858
2859 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002860 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002861 size += sizeof(struct perf_event_header);
2862 size += ids * sizeof(u64);
2863
2864 ev = malloc(size);
2865
Chris Samuelce47dc52010-11-13 13:35:06 +11002866 if (ev == NULL)
2867 return -ENOMEM;
2868
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002869 ev->attr.attr = *attr;
2870 memcpy(ev->attr.id, id, ids * sizeof(u64));
2871
2872 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002873 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002874
Robert Richterf4d83432012-08-16 21:10:17 +02002875 if (ev->attr.header.size == size)
2876 err = process(tool, ev, NULL, NULL);
2877 else
2878 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002879
2880 free(ev);
2881
2882 return err;
2883}
2884
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002885int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002886 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002887 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002888{
Robert Richter6606f872012-08-16 21:10:19 +02002889 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002890 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002891
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002892 evlist__for_each(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002893 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2894 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002895 if (err) {
2896 pr_debug("failed to create perf header attribute\n");
2897 return err;
2898 }
2899 }
2900
2901 return err;
2902}
2903
Adrian Hunter47c3d102013-07-04 16:20:21 +03002904int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2905 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002906 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002907{
Robert Richterf4d83432012-08-16 21:10:17 +02002908 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002909 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002910 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002911
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002912 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002913 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002914 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002915 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002916 }
2917
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002918 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002919 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002920 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002921
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002922 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002923
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002924 ids = event->header.size;
2925 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002926 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002927 /*
2928 * We don't have the cpu and thread maps on the header, so
2929 * for allocating the perf_sample_id table we fake 1 cpu and
2930 * hattr->ids threads.
2931 */
2932 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2933 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002934
2935 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002936 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002937 }
2938
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03002939 symbol_conf.nr_events = evlist->nr_entries;
2940
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002941 return 0;
2942}
Tom Zanussicd19a032010-04-01 23:59:20 -05002943
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002944int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002945 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002946 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05002947{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002948 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02002949 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05002950 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002951 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05002952
Jiri Olsa29208e52011-10-20 15:59:43 +02002953 /*
2954 * We are going to store the size of the data followed
2955 * by the data contents. Since the fd descriptor is a pipe,
2956 * we cannot seek back to store the size of the data once
2957 * we know it. Instead we:
2958 *
2959 * - write the tracing data to the temp file
2960 * - get/write the data size to pipe
2961 * - write the tracing data from the temp file
2962 * to the pipe
2963 */
2964 tdata = tracing_data_get(&evlist->entries, fd, true);
2965 if (!tdata)
2966 return -1;
2967
Tom Zanussi92155452010-04-01 23:59:21 -05002968 memset(&ev, 0, sizeof(ev));
2969
2970 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02002971 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002972 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05002973 padding = aligned_size - size;
2974 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2975 ev.tracing_data.size = aligned_size;
2976
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002977 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05002978
Jiri Olsa29208e52011-10-20 15:59:43 +02002979 /*
2980 * The put function will copy all the tracing data
2981 * stored in temp file to the pipe.
2982 */
2983 tracing_data_put(tdata);
2984
Tom Zanussi92155452010-04-01 23:59:21 -05002985 write_padded(fd, NULL, 0, padding);
2986
2987 return aligned_size;
2988}
2989
Adrian Hunter47c3d102013-07-04 16:20:21 +03002990int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
2991 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002992 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05002993{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002994 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002995 int fd = perf_data_file__fd(session->file);
2996 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05002997 char buf[BUFSIZ];
2998
2999 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003000 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05003001 SEEK_SET);
3002
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003003 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003004 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003005 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003006
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003007 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003008 pr_err("%s: reading input file", __func__);
3009 return -1;
3010 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003011 if (session->repipe) {
3012 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003013 if (retw <= 0 || retw != padding) {
3014 pr_err("%s: repiping tracing data padding", __func__);
3015 return -1;
3016 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003017 }
Tom Zanussi92155452010-04-01 23:59:21 -05003018
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003019 if (size_read + padding != size) {
3020 pr_err("%s: tracing data size mismatch", __func__);
3021 return -1;
3022 }
Tom Zanussi92155452010-04-01 23:59:21 -05003023
Namhyung Kim831394b2012-09-06 11:10:46 +09003024 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003025 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003026
Tom Zanussi92155452010-04-01 23:59:21 -05003027 return size_read + padding;
3028}
Tom Zanussic7929e42010-04-01 23:59:22 -05003029
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003030int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003031 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003032 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003033 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003034{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003035 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003036 size_t len;
3037 int err = 0;
3038
3039 if (!pos->hit)
3040 return err;
3041
3042 memset(&ev, 0, sizeof(ev));
3043
3044 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003045 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003046 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3047 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3048 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003049 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003050 ev.build_id.header.size = sizeof(ev.build_id) + len;
3051 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3052
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003053 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003054
3055 return err;
3056}
3057
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003058int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003059 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003060 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003061{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003062 __event_process_build_id(&event->build_id,
3063 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003064 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003065 return 0;
3066}
Stephane Eraniana1ac1d32010-06-17 11:39:01 +02003067
3068void disable_buildid_cache(void)
3069{
3070 no_buildid_cache = true;
3071}