blob: 76ed7d03e500fa246776689e85df53a89171f79a [file] [log] [blame]
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03001#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03002#include <inttypes.h>
Arnaldo Carvalho de Meloa9072bc2011-10-26 12:41:38 -02003#include "util.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -03004#include "string2.h"
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -03005#include <sys/param.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02006#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02007#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02008#include <unistd.h>
9#include <stdio.h>
10#include <stdlib.h>
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -030011#include <linux/compiler.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010012#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -020013#include <linux/kernel.h>
Robert Richterb1e5a9b2011-12-07 10:02:57 +010014#include <linux/bitops.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030015#include <sys/stat.h>
16#include <sys/types.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020017#include <sys/utsname.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030018#include <unistd.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020019
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020020#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030021#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020022#include "header.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030023#include "memswap.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020024#include "../perf.h"
25#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020026#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010027#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010028#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020029#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020030#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020031#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090032#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020033#include "build-id.h"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020034#include "data.h"
Jiri Olsa720e98b2016-02-16 16:01:43 +010035#include <api/fs/fs.h>
36#include "asm/bug.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020037
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030038#include "sane_ctype.h"
39
Stephane Eranian73323f52012-02-02 13:54:44 +010040/*
41 * magic2 = "PERFILE2"
42 * must be a numerical value to let the endianness
43 * determine the memory layout. That way we are able
44 * to detect endianness when reading the perf.data file
45 * back.
46 *
47 * we check for legacy (PERFFILE) format.
48 */
49static const char *__perf_magic1 = "PERFFILE";
50static const u64 __perf_magic2 = 0x32454c4946524550ULL;
51static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020052
Stephane Eranian73323f52012-02-02 13:54:44 +010053#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020054
Soramichi AKIYAMAd25ed5d2017-01-17 00:22:37 +090055const char perf_version_string[] = PERF_VERSION;
56
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020057struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020058 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020059 struct perf_file_section ids;
60};
61
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030062void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020063{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030064 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020065}
66
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030067void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020068{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030069 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020070}
71
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030072bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020073{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030074 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020075}
76
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020077static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020078{
79 while (size) {
80 int ret = write(fd, buf, size);
81
82 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -020083 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020084
85 size -= ret;
86 buf += ret;
87 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020088
89 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020090}
91
Namhyung Kime195fac2014-11-04 10:14:30 +090092int write_padded(int fd, const void *bf, size_t count, size_t count_aligned)
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -020093{
94 static const char zero_buf[NAME_ALIGN];
95 int err = do_write(fd, bf, count);
96
97 if (!err)
98 err = do_write(fd, zero_buf, count_aligned - count);
99
100 return err;
101}
102
Kan Liang2bb00d22015-09-01 09:58:12 -0400103#define string_size(str) \
104 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
105
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200106static int do_write_string(int fd, const char *str)
107{
108 u32 len, olen;
109 int ret;
110
111 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300112 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200113
114 /* write len, incl. \0 */
115 ret = do_write(fd, &len, sizeof(len));
116 if (ret < 0)
117 return ret;
118
119 return write_padded(fd, str, olen, len);
120}
121
122static char *do_read_string(int fd, struct perf_header *ph)
123{
124 ssize_t sz, ret;
125 u32 len;
126 char *buf;
127
Namhyung Kim5323f602012-12-17 15:38:54 +0900128 sz = readn(fd, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200129 if (sz < (ssize_t)sizeof(len))
130 return NULL;
131
132 if (ph->needs_swap)
133 len = bswap_32(len);
134
135 buf = malloc(len);
136 if (!buf)
137 return NULL;
138
Namhyung Kim5323f602012-12-17 15:38:54 +0900139 ret = readn(fd, buf, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200140 if (ret == (ssize_t)len) {
141 /*
142 * strings are padded by zeroes
143 * thus the actual strlen of buf
144 * may be less than len
145 */
146 return buf;
147 }
148
149 free(buf);
150 return NULL;
151}
152
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300153static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200154 struct perf_evlist *evlist)
155{
156 return read_tracing_data(fd, &evlist->entries);
157}
158
159
160static int write_build_id(int fd, struct perf_header *h,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300161 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200162{
163 struct perf_session *session;
164 int err;
165
166 session = container_of(h, struct perf_session, header);
167
Robert Richtere20960c2011-12-07 10:02:55 +0100168 if (!perf_session__read_build_ids(session, true))
169 return -1;
170
Namhyung Kim714c9c42014-11-04 10:14:29 +0900171 err = perf_session__write_buildid_table(session, fd);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200172 if (err < 0) {
173 pr_debug("failed to write buildid table\n");
174 return err;
175 }
Namhyung Kim73c5d222014-11-07 22:57:56 +0900176 perf_session__cache_build_ids(session);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200177
178 return 0;
179}
180
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300181static int write_hostname(int fd, struct perf_header *h __maybe_unused,
182 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200183{
184 struct utsname uts;
185 int ret;
186
187 ret = uname(&uts);
188 if (ret < 0)
189 return -1;
190
191 return do_write_string(fd, uts.nodename);
192}
193
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300194static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
195 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200196{
197 struct utsname uts;
198 int ret;
199
200 ret = uname(&uts);
201 if (ret < 0)
202 return -1;
203
204 return do_write_string(fd, uts.release);
205}
206
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300207static int write_arch(int fd, struct perf_header *h __maybe_unused,
208 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200209{
210 struct utsname uts;
211 int ret;
212
213 ret = uname(&uts);
214 if (ret < 0)
215 return -1;
216
217 return do_write_string(fd, uts.machine);
218}
219
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300220static int write_version(int fd, struct perf_header *h __maybe_unused,
221 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200222{
223 return do_write_string(fd, perf_version_string);
224}
225
Wang Nan493c3032014-10-24 09:45:26 +0800226static int __write_cpudesc(int fd, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200227{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200228 FILE *file;
229 char *buf = NULL;
230 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800231 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200232 size_t len = 0;
233 int ret = -1;
234
235 if (!search)
236 return -1;
237
238 file = fopen("/proc/cpuinfo", "r");
239 if (!file)
240 return -1;
241
242 while (getline(&buf, &len, file) > 0) {
243 ret = strncmp(buf, search, strlen(search));
244 if (!ret)
245 break;
246 }
247
Wang Naned307752014-10-16 11:08:29 +0800248 if (ret) {
249 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200250 goto done;
Wang Naned307752014-10-16 11:08:29 +0800251 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200252
253 s = buf;
254
255 p = strchr(buf, ':');
256 if (p && *(p+1) == ' ' && *(p+2))
257 s = p + 2;
258 p = strchr(s, '\n');
259 if (p)
260 *p = '\0';
261
262 /* squash extra space characters (branding string) */
263 p = s;
264 while (*p) {
265 if (isspace(*p)) {
266 char *r = p + 1;
267 char *q = r;
268 *p = ' ';
269 while (*q && isspace(*q))
270 q++;
271 if (q != (p+1))
272 while ((*r++ = *q++));
273 }
274 p++;
275 }
276 ret = do_write_string(fd, s);
277done:
278 free(buf);
279 fclose(file);
280 return ret;
281}
282
Wang Nan493c3032014-10-24 09:45:26 +0800283static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
284 struct perf_evlist *evlist __maybe_unused)
285{
286#ifndef CPUINFO_PROC
287#define CPUINFO_PROC {"model name", }
288#endif
289 const char *cpuinfo_procs[] = CPUINFO_PROC;
290 unsigned int i;
291
292 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
293 int ret;
294 ret = __write_cpudesc(fd, cpuinfo_procs[i]);
295 if (ret >= 0)
296 return ret;
297 }
298 return -1;
299}
300
301
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300302static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
303 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200304{
305 long nr;
306 u32 nrc, nra;
307 int ret;
308
Jan Stancekda8a58b2017-02-17 12:10:26 +0100309 nrc = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200310
311 nr = sysconf(_SC_NPROCESSORS_ONLN);
312 if (nr < 0)
313 return -1;
314
315 nra = (u32)(nr & UINT_MAX);
316
317 ret = do_write(fd, &nrc, sizeof(nrc));
318 if (ret < 0)
319 return ret;
320
321 return do_write(fd, &nra, sizeof(nra));
322}
323
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300324static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200325 struct perf_evlist *evlist)
326{
Robert Richter6606f872012-08-16 21:10:19 +0200327 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900328 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200329 int ret;
330
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900331 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200332
333 /*
334 * write number of events
335 */
336 ret = do_write(fd, &nre, sizeof(nre));
337 if (ret < 0)
338 return ret;
339
340 /*
341 * size of perf_event_attr struct
342 */
Robert Richter6606f872012-08-16 21:10:19 +0200343 sz = (u32)sizeof(evsel->attr);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200344 ret = do_write(fd, &sz, sizeof(sz));
345 if (ret < 0)
346 return ret;
347
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300348 evlist__for_each_entry(evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +0200349 ret = do_write(fd, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200350 if (ret < 0)
351 return ret;
352 /*
353 * write number of unique id per event
354 * there is one id per instance of an event
355 *
356 * copy into an nri to be independent of the
357 * type of ids,
358 */
Robert Richter6606f872012-08-16 21:10:19 +0200359 nri = evsel->ids;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200360 ret = do_write(fd, &nri, sizeof(nri));
361 if (ret < 0)
362 return ret;
363
364 /*
365 * write event string as passed on cmdline
366 */
Robert Richter6606f872012-08-16 21:10:19 +0200367 ret = do_write_string(fd, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200368 if (ret < 0)
369 return ret;
370 /*
371 * write unique ids for this event
372 */
Robert Richter6606f872012-08-16 21:10:19 +0200373 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200374 if (ret < 0)
375 return ret;
376 }
377 return 0;
378}
379
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300380static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
381 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200382{
383 char buf[MAXPATHLEN];
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300384 u32 n;
385 int i, ret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200386
Tommi Rantala55f771282017-03-22 15:06:24 +0200387 /* actual path to perf binary */
388 ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200389 if (ret <= 0)
390 return -1;
391
392 /* readlink() does not add null termination */
393 buf[ret] = '\0';
394
395 /* account for binary path */
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300396 n = perf_env.nr_cmdline + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200397
398 ret = do_write(fd, &n, sizeof(n));
399 if (ret < 0)
400 return ret;
401
402 ret = do_write_string(fd, buf);
403 if (ret < 0)
404 return ret;
405
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300406 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
407 ret = do_write_string(fd, perf_env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200408 if (ret < 0)
409 return ret;
410 }
411 return 0;
412}
413
414#define CORE_SIB_FMT \
415 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
416#define THRD_SIB_FMT \
417 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
418
419struct cpu_topo {
Kan Liang2bb00d22015-09-01 09:58:12 -0400420 u32 cpu_nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200421 u32 core_sib;
422 u32 thread_sib;
423 char **core_siblings;
424 char **thread_siblings;
425};
426
427static int build_cpu_topo(struct cpu_topo *tp, int cpu)
428{
429 FILE *fp;
430 char filename[MAXPATHLEN];
431 char *buf = NULL, *p;
432 size_t len = 0;
Stephane Eranianc5885742013-08-14 12:04:26 +0200433 ssize_t sret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200434 u32 i = 0;
435 int ret = -1;
436
437 sprintf(filename, CORE_SIB_FMT, cpu);
438 fp = fopen(filename, "r");
439 if (!fp)
Stephane Eranianc5885742013-08-14 12:04:26 +0200440 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200441
Stephane Eranianc5885742013-08-14 12:04:26 +0200442 sret = getline(&buf, &len, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200443 fclose(fp);
Stephane Eranianc5885742013-08-14 12:04:26 +0200444 if (sret <= 0)
445 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200446
447 p = strchr(buf, '\n');
448 if (p)
449 *p = '\0';
450
451 for (i = 0; i < tp->core_sib; i++) {
452 if (!strcmp(buf, tp->core_siblings[i]))
453 break;
454 }
455 if (i == tp->core_sib) {
456 tp->core_siblings[i] = buf;
457 tp->core_sib++;
458 buf = NULL;
459 len = 0;
460 }
Stephane Eranianc5885742013-08-14 12:04:26 +0200461 ret = 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200462
Stephane Eranianc5885742013-08-14 12:04:26 +0200463try_threads:
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200464 sprintf(filename, THRD_SIB_FMT, cpu);
465 fp = fopen(filename, "r");
466 if (!fp)
467 goto done;
468
469 if (getline(&buf, &len, fp) <= 0)
470 goto done;
471
472 p = strchr(buf, '\n');
473 if (p)
474 *p = '\0';
475
476 for (i = 0; i < tp->thread_sib; i++) {
477 if (!strcmp(buf, tp->thread_siblings[i]))
478 break;
479 }
480 if (i == tp->thread_sib) {
481 tp->thread_siblings[i] = buf;
482 tp->thread_sib++;
483 buf = NULL;
484 }
485 ret = 0;
486done:
487 if(fp)
488 fclose(fp);
489 free(buf);
490 return ret;
491}
492
493static void free_cpu_topo(struct cpu_topo *tp)
494{
495 u32 i;
496
497 if (!tp)
498 return;
499
500 for (i = 0 ; i < tp->core_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300501 zfree(&tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200502
503 for (i = 0 ; i < tp->thread_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300504 zfree(&tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200505
506 free(tp);
507}
508
509static struct cpu_topo *build_cpu_topology(void)
510{
Jan Stancek43db2842017-02-17 12:10:25 +0100511 struct cpu_topo *tp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200512 void *addr;
513 u32 nr, i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300514 size_t sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200515 long ncpus;
516 int ret = -1;
Jan Stancek43db2842017-02-17 12:10:25 +0100517 struct cpu_map *map;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200518
Jan Stancekda8a58b2017-02-17 12:10:26 +0100519 ncpus = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200520
Jan Stancek43db2842017-02-17 12:10:25 +0100521 /* build online CPU map */
522 map = cpu_map__new(NULL);
523 if (map == NULL) {
524 pr_debug("failed to get system cpumap\n");
525 return NULL;
526 }
527
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200528 nr = (u32)(ncpus & UINT_MAX);
529
530 sz = nr * sizeof(char *);
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300531 addr = calloc(1, sizeof(*tp) + 2 * sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200532 if (!addr)
Jan Stancek43db2842017-02-17 12:10:25 +0100533 goto out_free;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200534
535 tp = addr;
Kan Liang2bb00d22015-09-01 09:58:12 -0400536 tp->cpu_nr = nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200537 addr += sizeof(*tp);
538 tp->core_siblings = addr;
539 addr += sz;
540 tp->thread_siblings = addr;
541
542 for (i = 0; i < nr; i++) {
Jan Stancek43db2842017-02-17 12:10:25 +0100543 if (!cpu_map__has(map, i))
544 continue;
545
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200546 ret = build_cpu_topo(tp, i);
547 if (ret < 0)
548 break;
549 }
Jan Stancek43db2842017-02-17 12:10:25 +0100550
551out_free:
552 cpu_map__put(map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200553 if (ret) {
554 free_cpu_topo(tp);
555 tp = NULL;
556 }
557 return tp;
558}
559
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300560static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
561 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200562{
563 struct cpu_topo *tp;
564 u32 i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300565 int ret, j;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200566
567 tp = build_cpu_topology();
568 if (!tp)
569 return -1;
570
571 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
572 if (ret < 0)
573 goto done;
574
575 for (i = 0; i < tp->core_sib; i++) {
576 ret = do_write_string(fd, tp->core_siblings[i]);
577 if (ret < 0)
578 goto done;
579 }
580 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
581 if (ret < 0)
582 goto done;
583
584 for (i = 0; i < tp->thread_sib; i++) {
585 ret = do_write_string(fd, tp->thread_siblings[i]);
586 if (ret < 0)
587 break;
588 }
Kan Liang2bb00d22015-09-01 09:58:12 -0400589
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300590 ret = perf_env__read_cpu_topology_map(&perf_env);
591 if (ret < 0)
592 goto done;
593
594 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
595 ret = do_write(fd, &perf_env.cpu[j].core_id,
596 sizeof(perf_env.cpu[j].core_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400597 if (ret < 0)
598 return ret;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300599 ret = do_write(fd, &perf_env.cpu[j].socket_id,
600 sizeof(perf_env.cpu[j].socket_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400601 if (ret < 0)
602 return ret;
603 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200604done:
605 free_cpu_topo(tp);
606 return ret;
607}
608
609
610
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300611static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
612 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200613{
614 char *buf = NULL;
615 FILE *fp;
616 size_t len = 0;
617 int ret = -1, n;
618 uint64_t mem;
619
620 fp = fopen("/proc/meminfo", "r");
621 if (!fp)
622 return -1;
623
624 while (getline(&buf, &len, fp) > 0) {
625 ret = strncmp(buf, "MemTotal:", 9);
626 if (!ret)
627 break;
628 }
629 if (!ret) {
630 n = sscanf(buf, "%*s %"PRIu64, &mem);
631 if (n == 1)
632 ret = do_write(fd, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800633 } else
634 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200635 free(buf);
636 fclose(fp);
637 return ret;
638}
639
640static int write_topo_node(int fd, int node)
641{
642 char str[MAXPATHLEN];
643 char field[32];
644 char *buf = NULL, *p;
645 size_t len = 0;
646 FILE *fp;
647 u64 mem_total, mem_free, mem;
648 int ret = -1;
649
650 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
651 fp = fopen(str, "r");
652 if (!fp)
653 return -1;
654
655 while (getline(&buf, &len, fp) > 0) {
656 /* skip over invalid lines */
657 if (!strchr(buf, ':'))
658 continue;
Alan Coxa761a2d2014-01-20 19:10:11 +0100659 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200660 goto done;
661 if (!strcmp(field, "MemTotal:"))
662 mem_total = mem;
663 if (!strcmp(field, "MemFree:"))
664 mem_free = mem;
665 }
666
667 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100668 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200669
670 ret = do_write(fd, &mem_total, sizeof(u64));
671 if (ret)
672 goto done;
673
674 ret = do_write(fd, &mem_free, sizeof(u64));
675 if (ret)
676 goto done;
677
678 ret = -1;
679 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
680
681 fp = fopen(str, "r");
682 if (!fp)
683 goto done;
684
685 if (getline(&buf, &len, fp) <= 0)
686 goto done;
687
688 p = strchr(buf, '\n');
689 if (p)
690 *p = '\0';
691
692 ret = do_write_string(fd, buf);
693done:
694 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100695 if (fp)
696 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200697 return ret;
698}
699
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300700static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
701 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200702{
703 char *buf = NULL;
704 size_t len = 0;
705 FILE *fp;
706 struct cpu_map *node_map = NULL;
707 char *c;
708 u32 nr, i, j;
709 int ret = -1;
710
711 fp = fopen("/sys/devices/system/node/online", "r");
712 if (!fp)
713 return -1;
714
715 if (getline(&buf, &len, fp) <= 0)
716 goto done;
717
718 c = strchr(buf, '\n');
719 if (c)
720 *c = '\0';
721
722 node_map = cpu_map__new(buf);
723 if (!node_map)
724 goto done;
725
726 nr = (u32)node_map->nr;
727
728 ret = do_write(fd, &nr, sizeof(nr));
729 if (ret < 0)
730 goto done;
731
732 for (i = 0; i < nr; i++) {
733 j = (u32)node_map->map[i];
734 ret = do_write(fd, &j, sizeof(j));
735 if (ret < 0)
736 break;
737
738 ret = write_topo_node(fd, i);
739 if (ret < 0)
740 break;
741 }
742done:
743 free(buf);
744 fclose(fp);
Masami Hiramatsu5191d8872015-12-09 11:11:35 +0900745 cpu_map__put(node_map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200746 return ret;
747}
748
749/*
Robert Richter50a96672012-08-16 21:10:24 +0200750 * File format:
751 *
752 * struct pmu_mappings {
753 * u32 pmu_num;
754 * struct pmu_map {
755 * u32 type;
756 * char name[];
757 * }[pmu_num];
758 * };
759 */
760
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300761static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
762 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +0200763{
764 struct perf_pmu *pmu = NULL;
765 off_t offset = lseek(fd, 0, SEEK_CUR);
766 __u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900767 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200768
769 /* write real pmu_num later */
Namhyung Kim5323f602012-12-17 15:38:54 +0900770 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
771 if (ret < 0)
772 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200773
774 while ((pmu = perf_pmu__scan(pmu))) {
775 if (!pmu->name)
776 continue;
777 pmu_num++;
Namhyung Kim5323f602012-12-17 15:38:54 +0900778
779 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
780 if (ret < 0)
781 return ret;
782
783 ret = do_write_string(fd, pmu->name);
784 if (ret < 0)
785 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200786 }
787
788 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
789 /* discard all */
790 lseek(fd, offset, SEEK_SET);
791 return -1;
792 }
793
794 return 0;
795}
796
797/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900798 * File format:
799 *
800 * struct group_descs {
801 * u32 nr_groups;
802 * struct group_desc {
803 * char name[];
804 * u32 leader_idx;
805 * u32 nr_members;
806 * }[nr_groups];
807 * };
808 */
809static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
810 struct perf_evlist *evlist)
811{
812 u32 nr_groups = evlist->nr_groups;
813 struct perf_evsel *evsel;
814 int ret;
815
816 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
817 if (ret < 0)
818 return ret;
819
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300820 evlist__for_each_entry(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900821 if (perf_evsel__is_group_leader(evsel) &&
822 evsel->nr_members > 1) {
823 const char *name = evsel->group_name ?: "{anon_group}";
824 u32 leader_idx = evsel->idx;
825 u32 nr_members = evsel->nr_members;
826
827 ret = do_write_string(fd, name);
828 if (ret < 0)
829 return ret;
830
831 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
832 if (ret < 0)
833 return ret;
834
835 ret = do_write(fd, &nr_members, sizeof(nr_members));
836 if (ret < 0)
837 return ret;
838 }
839 }
840 return 0;
841}
842
843/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200844 * default get_cpuid(): nothing gets recorded
Jiada Wang7a759cd2017-04-09 20:02:37 -0700845 * actual implementation must be in arch/$(SRCARCH)/util/header.c
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200846 */
Rui Teng11d8f872016-07-28 10:05:57 +0800847int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200848{
849 return -1;
850}
851
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300852static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
853 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200854{
855 char buffer[64];
856 int ret;
857
858 ret = get_cpuid(buffer, sizeof(buffer));
859 if (!ret)
860 goto write_it;
861
862 return -1;
863write_it:
864 return do_write_string(fd, buffer);
865}
866
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300867static int write_branch_stack(int fd __maybe_unused,
868 struct perf_header *h __maybe_unused,
869 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +0100870{
871 return 0;
872}
873
Adrian Hunter99fa2982015-04-30 17:37:25 +0300874static int write_auxtrace(int fd, struct perf_header *h,
Adrian Hunter4025ea42015-04-09 18:53:41 +0300875 struct perf_evlist *evlist __maybe_unused)
876{
Adrian Hunter99fa2982015-04-30 17:37:25 +0300877 struct perf_session *session;
878 int err;
879
880 session = container_of(h, struct perf_session, header);
881
882 err = auxtrace_index__write(fd, &session->auxtrace_index);
883 if (err < 0)
884 pr_err("Failed to write auxtrace index\n");
885 return err;
Adrian Hunter4025ea42015-04-09 18:53:41 +0300886}
887
Jiri Olsa720e98b2016-02-16 16:01:43 +0100888static int cpu_cache_level__sort(const void *a, const void *b)
889{
890 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
891 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
892
893 return cache_a->level - cache_b->level;
894}
895
896static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
897{
898 if (a->level != b->level)
899 return false;
900
901 if (a->line_size != b->line_size)
902 return false;
903
904 if (a->sets != b->sets)
905 return false;
906
907 if (a->ways != b->ways)
908 return false;
909
910 if (strcmp(a->type, b->type))
911 return false;
912
913 if (strcmp(a->size, b->size))
914 return false;
915
916 if (strcmp(a->map, b->map))
917 return false;
918
919 return true;
920}
921
922static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
923{
924 char path[PATH_MAX], file[PATH_MAX];
925 struct stat st;
926 size_t len;
927
928 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
929 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
930
931 if (stat(file, &st))
932 return 1;
933
934 scnprintf(file, PATH_MAX, "%s/level", path);
935 if (sysfs__read_int(file, (int *) &cache->level))
936 return -1;
937
938 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
939 if (sysfs__read_int(file, (int *) &cache->line_size))
940 return -1;
941
942 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
943 if (sysfs__read_int(file, (int *) &cache->sets))
944 return -1;
945
946 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
947 if (sysfs__read_int(file, (int *) &cache->ways))
948 return -1;
949
950 scnprintf(file, PATH_MAX, "%s/type", path);
951 if (sysfs__read_str(file, &cache->type, &len))
952 return -1;
953
954 cache->type[len] = 0;
955 cache->type = rtrim(cache->type);
956
957 scnprintf(file, PATH_MAX, "%s/size", path);
958 if (sysfs__read_str(file, &cache->size, &len)) {
959 free(cache->type);
960 return -1;
961 }
962
963 cache->size[len] = 0;
964 cache->size = rtrim(cache->size);
965
966 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
967 if (sysfs__read_str(file, &cache->map, &len)) {
968 free(cache->map);
969 free(cache->type);
970 return -1;
971 }
972
973 cache->map[len] = 0;
974 cache->map = rtrim(cache->map);
975 return 0;
976}
977
978static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
979{
980 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
981}
982
983static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
984{
985 u32 i, cnt = 0;
986 long ncpus;
987 u32 nr, cpu;
988 u16 level;
989
990 ncpus = sysconf(_SC_NPROCESSORS_CONF);
991 if (ncpus < 0)
992 return -1;
993
994 nr = (u32)(ncpus & UINT_MAX);
995
996 for (cpu = 0; cpu < nr; cpu++) {
997 for (level = 0; level < 10; level++) {
998 struct cpu_cache_level c;
999 int err;
1000
1001 err = cpu_cache_level__read(&c, cpu, level);
1002 if (err < 0)
1003 return err;
1004
1005 if (err == 1)
1006 break;
1007
1008 for (i = 0; i < cnt; i++) {
1009 if (cpu_cache_level__cmp(&c, &caches[i]))
1010 break;
1011 }
1012
1013 if (i == cnt)
1014 caches[cnt++] = c;
1015 else
1016 cpu_cache_level__free(&c);
1017
1018 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1019 goto out;
1020 }
1021 }
1022 out:
1023 *cntp = cnt;
1024 return 0;
1025}
1026
1027#define MAX_CACHES 2000
1028
1029static int write_cache(int fd, struct perf_header *h __maybe_unused,
1030 struct perf_evlist *evlist __maybe_unused)
1031{
1032 struct cpu_cache_level caches[MAX_CACHES];
1033 u32 cnt = 0, i, version = 1;
1034 int ret;
1035
1036 ret = build_caches(caches, MAX_CACHES, &cnt);
1037 if (ret)
1038 goto out;
1039
1040 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1041
1042 ret = do_write(fd, &version, sizeof(u32));
1043 if (ret < 0)
1044 goto out;
1045
1046 ret = do_write(fd, &cnt, sizeof(u32));
1047 if (ret < 0)
1048 goto out;
1049
1050 for (i = 0; i < cnt; i++) {
1051 struct cpu_cache_level *c = &caches[i];
1052
1053 #define _W(v) \
1054 ret = do_write(fd, &c->v, sizeof(u32)); \
1055 if (ret < 0) \
1056 goto out;
1057
1058 _W(level)
1059 _W(line_size)
1060 _W(sets)
1061 _W(ways)
1062 #undef _W
1063
1064 #define _W(v) \
1065 ret = do_write_string(fd, (const char *) c->v); \
1066 if (ret < 0) \
1067 goto out;
1068
1069 _W(type)
1070 _W(size)
1071 _W(map)
1072 #undef _W
1073 }
1074
1075out:
1076 for (i = 0; i < cnt; i++)
1077 cpu_cache_level__free(&caches[i]);
1078 return ret;
1079}
1080
Jiri Olsaffa517a2015-10-25 15:51:43 +01001081static int write_stat(int fd __maybe_unused,
1082 struct perf_header *h __maybe_unused,
1083 struct perf_evlist *evlist __maybe_unused)
1084{
1085 return 0;
1086}
1087
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001088static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1089 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001090{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001091 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001092}
1093
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001094static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1095 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001096{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001097 fprintf(fp, "# os release : %s\n", ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001098}
1099
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001100static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001101{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001102 fprintf(fp, "# arch : %s\n", ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001103}
1104
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001105static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1106 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001107{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001108 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001109}
1110
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001111static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1112 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001113{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001114 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1115 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001116}
1117
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001118static void print_version(struct perf_header *ph, int fd __maybe_unused,
1119 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001120{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001121 fprintf(fp, "# perf version : %s\n", ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001122}
1123
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001124static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1125 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001126{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001127 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001128
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001129 nr = ph->env.nr_cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001130
1131 fprintf(fp, "# cmdline : ");
1132
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001133 for (i = 0; i < nr; i++)
1134 fprintf(fp, "%s ", ph->env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001135 fputc('\n', fp);
1136}
1137
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001138static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1139 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001140{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001141 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001142 char *str;
Jan Stancekda8a58b2017-02-17 12:10:26 +01001143 int cpu_nr = ph->env.nr_cpus_avail;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001144
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001145 nr = ph->env.nr_sibling_cores;
1146 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001147
1148 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001149 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001150 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001151 }
1152
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001153 nr = ph->env.nr_sibling_threads;
1154 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001155
1156 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001157 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001158 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001159 }
Kan Liang2bb00d22015-09-01 09:58:12 -04001160
1161 if (ph->env.cpu != NULL) {
1162 for (i = 0; i < cpu_nr; i++)
1163 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1164 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1165 } else
1166 fprintf(fp, "# Core ID and Socket ID information is not available\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001167}
1168
Robert Richter4e1b9c62012-08-16 21:10:22 +02001169static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001170{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001171 struct perf_evsel *evsel;
1172
1173 if (!events)
1174 return;
1175
1176 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001177 zfree(&evsel->name);
1178 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001179 }
1180
1181 free(events);
1182}
1183
1184static struct perf_evsel *
1185read_event_desc(struct perf_header *ph, int fd)
1186{
1187 struct perf_evsel *evsel, *events = NULL;
1188 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001189 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001190 u32 nre, sz, nr, i, j;
1191 ssize_t ret;
1192 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001193
1194 /* number of events */
Namhyung Kim5323f602012-12-17 15:38:54 +09001195 ret = readn(fd, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001196 if (ret != (ssize_t)sizeof(nre))
1197 goto error;
1198
1199 if (ph->needs_swap)
1200 nre = bswap_32(nre);
1201
Namhyung Kim5323f602012-12-17 15:38:54 +09001202 ret = readn(fd, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001203 if (ret != (ssize_t)sizeof(sz))
1204 goto error;
1205
1206 if (ph->needs_swap)
1207 sz = bswap_32(sz);
1208
Stephane Eranian62db9062012-02-09 23:21:07 +01001209 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001210 buf = malloc(sz);
1211 if (!buf)
1212 goto error;
1213
Robert Richter4e1b9c62012-08-16 21:10:22 +02001214 /* the last event terminates with evsel->attr.size == 0: */
1215 events = calloc(nre + 1, sizeof(*events));
1216 if (!events)
1217 goto error;
1218
1219 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001220 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001221 msz = sz;
1222
Robert Richter4e1b9c62012-08-16 21:10:22 +02001223 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1224 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001225
Stephane Eranian62db9062012-02-09 23:21:07 +01001226 /*
1227 * must read entire on-file attr struct to
1228 * sync up with layout.
1229 */
Namhyung Kim5323f602012-12-17 15:38:54 +09001230 ret = readn(fd, buf, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001231 if (ret != (ssize_t)sz)
1232 goto error;
1233
1234 if (ph->needs_swap)
1235 perf_event__attr_swap(buf);
1236
Robert Richter4e1b9c62012-08-16 21:10:22 +02001237 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001238
Namhyung Kim5323f602012-12-17 15:38:54 +09001239 ret = readn(fd, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001240 if (ret != (ssize_t)sizeof(nr))
1241 goto error;
1242
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001243 if (ph->needs_swap) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001244 nr = bswap_32(nr);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001245 evsel->needs_swap = true;
1246 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001247
Robert Richter4e1b9c62012-08-16 21:10:22 +02001248 evsel->name = do_read_string(fd, ph);
1249
1250 if (!nr)
1251 continue;
1252
1253 id = calloc(nr, sizeof(*id));
1254 if (!id)
1255 goto error;
1256 evsel->ids = nr;
1257 evsel->id = id;
1258
1259 for (j = 0 ; j < nr; j++) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001260 ret = readn(fd, id, sizeof(*id));
Robert Richter4e1b9c62012-08-16 21:10:22 +02001261 if (ret != (ssize_t)sizeof(*id))
1262 goto error;
1263 if (ph->needs_swap)
1264 *id = bswap_64(*id);
1265 id++;
1266 }
1267 }
1268out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001269 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001270 return events;
1271error:
Markus Elfring4cc97612015-06-25 17:12:32 +02001272 free_event_desc(events);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001273 events = NULL;
1274 goto out;
1275}
1276
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001277static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -03001278 void *priv __maybe_unused)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001279{
1280 return fprintf(fp, ", %s = %s", name, val);
1281}
1282
Robert Richter4e1b9c62012-08-16 21:10:22 +02001283static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1284{
1285 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1286 u32 j;
1287 u64 *id;
1288
1289 if (!events) {
1290 fprintf(fp, "# event desc: not available or unable to read\n");
1291 return;
1292 }
1293
1294 for (evsel = events; evsel->attr.size; evsel++) {
1295 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001296
Robert Richter4e1b9c62012-08-16 21:10:22 +02001297 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001298 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001299 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1300 if (j)
1301 fputc(',', fp);
1302 fprintf(fp, " %"PRIu64, *id);
1303 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001304 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001305 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001306
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001307 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001308
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001309 fputc('\n', fp);
1310 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001311
1312 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001313}
1314
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001315static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001316 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001317{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001318 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001319}
1320
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001321static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001322 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001323{
Jiri Olsac60da222016-07-04 14:16:20 +02001324 int i;
1325 struct numa_node *n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001326
Jiri Olsac60da222016-07-04 14:16:20 +02001327 for (i = 0; i < ph->env.nr_numa_nodes; i++) {
1328 n = &ph->env.numa_nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001329
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001330 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1331 " free = %"PRIu64" kB\n",
Jiri Olsac60da222016-07-04 14:16:20 +02001332 n->node, n->mem_total, n->mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001333
Jiri Olsac60da222016-07-04 14:16:20 +02001334 fprintf(fp, "# node%u cpu list : ", n->node);
1335 cpu_map__fprintf(n->map, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001336 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001337}
1338
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001339static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001340{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001341 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001342}
1343
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001344static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001345 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001346{
1347 fprintf(fp, "# contains samples with branch stack\n");
1348}
1349
Adrian Hunter4025ea42015-04-09 18:53:41 +03001350static void print_auxtrace(struct perf_header *ph __maybe_unused,
1351 int fd __maybe_unused, FILE *fp)
1352{
1353 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1354}
1355
Jiri Olsaffa517a2015-10-25 15:51:43 +01001356static void print_stat(struct perf_header *ph __maybe_unused,
1357 int fd __maybe_unused, FILE *fp)
1358{
1359 fprintf(fp, "# contains stat data\n");
1360}
1361
Jiri Olsa720e98b2016-02-16 16:01:43 +01001362static void print_cache(struct perf_header *ph __maybe_unused,
1363 int fd __maybe_unused, FILE *fp __maybe_unused)
1364{
1365 int i;
1366
1367 fprintf(fp, "# CPU cache info:\n");
1368 for (i = 0; i < ph->env.caches_cnt; i++) {
1369 fprintf(fp, "# ");
1370 cpu_cache_level__fprintf(fp, &ph->env.caches[i]);
1371 }
1372}
1373
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001374static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1375 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001376{
1377 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001378 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001379 u32 pmu_num;
1380 u32 type;
1381
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001382 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001383 if (!pmu_num) {
1384 fprintf(fp, "# pmu mappings: not available\n");
1385 return;
1386 }
1387
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001388 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001389
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001390 while (pmu_num) {
1391 type = strtoul(str, &tmp, 0);
1392 if (*tmp != ':')
1393 goto error;
1394
1395 str = tmp + 1;
1396 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1397
Robert Richter50a96672012-08-16 21:10:24 +02001398 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001399 str += strlen(str) + 1;
1400 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001401 }
1402
1403 fprintf(fp, "\n");
1404
1405 if (!pmu_num)
1406 return;
1407error:
1408 fprintf(fp, "# pmu mappings: unable to read\n");
1409}
1410
Namhyung Kima8bb5592013-01-22 18:09:31 +09001411static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1412 FILE *fp)
1413{
1414 struct perf_session *session;
1415 struct perf_evsel *evsel;
1416 u32 nr = 0;
1417
1418 session = container_of(ph, struct perf_session, header);
1419
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001420 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001421 if (perf_evsel__is_group_leader(evsel) &&
1422 evsel->nr_members > 1) {
1423 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1424 perf_evsel__name(evsel));
1425
1426 nr = evsel->nr_members - 1;
1427 } else if (nr) {
1428 fprintf(fp, ",%s", perf_evsel__name(evsel));
1429
1430 if (--nr == 0)
1431 fprintf(fp, "}\n");
1432 }
1433 }
1434}
1435
Robert Richter08d95bd2012-02-10 15:41:55 +01001436static int __event_process_build_id(struct build_id_event *bev,
1437 char *filename,
1438 struct perf_session *session)
1439{
1440 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01001441 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00001442 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01001443 struct dso *dso;
1444 enum dso_kernel_type dso_type;
1445
1446 machine = perf_session__findnew_machine(session, bev->pid);
1447 if (!machine)
1448 goto out;
1449
Wang Nan1f121b02015-06-03 08:52:21 +00001450 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01001451
Wang Nan1f121b02015-06-03 08:52:21 +00001452 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01001453 case PERF_RECORD_MISC_KERNEL:
1454 dso_type = DSO_TYPE_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001455 break;
1456 case PERF_RECORD_MISC_GUEST_KERNEL:
1457 dso_type = DSO_TYPE_GUEST_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001458 break;
1459 case PERF_RECORD_MISC_USER:
1460 case PERF_RECORD_MISC_GUEST_USER:
1461 dso_type = DSO_TYPE_USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01001462 break;
1463 default:
1464 goto out;
1465 }
1466
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001467 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001468 if (dso != NULL) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001469 char sbuild_id[SBUILD_ID_SIZE];
Robert Richter08d95bd2012-02-10 15:41:55 +01001470
1471 dso__set_build_id(dso, &bev->build_id);
1472
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001473 if (dso_type != DSO_TYPE_USER) {
1474 struct kmod_path m = { .name = NULL, };
1475
1476 if (!kmod_path__parse_name(&m, filename) && m.kmod)
Namhyung Kim6b335e82017-05-31 21:01:04 +09001477 dso__set_module_info(dso, &m, machine);
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001478 else
1479 dso->kernel = dso_type;
1480
1481 free(m.name);
1482 }
Robert Richter08d95bd2012-02-10 15:41:55 +01001483
1484 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1485 sbuild_id);
1486 pr_debug("build id event received for %s: %s\n",
1487 dso->long_name, sbuild_id);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001488 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01001489 }
1490
1491 err = 0;
1492out:
1493 return err;
1494}
1495
1496static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1497 int input, u64 offset, u64 size)
1498{
1499 struct perf_session *session = container_of(header, struct perf_session, header);
1500 struct {
1501 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001502 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001503 char filename[0];
1504 } old_bev;
1505 struct build_id_event bev;
1506 char filename[PATH_MAX];
1507 u64 limit = offset + size;
1508
1509 while (offset < limit) {
1510 ssize_t len;
1511
Namhyung Kim5323f602012-12-17 15:38:54 +09001512 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001513 return -1;
1514
1515 if (header->needs_swap)
1516 perf_event_header__bswap(&old_bev.header);
1517
1518 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001519 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001520 return -1;
1521
1522 bev.header = old_bev.header;
1523
1524 /*
1525 * As the pid is the missing value, we need to fill
1526 * it properly. The header.misc value give us nice hint.
1527 */
1528 bev.pid = HOST_KERNEL_ID;
1529 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1530 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1531 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1532
1533 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1534 __event_process_build_id(&bev, filename, session);
1535
1536 offset += bev.header.size;
1537 }
1538
1539 return 0;
1540}
1541
1542static int perf_header__read_build_ids(struct perf_header *header,
1543 int input, u64 offset, u64 size)
1544{
1545 struct perf_session *session = container_of(header, struct perf_session, header);
1546 struct build_id_event bev;
1547 char filename[PATH_MAX];
1548 u64 limit = offset + size, orig_offset = offset;
1549 int err = -1;
1550
1551 while (offset < limit) {
1552 ssize_t len;
1553
Namhyung Kim5323f602012-12-17 15:38:54 +09001554 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001555 goto out;
1556
1557 if (header->needs_swap)
1558 perf_event_header__bswap(&bev.header);
1559
1560 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001561 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001562 goto out;
1563 /*
1564 * The a1645ce1 changeset:
1565 *
1566 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1567 *
1568 * Added a field to struct build_id_event that broke the file
1569 * format.
1570 *
1571 * Since the kernel build-id is the first entry, process the
1572 * table using the old format if the well known
1573 * '[kernel.kallsyms]' string for the kernel build-id has the
1574 * first 4 characters chopped off (where the pid_t sits).
1575 */
1576 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1577 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1578 return -1;
1579 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1580 }
1581
1582 __event_process_build_id(&bev, filename, session);
1583
1584 offset += bev.header.size;
1585 }
1586 err = 0;
1587out:
1588 return err;
1589}
1590
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001591static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1592 struct perf_header *ph __maybe_unused,
1593 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001594{
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001595 ssize_t ret = trace_report(fd, data, false);
1596 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001597}
1598
1599static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001600 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001601 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001602{
1603 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1604 pr_debug("Failed to read buildids, continuing...\n");
1605 return 0;
1606}
1607
Namhyung Kima1ae5652012-09-24 17:14:59 +09001608static int process_hostname(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001609 struct perf_header *ph, int fd,
1610 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001611{
1612 ph->env.hostname = do_read_string(fd, ph);
1613 return ph->env.hostname ? 0 : -ENOMEM;
1614}
1615
1616static int process_osrelease(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001617 struct perf_header *ph, int fd,
1618 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001619{
1620 ph->env.os_release = do_read_string(fd, ph);
1621 return ph->env.os_release ? 0 : -ENOMEM;
1622}
1623
1624static int process_version(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001625 struct perf_header *ph, int fd,
1626 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001627{
1628 ph->env.version = do_read_string(fd, ph);
1629 return ph->env.version ? 0 : -ENOMEM;
1630}
1631
1632static int process_arch(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001633 struct perf_header *ph, int fd,
1634 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001635{
1636 ph->env.arch = do_read_string(fd, ph);
1637 return ph->env.arch ? 0 : -ENOMEM;
1638}
1639
1640static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001641 struct perf_header *ph, int fd,
1642 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001643{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001644 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001645 u32 nr;
1646
Namhyung Kim5323f602012-12-17 15:38:54 +09001647 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001648 if (ret != sizeof(nr))
1649 return -1;
1650
1651 if (ph->needs_swap)
1652 nr = bswap_32(nr);
1653
Arnaldo Carvalho de Melocaa47042015-09-11 12:36:12 -03001654 ph->env.nr_cpus_avail = nr;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001655
Namhyung Kim5323f602012-12-17 15:38:54 +09001656 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001657 if (ret != sizeof(nr))
1658 return -1;
1659
1660 if (ph->needs_swap)
1661 nr = bswap_32(nr);
1662
Arnaldo Carvalho de Melocaa47042015-09-11 12:36:12 -03001663 ph->env.nr_cpus_online = nr;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001664 return 0;
1665}
1666
1667static int process_cpudesc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001668 struct perf_header *ph, int fd,
1669 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001670{
1671 ph->env.cpu_desc = do_read_string(fd, ph);
1672 return ph->env.cpu_desc ? 0 : -ENOMEM;
1673}
1674
1675static int process_cpuid(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001676 struct perf_header *ph, int fd,
1677 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001678{
1679 ph->env.cpuid = do_read_string(fd, ph);
1680 return ph->env.cpuid ? 0 : -ENOMEM;
1681}
1682
1683static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001684 struct perf_header *ph, int fd,
1685 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001686{
1687 uint64_t mem;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001688 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001689
Namhyung Kim5323f602012-12-17 15:38:54 +09001690 ret = readn(fd, &mem, sizeof(mem));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001691 if (ret != sizeof(mem))
1692 return -1;
1693
1694 if (ph->needs_swap)
1695 mem = bswap_64(mem);
1696
1697 ph->env.total_mem = mem;
1698 return 0;
1699}
1700
Robert Richter7c2f7af2012-08-16 21:10:23 +02001701static struct perf_evsel *
1702perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1703{
1704 struct perf_evsel *evsel;
1705
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001706 evlist__for_each_entry(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001707 if (evsel->idx == idx)
1708 return evsel;
1709 }
1710
1711 return NULL;
1712}
1713
1714static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001715perf_evlist__set_event_name(struct perf_evlist *evlist,
1716 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001717{
1718 struct perf_evsel *evsel;
1719
1720 if (!event->name)
1721 return;
1722
1723 evsel = perf_evlist__find_by_index(evlist, event->idx);
1724 if (!evsel)
1725 return;
1726
1727 if (evsel->name)
1728 return;
1729
1730 evsel->name = strdup(event->name);
1731}
1732
1733static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001734process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001735 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001736 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001737{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001738 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001739 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1740
1741 if (!events)
1742 return 0;
1743
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001744 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001745 for (evsel = events; evsel->attr.size; evsel++)
1746 perf_evlist__set_event_name(session->evlist, evsel);
1747
1748 free_event_desc(events);
1749
1750 return 0;
1751}
1752
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001753static int process_cmdline(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001754 struct perf_header *ph, int fd,
1755 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001756{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001757 ssize_t ret;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001758 char *str, *cmdline = NULL, **argv = NULL;
1759 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001760
Namhyung Kim5323f602012-12-17 15:38:54 +09001761 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001762 if (ret != sizeof(nr))
1763 return -1;
1764
1765 if (ph->needs_swap)
1766 nr = bswap_32(nr);
1767
1768 ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001769
1770 cmdline = zalloc(section->size + nr + 1);
1771 if (!cmdline)
1772 return -1;
1773
1774 argv = zalloc(sizeof(char *) * (nr + 1));
1775 if (!argv)
1776 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001777
1778 for (i = 0; i < nr; i++) {
1779 str = do_read_string(fd, ph);
1780 if (!str)
1781 goto error;
1782
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001783 argv[i] = cmdline + len;
1784 memcpy(argv[i], str, strlen(str) + 1);
1785 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001786 free(str);
1787 }
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001788 ph->env.cmdline = cmdline;
1789 ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001790 return 0;
1791
1792error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001793 free(argv);
1794 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001795 return -1;
1796}
1797
Kan Liang2bb00d22015-09-01 09:58:12 -04001798static int process_cpu_topology(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001799 struct perf_header *ph, int fd,
1800 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001801{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001802 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001803 u32 nr, i;
1804 char *str;
1805 struct strbuf sb;
Jan Stancekda8a58b2017-02-17 12:10:26 +01001806 int cpu_nr = ph->env.nr_cpus_avail;
Kan Liang2bb00d22015-09-01 09:58:12 -04001807 u64 size = 0;
1808
1809 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1810 if (!ph->env.cpu)
1811 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001812
Namhyung Kim5323f602012-12-17 15:38:54 +09001813 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001814 if (ret != sizeof(nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001815 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001816
1817 if (ph->needs_swap)
1818 nr = bswap_32(nr);
1819
1820 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001821 size += sizeof(u32);
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001822 if (strbuf_init(&sb, 128) < 0)
1823 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001824
1825 for (i = 0; i < nr; i++) {
1826 str = do_read_string(fd, ph);
1827 if (!str)
1828 goto error;
1829
1830 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001831 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1832 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001833 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001834 free(str);
1835 }
1836 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1837
Namhyung Kim5323f602012-12-17 15:38:54 +09001838 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001839 if (ret != sizeof(nr))
1840 return -1;
1841
1842 if (ph->needs_swap)
1843 nr = bswap_32(nr);
1844
1845 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001846 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001847
1848 for (i = 0; i < nr; i++) {
1849 str = do_read_string(fd, ph);
1850 if (!str)
1851 goto error;
1852
1853 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001854 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1855 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001856 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001857 free(str);
1858 }
1859 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04001860
1861 /*
1862 * The header may be from old perf,
1863 * which doesn't include core id and socket id information.
1864 */
1865 if (section->size <= size) {
1866 zfree(&ph->env.cpu);
1867 return 0;
1868 }
1869
1870 for (i = 0; i < (u32)cpu_nr; i++) {
1871 ret = readn(fd, &nr, sizeof(nr));
1872 if (ret != sizeof(nr))
1873 goto free_cpu;
1874
1875 if (ph->needs_swap)
1876 nr = bswap_32(nr);
1877
Kan Liang2bb00d22015-09-01 09:58:12 -04001878 ph->env.cpu[i].core_id = nr;
1879
1880 ret = readn(fd, &nr, sizeof(nr));
1881 if (ret != sizeof(nr))
1882 goto free_cpu;
1883
1884 if (ph->needs_swap)
1885 nr = bswap_32(nr);
1886
Jan Stancekda8a58b2017-02-17 12:10:26 +01001887 if (nr != (u32)-1 && nr > (u32)cpu_nr) {
Kan Liang2bb00d22015-09-01 09:58:12 -04001888 pr_debug("socket_id number is too big."
1889 "You may need to upgrade the perf tool.\n");
1890 goto free_cpu;
1891 }
1892
1893 ph->env.cpu[i].socket_id = nr;
1894 }
1895
Namhyung Kima1ae5652012-09-24 17:14:59 +09001896 return 0;
1897
1898error:
1899 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04001900free_cpu:
1901 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001902 return -1;
1903}
1904
1905static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001906 struct perf_header *ph, int fd,
1907 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001908{
Jiri Olsac60da222016-07-04 14:16:20 +02001909 struct numa_node *nodes, *n;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001910 ssize_t ret;
Jiri Olsac60da222016-07-04 14:16:20 +02001911 u32 nr, i;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001912 char *str;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001913
1914 /* nr nodes */
Namhyung Kim5323f602012-12-17 15:38:54 +09001915 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001916 if (ret != sizeof(nr))
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001917 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001918
1919 if (ph->needs_swap)
1920 nr = bswap_32(nr);
1921
Jiri Olsac60da222016-07-04 14:16:20 +02001922 nodes = zalloc(sizeof(*nodes) * nr);
1923 if (!nodes)
1924 return -ENOMEM;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001925
1926 for (i = 0; i < nr; i++) {
Jiri Olsac60da222016-07-04 14:16:20 +02001927 n = &nodes[i];
1928
Namhyung Kima1ae5652012-09-24 17:14:59 +09001929 /* node number */
Jiri Olsac60da222016-07-04 14:16:20 +02001930 ret = readn(fd, &n->node, sizeof(u32));
1931 if (ret != sizeof(n->node))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001932 goto error;
1933
Jiri Olsac60da222016-07-04 14:16:20 +02001934 ret = readn(fd, &n->mem_total, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001935 if (ret != sizeof(u64))
1936 goto error;
1937
Jiri Olsac60da222016-07-04 14:16:20 +02001938 ret = readn(fd, &n->mem_free, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001939 if (ret != sizeof(u64))
1940 goto error;
1941
1942 if (ph->needs_swap) {
Jiri Olsac60da222016-07-04 14:16:20 +02001943 n->node = bswap_32(n->node);
1944 n->mem_total = bswap_64(n->mem_total);
1945 n->mem_free = bswap_64(n->mem_free);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001946 }
1947
Namhyung Kima1ae5652012-09-24 17:14:59 +09001948 str = do_read_string(fd, ph);
1949 if (!str)
1950 goto error;
1951
Jiri Olsac60da222016-07-04 14:16:20 +02001952 n->map = cpu_map__new(str);
1953 if (!n->map)
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001954 goto error;
Jiri Olsac60da222016-07-04 14:16:20 +02001955
Namhyung Kima1ae5652012-09-24 17:14:59 +09001956 free(str);
1957 }
Jiri Olsaf957a532016-10-10 09:56:32 +02001958 ph->env.nr_numa_nodes = nr;
Jiri Olsac60da222016-07-04 14:16:20 +02001959 ph->env.numa_nodes = nodes;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001960 return 0;
1961
1962error:
Jiri Olsac60da222016-07-04 14:16:20 +02001963 free(nodes);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001964 return -1;
1965}
1966
1967static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001968 struct perf_header *ph, int fd,
1969 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001970{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001971 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001972 char *name;
1973 u32 pmu_num;
1974 u32 type;
1975 struct strbuf sb;
1976
Namhyung Kim5323f602012-12-17 15:38:54 +09001977 ret = readn(fd, &pmu_num, sizeof(pmu_num));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001978 if (ret != sizeof(pmu_num))
1979 return -1;
1980
1981 if (ph->needs_swap)
1982 pmu_num = bswap_32(pmu_num);
1983
1984 if (!pmu_num) {
1985 pr_debug("pmu mappings not available\n");
1986 return 0;
1987 }
1988
1989 ph->env.nr_pmu_mappings = pmu_num;
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001990 if (strbuf_init(&sb, 128) < 0)
1991 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001992
1993 while (pmu_num) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001994 if (readn(fd, &type, sizeof(type)) != sizeof(type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001995 goto error;
1996 if (ph->needs_swap)
1997 type = bswap_32(type);
1998
1999 name = do_read_string(fd, ph);
2000 if (!name)
2001 goto error;
2002
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002003 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2004 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002005 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002006 if (strbuf_add(&sb, "", 1) < 0)
2007 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002008
Kan Liange0838e02015-09-10 11:03:05 -03002009 if (!strcmp(name, "msr"))
2010 ph->env.msr_pmu_type = type;
2011
Namhyung Kima1ae5652012-09-24 17:14:59 +09002012 free(name);
2013 pmu_num--;
2014 }
2015 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2016 return 0;
2017
2018error:
2019 strbuf_release(&sb);
2020 return -1;
2021}
2022
Namhyung Kima8bb5592013-01-22 18:09:31 +09002023static int process_group_desc(struct perf_file_section *section __maybe_unused,
2024 struct perf_header *ph, int fd,
2025 void *data __maybe_unused)
2026{
2027 size_t ret = -1;
2028 u32 i, nr, nr_groups;
2029 struct perf_session *session;
2030 struct perf_evsel *evsel, *leader = NULL;
2031 struct group_desc {
2032 char *name;
2033 u32 leader_idx;
2034 u32 nr_members;
2035 } *desc;
2036
2037 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
2038 return -1;
2039
2040 if (ph->needs_swap)
2041 nr_groups = bswap_32(nr_groups);
2042
2043 ph->env.nr_groups = nr_groups;
2044 if (!nr_groups) {
2045 pr_debug("group desc not available\n");
2046 return 0;
2047 }
2048
2049 desc = calloc(nr_groups, sizeof(*desc));
2050 if (!desc)
2051 return -1;
2052
2053 for (i = 0; i < nr_groups; i++) {
2054 desc[i].name = do_read_string(fd, ph);
2055 if (!desc[i].name)
2056 goto out_free;
2057
2058 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
2059 goto out_free;
2060
2061 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
2062 goto out_free;
2063
2064 if (ph->needs_swap) {
2065 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
2066 desc[i].nr_members = bswap_32(desc[i].nr_members);
2067 }
2068 }
2069
2070 /*
2071 * Rebuild group relationship based on the group_desc
2072 */
2073 session = container_of(ph, struct perf_session, header);
2074 session->evlist->nr_groups = nr_groups;
2075
2076 i = nr = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002077 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002078 if (evsel->idx == (int) desc[i].leader_idx) {
2079 evsel->leader = evsel;
2080 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09002081 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002082 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09002083 desc[i].name = NULL;
2084 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09002085 evsel->nr_members = desc[i].nr_members;
2086
2087 if (i >= nr_groups || nr > 0) {
2088 pr_debug("invalid group desc\n");
2089 goto out_free;
2090 }
2091
2092 leader = evsel;
2093 nr = evsel->nr_members - 1;
2094 i++;
2095 } else if (nr) {
2096 /* This is a group member */
2097 evsel->leader = leader;
2098
2099 nr--;
2100 }
2101 }
2102
2103 if (i != nr_groups || nr != 0) {
2104 pr_debug("invalid group desc\n");
2105 goto out_free;
2106 }
2107
2108 ret = 0;
2109out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002110 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002111 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002112 free(desc);
2113
2114 return ret;
2115}
2116
Adrian Hunter99fa2982015-04-30 17:37:25 +03002117static int process_auxtrace(struct perf_file_section *section,
2118 struct perf_header *ph, int fd,
2119 void *data __maybe_unused)
2120{
2121 struct perf_session *session;
2122 int err;
2123
2124 session = container_of(ph, struct perf_session, header);
2125
2126 err = auxtrace_index__process(fd, section->size, session,
2127 ph->needs_swap);
2128 if (err < 0)
2129 pr_err("Failed to process auxtrace index\n");
2130 return err;
2131}
2132
Jiri Olsa720e98b2016-02-16 16:01:43 +01002133static int process_cache(struct perf_file_section *section __maybe_unused,
2134 struct perf_header *ph __maybe_unused, int fd __maybe_unused,
2135 void *data __maybe_unused)
2136{
2137 struct cpu_cache_level *caches;
2138 u32 cnt, i, version;
2139
2140 if (readn(fd, &version, sizeof(version)) != sizeof(version))
2141 return -1;
2142
2143 if (ph->needs_swap)
2144 version = bswap_32(version);
2145
2146 if (version != 1)
2147 return -1;
2148
2149 if (readn(fd, &cnt, sizeof(cnt)) != sizeof(cnt))
2150 return -1;
2151
2152 if (ph->needs_swap)
2153 cnt = bswap_32(cnt);
2154
2155 caches = zalloc(sizeof(*caches) * cnt);
2156 if (!caches)
2157 return -1;
2158
2159 for (i = 0; i < cnt; i++) {
2160 struct cpu_cache_level c;
2161
2162 #define _R(v) \
2163 if (readn(fd, &c.v, sizeof(u32)) != sizeof(u32))\
2164 goto out_free_caches; \
2165 if (ph->needs_swap) \
2166 c.v = bswap_32(c.v); \
2167
2168 _R(level)
2169 _R(line_size)
2170 _R(sets)
2171 _R(ways)
2172 #undef _R
2173
2174 #define _R(v) \
2175 c.v = do_read_string(fd, ph); \
2176 if (!c.v) \
2177 goto out_free_caches;
2178
2179 _R(type)
2180 _R(size)
2181 _R(map)
2182 #undef _R
2183
2184 caches[i] = c;
2185 }
2186
2187 ph->env.caches = caches;
2188 ph->env.caches_cnt = cnt;
2189 return 0;
2190out_free_caches:
2191 free(caches);
2192 return -1;
2193}
2194
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002195struct feature_ops {
2196 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2197 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01002198 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002199 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002200 const char *name;
2201 bool full_only;
2202};
2203
Robert Richter8cdfa782011-12-07 10:02:56 +01002204#define FEAT_OPA(n, func) \
2205 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01002206#define FEAT_OPP(n, func) \
2207 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2208 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01002209#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01002210 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09002211 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01002212
2213/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002214#define print_tracing_data NULL
2215#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002216
2217static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002218 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01002219 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002220 FEAT_OPP(HEADER_HOSTNAME, hostname),
2221 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2222 FEAT_OPP(HEADER_VERSION, version),
2223 FEAT_OPP(HEADER_ARCH, arch),
2224 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2225 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09002226 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002227 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02002228 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002229 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01002230 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2231 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01002232 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002233 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09002234 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Adrian Hunter99fa2982015-04-30 17:37:25 +03002235 FEAT_OPP(HEADER_AUXTRACE, auxtrace),
Jiri Olsaffa517a2015-10-25 15:51:43 +01002236 FEAT_OPA(HEADER_STAT, stat),
Jiri Olsa720e98b2016-02-16 16:01:43 +01002237 FEAT_OPF(HEADER_CACHE, cache),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002238};
2239
2240struct header_print_data {
2241 FILE *fp;
2242 bool full; /* extended list of headers */
2243};
2244
2245static int perf_file_section__fprintf_info(struct perf_file_section *section,
2246 struct perf_header *ph,
2247 int feat, int fd, void *data)
2248{
2249 struct header_print_data *hd = data;
2250
2251 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2252 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2253 "%d, continuing...\n", section->offset, feat);
2254 return 0;
2255 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002256 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002257 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002258 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002259 }
2260 if (!feat_ops[feat].print)
2261 return 0;
2262
2263 if (!feat_ops[feat].full_only || hd->full)
2264 feat_ops[feat].print(ph, fd, hd->fp);
2265 else
2266 fprintf(hd->fp, "# %s info available, use -I to display\n",
2267 feat_ops[feat].name);
2268
2269 return 0;
2270}
2271
2272int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2273{
2274 struct header_print_data hd;
2275 struct perf_header *header = &session->header;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002276 int fd = perf_data_file__fd(session->file);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002277 struct stat st;
Jiri Olsaaabae162016-10-10 09:35:50 +02002278 int ret, bit;
Jiri Olsaf45f5612016-10-10 09:03:07 +02002279
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002280 hd.fp = fp;
2281 hd.full = full;
2282
Jiri Olsaf45f5612016-10-10 09:03:07 +02002283 ret = fstat(fd, &st);
2284 if (ret == -1)
2285 return -1;
2286
2287 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
2288
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002289 perf_header__process_sections(header, fd, &hd,
2290 perf_file_section__fprintf_info);
Jiri Olsaaabae162016-10-10 09:35:50 +02002291
David Carrillo-Cisnerosc9d1c932017-04-10 13:14:32 -07002292 if (session->file->is_pipe)
2293 return 0;
2294
Jiri Olsaaabae162016-10-10 09:35:50 +02002295 fprintf(fp, "# missing features: ");
2296 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2297 if (bit)
2298 fprintf(fp, "%s ", feat_ops[bit].name);
2299 }
2300
2301 fprintf(fp, "\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002302 return 0;
2303}
2304
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002305static int do_write_feat(int fd, struct perf_header *h, int type,
2306 struct perf_file_section **p,
2307 struct perf_evlist *evlist)
2308{
2309 int err;
2310 int ret = 0;
2311
2312 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002313 if (!feat_ops[type].write)
2314 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002315
2316 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2317
2318 err = feat_ops[type].write(fd, h, evlist);
2319 if (err < 0) {
Jiri Olsa0c2aff42016-10-10 09:38:02 +02002320 pr_debug("failed to write feature %s\n", feat_ops[type].name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002321
2322 /* undo anything written */
2323 lseek(fd, (*p)->offset, SEEK_SET);
2324
2325 return -1;
2326 }
2327 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2328 (*p)++;
2329 }
2330 return ret;
2331}
2332
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002333static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002334 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002335{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002336 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002337 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002338 int sec_size;
2339 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002340 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002341 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002342
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002343 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002344 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002345 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002346
Paul Gortmaker91b98802013-01-30 20:05:49 -05002347 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002348 if (feat_sec == NULL)
2349 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002350
2351 sec_size = sizeof(*feat_sec) * nr_sections;
2352
Jiri Olsa8d541e92013-07-17 19:49:44 +02002353 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002354 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002355
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002356 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2357 if (do_write_feat(fd, header, feat, &p, evlist))
2358 perf_header__clear_feat(header, feat);
2359 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002360
Xiao Guangrongf887f302010-02-04 16:46:42 +08002361 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002362 /*
2363 * may write more than needed due to dropped feature, but
2364 * this is okay, reader will skip the mising entries
2365 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002366 err = do_write(fd, feat_sec, sec_size);
2367 if (err < 0)
2368 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002369 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002370 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002371}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002372
Tom Zanussi8dc58102010-04-01 23:59:15 -05002373int perf_header__write_pipe(int fd)
2374{
2375 struct perf_pipe_file_header f_header;
2376 int err;
2377
2378 f_header = (struct perf_pipe_file_header){
2379 .magic = PERF_MAGIC,
2380 .size = sizeof(f_header),
2381 };
2382
2383 err = do_write(fd, &f_header, sizeof(f_header));
2384 if (err < 0) {
2385 pr_debug("failed to write perf pipe header\n");
2386 return err;
2387 }
2388
2389 return 0;
2390}
2391
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002392int perf_session__write_header(struct perf_session *session,
2393 struct perf_evlist *evlist,
2394 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002395{
2396 struct perf_file_header f_header;
2397 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002398 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002399 struct perf_evsel *evsel;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002400 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002401 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002402
2403 lseek(fd, sizeof(f_header), SEEK_SET);
2404
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002405 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002406 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2407 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002408 if (err < 0) {
2409 pr_debug("failed to write perf header\n");
2410 return err;
2411 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002412 }
2413
Jiri Olsa944d62b2013-07-17 19:49:43 +02002414 attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002415
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002416 evlist__for_each_entry(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002417 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002418 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002419 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002420 .offset = evsel->id_offset,
2421 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002422 }
2423 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002424 err = do_write(fd, &f_attr, sizeof(f_attr));
2425 if (err < 0) {
2426 pr_debug("failed to write perf header attribute\n");
2427 return err;
2428 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002429 }
2430
Adrian Hunterd645c442013-12-11 14:36:28 +02002431 if (!header->data_offset)
2432 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002433 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002434
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002435 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002436 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002437 if (err < 0)
2438 return err;
2439 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002440
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002441 f_header = (struct perf_file_header){
2442 .magic = PERF_MAGIC,
2443 .size = sizeof(f_header),
2444 .attr_size = sizeof(f_attr),
2445 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002446 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002447 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002448 },
2449 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002450 .offset = header->data_offset,
2451 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002452 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002453 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002454 };
2455
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002456 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002457
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002458 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002459 err = do_write(fd, &f_header, sizeof(f_header));
2460 if (err < 0) {
2461 pr_debug("failed to write perf header\n");
2462 return err;
2463 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002464 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002465
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002466 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002467}
2468
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002469static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002470 int fd, void *buf, size_t size)
2471{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002472 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002473 return -1;
2474
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002475 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002476 mem_bswap_64(buf, size);
2477
2478 return 0;
2479}
2480
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002481int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002482 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002483 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002484 struct perf_header *ph,
2485 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002486{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002487 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002488 int nr_sections;
2489 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002490 int feat;
2491 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002492
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002493 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002494 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002495 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002496
Paul Gortmaker91b98802013-01-30 20:05:49 -05002497 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002498 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002499 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002500
2501 sec_size = sizeof(*feat_sec) * nr_sections;
2502
Jiri Olsa8d541e92013-07-17 19:49:44 +02002503 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002504
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002505 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2506 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002507 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002508
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002509 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2510 err = process(sec++, header, feat, fd, data);
2511 if (err < 0)
2512 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002513 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002514 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002515out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002516 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002517 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002518}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002519
Stephane Eranian114382a2012-02-09 23:21:08 +01002520static const int attr_file_abi_sizes[] = {
2521 [0] = PERF_ATTR_SIZE_VER0,
2522 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002523 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002524 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002525 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002526 0,
2527};
2528
2529/*
2530 * In the legacy file format, the magic number is not used to encode endianness.
2531 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2532 * on ABI revisions, we need to try all combinations for all endianness to
2533 * detect the endianness.
2534 */
2535static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2536{
2537 uint64_t ref_size, attr_size;
2538 int i;
2539
2540 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2541 ref_size = attr_file_abi_sizes[i]
2542 + sizeof(struct perf_file_section);
2543 if (hdr_sz != ref_size) {
2544 attr_size = bswap_64(hdr_sz);
2545 if (attr_size != ref_size)
2546 continue;
2547
2548 ph->needs_swap = true;
2549 }
2550 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2551 i,
2552 ph->needs_swap);
2553 return 0;
2554 }
2555 /* could not determine endianness */
2556 return -1;
2557}
2558
2559#define PERF_PIPE_HDR_VER0 16
2560
2561static const size_t attr_pipe_abi_sizes[] = {
2562 [0] = PERF_PIPE_HDR_VER0,
2563 0,
2564};
2565
2566/*
2567 * In the legacy pipe format, there is an implicit assumption that endiannesss
2568 * between host recording the samples, and host parsing the samples is the
2569 * same. This is not always the case given that the pipe output may always be
2570 * redirected into a file and analyzed on a different machine with possibly a
2571 * different endianness and perf_event ABI revsions in the perf tool itself.
2572 */
2573static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2574{
2575 u64 attr_size;
2576 int i;
2577
2578 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2579 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2580 attr_size = bswap_64(hdr_sz);
2581 if (attr_size != hdr_sz)
2582 continue;
2583
2584 ph->needs_swap = true;
2585 }
2586 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2587 return 0;
2588 }
2589 return -1;
2590}
2591
Feng Tange84ba4e2012-10-30 11:56:07 +08002592bool is_perf_magic(u64 magic)
2593{
2594 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2595 || magic == __perf_magic2
2596 || magic == __perf_magic2_sw)
2597 return true;
2598
2599 return false;
2600}
2601
Stephane Eranian114382a2012-02-09 23:21:08 +01002602static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2603 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002604{
2605 int ret;
2606
2607 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002608 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002609 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002610 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002611 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002612 if (is_pipe)
2613 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002614
Stephane Eranian114382a2012-02-09 23:21:08 +01002615 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002616 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002617 /*
2618 * the new magic number serves two purposes:
2619 * - unique number to identify actual perf.data files
2620 * - encode endianness of file
2621 */
Namhyung Kimf7913972015-01-29 17:06:45 +09002622 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002623
Stephane Eranian114382a2012-02-09 23:21:08 +01002624 /* check magic number with one endianness */
2625 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002626 return 0;
2627
Stephane Eranian114382a2012-02-09 23:21:08 +01002628 /* check magic number with opposite endianness */
2629 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002630 return -1;
2631
2632 ph->needs_swap = true;
2633
2634 return 0;
2635}
2636
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002637int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002638 struct perf_header *ph, int fd)
2639{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002640 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002641
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002642 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002643
Stephane Eranian73323f52012-02-02 13:54:44 +01002644 ret = readn(fd, header, sizeof(*header));
2645 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002646 return -1;
2647
Stephane Eranian114382a2012-02-09 23:21:08 +01002648 if (check_magic_endian(header->magic,
2649 header->attr_size, false, ph) < 0) {
2650 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002651 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002652 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002653
Stephane Eranian73323f52012-02-02 13:54:44 +01002654 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002655 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002656 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002657 }
2658
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002659 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002660 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002661 if (header->size == offsetof(typeof(*header), adds_features))
2662 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002663 else
2664 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002665 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002666 /*
2667 * feature bitmap is declared as an array of unsigned longs --
2668 * not good since its size can differ between the host that
2669 * generated the data file and the host analyzing the file.
2670 *
2671 * We need to handle endianness, but we don't know the size of
2672 * the unsigned long where the file was generated. Take a best
2673 * guess at determining it: try 64-bit swap first (ie., file
2674 * created on a 64-bit host), and check if the hostname feature
2675 * bit is set (this feature bit is forced on as of fbe96f2).
2676 * If the bit is not, undo the 64-bit swap and try a 32-bit
2677 * swap. If the hostname bit is still not set (e.g., older data
2678 * file), punt and fallback to the original behavior --
2679 * clearing all feature bits and setting buildid.
2680 */
David Ahern80c01202012-06-08 11:47:51 -03002681 mem_bswap_64(&header->adds_features,
2682 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002683
2684 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002685 /* unswap as u64 */
2686 mem_bswap_64(&header->adds_features,
2687 BITS_TO_U64(HEADER_FEAT_BITS));
2688
2689 /* unswap as u32 */
2690 mem_bswap_32(&header->adds_features,
2691 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002692 }
2693
2694 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2695 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2696 set_bit(HEADER_BUILD_ID, header->adds_features);
2697 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002698 }
2699
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002700 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002701 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002702
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002703 ph->data_offset = header->data.offset;
2704 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002705 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002706 return 0;
2707}
2708
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002709static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002710 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002711 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002712{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002713 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002714 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002715 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002716 return 0;
2717 }
2718
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002719 if (feat >= HEADER_LAST_FEATURE) {
2720 pr_debug("unknown feature %d, continuing...\n", feat);
2721 return 0;
2722 }
2723
Robert Richterf1c67db2012-02-10 15:41:56 +01002724 if (!feat_ops[feat].process)
2725 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002726
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002727 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002728}
2729
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002730static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002731 struct perf_header *ph, int fd,
2732 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002733{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002734 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002735
2736 ret = readn(fd, header, sizeof(*header));
2737 if (ret <= 0)
2738 return -1;
2739
Stephane Eranian114382a2012-02-09 23:21:08 +01002740 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2741 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002742 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002743 }
2744
2745 if (ph->needs_swap)
2746 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002747
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002748 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002749 return -1;
2750
Tom Zanussi8dc58102010-04-01 23:59:15 -05002751 return 0;
2752}
2753
Jiri Olsad4339562013-07-17 19:49:41 +02002754static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002755{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002756 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002757 struct perf_pipe_file_header f_header;
2758
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002759 if (perf_file_header__read_pipe(&f_header, header,
2760 perf_data_file__fd(session->file),
Tom Zanussi454c4072010-05-01 01:41:20 -05002761 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002762 pr_debug("incompatible file format\n");
2763 return -EINVAL;
2764 }
2765
Tom Zanussi8dc58102010-04-01 23:59:15 -05002766 return 0;
2767}
2768
Stephane Eranian69996df2012-02-09 23:21:06 +01002769static int read_attr(int fd, struct perf_header *ph,
2770 struct perf_file_attr *f_attr)
2771{
2772 struct perf_event_attr *attr = &f_attr->attr;
2773 size_t sz, left;
2774 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002775 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002776
2777 memset(f_attr, 0, sizeof(*f_attr));
2778
2779 /* read minimal guaranteed structure */
2780 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2781 if (ret <= 0) {
2782 pr_debug("cannot read %d bytes of header attr\n",
2783 PERF_ATTR_SIZE_VER0);
2784 return -1;
2785 }
2786
2787 /* on file perf_event_attr size */
2788 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002789
Stephane Eranian69996df2012-02-09 23:21:06 +01002790 if (ph->needs_swap)
2791 sz = bswap_32(sz);
2792
2793 if (sz == 0) {
2794 /* assume ABI0 */
2795 sz = PERF_ATTR_SIZE_VER0;
2796 } else if (sz > our_sz) {
2797 pr_debug("file uses a more recent and unsupported ABI"
2798 " (%zu bytes extra)\n", sz - our_sz);
2799 return -1;
2800 }
2801 /* what we have not yet read and that we know about */
2802 left = sz - PERF_ATTR_SIZE_VER0;
2803 if (left) {
2804 void *ptr = attr;
2805 ptr += PERF_ATTR_SIZE_VER0;
2806
2807 ret = readn(fd, ptr, left);
2808 }
2809 /* read perf_file_section, ids are read in caller */
2810 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2811
2812 return ret <= 0 ? -1 : 0;
2813}
2814
Namhyung Kim831394b2012-09-06 11:10:46 +09002815static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2816 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002817{
Namhyung Kim831394b2012-09-06 11:10:46 +09002818 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002819 char bf[128];
2820
Namhyung Kim831394b2012-09-06 11:10:46 +09002821 /* already prepared */
2822 if (evsel->tp_format)
2823 return 0;
2824
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002825 if (pevent == NULL) {
2826 pr_debug("broken or missing trace data\n");
2827 return -1;
2828 }
2829
Namhyung Kim831394b2012-09-06 11:10:46 +09002830 event = pevent_find_event(pevent, evsel->attr.config);
Namhyung Kima7619ae2013-04-18 21:24:16 +09002831 if (event == NULL) {
2832 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002833 return -1;
Namhyung Kima7619ae2013-04-18 21:24:16 +09002834 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002835
Namhyung Kim831394b2012-09-06 11:10:46 +09002836 if (!evsel->name) {
2837 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2838 evsel->name = strdup(bf);
2839 if (evsel->name == NULL)
2840 return -1;
2841 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002842
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002843 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002844 return 0;
2845}
2846
Namhyung Kim831394b2012-09-06 11:10:46 +09002847static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2848 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002849{
2850 struct perf_evsel *pos;
2851
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002852 evlist__for_each_entry(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002853 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2854 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002855 return -1;
2856 }
2857
2858 return 0;
2859}
2860
Jiri Olsad4339562013-07-17 19:49:41 +02002861int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002862{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002863 struct perf_data_file *file = session->file;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002864 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002865 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002866 struct perf_file_attr f_attr;
2867 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002868 int nr_attrs, nr_ids, i, j;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002869 int fd = perf_data_file__fd(file);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002870
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002871 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002872 if (session->evlist == NULL)
2873 return -ENOMEM;
2874
Kan Liang2c071442015-08-28 05:48:05 -04002875 session->evlist->env = &header->env;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -03002876 session->machines.host.env = &header->env;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002877 if (perf_data_file__is_pipe(file))
Jiri Olsad4339562013-07-17 19:49:41 +02002878 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002879
Stephane Eranian69996df2012-02-09 23:21:06 +01002880 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002881 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002882
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002883 /*
2884 * Sanity check that perf.data was written cleanly; data size is
2885 * initialized to 0 and updated only if the on_exit function is run.
2886 * If data size is still 0 then the file contains only partial
2887 * information. Just warn user and process it as much as it can.
2888 */
2889 if (f_header.data.size == 0) {
2890 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2891 "Was the 'perf record' command properly terminated?\n",
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002892 file->path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002893 }
2894
Stephane Eranian69996df2012-02-09 23:21:06 +01002895 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002896 lseek(fd, f_header.attrs.offset, SEEK_SET);
2897
2898 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002899 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002900 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002901
Stephane Eranian69996df2012-02-09 23:21:06 +01002902 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002903 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002904
David Ahern1060ab82015-04-09 16:15:46 -04002905 if (header->needs_swap) {
2906 f_attr.ids.size = bswap_64(f_attr.ids.size);
2907 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06002908 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04002909 }
David Aherneda39132011-07-15 12:34:09 -06002910
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002911 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002912 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002913
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002914 if (evsel == NULL)
2915 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002916
2917 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002918 /*
2919 * Do it before so that if perf_evsel__alloc_id fails, this
2920 * entry gets purged too at perf_evlist__delete().
2921 */
2922 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002923
2924 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002925 /*
2926 * We don't have the cpu and thread maps on the header, so
2927 * for allocating the perf_sample_id table we fake 1 cpu and
2928 * hattr->ids threads.
2929 */
2930 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2931 goto out_delete_evlist;
2932
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002933 lseek(fd, f_attr.ids.offset, SEEK_SET);
2934
2935 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002936 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002937 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002938
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002939 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002940 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002941
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002942 lseek(fd, tmp, SEEK_SET);
2943 }
2944
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002945 symbol_conf.nr_events = nr_attrs;
2946
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002947 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002948 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002949
Namhyung Kim831394b2012-09-06 11:10:46 +09002950 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002951 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002952 goto out_delete_evlist;
2953
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002954 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002955out_errno:
2956 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002957
2958out_delete_evlist:
2959 perf_evlist__delete(session->evlist);
2960 session->evlist = NULL;
2961 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002962}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002963
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002964int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002965 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002966 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002967{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002968 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002969 size_t size;
2970 int err;
2971
2972 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002973 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002974 size += sizeof(struct perf_event_header);
2975 size += ids * sizeof(u64);
2976
2977 ev = malloc(size);
2978
Chris Samuelce47dc52010-11-13 13:35:06 +11002979 if (ev == NULL)
2980 return -ENOMEM;
2981
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002982 ev->attr.attr = *attr;
2983 memcpy(ev->attr.id, id, ids * sizeof(u64));
2984
2985 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002986 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002987
Robert Richterf4d83432012-08-16 21:10:17 +02002988 if (ev->attr.header.size == size)
2989 err = process(tool, ev, NULL, NULL);
2990 else
2991 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002992
2993 free(ev);
2994
2995 return err;
2996}
2997
Jiri Olsaa6e52812015-10-25 15:51:37 +01002998static struct event_update_event *
2999event_update_event__new(size_t size, u64 type, u64 id)
3000{
3001 struct event_update_event *ev;
3002
3003 size += sizeof(*ev);
3004 size = PERF_ALIGN(size, sizeof(u64));
3005
3006 ev = zalloc(size);
3007 if (ev) {
3008 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3009 ev->header.size = (u16)size;
3010 ev->type = type;
3011 ev->id = id;
3012 }
3013 return ev;
3014}
3015
3016int
3017perf_event__synthesize_event_update_unit(struct perf_tool *tool,
3018 struct perf_evsel *evsel,
3019 perf_event__handler_t process)
3020{
3021 struct event_update_event *ev;
3022 size_t size = strlen(evsel->unit);
3023 int err;
3024
3025 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
3026 if (ev == NULL)
3027 return -ENOMEM;
3028
3029 strncpy(ev->data, evsel->unit, size);
3030 err = process(tool, (union perf_event *)ev, NULL, NULL);
3031 free(ev);
3032 return err;
3033}
3034
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003035int
3036perf_event__synthesize_event_update_scale(struct perf_tool *tool,
3037 struct perf_evsel *evsel,
3038 perf_event__handler_t process)
3039{
3040 struct event_update_event *ev;
3041 struct event_update_event_scale *ev_data;
3042 int err;
3043
3044 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
3045 if (ev == NULL)
3046 return -ENOMEM;
3047
3048 ev_data = (struct event_update_event_scale *) ev->data;
3049 ev_data->scale = evsel->scale;
3050 err = process(tool, (union perf_event*) ev, NULL, NULL);
3051 free(ev);
3052 return err;
3053}
3054
Jiri Olsa802c9042015-10-25 15:51:39 +01003055int
3056perf_event__synthesize_event_update_name(struct perf_tool *tool,
3057 struct perf_evsel *evsel,
3058 perf_event__handler_t process)
3059{
3060 struct event_update_event *ev;
3061 size_t len = strlen(evsel->name);
3062 int err;
3063
3064 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
3065 if (ev == NULL)
3066 return -ENOMEM;
3067
3068 strncpy(ev->data, evsel->name, len);
3069 err = process(tool, (union perf_event*) ev, NULL, NULL);
3070 free(ev);
3071 return err;
3072}
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003073
Jiri Olsa86ebb092015-10-25 15:51:40 +01003074int
3075perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
3076 struct perf_evsel *evsel,
3077 perf_event__handler_t process)
3078{
3079 size_t size = sizeof(struct event_update_event);
3080 struct event_update_event *ev;
3081 int max, err;
3082 u16 type;
3083
3084 if (!evsel->own_cpus)
3085 return 0;
3086
3087 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3088 if (!ev)
3089 return -ENOMEM;
3090
3091 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3092 ev->header.size = (u16)size;
3093 ev->type = PERF_EVENT_UPDATE__CPUS;
3094 ev->id = evsel->id[0];
3095
3096 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3097 evsel->own_cpus,
3098 type, max);
3099
3100 err = process(tool, (union perf_event*) ev, NULL, NULL);
3101 free(ev);
3102 return err;
3103}
3104
Jiri Olsac853f932015-10-25 15:51:41 +01003105size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3106{
3107 struct event_update_event *ev = &event->event_update;
3108 struct event_update_event_scale *ev_scale;
3109 struct event_update_event_cpus *ev_cpus;
3110 struct cpu_map *map;
3111 size_t ret;
3112
3113 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3114
3115 switch (ev->type) {
3116 case PERF_EVENT_UPDATE__SCALE:
3117 ev_scale = (struct event_update_event_scale *) ev->data;
3118 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3119 break;
3120 case PERF_EVENT_UPDATE__UNIT:
3121 ret += fprintf(fp, "... unit: %s\n", ev->data);
3122 break;
3123 case PERF_EVENT_UPDATE__NAME:
3124 ret += fprintf(fp, "... name: %s\n", ev->data);
3125 break;
3126 case PERF_EVENT_UPDATE__CPUS:
3127 ev_cpus = (struct event_update_event_cpus *) ev->data;
3128 ret += fprintf(fp, "... ");
3129
3130 map = cpu_map__new_data(&ev_cpus->cpus);
3131 if (map)
3132 ret += cpu_map__fprintf(map, fp);
3133 else
3134 ret += fprintf(fp, "failed to get cpus\n");
3135 break;
3136 default:
3137 ret += fprintf(fp, "... unknown type\n");
3138 break;
3139 }
3140
3141 return ret;
3142}
Jiri Olsa86ebb092015-10-25 15:51:40 +01003143
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003144int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003145 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003146 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003147{
Robert Richter6606f872012-08-16 21:10:19 +02003148 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003149 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003150
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003151 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02003152 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3153 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003154 if (err) {
3155 pr_debug("failed to create perf header attribute\n");
3156 return err;
3157 }
3158 }
3159
3160 return err;
3161}
3162
Adrian Hunter47c3d102013-07-04 16:20:21 +03003163int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3164 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003165 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003166{
Robert Richterf4d83432012-08-16 21:10:17 +02003167 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003168 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003169 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003170
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003171 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09003172 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003173 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003174 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003175 }
3176
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03003177 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003178 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003179 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003180
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003181 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003182
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003183 ids = event->header.size;
3184 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003185 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003186 /*
3187 * We don't have the cpu and thread maps on the header, so
3188 * for allocating the perf_sample_id table we fake 1 cpu and
3189 * hattr->ids threads.
3190 */
3191 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3192 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003193
3194 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003195 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003196 }
3197
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03003198 symbol_conf.nr_events = evlist->nr_entries;
3199
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003200 return 0;
3201}
Tom Zanussicd19a032010-04-01 23:59:20 -05003202
Jiri Olsaffe777252015-10-25 15:51:36 +01003203int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3204 union perf_event *event,
3205 struct perf_evlist **pevlist)
3206{
3207 struct event_update_event *ev = &event->event_update;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003208 struct event_update_event_scale *ev_scale;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003209 struct event_update_event_cpus *ev_cpus;
Jiri Olsaffe777252015-10-25 15:51:36 +01003210 struct perf_evlist *evlist;
3211 struct perf_evsel *evsel;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003212 struct cpu_map *map;
Jiri Olsaffe777252015-10-25 15:51:36 +01003213
3214 if (!pevlist || *pevlist == NULL)
3215 return -EINVAL;
3216
3217 evlist = *pevlist;
3218
3219 evsel = perf_evlist__id2evsel(evlist, ev->id);
3220 if (evsel == NULL)
3221 return -EINVAL;
3222
Jiri Olsaa6e52812015-10-25 15:51:37 +01003223 switch (ev->type) {
3224 case PERF_EVENT_UPDATE__UNIT:
3225 evsel->unit = strdup(ev->data);
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003226 break;
Jiri Olsa802c9042015-10-25 15:51:39 +01003227 case PERF_EVENT_UPDATE__NAME:
3228 evsel->name = strdup(ev->data);
3229 break;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003230 case PERF_EVENT_UPDATE__SCALE:
3231 ev_scale = (struct event_update_event_scale *) ev->data;
3232 evsel->scale = ev_scale->scale;
Arnaldo Carvalho de Melo8434a2e2017-02-08 21:57:22 -03003233 break;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003234 case PERF_EVENT_UPDATE__CPUS:
3235 ev_cpus = (struct event_update_event_cpus *) ev->data;
3236
3237 map = cpu_map__new_data(&ev_cpus->cpus);
3238 if (map)
3239 evsel->own_cpus = map;
3240 else
3241 pr_err("failed to get event_update cpus\n");
Jiri Olsaa6e52812015-10-25 15:51:37 +01003242 default:
3243 break;
3244 }
3245
Jiri Olsaffe777252015-10-25 15:51:36 +01003246 return 0;
3247}
3248
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003249int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003250 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003251 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05003252{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003253 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02003254 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05003255 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003256 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05003257
Jiri Olsa29208e52011-10-20 15:59:43 +02003258 /*
3259 * We are going to store the size of the data followed
3260 * by the data contents. Since the fd descriptor is a pipe,
3261 * we cannot seek back to store the size of the data once
3262 * we know it. Instead we:
3263 *
3264 * - write the tracing data to the temp file
3265 * - get/write the data size to pipe
3266 * - write the tracing data from the temp file
3267 * to the pipe
3268 */
3269 tdata = tracing_data_get(&evlist->entries, fd, true);
3270 if (!tdata)
3271 return -1;
3272
Tom Zanussi92155452010-04-01 23:59:21 -05003273 memset(&ev, 0, sizeof(ev));
3274
3275 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02003276 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003277 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05003278 padding = aligned_size - size;
3279 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3280 ev.tracing_data.size = aligned_size;
3281
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003282 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05003283
Jiri Olsa29208e52011-10-20 15:59:43 +02003284 /*
3285 * The put function will copy all the tracing data
3286 * stored in temp file to the pipe.
3287 */
3288 tracing_data_put(tdata);
3289
Tom Zanussi92155452010-04-01 23:59:21 -05003290 write_padded(fd, NULL, 0, padding);
3291
3292 return aligned_size;
3293}
3294
Adrian Hunter47c3d102013-07-04 16:20:21 +03003295int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3296 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003297 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05003298{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003299 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003300 int fd = perf_data_file__fd(session->file);
3301 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05003302 char buf[BUFSIZ];
3303
3304 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003305 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05003306 SEEK_SET);
3307
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003308 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003309 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003310 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003311
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003312 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003313 pr_err("%s: reading input file", __func__);
3314 return -1;
3315 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003316 if (session->repipe) {
3317 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003318 if (retw <= 0 || retw != padding) {
3319 pr_err("%s: repiping tracing data padding", __func__);
3320 return -1;
3321 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003322 }
Tom Zanussi92155452010-04-01 23:59:21 -05003323
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003324 if (size_read + padding != size) {
3325 pr_err("%s: tracing data size mismatch", __func__);
3326 return -1;
3327 }
Tom Zanussi92155452010-04-01 23:59:21 -05003328
Namhyung Kim831394b2012-09-06 11:10:46 +09003329 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003330 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003331
Tom Zanussi92155452010-04-01 23:59:21 -05003332 return size_read + padding;
3333}
Tom Zanussic7929e42010-04-01 23:59:22 -05003334
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003335int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003336 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003337 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003338 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003339{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003340 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003341 size_t len;
3342 int err = 0;
3343
3344 if (!pos->hit)
3345 return err;
3346
3347 memset(&ev, 0, sizeof(ev));
3348
3349 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003350 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003351 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3352 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3353 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003354 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003355 ev.build_id.header.size = sizeof(ev.build_id) + len;
3356 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3357
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003358 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003359
3360 return err;
3361}
3362
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003363int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003364 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003365 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003366{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003367 __event_process_build_id(&event->build_id,
3368 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003369 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003370 return 0;
3371}