blob: 5ac7bdb0dff79b9d3af11f2f6d8a9539ea340619 [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 Eranian73323f52012-02-02 13:54:44 +010027/*
28 * magic2 = "PERFILE2"
29 * must be a numerical value to let the endianness
30 * determine the memory layout. That way we are able
31 * to detect endianness when reading the perf.data file
32 * back.
33 *
34 * we check for legacy (PERFFILE) format.
35 */
36static const char *__perf_magic1 = "PERFFILE";
37static const u64 __perf_magic2 = 0x32454c4946524550ULL;
38static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020039
Stephane Eranian73323f52012-02-02 13:54:44 +010040#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020041
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020042struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020043 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020044 struct perf_file_section ids;
45};
46
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030047void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020048{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030049 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020050}
51
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030052void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020053{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030054 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020055}
56
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030057bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020058{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030059 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020060}
61
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020062static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020063{
64 while (size) {
65 int ret = write(fd, buf, size);
66
67 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -020068 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020069
70 size -= ret;
71 buf += ret;
72 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020073
74 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020075}
76
Namhyung Kime195fac2014-11-04 10:14:30 +090077int write_padded(int fd, const void *bf, size_t count, size_t count_aligned)
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -020078{
79 static const char zero_buf[NAME_ALIGN];
80 int err = do_write(fd, bf, count);
81
82 if (!err)
83 err = do_write(fd, zero_buf, count_aligned - count);
84
85 return err;
86}
87
Kan Liang2bb00d22015-09-01 09:58:12 -040088#define string_size(str) \
89 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
90
Stephane Eranianfbe96f22011-09-30 15:40:40 +020091static int do_write_string(int fd, const char *str)
92{
93 u32 len, olen;
94 int ret;
95
96 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +030097 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +020098
99 /* write len, incl. \0 */
100 ret = do_write(fd, &len, sizeof(len));
101 if (ret < 0)
102 return ret;
103
104 return write_padded(fd, str, olen, len);
105}
106
107static char *do_read_string(int fd, struct perf_header *ph)
108{
109 ssize_t sz, ret;
110 u32 len;
111 char *buf;
112
Namhyung Kim5323f602012-12-17 15:38:54 +0900113 sz = readn(fd, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200114 if (sz < (ssize_t)sizeof(len))
115 return NULL;
116
117 if (ph->needs_swap)
118 len = bswap_32(len);
119
120 buf = malloc(len);
121 if (!buf)
122 return NULL;
123
Namhyung Kim5323f602012-12-17 15:38:54 +0900124 ret = readn(fd, buf, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200125 if (ret == (ssize_t)len) {
126 /*
127 * strings are padded by zeroes
128 * thus the actual strlen of buf
129 * may be less than len
130 */
131 return buf;
132 }
133
134 free(buf);
135 return NULL;
136}
137
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300138static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200139 struct perf_evlist *evlist)
140{
141 return read_tracing_data(fd, &evlist->entries);
142}
143
144
145static int write_build_id(int fd, struct perf_header *h,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300146 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200147{
148 struct perf_session *session;
149 int err;
150
151 session = container_of(h, struct perf_session, header);
152
Robert Richtere20960c2011-12-07 10:02:55 +0100153 if (!perf_session__read_build_ids(session, true))
154 return -1;
155
Namhyung Kim714c9c42014-11-04 10:14:29 +0900156 err = perf_session__write_buildid_table(session, fd);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200157 if (err < 0) {
158 pr_debug("failed to write buildid table\n");
159 return err;
160 }
Namhyung Kim73c5d222014-11-07 22:57:56 +0900161 perf_session__cache_build_ids(session);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200162
163 return 0;
164}
165
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300166static int write_hostname(int fd, struct perf_header *h __maybe_unused,
167 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200168{
169 struct utsname uts;
170 int ret;
171
172 ret = uname(&uts);
173 if (ret < 0)
174 return -1;
175
176 return do_write_string(fd, uts.nodename);
177}
178
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300179static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
180 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200181{
182 struct utsname uts;
183 int ret;
184
185 ret = uname(&uts);
186 if (ret < 0)
187 return -1;
188
189 return do_write_string(fd, uts.release);
190}
191
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300192static int write_arch(int fd, struct perf_header *h __maybe_unused,
193 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200194{
195 struct utsname uts;
196 int ret;
197
198 ret = uname(&uts);
199 if (ret < 0)
200 return -1;
201
202 return do_write_string(fd, uts.machine);
203}
204
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300205static int write_version(int fd, struct perf_header *h __maybe_unused,
206 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200207{
208 return do_write_string(fd, perf_version_string);
209}
210
Wang Nan493c3032014-10-24 09:45:26 +0800211static int __write_cpudesc(int fd, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200212{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200213 FILE *file;
214 char *buf = NULL;
215 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800216 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200217 size_t len = 0;
218 int ret = -1;
219
220 if (!search)
221 return -1;
222
223 file = fopen("/proc/cpuinfo", "r");
224 if (!file)
225 return -1;
226
227 while (getline(&buf, &len, file) > 0) {
228 ret = strncmp(buf, search, strlen(search));
229 if (!ret)
230 break;
231 }
232
Wang Naned307752014-10-16 11:08:29 +0800233 if (ret) {
234 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200235 goto done;
Wang Naned307752014-10-16 11:08:29 +0800236 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200237
238 s = buf;
239
240 p = strchr(buf, ':');
241 if (p && *(p+1) == ' ' && *(p+2))
242 s = p + 2;
243 p = strchr(s, '\n');
244 if (p)
245 *p = '\0';
246
247 /* squash extra space characters (branding string) */
248 p = s;
249 while (*p) {
250 if (isspace(*p)) {
251 char *r = p + 1;
252 char *q = r;
253 *p = ' ';
254 while (*q && isspace(*q))
255 q++;
256 if (q != (p+1))
257 while ((*r++ = *q++));
258 }
259 p++;
260 }
261 ret = do_write_string(fd, s);
262done:
263 free(buf);
264 fclose(file);
265 return ret;
266}
267
Wang Nan493c3032014-10-24 09:45:26 +0800268static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
269 struct perf_evlist *evlist __maybe_unused)
270{
271#ifndef CPUINFO_PROC
272#define CPUINFO_PROC {"model name", }
273#endif
274 const char *cpuinfo_procs[] = CPUINFO_PROC;
275 unsigned int i;
276
277 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
278 int ret;
279 ret = __write_cpudesc(fd, cpuinfo_procs[i]);
280 if (ret >= 0)
281 return ret;
282 }
283 return -1;
284}
285
286
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300287static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
288 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200289{
290 long nr;
291 u32 nrc, nra;
292 int ret;
293
294 nr = sysconf(_SC_NPROCESSORS_CONF);
295 if (nr < 0)
296 return -1;
297
298 nrc = (u32)(nr & UINT_MAX);
299
300 nr = sysconf(_SC_NPROCESSORS_ONLN);
301 if (nr < 0)
302 return -1;
303
304 nra = (u32)(nr & UINT_MAX);
305
306 ret = do_write(fd, &nrc, sizeof(nrc));
307 if (ret < 0)
308 return ret;
309
310 return do_write(fd, &nra, sizeof(nra));
311}
312
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300313static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200314 struct perf_evlist *evlist)
315{
Robert Richter6606f872012-08-16 21:10:19 +0200316 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900317 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200318 int ret;
319
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900320 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200321
322 /*
323 * write number of events
324 */
325 ret = do_write(fd, &nre, sizeof(nre));
326 if (ret < 0)
327 return ret;
328
329 /*
330 * size of perf_event_attr struct
331 */
Robert Richter6606f872012-08-16 21:10:19 +0200332 sz = (u32)sizeof(evsel->attr);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200333 ret = do_write(fd, &sz, sizeof(sz));
334 if (ret < 0)
335 return ret;
336
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300337 evlist__for_each(evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +0200338 ret = do_write(fd, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200339 if (ret < 0)
340 return ret;
341 /*
342 * write number of unique id per event
343 * there is one id per instance of an event
344 *
345 * copy into an nri to be independent of the
346 * type of ids,
347 */
Robert Richter6606f872012-08-16 21:10:19 +0200348 nri = evsel->ids;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200349 ret = do_write(fd, &nri, sizeof(nri));
350 if (ret < 0)
351 return ret;
352
353 /*
354 * write event string as passed on cmdline
355 */
Robert Richter6606f872012-08-16 21:10:19 +0200356 ret = do_write_string(fd, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200357 if (ret < 0)
358 return ret;
359 /*
360 * write unique ids for this event
361 */
Robert Richter6606f872012-08-16 21:10:19 +0200362 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200363 if (ret < 0)
364 return ret;
365 }
366 return 0;
367}
368
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300369static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
370 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200371{
372 char buf[MAXPATHLEN];
373 char proc[32];
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300374 u32 n;
375 int i, ret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200376
377 /*
378 * actual atual path to perf binary
379 */
380 sprintf(proc, "/proc/%d/exe", getpid());
381 ret = readlink(proc, buf, sizeof(buf));
382 if (ret <= 0)
383 return -1;
384
385 /* readlink() does not add null termination */
386 buf[ret] = '\0';
387
388 /* account for binary path */
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300389 n = perf_env.nr_cmdline + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200390
391 ret = do_write(fd, &n, sizeof(n));
392 if (ret < 0)
393 return ret;
394
395 ret = do_write_string(fd, buf);
396 if (ret < 0)
397 return ret;
398
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300399 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
400 ret = do_write_string(fd, perf_env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200401 if (ret < 0)
402 return ret;
403 }
404 return 0;
405}
406
407#define CORE_SIB_FMT \
408 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
409#define THRD_SIB_FMT \
410 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
411
412struct cpu_topo {
Kan Liang2bb00d22015-09-01 09:58:12 -0400413 u32 cpu_nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200414 u32 core_sib;
415 u32 thread_sib;
416 char **core_siblings;
417 char **thread_siblings;
418};
419
420static int build_cpu_topo(struct cpu_topo *tp, int cpu)
421{
422 FILE *fp;
423 char filename[MAXPATHLEN];
424 char *buf = NULL, *p;
425 size_t len = 0;
Stephane Eranianc5885742013-08-14 12:04:26 +0200426 ssize_t sret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200427 u32 i = 0;
428 int ret = -1;
429
430 sprintf(filename, CORE_SIB_FMT, cpu);
431 fp = fopen(filename, "r");
432 if (!fp)
Stephane Eranianc5885742013-08-14 12:04:26 +0200433 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200434
Stephane Eranianc5885742013-08-14 12:04:26 +0200435 sret = getline(&buf, &len, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200436 fclose(fp);
Stephane Eranianc5885742013-08-14 12:04:26 +0200437 if (sret <= 0)
438 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200439
440 p = strchr(buf, '\n');
441 if (p)
442 *p = '\0';
443
444 for (i = 0; i < tp->core_sib; i++) {
445 if (!strcmp(buf, tp->core_siblings[i]))
446 break;
447 }
448 if (i == tp->core_sib) {
449 tp->core_siblings[i] = buf;
450 tp->core_sib++;
451 buf = NULL;
452 len = 0;
453 }
Stephane Eranianc5885742013-08-14 12:04:26 +0200454 ret = 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200455
Stephane Eranianc5885742013-08-14 12:04:26 +0200456try_threads:
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200457 sprintf(filename, THRD_SIB_FMT, cpu);
458 fp = fopen(filename, "r");
459 if (!fp)
460 goto done;
461
462 if (getline(&buf, &len, fp) <= 0)
463 goto done;
464
465 p = strchr(buf, '\n');
466 if (p)
467 *p = '\0';
468
469 for (i = 0; i < tp->thread_sib; i++) {
470 if (!strcmp(buf, tp->thread_siblings[i]))
471 break;
472 }
473 if (i == tp->thread_sib) {
474 tp->thread_siblings[i] = buf;
475 tp->thread_sib++;
476 buf = NULL;
477 }
478 ret = 0;
479done:
480 if(fp)
481 fclose(fp);
482 free(buf);
483 return ret;
484}
485
486static void free_cpu_topo(struct cpu_topo *tp)
487{
488 u32 i;
489
490 if (!tp)
491 return;
492
493 for (i = 0 ; i < tp->core_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300494 zfree(&tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200495
496 for (i = 0 ; i < tp->thread_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300497 zfree(&tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200498
499 free(tp);
500}
501
502static struct cpu_topo *build_cpu_topology(void)
503{
504 struct cpu_topo *tp;
505 void *addr;
506 u32 nr, i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300507 size_t sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200508 long ncpus;
509 int ret = -1;
510
511 ncpus = sysconf(_SC_NPROCESSORS_CONF);
512 if (ncpus < 0)
513 return NULL;
514
515 nr = (u32)(ncpus & UINT_MAX);
516
517 sz = nr * sizeof(char *);
518
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300519 addr = calloc(1, sizeof(*tp) + 2 * sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200520 if (!addr)
521 return NULL;
522
523 tp = addr;
Kan Liang2bb00d22015-09-01 09:58:12 -0400524 tp->cpu_nr = nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200525 addr += sizeof(*tp);
526 tp->core_siblings = addr;
527 addr += sz;
528 tp->thread_siblings = addr;
529
530 for (i = 0; i < nr; i++) {
531 ret = build_cpu_topo(tp, i);
532 if (ret < 0)
533 break;
534 }
535 if (ret) {
536 free_cpu_topo(tp);
537 tp = NULL;
538 }
539 return tp;
540}
541
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300542static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
543 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200544{
545 struct cpu_topo *tp;
546 u32 i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300547 int ret, j;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200548
549 tp = build_cpu_topology();
550 if (!tp)
551 return -1;
552
553 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
554 if (ret < 0)
555 goto done;
556
557 for (i = 0; i < tp->core_sib; i++) {
558 ret = do_write_string(fd, tp->core_siblings[i]);
559 if (ret < 0)
560 goto done;
561 }
562 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
563 if (ret < 0)
564 goto done;
565
566 for (i = 0; i < tp->thread_sib; i++) {
567 ret = do_write_string(fd, tp->thread_siblings[i]);
568 if (ret < 0)
569 break;
570 }
Kan Liang2bb00d22015-09-01 09:58:12 -0400571
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300572 ret = perf_env__read_cpu_topology_map(&perf_env);
573 if (ret < 0)
574 goto done;
575
576 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
577 ret = do_write(fd, &perf_env.cpu[j].core_id,
578 sizeof(perf_env.cpu[j].core_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400579 if (ret < 0)
580 return ret;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300581 ret = do_write(fd, &perf_env.cpu[j].socket_id,
582 sizeof(perf_env.cpu[j].socket_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400583 if (ret < 0)
584 return ret;
585 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200586done:
587 free_cpu_topo(tp);
588 return ret;
589}
590
591
592
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300593static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
594 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200595{
596 char *buf = NULL;
597 FILE *fp;
598 size_t len = 0;
599 int ret = -1, n;
600 uint64_t mem;
601
602 fp = fopen("/proc/meminfo", "r");
603 if (!fp)
604 return -1;
605
606 while (getline(&buf, &len, fp) > 0) {
607 ret = strncmp(buf, "MemTotal:", 9);
608 if (!ret)
609 break;
610 }
611 if (!ret) {
612 n = sscanf(buf, "%*s %"PRIu64, &mem);
613 if (n == 1)
614 ret = do_write(fd, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800615 } else
616 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200617 free(buf);
618 fclose(fp);
619 return ret;
620}
621
622static int write_topo_node(int fd, int node)
623{
624 char str[MAXPATHLEN];
625 char field[32];
626 char *buf = NULL, *p;
627 size_t len = 0;
628 FILE *fp;
629 u64 mem_total, mem_free, mem;
630 int ret = -1;
631
632 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
633 fp = fopen(str, "r");
634 if (!fp)
635 return -1;
636
637 while (getline(&buf, &len, fp) > 0) {
638 /* skip over invalid lines */
639 if (!strchr(buf, ':'))
640 continue;
Alan Coxa761a2d2014-01-20 19:10:11 +0100641 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200642 goto done;
643 if (!strcmp(field, "MemTotal:"))
644 mem_total = mem;
645 if (!strcmp(field, "MemFree:"))
646 mem_free = mem;
647 }
648
649 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100650 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200651
652 ret = do_write(fd, &mem_total, sizeof(u64));
653 if (ret)
654 goto done;
655
656 ret = do_write(fd, &mem_free, sizeof(u64));
657 if (ret)
658 goto done;
659
660 ret = -1;
661 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
662
663 fp = fopen(str, "r");
664 if (!fp)
665 goto done;
666
667 if (getline(&buf, &len, fp) <= 0)
668 goto done;
669
670 p = strchr(buf, '\n');
671 if (p)
672 *p = '\0';
673
674 ret = do_write_string(fd, buf);
675done:
676 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100677 if (fp)
678 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200679 return ret;
680}
681
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300682static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
683 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200684{
685 char *buf = NULL;
686 size_t len = 0;
687 FILE *fp;
688 struct cpu_map *node_map = NULL;
689 char *c;
690 u32 nr, i, j;
691 int ret = -1;
692
693 fp = fopen("/sys/devices/system/node/online", "r");
694 if (!fp)
695 return -1;
696
697 if (getline(&buf, &len, fp) <= 0)
698 goto done;
699
700 c = strchr(buf, '\n');
701 if (c)
702 *c = '\0';
703
704 node_map = cpu_map__new(buf);
705 if (!node_map)
706 goto done;
707
708 nr = (u32)node_map->nr;
709
710 ret = do_write(fd, &nr, sizeof(nr));
711 if (ret < 0)
712 goto done;
713
714 for (i = 0; i < nr; i++) {
715 j = (u32)node_map->map[i];
716 ret = do_write(fd, &j, sizeof(j));
717 if (ret < 0)
718 break;
719
720 ret = write_topo_node(fd, i);
721 if (ret < 0)
722 break;
723 }
724done:
725 free(buf);
726 fclose(fp);
Masami Hiramatsu5191d8872015-12-09 11:11:35 +0900727 cpu_map__put(node_map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200728 return ret;
729}
730
731/*
Robert Richter50a96672012-08-16 21:10:24 +0200732 * File format:
733 *
734 * struct pmu_mappings {
735 * u32 pmu_num;
736 * struct pmu_map {
737 * u32 type;
738 * char name[];
739 * }[pmu_num];
740 * };
741 */
742
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300743static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
744 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +0200745{
746 struct perf_pmu *pmu = NULL;
747 off_t offset = lseek(fd, 0, SEEK_CUR);
748 __u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900749 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200750
751 /* write real pmu_num later */
Namhyung Kim5323f602012-12-17 15:38:54 +0900752 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
753 if (ret < 0)
754 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200755
756 while ((pmu = perf_pmu__scan(pmu))) {
757 if (!pmu->name)
758 continue;
759 pmu_num++;
Namhyung Kim5323f602012-12-17 15:38:54 +0900760
761 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
762 if (ret < 0)
763 return ret;
764
765 ret = do_write_string(fd, pmu->name);
766 if (ret < 0)
767 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200768 }
769
770 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
771 /* discard all */
772 lseek(fd, offset, SEEK_SET);
773 return -1;
774 }
775
776 return 0;
777}
778
779/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900780 * File format:
781 *
782 * struct group_descs {
783 * u32 nr_groups;
784 * struct group_desc {
785 * char name[];
786 * u32 leader_idx;
787 * u32 nr_members;
788 * }[nr_groups];
789 * };
790 */
791static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
792 struct perf_evlist *evlist)
793{
794 u32 nr_groups = evlist->nr_groups;
795 struct perf_evsel *evsel;
796 int ret;
797
798 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
799 if (ret < 0)
800 return ret;
801
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300802 evlist__for_each(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900803 if (perf_evsel__is_group_leader(evsel) &&
804 evsel->nr_members > 1) {
805 const char *name = evsel->group_name ?: "{anon_group}";
806 u32 leader_idx = evsel->idx;
807 u32 nr_members = evsel->nr_members;
808
809 ret = do_write_string(fd, name);
810 if (ret < 0)
811 return ret;
812
813 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
814 if (ret < 0)
815 return ret;
816
817 ret = do_write(fd, &nr_members, sizeof(nr_members));
818 if (ret < 0)
819 return ret;
820 }
821 }
822 return 0;
823}
824
825/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200826 * default get_cpuid(): nothing gets recorded
827 * actual implementation must be in arch/$(ARCH)/util/header.c
828 */
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300829int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused,
830 size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200831{
832 return -1;
833}
834
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300835static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
836 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200837{
838 char buffer[64];
839 int ret;
840
841 ret = get_cpuid(buffer, sizeof(buffer));
842 if (!ret)
843 goto write_it;
844
845 return -1;
846write_it:
847 return do_write_string(fd, buffer);
848}
849
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300850static int write_branch_stack(int fd __maybe_unused,
851 struct perf_header *h __maybe_unused,
852 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +0100853{
854 return 0;
855}
856
Adrian Hunter99fa2982015-04-30 17:37:25 +0300857static int write_auxtrace(int fd, struct perf_header *h,
Adrian Hunter4025ea42015-04-09 18:53:41 +0300858 struct perf_evlist *evlist __maybe_unused)
859{
Adrian Hunter99fa2982015-04-30 17:37:25 +0300860 struct perf_session *session;
861 int err;
862
863 session = container_of(h, struct perf_session, header);
864
865 err = auxtrace_index__write(fd, &session->auxtrace_index);
866 if (err < 0)
867 pr_err("Failed to write auxtrace index\n");
868 return err;
Adrian Hunter4025ea42015-04-09 18:53:41 +0300869}
870
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900871static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
872 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200873{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900874 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200875}
876
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900877static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
878 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200879{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900880 fprintf(fp, "# os release : %s\n", ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200881}
882
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900883static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200884{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900885 fprintf(fp, "# arch : %s\n", ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200886}
887
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900888static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
889 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200890{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900891 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200892}
893
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900894static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
895 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200896{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900897 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
898 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200899}
900
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900901static void print_version(struct perf_header *ph, int fd __maybe_unused,
902 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200903{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900904 fprintf(fp, "# perf version : %s\n", ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200905}
906
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900907static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
908 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200909{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900910 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200911
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900912 nr = ph->env.nr_cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200913
914 fprintf(fp, "# cmdline : ");
915
Jiri Olsa768dd3f2015-07-21 14:31:31 +0200916 for (i = 0; i < nr; i++)
917 fprintf(fp, "%s ", ph->env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200918 fputc('\n', fp);
919}
920
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900921static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
922 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200923{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900924 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200925 char *str;
Kan Liang2bb00d22015-09-01 09:58:12 -0400926 int cpu_nr = ph->env.nr_cpus_online;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200927
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900928 nr = ph->env.nr_sibling_cores;
929 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200930
931 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200932 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900933 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200934 }
935
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900936 nr = ph->env.nr_sibling_threads;
937 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200938
939 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200940 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +0900941 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200942 }
Kan Liang2bb00d22015-09-01 09:58:12 -0400943
944 if (ph->env.cpu != NULL) {
945 for (i = 0; i < cpu_nr; i++)
946 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
947 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
948 } else
949 fprintf(fp, "# Core ID and Socket ID information is not available\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200950}
951
Robert Richter4e1b9c62012-08-16 21:10:22 +0200952static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200953{
Robert Richter4e1b9c62012-08-16 21:10:22 +0200954 struct perf_evsel *evsel;
955
956 if (!events)
957 return;
958
959 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300960 zfree(&evsel->name);
961 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +0200962 }
963
964 free(events);
965}
966
967static struct perf_evsel *
968read_event_desc(struct perf_header *ph, int fd)
969{
970 struct perf_evsel *evsel, *events = NULL;
971 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200972 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +0100973 u32 nre, sz, nr, i, j;
974 ssize_t ret;
975 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200976
977 /* number of events */
Namhyung Kim5323f602012-12-17 15:38:54 +0900978 ret = readn(fd, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200979 if (ret != (ssize_t)sizeof(nre))
980 goto error;
981
982 if (ph->needs_swap)
983 nre = bswap_32(nre);
984
Namhyung Kim5323f602012-12-17 15:38:54 +0900985 ret = readn(fd, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200986 if (ret != (ssize_t)sizeof(sz))
987 goto error;
988
989 if (ph->needs_swap)
990 sz = bswap_32(sz);
991
Stephane Eranian62db9062012-02-09 23:21:07 +0100992 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200993 buf = malloc(sz);
994 if (!buf)
995 goto error;
996
Robert Richter4e1b9c62012-08-16 21:10:22 +0200997 /* the last event terminates with evsel->attr.size == 0: */
998 events = calloc(nre + 1, sizeof(*events));
999 if (!events)
1000 goto error;
1001
1002 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001003 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001004 msz = sz;
1005
Robert Richter4e1b9c62012-08-16 21:10:22 +02001006 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1007 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001008
Stephane Eranian62db9062012-02-09 23:21:07 +01001009 /*
1010 * must read entire on-file attr struct to
1011 * sync up with layout.
1012 */
Namhyung Kim5323f602012-12-17 15:38:54 +09001013 ret = readn(fd, buf, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001014 if (ret != (ssize_t)sz)
1015 goto error;
1016
1017 if (ph->needs_swap)
1018 perf_event__attr_swap(buf);
1019
Robert Richter4e1b9c62012-08-16 21:10:22 +02001020 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001021
Namhyung Kim5323f602012-12-17 15:38:54 +09001022 ret = readn(fd, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001023 if (ret != (ssize_t)sizeof(nr))
1024 goto error;
1025
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001026 if (ph->needs_swap) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001027 nr = bswap_32(nr);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001028 evsel->needs_swap = true;
1029 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001030
Robert Richter4e1b9c62012-08-16 21:10:22 +02001031 evsel->name = do_read_string(fd, ph);
1032
1033 if (!nr)
1034 continue;
1035
1036 id = calloc(nr, sizeof(*id));
1037 if (!id)
1038 goto error;
1039 evsel->ids = nr;
1040 evsel->id = id;
1041
1042 for (j = 0 ; j < nr; j++) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001043 ret = readn(fd, id, sizeof(*id));
Robert Richter4e1b9c62012-08-16 21:10:22 +02001044 if (ret != (ssize_t)sizeof(*id))
1045 goto error;
1046 if (ph->needs_swap)
1047 *id = bswap_64(*id);
1048 id++;
1049 }
1050 }
1051out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001052 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001053 return events;
1054error:
Markus Elfring4cc97612015-06-25 17:12:32 +02001055 free_event_desc(events);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001056 events = NULL;
1057 goto out;
1058}
1059
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001060static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1061 void *priv __attribute__((unused)))
1062{
1063 return fprintf(fp, ", %s = %s", name, val);
1064}
1065
Robert Richter4e1b9c62012-08-16 21:10:22 +02001066static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1067{
1068 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1069 u32 j;
1070 u64 *id;
1071
1072 if (!events) {
1073 fprintf(fp, "# event desc: not available or unable to read\n");
1074 return;
1075 }
1076
1077 for (evsel = events; evsel->attr.size; evsel++) {
1078 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001079
Robert Richter4e1b9c62012-08-16 21:10:22 +02001080 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001081 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001082 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1083 if (j)
1084 fputc(',', fp);
1085 fprintf(fp, " %"PRIu64, *id);
1086 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001087 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001088 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001089
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001090 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001091
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001092 fputc('\n', fp);
1093 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001094
1095 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001096}
1097
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001098static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001099 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001100{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001101 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001102}
1103
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001104static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001105 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001106{
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001107 u32 nr, c, i;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001108 char *str, *tmp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001109 uint64_t mem_total, mem_free;
1110
1111 /* nr nodes */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001112 nr = ph->env.nr_numa_nodes;
1113 str = ph->env.numa_nodes;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001114
1115 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001116 /* node number */
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001117 c = strtoul(str, &tmp, 0);
1118 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001119 goto error;
1120
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001121 str = tmp + 1;
1122 mem_total = strtoull(str, &tmp, 0);
1123 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001124 goto error;
1125
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001126 str = tmp + 1;
1127 mem_free = strtoull(str, &tmp, 0);
1128 if (*tmp != ':')
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001129 goto error;
1130
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001131 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1132 " free = %"PRIu64" kB\n",
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001133 c, mem_total, mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001134
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001135 str = tmp + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001136 fprintf(fp, "# node%u cpu list : %s\n", c, str);
Namhyung Kim12344712012-10-23 22:44:49 +09001137
1138 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001139 }
1140 return;
1141error:
1142 fprintf(fp, "# numa topology : not available\n");
1143}
1144
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001145static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001146{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001147 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001148}
1149
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001150static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001151 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001152{
1153 fprintf(fp, "# contains samples with branch stack\n");
1154}
1155
Adrian Hunter4025ea42015-04-09 18:53:41 +03001156static void print_auxtrace(struct perf_header *ph __maybe_unused,
1157 int fd __maybe_unused, FILE *fp)
1158{
1159 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1160}
1161
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001162static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1163 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001164{
1165 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001166 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001167 u32 pmu_num;
1168 u32 type;
1169
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001170 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001171 if (!pmu_num) {
1172 fprintf(fp, "# pmu mappings: not available\n");
1173 return;
1174 }
1175
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001176 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001177
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001178 while (pmu_num) {
1179 type = strtoul(str, &tmp, 0);
1180 if (*tmp != ':')
1181 goto error;
1182
1183 str = tmp + 1;
1184 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1185
Robert Richter50a96672012-08-16 21:10:24 +02001186 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001187 str += strlen(str) + 1;
1188 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001189 }
1190
1191 fprintf(fp, "\n");
1192
1193 if (!pmu_num)
1194 return;
1195error:
1196 fprintf(fp, "# pmu mappings: unable to read\n");
1197}
1198
Namhyung Kima8bb5592013-01-22 18:09:31 +09001199static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1200 FILE *fp)
1201{
1202 struct perf_session *session;
1203 struct perf_evsel *evsel;
1204 u32 nr = 0;
1205
1206 session = container_of(ph, struct perf_session, header);
1207
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001208 evlist__for_each(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001209 if (perf_evsel__is_group_leader(evsel) &&
1210 evsel->nr_members > 1) {
1211 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1212 perf_evsel__name(evsel));
1213
1214 nr = evsel->nr_members - 1;
1215 } else if (nr) {
1216 fprintf(fp, ",%s", perf_evsel__name(evsel));
1217
1218 if (--nr == 0)
1219 fprintf(fp, "}\n");
1220 }
1221 }
1222}
1223
Robert Richter08d95bd2012-02-10 15:41:55 +01001224static int __event_process_build_id(struct build_id_event *bev,
1225 char *filename,
1226 struct perf_session *session)
1227{
1228 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01001229 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00001230 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01001231 struct dso *dso;
1232 enum dso_kernel_type dso_type;
1233
1234 machine = perf_session__findnew_machine(session, bev->pid);
1235 if (!machine)
1236 goto out;
1237
Wang Nan1f121b02015-06-03 08:52:21 +00001238 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01001239
Wang Nan1f121b02015-06-03 08:52:21 +00001240 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01001241 case PERF_RECORD_MISC_KERNEL:
1242 dso_type = DSO_TYPE_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001243 break;
1244 case PERF_RECORD_MISC_GUEST_KERNEL:
1245 dso_type = DSO_TYPE_GUEST_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001246 break;
1247 case PERF_RECORD_MISC_USER:
1248 case PERF_RECORD_MISC_GUEST_USER:
1249 dso_type = DSO_TYPE_USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01001250 break;
1251 default:
1252 goto out;
1253 }
1254
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001255 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001256 if (dso != NULL) {
1257 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1258
1259 dso__set_build_id(dso, &bev->build_id);
1260
Wang Nan1f121b02015-06-03 08:52:21 +00001261 if (!is_kernel_module(filename, cpumode))
Robert Richter08d95bd2012-02-10 15:41:55 +01001262 dso->kernel = dso_type;
1263
1264 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1265 sbuild_id);
1266 pr_debug("build id event received for %s: %s\n",
1267 dso->long_name, sbuild_id);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001268 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01001269 }
1270
1271 err = 0;
1272out:
1273 return err;
1274}
1275
1276static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1277 int input, u64 offset, u64 size)
1278{
1279 struct perf_session *session = container_of(header, struct perf_session, header);
1280 struct {
1281 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001282 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001283 char filename[0];
1284 } old_bev;
1285 struct build_id_event bev;
1286 char filename[PATH_MAX];
1287 u64 limit = offset + size;
1288
1289 while (offset < limit) {
1290 ssize_t len;
1291
Namhyung Kim5323f602012-12-17 15:38:54 +09001292 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001293 return -1;
1294
1295 if (header->needs_swap)
1296 perf_event_header__bswap(&old_bev.header);
1297
1298 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001299 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001300 return -1;
1301
1302 bev.header = old_bev.header;
1303
1304 /*
1305 * As the pid is the missing value, we need to fill
1306 * it properly. The header.misc value give us nice hint.
1307 */
1308 bev.pid = HOST_KERNEL_ID;
1309 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1310 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1311 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1312
1313 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1314 __event_process_build_id(&bev, filename, session);
1315
1316 offset += bev.header.size;
1317 }
1318
1319 return 0;
1320}
1321
1322static int perf_header__read_build_ids(struct perf_header *header,
1323 int input, u64 offset, u64 size)
1324{
1325 struct perf_session *session = container_of(header, struct perf_session, header);
1326 struct build_id_event bev;
1327 char filename[PATH_MAX];
1328 u64 limit = offset + size, orig_offset = offset;
1329 int err = -1;
1330
1331 while (offset < limit) {
1332 ssize_t len;
1333
Namhyung Kim5323f602012-12-17 15:38:54 +09001334 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001335 goto out;
1336
1337 if (header->needs_swap)
1338 perf_event_header__bswap(&bev.header);
1339
1340 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001341 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001342 goto out;
1343 /*
1344 * The a1645ce1 changeset:
1345 *
1346 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1347 *
1348 * Added a field to struct build_id_event that broke the file
1349 * format.
1350 *
1351 * Since the kernel build-id is the first entry, process the
1352 * table using the old format if the well known
1353 * '[kernel.kallsyms]' string for the kernel build-id has the
1354 * first 4 characters chopped off (where the pid_t sits).
1355 */
1356 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1357 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1358 return -1;
1359 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1360 }
1361
1362 __event_process_build_id(&bev, filename, session);
1363
1364 offset += bev.header.size;
1365 }
1366 err = 0;
1367out:
1368 return err;
1369}
1370
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001371static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1372 struct perf_header *ph __maybe_unused,
1373 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001374{
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001375 ssize_t ret = trace_report(fd, data, false);
1376 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001377}
1378
1379static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001380 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001381 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001382{
1383 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1384 pr_debug("Failed to read buildids, continuing...\n");
1385 return 0;
1386}
1387
Namhyung Kima1ae5652012-09-24 17:14:59 +09001388static int process_hostname(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001389 struct perf_header *ph, int fd,
1390 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001391{
1392 ph->env.hostname = do_read_string(fd, ph);
1393 return ph->env.hostname ? 0 : -ENOMEM;
1394}
1395
1396static int process_osrelease(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001397 struct perf_header *ph, int fd,
1398 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001399{
1400 ph->env.os_release = do_read_string(fd, ph);
1401 return ph->env.os_release ? 0 : -ENOMEM;
1402}
1403
1404static int process_version(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001405 struct perf_header *ph, int fd,
1406 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001407{
1408 ph->env.version = do_read_string(fd, ph);
1409 return ph->env.version ? 0 : -ENOMEM;
1410}
1411
1412static int process_arch(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001413 struct perf_header *ph, int fd,
1414 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001415{
1416 ph->env.arch = do_read_string(fd, ph);
1417 return ph->env.arch ? 0 : -ENOMEM;
1418}
1419
1420static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001421 struct perf_header *ph, int fd,
1422 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001423{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001424 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001425 u32 nr;
1426
Namhyung Kim5323f602012-12-17 15:38:54 +09001427 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001428 if (ret != sizeof(nr))
1429 return -1;
1430
1431 if (ph->needs_swap)
1432 nr = bswap_32(nr);
1433
Arnaldo Carvalho de Melocaa47042015-09-11 12:36:12 -03001434 ph->env.nr_cpus_avail = nr;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001435
Namhyung Kim5323f602012-12-17 15:38:54 +09001436 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001437 if (ret != sizeof(nr))
1438 return -1;
1439
1440 if (ph->needs_swap)
1441 nr = bswap_32(nr);
1442
Arnaldo Carvalho de Melocaa47042015-09-11 12:36:12 -03001443 ph->env.nr_cpus_online = nr;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001444 return 0;
1445}
1446
1447static int process_cpudesc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001448 struct perf_header *ph, int fd,
1449 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001450{
1451 ph->env.cpu_desc = do_read_string(fd, ph);
1452 return ph->env.cpu_desc ? 0 : -ENOMEM;
1453}
1454
1455static int process_cpuid(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001456 struct perf_header *ph, int fd,
1457 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001458{
1459 ph->env.cpuid = do_read_string(fd, ph);
1460 return ph->env.cpuid ? 0 : -ENOMEM;
1461}
1462
1463static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001464 struct perf_header *ph, int fd,
1465 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001466{
1467 uint64_t mem;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001468 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001469
Namhyung Kim5323f602012-12-17 15:38:54 +09001470 ret = readn(fd, &mem, sizeof(mem));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001471 if (ret != sizeof(mem))
1472 return -1;
1473
1474 if (ph->needs_swap)
1475 mem = bswap_64(mem);
1476
1477 ph->env.total_mem = mem;
1478 return 0;
1479}
1480
Robert Richter7c2f7af2012-08-16 21:10:23 +02001481static struct perf_evsel *
1482perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1483{
1484 struct perf_evsel *evsel;
1485
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001486 evlist__for_each(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001487 if (evsel->idx == idx)
1488 return evsel;
1489 }
1490
1491 return NULL;
1492}
1493
1494static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001495perf_evlist__set_event_name(struct perf_evlist *evlist,
1496 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001497{
1498 struct perf_evsel *evsel;
1499
1500 if (!event->name)
1501 return;
1502
1503 evsel = perf_evlist__find_by_index(evlist, event->idx);
1504 if (!evsel)
1505 return;
1506
1507 if (evsel->name)
1508 return;
1509
1510 evsel->name = strdup(event->name);
1511}
1512
1513static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001514process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001515 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001516 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001517{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001518 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001519 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1520
1521 if (!events)
1522 return 0;
1523
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001524 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001525 for (evsel = events; evsel->attr.size; evsel++)
1526 perf_evlist__set_event_name(session->evlist, evsel);
1527
1528 free_event_desc(events);
1529
1530 return 0;
1531}
1532
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001533static int process_cmdline(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001534 struct perf_header *ph, int fd,
1535 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001536{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001537 ssize_t ret;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001538 char *str, *cmdline = NULL, **argv = NULL;
1539 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001540
Namhyung Kim5323f602012-12-17 15:38:54 +09001541 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001542 if (ret != sizeof(nr))
1543 return -1;
1544
1545 if (ph->needs_swap)
1546 nr = bswap_32(nr);
1547
1548 ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001549
1550 cmdline = zalloc(section->size + nr + 1);
1551 if (!cmdline)
1552 return -1;
1553
1554 argv = zalloc(sizeof(char *) * (nr + 1));
1555 if (!argv)
1556 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001557
1558 for (i = 0; i < nr; i++) {
1559 str = do_read_string(fd, ph);
1560 if (!str)
1561 goto error;
1562
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001563 argv[i] = cmdline + len;
1564 memcpy(argv[i], str, strlen(str) + 1);
1565 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001566 free(str);
1567 }
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001568 ph->env.cmdline = cmdline;
1569 ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001570 return 0;
1571
1572error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001573 free(argv);
1574 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001575 return -1;
1576}
1577
Kan Liang2bb00d22015-09-01 09:58:12 -04001578static int process_cpu_topology(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001579 struct perf_header *ph, int fd,
1580 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001581{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001582 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001583 u32 nr, i;
1584 char *str;
1585 struct strbuf sb;
Kan Liang2bb00d22015-09-01 09:58:12 -04001586 int cpu_nr = ph->env.nr_cpus_online;
1587 u64 size = 0;
1588
1589 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1590 if (!ph->env.cpu)
1591 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001592
Namhyung Kim5323f602012-12-17 15:38:54 +09001593 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001594 if (ret != sizeof(nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001595 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001596
1597 if (ph->needs_swap)
1598 nr = bswap_32(nr);
1599
1600 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001601 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001602 strbuf_init(&sb, 128);
1603
1604 for (i = 0; i < nr; i++) {
1605 str = do_read_string(fd, ph);
1606 if (!str)
1607 goto error;
1608
1609 /* include a NULL character at the end */
1610 strbuf_add(&sb, str, strlen(str) + 1);
Kan Liang2bb00d22015-09-01 09:58:12 -04001611 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001612 free(str);
1613 }
1614 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1615
Namhyung Kim5323f602012-12-17 15:38:54 +09001616 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001617 if (ret != sizeof(nr))
1618 return -1;
1619
1620 if (ph->needs_swap)
1621 nr = bswap_32(nr);
1622
1623 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001624 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001625
1626 for (i = 0; i < nr; i++) {
1627 str = do_read_string(fd, ph);
1628 if (!str)
1629 goto error;
1630
1631 /* include a NULL character at the end */
1632 strbuf_add(&sb, str, strlen(str) + 1);
Kan Liang2bb00d22015-09-01 09:58:12 -04001633 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001634 free(str);
1635 }
1636 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04001637
1638 /*
1639 * The header may be from old perf,
1640 * which doesn't include core id and socket id information.
1641 */
1642 if (section->size <= size) {
1643 zfree(&ph->env.cpu);
1644 return 0;
1645 }
1646
1647 for (i = 0; i < (u32)cpu_nr; i++) {
1648 ret = readn(fd, &nr, sizeof(nr));
1649 if (ret != sizeof(nr))
1650 goto free_cpu;
1651
1652 if (ph->needs_swap)
1653 nr = bswap_32(nr);
1654
1655 if (nr > (u32)cpu_nr) {
1656 pr_debug("core_id number is too big."
1657 "You may need to upgrade the perf tool.\n");
1658 goto free_cpu;
1659 }
1660 ph->env.cpu[i].core_id = nr;
1661
1662 ret = readn(fd, &nr, sizeof(nr));
1663 if (ret != sizeof(nr))
1664 goto free_cpu;
1665
1666 if (ph->needs_swap)
1667 nr = bswap_32(nr);
1668
1669 if (nr > (u32)cpu_nr) {
1670 pr_debug("socket_id number is too big."
1671 "You may need to upgrade the perf tool.\n");
1672 goto free_cpu;
1673 }
1674
1675 ph->env.cpu[i].socket_id = nr;
1676 }
1677
Namhyung Kima1ae5652012-09-24 17:14:59 +09001678 return 0;
1679
1680error:
1681 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04001682free_cpu:
1683 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001684 return -1;
1685}
1686
1687static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001688 struct perf_header *ph, int fd,
1689 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001690{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001691 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001692 u32 nr, node, i;
1693 char *str;
1694 uint64_t mem_total, mem_free;
1695 struct strbuf sb;
1696
1697 /* nr nodes */
Namhyung Kim5323f602012-12-17 15:38:54 +09001698 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001699 if (ret != sizeof(nr))
1700 goto error;
1701
1702 if (ph->needs_swap)
1703 nr = bswap_32(nr);
1704
1705 ph->env.nr_numa_nodes = nr;
1706 strbuf_init(&sb, 256);
1707
1708 for (i = 0; i < nr; i++) {
1709 /* node number */
Namhyung Kim5323f602012-12-17 15:38:54 +09001710 ret = readn(fd, &node, sizeof(node));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001711 if (ret != sizeof(node))
1712 goto error;
1713
Namhyung Kim5323f602012-12-17 15:38:54 +09001714 ret = readn(fd, &mem_total, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001715 if (ret != sizeof(u64))
1716 goto error;
1717
Namhyung Kim5323f602012-12-17 15:38:54 +09001718 ret = readn(fd, &mem_free, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001719 if (ret != sizeof(u64))
1720 goto error;
1721
1722 if (ph->needs_swap) {
1723 node = bswap_32(node);
1724 mem_total = bswap_64(mem_total);
1725 mem_free = bswap_64(mem_free);
1726 }
1727
1728 strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":",
1729 node, mem_total, mem_free);
1730
1731 str = do_read_string(fd, ph);
1732 if (!str)
1733 goto error;
1734
1735 /* include a NULL character at the end */
1736 strbuf_add(&sb, str, strlen(str) + 1);
1737 free(str);
1738 }
1739 ph->env.numa_nodes = strbuf_detach(&sb, NULL);
1740 return 0;
1741
1742error:
1743 strbuf_release(&sb);
1744 return -1;
1745}
1746
1747static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001748 struct perf_header *ph, int fd,
1749 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001750{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001751 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001752 char *name;
1753 u32 pmu_num;
1754 u32 type;
1755 struct strbuf sb;
1756
Namhyung Kim5323f602012-12-17 15:38:54 +09001757 ret = readn(fd, &pmu_num, sizeof(pmu_num));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001758 if (ret != sizeof(pmu_num))
1759 return -1;
1760
1761 if (ph->needs_swap)
1762 pmu_num = bswap_32(pmu_num);
1763
1764 if (!pmu_num) {
1765 pr_debug("pmu mappings not available\n");
1766 return 0;
1767 }
1768
1769 ph->env.nr_pmu_mappings = pmu_num;
1770 strbuf_init(&sb, 128);
1771
1772 while (pmu_num) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001773 if (readn(fd, &type, sizeof(type)) != sizeof(type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001774 goto error;
1775 if (ph->needs_swap)
1776 type = bswap_32(type);
1777
1778 name = do_read_string(fd, ph);
1779 if (!name)
1780 goto error;
1781
1782 strbuf_addf(&sb, "%u:%s", type, name);
1783 /* include a NULL character at the end */
1784 strbuf_add(&sb, "", 1);
1785
Kan Liange0838e02015-09-10 11:03:05 -03001786 if (!strcmp(name, "msr"))
1787 ph->env.msr_pmu_type = type;
1788
Namhyung Kima1ae5652012-09-24 17:14:59 +09001789 free(name);
1790 pmu_num--;
1791 }
1792 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1793 return 0;
1794
1795error:
1796 strbuf_release(&sb);
1797 return -1;
1798}
1799
Namhyung Kima8bb5592013-01-22 18:09:31 +09001800static int process_group_desc(struct perf_file_section *section __maybe_unused,
1801 struct perf_header *ph, int fd,
1802 void *data __maybe_unused)
1803{
1804 size_t ret = -1;
1805 u32 i, nr, nr_groups;
1806 struct perf_session *session;
1807 struct perf_evsel *evsel, *leader = NULL;
1808 struct group_desc {
1809 char *name;
1810 u32 leader_idx;
1811 u32 nr_members;
1812 } *desc;
1813
1814 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
1815 return -1;
1816
1817 if (ph->needs_swap)
1818 nr_groups = bswap_32(nr_groups);
1819
1820 ph->env.nr_groups = nr_groups;
1821 if (!nr_groups) {
1822 pr_debug("group desc not available\n");
1823 return 0;
1824 }
1825
1826 desc = calloc(nr_groups, sizeof(*desc));
1827 if (!desc)
1828 return -1;
1829
1830 for (i = 0; i < nr_groups; i++) {
1831 desc[i].name = do_read_string(fd, ph);
1832 if (!desc[i].name)
1833 goto out_free;
1834
1835 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
1836 goto out_free;
1837
1838 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
1839 goto out_free;
1840
1841 if (ph->needs_swap) {
1842 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
1843 desc[i].nr_members = bswap_32(desc[i].nr_members);
1844 }
1845 }
1846
1847 /*
1848 * Rebuild group relationship based on the group_desc
1849 */
1850 session = container_of(ph, struct perf_session, header);
1851 session->evlist->nr_groups = nr_groups;
1852
1853 i = nr = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001854 evlist__for_each(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001855 if (evsel->idx == (int) desc[i].leader_idx) {
1856 evsel->leader = evsel;
1857 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09001858 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001859 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09001860 desc[i].name = NULL;
1861 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09001862 evsel->nr_members = desc[i].nr_members;
1863
1864 if (i >= nr_groups || nr > 0) {
1865 pr_debug("invalid group desc\n");
1866 goto out_free;
1867 }
1868
1869 leader = evsel;
1870 nr = evsel->nr_members - 1;
1871 i++;
1872 } else if (nr) {
1873 /* This is a group member */
1874 evsel->leader = leader;
1875
1876 nr--;
1877 }
1878 }
1879
1880 if (i != nr_groups || nr != 0) {
1881 pr_debug("invalid group desc\n");
1882 goto out_free;
1883 }
1884
1885 ret = 0;
1886out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09001887 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001888 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001889 free(desc);
1890
1891 return ret;
1892}
1893
Adrian Hunter99fa2982015-04-30 17:37:25 +03001894static int process_auxtrace(struct perf_file_section *section,
1895 struct perf_header *ph, int fd,
1896 void *data __maybe_unused)
1897{
1898 struct perf_session *session;
1899 int err;
1900
1901 session = container_of(ph, struct perf_session, header);
1902
1903 err = auxtrace_index__process(fd, section->size, session,
1904 ph->needs_swap);
1905 if (err < 0)
1906 pr_err("Failed to process auxtrace index\n");
1907 return err;
1908}
1909
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001910struct feature_ops {
1911 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1912 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01001913 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001914 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001915 const char *name;
1916 bool full_only;
1917};
1918
Robert Richter8cdfa782011-12-07 10:02:56 +01001919#define FEAT_OPA(n, func) \
1920 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01001921#define FEAT_OPP(n, func) \
1922 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1923 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01001924#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01001925 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09001926 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01001927
1928/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001929#define print_tracing_data NULL
1930#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001931
1932static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001933 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01001934 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001935 FEAT_OPP(HEADER_HOSTNAME, hostname),
1936 FEAT_OPP(HEADER_OSRELEASE, osrelease),
1937 FEAT_OPP(HEADER_VERSION, version),
1938 FEAT_OPP(HEADER_ARCH, arch),
1939 FEAT_OPP(HEADER_NRCPUS, nrcpus),
1940 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09001941 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001942 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02001943 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001944 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01001945 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1946 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01001947 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09001948 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09001949 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Adrian Hunter99fa2982015-04-30 17:37:25 +03001950 FEAT_OPP(HEADER_AUXTRACE, auxtrace),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001951};
1952
1953struct header_print_data {
1954 FILE *fp;
1955 bool full; /* extended list of headers */
1956};
1957
1958static int perf_file_section__fprintf_info(struct perf_file_section *section,
1959 struct perf_header *ph,
1960 int feat, int fd, void *data)
1961{
1962 struct header_print_data *hd = data;
1963
1964 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1965 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1966 "%d, continuing...\n", section->offset, feat);
1967 return 0;
1968 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001969 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001970 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01001971 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001972 }
1973 if (!feat_ops[feat].print)
1974 return 0;
1975
1976 if (!feat_ops[feat].full_only || hd->full)
1977 feat_ops[feat].print(ph, fd, hd->fp);
1978 else
1979 fprintf(hd->fp, "# %s info available, use -I to display\n",
1980 feat_ops[feat].name);
1981
1982 return 0;
1983}
1984
1985int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
1986{
1987 struct header_print_data hd;
1988 struct perf_header *header = &session->header;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02001989 int fd = perf_data_file__fd(session->file);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001990 hd.fp = fp;
1991 hd.full = full;
1992
1993 perf_header__process_sections(header, fd, &hd,
1994 perf_file_section__fprintf_info);
1995 return 0;
1996}
1997
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001998static int do_write_feat(int fd, struct perf_header *h, int type,
1999 struct perf_file_section **p,
2000 struct perf_evlist *evlist)
2001{
2002 int err;
2003 int ret = 0;
2004
2005 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002006 if (!feat_ops[type].write)
2007 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002008
2009 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2010
2011 err = feat_ops[type].write(fd, h, evlist);
2012 if (err < 0) {
2013 pr_debug("failed to write feature %d\n", type);
2014
2015 /* undo anything written */
2016 lseek(fd, (*p)->offset, SEEK_SET);
2017
2018 return -1;
2019 }
2020 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2021 (*p)++;
2022 }
2023 return ret;
2024}
2025
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002026static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002027 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002028{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002029 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002030 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002031 int sec_size;
2032 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002033 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002034 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002035
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002036 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002037 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002038 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002039
Paul Gortmaker91b98802013-01-30 20:05:49 -05002040 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002041 if (feat_sec == NULL)
2042 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002043
2044 sec_size = sizeof(*feat_sec) * nr_sections;
2045
Jiri Olsa8d541e92013-07-17 19:49:44 +02002046 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002047 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002048
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002049 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2050 if (do_write_feat(fd, header, feat, &p, evlist))
2051 perf_header__clear_feat(header, feat);
2052 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002053
Xiao Guangrongf887f302010-02-04 16:46:42 +08002054 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002055 /*
2056 * may write more than needed due to dropped feature, but
2057 * this is okay, reader will skip the mising entries
2058 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002059 err = do_write(fd, feat_sec, sec_size);
2060 if (err < 0)
2061 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002062 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002063 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002064}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002065
Tom Zanussi8dc58102010-04-01 23:59:15 -05002066int perf_header__write_pipe(int fd)
2067{
2068 struct perf_pipe_file_header f_header;
2069 int err;
2070
2071 f_header = (struct perf_pipe_file_header){
2072 .magic = PERF_MAGIC,
2073 .size = sizeof(f_header),
2074 };
2075
2076 err = do_write(fd, &f_header, sizeof(f_header));
2077 if (err < 0) {
2078 pr_debug("failed to write perf pipe header\n");
2079 return err;
2080 }
2081
2082 return 0;
2083}
2084
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002085int perf_session__write_header(struct perf_session *session,
2086 struct perf_evlist *evlist,
2087 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002088{
2089 struct perf_file_header f_header;
2090 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002091 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002092 struct perf_evsel *evsel;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002093 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002094 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002095
2096 lseek(fd, sizeof(f_header), SEEK_SET);
2097
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002098 evlist__for_each(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002099 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2100 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002101 if (err < 0) {
2102 pr_debug("failed to write perf header\n");
2103 return err;
2104 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002105 }
2106
Jiri Olsa944d62b2013-07-17 19:49:43 +02002107 attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002108
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002109 evlist__for_each(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002110 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002111 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002112 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002113 .offset = evsel->id_offset,
2114 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002115 }
2116 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002117 err = do_write(fd, &f_attr, sizeof(f_attr));
2118 if (err < 0) {
2119 pr_debug("failed to write perf header attribute\n");
2120 return err;
2121 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002122 }
2123
Adrian Hunterd645c442013-12-11 14:36:28 +02002124 if (!header->data_offset)
2125 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002126 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002127
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002128 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002129 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002130 if (err < 0)
2131 return err;
2132 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002133
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002134 f_header = (struct perf_file_header){
2135 .magic = PERF_MAGIC,
2136 .size = sizeof(f_header),
2137 .attr_size = sizeof(f_attr),
2138 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002139 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002140 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002141 },
2142 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002143 .offset = header->data_offset,
2144 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002145 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002146 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002147 };
2148
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002149 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002150
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002151 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002152 err = do_write(fd, &f_header, sizeof(f_header));
2153 if (err < 0) {
2154 pr_debug("failed to write perf header\n");
2155 return err;
2156 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002157 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002158
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002159 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002160}
2161
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002162static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002163 int fd, void *buf, size_t size)
2164{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002165 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002166 return -1;
2167
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002168 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002169 mem_bswap_64(buf, size);
2170
2171 return 0;
2172}
2173
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002174int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002175 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002176 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002177 struct perf_header *ph,
2178 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002179{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002180 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002181 int nr_sections;
2182 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002183 int feat;
2184 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002185
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002186 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002187 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002188 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002189
Paul Gortmaker91b98802013-01-30 20:05:49 -05002190 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002191 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002192 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002193
2194 sec_size = sizeof(*feat_sec) * nr_sections;
2195
Jiri Olsa8d541e92013-07-17 19:49:44 +02002196 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002197
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002198 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2199 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002200 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002201
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002202 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2203 err = process(sec++, header, feat, fd, data);
2204 if (err < 0)
2205 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002206 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002207 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002208out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002209 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002210 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002211}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002212
Stephane Eranian114382a2012-02-09 23:21:08 +01002213static const int attr_file_abi_sizes[] = {
2214 [0] = PERF_ATTR_SIZE_VER0,
2215 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002216 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002217 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002218 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002219 0,
2220};
2221
2222/*
2223 * In the legacy file format, the magic number is not used to encode endianness.
2224 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2225 * on ABI revisions, we need to try all combinations for all endianness to
2226 * detect the endianness.
2227 */
2228static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2229{
2230 uint64_t ref_size, attr_size;
2231 int i;
2232
2233 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2234 ref_size = attr_file_abi_sizes[i]
2235 + sizeof(struct perf_file_section);
2236 if (hdr_sz != ref_size) {
2237 attr_size = bswap_64(hdr_sz);
2238 if (attr_size != ref_size)
2239 continue;
2240
2241 ph->needs_swap = true;
2242 }
2243 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2244 i,
2245 ph->needs_swap);
2246 return 0;
2247 }
2248 /* could not determine endianness */
2249 return -1;
2250}
2251
2252#define PERF_PIPE_HDR_VER0 16
2253
2254static const size_t attr_pipe_abi_sizes[] = {
2255 [0] = PERF_PIPE_HDR_VER0,
2256 0,
2257};
2258
2259/*
2260 * In the legacy pipe format, there is an implicit assumption that endiannesss
2261 * between host recording the samples, and host parsing the samples is the
2262 * same. This is not always the case given that the pipe output may always be
2263 * redirected into a file and analyzed on a different machine with possibly a
2264 * different endianness and perf_event ABI revsions in the perf tool itself.
2265 */
2266static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2267{
2268 u64 attr_size;
2269 int i;
2270
2271 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2272 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2273 attr_size = bswap_64(hdr_sz);
2274 if (attr_size != hdr_sz)
2275 continue;
2276
2277 ph->needs_swap = true;
2278 }
2279 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2280 return 0;
2281 }
2282 return -1;
2283}
2284
Feng Tange84ba4e2012-10-30 11:56:07 +08002285bool is_perf_magic(u64 magic)
2286{
2287 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2288 || magic == __perf_magic2
2289 || magic == __perf_magic2_sw)
2290 return true;
2291
2292 return false;
2293}
2294
Stephane Eranian114382a2012-02-09 23:21:08 +01002295static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2296 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002297{
2298 int ret;
2299
2300 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002301 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002302 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002303 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002304 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002305 if (is_pipe)
2306 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002307
Stephane Eranian114382a2012-02-09 23:21:08 +01002308 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002309 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002310 /*
2311 * the new magic number serves two purposes:
2312 * - unique number to identify actual perf.data files
2313 * - encode endianness of file
2314 */
Namhyung Kimf7913972015-01-29 17:06:45 +09002315 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002316
Stephane Eranian114382a2012-02-09 23:21:08 +01002317 /* check magic number with one endianness */
2318 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002319 return 0;
2320
Stephane Eranian114382a2012-02-09 23:21:08 +01002321 /* check magic number with opposite endianness */
2322 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002323 return -1;
2324
2325 ph->needs_swap = true;
2326
2327 return 0;
2328}
2329
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002330int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002331 struct perf_header *ph, int fd)
2332{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002333 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002334
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002335 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002336
Stephane Eranian73323f52012-02-02 13:54:44 +01002337 ret = readn(fd, header, sizeof(*header));
2338 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002339 return -1;
2340
Stephane Eranian114382a2012-02-09 23:21:08 +01002341 if (check_magic_endian(header->magic,
2342 header->attr_size, false, ph) < 0) {
2343 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002344 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002345 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002346
Stephane Eranian73323f52012-02-02 13:54:44 +01002347 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002348 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002349 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002350 }
2351
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002352 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002353 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002354 if (header->size == offsetof(typeof(*header), adds_features))
2355 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002356 else
2357 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002358 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002359 /*
2360 * feature bitmap is declared as an array of unsigned longs --
2361 * not good since its size can differ between the host that
2362 * generated the data file and the host analyzing the file.
2363 *
2364 * We need to handle endianness, but we don't know the size of
2365 * the unsigned long where the file was generated. Take a best
2366 * guess at determining it: try 64-bit swap first (ie., file
2367 * created on a 64-bit host), and check if the hostname feature
2368 * bit is set (this feature bit is forced on as of fbe96f2).
2369 * If the bit is not, undo the 64-bit swap and try a 32-bit
2370 * swap. If the hostname bit is still not set (e.g., older data
2371 * file), punt and fallback to the original behavior --
2372 * clearing all feature bits and setting buildid.
2373 */
David Ahern80c01202012-06-08 11:47:51 -03002374 mem_bswap_64(&header->adds_features,
2375 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002376
2377 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002378 /* unswap as u64 */
2379 mem_bswap_64(&header->adds_features,
2380 BITS_TO_U64(HEADER_FEAT_BITS));
2381
2382 /* unswap as u32 */
2383 mem_bswap_32(&header->adds_features,
2384 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002385 }
2386
2387 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2388 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2389 set_bit(HEADER_BUILD_ID, header->adds_features);
2390 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002391 }
2392
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002393 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002394 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002395
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002396 ph->data_offset = header->data.offset;
2397 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002398 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002399 return 0;
2400}
2401
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002402static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002403 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002404 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002405{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002406 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002407 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002408 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002409 return 0;
2410 }
2411
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002412 if (feat >= HEADER_LAST_FEATURE) {
2413 pr_debug("unknown feature %d, continuing...\n", feat);
2414 return 0;
2415 }
2416
Robert Richterf1c67db2012-02-10 15:41:56 +01002417 if (!feat_ops[feat].process)
2418 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002419
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002420 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002421}
2422
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002423static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002424 struct perf_header *ph, int fd,
2425 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002426{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002427 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002428
2429 ret = readn(fd, header, sizeof(*header));
2430 if (ret <= 0)
2431 return -1;
2432
Stephane Eranian114382a2012-02-09 23:21:08 +01002433 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2434 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002435 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002436 }
2437
2438 if (ph->needs_swap)
2439 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002440
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002441 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002442 return -1;
2443
Tom Zanussi8dc58102010-04-01 23:59:15 -05002444 return 0;
2445}
2446
Jiri Olsad4339562013-07-17 19:49:41 +02002447static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002448{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002449 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002450 struct perf_pipe_file_header f_header;
2451
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002452 if (perf_file_header__read_pipe(&f_header, header,
2453 perf_data_file__fd(session->file),
Tom Zanussi454c4072010-05-01 01:41:20 -05002454 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002455 pr_debug("incompatible file format\n");
2456 return -EINVAL;
2457 }
2458
Tom Zanussi8dc58102010-04-01 23:59:15 -05002459 return 0;
2460}
2461
Stephane Eranian69996df2012-02-09 23:21:06 +01002462static int read_attr(int fd, struct perf_header *ph,
2463 struct perf_file_attr *f_attr)
2464{
2465 struct perf_event_attr *attr = &f_attr->attr;
2466 size_t sz, left;
2467 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002468 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002469
2470 memset(f_attr, 0, sizeof(*f_attr));
2471
2472 /* read minimal guaranteed structure */
2473 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2474 if (ret <= 0) {
2475 pr_debug("cannot read %d bytes of header attr\n",
2476 PERF_ATTR_SIZE_VER0);
2477 return -1;
2478 }
2479
2480 /* on file perf_event_attr size */
2481 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002482
Stephane Eranian69996df2012-02-09 23:21:06 +01002483 if (ph->needs_swap)
2484 sz = bswap_32(sz);
2485
2486 if (sz == 0) {
2487 /* assume ABI0 */
2488 sz = PERF_ATTR_SIZE_VER0;
2489 } else if (sz > our_sz) {
2490 pr_debug("file uses a more recent and unsupported ABI"
2491 " (%zu bytes extra)\n", sz - our_sz);
2492 return -1;
2493 }
2494 /* what we have not yet read and that we know about */
2495 left = sz - PERF_ATTR_SIZE_VER0;
2496 if (left) {
2497 void *ptr = attr;
2498 ptr += PERF_ATTR_SIZE_VER0;
2499
2500 ret = readn(fd, ptr, left);
2501 }
2502 /* read perf_file_section, ids are read in caller */
2503 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2504
2505 return ret <= 0 ? -1 : 0;
2506}
2507
Namhyung Kim831394b2012-09-06 11:10:46 +09002508static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2509 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002510{
Namhyung Kim831394b2012-09-06 11:10:46 +09002511 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002512 char bf[128];
2513
Namhyung Kim831394b2012-09-06 11:10:46 +09002514 /* already prepared */
2515 if (evsel->tp_format)
2516 return 0;
2517
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002518 if (pevent == NULL) {
2519 pr_debug("broken or missing trace data\n");
2520 return -1;
2521 }
2522
Namhyung Kim831394b2012-09-06 11:10:46 +09002523 event = pevent_find_event(pevent, evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002524 if (event == NULL)
2525 return -1;
2526
Namhyung Kim831394b2012-09-06 11:10:46 +09002527 if (!evsel->name) {
2528 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2529 evsel->name = strdup(bf);
2530 if (evsel->name == NULL)
2531 return -1;
2532 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002533
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002534 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002535 return 0;
2536}
2537
Namhyung Kim831394b2012-09-06 11:10:46 +09002538static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2539 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002540{
2541 struct perf_evsel *pos;
2542
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002543 evlist__for_each(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002544 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2545 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002546 return -1;
2547 }
2548
2549 return 0;
2550}
2551
Jiri Olsad4339562013-07-17 19:49:41 +02002552int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002553{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002554 struct perf_data_file *file = session->file;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002555 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002556 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002557 struct perf_file_attr f_attr;
2558 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002559 int nr_attrs, nr_ids, i, j;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002560 int fd = perf_data_file__fd(file);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002561
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002562 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002563 if (session->evlist == NULL)
2564 return -ENOMEM;
2565
Kan Liang2c071442015-08-28 05:48:05 -04002566 session->evlist->env = &header->env;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -03002567 session->machines.host.env = &header->env;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002568 if (perf_data_file__is_pipe(file))
Jiri Olsad4339562013-07-17 19:49:41 +02002569 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002570
Stephane Eranian69996df2012-02-09 23:21:06 +01002571 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002572 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002573
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002574 /*
2575 * Sanity check that perf.data was written cleanly; data size is
2576 * initialized to 0 and updated only if the on_exit function is run.
2577 * If data size is still 0 then the file contains only partial
2578 * information. Just warn user and process it as much as it can.
2579 */
2580 if (f_header.data.size == 0) {
2581 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2582 "Was the 'perf record' command properly terminated?\n",
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002583 file->path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002584 }
2585
Stephane Eranian69996df2012-02-09 23:21:06 +01002586 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002587 lseek(fd, f_header.attrs.offset, SEEK_SET);
2588
2589 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002590 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002591 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002592
Stephane Eranian69996df2012-02-09 23:21:06 +01002593 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002594 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002595
David Ahern1060ab82015-04-09 16:15:46 -04002596 if (header->needs_swap) {
2597 f_attr.ids.size = bswap_64(f_attr.ids.size);
2598 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06002599 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04002600 }
David Aherneda39132011-07-15 12:34:09 -06002601
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002602 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002603 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002604
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002605 if (evsel == NULL)
2606 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002607
2608 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002609 /*
2610 * Do it before so that if perf_evsel__alloc_id fails, this
2611 * entry gets purged too at perf_evlist__delete().
2612 */
2613 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002614
2615 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002616 /*
2617 * We don't have the cpu and thread maps on the header, so
2618 * for allocating the perf_sample_id table we fake 1 cpu and
2619 * hattr->ids threads.
2620 */
2621 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2622 goto out_delete_evlist;
2623
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002624 lseek(fd, f_attr.ids.offset, SEEK_SET);
2625
2626 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002627 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002628 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002629
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002630 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002631 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002632
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002633 lseek(fd, tmp, SEEK_SET);
2634 }
2635
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002636 symbol_conf.nr_events = nr_attrs;
2637
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002638 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002639 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002640
Namhyung Kim831394b2012-09-06 11:10:46 +09002641 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002642 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002643 goto out_delete_evlist;
2644
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002645 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002646out_errno:
2647 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002648
2649out_delete_evlist:
2650 perf_evlist__delete(session->evlist);
2651 session->evlist = NULL;
2652 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002653}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002654
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002655int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002656 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002657 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002658{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002659 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002660 size_t size;
2661 int err;
2662
2663 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002664 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002665 size += sizeof(struct perf_event_header);
2666 size += ids * sizeof(u64);
2667
2668 ev = malloc(size);
2669
Chris Samuelce47dc52010-11-13 13:35:06 +11002670 if (ev == NULL)
2671 return -ENOMEM;
2672
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002673 ev->attr.attr = *attr;
2674 memcpy(ev->attr.id, id, ids * sizeof(u64));
2675
2676 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002677 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002678
Robert Richterf4d83432012-08-16 21:10:17 +02002679 if (ev->attr.header.size == size)
2680 err = process(tool, ev, NULL, NULL);
2681 else
2682 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002683
2684 free(ev);
2685
2686 return err;
2687}
2688
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002689int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002690 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002691 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002692{
Robert Richter6606f872012-08-16 21:10:19 +02002693 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002694 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002695
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002696 evlist__for_each(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002697 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2698 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002699 if (err) {
2700 pr_debug("failed to create perf header attribute\n");
2701 return err;
2702 }
2703 }
2704
2705 return err;
2706}
2707
Adrian Hunter47c3d102013-07-04 16:20:21 +03002708int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
2709 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002710 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002711{
Robert Richterf4d83432012-08-16 21:10:17 +02002712 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002713 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002714 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002715
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002716 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002717 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002718 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002719 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002720 }
2721
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002722 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002723 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002724 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002725
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002726 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002727
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002728 ids = event->header.size;
2729 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002730 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002731 /*
2732 * We don't have the cpu and thread maps on the header, so
2733 * for allocating the perf_sample_id table we fake 1 cpu and
2734 * hattr->ids threads.
2735 */
2736 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2737 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002738
2739 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002740 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002741 }
2742
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03002743 symbol_conf.nr_events = evlist->nr_entries;
2744
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002745 return 0;
2746}
Tom Zanussicd19a032010-04-01 23:59:20 -05002747
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002748int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002749 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002750 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05002751{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002752 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02002753 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05002754 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002755 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05002756
Jiri Olsa29208e52011-10-20 15:59:43 +02002757 /*
2758 * We are going to store the size of the data followed
2759 * by the data contents. Since the fd descriptor is a pipe,
2760 * we cannot seek back to store the size of the data once
2761 * we know it. Instead we:
2762 *
2763 * - write the tracing data to the temp file
2764 * - get/write the data size to pipe
2765 * - write the tracing data from the temp file
2766 * to the pipe
2767 */
2768 tdata = tracing_data_get(&evlist->entries, fd, true);
2769 if (!tdata)
2770 return -1;
2771
Tom Zanussi92155452010-04-01 23:59:21 -05002772 memset(&ev, 0, sizeof(ev));
2773
2774 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02002775 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002776 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05002777 padding = aligned_size - size;
2778 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2779 ev.tracing_data.size = aligned_size;
2780
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002781 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05002782
Jiri Olsa29208e52011-10-20 15:59:43 +02002783 /*
2784 * The put function will copy all the tracing data
2785 * stored in temp file to the pipe.
2786 */
2787 tracing_data_put(tdata);
2788
Tom Zanussi92155452010-04-01 23:59:21 -05002789 write_padded(fd, NULL, 0, padding);
2790
2791 return aligned_size;
2792}
2793
Adrian Hunter47c3d102013-07-04 16:20:21 +03002794int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
2795 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002796 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05002797{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002798 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002799 int fd = perf_data_file__fd(session->file);
2800 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05002801 char buf[BUFSIZ];
2802
2803 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002804 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05002805 SEEK_SET);
2806
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002807 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002808 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002809 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05002810
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002811 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002812 pr_err("%s: reading input file", __func__);
2813 return -1;
2814 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002815 if (session->repipe) {
2816 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002817 if (retw <= 0 || retw != padding) {
2818 pr_err("%s: repiping tracing data padding", __func__);
2819 return -1;
2820 }
Tom Zanussi454c4072010-05-01 01:41:20 -05002821 }
Tom Zanussi92155452010-04-01 23:59:21 -05002822
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03002823 if (size_read + padding != size) {
2824 pr_err("%s: tracing data size mismatch", __func__);
2825 return -1;
2826 }
Tom Zanussi92155452010-04-01 23:59:21 -05002827
Namhyung Kim831394b2012-09-06 11:10:46 +09002828 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002829 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03002830
Tom Zanussi92155452010-04-01 23:59:21 -05002831 return size_read + padding;
2832}
Tom Zanussic7929e42010-04-01 23:59:22 -05002833
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002834int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002835 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002836 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002837 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05002838{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002839 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05002840 size_t len;
2841 int err = 0;
2842
2843 if (!pos->hit)
2844 return err;
2845
2846 memset(&ev, 0, sizeof(ev));
2847
2848 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002849 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05002850 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2851 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2852 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002853 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05002854 ev.build_id.header.size = sizeof(ev.build_id) + len;
2855 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2856
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002857 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05002858
2859 return err;
2860}
2861
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002862int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002863 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002864 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05002865{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002866 __event_process_build_id(&event->build_id,
2867 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002868 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05002869 return 0;
2870}