blob: be8d02eb97e91b34ef9875e5f227b8a0d6fb546e [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"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020025#include "data.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020026
Stephane Eraniana1ac1d32010-06-17 11:39:01 +020027static bool no_buildid_cache = false;
28
Stephane Eranianfbe96f22011-09-30 15:40:40 +020029static u32 header_argc;
30static const char **header_argv;
31
Stephane Eranian73323f52012-02-02 13:54:44 +010032/*
33 * magic2 = "PERFILE2"
34 * must be a numerical value to let the endianness
35 * determine the memory layout. That way we are able
36 * to detect endianness when reading the perf.data file
37 * back.
38 *
39 * we check for legacy (PERFFILE) format.
40 */
41static const char *__perf_magic1 = "PERFFILE";
42static const u64 __perf_magic2 = 0x32454c4946524550ULL;
43static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020044
Stephane Eranian73323f52012-02-02 13:54:44 +010045#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020046
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020047struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020048 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020049 struct perf_file_section ids;
50};
51
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030052void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020053{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030054 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020055}
56
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030057void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020058{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030059 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020060}
61
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030062bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020063{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030064 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020065}
66
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020067static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020068{
69 while (size) {
70 int ret = write(fd, buf, size);
71
72 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -020073 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020074
75 size -= ret;
76 buf += ret;
77 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020078
79 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020080}
81
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -020082#define NAME_ALIGN 64
83
84static int write_padded(int fd, const void *bf, size_t count,
85 size_t count_aligned)
86{
87 static const char zero_buf[NAME_ALIGN];
88 int err = do_write(fd, bf, count);
89
90 if (!err)
91 err = do_write(fd, zero_buf, count_aligned - count);
92
93 return err;
94}
95
Stephane Eranianfbe96f22011-09-30 15:40:40 +020096static int do_write_string(int fd, const char *str)
97{
98 u32 len, olen;
99 int ret;
100
101 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300102 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200103
104 /* write len, incl. \0 */
105 ret = do_write(fd, &len, sizeof(len));
106 if (ret < 0)
107 return ret;
108
109 return write_padded(fd, str, olen, len);
110}
111
112static char *do_read_string(int fd, struct perf_header *ph)
113{
114 ssize_t sz, ret;
115 u32 len;
116 char *buf;
117
Namhyung Kim5323f602012-12-17 15:38:54 +0900118 sz = readn(fd, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200119 if (sz < (ssize_t)sizeof(len))
120 return NULL;
121
122 if (ph->needs_swap)
123 len = bswap_32(len);
124
125 buf = malloc(len);
126 if (!buf)
127 return NULL;
128
Namhyung Kim5323f602012-12-17 15:38:54 +0900129 ret = readn(fd, buf, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200130 if (ret == (ssize_t)len) {
131 /*
132 * strings are padded by zeroes
133 * thus the actual strlen of buf
134 * may be less than len
135 */
136 return buf;
137 }
138
139 free(buf);
140 return NULL;
141}
142
143int
144perf_header__set_cmdline(int argc, const char **argv)
145{
146 int i;
147
David Ahern56e6f602012-07-29 20:53:51 -0600148 /*
149 * If header_argv has already been set, do not override it.
150 * This allows a command to set the cmdline, parse args and
151 * then call another builtin function that implements a
152 * command -- e.g, cmd_kvm calling cmd_record.
153 */
154 if (header_argv)
155 return 0;
156
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200157 header_argc = (u32)argc;
158
159 /* do not include NULL termination */
160 header_argv = calloc(argc, sizeof(char *));
161 if (!header_argv)
162 return -ENOMEM;
163
164 /*
165 * must copy argv contents because it gets moved
166 * around during option parsing
167 */
168 for (i = 0; i < argc ; i++)
169 header_argv[i] = argv[i];
170
171 return 0;
172}
173
Robert Richter1b549502011-12-07 10:02:53 +0100174#define dsos__for_each_with_build_id(pos, head) \
175 list_for_each_entry(pos, head, node) \
176 if (!pos->has_build_id) \
177 continue; \
178 else
179
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300180static int write_buildid(const char *name, size_t name_len, u8 *build_id,
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200181 pid_t pid, u16 misc, int fd)
182{
183 int err;
184 struct build_id_event b;
185 size_t len;
186
187 len = name_len + 1;
188 len = PERF_ALIGN(len, NAME_ALIGN);
189
190 memset(&b, 0, sizeof(b));
191 memcpy(&b.build_id, build_id, BUILD_ID_SIZE);
192 b.pid = pid;
193 b.header.misc = misc;
194 b.header.size = sizeof(b) + len;
195
196 err = do_write(fd, &b, sizeof(b));
197 if (err < 0)
198 return err;
199
200 return write_padded(fd, name, name_len + 1, len);
201}
202
Adrian Hunter1f625b02014-07-22 16:17:23 +0300203static int __dsos__hit_all(struct list_head *head)
204{
205 struct dso *pos;
206
207 list_for_each_entry(pos, head, node)
208 pos->hit = true;
209
210 return 0;
211}
212
213static int machine__hit_all_dsos(struct machine *machine)
214{
215 int err;
216
Waiman Long8fa7d872014-09-29 16:07:28 -0400217 err = __dsos__hit_all(&machine->kernel_dsos.head);
Adrian Hunter1f625b02014-07-22 16:17:23 +0300218 if (err)
219 return err;
220
Waiman Long8fa7d872014-09-29 16:07:28 -0400221 return __dsos__hit_all(&machine->user_dsos.head);
Adrian Hunter1f625b02014-07-22 16:17:23 +0300222}
223
224int dsos__hit_all(struct perf_session *session)
225{
226 struct rb_node *nd;
227 int err;
228
229 err = machine__hit_all_dsos(&session->machines.host);
230 if (err)
231 return err;
232
233 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
234 struct machine *pos = rb_entry(nd, struct machine, rb_node);
235
236 err = machine__hit_all_dsos(pos);
237 if (err)
238 return err;
239 }
240
241 return 0;
242}
243
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300244static int __dsos__write_buildid_table(struct list_head *head,
245 struct machine *machine,
246 pid_t pid, u16 misc, int fd)
Robert Richter1b549502011-12-07 10:02:53 +0100247{
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300248 char nm[PATH_MAX];
Robert Richter1b549502011-12-07 10:02:53 +0100249 struct dso *pos;
250
251 dsos__for_each_with_build_id(pos, head) {
252 int err;
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300253 const char *name;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200254 size_t name_len;
Robert Richter1b549502011-12-07 10:02:53 +0100255
256 if (!pos->hit)
257 continue;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200258
Adrian Hunter51682dc2014-07-22 16:17:57 +0300259 if (dso__is_vdso(pos)) {
260 name = pos->short_name;
261 name_len = pos->short_name_len + 1;
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300262 } else if (dso__is_kcore(pos)) {
263 machine__mmap_name(machine, nm, sizeof(nm));
264 name = nm;
265 name_len = strlen(nm) + 1;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200266 } else {
267 name = pos->long_name;
268 name_len = pos->long_name_len + 1;
269 }
270
271 err = write_buildid(name, name_len, pos->build_id,
272 pid, misc, fd);
273 if (err)
Robert Richter1b549502011-12-07 10:02:53 +0100274 return err;
275 }
276
277 return 0;
278}
279
280static int machine__write_buildid_table(struct machine *machine, int fd)
281{
282 int err;
283 u16 kmisc = PERF_RECORD_MISC_KERNEL,
284 umisc = PERF_RECORD_MISC_USER;
285
286 if (!machine__is_host(machine)) {
287 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
288 umisc = PERF_RECORD_MISC_GUEST_USER;
289 }
290
Waiman Long8fa7d872014-09-29 16:07:28 -0400291 err = __dsos__write_buildid_table(&machine->kernel_dsos.head, machine,
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300292 machine->pid, kmisc, fd);
Robert Richter1b549502011-12-07 10:02:53 +0100293 if (err == 0)
Waiman Long8fa7d872014-09-29 16:07:28 -0400294 err = __dsos__write_buildid_table(&machine->user_dsos.head,
295 machine, machine->pid, umisc,
296 fd);
Robert Richter1b549502011-12-07 10:02:53 +0100297 return err;
298}
299
Namhyung Kim714c9c42014-11-04 10:14:29 +0900300static int perf_session__write_buildid_table(struct perf_session *session, int fd)
Robert Richter1b549502011-12-07 10:02:53 +0100301{
Robert Richter1b549502011-12-07 10:02:53 +0100302 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300303 int err = machine__write_buildid_table(&session->machines.host, fd);
Robert Richter1b549502011-12-07 10:02:53 +0100304
305 if (err)
306 return err;
307
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300308 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
Robert Richter1b549502011-12-07 10:02:53 +0100309 struct machine *pos = rb_entry(nd, struct machine, rb_node);
310 err = machine__write_buildid_table(pos, fd);
311 if (err)
312 break;
313 }
314 return err;
315}
316
317int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200318 const char *name, bool is_kallsyms, bool is_vdso)
Robert Richter1b549502011-12-07 10:02:53 +0100319{
320 const size_t size = PATH_MAX;
321 char *realname, *filename = zalloc(size),
322 *linkname = zalloc(size), *targetname;
323 int len, err = -1;
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200324 bool slash = is_kallsyms || is_vdso;
Robert Richter1b549502011-12-07 10:02:53 +0100325
326 if (is_kallsyms) {
327 if (symbol_conf.kptr_restrict) {
328 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
Thomas Jaroschfdae6372013-01-25 11:21:39 +0100329 err = 0;
330 goto out_free;
Robert Richter1b549502011-12-07 10:02:53 +0100331 }
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200332 realname = (char *) name;
Robert Richter1b549502011-12-07 10:02:53 +0100333 } else
334 realname = realpath(name, NULL);
335
336 if (realname == NULL || filename == NULL || linkname == NULL)
337 goto out_free;
338
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300339 len = scnprintf(filename, size, "%s%s%s",
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200340 debugdir, slash ? "/" : "",
Adrian Hunter51682dc2014-07-22 16:17:57 +0300341 is_vdso ? DSO__NAME_VDSO : realname);
Robert Richter1b549502011-12-07 10:02:53 +0100342 if (mkdir_p(filename, 0755))
343 goto out_free;
344
Namhyung Kimafda0f92012-05-01 23:19:36 +0900345 snprintf(filename + len, size - len, "/%s", sbuild_id);
Robert Richter1b549502011-12-07 10:02:53 +0100346
347 if (access(filename, F_OK)) {
348 if (is_kallsyms) {
349 if (copyfile("/proc/kallsyms", filename))
350 goto out_free;
351 } else if (link(realname, filename) && copyfile(name, filename))
352 goto out_free;
353 }
354
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300355 len = scnprintf(linkname, size, "%s/.build-id/%.2s",
Robert Richter1b549502011-12-07 10:02:53 +0100356 debugdir, sbuild_id);
357
358 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
359 goto out_free;
360
361 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
362 targetname = filename + strlen(debugdir) - 5;
363 memcpy(targetname, "../..", 5);
364
365 if (symlink(targetname, linkname) == 0)
366 err = 0;
367out_free:
368 if (!is_kallsyms)
369 free(realname);
370 free(filename);
371 free(linkname);
372 return err;
373}
374
375static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
376 const char *name, const char *debugdir,
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200377 bool is_kallsyms, bool is_vdso)
Robert Richter1b549502011-12-07 10:02:53 +0100378{
379 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
380
381 build_id__sprintf(build_id, build_id_size, sbuild_id);
382
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200383 return build_id_cache__add_s(sbuild_id, debugdir, name,
384 is_kallsyms, is_vdso);
Robert Richter1b549502011-12-07 10:02:53 +0100385}
386
387int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
388{
389 const size_t size = PATH_MAX;
390 char *filename = zalloc(size),
391 *linkname = zalloc(size);
392 int err = -1;
393
394 if (filename == NULL || linkname == NULL)
395 goto out_free;
396
397 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
398 debugdir, sbuild_id, sbuild_id + 2);
399
400 if (access(linkname, F_OK))
401 goto out_free;
402
403 if (readlink(linkname, filename, size - 1) < 0)
404 goto out_free;
405
406 if (unlink(linkname))
407 goto out_free;
408
409 /*
410 * Since the link is relative, we must make it absolute:
411 */
412 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
413 debugdir, sbuild_id, filename);
414
415 if (unlink(linkname))
416 goto out_free;
417
418 err = 0;
419out_free:
420 free(filename);
421 free(linkname);
422 return err;
423}
424
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300425static int dso__cache_build_id(struct dso *dso, struct machine *machine,
426 const char *debugdir)
Robert Richter1b549502011-12-07 10:02:53 +0100427{
428 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
Adrian Hunter51682dc2014-07-22 16:17:57 +0300429 bool is_vdso = dso__is_vdso(dso);
Arnaldo Carvalho de Melobf4414a2013-12-10 15:19:23 -0300430 const char *name = dso->long_name;
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300431 char nm[PATH_MAX];
Robert Richter1b549502011-12-07 10:02:53 +0100432
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300433 if (dso__is_kcore(dso)) {
434 is_kallsyms = true;
435 machine__mmap_name(machine, nm, sizeof(nm));
436 name = nm;
437 }
438 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), name,
439 debugdir, is_kallsyms, is_vdso);
Robert Richter1b549502011-12-07 10:02:53 +0100440}
441
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300442static int __dsos__cache_build_ids(struct list_head *head,
443 struct machine *machine, const char *debugdir)
Robert Richter1b549502011-12-07 10:02:53 +0100444{
445 struct dso *pos;
446 int err = 0;
447
448 dsos__for_each_with_build_id(pos, head)
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300449 if (dso__cache_build_id(pos, machine, debugdir))
Robert Richter1b549502011-12-07 10:02:53 +0100450 err = -1;
451
452 return err;
453}
454
455static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
456{
Waiman Long8fa7d872014-09-29 16:07:28 -0400457 int ret = __dsos__cache_build_ids(&machine->kernel_dsos.head, machine,
Adrian Hunter5b6a42f2013-09-12 21:19:19 +0300458 debugdir);
Waiman Long8fa7d872014-09-29 16:07:28 -0400459 ret |= __dsos__cache_build_ids(&machine->user_dsos.head, machine,
460 debugdir);
Robert Richter1b549502011-12-07 10:02:53 +0100461 return ret;
462}
463
464static int perf_session__cache_build_ids(struct perf_session *session)
465{
466 struct rb_node *nd;
467 int ret;
468 char debugdir[PATH_MAX];
469
470 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
471
472 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
473 return -1;
474
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300475 ret = machine__cache_build_ids(&session->machines.host, debugdir);
Robert Richter1b549502011-12-07 10:02:53 +0100476
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300477 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
Robert Richter1b549502011-12-07 10:02:53 +0100478 struct machine *pos = rb_entry(nd, struct machine, rb_node);
479 ret |= machine__cache_build_ids(pos, debugdir);
480 }
481 return ret ? -1 : 0;
482}
483
484static bool machine__read_build_ids(struct machine *machine, bool with_hits)
485{
Waiman Long8fa7d872014-09-29 16:07:28 -0400486 bool ret;
487
488 ret = __dsos__read_build_ids(&machine->kernel_dsos.head, with_hits);
489 ret |= __dsos__read_build_ids(&machine->user_dsos.head, with_hits);
Robert Richter1b549502011-12-07 10:02:53 +0100490 return ret;
491}
492
493static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
494{
495 struct rb_node *nd;
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300496 bool ret = machine__read_build_ids(&session->machines.host, with_hits);
Robert Richter1b549502011-12-07 10:02:53 +0100497
Arnaldo Carvalho de Melo876650e2012-12-18 19:15:48 -0300498 for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
Robert Richter1b549502011-12-07 10:02:53 +0100499 struct machine *pos = rb_entry(nd, struct machine, rb_node);
500 ret |= machine__read_build_ids(pos, with_hits);
501 }
502
503 return ret;
504}
505
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300506static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200507 struct perf_evlist *evlist)
508{
509 return read_tracing_data(fd, &evlist->entries);
510}
511
512
513static int write_build_id(int fd, struct perf_header *h,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300514 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200515{
516 struct perf_session *session;
517 int err;
518
519 session = container_of(h, struct perf_session, header);
520
Robert Richtere20960c2011-12-07 10:02:55 +0100521 if (!perf_session__read_build_ids(session, true))
522 return -1;
523
Namhyung Kim714c9c42014-11-04 10:14:29 +0900524 err = perf_session__write_buildid_table(session, fd);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200525 if (err < 0) {
526 pr_debug("failed to write buildid table\n");
527 return err;
528 }
529 if (!no_buildid_cache)
530 perf_session__cache_build_ids(session);
531
532 return 0;
533}
534
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300535static int write_hostname(int fd, struct perf_header *h __maybe_unused,
536 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200537{
538 struct utsname uts;
539 int ret;
540
541 ret = uname(&uts);
542 if (ret < 0)
543 return -1;
544
545 return do_write_string(fd, uts.nodename);
546}
547
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300548static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
549 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200550{
551 struct utsname uts;
552 int ret;
553
554 ret = uname(&uts);
555 if (ret < 0)
556 return -1;
557
558 return do_write_string(fd, uts.release);
559}
560
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300561static int write_arch(int fd, struct perf_header *h __maybe_unused,
562 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200563{
564 struct utsname uts;
565 int ret;
566
567 ret = uname(&uts);
568 if (ret < 0)
569 return -1;
570
571 return do_write_string(fd, uts.machine);
572}
573
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300574static int write_version(int fd, struct perf_header *h __maybe_unused,
575 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200576{
577 return do_write_string(fd, perf_version_string);
578}
579
Wang Nan493c3032014-10-24 09:45:26 +0800580static int __write_cpudesc(int fd, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200581{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200582 FILE *file;
583 char *buf = NULL;
584 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800585 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200586 size_t len = 0;
587 int ret = -1;
588
589 if (!search)
590 return -1;
591
592 file = fopen("/proc/cpuinfo", "r");
593 if (!file)
594 return -1;
595
596 while (getline(&buf, &len, file) > 0) {
597 ret = strncmp(buf, search, strlen(search));
598 if (!ret)
599 break;
600 }
601
Wang Naned307752014-10-16 11:08:29 +0800602 if (ret) {
603 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200604 goto done;
Wang Naned307752014-10-16 11:08:29 +0800605 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200606
607 s = buf;
608
609 p = strchr(buf, ':');
610 if (p && *(p+1) == ' ' && *(p+2))
611 s = p + 2;
612 p = strchr(s, '\n');
613 if (p)
614 *p = '\0';
615
616 /* squash extra space characters (branding string) */
617 p = s;
618 while (*p) {
619 if (isspace(*p)) {
620 char *r = p + 1;
621 char *q = r;
622 *p = ' ';
623 while (*q && isspace(*q))
624 q++;
625 if (q != (p+1))
626 while ((*r++ = *q++));
627 }
628 p++;
629 }
630 ret = do_write_string(fd, s);
631done:
632 free(buf);
633 fclose(file);
634 return ret;
635}
636
Wang Nan493c3032014-10-24 09:45:26 +0800637static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
638 struct perf_evlist *evlist __maybe_unused)
639{
640#ifndef CPUINFO_PROC
641#define CPUINFO_PROC {"model name", }
642#endif
643 const char *cpuinfo_procs[] = CPUINFO_PROC;
644 unsigned int i;
645
646 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
647 int ret;
648 ret = __write_cpudesc(fd, cpuinfo_procs[i]);
649 if (ret >= 0)
650 return ret;
651 }
652 return -1;
653}
654
655
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300656static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
657 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200658{
659 long nr;
660 u32 nrc, nra;
661 int ret;
662
663 nr = sysconf(_SC_NPROCESSORS_CONF);
664 if (nr < 0)
665 return -1;
666
667 nrc = (u32)(nr & UINT_MAX);
668
669 nr = sysconf(_SC_NPROCESSORS_ONLN);
670 if (nr < 0)
671 return -1;
672
673 nra = (u32)(nr & UINT_MAX);
674
675 ret = do_write(fd, &nrc, sizeof(nrc));
676 if (ret < 0)
677 return ret;
678
679 return do_write(fd, &nra, sizeof(nra));
680}
681
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300682static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200683 struct perf_evlist *evlist)
684{
Robert Richter6606f872012-08-16 21:10:19 +0200685 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900686 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200687 int ret;
688
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900689 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200690
691 /*
692 * write number of events
693 */
694 ret = do_write(fd, &nre, sizeof(nre));
695 if (ret < 0)
696 return ret;
697
698 /*
699 * size of perf_event_attr struct
700 */
Robert Richter6606f872012-08-16 21:10:19 +0200701 sz = (u32)sizeof(evsel->attr);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200702 ret = do_write(fd, &sz, sizeof(sz));
703 if (ret < 0)
704 return ret;
705
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300706 evlist__for_each(evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +0200707 ret = do_write(fd, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200708 if (ret < 0)
709 return ret;
710 /*
711 * write number of unique id per event
712 * there is one id per instance of an event
713 *
714 * copy into an nri to be independent of the
715 * type of ids,
716 */
Robert Richter6606f872012-08-16 21:10:19 +0200717 nri = evsel->ids;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200718 ret = do_write(fd, &nri, sizeof(nri));
719 if (ret < 0)
720 return ret;
721
722 /*
723 * write event string as passed on cmdline
724 */
Robert Richter6606f872012-08-16 21:10:19 +0200725 ret = do_write_string(fd, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200726 if (ret < 0)
727 return ret;
728 /*
729 * write unique ids for this event
730 */
Robert Richter6606f872012-08-16 21:10:19 +0200731 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200732 if (ret < 0)
733 return ret;
734 }
735 return 0;
736}
737
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300738static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
739 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200740{
741 char buf[MAXPATHLEN];
742 char proc[32];
743 u32 i, n;
744 int ret;
745
746 /*
747 * actual atual path to perf binary
748 */
749 sprintf(proc, "/proc/%d/exe", getpid());
750 ret = readlink(proc, buf, sizeof(buf));
751 if (ret <= 0)
752 return -1;
753
754 /* readlink() does not add null termination */
755 buf[ret] = '\0';
756
757 /* account for binary path */
758 n = header_argc + 1;
759
760 ret = do_write(fd, &n, sizeof(n));
761 if (ret < 0)
762 return ret;
763
764 ret = do_write_string(fd, buf);
765 if (ret < 0)
766 return ret;
767
768 for (i = 0 ; i < header_argc; i++) {
769 ret = do_write_string(fd, header_argv[i]);
770 if (ret < 0)
771 return ret;
772 }
773 return 0;
774}
775
776#define CORE_SIB_FMT \
777 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
778#define THRD_SIB_FMT \
779 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
780
781struct cpu_topo {
782 u32 core_sib;
783 u32 thread_sib;
784 char **core_siblings;
785 char **thread_siblings;
786};
787
788static int build_cpu_topo(struct cpu_topo *tp, int cpu)
789{
790 FILE *fp;
791 char filename[MAXPATHLEN];
792 char *buf = NULL, *p;
793 size_t len = 0;
Stephane Eranianc5885742013-08-14 12:04:26 +0200794 ssize_t sret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200795 u32 i = 0;
796 int ret = -1;
797
798 sprintf(filename, CORE_SIB_FMT, cpu);
799 fp = fopen(filename, "r");
800 if (!fp)
Stephane Eranianc5885742013-08-14 12:04:26 +0200801 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200802
Stephane Eranianc5885742013-08-14 12:04:26 +0200803 sret = getline(&buf, &len, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200804 fclose(fp);
Stephane Eranianc5885742013-08-14 12:04:26 +0200805 if (sret <= 0)
806 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200807
808 p = strchr(buf, '\n');
809 if (p)
810 *p = '\0';
811
812 for (i = 0; i < tp->core_sib; i++) {
813 if (!strcmp(buf, tp->core_siblings[i]))
814 break;
815 }
816 if (i == tp->core_sib) {
817 tp->core_siblings[i] = buf;
818 tp->core_sib++;
819 buf = NULL;
820 len = 0;
821 }
Stephane Eranianc5885742013-08-14 12:04:26 +0200822 ret = 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200823
Stephane Eranianc5885742013-08-14 12:04:26 +0200824try_threads:
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200825 sprintf(filename, THRD_SIB_FMT, cpu);
826 fp = fopen(filename, "r");
827 if (!fp)
828 goto done;
829
830 if (getline(&buf, &len, fp) <= 0)
831 goto done;
832
833 p = strchr(buf, '\n');
834 if (p)
835 *p = '\0';
836
837 for (i = 0; i < tp->thread_sib; i++) {
838 if (!strcmp(buf, tp->thread_siblings[i]))
839 break;
840 }
841 if (i == tp->thread_sib) {
842 tp->thread_siblings[i] = buf;
843 tp->thread_sib++;
844 buf = NULL;
845 }
846 ret = 0;
847done:
848 if(fp)
849 fclose(fp);
850 free(buf);
851 return ret;
852}
853
854static void free_cpu_topo(struct cpu_topo *tp)
855{
856 u32 i;
857
858 if (!tp)
859 return;
860
861 for (i = 0 ; i < tp->core_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300862 zfree(&tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200863
864 for (i = 0 ; i < tp->thread_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300865 zfree(&tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200866
867 free(tp);
868}
869
870static struct cpu_topo *build_cpu_topology(void)
871{
872 struct cpu_topo *tp;
873 void *addr;
874 u32 nr, i;
875 size_t sz;
876 long ncpus;
877 int ret = -1;
878
879 ncpus = sysconf(_SC_NPROCESSORS_CONF);
880 if (ncpus < 0)
881 return NULL;
882
883 nr = (u32)(ncpus & UINT_MAX);
884
885 sz = nr * sizeof(char *);
886
887 addr = calloc(1, sizeof(*tp) + 2 * sz);
888 if (!addr)
889 return NULL;
890
891 tp = addr;
892
893 addr += sizeof(*tp);
894 tp->core_siblings = addr;
895 addr += sz;
896 tp->thread_siblings = addr;
897
898 for (i = 0; i < nr; i++) {
899 ret = build_cpu_topo(tp, i);
900 if (ret < 0)
901 break;
902 }
903 if (ret) {
904 free_cpu_topo(tp);
905 tp = NULL;
906 }
907 return tp;
908}
909
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300910static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
911 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200912{
913 struct cpu_topo *tp;
914 u32 i;
915 int ret;
916
917 tp = build_cpu_topology();
918 if (!tp)
919 return -1;
920
921 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
922 if (ret < 0)
923 goto done;
924
925 for (i = 0; i < tp->core_sib; i++) {
926 ret = do_write_string(fd, tp->core_siblings[i]);
927 if (ret < 0)
928 goto done;
929 }
930 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
931 if (ret < 0)
932 goto done;
933
934 for (i = 0; i < tp->thread_sib; i++) {
935 ret = do_write_string(fd, tp->thread_siblings[i]);
936 if (ret < 0)
937 break;
938 }
939done:
940 free_cpu_topo(tp);
941 return ret;
942}
943
944
945
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300946static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
947 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200948{
949 char *buf = NULL;
950 FILE *fp;
951 size_t len = 0;
952 int ret = -1, n;
953 uint64_t mem;
954
955 fp = fopen("/proc/meminfo", "r");
956 if (!fp)
957 return -1;
958
959 while (getline(&buf, &len, fp) > 0) {
960 ret = strncmp(buf, "MemTotal:", 9);
961 if (!ret)
962 break;
963 }
964 if (!ret) {
965 n = sscanf(buf, "%*s %"PRIu64, &mem);
966 if (n == 1)
967 ret = do_write(fd, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800968 } else
969 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200970 free(buf);
971 fclose(fp);
972 return ret;
973}
974
975static int write_topo_node(int fd, int node)
976{
977 char str[MAXPATHLEN];
978 char field[32];
979 char *buf = NULL, *p;
980 size_t len = 0;
981 FILE *fp;
982 u64 mem_total, mem_free, mem;
983 int ret = -1;
984
985 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
986 fp = fopen(str, "r");
987 if (!fp)
988 return -1;
989
990 while (getline(&buf, &len, fp) > 0) {
991 /* skip over invalid lines */
992 if (!strchr(buf, ':'))
993 continue;
Alan Coxa761a2d2014-01-20 19:10:11 +0100994 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200995 goto done;
996 if (!strcmp(field, "MemTotal:"))
997 mem_total = mem;
998 if (!strcmp(field, "MemFree:"))
999 mem_free = mem;
1000 }
1001
1002 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +01001003 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001004
1005 ret = do_write(fd, &mem_total, sizeof(u64));
1006 if (ret)
1007 goto done;
1008
1009 ret = do_write(fd, &mem_free, sizeof(u64));
1010 if (ret)
1011 goto done;
1012
1013 ret = -1;
1014 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
1015
1016 fp = fopen(str, "r");
1017 if (!fp)
1018 goto done;
1019
1020 if (getline(&buf, &len, fp) <= 0)
1021 goto done;
1022
1023 p = strchr(buf, '\n');
1024 if (p)
1025 *p = '\0';
1026
1027 ret = do_write_string(fd, buf);
1028done:
1029 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +01001030 if (fp)
1031 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001032 return ret;
1033}
1034
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001035static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
1036 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001037{
1038 char *buf = NULL;
1039 size_t len = 0;
1040 FILE *fp;
1041 struct cpu_map *node_map = NULL;
1042 char *c;
1043 u32 nr, i, j;
1044 int ret = -1;
1045
1046 fp = fopen("/sys/devices/system/node/online", "r");
1047 if (!fp)
1048 return -1;
1049
1050 if (getline(&buf, &len, fp) <= 0)
1051 goto done;
1052
1053 c = strchr(buf, '\n');
1054 if (c)
1055 *c = '\0';
1056
1057 node_map = cpu_map__new(buf);
1058 if (!node_map)
1059 goto done;
1060
1061 nr = (u32)node_map->nr;
1062
1063 ret = do_write(fd, &nr, sizeof(nr));
1064 if (ret < 0)
1065 goto done;
1066
1067 for (i = 0; i < nr; i++) {
1068 j = (u32)node_map->map[i];
1069 ret = do_write(fd, &j, sizeof(j));
1070 if (ret < 0)
1071 break;
1072
1073 ret = write_topo_node(fd, i);
1074 if (ret < 0)
1075 break;
1076 }
1077done:
1078 free(buf);
1079 fclose(fp);
1080 free(node_map);
1081 return ret;
1082}
1083
1084/*
Robert Richter50a96672012-08-16 21:10:24 +02001085 * File format:
1086 *
1087 * struct pmu_mappings {
1088 * u32 pmu_num;
1089 * struct pmu_map {
1090 * u32 type;
1091 * char name[];
1092 * }[pmu_num];
1093 * };
1094 */
1095
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001096static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
1097 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +02001098{
1099 struct perf_pmu *pmu = NULL;
1100 off_t offset = lseek(fd, 0, SEEK_CUR);
1101 __u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +09001102 int ret;
Robert Richter50a96672012-08-16 21:10:24 +02001103
1104 /* write real pmu_num later */
Namhyung Kim5323f602012-12-17 15:38:54 +09001105 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
1106 if (ret < 0)
1107 return ret;
Robert Richter50a96672012-08-16 21:10:24 +02001108
1109 while ((pmu = perf_pmu__scan(pmu))) {
1110 if (!pmu->name)
1111 continue;
1112 pmu_num++;
Namhyung Kim5323f602012-12-17 15:38:54 +09001113
1114 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
1115 if (ret < 0)
1116 return ret;
1117
1118 ret = do_write_string(fd, pmu->name);
1119 if (ret < 0)
1120 return ret;
Robert Richter50a96672012-08-16 21:10:24 +02001121 }
1122
1123 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1124 /* discard all */
1125 lseek(fd, offset, SEEK_SET);
1126 return -1;
1127 }
1128
1129 return 0;
1130}
1131
1132/*
Namhyung Kima8bb5592013-01-22 18:09:31 +09001133 * File format:
1134 *
1135 * struct group_descs {
1136 * u32 nr_groups;
1137 * struct group_desc {
1138 * char name[];
1139 * u32 leader_idx;
1140 * u32 nr_members;
1141 * }[nr_groups];
1142 * };
1143 */
1144static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
1145 struct perf_evlist *evlist)
1146{
1147 u32 nr_groups = evlist->nr_groups;
1148 struct perf_evsel *evsel;
1149 int ret;
1150
1151 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
1152 if (ret < 0)
1153 return ret;
1154
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001155 evlist__for_each(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001156 if (perf_evsel__is_group_leader(evsel) &&
1157 evsel->nr_members > 1) {
1158 const char *name = evsel->group_name ?: "{anon_group}";
1159 u32 leader_idx = evsel->idx;
1160 u32 nr_members = evsel->nr_members;
1161
1162 ret = do_write_string(fd, name);
1163 if (ret < 0)
1164 return ret;
1165
1166 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
1167 if (ret < 0)
1168 return ret;
1169
1170 ret = do_write(fd, &nr_members, sizeof(nr_members));
1171 if (ret < 0)
1172 return ret;
1173 }
1174 }
1175 return 0;
1176}
1177
1178/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001179 * default get_cpuid(): nothing gets recorded
1180 * actual implementation must be in arch/$(ARCH)/util/header.c
1181 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001182int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
1183 size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001184{
1185 return -1;
1186}
1187
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001188static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
1189 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001190{
1191 char buffer[64];
1192 int ret;
1193
1194 ret = get_cpuid(buffer, sizeof(buffer));
1195 if (!ret)
1196 goto write_it;
1197
1198 return -1;
1199write_it:
1200 return do_write_string(fd, buffer);
1201}
1202
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001203static int write_branch_stack(int fd __maybe_unused,
1204 struct perf_header *h __maybe_unused,
1205 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +01001206{
1207 return 0;
1208}
1209
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001210static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1211 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001212{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001213 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001214}
1215
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001216static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1217 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001218{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001219 fprintf(fp, "# os release : %s\n", ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001220}
1221
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001222static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001223{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001224 fprintf(fp, "# arch : %s\n", ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001225}
1226
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001227static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1228 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001229{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001230 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001231}
1232
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001233static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1234 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001235{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001236 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1237 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001238}
1239
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001240static void print_version(struct perf_header *ph, int fd __maybe_unused,
1241 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001242{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001243 fprintf(fp, "# perf version : %s\n", ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001244}
1245
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001246static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1247 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001248{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001249 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001250 char *str;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001251
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001252 nr = ph->env.nr_cmdline;
1253 str = ph->env.cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001254
1255 fprintf(fp, "# cmdline : ");
1256
1257 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001258 fprintf(fp, "%s ", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001259 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001260 }
1261 fputc('\n', fp);
1262}
1263
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001264static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1265 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001266{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001267 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001268 char *str;
1269
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001270 nr = ph->env.nr_sibling_cores;
1271 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001272
1273 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001274 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001275 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001276 }
1277
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001278 nr = ph->env.nr_sibling_threads;
1279 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001280
1281 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001282 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001283 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001284 }
1285}
1286
Robert Richter4e1b9c62012-08-16 21:10:22 +02001287static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001288{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001289 struct perf_evsel *evsel;
1290
1291 if (!events)
1292 return;
1293
1294 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001295 zfree(&evsel->name);
1296 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001297 }
1298
1299 free(events);
1300}
1301
1302static struct perf_evsel *
1303read_event_desc(struct perf_header *ph, int fd)
1304{
1305 struct perf_evsel *evsel, *events = NULL;
1306 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001307 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001308 u32 nre, sz, nr, i, j;
1309 ssize_t ret;
1310 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001311
1312 /* number of events */
Namhyung Kim5323f602012-12-17 15:38:54 +09001313 ret = readn(fd, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001314 if (ret != (ssize_t)sizeof(nre))
1315 goto error;
1316
1317 if (ph->needs_swap)
1318 nre = bswap_32(nre);
1319
Namhyung Kim5323f602012-12-17 15:38:54 +09001320 ret = readn(fd, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001321 if (ret != (ssize_t)sizeof(sz))
1322 goto error;
1323
1324 if (ph->needs_swap)
1325 sz = bswap_32(sz);
1326
Stephane Eranian62db9062012-02-09 23:21:07 +01001327 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001328 buf = malloc(sz);
1329 if (!buf)
1330 goto error;
1331
Robert Richter4e1b9c62012-08-16 21:10:22 +02001332 /* the last event terminates with evsel->attr.size == 0: */
1333 events = calloc(nre + 1, sizeof(*events));
1334 if (!events)
1335 goto error;
1336
1337 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001338 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001339 msz = sz;
1340
Robert Richter4e1b9c62012-08-16 21:10:22 +02001341 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1342 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001343
Stephane Eranian62db9062012-02-09 23:21:07 +01001344 /*
1345 * must read entire on-file attr struct to
1346 * sync up with layout.
1347 */
Namhyung Kim5323f602012-12-17 15:38:54 +09001348 ret = readn(fd, buf, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001349 if (ret != (ssize_t)sz)
1350 goto error;
1351
1352 if (ph->needs_swap)
1353 perf_event__attr_swap(buf);
1354
Robert Richter4e1b9c62012-08-16 21:10:22 +02001355 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001356
Namhyung Kim5323f602012-12-17 15:38:54 +09001357 ret = readn(fd, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001358 if (ret != (ssize_t)sizeof(nr))
1359 goto error;
1360
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001361 if (ph->needs_swap) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001362 nr = bswap_32(nr);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001363 evsel->needs_swap = true;
1364 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001365
Robert Richter4e1b9c62012-08-16 21:10:22 +02001366 evsel->name = do_read_string(fd, ph);
1367
1368 if (!nr)
1369 continue;
1370
1371 id = calloc(nr, sizeof(*id));
1372 if (!id)
1373 goto error;
1374 evsel->ids = nr;
1375 evsel->id = id;
1376
1377 for (j = 0 ; j < nr; j++) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001378 ret = readn(fd, id, sizeof(*id));
Robert Richter4e1b9c62012-08-16 21:10:22 +02001379 if (ret != (ssize_t)sizeof(*id))
1380 goto error;
1381 if (ph->needs_swap)
1382 *id = bswap_64(*id);
1383 id++;
1384 }
1385 }
1386out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001387 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001388 return events;
1389error:
1390 if (events)
1391 free_event_desc(events);
1392 events = NULL;
1393 goto out;
1394}
1395
1396static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1397{
1398 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1399 u32 j;
1400 u64 *id;
1401
1402 if (!events) {
1403 fprintf(fp, "# event desc: not available or unable to read\n");
1404 return;
1405 }
1406
1407 for (evsel = events; evsel->attr.size; evsel++) {
1408 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001409
1410 fprintf(fp, "type = %d, config = 0x%"PRIx64
1411 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
Robert Richter4e1b9c62012-08-16 21:10:22 +02001412 evsel->attr.type,
1413 (u64)evsel->attr.config,
1414 (u64)evsel->attr.config1,
1415 (u64)evsel->attr.config2);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001416
1417 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001418 evsel->attr.exclude_user,
1419 evsel->attr.exclude_kernel);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001420
David Ahern78b961f2012-07-20 17:25:52 -06001421 fprintf(fp, ", excl_host = %d, excl_guest = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001422 evsel->attr.exclude_host,
1423 evsel->attr.exclude_guest);
David Ahern78b961f2012-07-20 17:25:52 -06001424
Robert Richter4e1b9c62012-08-16 21:10:22 +02001425 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
David Ahern78b961f2012-07-20 17:25:52 -06001426
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001427 fprintf(fp, ", attr_mmap2 = %d", evsel->attr.mmap2);
1428 fprintf(fp, ", attr_mmap = %d", evsel->attr.mmap);
1429 fprintf(fp, ", attr_mmap_data = %d", evsel->attr.mmap_data);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001430 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001431 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001432 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1433 if (j)
1434 fputc(',', fp);
1435 fprintf(fp, " %"PRIu64, *id);
1436 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001437 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001438 }
1439
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001440 fputc('\n', fp);
1441 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001442
1443 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001444}
1445
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001446static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001447 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001448{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001449 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001450}
1451
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001452static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001453 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001454{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001455 u32 nr, c, i;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001456 char *str, *tmp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001457 uint64_t mem_total, mem_free;
1458
1459 /* nr nodes */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001460 nr = ph->env.nr_numa_nodes;
1461 str = ph->env.numa_nodes;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001462
1463 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001464 /* node number */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001465 c = strtoul(str, &tmp, 0);
1466 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001467 goto error;
1468
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001469 str = tmp + 1;
1470 mem_total = strtoull(str, &tmp, 0);
1471 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001472 goto error;
1473
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001474 str = tmp + 1;
1475 mem_free = strtoull(str, &tmp, 0);
1476 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001477 goto error;
1478
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001479 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1480 " free = %"PRIu64" kB\n",
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001481 c, mem_total, mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001482
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001483 str = tmp + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001484 fprintf(fp, "# node%u cpu list : %s\n", c, str);
Namhyung Kim12344712012-10-23 22:44:49 +09001485
1486 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001487 }
1488 return;
1489error:
1490 fprintf(fp, "# numa topology : not available\n");
1491}
1492
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001493static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001494{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001495 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001496}
1497
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001498static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001499 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001500{
1501 fprintf(fp, "# contains samples with branch stack\n");
1502}
1503
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001504static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1505 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001506{
1507 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001508 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001509 u32 pmu_num;
1510 u32 type;
1511
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001512 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001513 if (!pmu_num) {
1514 fprintf(fp, "# pmu mappings: not available\n");
1515 return;
1516 }
1517
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001518 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001519
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001520 while (pmu_num) {
1521 type = strtoul(str, &tmp, 0);
1522 if (*tmp != ':')
1523 goto error;
1524
1525 str = tmp + 1;
1526 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1527
Robert Richter50a96672012-08-16 21:10:24 +02001528 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001529 str += strlen(str) + 1;
1530 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001531 }
1532
1533 fprintf(fp, "\n");
1534
1535 if (!pmu_num)
1536 return;
1537error:
1538 fprintf(fp, "# pmu mappings: unable to read\n");
1539}
1540
Namhyung Kima8bb5592013-01-22 18:09:31 +09001541static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1542 FILE *fp)
1543{
1544 struct perf_session *session;
1545 struct perf_evsel *evsel;
1546 u32 nr = 0;
1547
1548 session = container_of(ph, struct perf_session, header);
1549
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001550 evlist__for_each(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001551 if (perf_evsel__is_group_leader(evsel) &&
1552 evsel->nr_members > 1) {
1553 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1554 perf_evsel__name(evsel));
1555
1556 nr = evsel->nr_members - 1;
1557 } else if (nr) {
1558 fprintf(fp, ",%s", perf_evsel__name(evsel));
1559
1560 if (--nr == 0)
1561 fprintf(fp, "}\n");
1562 }
1563 }
1564}
1565
Robert Richter08d95bd2012-02-10 15:41:55 +01001566static int __event_process_build_id(struct build_id_event *bev,
1567 char *filename,
1568 struct perf_session *session)
1569{
1570 int err = -1;
Waiman Long8fa7d872014-09-29 16:07:28 -04001571 struct dsos *dsos;
Robert Richter08d95bd2012-02-10 15:41:55 +01001572 struct machine *machine;
1573 u16 misc;
1574 struct dso *dso;
1575 enum dso_kernel_type dso_type;
1576
1577 machine = perf_session__findnew_machine(session, bev->pid);
1578 if (!machine)
1579 goto out;
1580
1581 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1582
1583 switch (misc) {
1584 case PERF_RECORD_MISC_KERNEL:
1585 dso_type = DSO_TYPE_KERNEL;
Waiman Long8fa7d872014-09-29 16:07:28 -04001586 dsos = &machine->kernel_dsos;
Robert Richter08d95bd2012-02-10 15:41:55 +01001587 break;
1588 case PERF_RECORD_MISC_GUEST_KERNEL:
1589 dso_type = DSO_TYPE_GUEST_KERNEL;
Waiman Long8fa7d872014-09-29 16:07:28 -04001590 dsos = &machine->kernel_dsos;
Robert Richter08d95bd2012-02-10 15:41:55 +01001591 break;
1592 case PERF_RECORD_MISC_USER:
1593 case PERF_RECORD_MISC_GUEST_USER:
1594 dso_type = DSO_TYPE_USER;
Waiman Long8fa7d872014-09-29 16:07:28 -04001595 dsos = &machine->user_dsos;
Robert Richter08d95bd2012-02-10 15:41:55 +01001596 break;
1597 default:
1598 goto out;
1599 }
1600
Waiman Long8fa7d872014-09-29 16:07:28 -04001601 dso = __dsos__findnew(dsos, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001602 if (dso != NULL) {
1603 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1604
1605 dso__set_build_id(dso, &bev->build_id);
1606
1607 if (filename[0] == '[')
1608 dso->kernel = dso_type;
1609
1610 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1611 sbuild_id);
1612 pr_debug("build id event received for %s: %s\n",
1613 dso->long_name, sbuild_id);
1614 }
1615
1616 err = 0;
1617out:
1618 return err;
1619}
1620
1621static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1622 int input, u64 offset, u64 size)
1623{
1624 struct perf_session *session = container_of(header, struct perf_session, header);
1625 struct {
1626 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001627 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001628 char filename[0];
1629 } old_bev;
1630 struct build_id_event bev;
1631 char filename[PATH_MAX];
1632 u64 limit = offset + size;
1633
1634 while (offset < limit) {
1635 ssize_t len;
1636
Namhyung Kim5323f602012-12-17 15:38:54 +09001637 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001638 return -1;
1639
1640 if (header->needs_swap)
1641 perf_event_header__bswap(&old_bev.header);
1642
1643 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001644 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001645 return -1;
1646
1647 bev.header = old_bev.header;
1648
1649 /*
1650 * As the pid is the missing value, we need to fill
1651 * it properly. The header.misc value give us nice hint.
1652 */
1653 bev.pid = HOST_KERNEL_ID;
1654 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1655 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1656 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1657
1658 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1659 __event_process_build_id(&bev, filename, session);
1660
1661 offset += bev.header.size;
1662 }
1663
1664 return 0;
1665}
1666
1667static int perf_header__read_build_ids(struct perf_header *header,
1668 int input, u64 offset, u64 size)
1669{
1670 struct perf_session *session = container_of(header, struct perf_session, header);
1671 struct build_id_event bev;
1672 char filename[PATH_MAX];
1673 u64 limit = offset + size, orig_offset = offset;
1674 int err = -1;
1675
1676 while (offset < limit) {
1677 ssize_t len;
1678
Namhyung Kim5323f602012-12-17 15:38:54 +09001679 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001680 goto out;
1681
1682 if (header->needs_swap)
1683 perf_event_header__bswap(&bev.header);
1684
1685 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001686 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001687 goto out;
1688 /*
1689 * The a1645ce1 changeset:
1690 *
1691 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1692 *
1693 * Added a field to struct build_id_event that broke the file
1694 * format.
1695 *
1696 * Since the kernel build-id is the first entry, process the
1697 * table using the old format if the well known
1698 * '[kernel.kallsyms]' string for the kernel build-id has the
1699 * first 4 characters chopped off (where the pid_t sits).
1700 */
1701 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1702 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1703 return -1;
1704 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1705 }
1706
1707 __event_process_build_id(&bev, filename, session);
1708
1709 offset += bev.header.size;
1710 }
1711 err = 0;
1712out:
1713 return err;
1714}
1715
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001716static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1717 struct perf_header *ph __maybe_unused,
1718 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001719{
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001720 ssize_t ret = trace_report(fd, data, false);
1721 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001722}
1723
1724static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001725 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001726 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001727{
1728 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1729 pr_debug("Failed to read buildids, continuing...\n");
1730 return 0;
1731}
1732
Namhyung Kima1ae5652012-09-24 17:14:59 +09001733static int process_hostname(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001734 struct perf_header *ph, int fd,
1735 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001736{
1737 ph->env.hostname = do_read_string(fd, ph);
1738 return ph->env.hostname ? 0 : -ENOMEM;
1739}
1740
1741static int process_osrelease(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001742 struct perf_header *ph, int fd,
1743 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001744{
1745 ph->env.os_release = do_read_string(fd, ph);
1746 return ph->env.os_release ? 0 : -ENOMEM;
1747}
1748
1749static int process_version(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001750 struct perf_header *ph, int fd,
1751 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001752{
1753 ph->env.version = do_read_string(fd, ph);
1754 return ph->env.version ? 0 : -ENOMEM;
1755}
1756
1757static int process_arch(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001758 struct perf_header *ph, int fd,
1759 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001760{
1761 ph->env.arch = do_read_string(fd, ph);
1762 return ph->env.arch ? 0 : -ENOMEM;
1763}
1764
1765static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001766 struct perf_header *ph, int fd,
1767 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001768{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001769 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001770 u32 nr;
1771
Namhyung Kim5323f602012-12-17 15:38:54 +09001772 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001773 if (ret != sizeof(nr))
1774 return -1;
1775
1776 if (ph->needs_swap)
1777 nr = bswap_32(nr);
1778
1779 ph->env.nr_cpus_online = nr;
1780
Namhyung Kim5323f602012-12-17 15:38:54 +09001781 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001782 if (ret != sizeof(nr))
1783 return -1;
1784
1785 if (ph->needs_swap)
1786 nr = bswap_32(nr);
1787
1788 ph->env.nr_cpus_avail = nr;
1789 return 0;
1790}
1791
1792static int process_cpudesc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001793 struct perf_header *ph, int fd,
1794 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001795{
1796 ph->env.cpu_desc = do_read_string(fd, ph);
1797 return ph->env.cpu_desc ? 0 : -ENOMEM;
1798}
1799
1800static int process_cpuid(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001801 struct perf_header *ph, int fd,
1802 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001803{
1804 ph->env.cpuid = do_read_string(fd, ph);
1805 return ph->env.cpuid ? 0 : -ENOMEM;
1806}
1807
1808static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001809 struct perf_header *ph, int fd,
1810 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001811{
1812 uint64_t mem;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001813 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001814
Namhyung Kim5323f602012-12-17 15:38:54 +09001815 ret = readn(fd, &mem, sizeof(mem));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001816 if (ret != sizeof(mem))
1817 return -1;
1818
1819 if (ph->needs_swap)
1820 mem = bswap_64(mem);
1821
1822 ph->env.total_mem = mem;
1823 return 0;
1824}
1825
Robert Richter7c2f7af2012-08-16 21:10:23 +02001826static struct perf_evsel *
1827perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1828{
1829 struct perf_evsel *evsel;
1830
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001831 evlist__for_each(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001832 if (evsel->idx == idx)
1833 return evsel;
1834 }
1835
1836 return NULL;
1837}
1838
1839static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001840perf_evlist__set_event_name(struct perf_evlist *evlist,
1841 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001842{
1843 struct perf_evsel *evsel;
1844
1845 if (!event->name)
1846 return;
1847
1848 evsel = perf_evlist__find_by_index(evlist, event->idx);
1849 if (!evsel)
1850 return;
1851
1852 if (evsel->name)
1853 return;
1854
1855 evsel->name = strdup(event->name);
1856}
1857
1858static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001859process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001860 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001861 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001862{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001863 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001864 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1865
1866 if (!events)
1867 return 0;
1868
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001869 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001870 for (evsel = events; evsel->attr.size; evsel++)
1871 perf_evlist__set_event_name(session->evlist, evsel);
1872
1873 free_event_desc(events);
1874
1875 return 0;
1876}
1877
Namhyung Kima1ae5652012-09-24 17:14:59 +09001878static int process_cmdline(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001879 struct perf_header *ph, int fd,
1880 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001881{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001882 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001883 char *str;
1884 u32 nr, i;
1885 struct strbuf sb;
1886
Namhyung Kim5323f602012-12-17 15:38:54 +09001887 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001888 if (ret != sizeof(nr))
1889 return -1;
1890
1891 if (ph->needs_swap)
1892 nr = bswap_32(nr);
1893
1894 ph->env.nr_cmdline = nr;
1895 strbuf_init(&sb, 128);
1896
1897 for (i = 0; i < nr; i++) {
1898 str = do_read_string(fd, ph);
1899 if (!str)
1900 goto error;
1901
1902 /* include a NULL character at the end */
1903 strbuf_add(&sb, str, strlen(str) + 1);
1904 free(str);
1905 }
1906 ph->env.cmdline = strbuf_detach(&sb, NULL);
1907 return 0;
1908
1909error:
1910 strbuf_release(&sb);
1911 return -1;
1912}
1913
1914static int process_cpu_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001915 struct perf_header *ph, int fd,
1916 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001917{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001918 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001919 u32 nr, i;
1920 char *str;
1921 struct strbuf sb;
1922
Namhyung Kim5323f602012-12-17 15:38:54 +09001923 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001924 if (ret != sizeof(nr))
1925 return -1;
1926
1927 if (ph->needs_swap)
1928 nr = bswap_32(nr);
1929
1930 ph->env.nr_sibling_cores = nr;
1931 strbuf_init(&sb, 128);
1932
1933 for (i = 0; i < nr; i++) {
1934 str = do_read_string(fd, ph);
1935 if (!str)
1936 goto error;
1937
1938 /* include a NULL character at the end */
1939 strbuf_add(&sb, str, strlen(str) + 1);
1940 free(str);
1941 }
1942 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1943
Namhyung Kim5323f602012-12-17 15:38:54 +09001944 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001945 if (ret != sizeof(nr))
1946 return -1;
1947
1948 if (ph->needs_swap)
1949 nr = bswap_32(nr);
1950
1951 ph->env.nr_sibling_threads = nr;
1952
1953 for (i = 0; i < nr; i++) {
1954 str = do_read_string(fd, ph);
1955 if (!str)
1956 goto error;
1957
1958 /* include a NULL character at the end */
1959 strbuf_add(&sb, str, strlen(str) + 1);
1960 free(str);
1961 }
1962 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1963 return 0;
1964
1965error:
1966 strbuf_release(&sb);
1967 return -1;
1968}
1969
1970static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001971 struct perf_header *ph, int fd,
1972 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001973{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001974 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001975 u32 nr, node, i;
1976 char *str;
1977 uint64_t mem_total, mem_free;
1978 struct strbuf sb;
1979
1980 /* nr nodes */
Namhyung Kim5323f602012-12-17 15:38:54 +09001981 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001982 if (ret != sizeof(nr))
1983 goto error;
1984
1985 if (ph->needs_swap)
1986 nr = bswap_32(nr);
1987
1988 ph->env.nr_numa_nodes = nr;
1989 strbuf_init(&sb, 256);
1990
1991 for (i = 0; i < nr; i++) {
1992 /* node number */
Namhyung Kim5323f602012-12-17 15:38:54 +09001993 ret = readn(fd, &node, sizeof(node));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001994 if (ret != sizeof(node))
1995 goto error;
1996
Namhyung Kim5323f602012-12-17 15:38:54 +09001997 ret = readn(fd, &mem_total, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001998 if (ret != sizeof(u64))
1999 goto error;
2000
Namhyung Kim5323f602012-12-17 15:38:54 +09002001 ret = readn(fd, &mem_free, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09002002 if (ret != sizeof(u64))
2003 goto error;
2004
2005 if (ph->needs_swap) {
2006 node = bswap_32(node);
2007 mem_total = bswap_64(mem_total);
2008 mem_free = bswap_64(mem_free);
2009 }
2010
2011 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
2012 node, mem_total, mem_free);
2013
2014 str = do_read_string(fd, ph);
2015 if (!str)
2016 goto error;
2017
2018 /* include a NULL character at the end */
2019 strbuf_add(&sb, str, strlen(str) + 1);
2020 free(str);
2021 }
2022 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
2023 return 0;
2024
2025error:
2026 strbuf_release(&sb);
2027 return -1;
2028}
2029
2030static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002031 struct perf_header *ph, int fd,
2032 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002033{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002034 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002035 char *name;
2036 u32 pmu_num;
2037 u32 type;
2038 struct strbuf sb;
2039
Namhyung Kim5323f602012-12-17 15:38:54 +09002040 ret = readn(fd, &pmu_num, sizeof(pmu_num));
Namhyung Kima1ae5652012-09-24 17:14:59 +09002041 if (ret != sizeof(pmu_num))
2042 return -1;
2043
2044 if (ph->needs_swap)
2045 pmu_num = bswap_32(pmu_num);
2046
2047 if (!pmu_num) {
2048 pr_debug("pmu mappings not available\n");
2049 return 0;
2050 }
2051
2052 ph->env.nr_pmu_mappings = pmu_num;
2053 strbuf_init(&sb, 128);
2054
2055 while (pmu_num) {
Namhyung Kim5323f602012-12-17 15:38:54 +09002056 if (readn(fd, &type, sizeof(type)) != sizeof(type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002057 goto error;
2058 if (ph->needs_swap)
2059 type = bswap_32(type);
2060
2061 name = do_read_string(fd, ph);
2062 if (!name)
2063 goto error;
2064
2065 strbuf_addf(&sb, "%u:%s", type, name);
2066 /* include a NULL character at the end */
2067 strbuf_add(&sb, "", 1);
2068
2069 free(name);
2070 pmu_num--;
2071 }
2072 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2073 return 0;
2074
2075error:
2076 strbuf_release(&sb);
2077 return -1;
2078}
2079
Namhyung Kima8bb5592013-01-22 18:09:31 +09002080static int process_group_desc(struct perf_file_section *section __maybe_unused,
2081 struct perf_header *ph, int fd,
2082 void *data __maybe_unused)
2083{
2084 size_t ret = -1;
2085 u32 i, nr, nr_groups;
2086 struct perf_session *session;
2087 struct perf_evsel *evsel, *leader = NULL;
2088 struct group_desc {
2089 char *name;
2090 u32 leader_idx;
2091 u32 nr_members;
2092 } *desc;
2093
2094 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
2095 return -1;
2096
2097 if (ph->needs_swap)
2098 nr_groups = bswap_32(nr_groups);
2099
2100 ph->env.nr_groups = nr_groups;
2101 if (!nr_groups) {
2102 pr_debug("group desc not available\n");
2103 return 0;
2104 }
2105
2106 desc = calloc(nr_groups, sizeof(*desc));
2107 if (!desc)
2108 return -1;
2109
2110 for (i = 0; i < nr_groups; i++) {
2111 desc[i].name = do_read_string(fd, ph);
2112 if (!desc[i].name)
2113 goto out_free;
2114
2115 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
2116 goto out_free;
2117
2118 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
2119 goto out_free;
2120
2121 if (ph->needs_swap) {
2122 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
2123 desc[i].nr_members = bswap_32(desc[i].nr_members);
2124 }
2125 }
2126
2127 /*
2128 * Rebuild group relationship based on the group_desc
2129 */
2130 session = container_of(ph, struct perf_session, header);
2131 session->evlist->nr_groups = nr_groups;
2132
2133 i = nr = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002134 evlist__for_each(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002135 if (evsel->idx == (int) desc[i].leader_idx) {
2136 evsel->leader = evsel;
2137 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09002138 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002139 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09002140 desc[i].name = NULL;
2141 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09002142 evsel->nr_members = desc[i].nr_members;
2143
2144 if (i >= nr_groups || nr > 0) {
2145 pr_debug("invalid group desc\n");
2146 goto out_free;
2147 }
2148
2149 leader = evsel;
2150 nr = evsel->nr_members - 1;
2151 i++;
2152 } else if (nr) {
2153 /* This is a group member */
2154 evsel->leader = leader;
2155
2156 nr--;
2157 }
2158 }
2159
2160 if (i != nr_groups || nr != 0) {
2161 pr_debug("invalid group desc\n");
2162 goto out_free;
2163 }
2164
2165 ret = 0;
2166out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002167 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002168 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002169 free(desc);
2170
2171 return ret;
2172}
2173
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002174struct feature_ops {
2175 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2176 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01002177 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002178 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002179 const char *name;
2180 bool full_only;
2181};
2182
Robert Richter8cdfa782011-12-07 10:02:56 +01002183#define FEAT_OPA(n, func) \
2184 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01002185#define FEAT_OPP(n, func) \
2186 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2187 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01002188#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01002189 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09002190 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01002191
2192/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002193#define print_tracing_data NULL
2194#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002195
2196static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002197 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01002198 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002199 FEAT_OPP(HEADER_HOSTNAME, hostname),
2200 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2201 FEAT_OPP(HEADER_VERSION, version),
2202 FEAT_OPP(HEADER_ARCH, arch),
2203 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2204 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09002205 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002206 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02002207 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002208 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01002209 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2210 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01002211 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002212 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09002213 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002214};
2215
2216struct header_print_data {
2217 FILE *fp;
2218 bool full; /* extended list of headers */
2219};
2220
2221static int perf_file_section__fprintf_info(struct perf_file_section *section,
2222 struct perf_header *ph,
2223 int feat, int fd, void *data)
2224{
2225 struct header_print_data *hd = data;
2226
2227 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2228 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2229 "%d, continuing...\n", section->offset, feat);
2230 return 0;
2231 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002232 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002233 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002234 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002235 }
2236 if (!feat_ops[feat].print)
2237 return 0;
2238
2239 if (!feat_ops[feat].full_only || hd->full)
2240 feat_ops[feat].print(ph, fd, hd->fp);
2241 else
2242 fprintf(hd->fp, "# %s info available, use -I to display\n",
2243 feat_ops[feat].name);
2244
2245 return 0;
2246}
2247
2248int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2249{
2250 struct header_print_data hd;
2251 struct perf_header *header = &session->header;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002252 int fd = perf_data_file__fd(session->file);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002253 hd.fp = fp;
2254 hd.full = full;
2255
2256 perf_header__process_sections(header, fd, &hd,
2257 perf_file_section__fprintf_info);
2258 return 0;
2259}
2260
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002261static int do_write_feat(int fd, struct perf_header *h, int type,
2262 struct perf_file_section **p,
2263 struct perf_evlist *evlist)
2264{
2265 int err;
2266 int ret = 0;
2267
2268 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002269 if (!feat_ops[type].write)
2270 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002271
2272 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2273
2274 err = feat_ops[type].write(fd, h, evlist);
2275 if (err < 0) {
2276 pr_debug("failed to write feature %d\n", type);
2277
2278 /* undo anything written */
2279 lseek(fd, (*p)->offset, SEEK_SET);
2280
2281 return -1;
2282 }
2283 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2284 (*p)++;
2285 }
2286 return ret;
2287}
2288
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002289static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002290 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002291{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002292 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002293 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002294 int sec_size;
2295 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002296 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002297 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002298
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002299 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002300 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002301 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002302
Paul Gortmaker91b98802013-01-30 20:05:49 -05002303 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002304 if (feat_sec == NULL)
2305 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002306
2307 sec_size = sizeof(*feat_sec) * nr_sections;
2308
Jiri Olsa8d541e92013-07-17 19:49:44 +02002309 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002310 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002311
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002312 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2313 if (do_write_feat(fd, header, feat, &p, evlist))
2314 perf_header__clear_feat(header, feat);
2315 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002316
Xiao Guangrongf887f302010-02-04 16:46:42 +08002317 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002318 /*
2319 * may write more than needed due to dropped feature, but
2320 * this is okay, reader will skip the mising entries
2321 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002322 err = do_write(fd, feat_sec, sec_size);
2323 if (err < 0)
2324 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002325 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002326 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002327}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002328
Tom Zanussi8dc58102010-04-01 23:59:15 -05002329int perf_header__write_pipe(int fd)
2330{
2331 struct perf_pipe_file_header f_header;
2332 int err;
2333
2334 f_header = (struct perf_pipe_file_header){
2335 .magic = PERF_MAGIC,
2336 .size = sizeof(f_header),
2337 };
2338
2339 err = do_write(fd, &f_header, sizeof(f_header));
2340 if (err < 0) {
2341 pr_debug("failed to write perf pipe header\n");
2342 return err;
2343 }
2344
2345 return 0;
2346}
2347
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002348int perf_session__write_header(struct perf_session *session,
2349 struct perf_evlist *evlist,
2350 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002351{
2352 struct perf_file_header f_header;
2353 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002354 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002355 struct perf_evsel *evsel;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002356 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002357 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002358
2359 lseek(fd, sizeof(f_header), SEEK_SET);
2360
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002361 evlist__for_each(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002362 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2363 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002364 if (err < 0) {
2365 pr_debug("failed to write perf header\n");
2366 return err;
2367 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002368 }
2369
Jiri Olsa944d62b2013-07-17 19:49:43 +02002370 attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002371
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002372 evlist__for_each(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002373 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002374 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002375 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002376 .offset = evsel->id_offset,
2377 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002378 }
2379 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002380 err = do_write(fd, &f_attr, sizeof(f_attr));
2381 if (err < 0) {
2382 pr_debug("failed to write perf header attribute\n");
2383 return err;
2384 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002385 }
2386
Adrian Hunterd645c442013-12-11 14:36:28 +02002387 if (!header->data_offset)
2388 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002389 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002390
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002391 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002392 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002393 if (err < 0)
2394 return err;
2395 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002396
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002397 f_header = (struct perf_file_header){
2398 .magic = PERF_MAGIC,
2399 .size = sizeof(f_header),
2400 .attr_size = sizeof(f_attr),
2401 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002402 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002403 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002404 },
2405 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002406 .offset = header->data_offset,
2407 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002408 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002409 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002410 };
2411
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002412 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002413
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002414 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002415 err = do_write(fd, &f_header, sizeof(f_header));
2416 if (err < 0) {
2417 pr_debug("failed to write perf header\n");
2418 return err;
2419 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002420 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002421
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002422 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002423}
2424
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002425static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002426 int fd, void *buf, size_t size)
2427{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002428 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002429 return -1;
2430
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002431 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002432 mem_bswap_64(buf, size);
2433
2434 return 0;
2435}
2436
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002437int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002438 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002439 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002440 struct perf_header *ph,
2441 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002442{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002443 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002444 int nr_sections;
2445 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002446 int feat;
2447 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002448
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002449 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002450 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002451 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002452
Paul Gortmaker91b98802013-01-30 20:05:49 -05002453 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002454 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002455 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002456
2457 sec_size = sizeof(*feat_sec) * nr_sections;
2458
Jiri Olsa8d541e92013-07-17 19:49:44 +02002459 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002460
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002461 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2462 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002463 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002464
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002465 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2466 err = process(sec++, header, feat, fd, data);
2467 if (err < 0)
2468 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002469 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002470 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002471out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002472 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002473 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002474}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002475
Stephane Eranian114382a2012-02-09 23:21:08 +01002476static const int attr_file_abi_sizes[] = {
2477 [0] = PERF_ATTR_SIZE_VER0,
2478 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002479 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002480 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian114382a2012-02-09 23:21:08 +01002481 0,
2482};
2483
2484/*
2485 * In the legacy file format, the magic number is not used to encode endianness.
2486 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2487 * on ABI revisions, we need to try all combinations for all endianness to
2488 * detect the endianness.
2489 */
2490static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2491{
2492 uint64_t ref_size, attr_size;
2493 int i;
2494
2495 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2496 ref_size = attr_file_abi_sizes[i]
2497 + sizeof(struct perf_file_section);
2498 if (hdr_sz != ref_size) {
2499 attr_size = bswap_64(hdr_sz);
2500 if (attr_size != ref_size)
2501 continue;
2502
2503 ph->needs_swap = true;
2504 }
2505 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2506 i,
2507 ph->needs_swap);
2508 return 0;
2509 }
2510 /* could not determine endianness */
2511 return -1;
2512}
2513
2514#define PERF_PIPE_HDR_VER0 16
2515
2516static const size_t attr_pipe_abi_sizes[] = {
2517 [0] = PERF_PIPE_HDR_VER0,
2518 0,
2519};
2520
2521/*
2522 * In the legacy pipe format, there is an implicit assumption that endiannesss
2523 * between host recording the samples, and host parsing the samples is the
2524 * same. This is not always the case given that the pipe output may always be
2525 * redirected into a file and analyzed on a different machine with possibly a
2526 * different endianness and perf_event ABI revsions in the perf tool itself.
2527 */
2528static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2529{
2530 u64 attr_size;
2531 int i;
2532
2533 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2534 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2535 attr_size = bswap_64(hdr_sz);
2536 if (attr_size != hdr_sz)
2537 continue;
2538
2539 ph->needs_swap = true;
2540 }
2541 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2542 return 0;
2543 }
2544 return -1;
2545}
2546
Feng Tange84ba4e2012-10-30 11:56:07 +08002547bool is_perf_magic(u64 magic)
2548{
2549 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2550 || magic == __perf_magic2
2551 || magic == __perf_magic2_sw)
2552 return true;
2553
2554 return false;
2555}
2556
Stephane Eranian114382a2012-02-09 23:21:08 +01002557static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2558 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002559{
2560 int ret;
2561
2562 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002563 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002564 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002565 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002566 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002567 if (is_pipe)
2568 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002569
Stephane Eranian114382a2012-02-09 23:21:08 +01002570 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002571 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002572 /*
2573 * the new magic number serves two purposes:
2574 * - unique number to identify actual perf.data files
2575 * - encode endianness of file
2576 */
Stephane Eranian73323f52012-02-02 13:54:44 +01002577
Stephane Eranian114382a2012-02-09 23:21:08 +01002578 /* check magic number with one endianness */
2579 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002580 return 0;
2581
Stephane Eranian114382a2012-02-09 23:21:08 +01002582 /* check magic number with opposite endianness */
2583 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002584 return -1;
2585
2586 ph->needs_swap = true;
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002587 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002588
2589 return 0;
2590}
2591
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002592int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002593 struct perf_header *ph, int fd)
2594{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002595 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002596
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002597 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002598
Stephane Eranian73323f52012-02-02 13:54:44 +01002599 ret = readn(fd, header, sizeof(*header));
2600 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002601 return -1;
2602
Stephane Eranian114382a2012-02-09 23:21:08 +01002603 if (check_magic_endian(header->magic,
2604 header->attr_size, false, ph) < 0) {
2605 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002606 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002607 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002608
Stephane Eranian73323f52012-02-02 13:54:44 +01002609 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002610 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002611 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002612 }
2613
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002614 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002615 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002616 if (header->size == offsetof(typeof(*header), adds_features))
2617 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002618 else
2619 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002620 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002621 /*
2622 * feature bitmap is declared as an array of unsigned longs --
2623 * not good since its size can differ between the host that
2624 * generated the data file and the host analyzing the file.
2625 *
2626 * We need to handle endianness, but we don't know the size of
2627 * the unsigned long where the file was generated. Take a best
2628 * guess at determining it: try 64-bit swap first (ie., file
2629 * created on a 64-bit host), and check if the hostname feature
2630 * bit is set (this feature bit is forced on as of fbe96f2).
2631 * If the bit is not, undo the 64-bit swap and try a 32-bit
2632 * swap. If the hostname bit is still not set (e.g., older data
2633 * file), punt and fallback to the original behavior --
2634 * clearing all feature bits and setting buildid.
2635 */
David Ahern80c01202012-06-08 11:47:51 -03002636 mem_bswap_64(&header->adds_features,
2637 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002638
2639 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002640 /* unswap as u64 */
2641 mem_bswap_64(&header->adds_features,
2642 BITS_TO_U64(HEADER_FEAT_BITS));
2643
2644 /* unswap as u32 */
2645 mem_bswap_32(&header->adds_features,
2646 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002647 }
2648
2649 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2650 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2651 set_bit(HEADER_BUILD_ID, header->adds_features);
2652 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002653 }
2654
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002655 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002656 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002657
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002658 ph->data_offset = header->data.offset;
2659 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002660 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002661 return 0;
2662}
2663
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002664static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002665 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002666 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002667{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002668 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002669 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002670 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002671 return 0;
2672 }
2673
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002674 if (feat >= HEADER_LAST_FEATURE) {
2675 pr_debug("unknown feature %d, continuing...\n", feat);
2676 return 0;
2677 }
2678
Robert Richterf1c67db2012-02-10 15:41:56 +01002679 if (!feat_ops[feat].process)
2680 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002681
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002682 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002683}
2684
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002685static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002686 struct perf_header *ph, int fd,
2687 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002688{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002689 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002690
2691 ret = readn(fd, header, sizeof(*header));
2692 if (ret <= 0)
2693 return -1;
2694
Stephane Eranian114382a2012-02-09 23:21:08 +01002695 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2696 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002697 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002698 }
2699
2700 if (ph->needs_swap)
2701 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002702
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002703 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002704 return -1;
2705
Tom Zanussi8dc58102010-04-01 23:59:15 -05002706 return 0;
2707}
2708
Jiri Olsad4339562013-07-17 19:49:41 +02002709static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002710{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002711 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002712 struct perf_pipe_file_header f_header;
2713
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002714 if (perf_file_header__read_pipe(&f_header, header,
2715 perf_data_file__fd(session->file),
Tom Zanussi454c4072010-05-01 01:41:20 -05002716 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002717 pr_debug("incompatible file format\n");
2718 return -EINVAL;
2719 }
2720
Tom Zanussi8dc58102010-04-01 23:59:15 -05002721 return 0;
2722}
2723
Stephane Eranian69996df2012-02-09 23:21:06 +01002724static int read_attr(int fd, struct perf_header *ph,
2725 struct perf_file_attr *f_attr)
2726{
2727 struct perf_event_attr *attr = &f_attr->attr;
2728 size_t sz, left;
2729 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002730 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002731
2732 memset(f_attr, 0, sizeof(*f_attr));
2733
2734 /* read minimal guaranteed structure */
2735 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2736 if (ret <= 0) {
2737 pr_debug("cannot read %d bytes of header attr\n",
2738 PERF_ATTR_SIZE_VER0);
2739 return -1;
2740 }
2741
2742 /* on file perf_event_attr size */
2743 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002744
Stephane Eranian69996df2012-02-09 23:21:06 +01002745 if (ph->needs_swap)
2746 sz = bswap_32(sz);
2747
2748 if (sz == 0) {
2749 /* assume ABI0 */
2750 sz = PERF_ATTR_SIZE_VER0;
2751 } else if (sz > our_sz) {
2752 pr_debug("file uses a more recent and unsupported ABI"
2753 " (%zu bytes extra)\n", sz - our_sz);
2754 return -1;
2755 }
2756 /* what we have not yet read and that we know about */
2757 left = sz - PERF_ATTR_SIZE_VER0;
2758 if (left) {
2759 void *ptr = attr;
2760 ptr += PERF_ATTR_SIZE_VER0;
2761
2762 ret = readn(fd, ptr, left);
2763 }
2764 /* read perf_file_section, ids are read in caller */
2765 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2766
2767 return ret <= 0 ? -1 : 0;
2768}
2769
Namhyung Kim831394b2012-09-06 11:10:46 +09002770static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2771 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002772{
Namhyung Kim831394b2012-09-06 11:10:46 +09002773 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002774 char bf[128];
2775
Namhyung Kim831394b2012-09-06 11:10:46 +09002776 /* already prepared */
2777 if (evsel->tp_format)
2778 return 0;
2779
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002780 if (pevent == NULL) {
2781 pr_debug("broken or missing trace data\n");
2782 return -1;
2783 }
2784
Namhyung Kim831394b2012-09-06 11:10:46 +09002785 event = pevent_find_event(pevent, evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002786 if (event == NULL)
2787 return -1;
2788
Namhyung Kim831394b2012-09-06 11:10:46 +09002789 if (!evsel->name) {
2790 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2791 evsel->name = strdup(bf);
2792 if (evsel->name == NULL)
2793 return -1;
2794 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002795
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002796 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002797 return 0;
2798}
2799
Namhyung Kim831394b2012-09-06 11:10:46 +09002800static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2801 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002802{
2803 struct perf_evsel *pos;
2804
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002805 evlist__for_each(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002806 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2807 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002808 return -1;
2809 }
2810
2811 return 0;
2812}
2813
Jiri Olsad4339562013-07-17 19:49:41 +02002814int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002815{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002816 struct perf_data_file *file = session->file;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002817 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002818 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002819 struct perf_file_attr f_attr;
2820 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002821 int nr_attrs, nr_ids, i, j;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002822 int fd = perf_data_file__fd(file);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002823
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002824 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002825 if (session->evlist == NULL)
2826 return -ENOMEM;
2827
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002828 if (perf_data_file__is_pipe(file))
Jiri Olsad4339562013-07-17 19:49:41 +02002829 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002830
Stephane Eranian69996df2012-02-09 23:21:06 +01002831 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002832 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002833
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002834 /*
2835 * Sanity check that perf.data was written cleanly; data size is
2836 * initialized to 0 and updated only if the on_exit function is run.
2837 * If data size is still 0 then the file contains only partial
2838 * information. Just warn user and process it as much as it can.
2839 */
2840 if (f_header.data.size == 0) {
2841 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2842 "Was the 'perf record' command properly terminated?\n",
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002843 file->path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002844 }
2845
Stephane Eranian69996df2012-02-09 23:21:06 +01002846 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002847 lseek(fd, f_header.attrs.offset, SEEK_SET);
2848
2849 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002850 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002851 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002852
Stephane Eranian69996df2012-02-09 23:21:06 +01002853 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002854 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002855
David Aherneda39132011-07-15 12:34:09 -06002856 if (header->needs_swap)
2857 perf_event__attr_swap(&f_attr.attr);
2858
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002859 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002860 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002861
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002862 if (evsel == NULL)
2863 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002864
2865 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002866 /*
2867 * Do it before so that if perf_evsel__alloc_id fails, this
2868 * entry gets purged too at perf_evlist__delete().
2869 */
2870 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002871
2872 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002873 /*
2874 * We don't have the cpu and thread maps on the header, so
2875 * for allocating the perf_sample_id table we fake 1 cpu and
2876 * hattr->ids threads.
2877 */
2878 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2879 goto out_delete_evlist;
2880
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002881 lseek(fd, f_attr.ids.offset, SEEK_SET);
2882
2883 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002884 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002885 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002886
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002887 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002888 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002889
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002890 lseek(fd, tmp, SEEK_SET);
2891 }
2892
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002893 symbol_conf.nr_events = nr_attrs;
2894
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002895 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002896 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002897
Namhyung Kim831394b2012-09-06 11:10:46 +09002898 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002899 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002900 goto out_delete_evlist;
2901
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002902 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002903out_errno:
2904 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002905
2906out_delete_evlist:
2907 perf_evlist__delete(session->evlist);
2908 session->evlist = NULL;
2909 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002910}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002911
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002912int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002913 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002914 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002915{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002916 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002917 size_t size;
2918 int err;
2919
2920 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002921 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002922 size += sizeof(struct perf_event_header);
2923 size += ids * sizeof(u64);
2924
2925 ev = malloc(size);
2926
Chris Samuelce47dc52010-11-13 13:35:06 +11002927 if (ev == NULL)
2928 return -ENOMEM;
2929
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002930 ev->attr.attr = *attr;
2931 memcpy(ev->attr.id, id, ids * sizeof(u64));
2932
2933 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002934 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002935
Robert Richterf4d83432012-08-16 21:10:17 +02002936 if (ev->attr.header.size == size)
2937 err = process(tool, ev, NULL, NULL);
2938 else
2939 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002940
2941 free(ev);
2942
2943 return err;
2944}
2945
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002946int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002947 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002948 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002949{
Robert Richter6606f872012-08-16 21:10:19 +02002950 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002951 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002952
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002953 evlist__for_each(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002954 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2955 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002956 if (err) {
2957 pr_debug("failed to create perf header attribute\n");
2958 return err;
2959 }
2960 }
2961
2962 return err;
2963}
2964
Adrian Hunter47c3d102013-07-04 16:20:21 +03002965int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2966 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002967 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002968{
Robert Richterf4d83432012-08-16 21:10:17 +02002969 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002970 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002971 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002972
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002973 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002974 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002975 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002976 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002977 }
2978
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002979 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002980 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002981 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002982
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002983 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002984
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002985 ids = event->header.size;
2986 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002987 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002988 /*
2989 * We don't have the cpu and thread maps on the header, so
2990 * for allocating the perf_sample_id table we fake 1 cpu and
2991 * hattr->ids threads.
2992 */
2993 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2994 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002995
2996 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002997 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002998 }
2999
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03003000 symbol_conf.nr_events = evlist->nr_entries;
3001
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003002 return 0;
3003}
Tom Zanussicd19a032010-04-01 23:59:20 -05003004
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003005int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003006 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003007 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05003008{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003009 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02003010 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05003011 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003012 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05003013
Jiri Olsa29208e52011-10-20 15:59:43 +02003014 /*
3015 * We are going to store the size of the data followed
3016 * by the data contents. Since the fd descriptor is a pipe,
3017 * we cannot seek back to store the size of the data once
3018 * we know it. Instead we:
3019 *
3020 * - write the tracing data to the temp file
3021 * - get/write the data size to pipe
3022 * - write the tracing data from the temp file
3023 * to the pipe
3024 */
3025 tdata = tracing_data_get(&evlist->entries, fd, true);
3026 if (!tdata)
3027 return -1;
3028
Tom Zanussi92155452010-04-01 23:59:21 -05003029 memset(&ev, 0, sizeof(ev));
3030
3031 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02003032 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003033 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05003034 padding = aligned_size - size;
3035 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3036 ev.tracing_data.size = aligned_size;
3037
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003038 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05003039
Jiri Olsa29208e52011-10-20 15:59:43 +02003040 /*
3041 * The put function will copy all the tracing data
3042 * stored in temp file to the pipe.
3043 */
3044 tracing_data_put(tdata);
3045
Tom Zanussi92155452010-04-01 23:59:21 -05003046 write_padded(fd, NULL, 0, padding);
3047
3048 return aligned_size;
3049}
3050
Adrian Hunter47c3d102013-07-04 16:20:21 +03003051int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3052 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003053 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05003054{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003055 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003056 int fd = perf_data_file__fd(session->file);
3057 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05003058 char buf[BUFSIZ];
3059
3060 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003061 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05003062 SEEK_SET);
3063
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003064 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003065 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003066 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003067
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003068 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003069 pr_err("%s: reading input file", __func__);
3070 return -1;
3071 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003072 if (session->repipe) {
3073 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003074 if (retw <= 0 || retw != padding) {
3075 pr_err("%s: repiping tracing data padding", __func__);
3076 return -1;
3077 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003078 }
Tom Zanussi92155452010-04-01 23:59:21 -05003079
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003080 if (size_read + padding != size) {
3081 pr_err("%s: tracing data size mismatch", __func__);
3082 return -1;
3083 }
Tom Zanussi92155452010-04-01 23:59:21 -05003084
Namhyung Kim831394b2012-09-06 11:10:46 +09003085 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003086 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003087
Tom Zanussi92155452010-04-01 23:59:21 -05003088 return size_read + padding;
3089}
Tom Zanussic7929e42010-04-01 23:59:22 -05003090
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003091int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003092 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003093 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003094 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003095{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003096 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003097 size_t len;
3098 int err = 0;
3099
3100 if (!pos->hit)
3101 return err;
3102
3103 memset(&ev, 0, sizeof(ev));
3104
3105 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003106 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003107 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3108 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3109 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003110 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003111 ev.build_id.header.size = sizeof(ev.build_id) + len;
3112 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3113
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003114 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003115
3116 return err;
3117}
3118
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003119int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003120 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003121 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003122{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003123 __event_process_build_id(&event->build_id,
3124 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003125 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003126 return 0;
3127}
Stephane Eraniana1ac1d32010-06-17 11:39:01 +02003128
3129void disable_buildid_cache(void)
3130{
3131 no_buildid_cache = true;
3132}