blob: 8fd7b7de1acd702b64a91d5368f2ca8a650a09f2 [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 Eranianfbe96f22011-09-30 15:40:40 +020027static u32 header_argc;
28static const char **header_argv;
29
Stephane Eranian73323f52012-02-02 13:54:44 +010030/*
31 * magic2 = "PERFILE2"
32 * must be a numerical value to let the endianness
33 * determine the memory layout. That way we are able
34 * to detect endianness when reading the perf.data file
35 * back.
36 *
37 * we check for legacy (PERFFILE) format.
38 */
39static const char *__perf_magic1 = "PERFFILE";
40static const u64 __perf_magic2 = 0x32454c4946524550ULL;
41static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020042
Stephane Eranian73323f52012-02-02 13:54:44 +010043#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020044
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020045struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020046 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020047 struct perf_file_section ids;
48};
49
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030050void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020051{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030052 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020053}
54
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030055void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020056{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030057 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020058}
59
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030060bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020061{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030062 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020063}
64
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020065static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020066{
67 while (size) {
68 int ret = write(fd, buf, size);
69
70 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -020071 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020072
73 size -= ret;
74 buf += ret;
75 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020076
77 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020078}
79
Namhyung Kime195fac2014-11-04 10:14:30 +090080int write_padded(int fd, const void *bf, size_t count, size_t count_aligned)
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -020081{
82 static const char zero_buf[NAME_ALIGN];
83 int err = do_write(fd, bf, count);
84
85 if (!err)
86 err = do_write(fd, zero_buf, count_aligned - count);
87
88 return err;
89}
90
Kan Liang2bb00d22015-09-01 09:58:12 -040091#define string_size(str) \
92 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
93
Stephane Eranianfbe96f22011-09-30 15:40:40 +020094static int do_write_string(int fd, const char *str)
95{
96 u32 len, olen;
97 int ret;
98
99 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300100 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200101
102 /* write len, incl. \0 */
103 ret = do_write(fd, &len, sizeof(len));
104 if (ret < 0)
105 return ret;
106
107 return write_padded(fd, str, olen, len);
108}
109
110static char *do_read_string(int fd, struct perf_header *ph)
111{
112 ssize_t sz, ret;
113 u32 len;
114 char *buf;
115
Namhyung Kim5323f602012-12-17 15:38:54 +0900116 sz = readn(fd, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200117 if (sz < (ssize_t)sizeof(len))
118 return NULL;
119
120 if (ph->needs_swap)
121 len = bswap_32(len);
122
123 buf = malloc(len);
124 if (!buf)
125 return NULL;
126
Namhyung Kim5323f602012-12-17 15:38:54 +0900127 ret = readn(fd, buf, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200128 if (ret == (ssize_t)len) {
129 /*
130 * strings are padded by zeroes
131 * thus the actual strlen of buf
132 * may be less than len
133 */
134 return buf;
135 }
136
137 free(buf);
138 return NULL;
139}
140
141int
142perf_header__set_cmdline(int argc, const char **argv)
143{
144 int i;
145
David Ahern56e6f602012-07-29 20:53:51 -0600146 /*
147 * If header_argv has already been set, do not override it.
148 * This allows a command to set the cmdline, parse args and
149 * then call another builtin function that implements a
150 * command -- e.g, cmd_kvm calling cmd_record.
151 */
152 if (header_argv)
153 return 0;
154
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200155 header_argc = (u32)argc;
156
157 /* do not include NULL termination */
158 header_argv = calloc(argc, sizeof(char *));
159 if (!header_argv)
160 return -ENOMEM;
161
162 /*
163 * must copy argv contents because it gets moved
164 * around during option parsing
165 */
166 for (i = 0; i < argc ; i++)
167 header_argv[i] = argv[i];
168
169 return 0;
170}
171
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300172static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200173 struct perf_evlist *evlist)
174{
175 return read_tracing_data(fd, &evlist->entries);
176}
177
178
179static int write_build_id(int fd, struct perf_header *h,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300180 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200181{
182 struct perf_session *session;
183 int err;
184
185 session = container_of(h, struct perf_session, header);
186
Robert Richtere20960c2011-12-07 10:02:55 +0100187 if (!perf_session__read_build_ids(session, true))
188 return -1;
189
Namhyung Kim714c9c42014-11-04 10:14:29 +0900190 err = perf_session__write_buildid_table(session, fd);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200191 if (err < 0) {
192 pr_debug("failed to write buildid table\n");
193 return err;
194 }
Namhyung Kim73c5d222014-11-07 22:57:56 +0900195 perf_session__cache_build_ids(session);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200196
197 return 0;
198}
199
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300200static int write_hostname(int fd, struct perf_header *h __maybe_unused,
201 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200202{
203 struct utsname uts;
204 int ret;
205
206 ret = uname(&uts);
207 if (ret < 0)
208 return -1;
209
210 return do_write_string(fd, uts.nodename);
211}
212
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300213static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
214 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200215{
216 struct utsname uts;
217 int ret;
218
219 ret = uname(&uts);
220 if (ret < 0)
221 return -1;
222
223 return do_write_string(fd, uts.release);
224}
225
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300226static int write_arch(int fd, struct perf_header *h __maybe_unused,
227 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200228{
229 struct utsname uts;
230 int ret;
231
232 ret = uname(&uts);
233 if (ret < 0)
234 return -1;
235
236 return do_write_string(fd, uts.machine);
237}
238
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300239static int write_version(int fd, struct perf_header *h __maybe_unused,
240 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200241{
242 return do_write_string(fd, perf_version_string);
243}
244
Wang Nan493c3032014-10-24 09:45:26 +0800245static int __write_cpudesc(int fd, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200246{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200247 FILE *file;
248 char *buf = NULL;
249 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800250 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200251 size_t len = 0;
252 int ret = -1;
253
254 if (!search)
255 return -1;
256
257 file = fopen("/proc/cpuinfo", "r");
258 if (!file)
259 return -1;
260
261 while (getline(&buf, &len, file) > 0) {
262 ret = strncmp(buf, search, strlen(search));
263 if (!ret)
264 break;
265 }
266
Wang Naned307752014-10-16 11:08:29 +0800267 if (ret) {
268 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200269 goto done;
Wang Naned307752014-10-16 11:08:29 +0800270 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200271
272 s = buf;
273
274 p = strchr(buf, ':');
275 if (p && *(p+1) == ' ' && *(p+2))
276 s = p + 2;
277 p = strchr(s, '\n');
278 if (p)
279 *p = '\0';
280
281 /* squash extra space characters (branding string) */
282 p = s;
283 while (*p) {
284 if (isspace(*p)) {
285 char *r = p + 1;
286 char *q = r;
287 *p = ' ';
288 while (*q && isspace(*q))
289 q++;
290 if (q != (p+1))
291 while ((*r++ = *q++));
292 }
293 p++;
294 }
295 ret = do_write_string(fd, s);
296done:
297 free(buf);
298 fclose(file);
299 return ret;
300}
301
Wang Nan493c3032014-10-24 09:45:26 +0800302static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
303 struct perf_evlist *evlist __maybe_unused)
304{
305#ifndef CPUINFO_PROC
306#define CPUINFO_PROC {"model name", }
307#endif
308 const char *cpuinfo_procs[] = CPUINFO_PROC;
309 unsigned int i;
310
311 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
312 int ret;
313 ret = __write_cpudesc(fd, cpuinfo_procs[i]);
314 if (ret >= 0)
315 return ret;
316 }
317 return -1;
318}
319
320
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300321static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
322 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200323{
324 long nr;
325 u32 nrc, nra;
326 int ret;
327
328 nr = sysconf(_SC_NPROCESSORS_CONF);
329 if (nr < 0)
330 return -1;
331
332 nrc = (u32)(nr & UINT_MAX);
333
334 nr = sysconf(_SC_NPROCESSORS_ONLN);
335 if (nr < 0)
336 return -1;
337
338 nra = (u32)(nr & UINT_MAX);
339
340 ret = do_write(fd, &nrc, sizeof(nrc));
341 if (ret < 0)
342 return ret;
343
344 return do_write(fd, &nra, sizeof(nra));
345}
346
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300347static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200348 struct perf_evlist *evlist)
349{
Robert Richter6606f872012-08-16 21:10:19 +0200350 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900351 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200352 int ret;
353
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900354 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200355
356 /*
357 * write number of events
358 */
359 ret = do_write(fd, &nre, sizeof(nre));
360 if (ret < 0)
361 return ret;
362
363 /*
364 * size of perf_event_attr struct
365 */
Robert Richter6606f872012-08-16 21:10:19 +0200366 sz = (u32)sizeof(evsel->attr);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200367 ret = do_write(fd, &sz, sizeof(sz));
368 if (ret < 0)
369 return ret;
370
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300371 evlist__for_each(evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +0200372 ret = do_write(fd, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200373 if (ret < 0)
374 return ret;
375 /*
376 * write number of unique id per event
377 * there is one id per instance of an event
378 *
379 * copy into an nri to be independent of the
380 * type of ids,
381 */
Robert Richter6606f872012-08-16 21:10:19 +0200382 nri = evsel->ids;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200383 ret = do_write(fd, &nri, sizeof(nri));
384 if (ret < 0)
385 return ret;
386
387 /*
388 * write event string as passed on cmdline
389 */
Robert Richter6606f872012-08-16 21:10:19 +0200390 ret = do_write_string(fd, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200391 if (ret < 0)
392 return ret;
393 /*
394 * write unique ids for this event
395 */
Robert Richter6606f872012-08-16 21:10:19 +0200396 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200397 if (ret < 0)
398 return ret;
399 }
400 return 0;
401}
402
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300403static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
404 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200405{
406 char buf[MAXPATHLEN];
407 char proc[32];
408 u32 i, n;
409 int ret;
410
411 /*
412 * actual atual path to perf binary
413 */
414 sprintf(proc, "/proc/%d/exe", getpid());
415 ret = readlink(proc, buf, sizeof(buf));
416 if (ret <= 0)
417 return -1;
418
419 /* readlink() does not add null termination */
420 buf[ret] = '\0';
421
422 /* account for binary path */
423 n = header_argc + 1;
424
425 ret = do_write(fd, &n, sizeof(n));
426 if (ret < 0)
427 return ret;
428
429 ret = do_write_string(fd, buf);
430 if (ret < 0)
431 return ret;
432
433 for (i = 0 ; i < header_argc; i++) {
434 ret = do_write_string(fd, header_argv[i]);
435 if (ret < 0)
436 return ret;
437 }
438 return 0;
439}
440
441#define CORE_SIB_FMT \
442 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
443#define THRD_SIB_FMT \
444 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
445
446struct cpu_topo {
Kan Liang2bb00d22015-09-01 09:58:12 -0400447 u32 cpu_nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200448 u32 core_sib;
449 u32 thread_sib;
450 char **core_siblings;
451 char **thread_siblings;
Kan Liang2bb00d22015-09-01 09:58:12 -0400452 int *core_id;
453 int *phy_pkg_id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200454};
455
456static int build_cpu_topo(struct cpu_topo *tp, int cpu)
457{
458 FILE *fp;
459 char filename[MAXPATHLEN];
460 char *buf = NULL, *p;
461 size_t len = 0;
Stephane Eranianc5885742013-08-14 12:04:26 +0200462 ssize_t sret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200463 u32 i = 0;
464 int ret = -1;
465
466 sprintf(filename, CORE_SIB_FMT, cpu);
467 fp = fopen(filename, "r");
468 if (!fp)
Stephane Eranianc5885742013-08-14 12:04:26 +0200469 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200470
Stephane Eranianc5885742013-08-14 12:04:26 +0200471 sret = getline(&buf, &len, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200472 fclose(fp);
Stephane Eranianc5885742013-08-14 12:04:26 +0200473 if (sret <= 0)
474 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200475
476 p = strchr(buf, '\n');
477 if (p)
478 *p = '\0';
479
480 for (i = 0; i < tp->core_sib; i++) {
481 if (!strcmp(buf, tp->core_siblings[i]))
482 break;
483 }
484 if (i == tp->core_sib) {
485 tp->core_siblings[i] = buf;
486 tp->core_sib++;
487 buf = NULL;
488 len = 0;
489 }
Stephane Eranianc5885742013-08-14 12:04:26 +0200490 ret = 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200491
Stephane Eranianc5885742013-08-14 12:04:26 +0200492try_threads:
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200493 sprintf(filename, THRD_SIB_FMT, cpu);
494 fp = fopen(filename, "r");
495 if (!fp)
496 goto done;
497
498 if (getline(&buf, &len, fp) <= 0)
499 goto done;
500
501 p = strchr(buf, '\n');
502 if (p)
503 *p = '\0';
504
505 for (i = 0; i < tp->thread_sib; i++) {
506 if (!strcmp(buf, tp->thread_siblings[i]))
507 break;
508 }
509 if (i == tp->thread_sib) {
510 tp->thread_siblings[i] = buf;
511 tp->thread_sib++;
512 buf = NULL;
513 }
514 ret = 0;
515done:
Kan Liang2bb00d22015-09-01 09:58:12 -0400516 tp->core_id[cpu] = cpu_map__get_core_id(cpu);
517 tp->phy_pkg_id[cpu] = cpu_map__get_socket_id(cpu);
518
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200519 if(fp)
520 fclose(fp);
521 free(buf);
522 return ret;
523}
524
525static void free_cpu_topo(struct cpu_topo *tp)
526{
527 u32 i;
528
529 if (!tp)
530 return;
531
532 for (i = 0 ; i < tp->core_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300533 zfree(&tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200534
535 for (i = 0 ; i < tp->thread_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300536 zfree(&tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200537
538 free(tp);
539}
540
541static struct cpu_topo *build_cpu_topology(void)
542{
543 struct cpu_topo *tp;
544 void *addr;
545 u32 nr, i;
Kan Liang2bb00d22015-09-01 09:58:12 -0400546 size_t sz, sz_id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200547 long ncpus;
548 int ret = -1;
549
550 ncpus = sysconf(_SC_NPROCESSORS_CONF);
551 if (ncpus < 0)
552 return NULL;
553
554 nr = (u32)(ncpus & UINT_MAX);
555
556 sz = nr * sizeof(char *);
Kan Liang2bb00d22015-09-01 09:58:12 -0400557 sz_id = nr * sizeof(int);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200558
Kan Liang2bb00d22015-09-01 09:58:12 -0400559 addr = calloc(1, sizeof(*tp) + 2 * sz + 2 * sz_id);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200560 if (!addr)
561 return NULL;
562
563 tp = addr;
Kan Liang2bb00d22015-09-01 09:58:12 -0400564 tp->cpu_nr = nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200565 addr += sizeof(*tp);
566 tp->core_siblings = addr;
567 addr += sz;
568 tp->thread_siblings = addr;
Kan Liang2bb00d22015-09-01 09:58:12 -0400569 addr += sz;
570 tp->core_id = addr;
571 addr += sz_id;
572 tp->phy_pkg_id = addr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200573
574 for (i = 0; i < nr; i++) {
575 ret = build_cpu_topo(tp, i);
576 if (ret < 0)
577 break;
578 }
579 if (ret) {
580 free_cpu_topo(tp);
581 tp = NULL;
582 }
583 return tp;
584}
585
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300586static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
587 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200588{
589 struct cpu_topo *tp;
590 u32 i;
591 int ret;
592
593 tp = build_cpu_topology();
594 if (!tp)
595 return -1;
596
597 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
598 if (ret < 0)
599 goto done;
600
601 for (i = 0; i < tp->core_sib; i++) {
602 ret = do_write_string(fd, tp->core_siblings[i]);
603 if (ret < 0)
604 goto done;
605 }
606 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
607 if (ret < 0)
608 goto done;
609
610 for (i = 0; i < tp->thread_sib; i++) {
611 ret = do_write_string(fd, tp->thread_siblings[i]);
612 if (ret < 0)
613 break;
614 }
Kan Liang2bb00d22015-09-01 09:58:12 -0400615
616 for (i = 0; i < tp->cpu_nr; i++) {
617 ret = do_write(fd, &tp->core_id[i], sizeof(int));
618 if (ret < 0)
619 return ret;
620 ret = do_write(fd, &tp->phy_pkg_id[i], sizeof(int));
621 if (ret < 0)
622 return ret;
623 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200624done:
625 free_cpu_topo(tp);
626 return ret;
627}
628
629
630
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300631static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
632 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200633{
634 char *buf = NULL;
635 FILE *fp;
636 size_t len = 0;
637 int ret = -1, n;
638 uint64_t mem;
639
640 fp = fopen("/proc/meminfo", "r");
641 if (!fp)
642 return -1;
643
644 while (getline(&buf, &len, fp) > 0) {
645 ret = strncmp(buf, "MemTotal:", 9);
646 if (!ret)
647 break;
648 }
649 if (!ret) {
650 n = sscanf(buf, "%*s %"PRIu64, &mem);
651 if (n == 1)
652 ret = do_write(fd, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800653 } else
654 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200655 free(buf);
656 fclose(fp);
657 return ret;
658}
659
660static int write_topo_node(int fd, int node)
661{
662 char str[MAXPATHLEN];
663 char field[32];
664 char *buf = NULL, *p;
665 size_t len = 0;
666 FILE *fp;
667 u64 mem_total, mem_free, mem;
668 int ret = -1;
669
670 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
671 fp = fopen(str, "r");
672 if (!fp)
673 return -1;
674
675 while (getline(&buf, &len, fp) > 0) {
676 /* skip over invalid lines */
677 if (!strchr(buf, ':'))
678 continue;
Alan Coxa761a2d2014-01-20 19:10:11 +0100679 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200680 goto done;
681 if (!strcmp(field, "MemTotal:"))
682 mem_total = mem;
683 if (!strcmp(field, "MemFree:"))
684 mem_free = mem;
685 }
686
687 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100688 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200689
690 ret = do_write(fd, &mem_total, sizeof(u64));
691 if (ret)
692 goto done;
693
694 ret = do_write(fd, &mem_free, sizeof(u64));
695 if (ret)
696 goto done;
697
698 ret = -1;
699 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
700
701 fp = fopen(str, "r");
702 if (!fp)
703 goto done;
704
705 if (getline(&buf, &len, fp) <= 0)
706 goto done;
707
708 p = strchr(buf, '\n');
709 if (p)
710 *p = '\0';
711
712 ret = do_write_string(fd, buf);
713done:
714 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100715 if (fp)
716 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200717 return ret;
718}
719
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300720static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
721 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200722{
723 char *buf = NULL;
724 size_t len = 0;
725 FILE *fp;
726 struct cpu_map *node_map = NULL;
727 char *c;
728 u32 nr, i, j;
729 int ret = -1;
730
731 fp = fopen("/sys/devices/system/node/online", "r");
732 if (!fp)
733 return -1;
734
735 if (getline(&buf, &len, fp) <= 0)
736 goto done;
737
738 c = strchr(buf, '\n');
739 if (c)
740 *c = '\0';
741
742 node_map = cpu_map__new(buf);
743 if (!node_map)
744 goto done;
745
746 nr = (u32)node_map->nr;
747
748 ret = do_write(fd, &nr, sizeof(nr));
749 if (ret < 0)
750 goto done;
751
752 for (i = 0; i < nr; i++) {
753 j = (u32)node_map->map[i];
754 ret = do_write(fd, &j, sizeof(j));
755 if (ret < 0)
756 break;
757
758 ret = write_topo_node(fd, i);
759 if (ret < 0)
760 break;
761 }
762done:
763 free(buf);
764 fclose(fp);
765 free(node_map);
766 return ret;
767}
768
769/*
Robert Richter50a96672012-08-16 21:10:24 +0200770 * File format:
771 *
772 * struct pmu_mappings {
773 * u32 pmu_num;
774 * struct pmu_map {
775 * u32 type;
776 * char name[];
777 * }[pmu_num];
778 * };
779 */
780
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300781static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
782 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +0200783{
784 struct perf_pmu *pmu = NULL;
785 off_t offset = lseek(fd, 0, SEEK_CUR);
786 __u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900787 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200788
789 /* write real pmu_num later */
Namhyung Kim5323f602012-12-17 15:38:54 +0900790 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
791 if (ret < 0)
792 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200793
794 while ((pmu = perf_pmu__scan(pmu))) {
795 if (!pmu->name)
796 continue;
797 pmu_num++;
Namhyung Kim5323f602012-12-17 15:38:54 +0900798
799 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
800 if (ret < 0)
801 return ret;
802
803 ret = do_write_string(fd, pmu->name);
804 if (ret < 0)
805 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200806 }
807
808 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
809 /* discard all */
810 lseek(fd, offset, SEEK_SET);
811 return -1;
812 }
813
814 return 0;
815}
816
817/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900818 * File format:
819 *
820 * struct group_descs {
821 * u32 nr_groups;
822 * struct group_desc {
823 * char name[];
824 * u32 leader_idx;
825 * u32 nr_members;
826 * }[nr_groups];
827 * };
828 */
829static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
830 struct perf_evlist *evlist)
831{
832 u32 nr_groups = evlist->nr_groups;
833 struct perf_evsel *evsel;
834 int ret;
835
836 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
837 if (ret < 0)
838 return ret;
839
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300840 evlist__for_each(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900841 if (perf_evsel__is_group_leader(evsel) &&
842 evsel->nr_members > 1) {
843 const char *name = evsel->group_name ?: "{anon_group}";
844 u32 leader_idx = evsel->idx;
845 u32 nr_members = evsel->nr_members;
846
847 ret = do_write_string(fd, name);
848 if (ret < 0)
849 return ret;
850
851 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
852 if (ret < 0)
853 return ret;
854
855 ret = do_write(fd, &nr_members, sizeof(nr_members));
856 if (ret < 0)
857 return ret;
858 }
859 }
860 return 0;
861}
862
863/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200864 * default get_cpuid(): nothing gets recorded
865 * actual implementation must be in arch/$(ARCH)/util/header.c
866 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300867int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
868 size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200869{
870 return -1;
871}
872
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300873static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
874 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200875{
876 char buffer[64];
877 int ret;
878
879 ret = get_cpuid(buffer, sizeof(buffer));
880 if (!ret)
881 goto write_it;
882
883 return -1;
884write_it:
885 return do_write_string(fd, buffer);
886}
887
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300888static int write_branch_stack(int fd __maybe_unused,
889 struct perf_header *h __maybe_unused,
890 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +0100891{
892 return 0;
893}
894
Adrian Hunter99fa2982015-04-30 17:37:25 +0300895static int write_auxtrace(int fd, struct perf_header *h,
Adrian Hunter4025ea42015-04-09 18:53:41 +0300896 struct perf_evlist *evlist __maybe_unused)
897{
Adrian Hunter99fa2982015-04-30 17:37:25 +0300898 struct perf_session *session;
899 int err;
900
901 session = container_of(h, struct perf_session, header);
902
903 err = auxtrace_index__write(fd, &session->auxtrace_index);
904 if (err < 0)
905 pr_err("Failed to write auxtrace index\n");
906 return err;
Adrian Hunter4025ea42015-04-09 18:53:41 +0300907}
908
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900909static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
910 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200911{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900912 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200913}
914
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900915static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
916 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200917{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900918 fprintf(fp, "# os release : %s\n", ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200919}
920
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900921static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200922{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900923 fprintf(fp, "# arch : %s\n", ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200924}
925
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900926static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
927 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200928{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900929 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200930}
931
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900932static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
933 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200934{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900935 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
936 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200937}
938
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900939static void print_version(struct perf_header *ph, int fd __maybe_unused,
940 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200941{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900942 fprintf(fp, "# perf version : %s\n", ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200943}
944
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900945static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
946 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200947{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900948 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200949
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900950 nr = ph->env.nr_cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200951
952 fprintf(fp, "# cmdline : ");
953
Jiri Olsa768dd3f2015-07-21 14:31:31 +0200954 for (i = 0; i < nr; i++)
955 fprintf(fp, "%s ", ph->env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200956 fputc('\n', fp);
957}
958
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900959static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
960 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200961{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900962 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200963 char *str;
Kan Liang2bb00d22015-09-01 09:58:12 -0400964 int cpu_nr = ph->env.nr_cpus_online;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200965
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900966 nr = ph->env.nr_sibling_cores;
967 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200968
969 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200970 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900971 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200972 }
973
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900974 nr = ph->env.nr_sibling_threads;
975 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200976
977 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200978 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900979 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200980 }
Kan Liang2bb00d22015-09-01 09:58:12 -0400981
982 if (ph->env.cpu != NULL) {
983 for (i = 0; i < cpu_nr; i++)
984 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
985 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
986 } else
987 fprintf(fp, "# Core ID and Socket ID information is not available\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200988}
989
Robert Richter4e1b9c62012-08-16 21:10:22 +0200990static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200991{
Robert Richter4e1b9c62012-08-16 21:10:22 +0200992 struct perf_evsel *evsel;
993
994 if (!events)
995 return;
996
997 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300998 zfree(&evsel->name);
999 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001000 }
1001
1002 free(events);
1003}
1004
1005static struct perf_evsel *
1006read_event_desc(struct perf_header *ph, int fd)
1007{
1008 struct perf_evsel *evsel, *events = NULL;
1009 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001010 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001011 u32 nre, sz, nr, i, j;
1012 ssize_t ret;
1013 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001014
1015 /* number of events */
Namhyung Kim5323f602012-12-17 15:38:54 +09001016 ret = readn(fd, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001017 if (ret != (ssize_t)sizeof(nre))
1018 goto error;
1019
1020 if (ph->needs_swap)
1021 nre = bswap_32(nre);
1022
Namhyung Kim5323f602012-12-17 15:38:54 +09001023 ret = readn(fd, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001024 if (ret != (ssize_t)sizeof(sz))
1025 goto error;
1026
1027 if (ph->needs_swap)
1028 sz = bswap_32(sz);
1029
Stephane Eranian62db9062012-02-09 23:21:07 +01001030 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001031 buf = malloc(sz);
1032 if (!buf)
1033 goto error;
1034
Robert Richter4e1b9c62012-08-16 21:10:22 +02001035 /* the last event terminates with evsel->attr.size == 0: */
1036 events = calloc(nre + 1, sizeof(*events));
1037 if (!events)
1038 goto error;
1039
1040 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001041 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001042 msz = sz;
1043
Robert Richter4e1b9c62012-08-16 21:10:22 +02001044 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1045 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001046
Stephane Eranian62db9062012-02-09 23:21:07 +01001047 /*
1048 * must read entire on-file attr struct to
1049 * sync up with layout.
1050 */
Namhyung Kim5323f602012-12-17 15:38:54 +09001051 ret = readn(fd, buf, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001052 if (ret != (ssize_t)sz)
1053 goto error;
1054
1055 if (ph->needs_swap)
1056 perf_event__attr_swap(buf);
1057
Robert Richter4e1b9c62012-08-16 21:10:22 +02001058 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001059
Namhyung Kim5323f602012-12-17 15:38:54 +09001060 ret = readn(fd, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001061 if (ret != (ssize_t)sizeof(nr))
1062 goto error;
1063
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001064 if (ph->needs_swap) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001065 nr = bswap_32(nr);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001066 evsel->needs_swap = true;
1067 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001068
Robert Richter4e1b9c62012-08-16 21:10:22 +02001069 evsel->name = do_read_string(fd, ph);
1070
1071 if (!nr)
1072 continue;
1073
1074 id = calloc(nr, sizeof(*id));
1075 if (!id)
1076 goto error;
1077 evsel->ids = nr;
1078 evsel->id = id;
1079
1080 for (j = 0 ; j < nr; j++) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001081 ret = readn(fd, id, sizeof(*id));
Robert Richter4e1b9c62012-08-16 21:10:22 +02001082 if (ret != (ssize_t)sizeof(*id))
1083 goto error;
1084 if (ph->needs_swap)
1085 *id = bswap_64(*id);
1086 id++;
1087 }
1088 }
1089out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001090 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001091 return events;
1092error:
Markus Elfring4cc97612015-06-25 17:12:32 +02001093 free_event_desc(events);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001094 events = NULL;
1095 goto out;
1096}
1097
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001098static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1099 void *priv __attribute__((unused)))
1100{
1101 return fprintf(fp, ", %s = %s", name, val);
1102}
1103
Robert Richter4e1b9c62012-08-16 21:10:22 +02001104static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1105{
1106 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1107 u32 j;
1108 u64 *id;
1109
1110 if (!events) {
1111 fprintf(fp, "# event desc: not available or unable to read\n");
1112 return;
1113 }
1114
1115 for (evsel = events; evsel->attr.size; evsel++) {
1116 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001117
Robert Richter4e1b9c62012-08-16 21:10:22 +02001118 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001119 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001120 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1121 if (j)
1122 fputc(',', fp);
1123 fprintf(fp, " %"PRIu64, *id);
1124 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001125 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001126 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001127
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001128 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001129
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001130 fputc('\n', fp);
1131 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001132
1133 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001134}
1135
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001136static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001137 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001138{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001139 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001140}
1141
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001142static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001143 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001144{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001145 u32 nr, c, i;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001146 char *str, *tmp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001147 uint64_t mem_total, mem_free;
1148
1149 /* nr nodes */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001150 nr = ph->env.nr_numa_nodes;
1151 str = ph->env.numa_nodes;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001152
1153 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001154 /* node number */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001155 c = strtoul(str, &tmp, 0);
1156 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001157 goto error;
1158
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001159 str = tmp + 1;
1160 mem_total = strtoull(str, &tmp, 0);
1161 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001162 goto error;
1163
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001164 str = tmp + 1;
1165 mem_free = strtoull(str, &tmp, 0);
1166 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001167 goto error;
1168
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001169 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1170 " free = %"PRIu64" kB\n",
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001171 c, mem_total, mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001172
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001173 str = tmp + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001174 fprintf(fp, "# node%u cpu list : %s\n", c, str);
Namhyung Kim12344712012-10-23 22:44:49 +09001175
1176 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001177 }
1178 return;
1179error:
1180 fprintf(fp, "# numa topology : not available\n");
1181}
1182
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001183static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001184{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001185 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001186}
1187
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001188static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001189 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001190{
1191 fprintf(fp, "# contains samples with branch stack\n");
1192}
1193
Adrian Hunter4025ea42015-04-09 18:53:41 +03001194static void print_auxtrace(struct perf_header *ph __maybe_unused,
1195 int fd __maybe_unused, FILE *fp)
1196{
1197 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1198}
1199
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001200static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1201 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001202{
1203 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001204 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001205 u32 pmu_num;
1206 u32 type;
1207
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001208 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001209 if (!pmu_num) {
1210 fprintf(fp, "# pmu mappings: not available\n");
1211 return;
1212 }
1213
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001214 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001215
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001216 while (pmu_num) {
1217 type = strtoul(str, &tmp, 0);
1218 if (*tmp != ':')
1219 goto error;
1220
1221 str = tmp + 1;
1222 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1223
Robert Richter50a96672012-08-16 21:10:24 +02001224 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001225 str += strlen(str) + 1;
1226 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001227 }
1228
1229 fprintf(fp, "\n");
1230
1231 if (!pmu_num)
1232 return;
1233error:
1234 fprintf(fp, "# pmu mappings: unable to read\n");
1235}
1236
Namhyung Kima8bb5592013-01-22 18:09:31 +09001237static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1238 FILE *fp)
1239{
1240 struct perf_session *session;
1241 struct perf_evsel *evsel;
1242 u32 nr = 0;
1243
1244 session = container_of(ph, struct perf_session, header);
1245
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001246 evlist__for_each(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001247 if (perf_evsel__is_group_leader(evsel) &&
1248 evsel->nr_members > 1) {
1249 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1250 perf_evsel__name(evsel));
1251
1252 nr = evsel->nr_members - 1;
1253 } else if (nr) {
1254 fprintf(fp, ",%s", perf_evsel__name(evsel));
1255
1256 if (--nr == 0)
1257 fprintf(fp, "}\n");
1258 }
1259 }
1260}
1261
Robert Richter08d95bd2012-02-10 15:41:55 +01001262static int __event_process_build_id(struct build_id_event *bev,
1263 char *filename,
1264 struct perf_session *session)
1265{
1266 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01001267 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00001268 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01001269 struct dso *dso;
1270 enum dso_kernel_type dso_type;
1271
1272 machine = perf_session__findnew_machine(session, bev->pid);
1273 if (!machine)
1274 goto out;
1275
Wang Nan1f121b02015-06-03 08:52:21 +00001276 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01001277
Wang Nan1f121b02015-06-03 08:52:21 +00001278 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01001279 case PERF_RECORD_MISC_KERNEL:
1280 dso_type = DSO_TYPE_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001281 break;
1282 case PERF_RECORD_MISC_GUEST_KERNEL:
1283 dso_type = DSO_TYPE_GUEST_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001284 break;
1285 case PERF_RECORD_MISC_USER:
1286 case PERF_RECORD_MISC_GUEST_USER:
1287 dso_type = DSO_TYPE_USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01001288 break;
1289 default:
1290 goto out;
1291 }
1292
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001293 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001294 if (dso != NULL) {
1295 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1296
1297 dso__set_build_id(dso, &bev->build_id);
1298
Wang Nan1f121b02015-06-03 08:52:21 +00001299 if (!is_kernel_module(filename, cpumode))
Robert Richter08d95bd2012-02-10 15:41:55 +01001300 dso->kernel = dso_type;
1301
1302 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1303 sbuild_id);
1304 pr_debug("build id event received for %s: %s\n",
1305 dso->long_name, sbuild_id);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001306 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01001307 }
1308
1309 err = 0;
1310out:
1311 return err;
1312}
1313
1314static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1315 int input, u64 offset, u64 size)
1316{
1317 struct perf_session *session = container_of(header, struct perf_session, header);
1318 struct {
1319 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001320 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001321 char filename[0];
1322 } old_bev;
1323 struct build_id_event bev;
1324 char filename[PATH_MAX];
1325 u64 limit = offset + size;
1326
1327 while (offset < limit) {
1328 ssize_t len;
1329
Namhyung Kim5323f602012-12-17 15:38:54 +09001330 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001331 return -1;
1332
1333 if (header->needs_swap)
1334 perf_event_header__bswap(&old_bev.header);
1335
1336 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001337 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001338 return -1;
1339
1340 bev.header = old_bev.header;
1341
1342 /*
1343 * As the pid is the missing value, we need to fill
1344 * it properly. The header.misc value give us nice hint.
1345 */
1346 bev.pid = HOST_KERNEL_ID;
1347 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1348 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1349 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1350
1351 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1352 __event_process_build_id(&bev, filename, session);
1353
1354 offset += bev.header.size;
1355 }
1356
1357 return 0;
1358}
1359
1360static int perf_header__read_build_ids(struct perf_header *header,
1361 int input, u64 offset, u64 size)
1362{
1363 struct perf_session *session = container_of(header, struct perf_session, header);
1364 struct build_id_event bev;
1365 char filename[PATH_MAX];
1366 u64 limit = offset + size, orig_offset = offset;
1367 int err = -1;
1368
1369 while (offset < limit) {
1370 ssize_t len;
1371
Namhyung Kim5323f602012-12-17 15:38:54 +09001372 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001373 goto out;
1374
1375 if (header->needs_swap)
1376 perf_event_header__bswap(&bev.header);
1377
1378 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001379 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001380 goto out;
1381 /*
1382 * The a1645ce1 changeset:
1383 *
1384 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1385 *
1386 * Added a field to struct build_id_event that broke the file
1387 * format.
1388 *
1389 * Since the kernel build-id is the first entry, process the
1390 * table using the old format if the well known
1391 * '[kernel.kallsyms]' string for the kernel build-id has the
1392 * first 4 characters chopped off (where the pid_t sits).
1393 */
1394 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1395 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1396 return -1;
1397 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1398 }
1399
1400 __event_process_build_id(&bev, filename, session);
1401
1402 offset += bev.header.size;
1403 }
1404 err = 0;
1405out:
1406 return err;
1407}
1408
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001409static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1410 struct perf_header *ph __maybe_unused,
1411 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001412{
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001413 ssize_t ret = trace_report(fd, data, false);
1414 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001415}
1416
1417static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001418 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001419 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001420{
1421 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1422 pr_debug("Failed to read buildids, continuing...\n");
1423 return 0;
1424}
1425
Namhyung Kima1ae5652012-09-24 17:14:59 +09001426static int process_hostname(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001427 struct perf_header *ph, int fd,
1428 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001429{
1430 ph->env.hostname = do_read_string(fd, ph);
1431 return ph->env.hostname ? 0 : -ENOMEM;
1432}
1433
1434static int process_osrelease(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001435 struct perf_header *ph, int fd,
1436 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001437{
1438 ph->env.os_release = do_read_string(fd, ph);
1439 return ph->env.os_release ? 0 : -ENOMEM;
1440}
1441
1442static int process_version(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001443 struct perf_header *ph, int fd,
1444 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001445{
1446 ph->env.version = do_read_string(fd, ph);
1447 return ph->env.version ? 0 : -ENOMEM;
1448}
1449
1450static int process_arch(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001451 struct perf_header *ph, int fd,
1452 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001453{
1454 ph->env.arch = do_read_string(fd, ph);
1455 return ph->env.arch ? 0 : -ENOMEM;
1456}
1457
1458static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001459 struct perf_header *ph, int fd,
1460 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001461{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001462 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001463 u32 nr;
1464
Namhyung Kim5323f602012-12-17 15:38:54 +09001465 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001466 if (ret != sizeof(nr))
1467 return -1;
1468
1469 if (ph->needs_swap)
1470 nr = bswap_32(nr);
1471
1472 ph->env.nr_cpus_online = nr;
1473
Namhyung Kim5323f602012-12-17 15:38:54 +09001474 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001475 if (ret != sizeof(nr))
1476 return -1;
1477
1478 if (ph->needs_swap)
1479 nr = bswap_32(nr);
1480
1481 ph->env.nr_cpus_avail = nr;
1482 return 0;
1483}
1484
1485static int process_cpudesc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001486 struct perf_header *ph, int fd,
1487 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001488{
1489 ph->env.cpu_desc = do_read_string(fd, ph);
1490 return ph->env.cpu_desc ? 0 : -ENOMEM;
1491}
1492
1493static int process_cpuid(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001494 struct perf_header *ph, int fd,
1495 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001496{
1497 ph->env.cpuid = do_read_string(fd, ph);
1498 return ph->env.cpuid ? 0 : -ENOMEM;
1499}
1500
1501static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001502 struct perf_header *ph, int fd,
1503 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001504{
1505 uint64_t mem;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001506 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001507
Namhyung Kim5323f602012-12-17 15:38:54 +09001508 ret = readn(fd, &mem, sizeof(mem));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001509 if (ret != sizeof(mem))
1510 return -1;
1511
1512 if (ph->needs_swap)
1513 mem = bswap_64(mem);
1514
1515 ph->env.total_mem = mem;
1516 return 0;
1517}
1518
Robert Richter7c2f7af2012-08-16 21:10:23 +02001519static struct perf_evsel *
1520perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1521{
1522 struct perf_evsel *evsel;
1523
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001524 evlist__for_each(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001525 if (evsel->idx == idx)
1526 return evsel;
1527 }
1528
1529 return NULL;
1530}
1531
1532static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001533perf_evlist__set_event_name(struct perf_evlist *evlist,
1534 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001535{
1536 struct perf_evsel *evsel;
1537
1538 if (!event->name)
1539 return;
1540
1541 evsel = perf_evlist__find_by_index(evlist, event->idx);
1542 if (!evsel)
1543 return;
1544
1545 if (evsel->name)
1546 return;
1547
1548 evsel->name = strdup(event->name);
1549}
1550
1551static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001552process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001553 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001554 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001555{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001556 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001557 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1558
1559 if (!events)
1560 return 0;
1561
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001562 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001563 for (evsel = events; evsel->attr.size; evsel++)
1564 perf_evlist__set_event_name(session->evlist, evsel);
1565
1566 free_event_desc(events);
1567
1568 return 0;
1569}
1570
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001571static int process_cmdline(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001572 struct perf_header *ph, int fd,
1573 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001574{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001575 ssize_t ret;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001576 char *str, *cmdline = NULL, **argv = NULL;
1577 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001578
Namhyung Kim5323f602012-12-17 15:38:54 +09001579 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001580 if (ret != sizeof(nr))
1581 return -1;
1582
1583 if (ph->needs_swap)
1584 nr = bswap_32(nr);
1585
1586 ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001587
1588 cmdline = zalloc(section->size + nr + 1);
1589 if (!cmdline)
1590 return -1;
1591
1592 argv = zalloc(sizeof(char *) * (nr + 1));
1593 if (!argv)
1594 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001595
1596 for (i = 0; i < nr; i++) {
1597 str = do_read_string(fd, ph);
1598 if (!str)
1599 goto error;
1600
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001601 argv[i] = cmdline + len;
1602 memcpy(argv[i], str, strlen(str) + 1);
1603 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001604 free(str);
1605 }
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001606 ph->env.cmdline = cmdline;
1607 ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001608 return 0;
1609
1610error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001611 free(argv);
1612 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001613 return -1;
1614}
1615
Kan Liang2bb00d22015-09-01 09:58:12 -04001616static int process_cpu_topology(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001617 struct perf_header *ph, int fd,
1618 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001619{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001620 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001621 u32 nr, i;
1622 char *str;
1623 struct strbuf sb;
Kan Liang2bb00d22015-09-01 09:58:12 -04001624 int cpu_nr = ph->env.nr_cpus_online;
1625 u64 size = 0;
1626
1627 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1628 if (!ph->env.cpu)
1629 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001630
Namhyung Kim5323f602012-12-17 15:38:54 +09001631 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001632 if (ret != sizeof(nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001633 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001634
1635 if (ph->needs_swap)
1636 nr = bswap_32(nr);
1637
1638 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001639 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001640 strbuf_init(&sb, 128);
1641
1642 for (i = 0; i < nr; i++) {
1643 str = do_read_string(fd, ph);
1644 if (!str)
1645 goto error;
1646
1647 /* include a NULL character at the end */
1648 strbuf_add(&sb, str, strlen(str) + 1);
Kan Liang2bb00d22015-09-01 09:58:12 -04001649 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001650 free(str);
1651 }
1652 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1653
Namhyung Kim5323f602012-12-17 15:38:54 +09001654 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001655 if (ret != sizeof(nr))
1656 return -1;
1657
1658 if (ph->needs_swap)
1659 nr = bswap_32(nr);
1660
1661 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001662 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001663
1664 for (i = 0; i < nr; i++) {
1665 str = do_read_string(fd, ph);
1666 if (!str)
1667 goto error;
1668
1669 /* include a NULL character at the end */
1670 strbuf_add(&sb, str, strlen(str) + 1);
Kan Liang2bb00d22015-09-01 09:58:12 -04001671 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001672 free(str);
1673 }
1674 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04001675
1676 /*
1677 * The header may be from old perf,
1678 * which doesn't include core id and socket id information.
1679 */
1680 if (section->size <= size) {
1681 zfree(&ph->env.cpu);
1682 return 0;
1683 }
1684
1685 for (i = 0; i < (u32)cpu_nr; i++) {
1686 ret = readn(fd, &nr, sizeof(nr));
1687 if (ret != sizeof(nr))
1688 goto free_cpu;
1689
1690 if (ph->needs_swap)
1691 nr = bswap_32(nr);
1692
1693 if (nr > (u32)cpu_nr) {
1694 pr_debug("core_id number is too big."
1695 "You may need to upgrade the perf tool.\n");
1696 goto free_cpu;
1697 }
1698 ph->env.cpu[i].core_id = nr;
1699
1700 ret = readn(fd, &nr, sizeof(nr));
1701 if (ret != sizeof(nr))
1702 goto free_cpu;
1703
1704 if (ph->needs_swap)
1705 nr = bswap_32(nr);
1706
1707 if (nr > (u32)cpu_nr) {
1708 pr_debug("socket_id number is too big."
1709 "You may need to upgrade the perf tool.\n");
1710 goto free_cpu;
1711 }
1712
1713 ph->env.cpu[i].socket_id = nr;
1714 }
1715
Namhyung Kima1ae5652012-09-24 17:14:59 +09001716 return 0;
1717
1718error:
1719 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04001720free_cpu:
1721 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001722 return -1;
1723}
1724
1725static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001726 struct perf_header *ph, int fd,
1727 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001728{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001729 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001730 u32 nr, node, i;
1731 char *str;
1732 uint64_t mem_total, mem_free;
1733 struct strbuf sb;
1734
1735 /* nr nodes */
Namhyung Kim5323f602012-12-17 15:38:54 +09001736 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001737 if (ret != sizeof(nr))
1738 goto error;
1739
1740 if (ph->needs_swap)
1741 nr = bswap_32(nr);
1742
1743 ph->env.nr_numa_nodes = nr;
1744 strbuf_init(&sb, 256);
1745
1746 for (i = 0; i < nr; i++) {
1747 /* node number */
Namhyung Kim5323f602012-12-17 15:38:54 +09001748 ret = readn(fd, &node, sizeof(node));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001749 if (ret != sizeof(node))
1750 goto error;
1751
Namhyung Kim5323f602012-12-17 15:38:54 +09001752 ret = readn(fd, &mem_total, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001753 if (ret != sizeof(u64))
1754 goto error;
1755
Namhyung Kim5323f602012-12-17 15:38:54 +09001756 ret = readn(fd, &mem_free, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001757 if (ret != sizeof(u64))
1758 goto error;
1759
1760 if (ph->needs_swap) {
1761 node = bswap_32(node);
1762 mem_total = bswap_64(mem_total);
1763 mem_free = bswap_64(mem_free);
1764 }
1765
1766 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1767 node, mem_total, mem_free);
1768
1769 str = do_read_string(fd, ph);
1770 if (!str)
1771 goto error;
1772
1773 /* include a NULL character at the end */
1774 strbuf_add(&sb, str, strlen(str) + 1);
1775 free(str);
1776 }
1777 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1778 return 0;
1779
1780error:
1781 strbuf_release(&sb);
1782 return -1;
1783}
1784
1785static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001786 struct perf_header *ph, int fd,
1787 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001788{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001789 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001790 char *name;
1791 u32 pmu_num;
1792 u32 type;
1793 struct strbuf sb;
1794
Namhyung Kim5323f602012-12-17 15:38:54 +09001795 ret = readn(fd, &pmu_num, sizeof(pmu_num));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001796 if (ret != sizeof(pmu_num))
1797 return -1;
1798
1799 if (ph->needs_swap)
1800 pmu_num = bswap_32(pmu_num);
1801
1802 if (!pmu_num) {
1803 pr_debug("pmu mappings not available\n");
1804 return 0;
1805 }
1806
1807 ph->env.nr_pmu_mappings = pmu_num;
1808 strbuf_init(&sb, 128);
1809
1810 while (pmu_num) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001811 if (readn(fd, &type, sizeof(type)) != sizeof(type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001812 goto error;
1813 if (ph->needs_swap)
1814 type = bswap_32(type);
1815
1816 name = do_read_string(fd, ph);
1817 if (!name)
1818 goto error;
1819
1820 strbuf_addf(&sb, "%u:%s", type, name);
1821 /* include a NULL character at the end */
1822 strbuf_add(&sb, "", 1);
1823
1824 free(name);
1825 pmu_num--;
1826 }
1827 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1828 return 0;
1829
1830error:
1831 strbuf_release(&sb);
1832 return -1;
1833}
1834
Namhyung Kima8bb5592013-01-22 18:09:31 +09001835static int process_group_desc(struct perf_file_section *section __maybe_unused,
1836 struct perf_header *ph, int fd,
1837 void *data __maybe_unused)
1838{
1839 size_t ret = -1;
1840 u32 i, nr, nr_groups;
1841 struct perf_session *session;
1842 struct perf_evsel *evsel, *leader = NULL;
1843 struct group_desc {
1844 char *name;
1845 u32 leader_idx;
1846 u32 nr_members;
1847 } *desc;
1848
1849 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
1850 return -1;
1851
1852 if (ph->needs_swap)
1853 nr_groups = bswap_32(nr_groups);
1854
1855 ph->env.nr_groups = nr_groups;
1856 if (!nr_groups) {
1857 pr_debug("group desc not available\n");
1858 return 0;
1859 }
1860
1861 desc = calloc(nr_groups, sizeof(*desc));
1862 if (!desc)
1863 return -1;
1864
1865 for (i = 0; i < nr_groups; i++) {
1866 desc[i].name = do_read_string(fd, ph);
1867 if (!desc[i].name)
1868 goto out_free;
1869
1870 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
1871 goto out_free;
1872
1873 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
1874 goto out_free;
1875
1876 if (ph->needs_swap) {
1877 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
1878 desc[i].nr_members = bswap_32(desc[i].nr_members);
1879 }
1880 }
1881
1882 /*
1883 * Rebuild group relationship based on the group_desc
1884 */
1885 session = container_of(ph, struct perf_session, header);
1886 session->evlist->nr_groups = nr_groups;
1887
1888 i = nr = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001889 evlist__for_each(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001890 if (evsel->idx == (int) desc[i].leader_idx) {
1891 evsel->leader = evsel;
1892 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09001893 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001894 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09001895 desc[i].name = NULL;
1896 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09001897 evsel->nr_members = desc[i].nr_members;
1898
1899 if (i >= nr_groups || nr > 0) {
1900 pr_debug("invalid group desc\n");
1901 goto out_free;
1902 }
1903
1904 leader = evsel;
1905 nr = evsel->nr_members - 1;
1906 i++;
1907 } else if (nr) {
1908 /* This is a group member */
1909 evsel->leader = leader;
1910
1911 nr--;
1912 }
1913 }
1914
1915 if (i != nr_groups || nr != 0) {
1916 pr_debug("invalid group desc\n");
1917 goto out_free;
1918 }
1919
1920 ret = 0;
1921out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09001922 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001923 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001924 free(desc);
1925
1926 return ret;
1927}
1928
Adrian Hunter99fa2982015-04-30 17:37:25 +03001929static int process_auxtrace(struct perf_file_section *section,
1930 struct perf_header *ph, int fd,
1931 void *data __maybe_unused)
1932{
1933 struct perf_session *session;
1934 int err;
1935
1936 session = container_of(ph, struct perf_session, header);
1937
1938 err = auxtrace_index__process(fd, section->size, session,
1939 ph->needs_swap);
1940 if (err < 0)
1941 pr_err("Failed to process auxtrace index\n");
1942 return err;
1943}
1944
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001945struct feature_ops {
1946 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1947 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01001948 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001949 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001950 const char *name;
1951 bool full_only;
1952};
1953
Robert Richter8cdfa782011-12-07 10:02:56 +01001954#define FEAT_OPA(n, func) \
1955 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01001956#define FEAT_OPP(n, func) \
1957 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1958 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01001959#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01001960 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09001961 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01001962
1963/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001964#define print_tracing_data NULL
1965#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001966
1967static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001968 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01001969 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001970 FEAT_OPP(HEADER_HOSTNAME, hostname),
1971 FEAT_OPP(HEADER_OSRELEASE, osrelease),
1972 FEAT_OPP(HEADER_VERSION, version),
1973 FEAT_OPP(HEADER_ARCH, arch),
1974 FEAT_OPP(HEADER_NRCPUS, nrcpus),
1975 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09001976 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001977 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02001978 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001979 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01001980 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1981 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01001982 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001983 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09001984 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Adrian Hunter99fa2982015-04-30 17:37:25 +03001985 FEAT_OPP(HEADER_AUXTRACE, auxtrace),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001986};
1987
1988struct header_print_data {
1989 FILE *fp;
1990 bool full; /* extended list of headers */
1991};
1992
1993static int perf_file_section__fprintf_info(struct perf_file_section *section,
1994 struct perf_header *ph,
1995 int feat, int fd, void *data)
1996{
1997 struct header_print_data *hd = data;
1998
1999 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2000 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2001 "%d, continuing...\n", section->offset, feat);
2002 return 0;
2003 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002004 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002005 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002006 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002007 }
2008 if (!feat_ops[feat].print)
2009 return 0;
2010
2011 if (!feat_ops[feat].full_only || hd->full)
2012 feat_ops[feat].print(ph, fd, hd->fp);
2013 else
2014 fprintf(hd->fp, "# %s info available, use -I to display\n",
2015 feat_ops[feat].name);
2016
2017 return 0;
2018}
2019
2020int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2021{
2022 struct header_print_data hd;
2023 struct perf_header *header = &session->header;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002024 int fd = perf_data_file__fd(session->file);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002025 hd.fp = fp;
2026 hd.full = full;
2027
2028 perf_header__process_sections(header, fd, &hd,
2029 perf_file_section__fprintf_info);
2030 return 0;
2031}
2032
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002033static int do_write_feat(int fd, struct perf_header *h, int type,
2034 struct perf_file_section **p,
2035 struct perf_evlist *evlist)
2036{
2037 int err;
2038 int ret = 0;
2039
2040 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002041 if (!feat_ops[type].write)
2042 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002043
2044 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2045
2046 err = feat_ops[type].write(fd, h, evlist);
2047 if (err < 0) {
2048 pr_debug("failed to write feature %d\n", type);
2049
2050 /* undo anything written */
2051 lseek(fd, (*p)->offset, SEEK_SET);
2052
2053 return -1;
2054 }
2055 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2056 (*p)++;
2057 }
2058 return ret;
2059}
2060
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002061static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002062 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002063{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002064 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002065 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002066 int sec_size;
2067 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002068 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002069 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002070
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002071 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002072 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002073 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002074
Paul Gortmaker91b98802013-01-30 20:05:49 -05002075 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002076 if (feat_sec == NULL)
2077 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002078
2079 sec_size = sizeof(*feat_sec) * nr_sections;
2080
Jiri Olsa8d541e92013-07-17 19:49:44 +02002081 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002082 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002083
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002084 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2085 if (do_write_feat(fd, header, feat, &p, evlist))
2086 perf_header__clear_feat(header, feat);
2087 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002088
Xiao Guangrongf887f302010-02-04 16:46:42 +08002089 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002090 /*
2091 * may write more than needed due to dropped feature, but
2092 * this is okay, reader will skip the mising entries
2093 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002094 err = do_write(fd, feat_sec, sec_size);
2095 if (err < 0)
2096 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002097 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002098 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002099}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002100
Tom Zanussi8dc58102010-04-01 23:59:15 -05002101int perf_header__write_pipe(int fd)
2102{
2103 struct perf_pipe_file_header f_header;
2104 int err;
2105
2106 f_header = (struct perf_pipe_file_header){
2107 .magic = PERF_MAGIC,
2108 .size = sizeof(f_header),
2109 };
2110
2111 err = do_write(fd, &f_header, sizeof(f_header));
2112 if (err < 0) {
2113 pr_debug("failed to write perf pipe header\n");
2114 return err;
2115 }
2116
2117 return 0;
2118}
2119
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002120int perf_session__write_header(struct perf_session *session,
2121 struct perf_evlist *evlist,
2122 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002123{
2124 struct perf_file_header f_header;
2125 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002126 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002127 struct perf_evsel *evsel;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002128 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002129 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002130
2131 lseek(fd, sizeof(f_header), SEEK_SET);
2132
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002133 evlist__for_each(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002134 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2135 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002136 if (err < 0) {
2137 pr_debug("failed to write perf header\n");
2138 return err;
2139 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002140 }
2141
Jiri Olsa944d62b2013-07-17 19:49:43 +02002142 attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002143
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002144 evlist__for_each(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002145 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002146 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002147 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002148 .offset = evsel->id_offset,
2149 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002150 }
2151 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002152 err = do_write(fd, &f_attr, sizeof(f_attr));
2153 if (err < 0) {
2154 pr_debug("failed to write perf header attribute\n");
2155 return err;
2156 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002157 }
2158
Adrian Hunterd645c442013-12-11 14:36:28 +02002159 if (!header->data_offset)
2160 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002161 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002162
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002163 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002164 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002165 if (err < 0)
2166 return err;
2167 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002168
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002169 f_header = (struct perf_file_header){
2170 .magic = PERF_MAGIC,
2171 .size = sizeof(f_header),
2172 .attr_size = sizeof(f_attr),
2173 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002174 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002175 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002176 },
2177 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002178 .offset = header->data_offset,
2179 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002180 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002181 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002182 };
2183
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002184 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002185
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002186 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002187 err = do_write(fd, &f_header, sizeof(f_header));
2188 if (err < 0) {
2189 pr_debug("failed to write perf header\n");
2190 return err;
2191 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002192 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002193
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002194 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002195}
2196
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002197static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002198 int fd, void *buf, size_t size)
2199{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002200 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002201 return -1;
2202
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002203 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002204 mem_bswap_64(buf, size);
2205
2206 return 0;
2207}
2208
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002209int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002210 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002211 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002212 struct perf_header *ph,
2213 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002214{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002215 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002216 int nr_sections;
2217 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002218 int feat;
2219 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002220
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002221 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002222 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002223 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002224
Paul Gortmaker91b98802013-01-30 20:05:49 -05002225 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002226 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002227 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002228
2229 sec_size = sizeof(*feat_sec) * nr_sections;
2230
Jiri Olsa8d541e92013-07-17 19:49:44 +02002231 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002232
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002233 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2234 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002235 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002236
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002237 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2238 err = process(sec++, header, feat, fd, data);
2239 if (err < 0)
2240 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002241 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002242 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002243out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002244 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002245 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002246}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002247
Stephane Eranian114382a2012-02-09 23:21:08 +01002248static const int attr_file_abi_sizes[] = {
2249 [0] = PERF_ATTR_SIZE_VER0,
2250 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002251 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002252 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002253 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002254 0,
2255};
2256
2257/*
2258 * In the legacy file format, the magic number is not used to encode endianness.
2259 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2260 * on ABI revisions, we need to try all combinations for all endianness to
2261 * detect the endianness.
2262 */
2263static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2264{
2265 uint64_t ref_size, attr_size;
2266 int i;
2267
2268 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2269 ref_size = attr_file_abi_sizes[i]
2270 + sizeof(struct perf_file_section);
2271 if (hdr_sz != ref_size) {
2272 attr_size = bswap_64(hdr_sz);
2273 if (attr_size != ref_size)
2274 continue;
2275
2276 ph->needs_swap = true;
2277 }
2278 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2279 i,
2280 ph->needs_swap);
2281 return 0;
2282 }
2283 /* could not determine endianness */
2284 return -1;
2285}
2286
2287#define PERF_PIPE_HDR_VER0 16
2288
2289static const size_t attr_pipe_abi_sizes[] = {
2290 [0] = PERF_PIPE_HDR_VER0,
2291 0,
2292};
2293
2294/*
2295 * In the legacy pipe format, there is an implicit assumption that endiannesss
2296 * between host recording the samples, and host parsing the samples is the
2297 * same. This is not always the case given that the pipe output may always be
2298 * redirected into a file and analyzed on a different machine with possibly a
2299 * different endianness and perf_event ABI revsions in the perf tool itself.
2300 */
2301static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2302{
2303 u64 attr_size;
2304 int i;
2305
2306 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2307 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2308 attr_size = bswap_64(hdr_sz);
2309 if (attr_size != hdr_sz)
2310 continue;
2311
2312 ph->needs_swap = true;
2313 }
2314 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2315 return 0;
2316 }
2317 return -1;
2318}
2319
Feng Tange84ba4e2012-10-30 11:56:07 +08002320bool is_perf_magic(u64 magic)
2321{
2322 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2323 || magic == __perf_magic2
2324 || magic == __perf_magic2_sw)
2325 return true;
2326
2327 return false;
2328}
2329
Stephane Eranian114382a2012-02-09 23:21:08 +01002330static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2331 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002332{
2333 int ret;
2334
2335 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002336 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002337 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002338 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002339 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002340 if (is_pipe)
2341 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002342
Stephane Eranian114382a2012-02-09 23:21:08 +01002343 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002344 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002345 /*
2346 * the new magic number serves two purposes:
2347 * - unique number to identify actual perf.data files
2348 * - encode endianness of file
2349 */
Namhyung Kimf7913972015-01-29 17:06:45 +09002350 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002351
Stephane Eranian114382a2012-02-09 23:21:08 +01002352 /* check magic number with one endianness */
2353 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002354 return 0;
2355
Stephane Eranian114382a2012-02-09 23:21:08 +01002356 /* check magic number with opposite endianness */
2357 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002358 return -1;
2359
2360 ph->needs_swap = true;
2361
2362 return 0;
2363}
2364
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002365int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002366 struct perf_header *ph, int fd)
2367{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002368 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002369
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002370 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002371
Stephane Eranian73323f52012-02-02 13:54:44 +01002372 ret = readn(fd, header, sizeof(*header));
2373 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002374 return -1;
2375
Stephane Eranian114382a2012-02-09 23:21:08 +01002376 if (check_magic_endian(header->magic,
2377 header->attr_size, false, ph) < 0) {
2378 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002379 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002380 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002381
Stephane Eranian73323f52012-02-02 13:54:44 +01002382 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002383 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002384 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002385 }
2386
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002387 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002388 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002389 if (header->size == offsetof(typeof(*header), adds_features))
2390 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002391 else
2392 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002393 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002394 /*
2395 * feature bitmap is declared as an array of unsigned longs --
2396 * not good since its size can differ between the host that
2397 * generated the data file and the host analyzing the file.
2398 *
2399 * We need to handle endianness, but we don't know the size of
2400 * the unsigned long where the file was generated. Take a best
2401 * guess at determining it: try 64-bit swap first (ie., file
2402 * created on a 64-bit host), and check if the hostname feature
2403 * bit is set (this feature bit is forced on as of fbe96f2).
2404 * If the bit is not, undo the 64-bit swap and try a 32-bit
2405 * swap. If the hostname bit is still not set (e.g., older data
2406 * file), punt and fallback to the original behavior --
2407 * clearing all feature bits and setting buildid.
2408 */
David Ahern80c01202012-06-08 11:47:51 -03002409 mem_bswap_64(&header->adds_features,
2410 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002411
2412 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002413 /* unswap as u64 */
2414 mem_bswap_64(&header->adds_features,
2415 BITS_TO_U64(HEADER_FEAT_BITS));
2416
2417 /* unswap as u32 */
2418 mem_bswap_32(&header->adds_features,
2419 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002420 }
2421
2422 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2423 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2424 set_bit(HEADER_BUILD_ID, header->adds_features);
2425 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002426 }
2427
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002428 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002429 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002430
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002431 ph->data_offset = header->data.offset;
2432 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002433 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002434 return 0;
2435}
2436
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002437static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002438 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002439 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002440{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002441 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002442 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002443 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002444 return 0;
2445 }
2446
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002447 if (feat >= HEADER_LAST_FEATURE) {
2448 pr_debug("unknown feature %d, continuing...\n", feat);
2449 return 0;
2450 }
2451
Robert Richterf1c67db2012-02-10 15:41:56 +01002452 if (!feat_ops[feat].process)
2453 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002454
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002455 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002456}
2457
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002458static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002459 struct perf_header *ph, int fd,
2460 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002461{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002462 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002463
2464 ret = readn(fd, header, sizeof(*header));
2465 if (ret <= 0)
2466 return -1;
2467
Stephane Eranian114382a2012-02-09 23:21:08 +01002468 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2469 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002470 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002471 }
2472
2473 if (ph->needs_swap)
2474 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002475
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002476 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002477 return -1;
2478
Tom Zanussi8dc58102010-04-01 23:59:15 -05002479 return 0;
2480}
2481
Jiri Olsad4339562013-07-17 19:49:41 +02002482static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002483{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002484 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002485 struct perf_pipe_file_header f_header;
2486
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002487 if (perf_file_header__read_pipe(&f_header, header,
2488 perf_data_file__fd(session->file),
Tom Zanussi454c4072010-05-01 01:41:20 -05002489 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002490 pr_debug("incompatible file format\n");
2491 return -EINVAL;
2492 }
2493
Tom Zanussi8dc58102010-04-01 23:59:15 -05002494 return 0;
2495}
2496
Stephane Eranian69996df2012-02-09 23:21:06 +01002497static int read_attr(int fd, struct perf_header *ph,
2498 struct perf_file_attr *f_attr)
2499{
2500 struct perf_event_attr *attr = &f_attr->attr;
2501 size_t sz, left;
2502 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002503 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002504
2505 memset(f_attr, 0, sizeof(*f_attr));
2506
2507 /* read minimal guaranteed structure */
2508 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2509 if (ret <= 0) {
2510 pr_debug("cannot read %d bytes of header attr\n",
2511 PERF_ATTR_SIZE_VER0);
2512 return -1;
2513 }
2514
2515 /* on file perf_event_attr size */
2516 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002517
Stephane Eranian69996df2012-02-09 23:21:06 +01002518 if (ph->needs_swap)
2519 sz = bswap_32(sz);
2520
2521 if (sz == 0) {
2522 /* assume ABI0 */
2523 sz = PERF_ATTR_SIZE_VER0;
2524 } else if (sz > our_sz) {
2525 pr_debug("file uses a more recent and unsupported ABI"
2526 " (%zu bytes extra)\n", sz - our_sz);
2527 return -1;
2528 }
2529 /* what we have not yet read and that we know about */
2530 left = sz - PERF_ATTR_SIZE_VER0;
2531 if (left) {
2532 void *ptr = attr;
2533 ptr += PERF_ATTR_SIZE_VER0;
2534
2535 ret = readn(fd, ptr, left);
2536 }
2537 /* read perf_file_section, ids are read in caller */
2538 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2539
2540 return ret <= 0 ? -1 : 0;
2541}
2542
Namhyung Kim831394b2012-09-06 11:10:46 +09002543static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2544 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002545{
Namhyung Kim831394b2012-09-06 11:10:46 +09002546 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002547 char bf[128];
2548
Namhyung Kim831394b2012-09-06 11:10:46 +09002549 /* already prepared */
2550 if (evsel->tp_format)
2551 return 0;
2552
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002553 if (pevent == NULL) {
2554 pr_debug("broken or missing trace data\n");
2555 return -1;
2556 }
2557
Namhyung Kim831394b2012-09-06 11:10:46 +09002558 event = pevent_find_event(pevent, evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002559 if (event == NULL)
2560 return -1;
2561
Namhyung Kim831394b2012-09-06 11:10:46 +09002562 if (!evsel->name) {
2563 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2564 evsel->name = strdup(bf);
2565 if (evsel->name == NULL)
2566 return -1;
2567 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002568
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002569 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002570 return 0;
2571}
2572
Namhyung Kim831394b2012-09-06 11:10:46 +09002573static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2574 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002575{
2576 struct perf_evsel *pos;
2577
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002578 evlist__for_each(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002579 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2580 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002581 return -1;
2582 }
2583
2584 return 0;
2585}
2586
Jiri Olsad4339562013-07-17 19:49:41 +02002587int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002588{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002589 struct perf_data_file *file = session->file;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002590 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002591 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002592 struct perf_file_attr f_attr;
2593 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002594 int nr_attrs, nr_ids, i, j;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002595 int fd = perf_data_file__fd(file);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002596
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002597 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002598 if (session->evlist == NULL)
2599 return -ENOMEM;
2600
Kan Liang2c071442015-08-28 05:48:05 -04002601 session->evlist->env = &header->env;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002602 if (perf_data_file__is_pipe(file))
Jiri Olsad4339562013-07-17 19:49:41 +02002603 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002604
Stephane Eranian69996df2012-02-09 23:21:06 +01002605 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002606 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002607
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002608 /*
2609 * Sanity check that perf.data was written cleanly; data size is
2610 * initialized to 0 and updated only if the on_exit function is run.
2611 * If data size is still 0 then the file contains only partial
2612 * information. Just warn user and process it as much as it can.
2613 */
2614 if (f_header.data.size == 0) {
2615 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2616 "Was the 'perf record' command properly terminated?\n",
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002617 file->path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002618 }
2619
Stephane Eranian69996df2012-02-09 23:21:06 +01002620 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002621 lseek(fd, f_header.attrs.offset, SEEK_SET);
2622
2623 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002624 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002625 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002626
Stephane Eranian69996df2012-02-09 23:21:06 +01002627 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002628 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002629
David Ahern1060ab82015-04-09 16:15:46 -04002630 if (header->needs_swap) {
2631 f_attr.ids.size = bswap_64(f_attr.ids.size);
2632 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06002633 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04002634 }
David Aherneda39132011-07-15 12:34:09 -06002635
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002636 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002637 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002638
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002639 if (evsel == NULL)
2640 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002641
2642 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002643 /*
2644 * Do it before so that if perf_evsel__alloc_id fails, this
2645 * entry gets purged too at perf_evlist__delete().
2646 */
2647 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002648
2649 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002650 /*
2651 * We don't have the cpu and thread maps on the header, so
2652 * for allocating the perf_sample_id table we fake 1 cpu and
2653 * hattr->ids threads.
2654 */
2655 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2656 goto out_delete_evlist;
2657
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002658 lseek(fd, f_attr.ids.offset, SEEK_SET);
2659
2660 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002661 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002662 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002663
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002664 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002665 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002666
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002667 lseek(fd, tmp, SEEK_SET);
2668 }
2669
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002670 symbol_conf.nr_events = nr_attrs;
2671
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002672 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002673 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002674
Namhyung Kim831394b2012-09-06 11:10:46 +09002675 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002676 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002677 goto out_delete_evlist;
2678
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002679 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002680out_errno:
2681 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002682
2683out_delete_evlist:
2684 perf_evlist__delete(session->evlist);
2685 session->evlist = NULL;
2686 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002687}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002688
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002689int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002690 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002691 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002692{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002693 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002694 size_t size;
2695 int err;
2696
2697 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002698 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002699 size += sizeof(struct perf_event_header);
2700 size += ids * sizeof(u64);
2701
2702 ev = malloc(size);
2703
Chris Samuelce47dc52010-11-13 13:35:06 +11002704 if (ev == NULL)
2705 return -ENOMEM;
2706
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002707 ev->attr.attr = *attr;
2708 memcpy(ev->attr.id, id, ids * sizeof(u64));
2709
2710 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002711 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002712
Robert Richterf4d83432012-08-16 21:10:17 +02002713 if (ev->attr.header.size == size)
2714 err = process(tool, ev, NULL, NULL);
2715 else
2716 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002717
2718 free(ev);
2719
2720 return err;
2721}
2722
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002723int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002724 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002725 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002726{
Robert Richter6606f872012-08-16 21:10:19 +02002727 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002728 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002729
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002730 evlist__for_each(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002731 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2732 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002733 if (err) {
2734 pr_debug("failed to create perf header attribute\n");
2735 return err;
2736 }
2737 }
2738
2739 return err;
2740}
2741
Adrian Hunter47c3d102013-07-04 16:20:21 +03002742int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2743 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002744 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002745{
Robert Richterf4d83432012-08-16 21:10:17 +02002746 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002747 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002748 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002749
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002750 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002751 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002752 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002753 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002754 }
2755
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002756 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002757 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002758 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002759
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002760 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002761
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002762 ids = event->header.size;
2763 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002764 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002765 /*
2766 * We don't have the cpu and thread maps on the header, so
2767 * for allocating the perf_sample_id table we fake 1 cpu and
2768 * hattr->ids threads.
2769 */
2770 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2771 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002772
2773 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002774 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002775 }
2776
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03002777 symbol_conf.nr_events = evlist->nr_entries;
2778
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002779 return 0;
2780}
Tom Zanussicd19a032010-04-01 23:59:20 -05002781
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002782int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002783 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002784 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05002785{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002786 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02002787 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05002788 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002789 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05002790
Jiri Olsa29208e52011-10-20 15:59:43 +02002791 /*
2792 * We are going to store the size of the data followed
2793 * by the data contents. Since the fd descriptor is a pipe,
2794 * we cannot seek back to store the size of the data once
2795 * we know it. Instead we:
2796 *
2797 * - write the tracing data to the temp file
2798 * - get/write the data size to pipe
2799 * - write the tracing data from the temp file
2800 * to the pipe
2801 */
2802 tdata = tracing_data_get(&evlist->entries, fd, true);
2803 if (!tdata)
2804 return -1;
2805
Tom Zanussi92155452010-04-01 23:59:21 -05002806 memset(&ev, 0, sizeof(ev));
2807
2808 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02002809 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002810 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05002811 padding = aligned_size - size;
2812 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2813 ev.tracing_data.size = aligned_size;
2814
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002815 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05002816
Jiri Olsa29208e52011-10-20 15:59:43 +02002817 /*
2818 * The put function will copy all the tracing data
2819 * stored in temp file to the pipe.
2820 */
2821 tracing_data_put(tdata);
2822
Tom Zanussi92155452010-04-01 23:59:21 -05002823 write_padded(fd, NULL, 0, padding);
2824
2825 return aligned_size;
2826}
2827
Adrian Hunter47c3d102013-07-04 16:20:21 +03002828int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
2829 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002830 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05002831{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002832 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002833 int fd = perf_data_file__fd(session->file);
2834 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05002835 char buf[BUFSIZ];
2836
2837 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002838 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05002839 SEEK_SET);
2840
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002841 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002842 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002843 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05002844
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002845 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002846 pr_err("%s: reading input file", __func__);
2847 return -1;
2848 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002849 if (session->repipe) {
2850 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002851 if (retw <= 0 || retw != padding) {
2852 pr_err("%s: repiping tracing data padding", __func__);
2853 return -1;
2854 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002855 }
Tom Zanussi92155452010-04-01 23:59:21 -05002856
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002857 if (size_read + padding != size) {
2858 pr_err("%s: tracing data size mismatch", __func__);
2859 return -1;
2860 }
Tom Zanussi92155452010-04-01 23:59:21 -05002861
Namhyung Kim831394b2012-09-06 11:10:46 +09002862 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002863 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03002864
Tom Zanussi92155452010-04-01 23:59:21 -05002865 return size_read + padding;
2866}
Tom Zanussic7929e42010-04-01 23:59:22 -05002867
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002868int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002869 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002870 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002871 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05002872{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002873 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05002874 size_t len;
2875 int err = 0;
2876
2877 if (!pos->hit)
2878 return err;
2879
2880 memset(&ev, 0, sizeof(ev));
2881
2882 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002883 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05002884 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2885 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2886 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002887 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05002888 ev.build_id.header.size = sizeof(ev.build_id) + len;
2889 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2890
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002891 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05002892
2893 return err;
2894}
2895
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002896int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002897 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002898 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05002899{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002900 __event_process_build_id(&event->build_id,
2901 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002902 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05002903 return 0;
2904}