blob: a33197a4fd21b4790191ca9f654498e0b660bced [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
Robert Richter4e1b9c62012-08-16 21:10:22 +02001354 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001355 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001356 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1357 if (j)
1358 fputc(',', fp);
1359 fprintf(fp, " %"PRIu64, *id);
1360 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001361 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001362 }
1363
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001364 fputc('\n', fp);
1365 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001366
1367 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001368}
1369
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001370static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001371 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001372{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001373 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001374}
1375
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001376static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001377 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001378{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001379 u32 nr, c, i;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001380 char *str, *tmp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001381 uint64_t mem_total, mem_free;
1382
1383 /* nr nodes */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001384 nr = ph->env.nr_numa_nodes;
1385 str = ph->env.numa_nodes;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001386
1387 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001388 /* node number */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001389 c = strtoul(str, &tmp, 0);
1390 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001391 goto error;
1392
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001393 str = tmp + 1;
1394 mem_total = strtoull(str, &tmp, 0);
1395 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001396 goto error;
1397
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001398 str = tmp + 1;
1399 mem_free = strtoull(str, &tmp, 0);
1400 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001401 goto error;
1402
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001403 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1404 " free = %"PRIu64" kB\n",
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001405 c, mem_total, mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001406
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001407 str = tmp + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001408 fprintf(fp, "# node%u cpu list : %s\n", c, str);
Namhyung Kim12344712012-10-23 22:44:49 +09001409
1410 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001411 }
1412 return;
1413error:
1414 fprintf(fp, "# numa topology : not available\n");
1415}
1416
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001417static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001418{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001419 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001420}
1421
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001422static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001423 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001424{
1425 fprintf(fp, "# contains samples with branch stack\n");
1426}
1427
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001428static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1429 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001430{
1431 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001432 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001433 u32 pmu_num;
1434 u32 type;
1435
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001436 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001437 if (!pmu_num) {
1438 fprintf(fp, "# pmu mappings: not available\n");
1439 return;
1440 }
1441
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001442 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001443
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001444 while (pmu_num) {
1445 type = strtoul(str, &tmp, 0);
1446 if (*tmp != ':')
1447 goto error;
1448
1449 str = tmp + 1;
1450 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1451
Robert Richter50a96672012-08-16 21:10:24 +02001452 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001453 str += strlen(str) + 1;
1454 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001455 }
1456
1457 fprintf(fp, "\n");
1458
1459 if (!pmu_num)
1460 return;
1461error:
1462 fprintf(fp, "# pmu mappings: unable to read\n");
1463}
1464
Namhyung Kima8bb5592013-01-22 18:09:31 +09001465static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1466 FILE *fp)
1467{
1468 struct perf_session *session;
1469 struct perf_evsel *evsel;
1470 u32 nr = 0;
1471
1472 session = container_of(ph, struct perf_session, header);
1473
1474 list_for_each_entry(evsel, &session->evlist->entries, node) {
1475 if (perf_evsel__is_group_leader(evsel) &&
1476 evsel->nr_members > 1) {
1477 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1478 perf_evsel__name(evsel));
1479
1480 nr = evsel->nr_members - 1;
1481 } else if (nr) {
1482 fprintf(fp, ",%s", perf_evsel__name(evsel));
1483
1484 if (--nr == 0)
1485 fprintf(fp, "}\n");
1486 }
1487 }
1488}
1489
Robert Richter08d95bd2012-02-10 15:41:55 +01001490static int __event_process_build_id(struct build_id_event *bev,
1491 char *filename,
1492 struct perf_session *session)
1493{
1494 int err = -1;
1495 struct list_head *head;
1496 struct machine *machine;
1497 u16 misc;
1498 struct dso *dso;
1499 enum dso_kernel_type dso_type;
1500
1501 machine = perf_session__findnew_machine(session, bev->pid);
1502 if (!machine)
1503 goto out;
1504
1505 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1506
1507 switch (misc) {
1508 case PERF_RECORD_MISC_KERNEL:
1509 dso_type = DSO_TYPE_KERNEL;
1510 head = &machine->kernel_dsos;
1511 break;
1512 case PERF_RECORD_MISC_GUEST_KERNEL:
1513 dso_type = DSO_TYPE_GUEST_KERNEL;
1514 head = &machine->kernel_dsos;
1515 break;
1516 case PERF_RECORD_MISC_USER:
1517 case PERF_RECORD_MISC_GUEST_USER:
1518 dso_type = DSO_TYPE_USER;
1519 head = &machine->user_dsos;
1520 break;
1521 default:
1522 goto out;
1523 }
1524
1525 dso = __dsos__findnew(head, filename);
1526 if (dso != NULL) {
1527 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1528
1529 dso__set_build_id(dso, &bev->build_id);
1530
1531 if (filename[0] == '[')
1532 dso->kernel = dso_type;
1533
1534 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1535 sbuild_id);
1536 pr_debug("build id event received for %s: %s\n",
1537 dso->long_name, sbuild_id);
1538 }
1539
1540 err = 0;
1541out:
1542 return err;
1543}
1544
1545static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1546 int input, u64 offset, u64 size)
1547{
1548 struct perf_session *session = container_of(header, struct perf_session, header);
1549 struct {
1550 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001551 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001552 char filename[0];
1553 } old_bev;
1554 struct build_id_event bev;
1555 char filename[PATH_MAX];
1556 u64 limit = offset + size;
1557
1558 while (offset < limit) {
1559 ssize_t len;
1560
Namhyung Kim5323f602012-12-17 15:38:54 +09001561 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001562 return -1;
1563
1564 if (header->needs_swap)
1565 perf_event_header__bswap(&old_bev.header);
1566
1567 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001568 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001569 return -1;
1570
1571 bev.header = old_bev.header;
1572
1573 /*
1574 * As the pid is the missing value, we need to fill
1575 * it properly. The header.misc value give us nice hint.
1576 */
1577 bev.pid = HOST_KERNEL_ID;
1578 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1579 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1580 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1581
1582 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1583 __event_process_build_id(&bev, filename, session);
1584
1585 offset += bev.header.size;
1586 }
1587
1588 return 0;
1589}
1590
1591static int perf_header__read_build_ids(struct perf_header *header,
1592 int input, u64 offset, u64 size)
1593{
1594 struct perf_session *session = container_of(header, struct perf_session, header);
1595 struct build_id_event bev;
1596 char filename[PATH_MAX];
1597 u64 limit = offset + size, orig_offset = offset;
1598 int err = -1;
1599
1600 while (offset < limit) {
1601 ssize_t len;
1602
Namhyung Kim5323f602012-12-17 15:38:54 +09001603 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001604 goto out;
1605
1606 if (header->needs_swap)
1607 perf_event_header__bswap(&bev.header);
1608
1609 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001610 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001611 goto out;
1612 /*
1613 * The a1645ce1 changeset:
1614 *
1615 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1616 *
1617 * Added a field to struct build_id_event that broke the file
1618 * format.
1619 *
1620 * Since the kernel build-id is the first entry, process the
1621 * table using the old format if the well known
1622 * '[kernel.kallsyms]' string for the kernel build-id has the
1623 * first 4 characters chopped off (where the pid_t sits).
1624 */
1625 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1626 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1627 return -1;
1628 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1629 }
1630
1631 __event_process_build_id(&bev, filename, session);
1632
1633 offset += bev.header.size;
1634 }
1635 err = 0;
1636out:
1637 return err;
1638}
1639
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001640static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1641 struct perf_header *ph __maybe_unused,
1642 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001643{
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001644 ssize_t ret = trace_report(fd, data, false);
1645 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001646}
1647
1648static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001649 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001650 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001651{
1652 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1653 pr_debug("Failed to read buildids, continuing...\n");
1654 return 0;
1655}
1656
Namhyung Kima1ae5652012-09-24 17:14:59 +09001657static int process_hostname(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001658 struct perf_header *ph, int fd,
1659 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001660{
1661 ph->env.hostname = do_read_string(fd, ph);
1662 return ph->env.hostname ? 0 : -ENOMEM;
1663}
1664
1665static int process_osrelease(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001666 struct perf_header *ph, int fd,
1667 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001668{
1669 ph->env.os_release = do_read_string(fd, ph);
1670 return ph->env.os_release ? 0 : -ENOMEM;
1671}
1672
1673static int process_version(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001674 struct perf_header *ph, int fd,
1675 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001676{
1677 ph->env.version = do_read_string(fd, ph);
1678 return ph->env.version ? 0 : -ENOMEM;
1679}
1680
1681static int process_arch(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001682 struct perf_header *ph, int fd,
1683 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001684{
1685 ph->env.arch = do_read_string(fd, ph);
1686 return ph->env.arch ? 0 : -ENOMEM;
1687}
1688
1689static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001690 struct perf_header *ph, int fd,
1691 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001692{
1693 size_t ret;
1694 u32 nr;
1695
Namhyung Kim5323f602012-12-17 15:38:54 +09001696 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001697 if (ret != sizeof(nr))
1698 return -1;
1699
1700 if (ph->needs_swap)
1701 nr = bswap_32(nr);
1702
1703 ph->env.nr_cpus_online = nr;
1704
Namhyung Kim5323f602012-12-17 15:38:54 +09001705 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001706 if (ret != sizeof(nr))
1707 return -1;
1708
1709 if (ph->needs_swap)
1710 nr = bswap_32(nr);
1711
1712 ph->env.nr_cpus_avail = nr;
1713 return 0;
1714}
1715
1716static int process_cpudesc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001717 struct perf_header *ph, int fd,
1718 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001719{
1720 ph->env.cpu_desc = do_read_string(fd, ph);
1721 return ph->env.cpu_desc ? 0 : -ENOMEM;
1722}
1723
1724static int process_cpuid(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001725 struct perf_header *ph, int fd,
1726 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001727{
1728 ph->env.cpuid = do_read_string(fd, ph);
1729 return ph->env.cpuid ? 0 : -ENOMEM;
1730}
1731
1732static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001733 struct perf_header *ph, int fd,
1734 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001735{
1736 uint64_t mem;
1737 size_t ret;
1738
Namhyung Kim5323f602012-12-17 15:38:54 +09001739 ret = readn(fd, &mem, sizeof(mem));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001740 if (ret != sizeof(mem))
1741 return -1;
1742
1743 if (ph->needs_swap)
1744 mem = bswap_64(mem);
1745
1746 ph->env.total_mem = mem;
1747 return 0;
1748}
1749
Robert Richter7c2f7af2012-08-16 21:10:23 +02001750static struct perf_evsel *
1751perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1752{
1753 struct perf_evsel *evsel;
1754
1755 list_for_each_entry(evsel, &evlist->entries, node) {
1756 if (evsel->idx == idx)
1757 return evsel;
1758 }
1759
1760 return NULL;
1761}
1762
1763static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001764perf_evlist__set_event_name(struct perf_evlist *evlist,
1765 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001766{
1767 struct perf_evsel *evsel;
1768
1769 if (!event->name)
1770 return;
1771
1772 evsel = perf_evlist__find_by_index(evlist, event->idx);
1773 if (!evsel)
1774 return;
1775
1776 if (evsel->name)
1777 return;
1778
1779 evsel->name = strdup(event->name);
1780}
1781
1782static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001783process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001784 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001785 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001786{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001787 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001788 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1789
1790 if (!events)
1791 return 0;
1792
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001793 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001794 for (evsel = events; evsel->attr.size; evsel++)
1795 perf_evlist__set_event_name(session->evlist, evsel);
1796
1797 free_event_desc(events);
1798
1799 return 0;
1800}
1801
Namhyung Kima1ae5652012-09-24 17:14:59 +09001802static int process_cmdline(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001803 struct perf_header *ph, int fd,
1804 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001805{
1806 size_t ret;
1807 char *str;
1808 u32 nr, i;
1809 struct strbuf sb;
1810
Namhyung Kim5323f602012-12-17 15:38:54 +09001811 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001812 if (ret != sizeof(nr))
1813 return -1;
1814
1815 if (ph->needs_swap)
1816 nr = bswap_32(nr);
1817
1818 ph->env.nr_cmdline = nr;
1819 strbuf_init(&sb, 128);
1820
1821 for (i = 0; i < nr; i++) {
1822 str = do_read_string(fd, ph);
1823 if (!str)
1824 goto error;
1825
1826 /* include a NULL character at the end */
1827 strbuf_add(&sb, str, strlen(str) + 1);
1828 free(str);
1829 }
1830 ph->env.cmdline = strbuf_detach(&sb, NULL);
1831 return 0;
1832
1833error:
1834 strbuf_release(&sb);
1835 return -1;
1836}
1837
1838static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001839 struct perf_header *ph, int fd,
1840 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001841{
1842 size_t ret;
1843 u32 nr, i;
1844 char *str;
1845 struct strbuf sb;
1846
Namhyung Kim5323f602012-12-17 15:38:54 +09001847 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001848 if (ret != sizeof(nr))
1849 return -1;
1850
1851 if (ph->needs_swap)
1852 nr = bswap_32(nr);
1853
1854 ph->env.nr_sibling_cores = nr;
1855 strbuf_init(&sb, 128);
1856
1857 for (i = 0; i < nr; i++) {
1858 str = do_read_string(fd, ph);
1859 if (!str)
1860 goto error;
1861
1862 /* include a NULL character at the end */
1863 strbuf_add(&sb, str, strlen(str) + 1);
1864 free(str);
1865 }
1866 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1867
Namhyung Kim5323f602012-12-17 15:38:54 +09001868 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001869 if (ret != sizeof(nr))
1870 return -1;
1871
1872 if (ph->needs_swap)
1873 nr = bswap_32(nr);
1874
1875 ph->env.nr_sibling_threads = nr;
1876
1877 for (i = 0; i < nr; i++) {
1878 str = do_read_string(fd, ph);
1879 if (!str)
1880 goto error;
1881
1882 /* include a NULL character at the end */
1883 strbuf_add(&sb, str, strlen(str) + 1);
1884 free(str);
1885 }
1886 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1887 return 0;
1888
1889error:
1890 strbuf_release(&sb);
1891 return -1;
1892}
1893
1894static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001895 struct perf_header *ph, int fd,
1896 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001897{
1898 size_t ret;
1899 u32 nr, node, i;
1900 char *str;
1901 uint64_t mem_total, mem_free;
1902 struct strbuf sb;
1903
1904 /* nr nodes */
Namhyung Kim5323f602012-12-17 15:38:54 +09001905 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001906 if (ret != sizeof(nr))
1907 goto error;
1908
1909 if (ph->needs_swap)
1910 nr = bswap_32(nr);
1911
1912 ph->env.nr_numa_nodes = nr;
1913 strbuf_init(&sb, 256);
1914
1915 for (i = 0; i < nr; i++) {
1916 /* node number */
Namhyung Kim5323f602012-12-17 15:38:54 +09001917 ret = readn(fd, &node, sizeof(node));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001918 if (ret != sizeof(node))
1919 goto error;
1920
Namhyung Kim5323f602012-12-17 15:38:54 +09001921 ret = readn(fd, &mem_total, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001922 if (ret != sizeof(u64))
1923 goto error;
1924
Namhyung Kim5323f602012-12-17 15:38:54 +09001925 ret = readn(fd, &mem_free, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001926 if (ret != sizeof(u64))
1927 goto error;
1928
1929 if (ph->needs_swap) {
1930 node = bswap_32(node);
1931 mem_total = bswap_64(mem_total);
1932 mem_free = bswap_64(mem_free);
1933 }
1934
1935 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1936 node, mem_total, mem_free);
1937
1938 str = do_read_string(fd, ph);
1939 if (!str)
1940 goto error;
1941
1942 /* include a NULL character at the end */
1943 strbuf_add(&sb, str, strlen(str) + 1);
1944 free(str);
1945 }
1946 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1947 return 0;
1948
1949error:
1950 strbuf_release(&sb);
1951 return -1;
1952}
1953
1954static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001955 struct perf_header *ph, int fd,
1956 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001957{
1958 size_t ret;
1959 char *name;
1960 u32 pmu_num;
1961 u32 type;
1962 struct strbuf sb;
1963
Namhyung Kim5323f602012-12-17 15:38:54 +09001964 ret = readn(fd, &pmu_num, sizeof(pmu_num));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001965 if (ret != sizeof(pmu_num))
1966 return -1;
1967
1968 if (ph->needs_swap)
1969 pmu_num = bswap_32(pmu_num);
1970
1971 if (!pmu_num) {
1972 pr_debug("pmu mappings not available\n");
1973 return 0;
1974 }
1975
1976 ph->env.nr_pmu_mappings = pmu_num;
1977 strbuf_init(&sb, 128);
1978
1979 while (pmu_num) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001980 if (readn(fd, &type, sizeof(type)) != sizeof(type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001981 goto error;
1982 if (ph->needs_swap)
1983 type = bswap_32(type);
1984
1985 name = do_read_string(fd, ph);
1986 if (!name)
1987 goto error;
1988
1989 strbuf_addf(&sb, "%u:%s", type, name);
1990 /* include a NULL character at the end */
1991 strbuf_add(&sb, "", 1);
1992
1993 free(name);
1994 pmu_num--;
1995 }
1996 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1997 return 0;
1998
1999error:
2000 strbuf_release(&sb);
2001 return -1;
2002}
2003
Namhyung Kima8bb5592013-01-22 18:09:31 +09002004static int process_group_desc(struct perf_file_section *section __maybe_unused,
2005 struct perf_header *ph, int fd,
2006 void *data __maybe_unused)
2007{
2008 size_t ret = -1;
2009 u32 i, nr, nr_groups;
2010 struct perf_session *session;
2011 struct perf_evsel *evsel, *leader = NULL;
2012 struct group_desc {
2013 char *name;
2014 u32 leader_idx;
2015 u32 nr_members;
2016 } *desc;
2017
2018 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
2019 return -1;
2020
2021 if (ph->needs_swap)
2022 nr_groups = bswap_32(nr_groups);
2023
2024 ph->env.nr_groups = nr_groups;
2025 if (!nr_groups) {
2026 pr_debug("group desc not available\n");
2027 return 0;
2028 }
2029
2030 desc = calloc(nr_groups, sizeof(*desc));
2031 if (!desc)
2032 return -1;
2033
2034 for (i = 0; i < nr_groups; i++) {
2035 desc[i].name = do_read_string(fd, ph);
2036 if (!desc[i].name)
2037 goto out_free;
2038
2039 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
2040 goto out_free;
2041
2042 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
2043 goto out_free;
2044
2045 if (ph->needs_swap) {
2046 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
2047 desc[i].nr_members = bswap_32(desc[i].nr_members);
2048 }
2049 }
2050
2051 /*
2052 * Rebuild group relationship based on the group_desc
2053 */
2054 session = container_of(ph, struct perf_session, header);
2055 session->evlist->nr_groups = nr_groups;
2056
2057 i = nr = 0;
2058 list_for_each_entry(evsel, &session->evlist->entries, node) {
2059 if (evsel->idx == (int) desc[i].leader_idx) {
2060 evsel->leader = evsel;
2061 /* {anon_group} is a dummy name */
2062 if (strcmp(desc[i].name, "{anon_group}"))
2063 evsel->group_name = desc[i].name;
2064 evsel->nr_members = desc[i].nr_members;
2065
2066 if (i >= nr_groups || nr > 0) {
2067 pr_debug("invalid group desc\n");
2068 goto out_free;
2069 }
2070
2071 leader = evsel;
2072 nr = evsel->nr_members - 1;
2073 i++;
2074 } else if (nr) {
2075 /* This is a group member */
2076 evsel->leader = leader;
2077
2078 nr--;
2079 }
2080 }
2081
2082 if (i != nr_groups || nr != 0) {
2083 pr_debug("invalid group desc\n");
2084 goto out_free;
2085 }
2086
2087 ret = 0;
2088out_free:
2089 while ((int) --i >= 0)
2090 free(desc[i].name);
2091 free(desc);
2092
2093 return ret;
2094}
2095
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002096struct feature_ops {
2097 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2098 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01002099 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002100 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002101 const char *name;
2102 bool full_only;
2103};
2104
Robert Richter8cdfa782011-12-07 10:02:56 +01002105#define FEAT_OPA(n, func) \
2106 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01002107#define FEAT_OPP(n, func) \
2108 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2109 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01002110#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01002111 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09002112 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01002113
2114/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002115#define print_tracing_data NULL
2116#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002117
2118static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002119 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01002120 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002121 FEAT_OPP(HEADER_HOSTNAME, hostname),
2122 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2123 FEAT_OPP(HEADER_VERSION, version),
2124 FEAT_OPP(HEADER_ARCH, arch),
2125 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2126 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09002127 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002128 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02002129 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002130 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01002131 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2132 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01002133 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002134 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09002135 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002136};
2137
2138struct header_print_data {
2139 FILE *fp;
2140 bool full; /* extended list of headers */
2141};
2142
2143static int perf_file_section__fprintf_info(struct perf_file_section *section,
2144 struct perf_header *ph,
2145 int feat, int fd, void *data)
2146{
2147 struct header_print_data *hd = data;
2148
2149 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2150 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2151 "%d, continuing...\n", section->offset, feat);
2152 return 0;
2153 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002154 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002155 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002156 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002157 }
2158 if (!feat_ops[feat].print)
2159 return 0;
2160
2161 if (!feat_ops[feat].full_only || hd->full)
2162 feat_ops[feat].print(ph, fd, hd->fp);
2163 else
2164 fprintf(hd->fp, "# %s info available, use -I to display\n",
2165 feat_ops[feat].name);
2166
2167 return 0;
2168}
2169
2170int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2171{
2172 struct header_print_data hd;
2173 struct perf_header *header = &session->header;
2174 int fd = session->fd;
2175 hd.fp = fp;
2176 hd.full = full;
2177
2178 perf_header__process_sections(header, fd, &hd,
2179 perf_file_section__fprintf_info);
2180 return 0;
2181}
2182
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002183static int do_write_feat(int fd, struct perf_header *h, int type,
2184 struct perf_file_section **p,
2185 struct perf_evlist *evlist)
2186{
2187 int err;
2188 int ret = 0;
2189
2190 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002191 if (!feat_ops[type].write)
2192 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002193
2194 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2195
2196 err = feat_ops[type].write(fd, h, evlist);
2197 if (err < 0) {
2198 pr_debug("failed to write feature %d\n", type);
2199
2200 /* undo anything written */
2201 lseek(fd, (*p)->offset, SEEK_SET);
2202
2203 return -1;
2204 }
2205 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2206 (*p)++;
2207 }
2208 return ret;
2209}
2210
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002211static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002212 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002213{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002214 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002215 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002216 int sec_size;
2217 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002218 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002219 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002220
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002221 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002222 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002223 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002224
Paul Gortmaker91b98802013-01-30 20:05:49 -05002225 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002226 if (feat_sec == NULL)
2227 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002228
2229 sec_size = sizeof(*feat_sec) * nr_sections;
2230
Jiri Olsa8d541e92013-07-17 19:49:44 +02002231 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002232 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002233
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002234 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2235 if (do_write_feat(fd, header, feat, &p, evlist))
2236 perf_header__clear_feat(header, feat);
2237 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002238
Xiao Guangrongf887f302010-02-04 16:46:42 +08002239 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002240 /*
2241 * may write more than needed due to dropped feature, but
2242 * this is okay, reader will skip the mising entries
2243 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002244 err = do_write(fd, feat_sec, sec_size);
2245 if (err < 0)
2246 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002247 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002248 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002249}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002250
Tom Zanussi8dc58102010-04-01 23:59:15 -05002251int perf_header__write_pipe(int fd)
2252{
2253 struct perf_pipe_file_header f_header;
2254 int err;
2255
2256 f_header = (struct perf_pipe_file_header){
2257 .magic = PERF_MAGIC,
2258 .size = sizeof(f_header),
2259 };
2260
2261 err = do_write(fd, &f_header, sizeof(f_header));
2262 if (err < 0) {
2263 pr_debug("failed to write perf pipe header\n");
2264 return err;
2265 }
2266
2267 return 0;
2268}
2269
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002270int perf_session__write_header(struct perf_session *session,
2271 struct perf_evlist *evlist,
2272 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002273{
2274 struct perf_file_header f_header;
2275 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002276 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002277 struct perf_evsel *evsel;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002278 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002279 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002280
2281 lseek(fd, sizeof(f_header), SEEK_SET);
2282
Robert Richter6606f872012-08-16 21:10:19 +02002283 list_for_each_entry(evsel, &evlist->entries, node) {
2284 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2285 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002286 if (err < 0) {
2287 pr_debug("failed to write perf header\n");
2288 return err;
2289 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002290 }
2291
Jiri Olsa944d62b2013-07-17 19:49:43 +02002292 attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002293
Robert Richter6606f872012-08-16 21:10:19 +02002294 list_for_each_entry(evsel, &evlist->entries, node) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002295 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002296 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002297 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002298 .offset = evsel->id_offset,
2299 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002300 }
2301 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002302 err = do_write(fd, &f_attr, sizeof(f_attr));
2303 if (err < 0) {
2304 pr_debug("failed to write perf header attribute\n");
2305 return err;
2306 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002307 }
2308
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002309 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002310 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002311
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002312 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002313 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002314 if (err < 0)
2315 return err;
2316 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002317
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002318 f_header = (struct perf_file_header){
2319 .magic = PERF_MAGIC,
2320 .size = sizeof(f_header),
2321 .attr_size = sizeof(f_attr),
2322 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002323 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002324 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002325 },
2326 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002327 .offset = header->data_offset,
2328 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002329 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002330 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002331 };
2332
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002333 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002334
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002335 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002336 err = do_write(fd, &f_header, sizeof(f_header));
2337 if (err < 0) {
2338 pr_debug("failed to write perf header\n");
2339 return err;
2340 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002341 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002342
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002343 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002344}
2345
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002346static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002347 int fd, void *buf, size_t size)
2348{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002349 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002350 return -1;
2351
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002352 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002353 mem_bswap_64(buf, size);
2354
2355 return 0;
2356}
2357
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002358int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002359 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002360 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002361 struct perf_header *ph,
2362 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002363{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002364 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002365 int nr_sections;
2366 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002367 int feat;
2368 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002369
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002370 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002371 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002372 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002373
Paul Gortmaker91b98802013-01-30 20:05:49 -05002374 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002375 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002376 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002377
2378 sec_size = sizeof(*feat_sec) * nr_sections;
2379
Jiri Olsa8d541e92013-07-17 19:49:44 +02002380 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002381
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002382 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2383 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002384 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002385
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002386 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2387 err = process(sec++, header, feat, fd, data);
2388 if (err < 0)
2389 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002390 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002391 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002392out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002393 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002394 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002395}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002396
Stephane Eranian114382a2012-02-09 23:21:08 +01002397static const int attr_file_abi_sizes[] = {
2398 [0] = PERF_ATTR_SIZE_VER0,
2399 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002400 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002401 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian114382a2012-02-09 23:21:08 +01002402 0,
2403};
2404
2405/*
2406 * In the legacy file format, the magic number is not used to encode endianness.
2407 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2408 * on ABI revisions, we need to try all combinations for all endianness to
2409 * detect the endianness.
2410 */
2411static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2412{
2413 uint64_t ref_size, attr_size;
2414 int i;
2415
2416 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2417 ref_size = attr_file_abi_sizes[i]
2418 + sizeof(struct perf_file_section);
2419 if (hdr_sz != ref_size) {
2420 attr_size = bswap_64(hdr_sz);
2421 if (attr_size != ref_size)
2422 continue;
2423
2424 ph->needs_swap = true;
2425 }
2426 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2427 i,
2428 ph->needs_swap);
2429 return 0;
2430 }
2431 /* could not determine endianness */
2432 return -1;
2433}
2434
2435#define PERF_PIPE_HDR_VER0 16
2436
2437static const size_t attr_pipe_abi_sizes[] = {
2438 [0] = PERF_PIPE_HDR_VER0,
2439 0,
2440};
2441
2442/*
2443 * In the legacy pipe format, there is an implicit assumption that endiannesss
2444 * between host recording the samples, and host parsing the samples is the
2445 * same. This is not always the case given that the pipe output may always be
2446 * redirected into a file and analyzed on a different machine with possibly a
2447 * different endianness and perf_event ABI revsions in the perf tool itself.
2448 */
2449static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2450{
2451 u64 attr_size;
2452 int i;
2453
2454 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2455 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2456 attr_size = bswap_64(hdr_sz);
2457 if (attr_size != hdr_sz)
2458 continue;
2459
2460 ph->needs_swap = true;
2461 }
2462 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2463 return 0;
2464 }
2465 return -1;
2466}
2467
Feng Tange84ba4e2012-10-30 11:56:07 +08002468bool is_perf_magic(u64 magic)
2469{
2470 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2471 || magic == __perf_magic2
2472 || magic == __perf_magic2_sw)
2473 return true;
2474
2475 return false;
2476}
2477
Stephane Eranian114382a2012-02-09 23:21:08 +01002478static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2479 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002480{
2481 int ret;
2482
2483 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002484 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002485 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002486 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002487 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002488 if (is_pipe)
2489 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002490
Stephane Eranian114382a2012-02-09 23:21:08 +01002491 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002492 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002493 /*
2494 * the new magic number serves two purposes:
2495 * - unique number to identify actual perf.data files
2496 * - encode endianness of file
2497 */
Stephane Eranian73323f52012-02-02 13:54:44 +01002498
Stephane Eranian114382a2012-02-09 23:21:08 +01002499 /* check magic number with one endianness */
2500 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002501 return 0;
2502
Stephane Eranian114382a2012-02-09 23:21:08 +01002503 /* check magic number with opposite endianness */
2504 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002505 return -1;
2506
2507 ph->needs_swap = true;
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002508 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002509
2510 return 0;
2511}
2512
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002513int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002514 struct perf_header *ph, int fd)
2515{
Stephane Eranian73323f52012-02-02 13:54:44 +01002516 int ret;
2517
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002518 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002519
Stephane Eranian73323f52012-02-02 13:54:44 +01002520 ret = readn(fd, header, sizeof(*header));
2521 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002522 return -1;
2523
Stephane Eranian114382a2012-02-09 23:21:08 +01002524 if (check_magic_endian(header->magic,
2525 header->attr_size, false, ph) < 0) {
2526 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002527 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002528 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002529
Stephane Eranian73323f52012-02-02 13:54:44 +01002530 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002531 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002532 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002533 }
2534
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002535 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002536 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002537 if (header->size == offsetof(typeof(*header), adds_features))
2538 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002539 else
2540 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002541 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002542 /*
2543 * feature bitmap is declared as an array of unsigned longs --
2544 * not good since its size can differ between the host that
2545 * generated the data file and the host analyzing the file.
2546 *
2547 * We need to handle endianness, but we don't know the size of
2548 * the unsigned long where the file was generated. Take a best
2549 * guess at determining it: try 64-bit swap first (ie., file
2550 * created on a 64-bit host), and check if the hostname feature
2551 * bit is set (this feature bit is forced on as of fbe96f2).
2552 * If the bit is not, undo the 64-bit swap and try a 32-bit
2553 * swap. If the hostname bit is still not set (e.g., older data
2554 * file), punt and fallback to the original behavior --
2555 * clearing all feature bits and setting buildid.
2556 */
David Ahern80c01202012-06-08 11:47:51 -03002557 mem_bswap_64(&header->adds_features,
2558 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002559
2560 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002561 /* unswap as u64 */
2562 mem_bswap_64(&header->adds_features,
2563 BITS_TO_U64(HEADER_FEAT_BITS));
2564
2565 /* unswap as u32 */
2566 mem_bswap_32(&header->adds_features,
2567 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002568 }
2569
2570 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2571 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2572 set_bit(HEADER_BUILD_ID, header->adds_features);
2573 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002574 }
2575
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002576 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002577 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002578
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002579 ph->data_offset = header->data.offset;
2580 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002581 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002582 return 0;
2583}
2584
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002585static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002586 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002587 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002588{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002589 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002590 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002591 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002592 return 0;
2593 }
2594
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002595 if (feat >= HEADER_LAST_FEATURE) {
2596 pr_debug("unknown feature %d, continuing...\n", feat);
2597 return 0;
2598 }
2599
Robert Richterf1c67db2012-02-10 15:41:56 +01002600 if (!feat_ops[feat].process)
2601 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002602
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002603 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002604}
2605
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002606static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002607 struct perf_header *ph, int fd,
2608 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002609{
Stephane Eranian73323f52012-02-02 13:54:44 +01002610 int ret;
2611
2612 ret = readn(fd, header, sizeof(*header));
2613 if (ret <= 0)
2614 return -1;
2615
Stephane Eranian114382a2012-02-09 23:21:08 +01002616 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2617 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002618 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002619 }
2620
2621 if (ph->needs_swap)
2622 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002623
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002624 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002625 return -1;
2626
Tom Zanussi8dc58102010-04-01 23:59:15 -05002627 return 0;
2628}
2629
Jiri Olsad4339562013-07-17 19:49:41 +02002630static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002631{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002632 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002633 struct perf_pipe_file_header f_header;
2634
Jiri Olsad4339562013-07-17 19:49:41 +02002635 if (perf_file_header__read_pipe(&f_header, header, session->fd,
Tom Zanussi454c4072010-05-01 01:41:20 -05002636 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002637 pr_debug("incompatible file format\n");
2638 return -EINVAL;
2639 }
2640
Tom Zanussi8dc58102010-04-01 23:59:15 -05002641 return 0;
2642}
2643
Stephane Eranian69996df2012-02-09 23:21:06 +01002644static int read_attr(int fd, struct perf_header *ph,
2645 struct perf_file_attr *f_attr)
2646{
2647 struct perf_event_attr *attr = &f_attr->attr;
2648 size_t sz, left;
2649 size_t our_sz = sizeof(f_attr->attr);
2650 int ret;
2651
2652 memset(f_attr, 0, sizeof(*f_attr));
2653
2654 /* read minimal guaranteed structure */
2655 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2656 if (ret <= 0) {
2657 pr_debug("cannot read %d bytes of header attr\n",
2658 PERF_ATTR_SIZE_VER0);
2659 return -1;
2660 }
2661
2662 /* on file perf_event_attr size */
2663 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002664
Stephane Eranian69996df2012-02-09 23:21:06 +01002665 if (ph->needs_swap)
2666 sz = bswap_32(sz);
2667
2668 if (sz == 0) {
2669 /* assume ABI0 */
2670 sz = PERF_ATTR_SIZE_VER0;
2671 } else if (sz > our_sz) {
2672 pr_debug("file uses a more recent and unsupported ABI"
2673 " (%zu bytes extra)\n", sz - our_sz);
2674 return -1;
2675 }
2676 /* what we have not yet read and that we know about */
2677 left = sz - PERF_ATTR_SIZE_VER0;
2678 if (left) {
2679 void *ptr = attr;
2680 ptr += PERF_ATTR_SIZE_VER0;
2681
2682 ret = readn(fd, ptr, left);
2683 }
2684 /* read perf_file_section, ids are read in caller */
2685 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2686
2687 return ret <= 0 ? -1 : 0;
2688}
2689
Namhyung Kim831394b2012-09-06 11:10:46 +09002690static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2691 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002692{
Namhyung Kim831394b2012-09-06 11:10:46 +09002693 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002694 char bf[128];
2695
Namhyung Kim831394b2012-09-06 11:10:46 +09002696 /* already prepared */
2697 if (evsel->tp_format)
2698 return 0;
2699
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002700 if (pevent == NULL) {
2701 pr_debug("broken or missing trace data\n");
2702 return -1;
2703 }
2704
Namhyung Kim831394b2012-09-06 11:10:46 +09002705 event = pevent_find_event(pevent, evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002706 if (event == NULL)
2707 return -1;
2708
Namhyung Kim831394b2012-09-06 11:10:46 +09002709 if (!evsel->name) {
2710 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2711 evsel->name = strdup(bf);
2712 if (evsel->name == NULL)
2713 return -1;
2714 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002715
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002716 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002717 return 0;
2718}
2719
Namhyung Kim831394b2012-09-06 11:10:46 +09002720static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2721 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002722{
2723 struct perf_evsel *pos;
2724
2725 list_for_each_entry(pos, &evlist->entries, node) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002726 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2727 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002728 return -1;
2729 }
2730
2731 return 0;
2732}
2733
Jiri Olsad4339562013-07-17 19:49:41 +02002734int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002735{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002736 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002737 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002738 struct perf_file_attr f_attr;
2739 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002740 int nr_attrs, nr_ids, i, j;
Jiri Olsad4339562013-07-17 19:49:41 +02002741 int fd = session->fd;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002742
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002743 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002744 if (session->evlist == NULL)
2745 return -ENOMEM;
2746
Tom Zanussi8dc58102010-04-01 23:59:15 -05002747 if (session->fd_pipe)
Jiri Olsad4339562013-07-17 19:49:41 +02002748 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002749
Stephane Eranian69996df2012-02-09 23:21:06 +01002750 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002751 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002752
Stephane Eranian69996df2012-02-09 23:21:06 +01002753 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002754 lseek(fd, f_header.attrs.offset, SEEK_SET);
2755
2756 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002757 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002758 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002759
Stephane Eranian69996df2012-02-09 23:21:06 +01002760 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002761 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002762
David Aherneda39132011-07-15 12:34:09 -06002763 if (header->needs_swap)
2764 perf_event__attr_swap(&f_attr.attr);
2765
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002766 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002767 evsel = perf_evsel__new(&f_attr.attr, i);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002768
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002769 if (evsel == NULL)
2770 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002771
2772 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002773 /*
2774 * Do it before so that if perf_evsel__alloc_id fails, this
2775 * entry gets purged too at perf_evlist__delete().
2776 */
2777 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002778
2779 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002780 /*
2781 * We don't have the cpu and thread maps on the header, so
2782 * for allocating the perf_sample_id table we fake 1 cpu and
2783 * hattr->ids threads.
2784 */
2785 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2786 goto out_delete_evlist;
2787
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002788 lseek(fd, f_attr.ids.offset, SEEK_SET);
2789
2790 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002791 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002792 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002793
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002794 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002795 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002796
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002797 lseek(fd, tmp, SEEK_SET);
2798 }
2799
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002800 symbol_conf.nr_events = nr_attrs;
2801
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002802 perf_header__process_sections(header, fd, &session->pevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002803 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002804
Namhyung Kim831394b2012-09-06 11:10:46 +09002805 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2806 session->pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002807 goto out_delete_evlist;
2808
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002809 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002810out_errno:
2811 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002812
2813out_delete_evlist:
2814 perf_evlist__delete(session->evlist);
2815 session->evlist = NULL;
2816 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002817}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002818
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002819int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002820 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002821 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002822{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002823 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002824 size_t size;
2825 int err;
2826
2827 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002828 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002829 size += sizeof(struct perf_event_header);
2830 size += ids * sizeof(u64);
2831
2832 ev = malloc(size);
2833
Chris Samuelce47dc52010-11-13 13:35:06 +11002834 if (ev == NULL)
2835 return -ENOMEM;
2836
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002837 ev->attr.attr = *attr;
2838 memcpy(ev->attr.id, id, ids * sizeof(u64));
2839
2840 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002841 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002842
Robert Richterf4d83432012-08-16 21:10:17 +02002843 if (ev->attr.header.size == size)
2844 err = process(tool, ev, NULL, NULL);
2845 else
2846 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002847
2848 free(ev);
2849
2850 return err;
2851}
2852
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002853int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002854 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002855 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002856{
Robert Richter6606f872012-08-16 21:10:19 +02002857 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002858 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002859
Robert Richter6606f872012-08-16 21:10:19 +02002860 list_for_each_entry(evsel, &session->evlist->entries, node) {
2861 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2862 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002863 if (err) {
2864 pr_debug("failed to create perf header attribute\n");
2865 return err;
2866 }
2867 }
2868
2869 return err;
2870}
2871
Adrian Hunter47c3d102013-07-04 16:20:21 +03002872int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2873 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002874 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002875{
Robert Richterf4d83432012-08-16 21:10:17 +02002876 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002877 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002878 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002879
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002880 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002881 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002882 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002883 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002884 }
2885
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002886 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002887 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002888 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002889
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002890 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002891
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002892 ids = event->header.size;
2893 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002894 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002895 /*
2896 * We don't have the cpu and thread maps on the header, so
2897 * for allocating the perf_sample_id table we fake 1 cpu and
2898 * hattr->ids threads.
2899 */
2900 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2901 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002902
2903 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002904 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002905 }
2906
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03002907 symbol_conf.nr_events = evlist->nr_entries;
2908
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002909 return 0;
2910}
Tom Zanussicd19a032010-04-01 23:59:20 -05002911
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002912int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002913 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002914 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05002915{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002916 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02002917 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05002918 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002919 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05002920
Jiri Olsa29208e52011-10-20 15:59:43 +02002921 /*
2922 * We are going to store the size of the data followed
2923 * by the data contents. Since the fd descriptor is a pipe,
2924 * we cannot seek back to store the size of the data once
2925 * we know it. Instead we:
2926 *
2927 * - write the tracing data to the temp file
2928 * - get/write the data size to pipe
2929 * - write the tracing data from the temp file
2930 * to the pipe
2931 */
2932 tdata = tracing_data_get(&evlist->entries, fd, true);
2933 if (!tdata)
2934 return -1;
2935
Tom Zanussi92155452010-04-01 23:59:21 -05002936 memset(&ev, 0, sizeof(ev));
2937
2938 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02002939 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002940 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05002941 padding = aligned_size - size;
2942 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2943 ev.tracing_data.size = aligned_size;
2944
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002945 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05002946
Jiri Olsa29208e52011-10-20 15:59:43 +02002947 /*
2948 * The put function will copy all the tracing data
2949 * stored in temp file to the pipe.
2950 */
2951 tracing_data_put(tdata);
2952
Tom Zanussi92155452010-04-01 23:59:21 -05002953 write_padded(fd, NULL, 0, padding);
2954
2955 return aligned_size;
2956}
2957
Adrian Hunter47c3d102013-07-04 16:20:21 +03002958int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
2959 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002960 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05002961{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002962 ssize_t size_read, padding, size = event->tracing_data.size;
Tom Zanussi92155452010-04-01 23:59:21 -05002963 off_t offset = lseek(session->fd, 0, SEEK_CUR);
2964 char buf[BUFSIZ];
2965
2966 /* setup for reading amidst mmap */
2967 lseek(session->fd, offset + sizeof(struct tracing_data_event),
2968 SEEK_SET);
2969
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002970 size_read = trace_report(session->fd, &session->pevent,
2971 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002972 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05002973
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002974 if (readn(session->fd, buf, padding) < 0) {
2975 pr_err("%s: reading input file", __func__);
2976 return -1;
2977 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002978 if (session->repipe) {
2979 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002980 if (retw <= 0 || retw != padding) {
2981 pr_err("%s: repiping tracing data padding", __func__);
2982 return -1;
2983 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002984 }
Tom Zanussi92155452010-04-01 23:59:21 -05002985
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002986 if (size_read + padding != size) {
2987 pr_err("%s: tracing data size mismatch", __func__);
2988 return -1;
2989 }
Tom Zanussi92155452010-04-01 23:59:21 -05002990
Namhyung Kim831394b2012-09-06 11:10:46 +09002991 perf_evlist__prepare_tracepoint_events(session->evlist,
2992 session->pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03002993
Tom Zanussi92155452010-04-01 23:59:21 -05002994 return size_read + padding;
2995}
Tom Zanussic7929e42010-04-01 23:59:22 -05002996
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002997int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002998 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002999 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003000 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003001{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003002 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003003 size_t len;
3004 int err = 0;
3005
3006 if (!pos->hit)
3007 return err;
3008
3009 memset(&ev, 0, sizeof(ev));
3010
3011 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003012 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003013 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3014 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3015 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003016 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003017 ev.build_id.header.size = sizeof(ev.build_id) + len;
3018 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3019
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003020 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003021
3022 return err;
3023}
3024
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003025int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003026 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003027 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003028{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003029 __event_process_build_id(&event->build_id,
3030 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003031 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003032 return 0;
3033}
Stephane Eraniana1ac1d32010-06-17 11:39:01 +02003034
3035void disable_buildid_cache(void)
3036{
3037 no_buildid_cache = true;
3038}