blob: 26441d0e571bfce2bcd469dfdfe428175facb5ee [file] [log] [blame]
Arnaldo Carvalho de Meloa9072bc2011-10-26 12:41:38 -02001#include "util.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02004#include <unistd.h>
5#include <stdio.h>
6#include <stdlib.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +01007#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02008#include <linux/kernel.h>
Robert Richterb1e5a9b2011-12-07 10:02:57 +01009#include <linux/bitops.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020010#include <sys/utsname.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020011
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020012#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030013#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020014#include "header.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020015#include "../perf.h"
16#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020017#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010018#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010019#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020020#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020021#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020022#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090023#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020024#include "build-id.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020025
Stephane Eraniana1ac1d32010-06-17 11:39:01 +020026static bool no_buildid_cache = false;
27
Stephane Eranianfbe96f22011-09-30 15:40:40 +020028static u32 header_argc;
29static const char **header_argv;
30
Stephane Eranian73323f52012-02-02 13:54:44 +010031/*
32 * magic2 = "PERFILE2"
33 * must be a numerical value to let the endianness
34 * determine the memory layout. That way we are able
35 * to detect endianness when reading the perf.data file
36 * back.
37 *
38 * we check for legacy (PERFFILE) format.
39 */
40static const char *__perf_magic1 = "PERFFILE";
41static const u64 __perf_magic2 = 0x32454c4946524550ULL;
42static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020043
Stephane Eranian73323f52012-02-02 13:54:44 +010044#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020045
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020046struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020047 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020048 struct perf_file_section ids;
49};
50
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030051void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020052{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030053 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020054}
55
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030056void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020057{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030058 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020059}
60
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030061bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020062{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030063 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020064}
65
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020066static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020067{
68 while (size) {
69 int ret = write(fd, buf, size);
70
71 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -020072 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020073
74 size -= ret;
75 buf += ret;
76 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020077
78 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020079}
80
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -020081#define NAME_ALIGN 64
82
83static int write_padded(int fd, const void *bf, size_t count,
84 size_t count_aligned)
85{
86 static const char zero_buf[NAME_ALIGN];
87 int err = do_write(fd, bf, count);
88
89 if (!err)
90 err = do_write(fd, zero_buf, count_aligned - count);
91
92 return err;
93}
94
Stephane Eranianfbe96f22011-09-30 15:40:40 +020095static int do_write_string(int fd, const char *str)
96{
97 u32 len, olen;
98 int ret;
99
100 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300101 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200102
103 /* write len, incl. \0 */
104 ret = do_write(fd, &len, sizeof(len));
105 if (ret < 0)
106 return ret;
107
108 return write_padded(fd, str, olen, len);
109}
110
111static char *do_read_string(int fd, struct perf_header *ph)
112{
113 ssize_t sz, ret;
114 u32 len;
115 char *buf;
116
Namhyung Kim5323f602012-12-17 15:38:54 +0900117 sz = readn(fd, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200118 if (sz < (ssize_t)sizeof(len))
119 return NULL;
120
121 if (ph->needs_swap)
122 len = bswap_32(len);
123
124 buf = malloc(len);
125 if (!buf)
126 return NULL;
127
Namhyung Kim5323f602012-12-17 15:38:54 +0900128 ret = readn(fd, buf, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200129 if (ret == (ssize_t)len) {
130 /*
131 * strings are padded by zeroes
132 * thus the actual strlen of buf
133 * may be less than len
134 */
135 return buf;
136 }
137
138 free(buf);
139 return NULL;
140}
141
142int
143perf_header__set_cmdline(int argc, const char **argv)
144{
145 int i;
146
David Ahern56e6f602012-07-29 20:53:51 -0600147 /*
148 * If header_argv has already been set, do not override it.
149 * This allows a command to set the cmdline, parse args and
150 * then call another builtin function that implements a
151 * command -- e.g, cmd_kvm calling cmd_record.
152 */
153 if (header_argv)
154 return 0;
155
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200156 header_argc = (u32)argc;
157
158 /* do not include NULL termination */
159 header_argv = calloc(argc, sizeof(char *));
160 if (!header_argv)
161 return -ENOMEM;
162
163 /*
164 * must copy argv contents because it gets moved
165 * around during option parsing
166 */
167 for (i = 0; i < argc ; i++)
168 header_argv[i] = argv[i];
169
170 return 0;
171}
172
Robert Richter1b549502011-12-07 10:02:53 +0100173#define dsos__for_each_with_build_id(pos, head) \
174 list_for_each_entry(pos, head, node) \
175 if (!pos->has_build_id) \
176 continue; \
177 else
178
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200179static int write_buildid(char *name, size_t name_len, u8 *build_id,
180 pid_t pid, u16 misc, int fd)
181{
182 int err;
183 struct build_id_event b;
184 size_t len;
185
186 len = name_len + 1;
187 len = PERF_ALIGN(len, NAME_ALIGN);
188
189 memset(&b, 0, sizeof(b));
190 memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
191 b.pid = pid;
192 b.header.misc = misc;
193 b.header.size = sizeof(b) + len;
194
195 err = do_write(fd, &b, sizeof(b));
196 if (err < 0)
197 return err;
198
199 return write_padded(fd, name, name_len + 1, len);
200}
201
Robert Richter1b549502011-12-07 10:02:53 +0100202static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
203 u16 misc, int fd)
204{
205 struct dso *pos;
206
207 dsos__for_each_with_build_id(pos, head) {
208 int err;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200209 char *name;
210 size_t name_len;
Robert Richter1b549502011-12-07 10:02:53 +0100211
212 if (!pos->hit)
213 continue;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200214
215 if (is_vdso_map(pos->short_name)) {
216 name = (char *) VDSO__MAP_NAME;
217 name_len = sizeof(VDSO__MAP_NAME) + 1;
218 } else {
219 name = pos->long_name;
220 name_len = pos->long_name_len + 1;
221 }
222
223 err = write_buildid(name, name_len, pos->build_id,
224 pid, misc, fd);
225 if (err)
Robert Richter1b549502011-12-07 10:02:53 +0100226 return err;
227 }
228
229 return 0;
230}
231
232static int machine__write_buildid_table(struct machine *machine, int fd)
233{
234 int err;
235 u16 kmisc = PERF_RECORD_MISC_KERNEL,
236 umisc = PERF_RECORD_MISC_USER;
237
238 if (!machine__is_host(machine)) {
239 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
240 umisc = PERF_RECORD_MISC_GUEST_USER;
241 }
242
243 err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid,
244 kmisc, fd);
245 if (err == 0)
246 err = __dsos__write_buildid_table(&machine->user_dsos,
247 machine->pid, umisc, fd);
248 return err;
249}
250
251static int dsos__write_buildid_table(struct perf_header *header, int fd)
252{
253 struct perf_session *session = container_of(header,
254 struct perf_session, header);
255 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300256 int err = machine__write_buildid_table(&session->machines.host, fd);
Robert Richter1b549502011-12-07 10:02:53 +0100257
258 if (err)
259 return err;
260
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300261 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
Robert Richter1b549502011-12-07 10:02:53 +0100262 struct machine *pos = rb_entry(nd, struct machine, rb_node);
263 err = machine__write_buildid_table(pos, fd);
264 if (err)
265 break;
266 }
267 return err;
268}
269
270int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200271 const char *name, bool is_kallsyms, bool is_vdso)
Robert Richter1b549502011-12-07 10:02:53 +0100272{
273 const size_t size = PATH_MAX;
274 char *realname, *filename = zalloc(size),
275 *linkname = zalloc(size), *targetname;
276 int len, err = -1;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200277 bool slash = is_kallsyms || is_vdso;
Robert Richter1b549502011-12-07 10:02:53 +0100278
279 if (is_kallsyms) {
280 if (symbol_conf.kptr_restrict) {
281 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
Thomas Jaroschfdae6372013-01-25 11:21:39 +0100282 err = 0;
283 goto out_free;
Robert Richter1b549502011-12-07 10:02:53 +0100284 }
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200285 realname = (char *) name;
Robert Richter1b549502011-12-07 10:02:53 +0100286 } else
287 realname = realpath(name, NULL);
288
289 if (realname == NULL || filename == NULL || linkname == NULL)
290 goto out_free;
291
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300292 len = scnprintf(filename, size, "%s%s%s",
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200293 debugdir, slash ? "/" : "",
294 is_vdso ? VDSO__MAP_NAME : realname);
Robert Richter1b549502011-12-07 10:02:53 +0100295 if (mkdir_p(filename, 0755))
296 goto out_free;
297
Namhyung Kimafda0f92012-05-01 23:19:36 +0900298 snprintf(filename + len, size - len, "/%s", sbuild_id);
Robert Richter1b549502011-12-07 10:02:53 +0100299
300 if (access(filename, F_OK)) {
301 if (is_kallsyms) {
302 if (copyfile("/proc/kallsyms", filename))
303 goto out_free;
304 } else if (link(realname, filename) && copyfile(name, filename))
305 goto out_free;
306 }
307
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300308 len = scnprintf(linkname, size, "%s/.build-id/%.2s",
Robert Richter1b549502011-12-07 10:02:53 +0100309 debugdir, sbuild_id);
310
311 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
312 goto out_free;
313
314 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
315 targetname = filename + strlen(debugdir) - 5;
316 memcpy(targetname, "../..", 5);
317
318 if (symlink(targetname, linkname) == 0)
319 err = 0;
320out_free:
321 if (!is_kallsyms)
322 free(realname);
323 free(filename);
324 free(linkname);
325 return err;
326}
327
328static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
329 const char *name, const char *debugdir,
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200330 bool is_kallsyms, bool is_vdso)
Robert Richter1b549502011-12-07 10:02:53 +0100331{
332 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
333
334 build_id__sprintf(build_id, build_id_size, sbuild_id);
335
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200336 return build_id_cache__add_s(sbuild_id, debugdir, name,
337 is_kallsyms, is_vdso);
Robert Richter1b549502011-12-07 10:02:53 +0100338}
339
340int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
341{
342 const size_t size = PATH_MAX;
343 char *filename = zalloc(size),
344 *linkname = zalloc(size);
345 int err = -1;
346
347 if (filename == NULL || linkname == NULL)
348 goto out_free;
349
350 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
351 debugdir, sbuild_id, sbuild_id + 2);
352
353 if (access(linkname, F_OK))
354 goto out_free;
355
356 if (readlink(linkname, filename, size - 1) < 0)
357 goto out_free;
358
359 if (unlink(linkname))
360 goto out_free;
361
362 /*
363 * Since the link is relative, we must make it absolute:
364 */
365 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
366 debugdir, sbuild_id, filename);
367
368 if (unlink(linkname))
369 goto out_free;
370
371 err = 0;
372out_free:
373 free(filename);
374 free(linkname);
375 return err;
376}
377
378static int dso__cache_build_id(struct dso *dso, const char *debugdir)
379{
380 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200381 bool is_vdso = is_vdso_map(dso->short_name);
Robert Richter1b549502011-12-07 10:02:53 +0100382
383 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id),
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200384 dso->long_name, debugdir,
385 is_kallsyms, is_vdso);
Robert Richter1b549502011-12-07 10:02:53 +0100386}
387
388static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
389{
390 struct dso *pos;
391 int err = 0;
392
393 dsos__for_each_with_build_id(pos, head)
394 if (dso__cache_build_id(pos, debugdir))
395 err = -1;
396
397 return err;
398}
399
400static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
401{
402 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir);
403 ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir);
404 return ret;
405}
406
407static int perf_session__cache_build_ids(struct perf_session *session)
408{
409 struct rb_node *nd;
410 int ret;
411 char debugdir[PATH_MAX];
412
413 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
414
415 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
416 return -1;
417
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300418 ret = machine__cache_build_ids(&session->machines.host, debugdir);
Robert Richter1b549502011-12-07 10:02:53 +0100419
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300420 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
Robert Richter1b549502011-12-07 10:02:53 +0100421 struct machine *pos = rb_entry(nd, struct machine, rb_node);
422 ret |= machine__cache_build_ids(pos, debugdir);
423 }
424 return ret ? -1 : 0;
425}
426
427static bool machine__read_build_ids(struct machine *machine, bool with_hits)
428{
429 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
430 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
431 return ret;
432}
433
434static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
435{
436 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300437 bool ret = machine__read_build_ids(&session->machines.host, with_hits);
Robert Richter1b549502011-12-07 10:02:53 +0100438
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300439 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
Robert Richter1b549502011-12-07 10:02:53 +0100440 struct machine *pos = rb_entry(nd, struct machine, rb_node);
441 ret |= machine__read_build_ids(pos, with_hits);
442 }
443
444 return ret;
445}
446
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300447static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200448 struct perf_evlist *evlist)
449{
450 return read_tracing_data(fd, &evlist->entries);
451}
452
453
454static int write_build_id(int fd, struct perf_header *h,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300455 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200456{
457 struct perf_session *session;
458 int err;
459
460 session = container_of(h, struct perf_session, header);
461
Robert Richtere20960c2011-12-07 10:02:55 +0100462 if (!perf_session__read_build_ids(session, true))
463 return -1;
464
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200465 err = dsos__write_buildid_table(h, fd);
466 if (err < 0) {
467 pr_debug("failed to write buildid table\n");
468 return err;
469 }
470 if (!no_buildid_cache)
471 perf_session__cache_build_ids(session);
472
473 return 0;
474}
475
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300476static int write_hostname(int fd, struct perf_header *h __maybe_unused,
477 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200478{
479 struct utsname uts;
480 int ret;
481
482 ret = uname(&uts);
483 if (ret < 0)
484 return -1;
485
486 return do_write_string(fd, uts.nodename);
487}
488
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300489static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
490 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200491{
492 struct utsname uts;
493 int ret;
494
495 ret = uname(&uts);
496 if (ret < 0)
497 return -1;
498
499 return do_write_string(fd, uts.release);
500}
501
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300502static int write_arch(int fd, struct perf_header *h __maybe_unused,
503 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200504{
505 struct utsname uts;
506 int ret;
507
508 ret = uname(&uts);
509 if (ret < 0)
510 return -1;
511
512 return do_write_string(fd, uts.machine);
513}
514
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300515static int write_version(int fd, struct perf_header *h __maybe_unused,
516 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200517{
518 return do_write_string(fd, perf_version_string);
519}
520
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300521static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
522 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200523{
524#ifndef CPUINFO_PROC
525#define CPUINFO_PROC NULL
526#endif
527 FILE *file;
528 char *buf = NULL;
529 char *s, *p;
530 const char *search = CPUINFO_PROC;
531 size_t len = 0;
532 int ret = -1;
533
534 if (!search)
535 return -1;
536
537 file = fopen("/proc/cpuinfo", "r");
538 if (!file)
539 return -1;
540
541 while (getline(&buf, &len, file) > 0) {
542 ret = strncmp(buf, search, strlen(search));
543 if (!ret)
544 break;
545 }
546
547 if (ret)
548 goto done;
549
550 s = buf;
551
552 p = strchr(buf, ':');
553 if (p && *(p+1) == ' ' && *(p+2))
554 s = p + 2;
555 p = strchr(s, '\n');
556 if (p)
557 *p = '\0';
558
559 /* squash extra space characters (branding string) */
560 p = s;
561 while (*p) {
562 if (isspace(*p)) {
563 char *r = p + 1;
564 char *q = r;
565 *p = ' ';
566 while (*q && isspace(*q))
567 q++;
568 if (q != (p+1))
569 while ((*r++ = *q++));
570 }
571 p++;
572 }
573 ret = do_write_string(fd, s);
574done:
575 free(buf);
576 fclose(file);
577 return ret;
578}
579
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300580static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
581 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200582{
583 long nr;
584 u32 nrc, nra;
585 int ret;
586
587 nr = sysconf(_SC_NPROCESSORS_CONF);
588 if (nr < 0)
589 return -1;
590
591 nrc = (u32)(nr & UINT_MAX);
592
593 nr = sysconf(_SC_NPROCESSORS_ONLN);
594 if (nr < 0)
595 return -1;
596
597 nra = (u32)(nr & UINT_MAX);
598
599 ret = do_write(fd, &nrc, sizeof(nrc));
600 if (ret < 0)
601 return ret;
602
603 return do_write(fd, &nra, sizeof(nra));
604}
605
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300606static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200607 struct perf_evlist *evlist)
608{
Robert Richter6606f872012-08-16 21:10:19 +0200609 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900610 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200611 int ret;
612
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900613 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200614
615 /*
616 * write number of events
617 */
618 ret = do_write(fd, &nre, sizeof(nre));
619 if (ret < 0)
620 return ret;
621
622 /*
623 * size of perf_event_attr struct
624 */
Robert Richter6606f872012-08-16 21:10:19 +0200625 sz = (u32)sizeof(evsel->attr);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200626 ret = do_write(fd, &sz, sizeof(sz));
627 if (ret < 0)
628 return ret;
629
Robert Richter6606f872012-08-16 21:10:19 +0200630 list_for_each_entry(evsel, &evlist->entries, node) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200631
Robert Richter6606f872012-08-16 21:10:19 +0200632 ret = do_write(fd, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200633 if (ret < 0)
634 return ret;
635 /*
636 * write number of unique id per event
637 * there is one id per instance of an event
638 *
639 * copy into an nri to be independent of the
640 * type of ids,
641 */
Robert Richter6606f872012-08-16 21:10:19 +0200642 nri = evsel->ids;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200643 ret = do_write(fd, &nri, sizeof(nri));
644 if (ret < 0)
645 return ret;
646
647 /*
648 * write event string as passed on cmdline
649 */
Robert Richter6606f872012-08-16 21:10:19 +0200650 ret = do_write_string(fd, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200651 if (ret < 0)
652 return ret;
653 /*
654 * write unique ids for this event
655 */
Robert Richter6606f872012-08-16 21:10:19 +0200656 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200657 if (ret < 0)
658 return ret;
659 }
660 return 0;
661}
662
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300663static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
664 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200665{
666 char buf[MAXPATHLEN];
667 char proc[32];
668 u32 i, n;
669 int ret;
670
671 /*
672 * actual atual path to perf binary
673 */
674 sprintf(proc, "/proc/%d/exe", getpid());
675 ret = readlink(proc, buf, sizeof(buf));
676 if (ret <= 0)
677 return -1;
678
679 /* readlink() does not add null termination */
680 buf[ret] = '\0';
681
682 /* account for binary path */
683 n = header_argc + 1;
684
685 ret = do_write(fd, &n, sizeof(n));
686 if (ret < 0)
687 return ret;
688
689 ret = do_write_string(fd, buf);
690 if (ret < 0)
691 return ret;
692
693 for (i = 0 ; i < header_argc; i++) {
694 ret = do_write_string(fd, header_argv[i]);
695 if (ret < 0)
696 return ret;
697 }
698 return 0;
699}
700
701#define CORE_SIB_FMT \
702 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
703#define THRD_SIB_FMT \
704 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
705
706struct cpu_topo {
707 u32 core_sib;
708 u32 thread_sib;
709 char **core_siblings;
710 char **thread_siblings;
711};
712
713static int build_cpu_topo(struct cpu_topo *tp, int cpu)
714{
715 FILE *fp;
716 char filename[MAXPATHLEN];
717 char *buf = NULL, *p;
718 size_t len = 0;
Stephane Eranianc5885742013-08-14 12:04:26 +0200719 ssize_t sret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200720 u32 i = 0;
721 int ret = -1;
722
723 sprintf(filename, CORE_SIB_FMT, cpu);
724 fp = fopen(filename, "r");
725 if (!fp)
Stephane Eranianc5885742013-08-14 12:04:26 +0200726 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200727
Stephane Eranianc5885742013-08-14 12:04:26 +0200728 sret = getline(&buf, &len, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200729 fclose(fp);
Stephane Eranianc5885742013-08-14 12:04:26 +0200730 if (sret <= 0)
731 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200732
733 p = strchr(buf, '\n');
734 if (p)
735 *p = '\0';
736
737 for (i = 0; i < tp->core_sib; i++) {
738 if (!strcmp(buf, tp->core_siblings[i]))
739 break;
740 }
741 if (i == tp->core_sib) {
742 tp->core_siblings[i] = buf;
743 tp->core_sib++;
744 buf = NULL;
745 len = 0;
746 }
Stephane Eranianc5885742013-08-14 12:04:26 +0200747 ret = 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200748
Stephane Eranianc5885742013-08-14 12:04:26 +0200749try_threads:
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200750 sprintf(filename, THRD_SIB_FMT, cpu);
751 fp = fopen(filename, "r");
752 if (!fp)
753 goto done;
754
755 if (getline(&buf, &len, fp) <= 0)
756 goto done;
757
758 p = strchr(buf, '\n');
759 if (p)
760 *p = '\0';
761
762 for (i = 0; i < tp->thread_sib; i++) {
763 if (!strcmp(buf, tp->thread_siblings[i]))
764 break;
765 }
766 if (i == tp->thread_sib) {
767 tp->thread_siblings[i] = buf;
768 tp->thread_sib++;
769 buf = NULL;
770 }
771 ret = 0;
772done:
773 if(fp)
774 fclose(fp);
775 free(buf);
776 return ret;
777}
778
779static void free_cpu_topo(struct cpu_topo *tp)
780{
781 u32 i;
782
783 if (!tp)
784 return;
785
786 for (i = 0 ; i < tp->core_sib; i++)
787 free(tp->core_siblings[i]);
788
789 for (i = 0 ; i < tp->thread_sib; i++)
790 free(tp->thread_siblings[i]);
791
792 free(tp);
793}
794
795static struct cpu_topo *build_cpu_topology(void)
796{
797 struct cpu_topo *tp;
798 void *addr;
799 u32 nr, i;
800 size_t sz;
801 long ncpus;
802 int ret = -1;
803
804 ncpus = sysconf(_SC_NPROCESSORS_CONF);
805 if (ncpus < 0)
806 return NULL;
807
808 nr = (u32)(ncpus & UINT_MAX);
809
810 sz = nr * sizeof(char *);
811
812 addr = calloc(1, sizeof(*tp) + 2 * sz);
813 if (!addr)
814 return NULL;
815
816 tp = addr;
817
818 addr += sizeof(*tp);
819 tp->core_siblings = addr;
820 addr += sz;
821 tp->thread_siblings = addr;
822
823 for (i = 0; i < nr; i++) {
824 ret = build_cpu_topo(tp, i);
825 if (ret < 0)
826 break;
827 }
828 if (ret) {
829 free_cpu_topo(tp);
830 tp = NULL;
831 }
832 return tp;
833}
834
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300835static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
836 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200837{
838 struct cpu_topo *tp;
839 u32 i;
840 int ret;
841
842 tp = build_cpu_topology();
843 if (!tp)
844 return -1;
845
846 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
847 if (ret < 0)
848 goto done;
849
850 for (i = 0; i < tp->core_sib; i++) {
851 ret = do_write_string(fd, tp->core_siblings[i]);
852 if (ret < 0)
853 goto done;
854 }
855 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
856 if (ret < 0)
857 goto done;
858
859 for (i = 0; i < tp->thread_sib; i++) {
860 ret = do_write_string(fd, tp->thread_siblings[i]);
861 if (ret < 0)
862 break;
863 }
864done:
865 free_cpu_topo(tp);
866 return ret;
867}
868
869
870
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300871static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
872 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200873{
874 char *buf = NULL;
875 FILE *fp;
876 size_t len = 0;
877 int ret = -1, n;
878 uint64_t mem;
879
880 fp = fopen("/proc/meminfo", "r");
881 if (!fp)
882 return -1;
883
884 while (getline(&buf, &len, fp) > 0) {
885 ret = strncmp(buf, "MemTotal:", 9);
886 if (!ret)
887 break;
888 }
889 if (!ret) {
890 n = sscanf(buf, "%*s %"PRIu64, &mem);
891 if (n == 1)
892 ret = do_write(fd, &mem, sizeof(mem));
893 }
894 free(buf);
895 fclose(fp);
896 return ret;
897}
898
899static int write_topo_node(int fd, int node)
900{
901 char str[MAXPATHLEN];
902 char field[32];
903 char *buf = NULL, *p;
904 size_t len = 0;
905 FILE *fp;
906 u64 mem_total, mem_free, mem;
907 int ret = -1;
908
909 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
910 fp = fopen(str, "r");
911 if (!fp)
912 return -1;
913
914 while (getline(&buf, &len, fp) > 0) {
915 /* skip over invalid lines */
916 if (!strchr(buf, ':'))
917 continue;
918 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
919 goto done;
920 if (!strcmp(field, "MemTotal:"))
921 mem_total = mem;
922 if (!strcmp(field, "MemFree:"))
923 mem_free = mem;
924 }
925
926 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100927 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200928
929 ret = do_write(fd, &mem_total, sizeof(u64));
930 if (ret)
931 goto done;
932
933 ret = do_write(fd, &mem_free, sizeof(u64));
934 if (ret)
935 goto done;
936
937 ret = -1;
938 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
939
940 fp = fopen(str, "r");
941 if (!fp)
942 goto done;
943
944 if (getline(&buf, &len, fp) <= 0)
945 goto done;
946
947 p = strchr(buf, '\n');
948 if (p)
949 *p = '\0';
950
951 ret = do_write_string(fd, buf);
952done:
953 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100954 if (fp)
955 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200956 return ret;
957}
958
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300959static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
960 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200961{
962 char *buf = NULL;
963 size_t len = 0;
964 FILE *fp;
965 struct cpu_map *node_map = NULL;
966 char *c;
967 u32 nr, i, j;
968 int ret = -1;
969
970 fp = fopen("/sys/devices/system/node/online", "r");
971 if (!fp)
972 return -1;
973
974 if (getline(&buf, &len, fp) <= 0)
975 goto done;
976
977 c = strchr(buf, '\n');
978 if (c)
979 *c = '\0';
980
981 node_map = cpu_map__new(buf);
982 if (!node_map)
983 goto done;
984
985 nr = (u32)node_map->nr;
986
987 ret = do_write(fd, &nr, sizeof(nr));
988 if (ret < 0)
989 goto done;
990
991 for (i = 0; i < nr; i++) {
992 j = (u32)node_map->map[i];
993 ret = do_write(fd, &j, sizeof(j));
994 if (ret < 0)
995 break;
996
997 ret = write_topo_node(fd, i);
998 if (ret < 0)
999 break;
1000 }
1001done:
1002 free(buf);
1003 fclose(fp);
1004 free(node_map);
1005 return ret;
1006}
1007
1008/*
Robert Richter50a96672012-08-16 21:10:24 +02001009 * File format:
1010 *
1011 * struct pmu_mappings {
1012 * u32 pmu_num;
1013 * struct pmu_map {
1014 * u32 type;
1015 * char name[];
1016 * }[pmu_num];
1017 * };
1018 */
1019
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001020static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
1021 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +02001022{
1023 struct perf_pmu *pmu = NULL;
1024 off_t offset = lseek(fd, 0, SEEK_CUR);
1025 __u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +09001026 int ret;
Robert Richter50a96672012-08-16 21:10:24 +02001027
1028 /* write real pmu_num later */
Namhyung Kim5323f602012-12-17 15:38:54 +09001029 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
1030 if (ret < 0)
1031 return ret;
Robert Richter50a96672012-08-16 21:10:24 +02001032
1033 while ((pmu = perf_pmu__scan(pmu))) {
1034 if (!pmu->name)
1035 continue;
1036 pmu_num++;
Namhyung Kim5323f602012-12-17 15:38:54 +09001037
1038 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
1039 if (ret < 0)
1040 return ret;
1041
1042 ret = do_write_string(fd, pmu->name);
1043 if (ret < 0)
1044 return ret;
Robert Richter50a96672012-08-16 21:10:24 +02001045 }
1046
1047 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1048 /* discard all */
1049 lseek(fd, offset, SEEK_SET);
1050 return -1;
1051 }
1052
1053 return 0;
1054}
1055
1056/*
Namhyung Kima8bb5592013-01-22 18:09:31 +09001057 * File format:
1058 *
1059 * struct group_descs {
1060 * u32 nr_groups;
1061 * struct group_desc {
1062 * char name[];
1063 * u32 leader_idx;
1064 * u32 nr_members;
1065 * }[nr_groups];
1066 * };
1067 */
1068static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
1069 struct perf_evlist *evlist)
1070{
1071 u32 nr_groups = evlist->nr_groups;
1072 struct perf_evsel *evsel;
1073 int ret;
1074
1075 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
1076 if (ret < 0)
1077 return ret;
1078
1079 list_for_each_entry(evsel, &evlist->entries, node) {
1080 if (perf_evsel__is_group_leader(evsel) &&
1081 evsel->nr_members > 1) {
1082 const char *name = evsel->group_name ?: "{anon_group}";
1083 u32 leader_idx = evsel->idx;
1084 u32 nr_members = evsel->nr_members;
1085
1086 ret = do_write_string(fd, name);
1087 if (ret < 0)
1088 return ret;
1089
1090 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
1091 if (ret < 0)
1092 return ret;
1093
1094 ret = do_write(fd, &nr_members, sizeof(nr_members));
1095 if (ret < 0)
1096 return ret;
1097 }
1098 }
1099 return 0;
1100}
1101
1102/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001103 * default get_cpuid(): nothing gets recorded
1104 * actual implementation must be in arch/$(ARCH)/util/header.c
1105 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001106int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
1107 size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001108{
1109 return -1;
1110}
1111
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001112static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
1113 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001114{
1115 char buffer[64];
1116 int ret;
1117
1118 ret = get_cpuid(buffer, sizeof(buffer));
1119 if (!ret)
1120 goto write_it;
1121
1122 return -1;
1123write_it:
1124 return do_write_string(fd, buffer);
1125}
1126
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001127static int write_branch_stack(int fd __maybe_unused,
1128 struct perf_header *h __maybe_unused,
1129 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +01001130{
1131 return 0;
1132}
1133
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001134static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1135 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001136{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001137 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001138}
1139
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001140static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1141 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001142{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001143 fprintf(fp, "# os release : %s\n", ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001144}
1145
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001146static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001147{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001148 fprintf(fp, "# arch : %s\n", ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001149}
1150
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001151static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1152 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001153{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001154 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001155}
1156
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001157static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1158 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001159{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001160 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1161 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001162}
1163
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001164static void print_version(struct perf_header *ph, int fd __maybe_unused,
1165 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001166{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001167 fprintf(fp, "# perf version : %s\n", ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001168}
1169
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001170static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1171 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001172{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001173 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001174 char *str;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001175
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001176 nr = ph->env.nr_cmdline;
1177 str = ph->env.cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001178
1179 fprintf(fp, "# cmdline : ");
1180
1181 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001182 fprintf(fp, "%s ", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001183 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001184 }
1185 fputc('\n', fp);
1186}
1187
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001188static void print_cpu_topology(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 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001192 char *str;
1193
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001194 nr = ph->env.nr_sibling_cores;
1195 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001196
1197 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001198 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001199 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001200 }
1201
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001202 nr = ph->env.nr_sibling_threads;
1203 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001204
1205 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001206 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001207 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001208 }
1209}
1210
Robert Richter4e1b9c62012-08-16 21:10:22 +02001211static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001212{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001213 struct perf_evsel *evsel;
1214
1215 if (!events)
1216 return;
1217
1218 for (evsel = events; evsel->attr.size; evsel++) {
1219 if (evsel->name)
1220 free(evsel->name);
1221 if (evsel->id)
1222 free(evsel->id);
1223 }
1224
1225 free(events);
1226}
1227
1228static struct perf_evsel *
1229read_event_desc(struct perf_header *ph, int fd)
1230{
1231 struct perf_evsel *evsel, *events = NULL;
1232 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001233 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001234 u32 nre, sz, nr, i, j;
1235 ssize_t ret;
1236 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001237
1238 /* number of events */
Namhyung Kim5323f602012-12-17 15:38:54 +09001239 ret = readn(fd, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001240 if (ret != (ssize_t)sizeof(nre))
1241 goto error;
1242
1243 if (ph->needs_swap)
1244 nre = bswap_32(nre);
1245
Namhyung Kim5323f602012-12-17 15:38:54 +09001246 ret = readn(fd, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001247 if (ret != (ssize_t)sizeof(sz))
1248 goto error;
1249
1250 if (ph->needs_swap)
1251 sz = bswap_32(sz);
1252
Stephane Eranian62db9062012-02-09 23:21:07 +01001253 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001254 buf = malloc(sz);
1255 if (!buf)
1256 goto error;
1257
Robert Richter4e1b9c62012-08-16 21:10:22 +02001258 /* the last event terminates with evsel->attr.size == 0: */
1259 events = calloc(nre + 1, sizeof(*events));
1260 if (!events)
1261 goto error;
1262
1263 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001264 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001265 msz = sz;
1266
Robert Richter4e1b9c62012-08-16 21:10:22 +02001267 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1268 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001269
Stephane Eranian62db9062012-02-09 23:21:07 +01001270 /*
1271 * must read entire on-file attr struct to
1272 * sync up with layout.
1273 */
Namhyung Kim5323f602012-12-17 15:38:54 +09001274 ret = readn(fd, buf, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001275 if (ret != (ssize_t)sz)
1276 goto error;
1277
1278 if (ph->needs_swap)
1279 perf_event__attr_swap(buf);
1280
Robert Richter4e1b9c62012-08-16 21:10:22 +02001281 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001282
Namhyung Kim5323f602012-12-17 15:38:54 +09001283 ret = readn(fd, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001284 if (ret != (ssize_t)sizeof(nr))
1285 goto error;
1286
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001287 if (ph->needs_swap) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001288 nr = bswap_32(nr);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001289 evsel->needs_swap = true;
1290 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001291
Robert Richter4e1b9c62012-08-16 21:10:22 +02001292 evsel->name = do_read_string(fd, ph);
1293
1294 if (!nr)
1295 continue;
1296
1297 id = calloc(nr, sizeof(*id));
1298 if (!id)
1299 goto error;
1300 evsel->ids = nr;
1301 evsel->id = id;
1302
1303 for (j = 0 ; j < nr; j++) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001304 ret = readn(fd, id, sizeof(*id));
Robert Richter4e1b9c62012-08-16 21:10:22 +02001305 if (ret != (ssize_t)sizeof(*id))
1306 goto error;
1307 if (ph->needs_swap)
1308 *id = bswap_64(*id);
1309 id++;
1310 }
1311 }
1312out:
1313 if (buf)
1314 free(buf);
1315 return events;
1316error:
1317 if (events)
1318 free_event_desc(events);
1319 events = NULL;
1320 goto out;
1321}
1322
1323static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1324{
1325 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1326 u32 j;
1327 u64 *id;
1328
1329 if (!events) {
1330 fprintf(fp, "# event desc: not available or unable to read\n");
1331 return;
1332 }
1333
1334 for (evsel = events; evsel->attr.size; evsel++) {
1335 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001336
1337 fprintf(fp, "type = %d, config = 0x%"PRIx64
1338 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
Robert Richter4e1b9c62012-08-16 21:10:22 +02001339 evsel->attr.type,
1340 (u64)evsel->attr.config,
1341 (u64)evsel->attr.config1,
1342 (u64)evsel->attr.config2);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001343
1344 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001345 evsel->attr.exclude_user,
1346 evsel->attr.exclude_kernel);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001347
David Ahern78b961f2012-07-20 17:25:52 -06001348 fprintf(fp, ", excl_host = %d, excl_guest = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001349 evsel->attr.exclude_host,
1350 evsel->attr.exclude_guest);
David Ahern78b961f2012-07-20 17:25:52 -06001351
Robert Richter4e1b9c62012-08-16 21:10:22 +02001352 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
David Ahern78b961f2012-07-20 17:25:52 -06001353
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001354 fprintf(fp, ", attr_mmap2 = %d", evsel->attr.mmap2);
1355 fprintf(fp, ", attr_mmap = %d", evsel->attr.mmap);
1356 fprintf(fp, ", attr_mmap_data = %d", evsel->attr.mmap_data);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001357 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001358 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001359 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1360 if (j)
1361 fputc(',', fp);
1362 fprintf(fp, " %"PRIu64, *id);
1363 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001364 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001365 }
1366
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001367 fputc('\n', fp);
1368 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001369
1370 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001371}
1372
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001373static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001374 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001375{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001376 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001377}
1378
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001379static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001380 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001381{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001382 u32 nr, c, i;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001383 char *str, *tmp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001384 uint64_t mem_total, mem_free;
1385
1386 /* nr nodes */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001387 nr = ph->env.nr_numa_nodes;
1388 str = ph->env.numa_nodes;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001389
1390 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001391 /* node number */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001392 c = strtoul(str, &tmp, 0);
1393 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001394 goto error;
1395
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001396 str = tmp + 1;
1397 mem_total = strtoull(str, &tmp, 0);
1398 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001399 goto error;
1400
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001401 str = tmp + 1;
1402 mem_free = strtoull(str, &tmp, 0);
1403 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001404 goto error;
1405
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001406 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1407 " free = %"PRIu64" kB\n",
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001408 c, mem_total, mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001409
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001410 str = tmp + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001411 fprintf(fp, "# node%u cpu list : %s\n", c, str);
Namhyung Kim12344712012-10-23 22:44:49 +09001412
1413 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001414 }
1415 return;
1416error:
1417 fprintf(fp, "# numa topology : not available\n");
1418}
1419
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001420static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001421{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001422 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001423}
1424
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001425static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001426 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001427{
1428 fprintf(fp, "# contains samples with branch stack\n");
1429}
1430
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001431static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1432 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001433{
1434 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001435 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001436 u32 pmu_num;
1437 u32 type;
1438
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001439 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001440 if (!pmu_num) {
1441 fprintf(fp, "# pmu mappings: not available\n");
1442 return;
1443 }
1444
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001445 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001446
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001447 while (pmu_num) {
1448 type = strtoul(str, &tmp, 0);
1449 if (*tmp != ':')
1450 goto error;
1451
1452 str = tmp + 1;
1453 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1454
Robert Richter50a96672012-08-16 21:10:24 +02001455 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001456 str += strlen(str) + 1;
1457 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001458 }
1459
1460 fprintf(fp, "\n");
1461
1462 if (!pmu_num)
1463 return;
1464error:
1465 fprintf(fp, "# pmu mappings: unable to read\n");
1466}
1467
Namhyung Kima8bb5592013-01-22 18:09:31 +09001468static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1469 FILE *fp)
1470{
1471 struct perf_session *session;
1472 struct perf_evsel *evsel;
1473 u32 nr = 0;
1474
1475 session = container_of(ph, struct perf_session, header);
1476
1477 list_for_each_entry(evsel, &session->evlist->entries, node) {
1478 if (perf_evsel__is_group_leader(evsel) &&
1479 evsel->nr_members > 1) {
1480 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1481 perf_evsel__name(evsel));
1482
1483 nr = evsel->nr_members - 1;
1484 } else if (nr) {
1485 fprintf(fp, ",%s", perf_evsel__name(evsel));
1486
1487 if (--nr == 0)
1488 fprintf(fp, "}\n");
1489 }
1490 }
1491}
1492
Robert Richter08d95bd2012-02-10 15:41:55 +01001493static int __event_process_build_id(struct build_id_event *bev,
1494 char *filename,
1495 struct perf_session *session)
1496{
1497 int err = -1;
1498 struct list_head *head;
1499 struct machine *machine;
1500 u16 misc;
1501 struct dso *dso;
1502 enum dso_kernel_type dso_type;
1503
1504 machine = perf_session__findnew_machine(session, bev->pid);
1505 if (!machine)
1506 goto out;
1507
1508 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1509
1510 switch (misc) {
1511 case PERF_RECORD_MISC_KERNEL:
1512 dso_type = DSO_TYPE_KERNEL;
1513 head = &machine->kernel_dsos;
1514 break;
1515 case PERF_RECORD_MISC_GUEST_KERNEL:
1516 dso_type = DSO_TYPE_GUEST_KERNEL;
1517 head = &machine->kernel_dsos;
1518 break;
1519 case PERF_RECORD_MISC_USER:
1520 case PERF_RECORD_MISC_GUEST_USER:
1521 dso_type = DSO_TYPE_USER;
1522 head = &machine->user_dsos;
1523 break;
1524 default:
1525 goto out;
1526 }
1527
1528 dso = __dsos__findnew(head, filename);
1529 if (dso != NULL) {
1530 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1531
1532 dso__set_build_id(dso, &bev->build_id);
1533
1534 if (filename[0] == '[')
1535 dso->kernel = dso_type;
1536
1537 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1538 sbuild_id);
1539 pr_debug("build id event received for %s: %s\n",
1540 dso->long_name, sbuild_id);
1541 }
1542
1543 err = 0;
1544out:
1545 return err;
1546}
1547
1548static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1549 int input, u64 offset, u64 size)
1550{
1551 struct perf_session *session = container_of(header, struct perf_session, header);
1552 struct {
1553 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001554 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001555 char filename[0];
1556 } old_bev;
1557 struct build_id_event bev;
1558 char filename[PATH_MAX];
1559 u64 limit = offset + size;
1560
1561 while (offset < limit) {
1562 ssize_t len;
1563
Namhyung Kim5323f602012-12-17 15:38:54 +09001564 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001565 return -1;
1566
1567 if (header->needs_swap)
1568 perf_event_header__bswap(&old_bev.header);
1569
1570 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001571 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001572 return -1;
1573
1574 bev.header = old_bev.header;
1575
1576 /*
1577 * As the pid is the missing value, we need to fill
1578 * it properly. The header.misc value give us nice hint.
1579 */
1580 bev.pid = HOST_KERNEL_ID;
1581 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1582 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1583 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1584
1585 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1586 __event_process_build_id(&bev, filename, session);
1587
1588 offset += bev.header.size;
1589 }
1590
1591 return 0;
1592}
1593
1594static int perf_header__read_build_ids(struct perf_header *header,
1595 int input, u64 offset, u64 size)
1596{
1597 struct perf_session *session = container_of(header, struct perf_session, header);
1598 struct build_id_event bev;
1599 char filename[PATH_MAX];
1600 u64 limit = offset + size, orig_offset = offset;
1601 int err = -1;
1602
1603 while (offset < limit) {
1604 ssize_t len;
1605
Namhyung Kim5323f602012-12-17 15:38:54 +09001606 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001607 goto out;
1608
1609 if (header->needs_swap)
1610 perf_event_header__bswap(&bev.header);
1611
1612 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001613 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001614 goto out;
1615 /*
1616 * The a1645ce1 changeset:
1617 *
1618 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1619 *
1620 * Added a field to struct build_id_event that broke the file
1621 * format.
1622 *
1623 * Since the kernel build-id is the first entry, process the
1624 * table using the old format if the well known
1625 * '[kernel.kallsyms]' string for the kernel build-id has the
1626 * first 4 characters chopped off (where the pid_t sits).
1627 */
1628 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1629 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1630 return -1;
1631 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1632 }
1633
1634 __event_process_build_id(&bev, filename, session);
1635
1636 offset += bev.header.size;
1637 }
1638 err = 0;
1639out:
1640 return err;
1641}
1642
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001643static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1644 struct perf_header *ph __maybe_unused,
1645 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001646{
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001647 ssize_t ret = trace_report(fd, data, false);
1648 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001649}
1650
1651static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001652 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001653 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001654{
1655 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1656 pr_debug("Failed to read buildids, continuing...\n");
1657 return 0;
1658}
1659
Namhyung Kima1ae5652012-09-24 17:14:59 +09001660static int process_hostname(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001661 struct perf_header *ph, int fd,
1662 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001663{
1664 ph->env.hostname = do_read_string(fd, ph);
1665 return ph->env.hostname ? 0 : -ENOMEM;
1666}
1667
1668static int process_osrelease(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001669 struct perf_header *ph, int fd,
1670 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001671{
1672 ph->env.os_release = do_read_string(fd, ph);
1673 return ph->env.os_release ? 0 : -ENOMEM;
1674}
1675
1676static int process_version(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001677 struct perf_header *ph, int fd,
1678 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001679{
1680 ph->env.version = do_read_string(fd, ph);
1681 return ph->env.version ? 0 : -ENOMEM;
1682}
1683
1684static int process_arch(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001685 struct perf_header *ph, int fd,
1686 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001687{
1688 ph->env.arch = do_read_string(fd, ph);
1689 return ph->env.arch ? 0 : -ENOMEM;
1690}
1691
1692static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001693 struct perf_header *ph, int fd,
1694 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001695{
1696 size_t ret;
1697 u32 nr;
1698
Namhyung Kim5323f602012-12-17 15:38:54 +09001699 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001700 if (ret != sizeof(nr))
1701 return -1;
1702
1703 if (ph->needs_swap)
1704 nr = bswap_32(nr);
1705
1706 ph->env.nr_cpus_online = nr;
1707
Namhyung Kim5323f602012-12-17 15:38:54 +09001708 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001709 if (ret != sizeof(nr))
1710 return -1;
1711
1712 if (ph->needs_swap)
1713 nr = bswap_32(nr);
1714
1715 ph->env.nr_cpus_avail = nr;
1716 return 0;
1717}
1718
1719static int process_cpudesc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001720 struct perf_header *ph, int fd,
1721 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001722{
1723 ph->env.cpu_desc = do_read_string(fd, ph);
1724 return ph->env.cpu_desc ? 0 : -ENOMEM;
1725}
1726
1727static int process_cpuid(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001728 struct perf_header *ph, int fd,
1729 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001730{
1731 ph->env.cpuid = do_read_string(fd, ph);
1732 return ph->env.cpuid ? 0 : -ENOMEM;
1733}
1734
1735static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001736 struct perf_header *ph, int fd,
1737 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001738{
1739 uint64_t mem;
1740 size_t ret;
1741
Namhyung Kim5323f602012-12-17 15:38:54 +09001742 ret = readn(fd, &mem, sizeof(mem));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001743 if (ret != sizeof(mem))
1744 return -1;
1745
1746 if (ph->needs_swap)
1747 mem = bswap_64(mem);
1748
1749 ph->env.total_mem = mem;
1750 return 0;
1751}
1752
Robert Richter7c2f7af2012-08-16 21:10:23 +02001753static struct perf_evsel *
1754perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1755{
1756 struct perf_evsel *evsel;
1757
1758 list_for_each_entry(evsel, &evlist->entries, node) {
1759 if (evsel->idx == idx)
1760 return evsel;
1761 }
1762
1763 return NULL;
1764}
1765
1766static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001767perf_evlist__set_event_name(struct perf_evlist *evlist,
1768 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001769{
1770 struct perf_evsel *evsel;
1771
1772 if (!event->name)
1773 return;
1774
1775 evsel = perf_evlist__find_by_index(evlist, event->idx);
1776 if (!evsel)
1777 return;
1778
1779 if (evsel->name)
1780 return;
1781
1782 evsel->name = strdup(event->name);
1783}
1784
1785static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001786process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001787 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001788 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001789{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001790 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001791 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1792
1793 if (!events)
1794 return 0;
1795
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001796 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001797 for (evsel = events; evsel->attr.size; evsel++)
1798 perf_evlist__set_event_name(session->evlist, evsel);
1799
1800 free_event_desc(events);
1801
1802 return 0;
1803}
1804
Namhyung Kima1ae5652012-09-24 17:14:59 +09001805static int process_cmdline(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001806 struct perf_header *ph, int fd,
1807 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001808{
1809 size_t ret;
1810 char *str;
1811 u32 nr, i;
1812 struct strbuf sb;
1813
Namhyung Kim5323f602012-12-17 15:38:54 +09001814 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001815 if (ret != sizeof(nr))
1816 return -1;
1817
1818 if (ph->needs_swap)
1819 nr = bswap_32(nr);
1820
1821 ph->env.nr_cmdline = nr;
1822 strbuf_init(&sb, 128);
1823
1824 for (i = 0; i < nr; i++) {
1825 str = do_read_string(fd, ph);
1826 if (!str)
1827 goto error;
1828
1829 /* include a NULL character at the end */
1830 strbuf_add(&sb, str, strlen(str) + 1);
1831 free(str);
1832 }
1833 ph->env.cmdline = strbuf_detach(&sb, NULL);
1834 return 0;
1835
1836error:
1837 strbuf_release(&sb);
1838 return -1;
1839}
1840
1841static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001842 struct perf_header *ph, int fd,
1843 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001844{
1845 size_t ret;
1846 u32 nr, i;
1847 char *str;
1848 struct strbuf sb;
1849
Namhyung Kim5323f602012-12-17 15:38:54 +09001850 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001851 if (ret != sizeof(nr))
1852 return -1;
1853
1854 if (ph->needs_swap)
1855 nr = bswap_32(nr);
1856
1857 ph->env.nr_sibling_cores = nr;
1858 strbuf_init(&sb, 128);
1859
1860 for (i = 0; i < nr; i++) {
1861 str = do_read_string(fd, ph);
1862 if (!str)
1863 goto error;
1864
1865 /* include a NULL character at the end */
1866 strbuf_add(&sb, str, strlen(str) + 1);
1867 free(str);
1868 }
1869 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1870
Namhyung Kim5323f602012-12-17 15:38:54 +09001871 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001872 if (ret != sizeof(nr))
1873 return -1;
1874
1875 if (ph->needs_swap)
1876 nr = bswap_32(nr);
1877
1878 ph->env.nr_sibling_threads = nr;
1879
1880 for (i = 0; i < nr; i++) {
1881 str = do_read_string(fd, ph);
1882 if (!str)
1883 goto error;
1884
1885 /* include a NULL character at the end */
1886 strbuf_add(&sb, str, strlen(str) + 1);
1887 free(str);
1888 }
1889 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1890 return 0;
1891
1892error:
1893 strbuf_release(&sb);
1894 return -1;
1895}
1896
1897static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001898 struct perf_header *ph, int fd,
1899 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001900{
1901 size_t ret;
1902 u32 nr, node, i;
1903 char *str;
1904 uint64_t mem_total, mem_free;
1905 struct strbuf sb;
1906
1907 /* nr nodes */
Namhyung Kim5323f602012-12-17 15:38:54 +09001908 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001909 if (ret != sizeof(nr))
1910 goto error;
1911
1912 if (ph->needs_swap)
1913 nr = bswap_32(nr);
1914
1915 ph->env.nr_numa_nodes = nr;
1916 strbuf_init(&sb, 256);
1917
1918 for (i = 0; i < nr; i++) {
1919 /* node number */
Namhyung Kim5323f602012-12-17 15:38:54 +09001920 ret = readn(fd, &node, sizeof(node));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001921 if (ret != sizeof(node))
1922 goto error;
1923
Namhyung Kim5323f602012-12-17 15:38:54 +09001924 ret = readn(fd, &mem_total, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001925 if (ret != sizeof(u64))
1926 goto error;
1927
Namhyung Kim5323f602012-12-17 15:38:54 +09001928 ret = readn(fd, &mem_free, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001929 if (ret != sizeof(u64))
1930 goto error;
1931
1932 if (ph->needs_swap) {
1933 node = bswap_32(node);
1934 mem_total = bswap_64(mem_total);
1935 mem_free = bswap_64(mem_free);
1936 }
1937
1938 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1939 node, mem_total, mem_free);
1940
1941 str = do_read_string(fd, ph);
1942 if (!str)
1943 goto error;
1944
1945 /* include a NULL character at the end */
1946 strbuf_add(&sb, str, strlen(str) + 1);
1947 free(str);
1948 }
1949 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1950 return 0;
1951
1952error:
1953 strbuf_release(&sb);
1954 return -1;
1955}
1956
1957static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001958 struct perf_header *ph, int fd,
1959 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001960{
1961 size_t ret;
1962 char *name;
1963 u32 pmu_num;
1964 u32 type;
1965 struct strbuf sb;
1966
Namhyung Kim5323f602012-12-17 15:38:54 +09001967 ret = readn(fd, &pmu_num, sizeof(pmu_num));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001968 if (ret != sizeof(pmu_num))
1969 return -1;
1970
1971 if (ph->needs_swap)
1972 pmu_num = bswap_32(pmu_num);
1973
1974 if (!pmu_num) {
1975 pr_debug("pmu mappings not available\n");
1976 return 0;
1977 }
1978
1979 ph->env.nr_pmu_mappings = pmu_num;
1980 strbuf_init(&sb, 128);
1981
1982 while (pmu_num) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001983 if (readn(fd, &type, sizeof(type)) != sizeof(type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001984 goto error;
1985 if (ph->needs_swap)
1986 type = bswap_32(type);
1987
1988 name = do_read_string(fd, ph);
1989 if (!name)
1990 goto error;
1991
1992 strbuf_addf(&sb, "%u:%s", type, name);
1993 /* include a NULL character at the end */
1994 strbuf_add(&sb, "", 1);
1995
1996 free(name);
1997 pmu_num--;
1998 }
1999 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2000 return 0;
2001
2002error:
2003 strbuf_release(&sb);
2004 return -1;
2005}
2006
Namhyung Kima8bb5592013-01-22 18:09:31 +09002007static int process_group_desc(struct perf_file_section *section __maybe_unused,
2008 struct perf_header *ph, int fd,
2009 void *data __maybe_unused)
2010{
2011 size_t ret = -1;
2012 u32 i, nr, nr_groups;
2013 struct perf_session *session;
2014 struct perf_evsel *evsel, *leader = NULL;
2015 struct group_desc {
2016 char *name;
2017 u32 leader_idx;
2018 u32 nr_members;
2019 } *desc;
2020
2021 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
2022 return -1;
2023
2024 if (ph->needs_swap)
2025 nr_groups = bswap_32(nr_groups);
2026
2027 ph->env.nr_groups = nr_groups;
2028 if (!nr_groups) {
2029 pr_debug("group desc not available\n");
2030 return 0;
2031 }
2032
2033 desc = calloc(nr_groups, sizeof(*desc));
2034 if (!desc)
2035 return -1;
2036
2037 for (i = 0; i < nr_groups; i++) {
2038 desc[i].name = do_read_string(fd, ph);
2039 if (!desc[i].name)
2040 goto out_free;
2041
2042 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
2043 goto out_free;
2044
2045 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
2046 goto out_free;
2047
2048 if (ph->needs_swap) {
2049 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
2050 desc[i].nr_members = bswap_32(desc[i].nr_members);
2051 }
2052 }
2053
2054 /*
2055 * Rebuild group relationship based on the group_desc
2056 */
2057 session = container_of(ph, struct perf_session, header);
2058 session->evlist->nr_groups = nr_groups;
2059
2060 i = nr = 0;
2061 list_for_each_entry(evsel, &session->evlist->entries, node) {
2062 if (evsel->idx == (int) desc[i].leader_idx) {
2063 evsel->leader = evsel;
2064 /* {anon_group} is a dummy name */
2065 if (strcmp(desc[i].name, "{anon_group}"))
2066 evsel->group_name = desc[i].name;
2067 evsel->nr_members = desc[i].nr_members;
2068
2069 if (i >= nr_groups || nr > 0) {
2070 pr_debug("invalid group desc\n");
2071 goto out_free;
2072 }
2073
2074 leader = evsel;
2075 nr = evsel->nr_members - 1;
2076 i++;
2077 } else if (nr) {
2078 /* This is a group member */
2079 evsel->leader = leader;
2080
2081 nr--;
2082 }
2083 }
2084
2085 if (i != nr_groups || nr != 0) {
2086 pr_debug("invalid group desc\n");
2087 goto out_free;
2088 }
2089
2090 ret = 0;
2091out_free:
2092 while ((int) --i >= 0)
2093 free(desc[i].name);
2094 free(desc);
2095
2096 return ret;
2097}
2098
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002099struct feature_ops {
2100 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2101 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01002102 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002103 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002104 const char *name;
2105 bool full_only;
2106};
2107
Robert Richter8cdfa782011-12-07 10:02:56 +01002108#define FEAT_OPA(n, func) \
2109 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01002110#define FEAT_OPP(n, func) \
2111 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2112 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01002113#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01002114 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09002115 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01002116
2117/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002118#define print_tracing_data NULL
2119#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002120
2121static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002122 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01002123 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002124 FEAT_OPP(HEADER_HOSTNAME, hostname),
2125 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2126 FEAT_OPP(HEADER_VERSION, version),
2127 FEAT_OPP(HEADER_ARCH, arch),
2128 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2129 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09002130 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002131 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02002132 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002133 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01002134 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2135 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01002136 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002137 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09002138 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002139};
2140
2141struct header_print_data {
2142 FILE *fp;
2143 bool full; /* extended list of headers */
2144};
2145
2146static int perf_file_section__fprintf_info(struct perf_file_section *section,
2147 struct perf_header *ph,
2148 int feat, int fd, void *data)
2149{
2150 struct header_print_data *hd = data;
2151
2152 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2153 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2154 "%d, continuing...\n", section->offset, feat);
2155 return 0;
2156 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002157 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002158 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002159 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002160 }
2161 if (!feat_ops[feat].print)
2162 return 0;
2163
2164 if (!feat_ops[feat].full_only || hd->full)
2165 feat_ops[feat].print(ph, fd, hd->fp);
2166 else
2167 fprintf(hd->fp, "# %s info available, use -I to display\n",
2168 feat_ops[feat].name);
2169
2170 return 0;
2171}
2172
2173int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2174{
2175 struct header_print_data hd;
2176 struct perf_header *header = &session->header;
2177 int fd = session->fd;
2178 hd.fp = fp;
2179 hd.full = full;
2180
2181 perf_header__process_sections(header, fd, &hd,
2182 perf_file_section__fprintf_info);
2183 return 0;
2184}
2185
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002186static int do_write_feat(int fd, struct perf_header *h, int type,
2187 struct perf_file_section **p,
2188 struct perf_evlist *evlist)
2189{
2190 int err;
2191 int ret = 0;
2192
2193 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002194 if (!feat_ops[type].write)
2195 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002196
2197 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2198
2199 err = feat_ops[type].write(fd, h, evlist);
2200 if (err < 0) {
2201 pr_debug("failed to write feature %d\n", type);
2202
2203 /* undo anything written */
2204 lseek(fd, (*p)->offset, SEEK_SET);
2205
2206 return -1;
2207 }
2208 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2209 (*p)++;
2210 }
2211 return ret;
2212}
2213
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002214static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002215 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002216{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002217 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002218 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002219 int sec_size;
2220 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002221 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002222 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002223
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002224 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002225 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002226 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002227
Paul Gortmaker91b98802013-01-30 20:05:49 -05002228 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002229 if (feat_sec == NULL)
2230 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002231
2232 sec_size = sizeof(*feat_sec) * nr_sections;
2233
Jiri Olsa8d541e92013-07-17 19:49:44 +02002234 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002235 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002236
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002237 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2238 if (do_write_feat(fd, header, feat, &p, evlist))
2239 perf_header__clear_feat(header, feat);
2240 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002241
Xiao Guangrongf887f302010-02-04 16:46:42 +08002242 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002243 /*
2244 * may write more than needed due to dropped feature, but
2245 * this is okay, reader will skip the mising entries
2246 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002247 err = do_write(fd, feat_sec, sec_size);
2248 if (err < 0)
2249 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002250 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002251 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002252}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002253
Tom Zanussi8dc58102010-04-01 23:59:15 -05002254int perf_header__write_pipe(int fd)
2255{
2256 struct perf_pipe_file_header f_header;
2257 int err;
2258
2259 f_header = (struct perf_pipe_file_header){
2260 .magic = PERF_MAGIC,
2261 .size = sizeof(f_header),
2262 };
2263
2264 err = do_write(fd, &f_header, sizeof(f_header));
2265 if (err < 0) {
2266 pr_debug("failed to write perf pipe header\n");
2267 return err;
2268 }
2269
2270 return 0;
2271}
2272
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002273int perf_session__write_header(struct perf_session *session,
2274 struct perf_evlist *evlist,
2275 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002276{
2277 struct perf_file_header f_header;
2278 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002279 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002280 struct perf_evsel *evsel;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002281 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002282 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002283
2284 lseek(fd, sizeof(f_header), SEEK_SET);
2285
Robert Richter6606f872012-08-16 21:10:19 +02002286 list_for_each_entry(evsel, &evlist->entries, node) {
2287 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2288 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002289 if (err < 0) {
2290 pr_debug("failed to write perf header\n");
2291 return err;
2292 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002293 }
2294
Jiri Olsa944d62b2013-07-17 19:49:43 +02002295 attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002296
Robert Richter6606f872012-08-16 21:10:19 +02002297 list_for_each_entry(evsel, &evlist->entries, node) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002298 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002299 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002300 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002301 .offset = evsel->id_offset,
2302 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002303 }
2304 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002305 err = do_write(fd, &f_attr, sizeof(f_attr));
2306 if (err < 0) {
2307 pr_debug("failed to write perf header attribute\n");
2308 return err;
2309 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002310 }
2311
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002312 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002313 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002314
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002315 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002316 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002317 if (err < 0)
2318 return err;
2319 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002320
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002321 f_header = (struct perf_file_header){
2322 .magic = PERF_MAGIC,
2323 .size = sizeof(f_header),
2324 .attr_size = sizeof(f_attr),
2325 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002326 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002327 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002328 },
2329 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002330 .offset = header->data_offset,
2331 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002332 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002333 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002334 };
2335
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002336 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002337
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002338 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002339 err = do_write(fd, &f_header, sizeof(f_header));
2340 if (err < 0) {
2341 pr_debug("failed to write perf header\n");
2342 return err;
2343 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002344 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002345
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002346 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002347}
2348
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002349static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002350 int fd, void *buf, size_t size)
2351{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002352 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002353 return -1;
2354
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002355 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002356 mem_bswap_64(buf, size);
2357
2358 return 0;
2359}
2360
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002361int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002362 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002363 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002364 struct perf_header *ph,
2365 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002366{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002367 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002368 int nr_sections;
2369 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002370 int feat;
2371 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002372
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002373 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002374 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002375 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002376
Paul Gortmaker91b98802013-01-30 20:05:49 -05002377 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002378 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002379 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002380
2381 sec_size = sizeof(*feat_sec) * nr_sections;
2382
Jiri Olsa8d541e92013-07-17 19:49:44 +02002383 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002384
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002385 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2386 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002387 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002388
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002389 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2390 err = process(sec++, header, feat, fd, data);
2391 if (err < 0)
2392 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002393 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002394 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002395out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002396 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002397 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002398}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002399
Stephane Eranian114382a2012-02-09 23:21:08 +01002400static const int attr_file_abi_sizes[] = {
2401 [0] = PERF_ATTR_SIZE_VER0,
2402 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002403 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002404 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian114382a2012-02-09 23:21:08 +01002405 0,
2406};
2407
2408/*
2409 * In the legacy file format, the magic number is not used to encode endianness.
2410 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2411 * on ABI revisions, we need to try all combinations for all endianness to
2412 * detect the endianness.
2413 */
2414static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2415{
2416 uint64_t ref_size, attr_size;
2417 int i;
2418
2419 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2420 ref_size = attr_file_abi_sizes[i]
2421 + sizeof(struct perf_file_section);
2422 if (hdr_sz != ref_size) {
2423 attr_size = bswap_64(hdr_sz);
2424 if (attr_size != ref_size)
2425 continue;
2426
2427 ph->needs_swap = true;
2428 }
2429 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2430 i,
2431 ph->needs_swap);
2432 return 0;
2433 }
2434 /* could not determine endianness */
2435 return -1;
2436}
2437
2438#define PERF_PIPE_HDR_VER0 16
2439
2440static const size_t attr_pipe_abi_sizes[] = {
2441 [0] = PERF_PIPE_HDR_VER0,
2442 0,
2443};
2444
2445/*
2446 * In the legacy pipe format, there is an implicit assumption that endiannesss
2447 * between host recording the samples, and host parsing the samples is the
2448 * same. This is not always the case given that the pipe output may always be
2449 * redirected into a file and analyzed on a different machine with possibly a
2450 * different endianness and perf_event ABI revsions in the perf tool itself.
2451 */
2452static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2453{
2454 u64 attr_size;
2455 int i;
2456
2457 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2458 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2459 attr_size = bswap_64(hdr_sz);
2460 if (attr_size != hdr_sz)
2461 continue;
2462
2463 ph->needs_swap = true;
2464 }
2465 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2466 return 0;
2467 }
2468 return -1;
2469}
2470
Feng Tange84ba4e2012-10-30 11:56:07 +08002471bool is_perf_magic(u64 magic)
2472{
2473 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2474 || magic == __perf_magic2
2475 || magic == __perf_magic2_sw)
2476 return true;
2477
2478 return false;
2479}
2480
Stephane Eranian114382a2012-02-09 23:21:08 +01002481static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2482 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002483{
2484 int ret;
2485
2486 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002487 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002488 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002489 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002490 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002491 if (is_pipe)
2492 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002493
Stephane Eranian114382a2012-02-09 23:21:08 +01002494 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002495 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002496 /*
2497 * the new magic number serves two purposes:
2498 * - unique number to identify actual perf.data files
2499 * - encode endianness of file
2500 */
Stephane Eranian73323f52012-02-02 13:54:44 +01002501
Stephane Eranian114382a2012-02-09 23:21:08 +01002502 /* check magic number with one endianness */
2503 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002504 return 0;
2505
Stephane Eranian114382a2012-02-09 23:21:08 +01002506 /* check magic number with opposite endianness */
2507 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002508 return -1;
2509
2510 ph->needs_swap = true;
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002511 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002512
2513 return 0;
2514}
2515
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002516int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002517 struct perf_header *ph, int fd)
2518{
Stephane Eranian73323f52012-02-02 13:54:44 +01002519 int ret;
2520
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002521 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002522
Stephane Eranian73323f52012-02-02 13:54:44 +01002523 ret = readn(fd, header, sizeof(*header));
2524 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002525 return -1;
2526
Stephane Eranian114382a2012-02-09 23:21:08 +01002527 if (check_magic_endian(header->magic,
2528 header->attr_size, false, ph) < 0) {
2529 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002530 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002531 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002532
Stephane Eranian73323f52012-02-02 13:54:44 +01002533 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002534 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002535 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002536 }
2537
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002538 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002539 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002540 if (header->size == offsetof(typeof(*header), adds_features))
2541 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002542 else
2543 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002544 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002545 /*
2546 * feature bitmap is declared as an array of unsigned longs --
2547 * not good since its size can differ between the host that
2548 * generated the data file and the host analyzing the file.
2549 *
2550 * We need to handle endianness, but we don't know the size of
2551 * the unsigned long where the file was generated. Take a best
2552 * guess at determining it: try 64-bit swap first (ie., file
2553 * created on a 64-bit host), and check if the hostname feature
2554 * bit is set (this feature bit is forced on as of fbe96f2).
2555 * If the bit is not, undo the 64-bit swap and try a 32-bit
2556 * swap. If the hostname bit is still not set (e.g., older data
2557 * file), punt and fallback to the original behavior --
2558 * clearing all feature bits and setting buildid.
2559 */
David Ahern80c01202012-06-08 11:47:51 -03002560 mem_bswap_64(&header->adds_features,
2561 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002562
2563 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002564 /* unswap as u64 */
2565 mem_bswap_64(&header->adds_features,
2566 BITS_TO_U64(HEADER_FEAT_BITS));
2567
2568 /* unswap as u32 */
2569 mem_bswap_32(&header->adds_features,
2570 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002571 }
2572
2573 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2574 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2575 set_bit(HEADER_BUILD_ID, header->adds_features);
2576 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002577 }
2578
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002579 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002580 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002581
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002582 ph->data_offset = header->data.offset;
2583 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002584 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002585 return 0;
2586}
2587
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002588static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002589 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002590 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002591{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002592 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002593 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002594 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002595 return 0;
2596 }
2597
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002598 if (feat >= HEADER_LAST_FEATURE) {
2599 pr_debug("unknown feature %d, continuing...\n", feat);
2600 return 0;
2601 }
2602
Robert Richterf1c67db2012-02-10 15:41:56 +01002603 if (!feat_ops[feat].process)
2604 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002605
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002606 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002607}
2608
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002609static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002610 struct perf_header *ph, int fd,
2611 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002612{
Stephane Eranian73323f52012-02-02 13:54:44 +01002613 int ret;
2614
2615 ret = readn(fd, header, sizeof(*header));
2616 if (ret <= 0)
2617 return -1;
2618
Stephane Eranian114382a2012-02-09 23:21:08 +01002619 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2620 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002621 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002622 }
2623
2624 if (ph->needs_swap)
2625 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002626
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002627 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002628 return -1;
2629
Tom Zanussi8dc58102010-04-01 23:59:15 -05002630 return 0;
2631}
2632
Jiri Olsad4339562013-07-17 19:49:41 +02002633static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002634{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002635 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002636 struct perf_pipe_file_header f_header;
2637
Jiri Olsad4339562013-07-17 19:49:41 +02002638 if (perf_file_header__read_pipe(&f_header, header, session->fd,
Tom Zanussi454c4072010-05-01 01:41:20 -05002639 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002640 pr_debug("incompatible file format\n");
2641 return -EINVAL;
2642 }
2643
Tom Zanussi8dc58102010-04-01 23:59:15 -05002644 return 0;
2645}
2646
Stephane Eranian69996df2012-02-09 23:21:06 +01002647static int read_attr(int fd, struct perf_header *ph,
2648 struct perf_file_attr *f_attr)
2649{
2650 struct perf_event_attr *attr = &f_attr->attr;
2651 size_t sz, left;
2652 size_t our_sz = sizeof(f_attr->attr);
2653 int ret;
2654
2655 memset(f_attr, 0, sizeof(*f_attr));
2656
2657 /* read minimal guaranteed structure */
2658 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2659 if (ret <= 0) {
2660 pr_debug("cannot read %d bytes of header attr\n",
2661 PERF_ATTR_SIZE_VER0);
2662 return -1;
2663 }
2664
2665 /* on file perf_event_attr size */
2666 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002667
Stephane Eranian69996df2012-02-09 23:21:06 +01002668 if (ph->needs_swap)
2669 sz = bswap_32(sz);
2670
2671 if (sz == 0) {
2672 /* assume ABI0 */
2673 sz = PERF_ATTR_SIZE_VER0;
2674 } else if (sz > our_sz) {
2675 pr_debug("file uses a more recent and unsupported ABI"
2676 " (%zu bytes extra)\n", sz - our_sz);
2677 return -1;
2678 }
2679 /* what we have not yet read and that we know about */
2680 left = sz - PERF_ATTR_SIZE_VER0;
2681 if (left) {
2682 void *ptr = attr;
2683 ptr += PERF_ATTR_SIZE_VER0;
2684
2685 ret = readn(fd, ptr, left);
2686 }
2687 /* read perf_file_section, ids are read in caller */
2688 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2689
2690 return ret <= 0 ? -1 : 0;
2691}
2692
Namhyung Kim831394b2012-09-06 11:10:46 +09002693static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2694 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002695{
Namhyung Kim831394b2012-09-06 11:10:46 +09002696 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002697 char bf[128];
2698
Namhyung Kim831394b2012-09-06 11:10:46 +09002699 /* already prepared */
2700 if (evsel->tp_format)
2701 return 0;
2702
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002703 if (pevent == NULL) {
2704 pr_debug("broken or missing trace data\n");
2705 return -1;
2706 }
2707
Namhyung Kim831394b2012-09-06 11:10:46 +09002708 event = pevent_find_event(pevent, evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002709 if (event == NULL)
2710 return -1;
2711
Namhyung Kim831394b2012-09-06 11:10:46 +09002712 if (!evsel->name) {
2713 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2714 evsel->name = strdup(bf);
2715 if (evsel->name == NULL)
2716 return -1;
2717 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002718
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002719 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002720 return 0;
2721}
2722
Namhyung Kim831394b2012-09-06 11:10:46 +09002723static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2724 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002725{
2726 struct perf_evsel *pos;
2727
2728 list_for_each_entry(pos, &evlist->entries, node) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002729 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2730 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002731 return -1;
2732 }
2733
2734 return 0;
2735}
2736
Jiri Olsad4339562013-07-17 19:49:41 +02002737int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002738{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002739 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002740 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002741 struct perf_file_attr f_attr;
2742 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002743 int nr_attrs, nr_ids, i, j;
Jiri Olsad4339562013-07-17 19:49:41 +02002744 int fd = session->fd;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002745
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002746 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002747 if (session->evlist == NULL)
2748 return -ENOMEM;
2749
Tom Zanussi8dc58102010-04-01 23:59:15 -05002750 if (session->fd_pipe)
Jiri Olsad4339562013-07-17 19:49:41 +02002751 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002752
Stephane Eranian69996df2012-02-09 23:21:06 +01002753 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002754 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002755
Stephane Eranian69996df2012-02-09 23:21:06 +01002756 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002757 lseek(fd, f_header.attrs.offset, SEEK_SET);
2758
2759 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002760 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002761 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002762
Stephane Eranian69996df2012-02-09 23:21:06 +01002763 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002764 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002765
David Aherneda39132011-07-15 12:34:09 -06002766 if (header->needs_swap)
2767 perf_event__attr_swap(&f_attr.attr);
2768
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002769 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002770 evsel = perf_evsel__new(&f_attr.attr, i);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002771
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002772 if (evsel == NULL)
2773 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002774
2775 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002776 /*
2777 * Do it before so that if perf_evsel__alloc_id fails, this
2778 * entry gets purged too at perf_evlist__delete().
2779 */
2780 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002781
2782 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002783 /*
2784 * We don't have the cpu and thread maps on the header, so
2785 * for allocating the perf_sample_id table we fake 1 cpu and
2786 * hattr->ids threads.
2787 */
2788 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2789 goto out_delete_evlist;
2790
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002791 lseek(fd, f_attr.ids.offset, SEEK_SET);
2792
2793 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002794 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002795 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002796
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002797 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002798 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002799
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002800 lseek(fd, tmp, SEEK_SET);
2801 }
2802
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002803 symbol_conf.nr_events = nr_attrs;
2804
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002805 perf_header__process_sections(header, fd, &session->pevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002806 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002807
Namhyung Kim831394b2012-09-06 11:10:46 +09002808 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2809 session->pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002810 goto out_delete_evlist;
2811
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002812 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002813out_errno:
2814 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002815
2816out_delete_evlist:
2817 perf_evlist__delete(session->evlist);
2818 session->evlist = NULL;
2819 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002820}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002821
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002822int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002823 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002824 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002825{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002826 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002827 size_t size;
2828 int err;
2829
2830 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002831 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002832 size += sizeof(struct perf_event_header);
2833 size += ids * sizeof(u64);
2834
2835 ev = malloc(size);
2836
Chris Samuelce47dc52010-11-13 13:35:06 +11002837 if (ev == NULL)
2838 return -ENOMEM;
2839
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002840 ev->attr.attr = *attr;
2841 memcpy(ev->attr.id, id, ids * sizeof(u64));
2842
2843 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002844 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002845
Robert Richterf4d83432012-08-16 21:10:17 +02002846 if (ev->attr.header.size == size)
2847 err = process(tool, ev, NULL, NULL);
2848 else
2849 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002850
2851 free(ev);
2852
2853 return err;
2854}
2855
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002856int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002857 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002858 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002859{
Robert Richter6606f872012-08-16 21:10:19 +02002860 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002861 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002862
Robert Richter6606f872012-08-16 21:10:19 +02002863 list_for_each_entry(evsel, &session->evlist->entries, node) {
2864 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2865 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002866 if (err) {
2867 pr_debug("failed to create perf header attribute\n");
2868 return err;
2869 }
2870 }
2871
2872 return err;
2873}
2874
Adrian Hunter47c3d102013-07-04 16:20:21 +03002875int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2876 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002877 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002878{
Robert Richterf4d83432012-08-16 21:10:17 +02002879 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002880 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002881 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002882
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002883 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002884 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002885 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002886 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002887 }
2888
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002889 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002890 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002891 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002892
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002893 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002894
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002895 ids = event->header.size;
2896 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002897 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002898 /*
2899 * We don't have the cpu and thread maps on the header, so
2900 * for allocating the perf_sample_id table we fake 1 cpu and
2901 * hattr->ids threads.
2902 */
2903 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2904 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002905
2906 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002907 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002908 }
2909
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03002910 symbol_conf.nr_events = evlist->nr_entries;
2911
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002912 return 0;
2913}
Tom Zanussicd19a032010-04-01 23:59:20 -05002914
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002915int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002916 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002917 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05002918{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002919 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02002920 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05002921 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002922 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05002923
Jiri Olsa29208e52011-10-20 15:59:43 +02002924 /*
2925 * We are going to store the size of the data followed
2926 * by the data contents. Since the fd descriptor is a pipe,
2927 * we cannot seek back to store the size of the data once
2928 * we know it. Instead we:
2929 *
2930 * - write the tracing data to the temp file
2931 * - get/write the data size to pipe
2932 * - write the tracing data from the temp file
2933 * to the pipe
2934 */
2935 tdata = tracing_data_get(&evlist->entries, fd, true);
2936 if (!tdata)
2937 return -1;
2938
Tom Zanussi92155452010-04-01 23:59:21 -05002939 memset(&ev, 0, sizeof(ev));
2940
2941 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02002942 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002943 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05002944 padding = aligned_size - size;
2945 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2946 ev.tracing_data.size = aligned_size;
2947
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002948 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05002949
Jiri Olsa29208e52011-10-20 15:59:43 +02002950 /*
2951 * The put function will copy all the tracing data
2952 * stored in temp file to the pipe.
2953 */
2954 tracing_data_put(tdata);
2955
Tom Zanussi92155452010-04-01 23:59:21 -05002956 write_padded(fd, NULL, 0, padding);
2957
2958 return aligned_size;
2959}
2960
Adrian Hunter47c3d102013-07-04 16:20:21 +03002961int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
2962 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002963 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05002964{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002965 ssize_t size_read, padding, size = event->tracing_data.size;
Tom Zanussi92155452010-04-01 23:59:21 -05002966 off_t offset = lseek(session->fd, 0, SEEK_CUR);
2967 char buf[BUFSIZ];
2968
2969 /* setup for reading amidst mmap */
2970 lseek(session->fd, offset + sizeof(struct tracing_data_event),
2971 SEEK_SET);
2972
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002973 size_read = trace_report(session->fd, &session->pevent,
2974 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002975 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05002976
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002977 if (readn(session->fd, buf, padding) < 0) {
2978 pr_err("%s: reading input file", __func__);
2979 return -1;
2980 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002981 if (session->repipe) {
2982 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002983 if (retw <= 0 || retw != padding) {
2984 pr_err("%s: repiping tracing data padding", __func__);
2985 return -1;
2986 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002987 }
Tom Zanussi92155452010-04-01 23:59:21 -05002988
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002989 if (size_read + padding != size) {
2990 pr_err("%s: tracing data size mismatch", __func__);
2991 return -1;
2992 }
Tom Zanussi92155452010-04-01 23:59:21 -05002993
Namhyung Kim831394b2012-09-06 11:10:46 +09002994 perf_evlist__prepare_tracepoint_events(session->evlist,
2995 session->pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03002996
Tom Zanussi92155452010-04-01 23:59:21 -05002997 return size_read + padding;
2998}
Tom Zanussic7929e42010-04-01 23:59:22 -05002999
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003000int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003001 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003002 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003003 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003004{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003005 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003006 size_t len;
3007 int err = 0;
3008
3009 if (!pos->hit)
3010 return err;
3011
3012 memset(&ev, 0, sizeof(ev));
3013
3014 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003015 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003016 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3017 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3018 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003019 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003020 ev.build_id.header.size = sizeof(ev.build_id) + len;
3021 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3022
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003023 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003024
3025 return err;
3026}
3027
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003028int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003029 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003030 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003031{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003032 __event_process_build_id(&event->build_id,
3033 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003034 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003035 return 0;
3036}
Stephane Eraniana1ac1d32010-06-17 11:39:01 +02003037
3038void disable_buildid_cache(void)
3039{
3040 no_buildid_cache = true;
3041}