blob: f558f83769af1a7e7b9fe0780ab4c6b4793f260c [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;
719 u32 i = 0;
720 int ret = -1;
721
722 sprintf(filename, CORE_SIB_FMT, cpu);
723 fp = fopen(filename, "r");
724 if (!fp)
725 return -1;
726
727 if (getline(&buf, &len, fp) <= 0)
728 goto done;
729
730 fclose(fp);
731
732 p = strchr(buf, '\n');
733 if (p)
734 *p = '\0';
735
736 for (i = 0; i < tp->core_sib; i++) {
737 if (!strcmp(buf, tp->core_siblings[i]))
738 break;
739 }
740 if (i == tp->core_sib) {
741 tp->core_siblings[i] = buf;
742 tp->core_sib++;
743 buf = NULL;
744 len = 0;
745 }
746
747 sprintf(filename, THRD_SIB_FMT, cpu);
748 fp = fopen(filename, "r");
749 if (!fp)
750 goto done;
751
752 if (getline(&buf, &len, fp) <= 0)
753 goto done;
754
755 p = strchr(buf, '\n');
756 if (p)
757 *p = '\0';
758
759 for (i = 0; i < tp->thread_sib; i++) {
760 if (!strcmp(buf, tp->thread_siblings[i]))
761 break;
762 }
763 if (i == tp->thread_sib) {
764 tp->thread_siblings[i] = buf;
765 tp->thread_sib++;
766 buf = NULL;
767 }
768 ret = 0;
769done:
770 if(fp)
771 fclose(fp);
772 free(buf);
773 return ret;
774}
775
776static void free_cpu_topo(struct cpu_topo *tp)
777{
778 u32 i;
779
780 if (!tp)
781 return;
782
783 for (i = 0 ; i < tp->core_sib; i++)
784 free(tp->core_siblings[i]);
785
786 for (i = 0 ; i < tp->thread_sib; i++)
787 free(tp->thread_siblings[i]);
788
789 free(tp);
790}
791
792static struct cpu_topo *build_cpu_topology(void)
793{
794 struct cpu_topo *tp;
795 void *addr;
796 u32 nr, i;
797 size_t sz;
798 long ncpus;
799 int ret = -1;
800
801 ncpus = sysconf(_SC_NPROCESSORS_CONF);
802 if (ncpus < 0)
803 return NULL;
804
805 nr = (u32)(ncpus & UINT_MAX);
806
807 sz = nr * sizeof(char *);
808
809 addr = calloc(1, sizeof(*tp) + 2 * sz);
810 if (!addr)
811 return NULL;
812
813 tp = addr;
814
815 addr += sizeof(*tp);
816 tp->core_siblings = addr;
817 addr += sz;
818 tp->thread_siblings = addr;
819
820 for (i = 0; i < nr; i++) {
821 ret = build_cpu_topo(tp, i);
822 if (ret < 0)
823 break;
824 }
825 if (ret) {
826 free_cpu_topo(tp);
827 tp = NULL;
828 }
829 return tp;
830}
831
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300832static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
833 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200834{
835 struct cpu_topo *tp;
836 u32 i;
837 int ret;
838
839 tp = build_cpu_topology();
840 if (!tp)
841 return -1;
842
843 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
844 if (ret < 0)
845 goto done;
846
847 for (i = 0; i < tp->core_sib; i++) {
848 ret = do_write_string(fd, tp->core_siblings[i]);
849 if (ret < 0)
850 goto done;
851 }
852 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
853 if (ret < 0)
854 goto done;
855
856 for (i = 0; i < tp->thread_sib; i++) {
857 ret = do_write_string(fd, tp->thread_siblings[i]);
858 if (ret < 0)
859 break;
860 }
861done:
862 free_cpu_topo(tp);
863 return ret;
864}
865
866
867
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300868static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
869 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200870{
871 char *buf = NULL;
872 FILE *fp;
873 size_t len = 0;
874 int ret = -1, n;
875 uint64_t mem;
876
877 fp = fopen("/proc/meminfo", "r");
878 if (!fp)
879 return -1;
880
881 while (getline(&buf, &len, fp) > 0) {
882 ret = strncmp(buf, "MemTotal:", 9);
883 if (!ret)
884 break;
885 }
886 if (!ret) {
887 n = sscanf(buf, "%*s %"PRIu64, &mem);
888 if (n == 1)
889 ret = do_write(fd, &mem, sizeof(mem));
890 }
891 free(buf);
892 fclose(fp);
893 return ret;
894}
895
896static int write_topo_node(int fd, int node)
897{
898 char str[MAXPATHLEN];
899 char field[32];
900 char *buf = NULL, *p;
901 size_t len = 0;
902 FILE *fp;
903 u64 mem_total, mem_free, mem;
904 int ret = -1;
905
906 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
907 fp = fopen(str, "r");
908 if (!fp)
909 return -1;
910
911 while (getline(&buf, &len, fp) > 0) {
912 /* skip over invalid lines */
913 if (!strchr(buf, ':'))
914 continue;
915 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
916 goto done;
917 if (!strcmp(field, "MemTotal:"))
918 mem_total = mem;
919 if (!strcmp(field, "MemFree:"))
920 mem_free = mem;
921 }
922
923 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100924 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200925
926 ret = do_write(fd, &mem_total, sizeof(u64));
927 if (ret)
928 goto done;
929
930 ret = do_write(fd, &mem_free, sizeof(u64));
931 if (ret)
932 goto done;
933
934 ret = -1;
935 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
936
937 fp = fopen(str, "r");
938 if (!fp)
939 goto done;
940
941 if (getline(&buf, &len, fp) <= 0)
942 goto done;
943
944 p = strchr(buf, '\n');
945 if (p)
946 *p = '\0';
947
948 ret = do_write_string(fd, buf);
949done:
950 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100951 if (fp)
952 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200953 return ret;
954}
955
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300956static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
957 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200958{
959 char *buf = NULL;
960 size_t len = 0;
961 FILE *fp;
962 struct cpu_map *node_map = NULL;
963 char *c;
964 u32 nr, i, j;
965 int ret = -1;
966
967 fp = fopen("/sys/devices/system/node/online", "r");
968 if (!fp)
969 return -1;
970
971 if (getline(&buf, &len, fp) <= 0)
972 goto done;
973
974 c = strchr(buf, '\n');
975 if (c)
976 *c = '\0';
977
978 node_map = cpu_map__new(buf);
979 if (!node_map)
980 goto done;
981
982 nr = (u32)node_map->nr;
983
984 ret = do_write(fd, &nr, sizeof(nr));
985 if (ret < 0)
986 goto done;
987
988 for (i = 0; i < nr; i++) {
989 j = (u32)node_map->map[i];
990 ret = do_write(fd, &j, sizeof(j));
991 if (ret < 0)
992 break;
993
994 ret = write_topo_node(fd, i);
995 if (ret < 0)
996 break;
997 }
998done:
999 free(buf);
1000 fclose(fp);
1001 free(node_map);
1002 return ret;
1003}
1004
1005/*
Robert Richter50a96672012-08-16 21:10:24 +02001006 * File format:
1007 *
1008 * struct pmu_mappings {
1009 * u32 pmu_num;
1010 * struct pmu_map {
1011 * u32 type;
1012 * char name[];
1013 * }[pmu_num];
1014 * };
1015 */
1016
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001017static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
1018 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +02001019{
1020 struct perf_pmu *pmu = NULL;
1021 off_t offset = lseek(fd, 0, SEEK_CUR);
1022 __u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +09001023 int ret;
Robert Richter50a96672012-08-16 21:10:24 +02001024
1025 /* write real pmu_num later */
Namhyung Kim5323f602012-12-17 15:38:54 +09001026 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
1027 if (ret < 0)
1028 return ret;
Robert Richter50a96672012-08-16 21:10:24 +02001029
1030 while ((pmu = perf_pmu__scan(pmu))) {
1031 if (!pmu->name)
1032 continue;
1033 pmu_num++;
Namhyung Kim5323f602012-12-17 15:38:54 +09001034
1035 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
1036 if (ret < 0)
1037 return ret;
1038
1039 ret = do_write_string(fd, pmu->name);
1040 if (ret < 0)
1041 return ret;
Robert Richter50a96672012-08-16 21:10:24 +02001042 }
1043
1044 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1045 /* discard all */
1046 lseek(fd, offset, SEEK_SET);
1047 return -1;
1048 }
1049
1050 return 0;
1051}
1052
1053/*
Namhyung Kima8bb5592013-01-22 18:09:31 +09001054 * File format:
1055 *
1056 * struct group_descs {
1057 * u32 nr_groups;
1058 * struct group_desc {
1059 * char name[];
1060 * u32 leader_idx;
1061 * u32 nr_members;
1062 * }[nr_groups];
1063 * };
1064 */
1065static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
1066 struct perf_evlist *evlist)
1067{
1068 u32 nr_groups = evlist->nr_groups;
1069 struct perf_evsel *evsel;
1070 int ret;
1071
1072 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
1073 if (ret < 0)
1074 return ret;
1075
1076 list_for_each_entry(evsel, &evlist->entries, node) {
1077 if (perf_evsel__is_group_leader(evsel) &&
1078 evsel->nr_members > 1) {
1079 const char *name = evsel->group_name ?: "{anon_group}";
1080 u32 leader_idx = evsel->idx;
1081 u32 nr_members = evsel->nr_members;
1082
1083 ret = do_write_string(fd, name);
1084 if (ret < 0)
1085 return ret;
1086
1087 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
1088 if (ret < 0)
1089 return ret;
1090
1091 ret = do_write(fd, &nr_members, sizeof(nr_members));
1092 if (ret < 0)
1093 return ret;
1094 }
1095 }
1096 return 0;
1097}
1098
1099/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001100 * default get_cpuid(): nothing gets recorded
1101 * actual implementation must be in arch/$(ARCH)/util/header.c
1102 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001103int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
1104 size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001105{
1106 return -1;
1107}
1108
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001109static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
1110 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001111{
1112 char buffer[64];
1113 int ret;
1114
1115 ret = get_cpuid(buffer, sizeof(buffer));
1116 if (!ret)
1117 goto write_it;
1118
1119 return -1;
1120write_it:
1121 return do_write_string(fd, buffer);
1122}
1123
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001124static int write_branch_stack(int fd __maybe_unused,
1125 struct perf_header *h __maybe_unused,
1126 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +01001127{
1128 return 0;
1129}
1130
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001131static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1132 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001133{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001134 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001135}
1136
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001137static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1138 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001139{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001140 fprintf(fp, "# os release : %s\n", ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001141}
1142
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001143static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001144{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001145 fprintf(fp, "# arch : %s\n", ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001146}
1147
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001148static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1149 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001150{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001151 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001152}
1153
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001154static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1155 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001156{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001157 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1158 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001159}
1160
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001161static void print_version(struct perf_header *ph, int fd __maybe_unused,
1162 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001163{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001164 fprintf(fp, "# perf version : %s\n", ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001165}
1166
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001167static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1168 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001169{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001170 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001171 char *str;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001172
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001173 nr = ph->env.nr_cmdline;
1174 str = ph->env.cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001175
1176 fprintf(fp, "# cmdline : ");
1177
1178 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001179 fprintf(fp, "%s ", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001180 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001181 }
1182 fputc('\n', fp);
1183}
1184
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001185static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1186 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001187{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001188 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001189 char *str;
1190
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001191 nr = ph->env.nr_sibling_cores;
1192 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001193
1194 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001195 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001196 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001197 }
1198
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001199 nr = ph->env.nr_sibling_threads;
1200 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001201
1202 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001203 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001204 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001205 }
1206}
1207
Robert Richter4e1b9c62012-08-16 21:10:22 +02001208static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001209{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001210 struct perf_evsel *evsel;
1211
1212 if (!events)
1213 return;
1214
1215 for (evsel = events; evsel->attr.size; evsel++) {
1216 if (evsel->name)
1217 free(evsel->name);
1218 if (evsel->id)
1219 free(evsel->id);
1220 }
1221
1222 free(events);
1223}
1224
1225static struct perf_evsel *
1226read_event_desc(struct perf_header *ph, int fd)
1227{
1228 struct perf_evsel *evsel, *events = NULL;
1229 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001230 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001231 u32 nre, sz, nr, i, j;
1232 ssize_t ret;
1233 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001234
1235 /* number of events */
Namhyung Kim5323f602012-12-17 15:38:54 +09001236 ret = readn(fd, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001237 if (ret != (ssize_t)sizeof(nre))
1238 goto error;
1239
1240 if (ph->needs_swap)
1241 nre = bswap_32(nre);
1242
Namhyung Kim5323f602012-12-17 15:38:54 +09001243 ret = readn(fd, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001244 if (ret != (ssize_t)sizeof(sz))
1245 goto error;
1246
1247 if (ph->needs_swap)
1248 sz = bswap_32(sz);
1249
Stephane Eranian62db9062012-02-09 23:21:07 +01001250 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001251 buf = malloc(sz);
1252 if (!buf)
1253 goto error;
1254
Robert Richter4e1b9c62012-08-16 21:10:22 +02001255 /* the last event terminates with evsel->attr.size == 0: */
1256 events = calloc(nre + 1, sizeof(*events));
1257 if (!events)
1258 goto error;
1259
1260 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001261 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001262 msz = sz;
1263
Robert Richter4e1b9c62012-08-16 21:10:22 +02001264 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1265 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001266
Stephane Eranian62db9062012-02-09 23:21:07 +01001267 /*
1268 * must read entire on-file attr struct to
1269 * sync up with layout.
1270 */
Namhyung Kim5323f602012-12-17 15:38:54 +09001271 ret = readn(fd, buf, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001272 if (ret != (ssize_t)sz)
1273 goto error;
1274
1275 if (ph->needs_swap)
1276 perf_event__attr_swap(buf);
1277
Robert Richter4e1b9c62012-08-16 21:10:22 +02001278 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001279
Namhyung Kim5323f602012-12-17 15:38:54 +09001280 ret = readn(fd, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001281 if (ret != (ssize_t)sizeof(nr))
1282 goto error;
1283
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001284 if (ph->needs_swap) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001285 nr = bswap_32(nr);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001286 evsel->needs_swap = true;
1287 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001288
Robert Richter4e1b9c62012-08-16 21:10:22 +02001289 evsel->name = do_read_string(fd, ph);
1290
1291 if (!nr)
1292 continue;
1293
1294 id = calloc(nr, sizeof(*id));
1295 if (!id)
1296 goto error;
1297 evsel->ids = nr;
1298 evsel->id = id;
1299
1300 for (j = 0 ; j < nr; j++) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001301 ret = readn(fd, id, sizeof(*id));
Robert Richter4e1b9c62012-08-16 21:10:22 +02001302 if (ret != (ssize_t)sizeof(*id))
1303 goto error;
1304 if (ph->needs_swap)
1305 *id = bswap_64(*id);
1306 id++;
1307 }
1308 }
1309out:
1310 if (buf)
1311 free(buf);
1312 return events;
1313error:
1314 if (events)
1315 free_event_desc(events);
1316 events = NULL;
1317 goto out;
1318}
1319
1320static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1321{
1322 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1323 u32 j;
1324 u64 *id;
1325
1326 if (!events) {
1327 fprintf(fp, "# event desc: not available or unable to read\n");
1328 return;
1329 }
1330
1331 for (evsel = events; evsel->attr.size; evsel++) {
1332 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001333
1334 fprintf(fp, "type = %d, config = 0x%"PRIx64
1335 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
Robert Richter4e1b9c62012-08-16 21:10:22 +02001336 evsel->attr.type,
1337 (u64)evsel->attr.config,
1338 (u64)evsel->attr.config1,
1339 (u64)evsel->attr.config2);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001340
1341 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001342 evsel->attr.exclude_user,
1343 evsel->attr.exclude_kernel);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001344
David Ahern78b961f2012-07-20 17:25:52 -06001345 fprintf(fp, ", excl_host = %d, excl_guest = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001346 evsel->attr.exclude_host,
1347 evsel->attr.exclude_guest);
David Ahern78b961f2012-07-20 17:25:52 -06001348
Robert Richter4e1b9c62012-08-16 21:10:22 +02001349 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
David Ahern78b961f2012-07-20 17:25:52 -06001350
Robert Richter4e1b9c62012-08-16 21:10:22 +02001351 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001352 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001353 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1354 if (j)
1355 fputc(',', fp);
1356 fprintf(fp, " %"PRIu64, *id);
1357 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001358 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001359 }
1360
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001361 fputc('\n', fp);
1362 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001363
1364 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001365}
1366
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001367static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001368 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001369{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001370 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001371}
1372
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001373static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001374 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001375{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001376 u32 nr, c, i;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001377 char *str, *tmp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001378 uint64_t mem_total, mem_free;
1379
1380 /* nr nodes */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001381 nr = ph->env.nr_numa_nodes;
1382 str = ph->env.numa_nodes;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001383
1384 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001385 /* node number */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001386 c = strtoul(str, &tmp, 0);
1387 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001388 goto error;
1389
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001390 str = tmp + 1;
1391 mem_total = strtoull(str, &tmp, 0);
1392 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001393 goto error;
1394
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001395 str = tmp + 1;
1396 mem_free = strtoull(str, &tmp, 0);
1397 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001398 goto error;
1399
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001400 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1401 " free = %"PRIu64" kB\n",
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001402 c, mem_total, mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001403
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001404 str = tmp + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001405 fprintf(fp, "# node%u cpu list : %s\n", c, str);
Namhyung Kim12344712012-10-23 22:44:49 +09001406
1407 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001408 }
1409 return;
1410error:
1411 fprintf(fp, "# numa topology : not available\n");
1412}
1413
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001414static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001415{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001416 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001417}
1418
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001419static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001420 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001421{
1422 fprintf(fp, "# contains samples with branch stack\n");
1423}
1424
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001425static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1426 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001427{
1428 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001429 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001430 u32 pmu_num;
1431 u32 type;
1432
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001433 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001434 if (!pmu_num) {
1435 fprintf(fp, "# pmu mappings: not available\n");
1436 return;
1437 }
1438
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001439 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001440
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001441 while (pmu_num) {
1442 type = strtoul(str, &tmp, 0);
1443 if (*tmp != ':')
1444 goto error;
1445
1446 str = tmp + 1;
1447 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1448
Robert Richter50a96672012-08-16 21:10:24 +02001449 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001450 str += strlen(str) + 1;
1451 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001452 }
1453
1454 fprintf(fp, "\n");
1455
1456 if (!pmu_num)
1457 return;
1458error:
1459 fprintf(fp, "# pmu mappings: unable to read\n");
1460}
1461
Namhyung Kima8bb5592013-01-22 18:09:31 +09001462static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1463 FILE *fp)
1464{
1465 struct perf_session *session;
1466 struct perf_evsel *evsel;
1467 u32 nr = 0;
1468
1469 session = container_of(ph, struct perf_session, header);
1470
1471 list_for_each_entry(evsel, &session->evlist->entries, node) {
1472 if (perf_evsel__is_group_leader(evsel) &&
1473 evsel->nr_members > 1) {
1474 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1475 perf_evsel__name(evsel));
1476
1477 nr = evsel->nr_members - 1;
1478 } else if (nr) {
1479 fprintf(fp, ",%s", perf_evsel__name(evsel));
1480
1481 if (--nr == 0)
1482 fprintf(fp, "}\n");
1483 }
1484 }
1485}
1486
Robert Richter08d95bd2012-02-10 15:41:55 +01001487static int __event_process_build_id(struct build_id_event *bev,
1488 char *filename,
1489 struct perf_session *session)
1490{
1491 int err = -1;
1492 struct list_head *head;
1493 struct machine *machine;
1494 u16 misc;
1495 struct dso *dso;
1496 enum dso_kernel_type dso_type;
1497
1498 machine = perf_session__findnew_machine(session, bev->pid);
1499 if (!machine)
1500 goto out;
1501
1502 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1503
1504 switch (misc) {
1505 case PERF_RECORD_MISC_KERNEL:
1506 dso_type = DSO_TYPE_KERNEL;
1507 head = &machine->kernel_dsos;
1508 break;
1509 case PERF_RECORD_MISC_GUEST_KERNEL:
1510 dso_type = DSO_TYPE_GUEST_KERNEL;
1511 head = &machine->kernel_dsos;
1512 break;
1513 case PERF_RECORD_MISC_USER:
1514 case PERF_RECORD_MISC_GUEST_USER:
1515 dso_type = DSO_TYPE_USER;
1516 head = &machine->user_dsos;
1517 break;
1518 default:
1519 goto out;
1520 }
1521
1522 dso = __dsos__findnew(head, filename);
1523 if (dso != NULL) {
1524 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1525
1526 dso__set_build_id(dso, &bev->build_id);
1527
1528 if (filename[0] == '[')
1529 dso->kernel = dso_type;
1530
1531 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1532 sbuild_id);
1533 pr_debug("build id event received for %s: %s\n",
1534 dso->long_name, sbuild_id);
1535 }
1536
1537 err = 0;
1538out:
1539 return err;
1540}
1541
1542static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1543 int input, u64 offset, u64 size)
1544{
1545 struct perf_session *session = container_of(header, struct perf_session, header);
1546 struct {
1547 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001548 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001549 char filename[0];
1550 } old_bev;
1551 struct build_id_event bev;
1552 char filename[PATH_MAX];
1553 u64 limit = offset + size;
1554
1555 while (offset < limit) {
1556 ssize_t len;
1557
Namhyung Kim5323f602012-12-17 15:38:54 +09001558 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001559 return -1;
1560
1561 if (header->needs_swap)
1562 perf_event_header__bswap(&old_bev.header);
1563
1564 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001565 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001566 return -1;
1567
1568 bev.header = old_bev.header;
1569
1570 /*
1571 * As the pid is the missing value, we need to fill
1572 * it properly. The header.misc value give us nice hint.
1573 */
1574 bev.pid = HOST_KERNEL_ID;
1575 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1576 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1577 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1578
1579 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1580 __event_process_build_id(&bev, filename, session);
1581
1582 offset += bev.header.size;
1583 }
1584
1585 return 0;
1586}
1587
1588static int perf_header__read_build_ids(struct perf_header *header,
1589 int input, u64 offset, u64 size)
1590{
1591 struct perf_session *session = container_of(header, struct perf_session, header);
1592 struct build_id_event bev;
1593 char filename[PATH_MAX];
1594 u64 limit = offset + size, orig_offset = offset;
1595 int err = -1;
1596
1597 while (offset < limit) {
1598 ssize_t len;
1599
Namhyung Kim5323f602012-12-17 15:38:54 +09001600 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001601 goto out;
1602
1603 if (header->needs_swap)
1604 perf_event_header__bswap(&bev.header);
1605
1606 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001607 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001608 goto out;
1609 /*
1610 * The a1645ce1 changeset:
1611 *
1612 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1613 *
1614 * Added a field to struct build_id_event that broke the file
1615 * format.
1616 *
1617 * Since the kernel build-id is the first entry, process the
1618 * table using the old format if the well known
1619 * '[kernel.kallsyms]' string for the kernel build-id has the
1620 * first 4 characters chopped off (where the pid_t sits).
1621 */
1622 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1623 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1624 return -1;
1625 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1626 }
1627
1628 __event_process_build_id(&bev, filename, session);
1629
1630 offset += bev.header.size;
1631 }
1632 err = 0;
1633out:
1634 return err;
1635}
1636
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001637static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1638 struct perf_header *ph __maybe_unused,
1639 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001640{
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001641 ssize_t ret = trace_report(fd, data, false);
1642 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001643}
1644
1645static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001646 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001647 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001648{
1649 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1650 pr_debug("Failed to read buildids, continuing...\n");
1651 return 0;
1652}
1653
Namhyung Kima1ae5652012-09-24 17:14:59 +09001654static int process_hostname(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001655 struct perf_header *ph, int fd,
1656 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001657{
1658 ph->env.hostname = do_read_string(fd, ph);
1659 return ph->env.hostname ? 0 : -ENOMEM;
1660}
1661
1662static int process_osrelease(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001663 struct perf_header *ph, int fd,
1664 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001665{
1666 ph->env.os_release = do_read_string(fd, ph);
1667 return ph->env.os_release ? 0 : -ENOMEM;
1668}
1669
1670static int process_version(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001671 struct perf_header *ph, int fd,
1672 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001673{
1674 ph->env.version = do_read_string(fd, ph);
1675 return ph->env.version ? 0 : -ENOMEM;
1676}
1677
1678static int process_arch(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001679 struct perf_header *ph, int fd,
1680 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001681{
1682 ph->env.arch = do_read_string(fd, ph);
1683 return ph->env.arch ? 0 : -ENOMEM;
1684}
1685
1686static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001687 struct perf_header *ph, int fd,
1688 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001689{
1690 size_t ret;
1691 u32 nr;
1692
Namhyung Kim5323f602012-12-17 15:38:54 +09001693 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001694 if (ret != sizeof(nr))
1695 return -1;
1696
1697 if (ph->needs_swap)
1698 nr = bswap_32(nr);
1699
1700 ph->env.nr_cpus_online = nr;
1701
Namhyung Kim5323f602012-12-17 15:38:54 +09001702 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001703 if (ret != sizeof(nr))
1704 return -1;
1705
1706 if (ph->needs_swap)
1707 nr = bswap_32(nr);
1708
1709 ph->env.nr_cpus_avail = nr;
1710 return 0;
1711}
1712
1713static int process_cpudesc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001714 struct perf_header *ph, int fd,
1715 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001716{
1717 ph->env.cpu_desc = do_read_string(fd, ph);
1718 return ph->env.cpu_desc ? 0 : -ENOMEM;
1719}
1720
1721static int process_cpuid(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001722 struct perf_header *ph, int fd,
1723 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001724{
1725 ph->env.cpuid = do_read_string(fd, ph);
1726 return ph->env.cpuid ? 0 : -ENOMEM;
1727}
1728
1729static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001730 struct perf_header *ph, int fd,
1731 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001732{
1733 uint64_t mem;
1734 size_t ret;
1735
Namhyung Kim5323f602012-12-17 15:38:54 +09001736 ret = readn(fd, &mem, sizeof(mem));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001737 if (ret != sizeof(mem))
1738 return -1;
1739
1740 if (ph->needs_swap)
1741 mem = bswap_64(mem);
1742
1743 ph->env.total_mem = mem;
1744 return 0;
1745}
1746
Robert Richter7c2f7af2012-08-16 21:10:23 +02001747static struct perf_evsel *
1748perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1749{
1750 struct perf_evsel *evsel;
1751
1752 list_for_each_entry(evsel, &evlist->entries, node) {
1753 if (evsel->idx == idx)
1754 return evsel;
1755 }
1756
1757 return NULL;
1758}
1759
1760static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001761perf_evlist__set_event_name(struct perf_evlist *evlist,
1762 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001763{
1764 struct perf_evsel *evsel;
1765
1766 if (!event->name)
1767 return;
1768
1769 evsel = perf_evlist__find_by_index(evlist, event->idx);
1770 if (!evsel)
1771 return;
1772
1773 if (evsel->name)
1774 return;
1775
1776 evsel->name = strdup(event->name);
1777}
1778
1779static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001780process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001781 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001782 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001783{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001784 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001785 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1786
1787 if (!events)
1788 return 0;
1789
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001790 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001791 for (evsel = events; evsel->attr.size; evsel++)
1792 perf_evlist__set_event_name(session->evlist, evsel);
1793
1794 free_event_desc(events);
1795
1796 return 0;
1797}
1798
Namhyung Kima1ae5652012-09-24 17:14:59 +09001799static int process_cmdline(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001800 struct perf_header *ph, int fd,
1801 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001802{
1803 size_t ret;
1804 char *str;
1805 u32 nr, i;
1806 struct strbuf sb;
1807
Namhyung Kim5323f602012-12-17 15:38:54 +09001808 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001809 if (ret != sizeof(nr))
1810 return -1;
1811
1812 if (ph->needs_swap)
1813 nr = bswap_32(nr);
1814
1815 ph->env.nr_cmdline = nr;
1816 strbuf_init(&sb, 128);
1817
1818 for (i = 0; i < nr; i++) {
1819 str = do_read_string(fd, ph);
1820 if (!str)
1821 goto error;
1822
1823 /* include a NULL character at the end */
1824 strbuf_add(&sb, str, strlen(str) + 1);
1825 free(str);
1826 }
1827 ph->env.cmdline = strbuf_detach(&sb, NULL);
1828 return 0;
1829
1830error:
1831 strbuf_release(&sb);
1832 return -1;
1833}
1834
1835static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001836 struct perf_header *ph, int fd,
1837 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001838{
1839 size_t ret;
1840 u32 nr, i;
1841 char *str;
1842 struct strbuf sb;
1843
Namhyung Kim5323f602012-12-17 15:38:54 +09001844 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001845 if (ret != sizeof(nr))
1846 return -1;
1847
1848 if (ph->needs_swap)
1849 nr = bswap_32(nr);
1850
1851 ph->env.nr_sibling_cores = nr;
1852 strbuf_init(&sb, 128);
1853
1854 for (i = 0; i < nr; i++) {
1855 str = do_read_string(fd, ph);
1856 if (!str)
1857 goto error;
1858
1859 /* include a NULL character at the end */
1860 strbuf_add(&sb, str, strlen(str) + 1);
1861 free(str);
1862 }
1863 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1864
Namhyung Kim5323f602012-12-17 15:38:54 +09001865 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001866 if (ret != sizeof(nr))
1867 return -1;
1868
1869 if (ph->needs_swap)
1870 nr = bswap_32(nr);
1871
1872 ph->env.nr_sibling_threads = nr;
1873
1874 for (i = 0; i < nr; i++) {
1875 str = do_read_string(fd, ph);
1876 if (!str)
1877 goto error;
1878
1879 /* include a NULL character at the end */
1880 strbuf_add(&sb, str, strlen(str) + 1);
1881 free(str);
1882 }
1883 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1884 return 0;
1885
1886error:
1887 strbuf_release(&sb);
1888 return -1;
1889}
1890
1891static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001892 struct perf_header *ph, int fd,
1893 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001894{
1895 size_t ret;
1896 u32 nr, node, i;
1897 char *str;
1898 uint64_t mem_total, mem_free;
1899 struct strbuf sb;
1900
1901 /* nr nodes */
Namhyung Kim5323f602012-12-17 15:38:54 +09001902 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001903 if (ret != sizeof(nr))
1904 goto error;
1905
1906 if (ph->needs_swap)
1907 nr = bswap_32(nr);
1908
1909 ph->env.nr_numa_nodes = nr;
1910 strbuf_init(&sb, 256);
1911
1912 for (i = 0; i < nr; i++) {
1913 /* node number */
Namhyung Kim5323f602012-12-17 15:38:54 +09001914 ret = readn(fd, &node, sizeof(node));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001915 if (ret != sizeof(node))
1916 goto error;
1917
Namhyung Kim5323f602012-12-17 15:38:54 +09001918 ret = readn(fd, &mem_total, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001919 if (ret != sizeof(u64))
1920 goto error;
1921
Namhyung Kim5323f602012-12-17 15:38:54 +09001922 ret = readn(fd, &mem_free, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001923 if (ret != sizeof(u64))
1924 goto error;
1925
1926 if (ph->needs_swap) {
1927 node = bswap_32(node);
1928 mem_total = bswap_64(mem_total);
1929 mem_free = bswap_64(mem_free);
1930 }
1931
1932 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1933 node, mem_total, mem_free);
1934
1935 str = do_read_string(fd, ph);
1936 if (!str)
1937 goto error;
1938
1939 /* include a NULL character at the end */
1940 strbuf_add(&sb, str, strlen(str) + 1);
1941 free(str);
1942 }
1943 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1944 return 0;
1945
1946error:
1947 strbuf_release(&sb);
1948 return -1;
1949}
1950
1951static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001952 struct perf_header *ph, int fd,
1953 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001954{
1955 size_t ret;
1956 char *name;
1957 u32 pmu_num;
1958 u32 type;
1959 struct strbuf sb;
1960
Namhyung Kim5323f602012-12-17 15:38:54 +09001961 ret = readn(fd, &pmu_num, sizeof(pmu_num));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001962 if (ret != sizeof(pmu_num))
1963 return -1;
1964
1965 if (ph->needs_swap)
1966 pmu_num = bswap_32(pmu_num);
1967
1968 if (!pmu_num) {
1969 pr_debug("pmu mappings not available\n");
1970 return 0;
1971 }
1972
1973 ph->env.nr_pmu_mappings = pmu_num;
1974 strbuf_init(&sb, 128);
1975
1976 while (pmu_num) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001977 if (readn(fd, &type, sizeof(type)) != sizeof(type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001978 goto error;
1979 if (ph->needs_swap)
1980 type = bswap_32(type);
1981
1982 name = do_read_string(fd, ph);
1983 if (!name)
1984 goto error;
1985
1986 strbuf_addf(&sb, "%u:%s", type, name);
1987 /* include a NULL character at the end */
1988 strbuf_add(&sb, "", 1);
1989
1990 free(name);
1991 pmu_num--;
1992 }
1993 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1994 return 0;
1995
1996error:
1997 strbuf_release(&sb);
1998 return -1;
1999}
2000
Namhyung Kima8bb5592013-01-22 18:09:31 +09002001static int process_group_desc(struct perf_file_section *section __maybe_unused,
2002 struct perf_header *ph, int fd,
2003 void *data __maybe_unused)
2004{
2005 size_t ret = -1;
2006 u32 i, nr, nr_groups;
2007 struct perf_session *session;
2008 struct perf_evsel *evsel, *leader = NULL;
2009 struct group_desc {
2010 char *name;
2011 u32 leader_idx;
2012 u32 nr_members;
2013 } *desc;
2014
2015 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
2016 return -1;
2017
2018 if (ph->needs_swap)
2019 nr_groups = bswap_32(nr_groups);
2020
2021 ph->env.nr_groups = nr_groups;
2022 if (!nr_groups) {
2023 pr_debug("group desc not available\n");
2024 return 0;
2025 }
2026
2027 desc = calloc(nr_groups, sizeof(*desc));
2028 if (!desc)
2029 return -1;
2030
2031 for (i = 0; i < nr_groups; i++) {
2032 desc[i].name = do_read_string(fd, ph);
2033 if (!desc[i].name)
2034 goto out_free;
2035
2036 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
2037 goto out_free;
2038
2039 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
2040 goto out_free;
2041
2042 if (ph->needs_swap) {
2043 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
2044 desc[i].nr_members = bswap_32(desc[i].nr_members);
2045 }
2046 }
2047
2048 /*
2049 * Rebuild group relationship based on the group_desc
2050 */
2051 session = container_of(ph, struct perf_session, header);
2052 session->evlist->nr_groups = nr_groups;
2053
2054 i = nr = 0;
2055 list_for_each_entry(evsel, &session->evlist->entries, node) {
2056 if (evsel->idx == (int) desc[i].leader_idx) {
2057 evsel->leader = evsel;
2058 /* {anon_group} is a dummy name */
2059 if (strcmp(desc[i].name, "{anon_group}"))
2060 evsel->group_name = desc[i].name;
2061 evsel->nr_members = desc[i].nr_members;
2062
2063 if (i >= nr_groups || nr > 0) {
2064 pr_debug("invalid group desc\n");
2065 goto out_free;
2066 }
2067
2068 leader = evsel;
2069 nr = evsel->nr_members - 1;
2070 i++;
2071 } else if (nr) {
2072 /* This is a group member */
2073 evsel->leader = leader;
2074
2075 nr--;
2076 }
2077 }
2078
2079 if (i != nr_groups || nr != 0) {
2080 pr_debug("invalid group desc\n");
2081 goto out_free;
2082 }
2083
2084 ret = 0;
2085out_free:
2086 while ((int) --i >= 0)
2087 free(desc[i].name);
2088 free(desc);
2089
2090 return ret;
2091}
2092
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002093struct feature_ops {
2094 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2095 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01002096 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002097 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002098 const char *name;
2099 bool full_only;
2100};
2101
Robert Richter8cdfa782011-12-07 10:02:56 +01002102#define FEAT_OPA(n, func) \
2103 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01002104#define FEAT_OPP(n, func) \
2105 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2106 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01002107#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01002108 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09002109 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01002110
2111/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002112#define print_tracing_data NULL
2113#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002114
2115static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002116 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01002117 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002118 FEAT_OPP(HEADER_HOSTNAME, hostname),
2119 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2120 FEAT_OPP(HEADER_VERSION, version),
2121 FEAT_OPP(HEADER_ARCH, arch),
2122 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2123 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09002124 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002125 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02002126 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002127 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01002128 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2129 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01002130 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002131 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09002132 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002133};
2134
2135struct header_print_data {
2136 FILE *fp;
2137 bool full; /* extended list of headers */
2138};
2139
2140static int perf_file_section__fprintf_info(struct perf_file_section *section,
2141 struct perf_header *ph,
2142 int feat, int fd, void *data)
2143{
2144 struct header_print_data *hd = data;
2145
2146 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2147 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2148 "%d, continuing...\n", section->offset, feat);
2149 return 0;
2150 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002151 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002152 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002153 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002154 }
2155 if (!feat_ops[feat].print)
2156 return 0;
2157
2158 if (!feat_ops[feat].full_only || hd->full)
2159 feat_ops[feat].print(ph, fd, hd->fp);
2160 else
2161 fprintf(hd->fp, "# %s info available, use -I to display\n",
2162 feat_ops[feat].name);
2163
2164 return 0;
2165}
2166
2167int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2168{
2169 struct header_print_data hd;
2170 struct perf_header *header = &session->header;
2171 int fd = session->fd;
2172 hd.fp = fp;
2173 hd.full = full;
2174
2175 perf_header__process_sections(header, fd, &hd,
2176 perf_file_section__fprintf_info);
2177 return 0;
2178}
2179
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002180static int do_write_feat(int fd, struct perf_header *h, int type,
2181 struct perf_file_section **p,
2182 struct perf_evlist *evlist)
2183{
2184 int err;
2185 int ret = 0;
2186
2187 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002188 if (!feat_ops[type].write)
2189 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002190
2191 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2192
2193 err = feat_ops[type].write(fd, h, evlist);
2194 if (err < 0) {
2195 pr_debug("failed to write feature %d\n", type);
2196
2197 /* undo anything written */
2198 lseek(fd, (*p)->offset, SEEK_SET);
2199
2200 return -1;
2201 }
2202 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2203 (*p)++;
2204 }
2205 return ret;
2206}
2207
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002208static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002209 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002210{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002211 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002212 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002213 int sec_size;
2214 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002215 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002216 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002217
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002218 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002219 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002220 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002221
Paul Gortmaker91b98802013-01-30 20:05:49 -05002222 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002223 if (feat_sec == NULL)
2224 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002225
2226 sec_size = sizeof(*feat_sec) * nr_sections;
2227
Jiri Olsa8d541e92013-07-17 19:49:44 +02002228 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002229 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002230
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002231 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2232 if (do_write_feat(fd, header, feat, &p, evlist))
2233 perf_header__clear_feat(header, feat);
2234 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002235
Xiao Guangrongf887f302010-02-04 16:46:42 +08002236 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002237 /*
2238 * may write more than needed due to dropped feature, but
2239 * this is okay, reader will skip the mising entries
2240 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002241 err = do_write(fd, feat_sec, sec_size);
2242 if (err < 0)
2243 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002244 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002245 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002246}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002247
Tom Zanussi8dc58102010-04-01 23:59:15 -05002248int perf_header__write_pipe(int fd)
2249{
2250 struct perf_pipe_file_header f_header;
2251 int err;
2252
2253 f_header = (struct perf_pipe_file_header){
2254 .magic = PERF_MAGIC,
2255 .size = sizeof(f_header),
2256 };
2257
2258 err = do_write(fd, &f_header, sizeof(f_header));
2259 if (err < 0) {
2260 pr_debug("failed to write perf pipe header\n");
2261 return err;
2262 }
2263
2264 return 0;
2265}
2266
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002267int perf_session__write_header(struct perf_session *session,
2268 struct perf_evlist *evlist,
2269 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002270{
2271 struct perf_file_header f_header;
2272 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002273 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002274 struct perf_evsel *evsel;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002275 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002276 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002277
2278 lseek(fd, sizeof(f_header), SEEK_SET);
2279
Robert Richter6606f872012-08-16 21:10:19 +02002280 list_for_each_entry(evsel, &evlist->entries, node) {
2281 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2282 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002283 if (err < 0) {
2284 pr_debug("failed to write perf header\n");
2285 return err;
2286 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002287 }
2288
Jiri Olsa944d62b2013-07-17 19:49:43 +02002289 attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002290
Robert Richter6606f872012-08-16 21:10:19 +02002291 list_for_each_entry(evsel, &evlist->entries, node) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002292 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002293 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002294 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002295 .offset = evsel->id_offset,
2296 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002297 }
2298 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002299 err = do_write(fd, &f_attr, sizeof(f_attr));
2300 if (err < 0) {
2301 pr_debug("failed to write perf header attribute\n");
2302 return err;
2303 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002304 }
2305
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002306 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002307 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002308
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002309 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002310 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002311 if (err < 0)
2312 return err;
2313 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002314
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002315 f_header = (struct perf_file_header){
2316 .magic = PERF_MAGIC,
2317 .size = sizeof(f_header),
2318 .attr_size = sizeof(f_attr),
2319 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002320 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002321 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002322 },
2323 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002324 .offset = header->data_offset,
2325 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002326 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002327 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002328 };
2329
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002330 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002331
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002332 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002333 err = do_write(fd, &f_header, sizeof(f_header));
2334 if (err < 0) {
2335 pr_debug("failed to write perf header\n");
2336 return err;
2337 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002338 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002339
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002340 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002341}
2342
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002343static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002344 int fd, void *buf, size_t size)
2345{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002346 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002347 return -1;
2348
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002349 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002350 mem_bswap_64(buf, size);
2351
2352 return 0;
2353}
2354
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002355int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002356 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002357 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002358 struct perf_header *ph,
2359 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002360{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002361 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002362 int nr_sections;
2363 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002364 int feat;
2365 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002366
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002367 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002368 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002369 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002370
Paul Gortmaker91b98802013-01-30 20:05:49 -05002371 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002372 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002373 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002374
2375 sec_size = sizeof(*feat_sec) * nr_sections;
2376
Jiri Olsa8d541e92013-07-17 19:49:44 +02002377 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002378
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002379 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2380 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002381 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002382
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002383 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2384 err = process(sec++, header, feat, fd, data);
2385 if (err < 0)
2386 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002387 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002388 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002389out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002390 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002391 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002392}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002393
Stephane Eranian114382a2012-02-09 23:21:08 +01002394static const int attr_file_abi_sizes[] = {
2395 [0] = PERF_ATTR_SIZE_VER0,
2396 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002397 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002398 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian114382a2012-02-09 23:21:08 +01002399 0,
2400};
2401
2402/*
2403 * In the legacy file format, the magic number is not used to encode endianness.
2404 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2405 * on ABI revisions, we need to try all combinations for all endianness to
2406 * detect the endianness.
2407 */
2408static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2409{
2410 uint64_t ref_size, attr_size;
2411 int i;
2412
2413 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2414 ref_size = attr_file_abi_sizes[i]
2415 + sizeof(struct perf_file_section);
2416 if (hdr_sz != ref_size) {
2417 attr_size = bswap_64(hdr_sz);
2418 if (attr_size != ref_size)
2419 continue;
2420
2421 ph->needs_swap = true;
2422 }
2423 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2424 i,
2425 ph->needs_swap);
2426 return 0;
2427 }
2428 /* could not determine endianness */
2429 return -1;
2430}
2431
2432#define PERF_PIPE_HDR_VER0 16
2433
2434static const size_t attr_pipe_abi_sizes[] = {
2435 [0] = PERF_PIPE_HDR_VER0,
2436 0,
2437};
2438
2439/*
2440 * In the legacy pipe format, there is an implicit assumption that endiannesss
2441 * between host recording the samples, and host parsing the samples is the
2442 * same. This is not always the case given that the pipe output may always be
2443 * redirected into a file and analyzed on a different machine with possibly a
2444 * different endianness and perf_event ABI revsions in the perf tool itself.
2445 */
2446static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2447{
2448 u64 attr_size;
2449 int i;
2450
2451 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2452 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2453 attr_size = bswap_64(hdr_sz);
2454 if (attr_size != hdr_sz)
2455 continue;
2456
2457 ph->needs_swap = true;
2458 }
2459 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2460 return 0;
2461 }
2462 return -1;
2463}
2464
Feng Tange84ba4e2012-10-30 11:56:07 +08002465bool is_perf_magic(u64 magic)
2466{
2467 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2468 || magic == __perf_magic2
2469 || magic == __perf_magic2_sw)
2470 return true;
2471
2472 return false;
2473}
2474
Stephane Eranian114382a2012-02-09 23:21:08 +01002475static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2476 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002477{
2478 int ret;
2479
2480 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002481 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002482 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002483 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002484 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002485 if (is_pipe)
2486 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002487
Stephane Eranian114382a2012-02-09 23:21:08 +01002488 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002489 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002490 /*
2491 * the new magic number serves two purposes:
2492 * - unique number to identify actual perf.data files
2493 * - encode endianness of file
2494 */
Stephane Eranian73323f52012-02-02 13:54:44 +01002495
Stephane Eranian114382a2012-02-09 23:21:08 +01002496 /* check magic number with one endianness */
2497 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002498 return 0;
2499
Stephane Eranian114382a2012-02-09 23:21:08 +01002500 /* check magic number with opposite endianness */
2501 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002502 return -1;
2503
2504 ph->needs_swap = true;
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002505 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002506
2507 return 0;
2508}
2509
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002510int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002511 struct perf_header *ph, int fd)
2512{
Stephane Eranian73323f52012-02-02 13:54:44 +01002513 int ret;
2514
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002515 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002516
Stephane Eranian73323f52012-02-02 13:54:44 +01002517 ret = readn(fd, header, sizeof(*header));
2518 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002519 return -1;
2520
Stephane Eranian114382a2012-02-09 23:21:08 +01002521 if (check_magic_endian(header->magic,
2522 header->attr_size, false, ph) < 0) {
2523 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002524 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002525 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002526
Stephane Eranian73323f52012-02-02 13:54:44 +01002527 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002528 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002529 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002530 }
2531
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002532 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002533 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002534 if (header->size == offsetof(typeof(*header), adds_features))
2535 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002536 else
2537 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002538 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002539 /*
2540 * feature bitmap is declared as an array of unsigned longs --
2541 * not good since its size can differ between the host that
2542 * generated the data file and the host analyzing the file.
2543 *
2544 * We need to handle endianness, but we don't know the size of
2545 * the unsigned long where the file was generated. Take a best
2546 * guess at determining it: try 64-bit swap first (ie., file
2547 * created on a 64-bit host), and check if the hostname feature
2548 * bit is set (this feature bit is forced on as of fbe96f2).
2549 * If the bit is not, undo the 64-bit swap and try a 32-bit
2550 * swap. If the hostname bit is still not set (e.g., older data
2551 * file), punt and fallback to the original behavior --
2552 * clearing all feature bits and setting buildid.
2553 */
David Ahern80c01202012-06-08 11:47:51 -03002554 mem_bswap_64(&header->adds_features,
2555 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002556
2557 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002558 /* unswap as u64 */
2559 mem_bswap_64(&header->adds_features,
2560 BITS_TO_U64(HEADER_FEAT_BITS));
2561
2562 /* unswap as u32 */
2563 mem_bswap_32(&header->adds_features,
2564 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002565 }
2566
2567 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2568 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2569 set_bit(HEADER_BUILD_ID, header->adds_features);
2570 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002571 }
2572
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002573 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002574 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002575
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002576 ph->data_offset = header->data.offset;
2577 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002578 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002579 return 0;
2580}
2581
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002582static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002583 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002584 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002585{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002586 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002587 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002588 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002589 return 0;
2590 }
2591
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002592 if (feat >= HEADER_LAST_FEATURE) {
2593 pr_debug("unknown feature %d, continuing...\n", feat);
2594 return 0;
2595 }
2596
Robert Richterf1c67db2012-02-10 15:41:56 +01002597 if (!feat_ops[feat].process)
2598 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002599
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002600 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002601}
2602
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002603static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002604 struct perf_header *ph, int fd,
2605 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002606{
Stephane Eranian73323f52012-02-02 13:54:44 +01002607 int ret;
2608
2609 ret = readn(fd, header, sizeof(*header));
2610 if (ret <= 0)
2611 return -1;
2612
Stephane Eranian114382a2012-02-09 23:21:08 +01002613 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2614 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002615 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002616 }
2617
2618 if (ph->needs_swap)
2619 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002620
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002621 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002622 return -1;
2623
Tom Zanussi8dc58102010-04-01 23:59:15 -05002624 return 0;
2625}
2626
Jiri Olsad4339562013-07-17 19:49:41 +02002627static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002628{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002629 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002630 struct perf_pipe_file_header f_header;
2631
Jiri Olsad4339562013-07-17 19:49:41 +02002632 if (perf_file_header__read_pipe(&f_header, header, session->fd,
Tom Zanussi454c4072010-05-01 01:41:20 -05002633 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002634 pr_debug("incompatible file format\n");
2635 return -EINVAL;
2636 }
2637
Tom Zanussi8dc58102010-04-01 23:59:15 -05002638 return 0;
2639}
2640
Stephane Eranian69996df2012-02-09 23:21:06 +01002641static int read_attr(int fd, struct perf_header *ph,
2642 struct perf_file_attr *f_attr)
2643{
2644 struct perf_event_attr *attr = &f_attr->attr;
2645 size_t sz, left;
2646 size_t our_sz = sizeof(f_attr->attr);
2647 int ret;
2648
2649 memset(f_attr, 0, sizeof(*f_attr));
2650
2651 /* read minimal guaranteed structure */
2652 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2653 if (ret <= 0) {
2654 pr_debug("cannot read %d bytes of header attr\n",
2655 PERF_ATTR_SIZE_VER0);
2656 return -1;
2657 }
2658
2659 /* on file perf_event_attr size */
2660 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002661
Stephane Eranian69996df2012-02-09 23:21:06 +01002662 if (ph->needs_swap)
2663 sz = bswap_32(sz);
2664
2665 if (sz == 0) {
2666 /* assume ABI0 */
2667 sz = PERF_ATTR_SIZE_VER0;
2668 } else if (sz > our_sz) {
2669 pr_debug("file uses a more recent and unsupported ABI"
2670 " (%zu bytes extra)\n", sz - our_sz);
2671 return -1;
2672 }
2673 /* what we have not yet read and that we know about */
2674 left = sz - PERF_ATTR_SIZE_VER0;
2675 if (left) {
2676 void *ptr = attr;
2677 ptr += PERF_ATTR_SIZE_VER0;
2678
2679 ret = readn(fd, ptr, left);
2680 }
2681 /* read perf_file_section, ids are read in caller */
2682 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2683
2684 return ret <= 0 ? -1 : 0;
2685}
2686
Namhyung Kim831394b2012-09-06 11:10:46 +09002687static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2688 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002689{
Namhyung Kim831394b2012-09-06 11:10:46 +09002690 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002691 char bf[128];
2692
Namhyung Kim831394b2012-09-06 11:10:46 +09002693 /* already prepared */
2694 if (evsel->tp_format)
2695 return 0;
2696
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002697 if (pevent == NULL) {
2698 pr_debug("broken or missing trace data\n");
2699 return -1;
2700 }
2701
Namhyung Kim831394b2012-09-06 11:10:46 +09002702 event = pevent_find_event(pevent, evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002703 if (event == NULL)
2704 return -1;
2705
Namhyung Kim831394b2012-09-06 11:10:46 +09002706 if (!evsel->name) {
2707 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2708 evsel->name = strdup(bf);
2709 if (evsel->name == NULL)
2710 return -1;
2711 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002712
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002713 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002714 return 0;
2715}
2716
Namhyung Kim831394b2012-09-06 11:10:46 +09002717static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2718 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002719{
2720 struct perf_evsel *pos;
2721
2722 list_for_each_entry(pos, &evlist->entries, node) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002723 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2724 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002725 return -1;
2726 }
2727
2728 return 0;
2729}
2730
Jiri Olsad4339562013-07-17 19:49:41 +02002731int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002732{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002733 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002734 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002735 struct perf_file_attr f_attr;
2736 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002737 int nr_attrs, nr_ids, i, j;
Jiri Olsad4339562013-07-17 19:49:41 +02002738 int fd = session->fd;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002739
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002740 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002741 if (session->evlist == NULL)
2742 return -ENOMEM;
2743
Tom Zanussi8dc58102010-04-01 23:59:15 -05002744 if (session->fd_pipe)
Jiri Olsad4339562013-07-17 19:49:41 +02002745 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002746
Stephane Eranian69996df2012-02-09 23:21:06 +01002747 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002748 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002749
Stephane Eranian69996df2012-02-09 23:21:06 +01002750 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002751 lseek(fd, f_header.attrs.offset, SEEK_SET);
2752
2753 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002754 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002755 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002756
Stephane Eranian69996df2012-02-09 23:21:06 +01002757 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002758 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002759
David Aherneda39132011-07-15 12:34:09 -06002760 if (header->needs_swap)
2761 perf_event__attr_swap(&f_attr.attr);
2762
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002763 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002764 evsel = perf_evsel__new(&f_attr.attr, i);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002765
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002766 if (evsel == NULL)
2767 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002768
2769 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002770 /*
2771 * Do it before so that if perf_evsel__alloc_id fails, this
2772 * entry gets purged too at perf_evlist__delete().
2773 */
2774 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002775
2776 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002777 /*
2778 * We don't have the cpu and thread maps on the header, so
2779 * for allocating the perf_sample_id table we fake 1 cpu and
2780 * hattr->ids threads.
2781 */
2782 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2783 goto out_delete_evlist;
2784
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002785 lseek(fd, f_attr.ids.offset, SEEK_SET);
2786
2787 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002788 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002789 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002790
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002791 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002792 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002793
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002794 lseek(fd, tmp, SEEK_SET);
2795 }
2796
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002797 symbol_conf.nr_events = nr_attrs;
2798
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002799 perf_header__process_sections(header, fd, &session->pevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002800 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002801
Namhyung Kim831394b2012-09-06 11:10:46 +09002802 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2803 session->pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002804 goto out_delete_evlist;
2805
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002806 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002807out_errno:
2808 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002809
2810out_delete_evlist:
2811 perf_evlist__delete(session->evlist);
2812 session->evlist = NULL;
2813 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002814}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002815
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002816int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002817 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002818 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002819{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002820 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002821 size_t size;
2822 int err;
2823
2824 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002825 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002826 size += sizeof(struct perf_event_header);
2827 size += ids * sizeof(u64);
2828
2829 ev = malloc(size);
2830
Chris Samuelce47dc52010-11-13 13:35:06 +11002831 if (ev == NULL)
2832 return -ENOMEM;
2833
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002834 ev->attr.attr = *attr;
2835 memcpy(ev->attr.id, id, ids * sizeof(u64));
2836
2837 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002838 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002839
Robert Richterf4d83432012-08-16 21:10:17 +02002840 if (ev->attr.header.size == size)
2841 err = process(tool, ev, NULL, NULL);
2842 else
2843 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002844
2845 free(ev);
2846
2847 return err;
2848}
2849
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002850int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002851 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002852 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002853{
Robert Richter6606f872012-08-16 21:10:19 +02002854 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002855 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002856
Robert Richter6606f872012-08-16 21:10:19 +02002857 list_for_each_entry(evsel, &session->evlist->entries, node) {
2858 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2859 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002860 if (err) {
2861 pr_debug("failed to create perf header attribute\n");
2862 return err;
2863 }
2864 }
2865
2866 return err;
2867}
2868
Adrian Hunter47c3d102013-07-04 16:20:21 +03002869int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2870 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002871 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002872{
Robert Richterf4d83432012-08-16 21:10:17 +02002873 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002874 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002875 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002876
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002877 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002878 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002879 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002880 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002881 }
2882
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002883 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002884 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002885 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002886
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002887 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002888
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002889 ids = event->header.size;
2890 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002891 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002892 /*
2893 * We don't have the cpu and thread maps on the header, so
2894 * for allocating the perf_sample_id table we fake 1 cpu and
2895 * hattr->ids threads.
2896 */
2897 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2898 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002899
2900 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002901 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002902 }
2903
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03002904 symbol_conf.nr_events = evlist->nr_entries;
2905
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002906 return 0;
2907}
Tom Zanussicd19a032010-04-01 23:59:20 -05002908
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002909int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002910 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002911 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05002912{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002913 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02002914 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05002915 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002916 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05002917
Jiri Olsa29208e52011-10-20 15:59:43 +02002918 /*
2919 * We are going to store the size of the data followed
2920 * by the data contents. Since the fd descriptor is a pipe,
2921 * we cannot seek back to store the size of the data once
2922 * we know it. Instead we:
2923 *
2924 * - write the tracing data to the temp file
2925 * - get/write the data size to pipe
2926 * - write the tracing data from the temp file
2927 * to the pipe
2928 */
2929 tdata = tracing_data_get(&evlist->entries, fd, true);
2930 if (!tdata)
2931 return -1;
2932
Tom Zanussi92155452010-04-01 23:59:21 -05002933 memset(&ev, 0, sizeof(ev));
2934
2935 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02002936 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002937 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05002938 padding = aligned_size - size;
2939 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2940 ev.tracing_data.size = aligned_size;
2941
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002942 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05002943
Jiri Olsa29208e52011-10-20 15:59:43 +02002944 /*
2945 * The put function will copy all the tracing data
2946 * stored in temp file to the pipe.
2947 */
2948 tracing_data_put(tdata);
2949
Tom Zanussi92155452010-04-01 23:59:21 -05002950 write_padded(fd, NULL, 0, padding);
2951
2952 return aligned_size;
2953}
2954
Adrian Hunter47c3d102013-07-04 16:20:21 +03002955int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
2956 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002957 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05002958{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002959 ssize_t size_read, padding, size = event->tracing_data.size;
Tom Zanussi92155452010-04-01 23:59:21 -05002960 off_t offset = lseek(session->fd, 0, SEEK_CUR);
2961 char buf[BUFSIZ];
2962
2963 /* setup for reading amidst mmap */
2964 lseek(session->fd, offset + sizeof(struct tracing_data_event),
2965 SEEK_SET);
2966
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002967 size_read = trace_report(session->fd, &session->pevent,
2968 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002969 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05002970
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002971 if (readn(session->fd, buf, padding) < 0) {
2972 pr_err("%s: reading input file", __func__);
2973 return -1;
2974 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002975 if (session->repipe) {
2976 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002977 if (retw <= 0 || retw != padding) {
2978 pr_err("%s: repiping tracing data padding", __func__);
2979 return -1;
2980 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002981 }
Tom Zanussi92155452010-04-01 23:59:21 -05002982
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002983 if (size_read + padding != size) {
2984 pr_err("%s: tracing data size mismatch", __func__);
2985 return -1;
2986 }
Tom Zanussi92155452010-04-01 23:59:21 -05002987
Namhyung Kim831394b2012-09-06 11:10:46 +09002988 perf_evlist__prepare_tracepoint_events(session->evlist,
2989 session->pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03002990
Tom Zanussi92155452010-04-01 23:59:21 -05002991 return size_read + padding;
2992}
Tom Zanussic7929e42010-04-01 23:59:22 -05002993
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002994int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002995 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002996 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002997 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05002998{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002999 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003000 size_t len;
3001 int err = 0;
3002
3003 if (!pos->hit)
3004 return err;
3005
3006 memset(&ev, 0, sizeof(ev));
3007
3008 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003009 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003010 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3011 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3012 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003013 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003014 ev.build_id.header.size = sizeof(ev.build_id) + len;
3015 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3016
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003017 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003018
3019 return err;
3020}
3021
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003022int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003023 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003024 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003025{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003026 __event_process_build_id(&event->build_id,
3027 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003028 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003029 return 0;
3030}
Stephane Eraniana1ac1d32010-06-17 11:39:01 +02003031
3032void disable_buildid_cache(void)
3033{
3034 no_buildid_cache = true;
3035}