blob: f6081cb3fca3f6cc2244d0aca58ac94c09413c6e [file] [log] [blame]
Xiao Guangrongb8f46c52010-02-03 11:53:14 +08001#define _FILE_OFFSET_BITS 64
2
Arnaldo Carvalho de Meloa9072bc2011-10-26 12:41:38 -02003#include "util.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02004#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02005#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02006#include <unistd.h>
7#include <stdio.h>
8#include <stdlib.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +01009#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -020010#include <linux/kernel.h>
Robert Richterb1e5a9b2011-12-07 10:02:57 +010011#include <linux/bitops.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020012#include <sys/utsname.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020013
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020014#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030015#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020016#include "header.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020017#include "../perf.h"
18#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020019#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010020#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010021#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020022#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020023#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020024#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090025#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020026#include "build-id.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020027
Stephane Eraniana1ac1d32010-06-17 11:39:01 +020028static bool no_buildid_cache = false;
29
Robert Richterdb146f02012-08-16 21:10:20 +020030static int trace_event_count;
31static struct perf_trace_event_type *trace_events;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020032
Stephane Eranianfbe96f22011-09-30 15:40:40 +020033static u32 header_argc;
34static const char **header_argv;
35
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -020036int perf_header__push_event(u64 id, const char *name)
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020037{
Ulrich Drepperf3054c7782012-04-23 09:04:29 -040038 struct perf_trace_event_type *nevents;
39
Arjan van de Venec60a3f2009-09-19 13:36:30 +020040 if (strlen(name) > MAX_EVENT_NAME)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020041 pr_warning("Event %s will be truncated\n", name);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020042
Robert Richterdb146f02012-08-16 21:10:20 +020043 nevents = realloc(trace_events, (trace_event_count + 1) * sizeof(*trace_events));
Ulrich Drepperf3054c7782012-04-23 09:04:29 -040044 if (nevents == NULL)
45 return -ENOMEM;
Robert Richterdb146f02012-08-16 21:10:20 +020046 trace_events = nevents;
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -020047
Robert Richterdb146f02012-08-16 21:10:20 +020048 memset(&trace_events[trace_event_count], 0, sizeof(struct perf_trace_event_type));
49 trace_events[trace_event_count].event_id = id;
50 strncpy(trace_events[trace_event_count].name, name, MAX_EVENT_NAME - 1);
51 trace_event_count++;
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -020052 return 0;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020053}
54
55char *perf_header__find_event(u64 id)
56{
57 int i;
Robert Richterdb146f02012-08-16 21:10:20 +020058 for (i = 0 ; i < trace_event_count; i++) {
59 if (trace_events[i].event_id == id)
60 return trace_events[i].name;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020061 }
62 return NULL;
63}
64
Stephane Eranian73323f52012-02-02 13:54:44 +010065/*
66 * magic2 = "PERFILE2"
67 * must be a numerical value to let the endianness
68 * determine the memory layout. That way we are able
69 * to detect endianness when reading the perf.data file
70 * back.
71 *
72 * we check for legacy (PERFFILE) format.
73 */
74static const char *__perf_magic1 = "PERFFILE";
75static const u64 __perf_magic2 = 0x32454c4946524550ULL;
76static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020077
Stephane Eranian73323f52012-02-02 13:54:44 +010078#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020079
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020080struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020081 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020082 struct perf_file_section ids;
83};
84
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030085void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020086{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030087 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020088}
89
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030090void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020091{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030092 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020093}
94
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030095bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020096{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030097 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020098}
99
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200100static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200101{
102 while (size) {
103 int ret = write(fd, buf, size);
104
105 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200106 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200107
108 size -= ret;
109 buf += ret;
110 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200111
112 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200113}
114
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200115#define NAME_ALIGN 64
116
117static int write_padded(int fd, const void *bf, size_t count,
118 size_t count_aligned)
119{
120 static const char zero_buf[NAME_ALIGN];
121 int err = do_write(fd, bf, count);
122
123 if (!err)
124 err = do_write(fd, zero_buf, count_aligned - count);
125
126 return err;
127}
128
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200129static int do_write_string(int fd, const char *str)
130{
131 u32 len, olen;
132 int ret;
133
134 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300135 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200136
137 /* write len, incl. \0 */
138 ret = do_write(fd, &len, sizeof(len));
139 if (ret < 0)
140 return ret;
141
142 return write_padded(fd, str, olen, len);
143}
144
145static char *do_read_string(int fd, struct perf_header *ph)
146{
147 ssize_t sz, ret;
148 u32 len;
149 char *buf;
150
Namhyung Kim5323f602012-12-17 15:38:54 +0900151 sz = readn(fd, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200152 if (sz < (ssize_t)sizeof(len))
153 return NULL;
154
155 if (ph->needs_swap)
156 len = bswap_32(len);
157
158 buf = malloc(len);
159 if (!buf)
160 return NULL;
161
Namhyung Kim5323f602012-12-17 15:38:54 +0900162 ret = readn(fd, buf, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200163 if (ret == (ssize_t)len) {
164 /*
165 * strings are padded by zeroes
166 * thus the actual strlen of buf
167 * may be less than len
168 */
169 return buf;
170 }
171
172 free(buf);
173 return NULL;
174}
175
176int
177perf_header__set_cmdline(int argc, const char **argv)
178{
179 int i;
180
David Ahern56e6f602012-07-29 20:53:51 -0600181 /*
182 * If header_argv has already been set, do not override it.
183 * This allows a command to set the cmdline, parse args and
184 * then call another builtin function that implements a
185 * command -- e.g, cmd_kvm calling cmd_record.
186 */
187 if (header_argv)
188 return 0;
189
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200190 header_argc = (u32)argc;
191
192 /* do not include NULL termination */
193 header_argv = calloc(argc, sizeof(char *));
194 if (!header_argv)
195 return -ENOMEM;
196
197 /*
198 * must copy argv contents because it gets moved
199 * around during option parsing
200 */
201 for (i = 0; i < argc ; i++)
202 header_argv[i] = argv[i];
203
204 return 0;
205}
206
Robert Richter1b549502011-12-07 10:02:53 +0100207#define dsos__for_each_with_build_id(pos, head) \
208 list_for_each_entry(pos, head, node) \
209 if (!pos->has_build_id) \
210 continue; \
211 else
212
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200213static int write_buildid(char *name, size_t name_len, u8 *build_id,
214 pid_t pid, u16 misc, int fd)
215{
216 int err;
217 struct build_id_event b;
218 size_t len;
219
220 len = name_len + 1;
221 len = PERF_ALIGN(len, NAME_ALIGN);
222
223 memset(&b, 0, sizeof(b));
224 memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
225 b.pid = pid;
226 b.header.misc = misc;
227 b.header.size = sizeof(b) + len;
228
229 err = do_write(fd, &b, sizeof(b));
230 if (err < 0)
231 return err;
232
233 return write_padded(fd, name, name_len + 1, len);
234}
235
Robert Richter1b549502011-12-07 10:02:53 +0100236static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
237 u16 misc, int fd)
238{
239 struct dso *pos;
240
241 dsos__for_each_with_build_id(pos, head) {
242 int err;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200243 char *name;
244 size_t name_len;
Robert Richter1b549502011-12-07 10:02:53 +0100245
246 if (!pos->hit)
247 continue;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200248
249 if (is_vdso_map(pos->short_name)) {
250 name = (char *) VDSO__MAP_NAME;
251 name_len = sizeof(VDSO__MAP_NAME) + 1;
252 } else {
253 name = pos->long_name;
254 name_len = pos->long_name_len + 1;
255 }
256
257 err = write_buildid(name, name_len, pos->build_id,
258 pid, misc, fd);
259 if (err)
Robert Richter1b549502011-12-07 10:02:53 +0100260 return err;
261 }
262
263 return 0;
264}
265
266static int machine__write_buildid_table(struct machine *machine, int fd)
267{
268 int err;
269 u16 kmisc = PERF_RECORD_MISC_KERNEL,
270 umisc = PERF_RECORD_MISC_USER;
271
272 if (!machine__is_host(machine)) {
273 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
274 umisc = PERF_RECORD_MISC_GUEST_USER;
275 }
276
277 err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid,
278 kmisc, fd);
279 if (err == 0)
280 err = __dsos__write_buildid_table(&machine->user_dsos,
281 machine->pid, umisc, fd);
282 return err;
283}
284
285static int dsos__write_buildid_table(struct perf_header *header, int fd)
286{
287 struct perf_session *session = container_of(header,
288 struct perf_session, header);
289 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300290 int err = machine__write_buildid_table(&session->machines.host, fd);
Robert Richter1b549502011-12-07 10:02:53 +0100291
292 if (err)
293 return err;
294
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300295 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
Robert Richter1b549502011-12-07 10:02:53 +0100296 struct machine *pos = rb_entry(nd, struct machine, rb_node);
297 err = machine__write_buildid_table(pos, fd);
298 if (err)
299 break;
300 }
301 return err;
302}
303
304int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200305 const char *name, bool is_kallsyms, bool is_vdso)
Robert Richter1b549502011-12-07 10:02:53 +0100306{
307 const size_t size = PATH_MAX;
308 char *realname, *filename = zalloc(size),
309 *linkname = zalloc(size), *targetname;
310 int len, err = -1;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200311 bool slash = is_kallsyms || is_vdso;
Robert Richter1b549502011-12-07 10:02:53 +0100312
313 if (is_kallsyms) {
314 if (symbol_conf.kptr_restrict) {
315 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
Thomas Jaroschfdae6372013-01-25 11:21:39 +0100316 err = 0;
317 goto out_free;
Robert Richter1b549502011-12-07 10:02:53 +0100318 }
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200319 realname = (char *) name;
Robert Richter1b549502011-12-07 10:02:53 +0100320 } else
321 realname = realpath(name, NULL);
322
323 if (realname == NULL || filename == NULL || linkname == NULL)
324 goto out_free;
325
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300326 len = scnprintf(filename, size, "%s%s%s",
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200327 debugdir, slash ? "/" : "",
328 is_vdso ? VDSO__MAP_NAME : realname);
Robert Richter1b549502011-12-07 10:02:53 +0100329 if (mkdir_p(filename, 0755))
330 goto out_free;
331
Namhyung Kimafda0f92012-05-01 23:19:36 +0900332 snprintf(filename + len, size - len, "/%s", sbuild_id);
Robert Richter1b549502011-12-07 10:02:53 +0100333
334 if (access(filename, F_OK)) {
335 if (is_kallsyms) {
336 if (copyfile("/proc/kallsyms", filename))
337 goto out_free;
338 } else if (link(realname, filename) && copyfile(name, filename))
339 goto out_free;
340 }
341
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300342 len = scnprintf(linkname, size, "%s/.build-id/%.2s",
Robert Richter1b549502011-12-07 10:02:53 +0100343 debugdir, sbuild_id);
344
345 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
346 goto out_free;
347
348 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
349 targetname = filename + strlen(debugdir) - 5;
350 memcpy(targetname, "../..", 5);
351
352 if (symlink(targetname, linkname) == 0)
353 err = 0;
354out_free:
355 if (!is_kallsyms)
356 free(realname);
357 free(filename);
358 free(linkname);
359 return err;
360}
361
362static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
363 const char *name, const char *debugdir,
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200364 bool is_kallsyms, bool is_vdso)
Robert Richter1b549502011-12-07 10:02:53 +0100365{
366 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
367
368 build_id__sprintf(build_id, build_id_size, sbuild_id);
369
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200370 return build_id_cache__add_s(sbuild_id, debugdir, name,
371 is_kallsyms, is_vdso);
Robert Richter1b549502011-12-07 10:02:53 +0100372}
373
374int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
375{
376 const size_t size = PATH_MAX;
377 char *filename = zalloc(size),
378 *linkname = zalloc(size);
379 int err = -1;
380
381 if (filename == NULL || linkname == NULL)
382 goto out_free;
383
384 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
385 debugdir, sbuild_id, sbuild_id + 2);
386
387 if (access(linkname, F_OK))
388 goto out_free;
389
390 if (readlink(linkname, filename, size - 1) < 0)
391 goto out_free;
392
393 if (unlink(linkname))
394 goto out_free;
395
396 /*
397 * Since the link is relative, we must make it absolute:
398 */
399 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
400 debugdir, sbuild_id, filename);
401
402 if (unlink(linkname))
403 goto out_free;
404
405 err = 0;
406out_free:
407 free(filename);
408 free(linkname);
409 return err;
410}
411
412static int dso__cache_build_id(struct dso *dso, const char *debugdir)
413{
414 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200415 bool is_vdso = is_vdso_map(dso->short_name);
Robert Richter1b549502011-12-07 10:02:53 +0100416
417 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id),
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200418 dso->long_name, debugdir,
419 is_kallsyms, is_vdso);
Robert Richter1b549502011-12-07 10:02:53 +0100420}
421
422static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
423{
424 struct dso *pos;
425 int err = 0;
426
427 dsos__for_each_with_build_id(pos, head)
428 if (dso__cache_build_id(pos, debugdir))
429 err = -1;
430
431 return err;
432}
433
434static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
435{
436 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir);
437 ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir);
438 return ret;
439}
440
441static int perf_session__cache_build_ids(struct perf_session *session)
442{
443 struct rb_node *nd;
444 int ret;
445 char debugdir[PATH_MAX];
446
447 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
448
449 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
450 return -1;
451
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300452 ret = machine__cache_build_ids(&session->machines.host, debugdir);
Robert Richter1b549502011-12-07 10:02:53 +0100453
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300454 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
Robert Richter1b549502011-12-07 10:02:53 +0100455 struct machine *pos = rb_entry(nd, struct machine, rb_node);
456 ret |= machine__cache_build_ids(pos, debugdir);
457 }
458 return ret ? -1 : 0;
459}
460
461static bool machine__read_build_ids(struct machine *machine, bool with_hits)
462{
463 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
464 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
465 return ret;
466}
467
468static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
469{
470 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300471 bool ret = machine__read_build_ids(&session->machines.host, with_hits);
Robert Richter1b549502011-12-07 10:02:53 +0100472
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300473 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
Robert Richter1b549502011-12-07 10:02:53 +0100474 struct machine *pos = rb_entry(nd, struct machine, rb_node);
475 ret |= machine__read_build_ids(pos, with_hits);
476 }
477
478 return ret;
479}
480
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300481static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200482 struct perf_evlist *evlist)
483{
484 return read_tracing_data(fd, &evlist->entries);
485}
486
487
488static int write_build_id(int fd, struct perf_header *h,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300489 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200490{
491 struct perf_session *session;
492 int err;
493
494 session = container_of(h, struct perf_session, header);
495
Robert Richtere20960c2011-12-07 10:02:55 +0100496 if (!perf_session__read_build_ids(session, true))
497 return -1;
498
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200499 err = dsos__write_buildid_table(h, fd);
500 if (err < 0) {
501 pr_debug("failed to write buildid table\n");
502 return err;
503 }
504 if (!no_buildid_cache)
505 perf_session__cache_build_ids(session);
506
507 return 0;
508}
509
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300510static int write_hostname(int fd, struct perf_header *h __maybe_unused,
511 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200512{
513 struct utsname uts;
514 int ret;
515
516 ret = uname(&uts);
517 if (ret < 0)
518 return -1;
519
520 return do_write_string(fd, uts.nodename);
521}
522
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300523static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
524 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200525{
526 struct utsname uts;
527 int ret;
528
529 ret = uname(&uts);
530 if (ret < 0)
531 return -1;
532
533 return do_write_string(fd, uts.release);
534}
535
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300536static int write_arch(int fd, struct perf_header *h __maybe_unused,
537 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200538{
539 struct utsname uts;
540 int ret;
541
542 ret = uname(&uts);
543 if (ret < 0)
544 return -1;
545
546 return do_write_string(fd, uts.machine);
547}
548
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300549static int write_version(int fd, struct perf_header *h __maybe_unused,
550 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200551{
552 return do_write_string(fd, perf_version_string);
553}
554
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300555static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
556 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200557{
558#ifndef CPUINFO_PROC
559#define CPUINFO_PROC NULL
560#endif
561 FILE *file;
562 char *buf = NULL;
563 char *s, *p;
564 const char *search = CPUINFO_PROC;
565 size_t len = 0;
566 int ret = -1;
567
568 if (!search)
569 return -1;
570
571 file = fopen("/proc/cpuinfo", "r");
572 if (!file)
573 return -1;
574
575 while (getline(&buf, &len, file) > 0) {
576 ret = strncmp(buf, search, strlen(search));
577 if (!ret)
578 break;
579 }
580
581 if (ret)
582 goto done;
583
584 s = buf;
585
586 p = strchr(buf, ':');
587 if (p && *(p+1) == ' ' && *(p+2))
588 s = p + 2;
589 p = strchr(s, '\n');
590 if (p)
591 *p = '\0';
592
593 /* squash extra space characters (branding string) */
594 p = s;
595 while (*p) {
596 if (isspace(*p)) {
597 char *r = p + 1;
598 char *q = r;
599 *p = ' ';
600 while (*q && isspace(*q))
601 q++;
602 if (q != (p+1))
603 while ((*r++ = *q++));
604 }
605 p++;
606 }
607 ret = do_write_string(fd, s);
608done:
609 free(buf);
610 fclose(file);
611 return ret;
612}
613
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300614static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
615 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200616{
617 long nr;
618 u32 nrc, nra;
619 int ret;
620
621 nr = sysconf(_SC_NPROCESSORS_CONF);
622 if (nr < 0)
623 return -1;
624
625 nrc = (u32)(nr & UINT_MAX);
626
627 nr = sysconf(_SC_NPROCESSORS_ONLN);
628 if (nr < 0)
629 return -1;
630
631 nra = (u32)(nr & UINT_MAX);
632
633 ret = do_write(fd, &nrc, sizeof(nrc));
634 if (ret < 0)
635 return ret;
636
637 return do_write(fd, &nra, sizeof(nra));
638}
639
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300640static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200641 struct perf_evlist *evlist)
642{
Robert Richter6606f872012-08-16 21:10:19 +0200643 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900644 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200645 int ret;
646
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900647 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200648
649 /*
650 * write number of events
651 */
652 ret = do_write(fd, &nre, sizeof(nre));
653 if (ret < 0)
654 return ret;
655
656 /*
657 * size of perf_event_attr struct
658 */
Robert Richter6606f872012-08-16 21:10:19 +0200659 sz = (u32)sizeof(evsel->attr);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200660 ret = do_write(fd, &sz, sizeof(sz));
661 if (ret < 0)
662 return ret;
663
Robert Richter6606f872012-08-16 21:10:19 +0200664 list_for_each_entry(evsel, &evlist->entries, node) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200665
Robert Richter6606f872012-08-16 21:10:19 +0200666 ret = do_write(fd, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200667 if (ret < 0)
668 return ret;
669 /*
670 * write number of unique id per event
671 * there is one id per instance of an event
672 *
673 * copy into an nri to be independent of the
674 * type of ids,
675 */
Robert Richter6606f872012-08-16 21:10:19 +0200676 nri = evsel->ids;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200677 ret = do_write(fd, &nri, sizeof(nri));
678 if (ret < 0)
679 return ret;
680
681 /*
682 * write event string as passed on cmdline
683 */
Robert Richter6606f872012-08-16 21:10:19 +0200684 ret = do_write_string(fd, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200685 if (ret < 0)
686 return ret;
687 /*
688 * write unique ids for this event
689 */
Robert Richter6606f872012-08-16 21:10:19 +0200690 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200691 if (ret < 0)
692 return ret;
693 }
694 return 0;
695}
696
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300697static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
698 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200699{
700 char buf[MAXPATHLEN];
701 char proc[32];
702 u32 i, n;
703 int ret;
704
705 /*
706 * actual atual path to perf binary
707 */
708 sprintf(proc, "/proc/%d/exe", getpid());
709 ret = readlink(proc, buf, sizeof(buf));
710 if (ret <= 0)
711 return -1;
712
713 /* readlink() does not add null termination */
714 buf[ret] = '\0';
715
716 /* account for binary path */
717 n = header_argc + 1;
718
719 ret = do_write(fd, &n, sizeof(n));
720 if (ret < 0)
721 return ret;
722
723 ret = do_write_string(fd, buf);
724 if (ret < 0)
725 return ret;
726
727 for (i = 0 ; i < header_argc; i++) {
728 ret = do_write_string(fd, header_argv[i]);
729 if (ret < 0)
730 return ret;
731 }
732 return 0;
733}
734
735#define CORE_SIB_FMT \
736 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
737#define THRD_SIB_FMT \
738 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
739
740struct cpu_topo {
741 u32 core_sib;
742 u32 thread_sib;
743 char **core_siblings;
744 char **thread_siblings;
745};
746
747static int build_cpu_topo(struct cpu_topo *tp, int cpu)
748{
749 FILE *fp;
750 char filename[MAXPATHLEN];
751 char *buf = NULL, *p;
752 size_t len = 0;
753 u32 i = 0;
754 int ret = -1;
755
756 sprintf(filename, CORE_SIB_FMT, cpu);
757 fp = fopen(filename, "r");
758 if (!fp)
759 return -1;
760
761 if (getline(&buf, &len, fp) <= 0)
762 goto done;
763
764 fclose(fp);
765
766 p = strchr(buf, '\n');
767 if (p)
768 *p = '\0';
769
770 for (i = 0; i < tp->core_sib; i++) {
771 if (!strcmp(buf, tp->core_siblings[i]))
772 break;
773 }
774 if (i == tp->core_sib) {
775 tp->core_siblings[i] = buf;
776 tp->core_sib++;
777 buf = NULL;
778 len = 0;
779 }
780
781 sprintf(filename, THRD_SIB_FMT, cpu);
782 fp = fopen(filename, "r");
783 if (!fp)
784 goto done;
785
786 if (getline(&buf, &len, fp) <= 0)
787 goto done;
788
789 p = strchr(buf, '\n');
790 if (p)
791 *p = '\0';
792
793 for (i = 0; i < tp->thread_sib; i++) {
794 if (!strcmp(buf, tp->thread_siblings[i]))
795 break;
796 }
797 if (i == tp->thread_sib) {
798 tp->thread_siblings[i] = buf;
799 tp->thread_sib++;
800 buf = NULL;
801 }
802 ret = 0;
803done:
804 if(fp)
805 fclose(fp);
806 free(buf);
807 return ret;
808}
809
810static void free_cpu_topo(struct cpu_topo *tp)
811{
812 u32 i;
813
814 if (!tp)
815 return;
816
817 for (i = 0 ; i < tp->core_sib; i++)
818 free(tp->core_siblings[i]);
819
820 for (i = 0 ; i < tp->thread_sib; i++)
821 free(tp->thread_siblings[i]);
822
823 free(tp);
824}
825
826static struct cpu_topo *build_cpu_topology(void)
827{
828 struct cpu_topo *tp;
829 void *addr;
830 u32 nr, i;
831 size_t sz;
832 long ncpus;
833 int ret = -1;
834
835 ncpus = sysconf(_SC_NPROCESSORS_CONF);
836 if (ncpus < 0)
837 return NULL;
838
839 nr = (u32)(ncpus & UINT_MAX);
840
841 sz = nr * sizeof(char *);
842
843 addr = calloc(1, sizeof(*tp) + 2 * sz);
844 if (!addr)
845 return NULL;
846
847 tp = addr;
848
849 addr += sizeof(*tp);
850 tp->core_siblings = addr;
851 addr += sz;
852 tp->thread_siblings = addr;
853
854 for (i = 0; i < nr; i++) {
855 ret = build_cpu_topo(tp, i);
856 if (ret < 0)
857 break;
858 }
859 if (ret) {
860 free_cpu_topo(tp);
861 tp = NULL;
862 }
863 return tp;
864}
865
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300866static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
867 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200868{
869 struct cpu_topo *tp;
870 u32 i;
871 int ret;
872
873 tp = build_cpu_topology();
874 if (!tp)
875 return -1;
876
877 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
878 if (ret < 0)
879 goto done;
880
881 for (i = 0; i < tp->core_sib; i++) {
882 ret = do_write_string(fd, tp->core_siblings[i]);
883 if (ret < 0)
884 goto done;
885 }
886 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
887 if (ret < 0)
888 goto done;
889
890 for (i = 0; i < tp->thread_sib; i++) {
891 ret = do_write_string(fd, tp->thread_siblings[i]);
892 if (ret < 0)
893 break;
894 }
895done:
896 free_cpu_topo(tp);
897 return ret;
898}
899
900
901
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300902static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
903 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200904{
905 char *buf = NULL;
906 FILE *fp;
907 size_t len = 0;
908 int ret = -1, n;
909 uint64_t mem;
910
911 fp = fopen("/proc/meminfo", "r");
912 if (!fp)
913 return -1;
914
915 while (getline(&buf, &len, fp) > 0) {
916 ret = strncmp(buf, "MemTotal:", 9);
917 if (!ret)
918 break;
919 }
920 if (!ret) {
921 n = sscanf(buf, "%*s %"PRIu64, &mem);
922 if (n == 1)
923 ret = do_write(fd, &mem, sizeof(mem));
924 }
925 free(buf);
926 fclose(fp);
927 return ret;
928}
929
930static int write_topo_node(int fd, int node)
931{
932 char str[MAXPATHLEN];
933 char field[32];
934 char *buf = NULL, *p;
935 size_t len = 0;
936 FILE *fp;
937 u64 mem_total, mem_free, mem;
938 int ret = -1;
939
940 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
941 fp = fopen(str, "r");
942 if (!fp)
943 return -1;
944
945 while (getline(&buf, &len, fp) > 0) {
946 /* skip over invalid lines */
947 if (!strchr(buf, ':'))
948 continue;
949 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
950 goto done;
951 if (!strcmp(field, "MemTotal:"))
952 mem_total = mem;
953 if (!strcmp(field, "MemFree:"))
954 mem_free = mem;
955 }
956
957 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100958 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200959
960 ret = do_write(fd, &mem_total, sizeof(u64));
961 if (ret)
962 goto done;
963
964 ret = do_write(fd, &mem_free, sizeof(u64));
965 if (ret)
966 goto done;
967
968 ret = -1;
969 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
970
971 fp = fopen(str, "r");
972 if (!fp)
973 goto done;
974
975 if (getline(&buf, &len, fp) <= 0)
976 goto done;
977
978 p = strchr(buf, '\n');
979 if (p)
980 *p = '\0';
981
982 ret = do_write_string(fd, buf);
983done:
984 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100985 if (fp)
986 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200987 return ret;
988}
989
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300990static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
991 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200992{
993 char *buf = NULL;
994 size_t len = 0;
995 FILE *fp;
996 struct cpu_map *node_map = NULL;
997 char *c;
998 u32 nr, i, j;
999 int ret = -1;
1000
1001 fp = fopen("/sys/devices/system/node/online", "r");
1002 if (!fp)
1003 return -1;
1004
1005 if (getline(&buf, &len, fp) <= 0)
1006 goto done;
1007
1008 c = strchr(buf, '\n');
1009 if (c)
1010 *c = '\0';
1011
1012 node_map = cpu_map__new(buf);
1013 if (!node_map)
1014 goto done;
1015
1016 nr = (u32)node_map->nr;
1017
1018 ret = do_write(fd, &nr, sizeof(nr));
1019 if (ret < 0)
1020 goto done;
1021
1022 for (i = 0; i < nr; i++) {
1023 j = (u32)node_map->map[i];
1024 ret = do_write(fd, &j, sizeof(j));
1025 if (ret < 0)
1026 break;
1027
1028 ret = write_topo_node(fd, i);
1029 if (ret < 0)
1030 break;
1031 }
1032done:
1033 free(buf);
1034 fclose(fp);
1035 free(node_map);
1036 return ret;
1037}
1038
1039/*
Robert Richter50a96672012-08-16 21:10:24 +02001040 * File format:
1041 *
1042 * struct pmu_mappings {
1043 * u32 pmu_num;
1044 * struct pmu_map {
1045 * u32 type;
1046 * char name[];
1047 * }[pmu_num];
1048 * };
1049 */
1050
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001051static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
1052 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +02001053{
1054 struct perf_pmu *pmu = NULL;
1055 off_t offset = lseek(fd, 0, SEEK_CUR);
1056 __u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +09001057 int ret;
Robert Richter50a96672012-08-16 21:10:24 +02001058
1059 /* write real pmu_num later */
Namhyung Kim5323f602012-12-17 15:38:54 +09001060 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
1061 if (ret < 0)
1062 return ret;
Robert Richter50a96672012-08-16 21:10:24 +02001063
1064 while ((pmu = perf_pmu__scan(pmu))) {
1065 if (!pmu->name)
1066 continue;
1067 pmu_num++;
Namhyung Kim5323f602012-12-17 15:38:54 +09001068
1069 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
1070 if (ret < 0)
1071 return ret;
1072
1073 ret = do_write_string(fd, pmu->name);
1074 if (ret < 0)
1075 return ret;
Robert Richter50a96672012-08-16 21:10:24 +02001076 }
1077
1078 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1079 /* discard all */
1080 lseek(fd, offset, SEEK_SET);
1081 return -1;
1082 }
1083
1084 return 0;
1085}
1086
1087/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001088 * default get_cpuid(): nothing gets recorded
1089 * actual implementation must be in arch/$(ARCH)/util/header.c
1090 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001091int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
1092 size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001093{
1094 return -1;
1095}
1096
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001097static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
1098 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001099{
1100 char buffer[64];
1101 int ret;
1102
1103 ret = get_cpuid(buffer, sizeof(buffer));
1104 if (!ret)
1105 goto write_it;
1106
1107 return -1;
1108write_it:
1109 return do_write_string(fd, buffer);
1110}
1111
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001112static int write_branch_stack(int fd __maybe_unused,
1113 struct perf_header *h __maybe_unused,
1114 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +01001115{
1116 return 0;
1117}
1118
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001119static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1120 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001121{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001122 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001123}
1124
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001125static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1126 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001127{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001128 fprintf(fp, "# os release : %s\n", ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001129}
1130
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001131static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001132{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001133 fprintf(fp, "# arch : %s\n", ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001134}
1135
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001136static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1137 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001138{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001139 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001140}
1141
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001142static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1143 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001144{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001145 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1146 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001147}
1148
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001149static void print_version(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, "# perf version : %s\n", ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001153}
1154
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001155static void print_cmdline(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 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001159 char *str;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001160
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001161 nr = ph->env.nr_cmdline;
1162 str = ph->env.cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001163
1164 fprintf(fp, "# cmdline : ");
1165
1166 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001167 fprintf(fp, "%s ", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001168 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001169 }
1170 fputc('\n', fp);
1171}
1172
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001173static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1174 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001175{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001176 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001177 char *str;
1178
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001179 nr = ph->env.nr_sibling_cores;
1180 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001181
1182 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001183 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001184 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001185 }
1186
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001187 nr = ph->env.nr_sibling_threads;
1188 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001189
1190 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001191 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001192 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001193 }
1194}
1195
Robert Richter4e1b9c62012-08-16 21:10:22 +02001196static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001197{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001198 struct perf_evsel *evsel;
1199
1200 if (!events)
1201 return;
1202
1203 for (evsel = events; evsel->attr.size; evsel++) {
1204 if (evsel->name)
1205 free(evsel->name);
1206 if (evsel->id)
1207 free(evsel->id);
1208 }
1209
1210 free(events);
1211}
1212
1213static struct perf_evsel *
1214read_event_desc(struct perf_header *ph, int fd)
1215{
1216 struct perf_evsel *evsel, *events = NULL;
1217 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001218 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001219 u32 nre, sz, nr, i, j;
1220 ssize_t ret;
1221 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001222
1223 /* number of events */
Namhyung Kim5323f602012-12-17 15:38:54 +09001224 ret = readn(fd, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001225 if (ret != (ssize_t)sizeof(nre))
1226 goto error;
1227
1228 if (ph->needs_swap)
1229 nre = bswap_32(nre);
1230
Namhyung Kim5323f602012-12-17 15:38:54 +09001231 ret = readn(fd, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001232 if (ret != (ssize_t)sizeof(sz))
1233 goto error;
1234
1235 if (ph->needs_swap)
1236 sz = bswap_32(sz);
1237
Stephane Eranian62db9062012-02-09 23:21:07 +01001238 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001239 buf = malloc(sz);
1240 if (!buf)
1241 goto error;
1242
Robert Richter4e1b9c62012-08-16 21:10:22 +02001243 /* the last event terminates with evsel->attr.size == 0: */
1244 events = calloc(nre + 1, sizeof(*events));
1245 if (!events)
1246 goto error;
1247
1248 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001249 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001250 msz = sz;
1251
Robert Richter4e1b9c62012-08-16 21:10:22 +02001252 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1253 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001254
Stephane Eranian62db9062012-02-09 23:21:07 +01001255 /*
1256 * must read entire on-file attr struct to
1257 * sync up with layout.
1258 */
Namhyung Kim5323f602012-12-17 15:38:54 +09001259 ret = readn(fd, buf, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001260 if (ret != (ssize_t)sz)
1261 goto error;
1262
1263 if (ph->needs_swap)
1264 perf_event__attr_swap(buf);
1265
Robert Richter4e1b9c62012-08-16 21:10:22 +02001266 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001267
Namhyung Kim5323f602012-12-17 15:38:54 +09001268 ret = readn(fd, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001269 if (ret != (ssize_t)sizeof(nr))
1270 goto error;
1271
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001272 if (ph->needs_swap) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001273 nr = bswap_32(nr);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001274 evsel->needs_swap = true;
1275 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001276
Robert Richter4e1b9c62012-08-16 21:10:22 +02001277 evsel->name = do_read_string(fd, ph);
1278
1279 if (!nr)
1280 continue;
1281
1282 id = calloc(nr, sizeof(*id));
1283 if (!id)
1284 goto error;
1285 evsel->ids = nr;
1286 evsel->id = id;
1287
1288 for (j = 0 ; j < nr; j++) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001289 ret = readn(fd, id, sizeof(*id));
Robert Richter4e1b9c62012-08-16 21:10:22 +02001290 if (ret != (ssize_t)sizeof(*id))
1291 goto error;
1292 if (ph->needs_swap)
1293 *id = bswap_64(*id);
1294 id++;
1295 }
1296 }
1297out:
1298 if (buf)
1299 free(buf);
1300 return events;
1301error:
1302 if (events)
1303 free_event_desc(events);
1304 events = NULL;
1305 goto out;
1306}
1307
1308static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1309{
1310 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1311 u32 j;
1312 u64 *id;
1313
1314 if (!events) {
1315 fprintf(fp, "# event desc: not available or unable to read\n");
1316 return;
1317 }
1318
1319 for (evsel = events; evsel->attr.size; evsel++) {
1320 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001321
1322 fprintf(fp, "type = %d, config = 0x%"PRIx64
1323 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
Robert Richter4e1b9c62012-08-16 21:10:22 +02001324 evsel->attr.type,
1325 (u64)evsel->attr.config,
1326 (u64)evsel->attr.config1,
1327 (u64)evsel->attr.config2);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001328
1329 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001330 evsel->attr.exclude_user,
1331 evsel->attr.exclude_kernel);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001332
David Ahern78b961f2012-07-20 17:25:52 -06001333 fprintf(fp, ", excl_host = %d, excl_guest = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001334 evsel->attr.exclude_host,
1335 evsel->attr.exclude_guest);
David Ahern78b961f2012-07-20 17:25:52 -06001336
Robert Richter4e1b9c62012-08-16 21:10:22 +02001337 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
David Ahern78b961f2012-07-20 17:25:52 -06001338
Robert Richter4e1b9c62012-08-16 21:10:22 +02001339 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001340 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001341 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1342 if (j)
1343 fputc(',', fp);
1344 fprintf(fp, " %"PRIu64, *id);
1345 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001346 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001347 }
1348
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001349 fputc('\n', fp);
1350 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001351
1352 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001353}
1354
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001355static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001356 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001357{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001358 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001359}
1360
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001361static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001362 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001363{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001364 u32 nr, c, i;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001365 char *str, *tmp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001366 uint64_t mem_total, mem_free;
1367
1368 /* nr nodes */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001369 nr = ph->env.nr_numa_nodes;
1370 str = ph->env.numa_nodes;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001371
1372 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001373 /* node number */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001374 c = strtoul(str, &tmp, 0);
1375 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001376 goto error;
1377
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001378 str = tmp + 1;
1379 mem_total = strtoull(str, &tmp, 0);
1380 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001381 goto error;
1382
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001383 str = tmp + 1;
1384 mem_free = strtoull(str, &tmp, 0);
1385 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001386 goto error;
1387
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001388 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1389 " free = %"PRIu64" kB\n",
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001390 c, mem_total, mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001391
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001392 str = tmp + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001393 fprintf(fp, "# node%u cpu list : %s\n", c, str);
Namhyung Kim12344712012-10-23 22:44:49 +09001394
1395 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001396 }
1397 return;
1398error:
1399 fprintf(fp, "# numa topology : not available\n");
1400}
1401
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001402static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001403{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001404 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001405}
1406
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001407static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001408 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001409{
1410 fprintf(fp, "# contains samples with branch stack\n");
1411}
1412
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001413static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1414 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001415{
1416 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001417 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001418 u32 pmu_num;
1419 u32 type;
1420
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001421 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001422 if (!pmu_num) {
1423 fprintf(fp, "# pmu mappings: not available\n");
1424 return;
1425 }
1426
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001427 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001428
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001429 while (pmu_num) {
1430 type = strtoul(str, &tmp, 0);
1431 if (*tmp != ':')
1432 goto error;
1433
1434 str = tmp + 1;
1435 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1436
Robert Richter50a96672012-08-16 21:10:24 +02001437 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001438 str += strlen(str) + 1;
1439 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001440 }
1441
1442 fprintf(fp, "\n");
1443
1444 if (!pmu_num)
1445 return;
1446error:
1447 fprintf(fp, "# pmu mappings: unable to read\n");
1448}
1449
Robert Richter08d95bd2012-02-10 15:41:55 +01001450static int __event_process_build_id(struct build_id_event *bev,
1451 char *filename,
1452 struct perf_session *session)
1453{
1454 int err = -1;
1455 struct list_head *head;
1456 struct machine *machine;
1457 u16 misc;
1458 struct dso *dso;
1459 enum dso_kernel_type dso_type;
1460
1461 machine = perf_session__findnew_machine(session, bev->pid);
1462 if (!machine)
1463 goto out;
1464
1465 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1466
1467 switch (misc) {
1468 case PERF_RECORD_MISC_KERNEL:
1469 dso_type = DSO_TYPE_KERNEL;
1470 head = &machine->kernel_dsos;
1471 break;
1472 case PERF_RECORD_MISC_GUEST_KERNEL:
1473 dso_type = DSO_TYPE_GUEST_KERNEL;
1474 head = &machine->kernel_dsos;
1475 break;
1476 case PERF_RECORD_MISC_USER:
1477 case PERF_RECORD_MISC_GUEST_USER:
1478 dso_type = DSO_TYPE_USER;
1479 head = &machine->user_dsos;
1480 break;
1481 default:
1482 goto out;
1483 }
1484
1485 dso = __dsos__findnew(head, filename);
1486 if (dso != NULL) {
1487 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1488
1489 dso__set_build_id(dso, &bev->build_id);
1490
1491 if (filename[0] == '[')
1492 dso->kernel = dso_type;
1493
1494 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1495 sbuild_id);
1496 pr_debug("build id event received for %s: %s\n",
1497 dso->long_name, sbuild_id);
1498 }
1499
1500 err = 0;
1501out:
1502 return err;
1503}
1504
1505static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1506 int input, u64 offset, u64 size)
1507{
1508 struct perf_session *session = container_of(header, struct perf_session, header);
1509 struct {
1510 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001511 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001512 char filename[0];
1513 } old_bev;
1514 struct build_id_event bev;
1515 char filename[PATH_MAX];
1516 u64 limit = offset + size;
1517
1518 while (offset < limit) {
1519 ssize_t len;
1520
Namhyung Kim5323f602012-12-17 15:38:54 +09001521 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001522 return -1;
1523
1524 if (header->needs_swap)
1525 perf_event_header__bswap(&old_bev.header);
1526
1527 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001528 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001529 return -1;
1530
1531 bev.header = old_bev.header;
1532
1533 /*
1534 * As the pid is the missing value, we need to fill
1535 * it properly. The header.misc value give us nice hint.
1536 */
1537 bev.pid = HOST_KERNEL_ID;
1538 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1539 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1540 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1541
1542 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1543 __event_process_build_id(&bev, filename, session);
1544
1545 offset += bev.header.size;
1546 }
1547
1548 return 0;
1549}
1550
1551static int perf_header__read_build_ids(struct perf_header *header,
1552 int input, u64 offset, u64 size)
1553{
1554 struct perf_session *session = container_of(header, struct perf_session, header);
1555 struct build_id_event bev;
1556 char filename[PATH_MAX];
1557 u64 limit = offset + size, orig_offset = offset;
1558 int err = -1;
1559
1560 while (offset < limit) {
1561 ssize_t len;
1562
Namhyung Kim5323f602012-12-17 15:38:54 +09001563 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001564 goto out;
1565
1566 if (header->needs_swap)
1567 perf_event_header__bswap(&bev.header);
1568
1569 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001570 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001571 goto out;
1572 /*
1573 * The a1645ce1 changeset:
1574 *
1575 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1576 *
1577 * Added a field to struct build_id_event that broke the file
1578 * format.
1579 *
1580 * Since the kernel build-id is the first entry, process the
1581 * table using the old format if the well known
1582 * '[kernel.kallsyms]' string for the kernel build-id has the
1583 * first 4 characters chopped off (where the pid_t sits).
1584 */
1585 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1586 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1587 return -1;
1588 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1589 }
1590
1591 __event_process_build_id(&bev, filename, session);
1592
1593 offset += bev.header.size;
1594 }
1595 err = 0;
1596out:
1597 return err;
1598}
1599
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001600static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1601 struct perf_header *ph __maybe_unused,
1602 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001603{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001604 trace_report(fd, data, false);
Robert Richterf1c67db2012-02-10 15:41:56 +01001605 return 0;
1606}
1607
1608static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001609 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001610 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001611{
1612 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1613 pr_debug("Failed to read buildids, continuing...\n");
1614 return 0;
1615}
1616
Namhyung Kima1ae5652012-09-24 17:14:59 +09001617static int process_hostname(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001618 struct perf_header *ph, int fd,
1619 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001620{
1621 ph->env.hostname = do_read_string(fd, ph);
1622 return ph->env.hostname ? 0 : -ENOMEM;
1623}
1624
1625static int process_osrelease(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001626 struct perf_header *ph, int fd,
1627 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001628{
1629 ph->env.os_release = do_read_string(fd, ph);
1630 return ph->env.os_release ? 0 : -ENOMEM;
1631}
1632
1633static int process_version(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001634 struct perf_header *ph, int fd,
1635 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001636{
1637 ph->env.version = do_read_string(fd, ph);
1638 return ph->env.version ? 0 : -ENOMEM;
1639}
1640
1641static int process_arch(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001642 struct perf_header *ph, int fd,
1643 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001644{
1645 ph->env.arch = do_read_string(fd, ph);
1646 return ph->env.arch ? 0 : -ENOMEM;
1647}
1648
1649static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001650 struct perf_header *ph, int fd,
1651 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001652{
1653 size_t ret;
1654 u32 nr;
1655
Namhyung Kim5323f602012-12-17 15:38:54 +09001656 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001657 if (ret != sizeof(nr))
1658 return -1;
1659
1660 if (ph->needs_swap)
1661 nr = bswap_32(nr);
1662
1663 ph->env.nr_cpus_online = nr;
1664
Namhyung Kim5323f602012-12-17 15:38:54 +09001665 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001666 if (ret != sizeof(nr))
1667 return -1;
1668
1669 if (ph->needs_swap)
1670 nr = bswap_32(nr);
1671
1672 ph->env.nr_cpus_avail = nr;
1673 return 0;
1674}
1675
1676static int process_cpudesc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001677 struct perf_header *ph, int fd,
1678 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001679{
1680 ph->env.cpu_desc = do_read_string(fd, ph);
1681 return ph->env.cpu_desc ? 0 : -ENOMEM;
1682}
1683
1684static int process_cpuid(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001685 struct perf_header *ph, int fd,
1686 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001687{
1688 ph->env.cpuid = do_read_string(fd, ph);
1689 return ph->env.cpuid ? 0 : -ENOMEM;
1690}
1691
1692static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001693 struct perf_header *ph, int fd,
1694 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001695{
1696 uint64_t mem;
1697 size_t ret;
1698
Namhyung Kim5323f602012-12-17 15:38:54 +09001699 ret = readn(fd, &mem, sizeof(mem));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001700 if (ret != sizeof(mem))
1701 return -1;
1702
1703 if (ph->needs_swap)
1704 mem = bswap_64(mem);
1705
1706 ph->env.total_mem = mem;
1707 return 0;
1708}
1709
Robert Richter7c2f7af2012-08-16 21:10:23 +02001710static struct perf_evsel *
1711perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1712{
1713 struct perf_evsel *evsel;
1714
1715 list_for_each_entry(evsel, &evlist->entries, node) {
1716 if (evsel->idx == idx)
1717 return evsel;
1718 }
1719
1720 return NULL;
1721}
1722
1723static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001724perf_evlist__set_event_name(struct perf_evlist *evlist,
1725 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001726{
1727 struct perf_evsel *evsel;
1728
1729 if (!event->name)
1730 return;
1731
1732 evsel = perf_evlist__find_by_index(evlist, event->idx);
1733 if (!evsel)
1734 return;
1735
1736 if (evsel->name)
1737 return;
1738
1739 evsel->name = strdup(event->name);
1740}
1741
1742static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001743process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001744 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001745 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001746{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001747 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001748 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1749
1750 if (!events)
1751 return 0;
1752
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001753 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001754 for (evsel = events; evsel->attr.size; evsel++)
1755 perf_evlist__set_event_name(session->evlist, evsel);
1756
1757 free_event_desc(events);
1758
1759 return 0;
1760}
1761
Namhyung Kima1ae5652012-09-24 17:14:59 +09001762static int process_cmdline(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001763 struct perf_header *ph, int fd,
1764 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001765{
1766 size_t ret;
1767 char *str;
1768 u32 nr, i;
1769 struct strbuf sb;
1770
Namhyung Kim5323f602012-12-17 15:38:54 +09001771 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001772 if (ret != sizeof(nr))
1773 return -1;
1774
1775 if (ph->needs_swap)
1776 nr = bswap_32(nr);
1777
1778 ph->env.nr_cmdline = nr;
1779 strbuf_init(&sb, 128);
1780
1781 for (i = 0; i < nr; i++) {
1782 str = do_read_string(fd, ph);
1783 if (!str)
1784 goto error;
1785
1786 /* include a NULL character at the end */
1787 strbuf_add(&sb, str, strlen(str) + 1);
1788 free(str);
1789 }
1790 ph->env.cmdline = strbuf_detach(&sb, NULL);
1791 return 0;
1792
1793error:
1794 strbuf_release(&sb);
1795 return -1;
1796}
1797
1798static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001799 struct perf_header *ph, int fd,
1800 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001801{
1802 size_t ret;
1803 u32 nr, i;
1804 char *str;
1805 struct strbuf sb;
1806
Namhyung Kim5323f602012-12-17 15:38:54 +09001807 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001808 if (ret != sizeof(nr))
1809 return -1;
1810
1811 if (ph->needs_swap)
1812 nr = bswap_32(nr);
1813
1814 ph->env.nr_sibling_cores = nr;
1815 strbuf_init(&sb, 128);
1816
1817 for (i = 0; i < nr; i++) {
1818 str = do_read_string(fd, ph);
1819 if (!str)
1820 goto error;
1821
1822 /* include a NULL character at the end */
1823 strbuf_add(&sb, str, strlen(str) + 1);
1824 free(str);
1825 }
1826 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1827
Namhyung Kim5323f602012-12-17 15:38:54 +09001828 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001829 if (ret != sizeof(nr))
1830 return -1;
1831
1832 if (ph->needs_swap)
1833 nr = bswap_32(nr);
1834
1835 ph->env.nr_sibling_threads = nr;
1836
1837 for (i = 0; i < nr; i++) {
1838 str = do_read_string(fd, ph);
1839 if (!str)
1840 goto error;
1841
1842 /* include a NULL character at the end */
1843 strbuf_add(&sb, str, strlen(str) + 1);
1844 free(str);
1845 }
1846 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1847 return 0;
1848
1849error:
1850 strbuf_release(&sb);
1851 return -1;
1852}
1853
1854static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001855 struct perf_header *ph, int fd,
1856 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001857{
1858 size_t ret;
1859 u32 nr, node, i;
1860 char *str;
1861 uint64_t mem_total, mem_free;
1862 struct strbuf sb;
1863
1864 /* nr nodes */
Namhyung Kim5323f602012-12-17 15:38:54 +09001865 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001866 if (ret != sizeof(nr))
1867 goto error;
1868
1869 if (ph->needs_swap)
1870 nr = bswap_32(nr);
1871
1872 ph->env.nr_numa_nodes = nr;
1873 strbuf_init(&sb, 256);
1874
1875 for (i = 0; i < nr; i++) {
1876 /* node number */
Namhyung Kim5323f602012-12-17 15:38:54 +09001877 ret = readn(fd, &node, sizeof(node));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001878 if (ret != sizeof(node))
1879 goto error;
1880
Namhyung Kim5323f602012-12-17 15:38:54 +09001881 ret = readn(fd, &mem_total, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001882 if (ret != sizeof(u64))
1883 goto error;
1884
Namhyung Kim5323f602012-12-17 15:38:54 +09001885 ret = readn(fd, &mem_free, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001886 if (ret != sizeof(u64))
1887 goto error;
1888
1889 if (ph->needs_swap) {
1890 node = bswap_32(node);
1891 mem_total = bswap_64(mem_total);
1892 mem_free = bswap_64(mem_free);
1893 }
1894
1895 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1896 node, mem_total, mem_free);
1897
1898 str = do_read_string(fd, ph);
1899 if (!str)
1900 goto error;
1901
1902 /* include a NULL character at the end */
1903 strbuf_add(&sb, str, strlen(str) + 1);
1904 free(str);
1905 }
1906 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1907 return 0;
1908
1909error:
1910 strbuf_release(&sb);
1911 return -1;
1912}
1913
1914static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001915 struct perf_header *ph, int fd,
1916 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001917{
1918 size_t ret;
1919 char *name;
1920 u32 pmu_num;
1921 u32 type;
1922 struct strbuf sb;
1923
Namhyung Kim5323f602012-12-17 15:38:54 +09001924 ret = readn(fd, &pmu_num, sizeof(pmu_num));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001925 if (ret != sizeof(pmu_num))
1926 return -1;
1927
1928 if (ph->needs_swap)
1929 pmu_num = bswap_32(pmu_num);
1930
1931 if (!pmu_num) {
1932 pr_debug("pmu mappings not available\n");
1933 return 0;
1934 }
1935
1936 ph->env.nr_pmu_mappings = pmu_num;
1937 strbuf_init(&sb, 128);
1938
1939 while (pmu_num) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001940 if (readn(fd, &type, sizeof(type)) != sizeof(type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001941 goto error;
1942 if (ph->needs_swap)
1943 type = bswap_32(type);
1944
1945 name = do_read_string(fd, ph);
1946 if (!name)
1947 goto error;
1948
1949 strbuf_addf(&sb, "%u:%s", type, name);
1950 /* include a NULL character at the end */
1951 strbuf_add(&sb, "", 1);
1952
1953 free(name);
1954 pmu_num--;
1955 }
1956 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1957 return 0;
1958
1959error:
1960 strbuf_release(&sb);
1961 return -1;
1962}
1963
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001964struct feature_ops {
1965 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1966 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01001967 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001968 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001969 const char *name;
1970 bool full_only;
1971};
1972
Robert Richter8cdfa782011-12-07 10:02:56 +01001973#define FEAT_OPA(n, func) \
1974 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01001975#define FEAT_OPP(n, func) \
1976 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1977 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01001978#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01001979 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09001980 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01001981
1982/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001983#define print_tracing_data NULL
1984#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001985
1986static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001987 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01001988 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001989 FEAT_OPP(HEADER_HOSTNAME, hostname),
1990 FEAT_OPP(HEADER_OSRELEASE, osrelease),
1991 FEAT_OPP(HEADER_VERSION, version),
1992 FEAT_OPP(HEADER_ARCH, arch),
1993 FEAT_OPP(HEADER_NRCPUS, nrcpus),
1994 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09001995 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001996 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02001997 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001998 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01001999 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2000 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01002001 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002002 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002003};
2004
2005struct header_print_data {
2006 FILE *fp;
2007 bool full; /* extended list of headers */
2008};
2009
2010static int perf_file_section__fprintf_info(struct perf_file_section *section,
2011 struct perf_header *ph,
2012 int feat, int fd, void *data)
2013{
2014 struct header_print_data *hd = data;
2015
2016 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2017 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2018 "%d, continuing...\n", section->offset, feat);
2019 return 0;
2020 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002021 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002022 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002023 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002024 }
2025 if (!feat_ops[feat].print)
2026 return 0;
2027
2028 if (!feat_ops[feat].full_only || hd->full)
2029 feat_ops[feat].print(ph, fd, hd->fp);
2030 else
2031 fprintf(hd->fp, "# %s info available, use -I to display\n",
2032 feat_ops[feat].name);
2033
2034 return 0;
2035}
2036
2037int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2038{
2039 struct header_print_data hd;
2040 struct perf_header *header = &session->header;
2041 int fd = session->fd;
2042 hd.fp = fp;
2043 hd.full = full;
2044
2045 perf_header__process_sections(header, fd, &hd,
2046 perf_file_section__fprintf_info);
2047 return 0;
2048}
2049
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002050static int do_write_feat(int fd, struct perf_header *h, int type,
2051 struct perf_file_section **p,
2052 struct perf_evlist *evlist)
2053{
2054 int err;
2055 int ret = 0;
2056
2057 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002058 if (!feat_ops[type].write)
2059 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002060
2061 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2062
2063 err = feat_ops[type].write(fd, h, evlist);
2064 if (err < 0) {
2065 pr_debug("failed to write feature %d\n", type);
2066
2067 /* undo anything written */
2068 lseek(fd, (*p)->offset, SEEK_SET);
2069
2070 return -1;
2071 }
2072 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2073 (*p)++;
2074 }
2075 return ret;
2076}
2077
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002078static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002079 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002080{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002081 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002082 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002083 int sec_size;
2084 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002085 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002086 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002087
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002088 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002089 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002090 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002091
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002092 feat_sec = p = calloc(sizeof(*feat_sec), nr_sections);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002093 if (feat_sec == NULL)
2094 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002095
2096 sec_size = sizeof(*feat_sec) * nr_sections;
2097
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002098 sec_start = header->data_offset + header->data_size;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002099 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002100
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002101 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2102 if (do_write_feat(fd, header, feat, &p, evlist))
2103 perf_header__clear_feat(header, feat);
2104 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002105
Xiao Guangrongf887f302010-02-04 16:46:42 +08002106 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002107 /*
2108 * may write more than needed due to dropped feature, but
2109 * this is okay, reader will skip the mising entries
2110 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002111 err = do_write(fd, feat_sec, sec_size);
2112 if (err < 0)
2113 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002114 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002115 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002116}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002117
Tom Zanussi8dc58102010-04-01 23:59:15 -05002118int perf_header__write_pipe(int fd)
2119{
2120 struct perf_pipe_file_header f_header;
2121 int err;
2122
2123 f_header = (struct perf_pipe_file_header){
2124 .magic = PERF_MAGIC,
2125 .size = sizeof(f_header),
2126 };
2127
2128 err = do_write(fd, &f_header, sizeof(f_header));
2129 if (err < 0) {
2130 pr_debug("failed to write perf pipe header\n");
2131 return err;
2132 }
2133
2134 return 0;
2135}
2136
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002137int perf_session__write_header(struct perf_session *session,
2138 struct perf_evlist *evlist,
2139 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002140{
2141 struct perf_file_header f_header;
2142 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002143 struct perf_header *header = &session->header;
Robert Richter6606f872012-08-16 21:10:19 +02002144 struct perf_evsel *evsel, *pair = NULL;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002145 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002146
2147 lseek(fd, sizeof(f_header), SEEK_SET);
2148
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002149 if (session->evlist != evlist)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03002150 pair = perf_evlist__first(session->evlist);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002151
Robert Richter6606f872012-08-16 21:10:19 +02002152 list_for_each_entry(evsel, &evlist->entries, node) {
2153 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2154 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002155 if (err < 0) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002156out_err_write:
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002157 pr_debug("failed to write perf header\n");
2158 return err;
2159 }
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002160 if (session->evlist != evlist) {
2161 err = do_write(fd, pair->id, pair->ids * sizeof(u64));
2162 if (err < 0)
2163 goto out_err_write;
Robert Richter6606f872012-08-16 21:10:19 +02002164 evsel->ids += pair->ids;
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03002165 pair = perf_evsel__next(pair);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002166 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002167 }
2168
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002169 header->attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002170
Robert Richter6606f872012-08-16 21:10:19 +02002171 list_for_each_entry(evsel, &evlist->entries, node) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002172 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002173 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002174 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002175 .offset = evsel->id_offset,
2176 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002177 }
2178 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002179 err = do_write(fd, &f_attr, sizeof(f_attr));
2180 if (err < 0) {
2181 pr_debug("failed to write perf header attribute\n");
2182 return err;
2183 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002184 }
2185
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002186 header->event_offset = lseek(fd, 0, SEEK_CUR);
Robert Richterdb146f02012-08-16 21:10:20 +02002187 header->event_size = trace_event_count * sizeof(struct perf_trace_event_type);
2188 if (trace_events) {
2189 err = do_write(fd, trace_events, header->event_size);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002190 if (err < 0) {
2191 pr_debug("failed to write perf header events\n");
2192 return err;
2193 }
2194 }
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02002195
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002196 header->data_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002197
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002198 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002199 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002200 if (err < 0)
2201 return err;
2202 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002203
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002204 f_header = (struct perf_file_header){
2205 .magic = PERF_MAGIC,
2206 .size = sizeof(f_header),
2207 .attr_size = sizeof(f_attr),
2208 .attrs = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002209 .offset = header->attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002210 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002211 },
2212 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002213 .offset = header->data_offset,
2214 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002215 },
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02002216 .event_types = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002217 .offset = header->event_offset,
2218 .size = header->event_size,
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02002219 },
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002220 };
2221
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002222 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002223
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002224 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002225 err = do_write(fd, &f_header, sizeof(f_header));
2226 if (err < 0) {
2227 pr_debug("failed to write perf header\n");
2228 return err;
2229 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002230 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002231
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002232 header->frozen = 1;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002233 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002234}
2235
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002236static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002237 int fd, void *buf, size_t size)
2238{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002239 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002240 return -1;
2241
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002242 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002243 mem_bswap_64(buf, size);
2244
2245 return 0;
2246}
2247
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002248int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002249 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002250 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002251 struct perf_header *ph,
2252 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002253{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002254 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002255 int nr_sections;
2256 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002257 int feat;
2258 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002259
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002260 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002261 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002262 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002263
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002264 feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002265 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002266 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002267
2268 sec_size = sizeof(*feat_sec) * nr_sections;
2269
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002270 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002271
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002272 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2273 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002274 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002275
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002276 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2277 err = process(sec++, header, feat, fd, data);
2278 if (err < 0)
2279 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002280 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002281 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002282out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002283 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002284 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002285}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002286
Stephane Eranian114382a2012-02-09 23:21:08 +01002287static const int attr_file_abi_sizes[] = {
2288 [0] = PERF_ATTR_SIZE_VER0,
2289 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002290 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002291 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian114382a2012-02-09 23:21:08 +01002292 0,
2293};
2294
2295/*
2296 * In the legacy file format, the magic number is not used to encode endianness.
2297 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2298 * on ABI revisions, we need to try all combinations for all endianness to
2299 * detect the endianness.
2300 */
2301static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2302{
2303 uint64_t ref_size, attr_size;
2304 int i;
2305
2306 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2307 ref_size = attr_file_abi_sizes[i]
2308 + sizeof(struct perf_file_section);
2309 if (hdr_sz != ref_size) {
2310 attr_size = bswap_64(hdr_sz);
2311 if (attr_size != ref_size)
2312 continue;
2313
2314 ph->needs_swap = true;
2315 }
2316 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2317 i,
2318 ph->needs_swap);
2319 return 0;
2320 }
2321 /* could not determine endianness */
2322 return -1;
2323}
2324
2325#define PERF_PIPE_HDR_VER0 16
2326
2327static const size_t attr_pipe_abi_sizes[] = {
2328 [0] = PERF_PIPE_HDR_VER0,
2329 0,
2330};
2331
2332/*
2333 * In the legacy pipe format, there is an implicit assumption that endiannesss
2334 * between host recording the samples, and host parsing the samples is the
2335 * same. This is not always the case given that the pipe output may always be
2336 * redirected into a file and analyzed on a different machine with possibly a
2337 * different endianness and perf_event ABI revsions in the perf tool itself.
2338 */
2339static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2340{
2341 u64 attr_size;
2342 int i;
2343
2344 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2345 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2346 attr_size = bswap_64(hdr_sz);
2347 if (attr_size != hdr_sz)
2348 continue;
2349
2350 ph->needs_swap = true;
2351 }
2352 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2353 return 0;
2354 }
2355 return -1;
2356}
2357
Feng Tange84ba4e2012-10-30 11:56:07 +08002358bool is_perf_magic(u64 magic)
2359{
2360 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2361 || magic == __perf_magic2
2362 || magic == __perf_magic2_sw)
2363 return true;
2364
2365 return false;
2366}
2367
Stephane Eranian114382a2012-02-09 23:21:08 +01002368static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2369 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002370{
2371 int ret;
2372
2373 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002374 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002375 if (ret == 0) {
2376 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002377 if (is_pipe)
2378 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002379
Stephane Eranian114382a2012-02-09 23:21:08 +01002380 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002381 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002382 /*
2383 * the new magic number serves two purposes:
2384 * - unique number to identify actual perf.data files
2385 * - encode endianness of file
2386 */
Stephane Eranian73323f52012-02-02 13:54:44 +01002387
Stephane Eranian114382a2012-02-09 23:21:08 +01002388 /* check magic number with one endianness */
2389 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002390 return 0;
2391
Stephane Eranian114382a2012-02-09 23:21:08 +01002392 /* check magic number with opposite endianness */
2393 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002394 return -1;
2395
2396 ph->needs_swap = true;
2397
2398 return 0;
2399}
2400
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002401int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002402 struct perf_header *ph, int fd)
2403{
Stephane Eranian73323f52012-02-02 13:54:44 +01002404 int ret;
2405
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002406 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002407
Stephane Eranian73323f52012-02-02 13:54:44 +01002408 ret = readn(fd, header, sizeof(*header));
2409 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002410 return -1;
2411
Stephane Eranian114382a2012-02-09 23:21:08 +01002412 if (check_magic_endian(header->magic,
2413 header->attr_size, false, ph) < 0) {
2414 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002415 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002416 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002417
Stephane Eranian73323f52012-02-02 13:54:44 +01002418 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002419 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002420 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002421 }
2422
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002423 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002424 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002425 if (header->size == offsetof(typeof(*header), adds_features))
2426 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002427 else
2428 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002429 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002430 /*
2431 * feature bitmap is declared as an array of unsigned longs --
2432 * not good since its size can differ between the host that
2433 * generated the data file and the host analyzing the file.
2434 *
2435 * We need to handle endianness, but we don't know the size of
2436 * the unsigned long where the file was generated. Take a best
2437 * guess at determining it: try 64-bit swap first (ie., file
2438 * created on a 64-bit host), and check if the hostname feature
2439 * bit is set (this feature bit is forced on as of fbe96f2).
2440 * If the bit is not, undo the 64-bit swap and try a 32-bit
2441 * swap. If the hostname bit is still not set (e.g., older data
2442 * file), punt and fallback to the original behavior --
2443 * clearing all feature bits and setting buildid.
2444 */
David Ahern80c01202012-06-08 11:47:51 -03002445 mem_bswap_64(&header->adds_features,
2446 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002447
2448 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002449 /* unswap as u64 */
2450 mem_bswap_64(&header->adds_features,
2451 BITS_TO_U64(HEADER_FEAT_BITS));
2452
2453 /* unswap as u32 */
2454 mem_bswap_32(&header->adds_features,
2455 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002456 }
2457
2458 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2459 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2460 set_bit(HEADER_BUILD_ID, header->adds_features);
2461 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002462 }
2463
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002464 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002465 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002466
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002467 ph->event_offset = header->event_types.offset;
2468 ph->event_size = header->event_types.size;
2469 ph->data_offset = header->data.offset;
2470 ph->data_size = header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002471 return 0;
2472}
2473
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002474static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002475 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002476 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002477{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002478 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002479 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002480 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002481 return 0;
2482 }
2483
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002484 if (feat >= HEADER_LAST_FEATURE) {
2485 pr_debug("unknown feature %d, continuing...\n", feat);
2486 return 0;
2487 }
2488
Robert Richterf1c67db2012-02-10 15:41:56 +01002489 if (!feat_ops[feat].process)
2490 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002491
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002492 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002493}
2494
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002495static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002496 struct perf_header *ph, int fd,
2497 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002498{
Stephane Eranian73323f52012-02-02 13:54:44 +01002499 int ret;
2500
2501 ret = readn(fd, header, sizeof(*header));
2502 if (ret <= 0)
2503 return -1;
2504
Stephane Eranian114382a2012-02-09 23:21:08 +01002505 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2506 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002507 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002508 }
2509
2510 if (ph->needs_swap)
2511 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002512
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002513 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002514 return -1;
2515
Tom Zanussi8dc58102010-04-01 23:59:15 -05002516 return 0;
2517}
2518
2519static int perf_header__read_pipe(struct perf_session *session, int fd)
2520{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002521 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002522 struct perf_pipe_file_header f_header;
2523
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002524 if (perf_file_header__read_pipe(&f_header, header, fd,
Tom Zanussi454c4072010-05-01 01:41:20 -05002525 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002526 pr_debug("incompatible file format\n");
2527 return -EINVAL;
2528 }
2529
2530 session->fd = fd;
2531
2532 return 0;
2533}
2534
Stephane Eranian69996df2012-02-09 23:21:06 +01002535static int read_attr(int fd, struct perf_header *ph,
2536 struct perf_file_attr *f_attr)
2537{
2538 struct perf_event_attr *attr = &f_attr->attr;
2539 size_t sz, left;
2540 size_t our_sz = sizeof(f_attr->attr);
2541 int ret;
2542
2543 memset(f_attr, 0, sizeof(*f_attr));
2544
2545 /* read minimal guaranteed structure */
2546 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2547 if (ret <= 0) {
2548 pr_debug("cannot read %d bytes of header attr\n",
2549 PERF_ATTR_SIZE_VER0);
2550 return -1;
2551 }
2552
2553 /* on file perf_event_attr size */
2554 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002555
Stephane Eranian69996df2012-02-09 23:21:06 +01002556 if (ph->needs_swap)
2557 sz = bswap_32(sz);
2558
2559 if (sz == 0) {
2560 /* assume ABI0 */
2561 sz = PERF_ATTR_SIZE_VER0;
2562 } else if (sz > our_sz) {
2563 pr_debug("file uses a more recent and unsupported ABI"
2564 " (%zu bytes extra)\n", sz - our_sz);
2565 return -1;
2566 }
2567 /* what we have not yet read and that we know about */
2568 left = sz - PERF_ATTR_SIZE_VER0;
2569 if (left) {
2570 void *ptr = attr;
2571 ptr += PERF_ATTR_SIZE_VER0;
2572
2573 ret = readn(fd, ptr, left);
2574 }
2575 /* read perf_file_section, ids are read in caller */
2576 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2577
2578 return ret <= 0 ? -1 : 0;
2579}
2580
Namhyung Kim831394b2012-09-06 11:10:46 +09002581static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2582 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002583{
Namhyung Kim831394b2012-09-06 11:10:46 +09002584 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002585 char bf[128];
2586
Namhyung Kim831394b2012-09-06 11:10:46 +09002587 /* already prepared */
2588 if (evsel->tp_format)
2589 return 0;
2590
2591 event = pevent_find_event(pevent, evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002592 if (event == NULL)
2593 return -1;
2594
Namhyung Kim831394b2012-09-06 11:10:46 +09002595 if (!evsel->name) {
2596 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2597 evsel->name = strdup(bf);
2598 if (evsel->name == NULL)
2599 return -1;
2600 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002601
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002602 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002603 return 0;
2604}
2605
Namhyung Kim831394b2012-09-06 11:10:46 +09002606static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2607 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002608{
2609 struct perf_evsel *pos;
2610
2611 list_for_each_entry(pos, &evlist->entries, node) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002612 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2613 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002614 return -1;
2615 }
2616
2617 return 0;
2618}
2619
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002620int perf_session__read_header(struct perf_session *session, int fd)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002621{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002622 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002623 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002624 struct perf_file_attr f_attr;
2625 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002626 int nr_attrs, nr_ids, i, j;
2627
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002628 session->evlist = perf_evlist__new(NULL, NULL);
2629 if (session->evlist == NULL)
2630 return -ENOMEM;
2631
Tom Zanussi8dc58102010-04-01 23:59:15 -05002632 if (session->fd_pipe)
2633 return perf_header__read_pipe(session, fd);
2634
Stephane Eranian69996df2012-02-09 23:21:06 +01002635 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002636 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002637
Stephane Eranian69996df2012-02-09 23:21:06 +01002638 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002639 lseek(fd, f_header.attrs.offset, SEEK_SET);
2640
2641 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002642 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002643 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002644
Stephane Eranian69996df2012-02-09 23:21:06 +01002645 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002646 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002647
David Aherneda39132011-07-15 12:34:09 -06002648 if (header->needs_swap)
2649 perf_event__attr_swap(&f_attr.attr);
2650
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002651 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002652 evsel = perf_evsel__new(&f_attr.attr, i);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002653
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002654 if (evsel == NULL)
2655 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002656
2657 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002658 /*
2659 * Do it before so that if perf_evsel__alloc_id fails, this
2660 * entry gets purged too at perf_evlist__delete().
2661 */
2662 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002663
2664 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002665 /*
2666 * We don't have the cpu and thread maps on the header, so
2667 * for allocating the perf_sample_id table we fake 1 cpu and
2668 * hattr->ids threads.
2669 */
2670 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2671 goto out_delete_evlist;
2672
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002673 lseek(fd, f_attr.ids.offset, SEEK_SET);
2674
2675 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002676 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002677 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002678
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002679 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002680 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002681
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002682 lseek(fd, tmp, SEEK_SET);
2683 }
2684
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002685 symbol_conf.nr_events = nr_attrs;
2686
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02002687 if (f_header.event_types.size) {
2688 lseek(fd, f_header.event_types.offset, SEEK_SET);
Robert Richterdb146f02012-08-16 21:10:20 +02002689 trace_events = malloc(f_header.event_types.size);
2690 if (trace_events == NULL)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002691 return -ENOMEM;
Robert Richterdb146f02012-08-16 21:10:20 +02002692 if (perf_header__getbuffer64(header, fd, trace_events,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002693 f_header.event_types.size))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002694 goto out_errno;
Robert Richterdb146f02012-08-16 21:10:20 +02002695 trace_event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02002696 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +02002697
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002698 perf_header__process_sections(header, fd, &session->pevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002699 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002700
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002701 lseek(fd, header->data_offset, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002702
Namhyung Kim831394b2012-09-06 11:10:46 +09002703 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2704 session->pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002705 goto out_delete_evlist;
2706
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002707 header->frozen = 1;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002708 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002709out_errno:
2710 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002711
2712out_delete_evlist:
2713 perf_evlist__delete(session->evlist);
2714 session->evlist = NULL;
2715 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002716}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002717
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002718int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002719 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002720 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002721{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002722 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002723 size_t size;
2724 int err;
2725
2726 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002727 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002728 size += sizeof(struct perf_event_header);
2729 size += ids * sizeof(u64);
2730
2731 ev = malloc(size);
2732
Chris Samuelce47dc52010-11-13 13:35:06 +11002733 if (ev == NULL)
2734 return -ENOMEM;
2735
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002736 ev->attr.attr = *attr;
2737 memcpy(ev->attr.id, id, ids * sizeof(u64));
2738
2739 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002740 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002741
Robert Richterf4d83432012-08-16 21:10:17 +02002742 if (ev->attr.header.size == size)
2743 err = process(tool, ev, NULL, NULL);
2744 else
2745 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002746
2747 free(ev);
2748
2749 return err;
2750}
2751
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002752int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002753 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002754 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002755{
Robert Richter6606f872012-08-16 21:10:19 +02002756 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002757 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002758
Robert Richter6606f872012-08-16 21:10:19 +02002759 list_for_each_entry(evsel, &session->evlist->entries, node) {
2760 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2761 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002762 if (err) {
2763 pr_debug("failed to create perf header attribute\n");
2764 return err;
2765 }
2766 }
2767
2768 return err;
2769}
2770
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002771int perf_event__process_attr(union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002772 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002773{
Robert Richterf4d83432012-08-16 21:10:17 +02002774 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002775 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002776 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002777
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002778 if (evlist == NULL) {
2779 *pevlist = evlist = perf_evlist__new(NULL, NULL);
2780 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002781 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002782 }
2783
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002784 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002785 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002786 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002787
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002788 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002789
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002790 ids = event->header.size;
2791 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002792 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002793 /*
2794 * We don't have the cpu and thread maps on the header, so
2795 * for allocating the perf_sample_id table we fake 1 cpu and
2796 * hattr->ids threads.
2797 */
2798 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2799 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002800
2801 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002802 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002803 }
2804
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002805 return 0;
2806}
Tom Zanussicd19a032010-04-01 23:59:20 -05002807
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002808int perf_event__synthesize_event_type(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002809 u64 event_id, char *name,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002810 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002811 struct machine *machine)
Tom Zanussicd19a032010-04-01 23:59:20 -05002812{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002813 union perf_event ev;
Tom Zanussicd19a032010-04-01 23:59:20 -05002814 size_t size = 0;
2815 int err = 0;
2816
2817 memset(&ev, 0, sizeof(ev));
2818
2819 ev.event_type.event_type.event_id = event_id;
2820 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
2821 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
2822
2823 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
Stephane Eranianf8f4b282012-01-20 10:49:12 +01002824 size = strlen(ev.event_type.event_type.name);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002825 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussicd19a032010-04-01 23:59:20 -05002826 ev.event_type.header.size = sizeof(ev.event_type) -
2827 (sizeof(ev.event_type.event_type.name) - size);
2828
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002829 err = process(tool, &ev, NULL, machine);
Tom Zanussicd19a032010-04-01 23:59:20 -05002830
2831 return err;
2832}
2833
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002834int perf_event__synthesize_event_types(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002835 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002836 struct machine *machine)
Tom Zanussicd19a032010-04-01 23:59:20 -05002837{
2838 struct perf_trace_event_type *type;
2839 int i, err = 0;
2840
Robert Richterdb146f02012-08-16 21:10:20 +02002841 for (i = 0; i < trace_event_count; i++) {
2842 type = &trace_events[i];
Tom Zanussicd19a032010-04-01 23:59:20 -05002843
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002844 err = perf_event__synthesize_event_type(tool, type->event_id,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002845 type->name, process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002846 machine);
Tom Zanussicd19a032010-04-01 23:59:20 -05002847 if (err) {
2848 pr_debug("failed to create perf header event type\n");
2849 return err;
2850 }
2851 }
2852
2853 return err;
2854}
2855
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002856int perf_event__process_event_type(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002857 union perf_event *event)
Tom Zanussicd19a032010-04-01 23:59:20 -05002858{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002859 if (perf_header__push_event(event->event_type.event_type.event_id,
2860 event->event_type.event_type.name) < 0)
Tom Zanussicd19a032010-04-01 23:59:20 -05002861 return -ENOMEM;
2862
2863 return 0;
2864}
Tom Zanussi92155452010-04-01 23:59:21 -05002865
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002866int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002867 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002868 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05002869{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002870 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02002871 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05002872 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002873 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05002874
Jiri Olsa29208e52011-10-20 15:59:43 +02002875 /*
2876 * We are going to store the size of the data followed
2877 * by the data contents. Since the fd descriptor is a pipe,
2878 * we cannot seek back to store the size of the data once
2879 * we know it. Instead we:
2880 *
2881 * - write the tracing data to the temp file
2882 * - get/write the data size to pipe
2883 * - write the tracing data from the temp file
2884 * to the pipe
2885 */
2886 tdata = tracing_data_get(&evlist->entries, fd, true);
2887 if (!tdata)
2888 return -1;
2889
Tom Zanussi92155452010-04-01 23:59:21 -05002890 memset(&ev, 0, sizeof(ev));
2891
2892 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02002893 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002894 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05002895 padding = aligned_size - size;
2896 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2897 ev.tracing_data.size = aligned_size;
2898
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002899 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05002900
Jiri Olsa29208e52011-10-20 15:59:43 +02002901 /*
2902 * The put function will copy all the tracing data
2903 * stored in temp file to the pipe.
2904 */
2905 tracing_data_put(tdata);
2906
Tom Zanussi92155452010-04-01 23:59:21 -05002907 write_padded(fd, NULL, 0, padding);
2908
2909 return aligned_size;
2910}
2911
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002912int perf_event__process_tracing_data(union perf_event *event,
2913 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05002914{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002915 ssize_t size_read, padding, size = event->tracing_data.size;
Tom Zanussi92155452010-04-01 23:59:21 -05002916 off_t offset = lseek(session->fd, 0, SEEK_CUR);
2917 char buf[BUFSIZ];
2918
2919 /* setup for reading amidst mmap */
2920 lseek(session->fd, offset + sizeof(struct tracing_data_event),
2921 SEEK_SET);
2922
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002923 size_read = trace_report(session->fd, &session->pevent,
2924 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002925 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05002926
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002927 if (readn(session->fd, buf, padding) < 0) {
2928 pr_err("%s: reading input file", __func__);
2929 return -1;
2930 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002931 if (session->repipe) {
2932 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002933 if (retw <= 0 || retw != padding) {
2934 pr_err("%s: repiping tracing data padding", __func__);
2935 return -1;
2936 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002937 }
Tom Zanussi92155452010-04-01 23:59:21 -05002938
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002939 if (size_read + padding != size) {
2940 pr_err("%s: tracing data size mismatch", __func__);
2941 return -1;
2942 }
Tom Zanussi92155452010-04-01 23:59:21 -05002943
Namhyung Kim831394b2012-09-06 11:10:46 +09002944 perf_evlist__prepare_tracepoint_events(session->evlist,
2945 session->pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03002946
Tom Zanussi92155452010-04-01 23:59:21 -05002947 return size_read + padding;
2948}
Tom Zanussic7929e42010-04-01 23:59:22 -05002949
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002950int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002951 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002952 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002953 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05002954{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002955 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05002956 size_t len;
2957 int err = 0;
2958
2959 if (!pos->hit)
2960 return err;
2961
2962 memset(&ev, 0, sizeof(ev));
2963
2964 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002965 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05002966 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2967 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2968 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002969 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05002970 ev.build_id.header.size = sizeof(ev.build_id) + len;
2971 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2972
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002973 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05002974
2975 return err;
2976}
2977
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002978int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002979 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002980 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05002981{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002982 __event_process_build_id(&event->build_id,
2983 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002984 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05002985 return 0;
2986}
Stephane Eraniana1ac1d32010-06-17 11:39:01 +02002987
2988void disable_buildid_cache(void)
2989{
2990 no_buildid_cache = true;
2991}