blob: a9b7349f7c5fc7e35a42dd446b98b84ee256b392 [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 Drepperf3054c72012-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 Drepperf3054c72012-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/*
Namhyung Kima8bb5592013-01-22 18:09:31 +09001088 * File format:
1089 *
1090 * struct group_descs {
1091 * u32 nr_groups;
1092 * struct group_desc {
1093 * char name[];
1094 * u32 leader_idx;
1095 * u32 nr_members;
1096 * }[nr_groups];
1097 * };
1098 */
1099static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
1100 struct perf_evlist *evlist)
1101{
1102 u32 nr_groups = evlist->nr_groups;
1103 struct perf_evsel *evsel;
1104 int ret;
1105
1106 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
1107 if (ret < 0)
1108 return ret;
1109
1110 list_for_each_entry(evsel, &evlist->entries, node) {
1111 if (perf_evsel__is_group_leader(evsel) &&
1112 evsel->nr_members > 1) {
1113 const char *name = evsel->group_name ?: "{anon_group}";
1114 u32 leader_idx = evsel->idx;
1115 u32 nr_members = evsel->nr_members;
1116
1117 ret = do_write_string(fd, name);
1118 if (ret < 0)
1119 return ret;
1120
1121 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
1122 if (ret < 0)
1123 return ret;
1124
1125 ret = do_write(fd, &nr_members, sizeof(nr_members));
1126 if (ret < 0)
1127 return ret;
1128 }
1129 }
1130 return 0;
1131}
1132
1133/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001134 * default get_cpuid(): nothing gets recorded
1135 * actual implementation must be in arch/$(ARCH)/util/header.c
1136 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001137int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
1138 size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001139{
1140 return -1;
1141}
1142
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001143static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
1144 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001145{
1146 char buffer[64];
1147 int ret;
1148
1149 ret = get_cpuid(buffer, sizeof(buffer));
1150 if (!ret)
1151 goto write_it;
1152
1153 return -1;
1154write_it:
1155 return do_write_string(fd, buffer);
1156}
1157
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001158static int write_branch_stack(int fd __maybe_unused,
1159 struct perf_header *h __maybe_unused,
1160 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +01001161{
1162 return 0;
1163}
1164
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001165static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1166 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001167{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001168 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001169}
1170
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001171static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1172 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001173{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001174 fprintf(fp, "# os release : %s\n", ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001175}
1176
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001177static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001178{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001179 fprintf(fp, "# arch : %s\n", ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001180}
1181
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001182static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1183 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001184{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001185 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001186}
1187
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001188static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1189 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001190{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001191 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1192 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001193}
1194
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001195static void print_version(struct perf_header *ph, int fd __maybe_unused,
1196 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001197{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001198 fprintf(fp, "# perf version : %s\n", ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001199}
1200
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001201static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1202 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001203{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001204 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001205 char *str;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001206
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001207 nr = ph->env.nr_cmdline;
1208 str = ph->env.cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001209
1210 fprintf(fp, "# cmdline : ");
1211
1212 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001213 fprintf(fp, "%s ", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001214 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001215 }
1216 fputc('\n', fp);
1217}
1218
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001219static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1220 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001221{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001222 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001223 char *str;
1224
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001225 nr = ph->env.nr_sibling_cores;
1226 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001227
1228 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001229 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001230 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001231 }
1232
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001233 nr = ph->env.nr_sibling_threads;
1234 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001235
1236 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001237 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001238 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001239 }
1240}
1241
Robert Richter4e1b9c62012-08-16 21:10:22 +02001242static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001243{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001244 struct perf_evsel *evsel;
1245
1246 if (!events)
1247 return;
1248
1249 for (evsel = events; evsel->attr.size; evsel++) {
1250 if (evsel->name)
1251 free(evsel->name);
1252 if (evsel->id)
1253 free(evsel->id);
1254 }
1255
1256 free(events);
1257}
1258
1259static struct perf_evsel *
1260read_event_desc(struct perf_header *ph, int fd)
1261{
1262 struct perf_evsel *evsel, *events = NULL;
1263 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001264 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001265 u32 nre, sz, nr, i, j;
1266 ssize_t ret;
1267 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001268
1269 /* number of events */
Namhyung Kim5323f602012-12-17 15:38:54 +09001270 ret = readn(fd, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001271 if (ret != (ssize_t)sizeof(nre))
1272 goto error;
1273
1274 if (ph->needs_swap)
1275 nre = bswap_32(nre);
1276
Namhyung Kim5323f602012-12-17 15:38:54 +09001277 ret = readn(fd, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001278 if (ret != (ssize_t)sizeof(sz))
1279 goto error;
1280
1281 if (ph->needs_swap)
1282 sz = bswap_32(sz);
1283
Stephane Eranian62db9062012-02-09 23:21:07 +01001284 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001285 buf = malloc(sz);
1286 if (!buf)
1287 goto error;
1288
Robert Richter4e1b9c62012-08-16 21:10:22 +02001289 /* the last event terminates with evsel->attr.size == 0: */
1290 events = calloc(nre + 1, sizeof(*events));
1291 if (!events)
1292 goto error;
1293
1294 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001295 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001296 msz = sz;
1297
Robert Richter4e1b9c62012-08-16 21:10:22 +02001298 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1299 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001300
Stephane Eranian62db9062012-02-09 23:21:07 +01001301 /*
1302 * must read entire on-file attr struct to
1303 * sync up with layout.
1304 */
Namhyung Kim5323f602012-12-17 15:38:54 +09001305 ret = readn(fd, buf, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001306 if (ret != (ssize_t)sz)
1307 goto error;
1308
1309 if (ph->needs_swap)
1310 perf_event__attr_swap(buf);
1311
Robert Richter4e1b9c62012-08-16 21:10:22 +02001312 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001313
Namhyung Kim5323f602012-12-17 15:38:54 +09001314 ret = readn(fd, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001315 if (ret != (ssize_t)sizeof(nr))
1316 goto error;
1317
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001318 if (ph->needs_swap) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001319 nr = bswap_32(nr);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001320 evsel->needs_swap = true;
1321 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001322
Robert Richter4e1b9c62012-08-16 21:10:22 +02001323 evsel->name = do_read_string(fd, ph);
1324
1325 if (!nr)
1326 continue;
1327
1328 id = calloc(nr, sizeof(*id));
1329 if (!id)
1330 goto error;
1331 evsel->ids = nr;
1332 evsel->id = id;
1333
1334 for (j = 0 ; j < nr; j++) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001335 ret = readn(fd, id, sizeof(*id));
Robert Richter4e1b9c62012-08-16 21:10:22 +02001336 if (ret != (ssize_t)sizeof(*id))
1337 goto error;
1338 if (ph->needs_swap)
1339 *id = bswap_64(*id);
1340 id++;
1341 }
1342 }
1343out:
1344 if (buf)
1345 free(buf);
1346 return events;
1347error:
1348 if (events)
1349 free_event_desc(events);
1350 events = NULL;
1351 goto out;
1352}
1353
1354static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1355{
1356 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1357 u32 j;
1358 u64 *id;
1359
1360 if (!events) {
1361 fprintf(fp, "# event desc: not available or unable to read\n");
1362 return;
1363 }
1364
1365 for (evsel = events; evsel->attr.size; evsel++) {
1366 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001367
1368 fprintf(fp, "type = %d, config = 0x%"PRIx64
1369 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
Robert Richter4e1b9c62012-08-16 21:10:22 +02001370 evsel->attr.type,
1371 (u64)evsel->attr.config,
1372 (u64)evsel->attr.config1,
1373 (u64)evsel->attr.config2);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001374
1375 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001376 evsel->attr.exclude_user,
1377 evsel->attr.exclude_kernel);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001378
David Ahern78b961f2012-07-20 17:25:52 -06001379 fprintf(fp, ", excl_host = %d, excl_guest = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001380 evsel->attr.exclude_host,
1381 evsel->attr.exclude_guest);
David Ahern78b961f2012-07-20 17:25:52 -06001382
Robert Richter4e1b9c62012-08-16 21:10:22 +02001383 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
David Ahern78b961f2012-07-20 17:25:52 -06001384
Robert Richter4e1b9c62012-08-16 21:10:22 +02001385 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001386 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001387 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1388 if (j)
1389 fputc(',', fp);
1390 fprintf(fp, " %"PRIu64, *id);
1391 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001392 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001393 }
1394
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001395 fputc('\n', fp);
1396 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001397
1398 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001399}
1400
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001401static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001402 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001403{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001404 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001405}
1406
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001407static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001408 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001409{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001410 u32 nr, c, i;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001411 char *str, *tmp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001412 uint64_t mem_total, mem_free;
1413
1414 /* nr nodes */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001415 nr = ph->env.nr_numa_nodes;
1416 str = ph->env.numa_nodes;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001417
1418 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001419 /* node number */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001420 c = strtoul(str, &tmp, 0);
1421 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001422 goto error;
1423
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001424 str = tmp + 1;
1425 mem_total = strtoull(str, &tmp, 0);
1426 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001427 goto error;
1428
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001429 str = tmp + 1;
1430 mem_free = strtoull(str, &tmp, 0);
1431 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001432 goto error;
1433
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001434 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1435 " free = %"PRIu64" kB\n",
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001436 c, mem_total, mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001437
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001438 str = tmp + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001439 fprintf(fp, "# node%u cpu list : %s\n", c, str);
Namhyung Kim12344712012-10-23 22:44:49 +09001440
1441 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001442 }
1443 return;
1444error:
1445 fprintf(fp, "# numa topology : not available\n");
1446}
1447
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001448static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001449{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001450 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001451}
1452
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001453static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001454 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001455{
1456 fprintf(fp, "# contains samples with branch stack\n");
1457}
1458
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001459static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1460 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001461{
1462 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001463 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001464 u32 pmu_num;
1465 u32 type;
1466
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001467 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001468 if (!pmu_num) {
1469 fprintf(fp, "# pmu mappings: not available\n");
1470 return;
1471 }
1472
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001473 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001474
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001475 while (pmu_num) {
1476 type = strtoul(str, &tmp, 0);
1477 if (*tmp != ':')
1478 goto error;
1479
1480 str = tmp + 1;
1481 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1482
Robert Richter50a96672012-08-16 21:10:24 +02001483 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001484 str += strlen(str) + 1;
1485 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001486 }
1487
1488 fprintf(fp, "\n");
1489
1490 if (!pmu_num)
1491 return;
1492error:
1493 fprintf(fp, "# pmu mappings: unable to read\n");
1494}
1495
Namhyung Kima8bb5592013-01-22 18:09:31 +09001496static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1497 FILE *fp)
1498{
1499 struct perf_session *session;
1500 struct perf_evsel *evsel;
1501 u32 nr = 0;
1502
1503 session = container_of(ph, struct perf_session, header);
1504
1505 list_for_each_entry(evsel, &session->evlist->entries, node) {
1506 if (perf_evsel__is_group_leader(evsel) &&
1507 evsel->nr_members > 1) {
1508 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1509 perf_evsel__name(evsel));
1510
1511 nr = evsel->nr_members - 1;
1512 } else if (nr) {
1513 fprintf(fp, ",%s", perf_evsel__name(evsel));
1514
1515 if (--nr == 0)
1516 fprintf(fp, "}\n");
1517 }
1518 }
1519}
1520
Robert Richter08d95bd2012-02-10 15:41:55 +01001521static int __event_process_build_id(struct build_id_event *bev,
1522 char *filename,
1523 struct perf_session *session)
1524{
1525 int err = -1;
1526 struct list_head *head;
1527 struct machine *machine;
1528 u16 misc;
1529 struct dso *dso;
1530 enum dso_kernel_type dso_type;
1531
1532 machine = perf_session__findnew_machine(session, bev->pid);
1533 if (!machine)
1534 goto out;
1535
1536 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1537
1538 switch (misc) {
1539 case PERF_RECORD_MISC_KERNEL:
1540 dso_type = DSO_TYPE_KERNEL;
1541 head = &machine->kernel_dsos;
1542 break;
1543 case PERF_RECORD_MISC_GUEST_KERNEL:
1544 dso_type = DSO_TYPE_GUEST_KERNEL;
1545 head = &machine->kernel_dsos;
1546 break;
1547 case PERF_RECORD_MISC_USER:
1548 case PERF_RECORD_MISC_GUEST_USER:
1549 dso_type = DSO_TYPE_USER;
1550 head = &machine->user_dsos;
1551 break;
1552 default:
1553 goto out;
1554 }
1555
1556 dso = __dsos__findnew(head, filename);
1557 if (dso != NULL) {
1558 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1559
1560 dso__set_build_id(dso, &bev->build_id);
1561
1562 if (filename[0] == '[')
1563 dso->kernel = dso_type;
1564
1565 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1566 sbuild_id);
1567 pr_debug("build id event received for %s: %s\n",
1568 dso->long_name, sbuild_id);
1569 }
1570
1571 err = 0;
1572out:
1573 return err;
1574}
1575
1576static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1577 int input, u64 offset, u64 size)
1578{
1579 struct perf_session *session = container_of(header, struct perf_session, header);
1580 struct {
1581 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001582 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001583 char filename[0];
1584 } old_bev;
1585 struct build_id_event bev;
1586 char filename[PATH_MAX];
1587 u64 limit = offset + size;
1588
1589 while (offset < limit) {
1590 ssize_t len;
1591
Namhyung Kim5323f602012-12-17 15:38:54 +09001592 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001593 return -1;
1594
1595 if (header->needs_swap)
1596 perf_event_header__bswap(&old_bev.header);
1597
1598 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001599 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001600 return -1;
1601
1602 bev.header = old_bev.header;
1603
1604 /*
1605 * As the pid is the missing value, we need to fill
1606 * it properly. The header.misc value give us nice hint.
1607 */
1608 bev.pid = HOST_KERNEL_ID;
1609 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1610 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1611 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1612
1613 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1614 __event_process_build_id(&bev, filename, session);
1615
1616 offset += bev.header.size;
1617 }
1618
1619 return 0;
1620}
1621
1622static int perf_header__read_build_ids(struct perf_header *header,
1623 int input, u64 offset, u64 size)
1624{
1625 struct perf_session *session = container_of(header, struct perf_session, header);
1626 struct build_id_event bev;
1627 char filename[PATH_MAX];
1628 u64 limit = offset + size, orig_offset = offset;
1629 int err = -1;
1630
1631 while (offset < limit) {
1632 ssize_t len;
1633
Namhyung Kim5323f602012-12-17 15:38:54 +09001634 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001635 goto out;
1636
1637 if (header->needs_swap)
1638 perf_event_header__bswap(&bev.header);
1639
1640 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001641 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001642 goto out;
1643 /*
1644 * The a1645ce1 changeset:
1645 *
1646 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1647 *
1648 * Added a field to struct build_id_event that broke the file
1649 * format.
1650 *
1651 * Since the kernel build-id is the first entry, process the
1652 * table using the old format if the well known
1653 * '[kernel.kallsyms]' string for the kernel build-id has the
1654 * first 4 characters chopped off (where the pid_t sits).
1655 */
1656 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1657 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1658 return -1;
1659 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1660 }
1661
1662 __event_process_build_id(&bev, filename, session);
1663
1664 offset += bev.header.size;
1665 }
1666 err = 0;
1667out:
1668 return err;
1669}
1670
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001671static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1672 struct perf_header *ph __maybe_unused,
1673 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001674{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001675 trace_report(fd, data, false);
Robert Richterf1c67db2012-02-10 15:41:56 +01001676 return 0;
1677}
1678
1679static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001680 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001681 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001682{
1683 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1684 pr_debug("Failed to read buildids, continuing...\n");
1685 return 0;
1686}
1687
Namhyung Kima1ae5652012-09-24 17:14:59 +09001688static int process_hostname(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001689 struct perf_header *ph, int fd,
1690 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001691{
1692 ph->env.hostname = do_read_string(fd, ph);
1693 return ph->env.hostname ? 0 : -ENOMEM;
1694}
1695
1696static int process_osrelease(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001697 struct perf_header *ph, int fd,
1698 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001699{
1700 ph->env.os_release = do_read_string(fd, ph);
1701 return ph->env.os_release ? 0 : -ENOMEM;
1702}
1703
1704static int process_version(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001705 struct perf_header *ph, int fd,
1706 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001707{
1708 ph->env.version = do_read_string(fd, ph);
1709 return ph->env.version ? 0 : -ENOMEM;
1710}
1711
1712static int process_arch(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001713 struct perf_header *ph, int fd,
1714 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001715{
1716 ph->env.arch = do_read_string(fd, ph);
1717 return ph->env.arch ? 0 : -ENOMEM;
1718}
1719
1720static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001721 struct perf_header *ph, int fd,
1722 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001723{
1724 size_t ret;
1725 u32 nr;
1726
Namhyung Kim5323f602012-12-17 15:38:54 +09001727 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001728 if (ret != sizeof(nr))
1729 return -1;
1730
1731 if (ph->needs_swap)
1732 nr = bswap_32(nr);
1733
1734 ph->env.nr_cpus_online = nr;
1735
Namhyung Kim5323f602012-12-17 15:38:54 +09001736 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001737 if (ret != sizeof(nr))
1738 return -1;
1739
1740 if (ph->needs_swap)
1741 nr = bswap_32(nr);
1742
1743 ph->env.nr_cpus_avail = nr;
1744 return 0;
1745}
1746
1747static int process_cpudesc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001748 struct perf_header *ph, int fd,
1749 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001750{
1751 ph->env.cpu_desc = do_read_string(fd, ph);
1752 return ph->env.cpu_desc ? 0 : -ENOMEM;
1753}
1754
1755static int process_cpuid(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001756 struct perf_header *ph, int fd,
1757 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001758{
1759 ph->env.cpuid = do_read_string(fd, ph);
1760 return ph->env.cpuid ? 0 : -ENOMEM;
1761}
1762
1763static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001764 struct perf_header *ph, int fd,
1765 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001766{
1767 uint64_t mem;
1768 size_t ret;
1769
Namhyung Kim5323f602012-12-17 15:38:54 +09001770 ret = readn(fd, &mem, sizeof(mem));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001771 if (ret != sizeof(mem))
1772 return -1;
1773
1774 if (ph->needs_swap)
1775 mem = bswap_64(mem);
1776
1777 ph->env.total_mem = mem;
1778 return 0;
1779}
1780
Robert Richter7c2f7af2012-08-16 21:10:23 +02001781static struct perf_evsel *
1782perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1783{
1784 struct perf_evsel *evsel;
1785
1786 list_for_each_entry(evsel, &evlist->entries, node) {
1787 if (evsel->idx == idx)
1788 return evsel;
1789 }
1790
1791 return NULL;
1792}
1793
1794static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001795perf_evlist__set_event_name(struct perf_evlist *evlist,
1796 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001797{
1798 struct perf_evsel *evsel;
1799
1800 if (!event->name)
1801 return;
1802
1803 evsel = perf_evlist__find_by_index(evlist, event->idx);
1804 if (!evsel)
1805 return;
1806
1807 if (evsel->name)
1808 return;
1809
1810 evsel->name = strdup(event->name);
1811}
1812
1813static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001814process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001815 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001816 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001817{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001818 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001819 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1820
1821 if (!events)
1822 return 0;
1823
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001824 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001825 for (evsel = events; evsel->attr.size; evsel++)
1826 perf_evlist__set_event_name(session->evlist, evsel);
1827
1828 free_event_desc(events);
1829
1830 return 0;
1831}
1832
Namhyung Kima1ae5652012-09-24 17:14:59 +09001833static int process_cmdline(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001834 struct perf_header *ph, int fd,
1835 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001836{
1837 size_t ret;
1838 char *str;
1839 u32 nr, i;
1840 struct strbuf sb;
1841
Namhyung Kim5323f602012-12-17 15:38:54 +09001842 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001843 if (ret != sizeof(nr))
1844 return -1;
1845
1846 if (ph->needs_swap)
1847 nr = bswap_32(nr);
1848
1849 ph->env.nr_cmdline = nr;
1850 strbuf_init(&sb, 128);
1851
1852 for (i = 0; i < nr; i++) {
1853 str = do_read_string(fd, ph);
1854 if (!str)
1855 goto error;
1856
1857 /* include a NULL character at the end */
1858 strbuf_add(&sb, str, strlen(str) + 1);
1859 free(str);
1860 }
1861 ph->env.cmdline = strbuf_detach(&sb, NULL);
1862 return 0;
1863
1864error:
1865 strbuf_release(&sb);
1866 return -1;
1867}
1868
1869static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001870 struct perf_header *ph, int fd,
1871 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001872{
1873 size_t ret;
1874 u32 nr, i;
1875 char *str;
1876 struct strbuf sb;
1877
Namhyung Kim5323f602012-12-17 15:38:54 +09001878 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001879 if (ret != sizeof(nr))
1880 return -1;
1881
1882 if (ph->needs_swap)
1883 nr = bswap_32(nr);
1884
1885 ph->env.nr_sibling_cores = nr;
1886 strbuf_init(&sb, 128);
1887
1888 for (i = 0; i < nr; i++) {
1889 str = do_read_string(fd, ph);
1890 if (!str)
1891 goto error;
1892
1893 /* include a NULL character at the end */
1894 strbuf_add(&sb, str, strlen(str) + 1);
1895 free(str);
1896 }
1897 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1898
Namhyung Kim5323f602012-12-17 15:38:54 +09001899 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001900 if (ret != sizeof(nr))
1901 return -1;
1902
1903 if (ph->needs_swap)
1904 nr = bswap_32(nr);
1905
1906 ph->env.nr_sibling_threads = nr;
1907
1908 for (i = 0; i < nr; i++) {
1909 str = do_read_string(fd, ph);
1910 if (!str)
1911 goto error;
1912
1913 /* include a NULL character at the end */
1914 strbuf_add(&sb, str, strlen(str) + 1);
1915 free(str);
1916 }
1917 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1918 return 0;
1919
1920error:
1921 strbuf_release(&sb);
1922 return -1;
1923}
1924
1925static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001926 struct perf_header *ph, int fd,
1927 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001928{
1929 size_t ret;
1930 u32 nr, node, i;
1931 char *str;
1932 uint64_t mem_total, mem_free;
1933 struct strbuf sb;
1934
1935 /* nr nodes */
Namhyung Kim5323f602012-12-17 15:38:54 +09001936 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001937 if (ret != sizeof(nr))
1938 goto error;
1939
1940 if (ph->needs_swap)
1941 nr = bswap_32(nr);
1942
1943 ph->env.nr_numa_nodes = nr;
1944 strbuf_init(&sb, 256);
1945
1946 for (i = 0; i < nr; i++) {
1947 /* node number */
Namhyung Kim5323f602012-12-17 15:38:54 +09001948 ret = readn(fd, &node, sizeof(node));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001949 if (ret != sizeof(node))
1950 goto error;
1951
Namhyung Kim5323f602012-12-17 15:38:54 +09001952 ret = readn(fd, &mem_total, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001953 if (ret != sizeof(u64))
1954 goto error;
1955
Namhyung Kim5323f602012-12-17 15:38:54 +09001956 ret = readn(fd, &mem_free, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001957 if (ret != sizeof(u64))
1958 goto error;
1959
1960 if (ph->needs_swap) {
1961 node = bswap_32(node);
1962 mem_total = bswap_64(mem_total);
1963 mem_free = bswap_64(mem_free);
1964 }
1965
1966 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1967 node, mem_total, mem_free);
1968
1969 str = do_read_string(fd, ph);
1970 if (!str)
1971 goto error;
1972
1973 /* include a NULL character at the end */
1974 strbuf_add(&sb, str, strlen(str) + 1);
1975 free(str);
1976 }
1977 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1978 return 0;
1979
1980error:
1981 strbuf_release(&sb);
1982 return -1;
1983}
1984
1985static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001986 struct perf_header *ph, int fd,
1987 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001988{
1989 size_t ret;
1990 char *name;
1991 u32 pmu_num;
1992 u32 type;
1993 struct strbuf sb;
1994
Namhyung Kim5323f602012-12-17 15:38:54 +09001995 ret = readn(fd, &pmu_num, sizeof(pmu_num));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001996 if (ret != sizeof(pmu_num))
1997 return -1;
1998
1999 if (ph->needs_swap)
2000 pmu_num = bswap_32(pmu_num);
2001
2002 if (!pmu_num) {
2003 pr_debug("pmu mappings not available\n");
2004 return 0;
2005 }
2006
2007 ph->env.nr_pmu_mappings = pmu_num;
2008 strbuf_init(&sb, 128);
2009
2010 while (pmu_num) {
Namhyung Kim5323f602012-12-17 15:38:54 +09002011 if (readn(fd, &type, sizeof(type)) != sizeof(type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002012 goto error;
2013 if (ph->needs_swap)
2014 type = bswap_32(type);
2015
2016 name = do_read_string(fd, ph);
2017 if (!name)
2018 goto error;
2019
2020 strbuf_addf(&sb, "%u:%s", type, name);
2021 /* include a NULL character at the end */
2022 strbuf_add(&sb, "", 1);
2023
2024 free(name);
2025 pmu_num--;
2026 }
2027 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2028 return 0;
2029
2030error:
2031 strbuf_release(&sb);
2032 return -1;
2033}
2034
Namhyung Kima8bb5592013-01-22 18:09:31 +09002035static int process_group_desc(struct perf_file_section *section __maybe_unused,
2036 struct perf_header *ph, int fd,
2037 void *data __maybe_unused)
2038{
2039 size_t ret = -1;
2040 u32 i, nr, nr_groups;
2041 struct perf_session *session;
2042 struct perf_evsel *evsel, *leader = NULL;
2043 struct group_desc {
2044 char *name;
2045 u32 leader_idx;
2046 u32 nr_members;
2047 } *desc;
2048
2049 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
2050 return -1;
2051
2052 if (ph->needs_swap)
2053 nr_groups = bswap_32(nr_groups);
2054
2055 ph->env.nr_groups = nr_groups;
2056 if (!nr_groups) {
2057 pr_debug("group desc not available\n");
2058 return 0;
2059 }
2060
2061 desc = calloc(nr_groups, sizeof(*desc));
2062 if (!desc)
2063 return -1;
2064
2065 for (i = 0; i < nr_groups; i++) {
2066 desc[i].name = do_read_string(fd, ph);
2067 if (!desc[i].name)
2068 goto out_free;
2069
2070 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
2071 goto out_free;
2072
2073 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
2074 goto out_free;
2075
2076 if (ph->needs_swap) {
2077 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
2078 desc[i].nr_members = bswap_32(desc[i].nr_members);
2079 }
2080 }
2081
2082 /*
2083 * Rebuild group relationship based on the group_desc
2084 */
2085 session = container_of(ph, struct perf_session, header);
2086 session->evlist->nr_groups = nr_groups;
2087
2088 i = nr = 0;
2089 list_for_each_entry(evsel, &session->evlist->entries, node) {
2090 if (evsel->idx == (int) desc[i].leader_idx) {
2091 evsel->leader = evsel;
2092 /* {anon_group} is a dummy name */
2093 if (strcmp(desc[i].name, "{anon_group}"))
2094 evsel->group_name = desc[i].name;
2095 evsel->nr_members = desc[i].nr_members;
2096
2097 if (i >= nr_groups || nr > 0) {
2098 pr_debug("invalid group desc\n");
2099 goto out_free;
2100 }
2101
2102 leader = evsel;
2103 nr = evsel->nr_members - 1;
2104 i++;
2105 } else if (nr) {
2106 /* This is a group member */
2107 evsel->leader = leader;
2108
2109 nr--;
2110 }
2111 }
2112
2113 if (i != nr_groups || nr != 0) {
2114 pr_debug("invalid group desc\n");
2115 goto out_free;
2116 }
2117
2118 ret = 0;
2119out_free:
2120 while ((int) --i >= 0)
2121 free(desc[i].name);
2122 free(desc);
2123
2124 return ret;
2125}
2126
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002127struct feature_ops {
2128 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2129 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01002130 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002131 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002132 const char *name;
2133 bool full_only;
2134};
2135
Robert Richter8cdfa782011-12-07 10:02:56 +01002136#define FEAT_OPA(n, func) \
2137 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01002138#define FEAT_OPP(n, func) \
2139 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2140 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01002141#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01002142 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09002143 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01002144
2145/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002146#define print_tracing_data NULL
2147#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002148
2149static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002150 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01002151 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002152 FEAT_OPP(HEADER_HOSTNAME, hostname),
2153 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2154 FEAT_OPP(HEADER_VERSION, version),
2155 FEAT_OPP(HEADER_ARCH, arch),
2156 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2157 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09002158 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002159 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02002160 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002161 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01002162 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2163 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01002164 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002165 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09002166 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002167};
2168
2169struct header_print_data {
2170 FILE *fp;
2171 bool full; /* extended list of headers */
2172};
2173
2174static int perf_file_section__fprintf_info(struct perf_file_section *section,
2175 struct perf_header *ph,
2176 int feat, int fd, void *data)
2177{
2178 struct header_print_data *hd = data;
2179
2180 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2181 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2182 "%d, continuing...\n", section->offset, feat);
2183 return 0;
2184 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002185 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002186 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002187 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002188 }
2189 if (!feat_ops[feat].print)
2190 return 0;
2191
2192 if (!feat_ops[feat].full_only || hd->full)
2193 feat_ops[feat].print(ph, fd, hd->fp);
2194 else
2195 fprintf(hd->fp, "# %s info available, use -I to display\n",
2196 feat_ops[feat].name);
2197
2198 return 0;
2199}
2200
2201int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2202{
2203 struct header_print_data hd;
2204 struct perf_header *header = &session->header;
2205 int fd = session->fd;
2206 hd.fp = fp;
2207 hd.full = full;
2208
2209 perf_header__process_sections(header, fd, &hd,
2210 perf_file_section__fprintf_info);
2211 return 0;
2212}
2213
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002214static int do_write_feat(int fd, struct perf_header *h, int type,
2215 struct perf_file_section **p,
2216 struct perf_evlist *evlist)
2217{
2218 int err;
2219 int ret = 0;
2220
2221 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002222 if (!feat_ops[type].write)
2223 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002224
2225 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2226
2227 err = feat_ops[type].write(fd, h, evlist);
2228 if (err < 0) {
2229 pr_debug("failed to write feature %d\n", type);
2230
2231 /* undo anything written */
2232 lseek(fd, (*p)->offset, SEEK_SET);
2233
2234 return -1;
2235 }
2236 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2237 (*p)++;
2238 }
2239 return ret;
2240}
2241
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002242static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002243 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002244{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002245 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002246 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002247 int sec_size;
2248 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002249 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002250 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002251
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002252 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002253 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002254 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002255
Paul Gortmaker91b98802013-01-30 20:05:49 -05002256 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002257 if (feat_sec == NULL)
2258 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002259
2260 sec_size = sizeof(*feat_sec) * nr_sections;
2261
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002262 sec_start = header->data_offset + header->data_size;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002263 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002264
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002265 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2266 if (do_write_feat(fd, header, feat, &p, evlist))
2267 perf_header__clear_feat(header, feat);
2268 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002269
Xiao Guangrongf887f302010-02-04 16:46:42 +08002270 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002271 /*
2272 * may write more than needed due to dropped feature, but
2273 * this is okay, reader will skip the mising entries
2274 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002275 err = do_write(fd, feat_sec, sec_size);
2276 if (err < 0)
2277 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002278 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002279 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002280}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002281
Tom Zanussi8dc58102010-04-01 23:59:15 -05002282int perf_header__write_pipe(int fd)
2283{
2284 struct perf_pipe_file_header f_header;
2285 int err;
2286
2287 f_header = (struct perf_pipe_file_header){
2288 .magic = PERF_MAGIC,
2289 .size = sizeof(f_header),
2290 };
2291
2292 err = do_write(fd, &f_header, sizeof(f_header));
2293 if (err < 0) {
2294 pr_debug("failed to write perf pipe header\n");
2295 return err;
2296 }
2297
2298 return 0;
2299}
2300
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002301int perf_session__write_header(struct perf_session *session,
2302 struct perf_evlist *evlist,
2303 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002304{
2305 struct perf_file_header f_header;
2306 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002307 struct perf_header *header = &session->header;
Robert Richter6606f872012-08-16 21:10:19 +02002308 struct perf_evsel *evsel, *pair = NULL;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002309 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002310
2311 lseek(fd, sizeof(f_header), SEEK_SET);
2312
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002313 if (session->evlist != evlist)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03002314 pair = perf_evlist__first(session->evlist);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002315
Robert Richter6606f872012-08-16 21:10:19 +02002316 list_for_each_entry(evsel, &evlist->entries, node) {
2317 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2318 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002319 if (err < 0) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002320out_err_write:
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002321 pr_debug("failed to write perf header\n");
2322 return err;
2323 }
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002324 if (session->evlist != evlist) {
2325 err = do_write(fd, pair->id, pair->ids * sizeof(u64));
2326 if (err < 0)
2327 goto out_err_write;
Robert Richter6606f872012-08-16 21:10:19 +02002328 evsel->ids += pair->ids;
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03002329 pair = perf_evsel__next(pair);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002330 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002331 }
2332
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002333 header->attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002334
Robert Richter6606f872012-08-16 21:10:19 +02002335 list_for_each_entry(evsel, &evlist->entries, node) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002336 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002337 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002338 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002339 .offset = evsel->id_offset,
2340 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002341 }
2342 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002343 err = do_write(fd, &f_attr, sizeof(f_attr));
2344 if (err < 0) {
2345 pr_debug("failed to write perf header attribute\n");
2346 return err;
2347 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002348 }
2349
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002350 header->event_offset = lseek(fd, 0, SEEK_CUR);
Robert Richterdb146f02012-08-16 21:10:20 +02002351 header->event_size = trace_event_count * sizeof(struct perf_trace_event_type);
2352 if (trace_events) {
2353 err = do_write(fd, trace_events, header->event_size);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002354 if (err < 0) {
2355 pr_debug("failed to write perf header events\n");
2356 return err;
2357 }
2358 }
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02002359
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002360 header->data_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002361
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002362 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002363 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002364 if (err < 0)
2365 return err;
2366 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002367
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002368 f_header = (struct perf_file_header){
2369 .magic = PERF_MAGIC,
2370 .size = sizeof(f_header),
2371 .attr_size = sizeof(f_attr),
2372 .attrs = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002373 .offset = header->attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002374 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002375 },
2376 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002377 .offset = header->data_offset,
2378 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002379 },
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02002380 .event_types = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002381 .offset = header->event_offset,
2382 .size = header->event_size,
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02002383 },
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002384 };
2385
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002386 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002387
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002388 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002389 err = do_write(fd, &f_header, sizeof(f_header));
2390 if (err < 0) {
2391 pr_debug("failed to write perf header\n");
2392 return err;
2393 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002394 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002395
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002396 header->frozen = 1;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002397 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002398}
2399
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002400static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002401 int fd, void *buf, size_t size)
2402{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002403 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002404 return -1;
2405
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002406 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002407 mem_bswap_64(buf, size);
2408
2409 return 0;
2410}
2411
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002412int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002413 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002414 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002415 struct perf_header *ph,
2416 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002417{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002418 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002419 int nr_sections;
2420 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002421 int feat;
2422 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002423
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002424 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002425 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002426 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002427
Paul Gortmaker91b98802013-01-30 20:05:49 -05002428 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002429 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002430 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002431
2432 sec_size = sizeof(*feat_sec) * nr_sections;
2433
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002434 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002435
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002436 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2437 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002438 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002439
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002440 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2441 err = process(sec++, header, feat, fd, data);
2442 if (err < 0)
2443 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002444 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002445 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002446out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002447 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002448 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002449}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002450
Stephane Eranian114382a2012-02-09 23:21:08 +01002451static const int attr_file_abi_sizes[] = {
2452 [0] = PERF_ATTR_SIZE_VER0,
2453 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002454 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002455 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian114382a2012-02-09 23:21:08 +01002456 0,
2457};
2458
2459/*
2460 * In the legacy file format, the magic number is not used to encode endianness.
2461 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2462 * on ABI revisions, we need to try all combinations for all endianness to
2463 * detect the endianness.
2464 */
2465static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2466{
2467 uint64_t ref_size, attr_size;
2468 int i;
2469
2470 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2471 ref_size = attr_file_abi_sizes[i]
2472 + sizeof(struct perf_file_section);
2473 if (hdr_sz != ref_size) {
2474 attr_size = bswap_64(hdr_sz);
2475 if (attr_size != ref_size)
2476 continue;
2477
2478 ph->needs_swap = true;
2479 }
2480 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2481 i,
2482 ph->needs_swap);
2483 return 0;
2484 }
2485 /* could not determine endianness */
2486 return -1;
2487}
2488
2489#define PERF_PIPE_HDR_VER0 16
2490
2491static const size_t attr_pipe_abi_sizes[] = {
2492 [0] = PERF_PIPE_HDR_VER0,
2493 0,
2494};
2495
2496/*
2497 * In the legacy pipe format, there is an implicit assumption that endiannesss
2498 * between host recording the samples, and host parsing the samples is the
2499 * same. This is not always the case given that the pipe output may always be
2500 * redirected into a file and analyzed on a different machine with possibly a
2501 * different endianness and perf_event ABI revsions in the perf tool itself.
2502 */
2503static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2504{
2505 u64 attr_size;
2506 int i;
2507
2508 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2509 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2510 attr_size = bswap_64(hdr_sz);
2511 if (attr_size != hdr_sz)
2512 continue;
2513
2514 ph->needs_swap = true;
2515 }
2516 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2517 return 0;
2518 }
2519 return -1;
2520}
2521
Feng Tange84ba4e2012-10-30 11:56:07 +08002522bool is_perf_magic(u64 magic)
2523{
2524 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2525 || magic == __perf_magic2
2526 || magic == __perf_magic2_sw)
2527 return true;
2528
2529 return false;
2530}
2531
Stephane Eranian114382a2012-02-09 23:21:08 +01002532static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2533 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002534{
2535 int ret;
2536
2537 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002538 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002539 if (ret == 0) {
2540 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002541 if (is_pipe)
2542 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002543
Stephane Eranian114382a2012-02-09 23:21:08 +01002544 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002545 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002546 /*
2547 * the new magic number serves two purposes:
2548 * - unique number to identify actual perf.data files
2549 * - encode endianness of file
2550 */
Stephane Eranian73323f52012-02-02 13:54:44 +01002551
Stephane Eranian114382a2012-02-09 23:21:08 +01002552 /* check magic number with one endianness */
2553 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002554 return 0;
2555
Stephane Eranian114382a2012-02-09 23:21:08 +01002556 /* check magic number with opposite endianness */
2557 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002558 return -1;
2559
2560 ph->needs_swap = true;
2561
2562 return 0;
2563}
2564
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002565int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002566 struct perf_header *ph, int fd)
2567{
Stephane Eranian73323f52012-02-02 13:54:44 +01002568 int ret;
2569
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002570 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002571
Stephane Eranian73323f52012-02-02 13:54:44 +01002572 ret = readn(fd, header, sizeof(*header));
2573 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002574 return -1;
2575
Stephane Eranian114382a2012-02-09 23:21:08 +01002576 if (check_magic_endian(header->magic,
2577 header->attr_size, false, ph) < 0) {
2578 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002579 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002580 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002581
Stephane Eranian73323f52012-02-02 13:54:44 +01002582 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002583 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002584 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002585 }
2586
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002587 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002588 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002589 if (header->size == offsetof(typeof(*header), adds_features))
2590 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002591 else
2592 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002593 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002594 /*
2595 * feature bitmap is declared as an array of unsigned longs --
2596 * not good since its size can differ between the host that
2597 * generated the data file and the host analyzing the file.
2598 *
2599 * We need to handle endianness, but we don't know the size of
2600 * the unsigned long where the file was generated. Take a best
2601 * guess at determining it: try 64-bit swap first (ie., file
2602 * created on a 64-bit host), and check if the hostname feature
2603 * bit is set (this feature bit is forced on as of fbe96f2).
2604 * If the bit is not, undo the 64-bit swap and try a 32-bit
2605 * swap. If the hostname bit is still not set (e.g., older data
2606 * file), punt and fallback to the original behavior --
2607 * clearing all feature bits and setting buildid.
2608 */
David Ahern80c01202012-06-08 11:47:51 -03002609 mem_bswap_64(&header->adds_features,
2610 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002611
2612 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002613 /* unswap as u64 */
2614 mem_bswap_64(&header->adds_features,
2615 BITS_TO_U64(HEADER_FEAT_BITS));
2616
2617 /* unswap as u32 */
2618 mem_bswap_32(&header->adds_features,
2619 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002620 }
2621
2622 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2623 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2624 set_bit(HEADER_BUILD_ID, header->adds_features);
2625 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002626 }
2627
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002628 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002629 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002630
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002631 ph->event_offset = header->event_types.offset;
2632 ph->event_size = header->event_types.size;
2633 ph->data_offset = header->data.offset;
2634 ph->data_size = header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002635 return 0;
2636}
2637
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002638static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002639 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002640 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002641{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002642 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002643 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002644 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002645 return 0;
2646 }
2647
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002648 if (feat >= HEADER_LAST_FEATURE) {
2649 pr_debug("unknown feature %d, continuing...\n", feat);
2650 return 0;
2651 }
2652
Robert Richterf1c67db2012-02-10 15:41:56 +01002653 if (!feat_ops[feat].process)
2654 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002655
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002656 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002657}
2658
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002659static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002660 struct perf_header *ph, int fd,
2661 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002662{
Stephane Eranian73323f52012-02-02 13:54:44 +01002663 int ret;
2664
2665 ret = readn(fd, header, sizeof(*header));
2666 if (ret <= 0)
2667 return -1;
2668
Stephane Eranian114382a2012-02-09 23:21:08 +01002669 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2670 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002671 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002672 }
2673
2674 if (ph->needs_swap)
2675 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002676
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002677 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002678 return -1;
2679
Tom Zanussi8dc58102010-04-01 23:59:15 -05002680 return 0;
2681}
2682
2683static int perf_header__read_pipe(struct perf_session *session, int fd)
2684{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002685 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002686 struct perf_pipe_file_header f_header;
2687
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002688 if (perf_file_header__read_pipe(&f_header, header, fd,
Tom Zanussi454c4072010-05-01 01:41:20 -05002689 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002690 pr_debug("incompatible file format\n");
2691 return -EINVAL;
2692 }
2693
2694 session->fd = fd;
2695
2696 return 0;
2697}
2698
Stephane Eranian69996df2012-02-09 23:21:06 +01002699static int read_attr(int fd, struct perf_header *ph,
2700 struct perf_file_attr *f_attr)
2701{
2702 struct perf_event_attr *attr = &f_attr->attr;
2703 size_t sz, left;
2704 size_t our_sz = sizeof(f_attr->attr);
2705 int ret;
2706
2707 memset(f_attr, 0, sizeof(*f_attr));
2708
2709 /* read minimal guaranteed structure */
2710 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2711 if (ret <= 0) {
2712 pr_debug("cannot read %d bytes of header attr\n",
2713 PERF_ATTR_SIZE_VER0);
2714 return -1;
2715 }
2716
2717 /* on file perf_event_attr size */
2718 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002719
Stephane Eranian69996df2012-02-09 23:21:06 +01002720 if (ph->needs_swap)
2721 sz = bswap_32(sz);
2722
2723 if (sz == 0) {
2724 /* assume ABI0 */
2725 sz = PERF_ATTR_SIZE_VER0;
2726 } else if (sz > our_sz) {
2727 pr_debug("file uses a more recent and unsupported ABI"
2728 " (%zu bytes extra)\n", sz - our_sz);
2729 return -1;
2730 }
2731 /* what we have not yet read and that we know about */
2732 left = sz - PERF_ATTR_SIZE_VER0;
2733 if (left) {
2734 void *ptr = attr;
2735 ptr += PERF_ATTR_SIZE_VER0;
2736
2737 ret = readn(fd, ptr, left);
2738 }
2739 /* read perf_file_section, ids are read in caller */
2740 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2741
2742 return ret <= 0 ? -1 : 0;
2743}
2744
Namhyung Kim831394b2012-09-06 11:10:46 +09002745static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2746 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002747{
Namhyung Kim831394b2012-09-06 11:10:46 +09002748 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002749 char bf[128];
2750
Namhyung Kim831394b2012-09-06 11:10:46 +09002751 /* already prepared */
2752 if (evsel->tp_format)
2753 return 0;
2754
2755 event = pevent_find_event(pevent, evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002756 if (event == NULL)
2757 return -1;
2758
Namhyung Kim831394b2012-09-06 11:10:46 +09002759 if (!evsel->name) {
2760 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2761 evsel->name = strdup(bf);
2762 if (evsel->name == NULL)
2763 return -1;
2764 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002765
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002766 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002767 return 0;
2768}
2769
Namhyung Kim831394b2012-09-06 11:10:46 +09002770static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2771 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002772{
2773 struct perf_evsel *pos;
2774
2775 list_for_each_entry(pos, &evlist->entries, node) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002776 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2777 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002778 return -1;
2779 }
2780
2781 return 0;
2782}
2783
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002784int perf_session__read_header(struct perf_session *session, int fd)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002785{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002786 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002787 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002788 struct perf_file_attr f_attr;
2789 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002790 int nr_attrs, nr_ids, i, j;
2791
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002792 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002793 if (session->evlist == NULL)
2794 return -ENOMEM;
2795
Tom Zanussi8dc58102010-04-01 23:59:15 -05002796 if (session->fd_pipe)
2797 return perf_header__read_pipe(session, fd);
2798
Stephane Eranian69996df2012-02-09 23:21:06 +01002799 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002800 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002801
Stephane Eranian69996df2012-02-09 23:21:06 +01002802 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002803 lseek(fd, f_header.attrs.offset, SEEK_SET);
2804
2805 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002806 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002807 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002808
Stephane Eranian69996df2012-02-09 23:21:06 +01002809 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002810 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002811
David Aherneda39132011-07-15 12:34:09 -06002812 if (header->needs_swap)
2813 perf_event__attr_swap(&f_attr.attr);
2814
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002815 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002816 evsel = perf_evsel__new(&f_attr.attr, i);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002817
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002818 if (evsel == NULL)
2819 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002820
2821 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002822 /*
2823 * Do it before so that if perf_evsel__alloc_id fails, this
2824 * entry gets purged too at perf_evlist__delete().
2825 */
2826 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002827
2828 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002829 /*
2830 * We don't have the cpu and thread maps on the header, so
2831 * for allocating the perf_sample_id table we fake 1 cpu and
2832 * hattr->ids threads.
2833 */
2834 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2835 goto out_delete_evlist;
2836
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002837 lseek(fd, f_attr.ids.offset, SEEK_SET);
2838
2839 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002840 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002841 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002842
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002843 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002844 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002845
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002846 lseek(fd, tmp, SEEK_SET);
2847 }
2848
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002849 symbol_conf.nr_events = nr_attrs;
2850
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02002851 if (f_header.event_types.size) {
2852 lseek(fd, f_header.event_types.offset, SEEK_SET);
Robert Richterdb146f02012-08-16 21:10:20 +02002853 trace_events = malloc(f_header.event_types.size);
2854 if (trace_events == NULL)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002855 return -ENOMEM;
Robert Richterdb146f02012-08-16 21:10:20 +02002856 if (perf_header__getbuffer64(header, fd, trace_events,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002857 f_header.event_types.size))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002858 goto out_errno;
Robert Richterdb146f02012-08-16 21:10:20 +02002859 trace_event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02002860 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +02002861
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002862 perf_header__process_sections(header, fd, &session->pevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002863 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002864
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002865 lseek(fd, header->data_offset, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002866
Namhyung Kim831394b2012-09-06 11:10:46 +09002867 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2868 session->pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002869 goto out_delete_evlist;
2870
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002871 header->frozen = 1;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002872 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002873out_errno:
2874 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002875
2876out_delete_evlist:
2877 perf_evlist__delete(session->evlist);
2878 session->evlist = NULL;
2879 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002880}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002881
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002882int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002883 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002884 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002885{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002886 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002887 size_t size;
2888 int err;
2889
2890 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002891 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002892 size += sizeof(struct perf_event_header);
2893 size += ids * sizeof(u64);
2894
2895 ev = malloc(size);
2896
Chris Samuelce47dc52010-11-13 13:35:06 +11002897 if (ev == NULL)
2898 return -ENOMEM;
2899
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002900 ev->attr.attr = *attr;
2901 memcpy(ev->attr.id, id, ids * sizeof(u64));
2902
2903 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002904 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002905
Robert Richterf4d83432012-08-16 21:10:17 +02002906 if (ev->attr.header.size == size)
2907 err = process(tool, ev, NULL, NULL);
2908 else
2909 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002910
2911 free(ev);
2912
2913 return err;
2914}
2915
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002916int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002917 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002918 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002919{
Robert Richter6606f872012-08-16 21:10:19 +02002920 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002921 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002922
Robert Richter6606f872012-08-16 21:10:19 +02002923 list_for_each_entry(evsel, &session->evlist->entries, node) {
2924 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2925 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002926 if (err) {
2927 pr_debug("failed to create perf header attribute\n");
2928 return err;
2929 }
2930 }
2931
2932 return err;
2933}
2934
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002935int perf_event__process_attr(union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002936 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002937{
Robert Richterf4d83432012-08-16 21:10:17 +02002938 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002939 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002940 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002941
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002942 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002943 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002944 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002945 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002946 }
2947
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002948 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002949 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002950 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002951
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002952 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002953
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002954 ids = event->header.size;
2955 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002956 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002957 /*
2958 * We don't have the cpu and thread maps on the header, so
2959 * for allocating the perf_sample_id table we fake 1 cpu and
2960 * hattr->ids threads.
2961 */
2962 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2963 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002964
2965 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002966 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002967 }
2968
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002969 return 0;
2970}
Tom Zanussicd19a032010-04-01 23:59:20 -05002971
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002972int perf_event__synthesize_event_type(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002973 u64 event_id, char *name,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002974 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002975 struct machine *machine)
Tom Zanussicd19a032010-04-01 23:59:20 -05002976{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002977 union perf_event ev;
Tom Zanussicd19a032010-04-01 23:59:20 -05002978 size_t size = 0;
2979 int err = 0;
2980
2981 memset(&ev, 0, sizeof(ev));
2982
2983 ev.event_type.event_type.event_id = event_id;
2984 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
2985 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
2986
2987 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
Stephane Eranianf8f4b282012-01-20 10:49:12 +01002988 size = strlen(ev.event_type.event_type.name);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002989 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussicd19a032010-04-01 23:59:20 -05002990 ev.event_type.header.size = sizeof(ev.event_type) -
2991 (sizeof(ev.event_type.event_type.name) - size);
2992
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002993 err = process(tool, &ev, NULL, machine);
Tom Zanussicd19a032010-04-01 23:59:20 -05002994
2995 return err;
2996}
2997
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002998int perf_event__synthesize_event_types(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002999 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003000 struct machine *machine)
Tom Zanussicd19a032010-04-01 23:59:20 -05003001{
3002 struct perf_trace_event_type *type;
3003 int i, err = 0;
3004
Robert Richterdb146f02012-08-16 21:10:20 +02003005 for (i = 0; i < trace_event_count; i++) {
3006 type = &trace_events[i];
Tom Zanussicd19a032010-04-01 23:59:20 -05003007
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003008 err = perf_event__synthesize_event_type(tool, type->event_id,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003009 type->name, process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003010 machine);
Tom Zanussicd19a032010-04-01 23:59:20 -05003011 if (err) {
3012 pr_debug("failed to create perf header event type\n");
3013 return err;
3014 }
3015 }
3016
3017 return err;
3018}
3019
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003020int perf_event__process_event_type(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003021 union perf_event *event)
Tom Zanussicd19a032010-04-01 23:59:20 -05003022{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003023 if (perf_header__push_event(event->event_type.event_type.event_id,
3024 event->event_type.event_type.name) < 0)
Tom Zanussicd19a032010-04-01 23:59:20 -05003025 return -ENOMEM;
3026
3027 return 0;
3028}
Tom Zanussi92155452010-04-01 23:59:21 -05003029
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003030int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003031 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003032 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05003033{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003034 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02003035 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05003036 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003037 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05003038
Jiri Olsa29208e52011-10-20 15:59:43 +02003039 /*
3040 * We are going to store the size of the data followed
3041 * by the data contents. Since the fd descriptor is a pipe,
3042 * we cannot seek back to store the size of the data once
3043 * we know it. Instead we:
3044 *
3045 * - write the tracing data to the temp file
3046 * - get/write the data size to pipe
3047 * - write the tracing data from the temp file
3048 * to the pipe
3049 */
3050 tdata = tracing_data_get(&evlist->entries, fd, true);
3051 if (!tdata)
3052 return -1;
3053
Tom Zanussi92155452010-04-01 23:59:21 -05003054 memset(&ev, 0, sizeof(ev));
3055
3056 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02003057 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003058 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05003059 padding = aligned_size - size;
3060 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3061 ev.tracing_data.size = aligned_size;
3062
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003063 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05003064
Jiri Olsa29208e52011-10-20 15:59:43 +02003065 /*
3066 * The put function will copy all the tracing data
3067 * stored in temp file to the pipe.
3068 */
3069 tracing_data_put(tdata);
3070
Tom Zanussi92155452010-04-01 23:59:21 -05003071 write_padded(fd, NULL, 0, padding);
3072
3073 return aligned_size;
3074}
3075
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003076int perf_event__process_tracing_data(union perf_event *event,
3077 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05003078{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003079 ssize_t size_read, padding, size = event->tracing_data.size;
Tom Zanussi92155452010-04-01 23:59:21 -05003080 off_t offset = lseek(session->fd, 0, SEEK_CUR);
3081 char buf[BUFSIZ];
3082
3083 /* setup for reading amidst mmap */
3084 lseek(session->fd, offset + sizeof(struct tracing_data_event),
3085 SEEK_SET);
3086
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003087 size_read = trace_report(session->fd, &session->pevent,
3088 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003089 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003090
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003091 if (readn(session->fd, buf, padding) < 0) {
3092 pr_err("%s: reading input file", __func__);
3093 return -1;
3094 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003095 if (session->repipe) {
3096 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003097 if (retw <= 0 || retw != padding) {
3098 pr_err("%s: repiping tracing data padding", __func__);
3099 return -1;
3100 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003101 }
Tom Zanussi92155452010-04-01 23:59:21 -05003102
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003103 if (size_read + padding != size) {
3104 pr_err("%s: tracing data size mismatch", __func__);
3105 return -1;
3106 }
Tom Zanussi92155452010-04-01 23:59:21 -05003107
Namhyung Kim831394b2012-09-06 11:10:46 +09003108 perf_evlist__prepare_tracepoint_events(session->evlist,
3109 session->pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003110
Tom Zanussi92155452010-04-01 23:59:21 -05003111 return size_read + padding;
3112}
Tom Zanussic7929e42010-04-01 23:59:22 -05003113
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003114int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003115 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003116 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003117 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003118{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003119 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003120 size_t len;
3121 int err = 0;
3122
3123 if (!pos->hit)
3124 return err;
3125
3126 memset(&ev, 0, sizeof(ev));
3127
3128 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003129 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003130 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3131 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3132 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003133 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003134 ev.build_id.header.size = sizeof(ev.build_id) + len;
3135 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3136
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003137 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003138
3139 return err;
3140}
3141
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003142int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003143 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003144 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003145{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003146 __event_process_build_id(&event->build_id,
3147 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003148 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003149 return 0;
3150}
Stephane Eraniana1ac1d32010-06-17 11:39:01 +02003151
3152void disable_buildid_cache(void)
3153{
3154 no_buildid_cache = true;
3155}