blob: 847ae51a524bf1b17bd25493115b610c8f9416ce [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03003#include <inttypes.h>
Arnaldo Carvalho de Meloa9072bc2011-10-26 12:41:38 -02004#include "util.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -03005#include "string2.h"
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -03006#include <sys/param.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02007#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02008#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02009#include <unistd.h>
10#include <stdio.h>
11#include <stdlib.h>
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -030012#include <linux/compiler.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010013#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -020014#include <linux/kernel.h>
Robert Richterb1e5a9b2011-12-07 10:02:57 +010015#include <linux/bitops.h>
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -070016#include <linux/stringify.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030017#include <sys/stat.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020018#include <sys/utsname.h>
Jin Yao60115182017-12-08 21:13:41 +080019#include <linux/time64.h>
Jiri Olsae2091ce2018-03-07 16:50:08 +010020#include <dirent.h>
Song Liu606f9722019-03-11 22:30:43 -070021#include <bpf/libbpf.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020022
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020023#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030024#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020025#include "header.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030026#include "memswap.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020027#include "../perf.h"
28#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020029#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010030#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010031#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020032#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020033#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020034#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090035#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020036#include "build-id.h"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020037#include "data.h"
Jiri Olsa720e98b2016-02-16 16:01:43 +010038#include <api/fs/fs.h>
39#include "asm/bug.h"
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -070040#include "tool.h"
Jin Yao60115182017-12-08 21:13:41 +080041#include "time-utils.h"
Jiri Olsae2091ce2018-03-07 16:50:08 +010042#include "units.h"
Jiri Olsa5135d5e2019-02-19 10:58:13 +010043#include "cputopo.h"
Song Liu606f9722019-03-11 22:30:43 -070044#include "bpf-event.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020045
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030046#include "sane_ctype.h"
47
Stephane Eranian73323f52012-02-02 13:54:44 +010048/*
49 * magic2 = "PERFILE2"
50 * must be a numerical value to let the endianness
51 * determine the memory layout. That way we are able
52 * to detect endianness when reading the perf.data file
53 * back.
54 *
55 * we check for legacy (PERFFILE) format.
56 */
57static const char *__perf_magic1 = "PERFFILE";
58static const u64 __perf_magic2 = 0x32454c4946524550ULL;
59static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020060
Stephane Eranian73323f52012-02-02 13:54:44 +010061#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020062
Soramichi AKIYAMAd25ed5d2017-01-17 00:22:37 +090063const char perf_version_string[] = PERF_VERSION;
64
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020065struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020066 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020067 struct perf_file_section ids;
68};
69
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -070070struct feat_fd {
71 struct perf_header *ph;
72 int fd;
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -070073 void *buf; /* Either buf != NULL or fd >= 0 */
David Carrillo-Cisneros62552452017-07-17 21:25:42 -070074 ssize_t offset;
75 size_t size;
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -070076 struct perf_evsel *events;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -070077};
78
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030079void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020080{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030081 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020082}
83
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030084void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020085{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030086 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020087}
88
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030089bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020090{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030091 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020092}
93
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -070094static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
95{
96 ssize_t ret = writen(ff->fd, buf, size);
97
98 if (ret != (ssize_t)size)
99 return ret < 0 ? (int)ret : -1;
100 return 0;
101}
102
103static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
104{
105 /* struct perf_event_header::size is u16 */
106 const size_t max_size = 0xffff - sizeof(struct perf_event_header);
107 size_t new_size = ff->size;
108 void *addr;
109
110 if (size + ff->offset > max_size)
111 return -E2BIG;
112
113 while (size > (new_size - ff->offset))
114 new_size <<= 1;
115 new_size = min(max_size, new_size);
116
117 if (ff->size < new_size) {
118 addr = realloc(ff->buf, new_size);
119 if (!addr)
120 return -ENOMEM;
121 ff->buf = addr;
122 ff->size = new_size;
123 }
124
125 memcpy(ff->buf + ff->offset, buf, size);
126 ff->offset += size;
127
128 return 0;
129}
130
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700131/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700132int do_write(struct feat_fd *ff, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200133{
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700134 if (!ff->buf)
135 return __do_write_fd(ff, buf, size);
136 return __do_write_buf(ff, buf, size);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200137}
138
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700139/* Return: 0 if succeded, -ERR if failed. */
Jiri Olsae2091ce2018-03-07 16:50:08 +0100140static int do_write_bitmap(struct feat_fd *ff, unsigned long *set, u64 size)
141{
142 u64 *p = (u64 *) set;
143 int i, ret;
144
145 ret = do_write(ff, &size, sizeof(size));
146 if (ret < 0)
147 return ret;
148
149 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
150 ret = do_write(ff, p + i, sizeof(*p));
151 if (ret < 0)
152 return ret;
153 }
154
155 return 0;
156}
157
158/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700159int write_padded(struct feat_fd *ff, const void *bf,
160 size_t count, size_t count_aligned)
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200161{
162 static const char zero_buf[NAME_ALIGN];
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700163 int err = do_write(ff, bf, count);
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200164
165 if (!err)
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700166 err = do_write(ff, zero_buf, count_aligned - count);
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200167
168 return err;
169}
170
Kan Liang2bb00d22015-09-01 09:58:12 -0400171#define string_size(str) \
172 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
173
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700174/* Return: 0 if succeded, -ERR if failed. */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700175static int do_write_string(struct feat_fd *ff, const char *str)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200176{
177 u32 len, olen;
178 int ret;
179
180 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300181 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200182
183 /* write len, incl. \0 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700184 ret = do_write(ff, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200185 if (ret < 0)
186 return ret;
187
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700188 return write_padded(ff, str, olen, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200189}
190
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700191static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700192{
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700193 ssize_t ret = readn(ff->fd, addr, size);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700194
195 if (ret != size)
196 return ret < 0 ? (int)ret : -1;
197 return 0;
198}
199
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700200static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
201{
202 if (size > (ssize_t)ff->size - ff->offset)
203 return -1;
204
205 memcpy(addr, ff->buf + ff->offset, size);
206 ff->offset += size;
207
208 return 0;
209
210}
211
212static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
213{
214 if (!ff->buf)
215 return __do_read_fd(ff, addr, size);
216 return __do_read_buf(ff, addr, size);
217}
218
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700219static int do_read_u32(struct feat_fd *ff, u32 *addr)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700220{
221 int ret;
222
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700223 ret = __do_read(ff, addr, sizeof(*addr));
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700224 if (ret)
225 return ret;
226
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700227 if (ff->ph->needs_swap)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700228 *addr = bswap_32(*addr);
229 return 0;
230}
231
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700232static int do_read_u64(struct feat_fd *ff, u64 *addr)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700233{
234 int ret;
235
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700236 ret = __do_read(ff, addr, sizeof(*addr));
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700237 if (ret)
238 return ret;
239
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700240 if (ff->ph->needs_swap)
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700241 *addr = bswap_64(*addr);
242 return 0;
243}
244
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700245static char *do_read_string(struct feat_fd *ff)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200246{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200247 u32 len;
248 char *buf;
249
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700250 if (do_read_u32(ff, &len))
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200251 return NULL;
252
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200253 buf = malloc(len);
254 if (!buf)
255 return NULL;
256
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -0700257 if (!__do_read(ff, buf, len)) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200258 /*
259 * strings are padded by zeroes
260 * thus the actual strlen of buf
261 * may be less than len
262 */
263 return buf;
264 }
265
266 free(buf);
267 return NULL;
268}
269
Jiri Olsae2091ce2018-03-07 16:50:08 +0100270/* Return: 0 if succeded, -ERR if failed. */
271static int do_read_bitmap(struct feat_fd *ff, unsigned long **pset, u64 *psize)
272{
273 unsigned long *set;
274 u64 size, *p;
275 int i, ret;
276
277 ret = do_read_u64(ff, &size);
278 if (ret)
279 return ret;
280
281 set = bitmap_alloc(size);
282 if (!set)
283 return -ENOMEM;
284
Jiri Olsae2091ce2018-03-07 16:50:08 +0100285 p = (u64 *) set;
286
287 for (i = 0; (u64) i < BITS_TO_U64(size); i++) {
288 ret = do_read_u64(ff, p + i);
289 if (ret < 0) {
290 free(set);
291 return ret;
292 }
293 }
294
295 *pset = set;
296 *psize = size;
297 return 0;
298}
299
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700300static int write_tracing_data(struct feat_fd *ff,
301 struct perf_evlist *evlist)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200302{
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700303 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
304 return -1;
305
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700306 return read_tracing_data(ff->fd, &evlist->entries);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200307}
308
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700309static int write_build_id(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300310 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200311{
312 struct perf_session *session;
313 int err;
314
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700315 session = container_of(ff->ph, struct perf_session, header);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200316
Robert Richtere20960c2011-12-07 10:02:55 +0100317 if (!perf_session__read_build_ids(session, true))
318 return -1;
319
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700320 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
321 return -1;
322
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700323 err = perf_session__write_buildid_table(session, ff);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200324 if (err < 0) {
325 pr_debug("failed to write buildid table\n");
326 return err;
327 }
Namhyung Kim73c5d222014-11-07 22:57:56 +0900328 perf_session__cache_build_ids(session);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200329
330 return 0;
331}
332
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700333static int write_hostname(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300334 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200335{
336 struct utsname uts;
337 int ret;
338
339 ret = uname(&uts);
340 if (ret < 0)
341 return -1;
342
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700343 return do_write_string(ff, uts.nodename);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200344}
345
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700346static int write_osrelease(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300347 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200348{
349 struct utsname uts;
350 int ret;
351
352 ret = uname(&uts);
353 if (ret < 0)
354 return -1;
355
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700356 return do_write_string(ff, uts.release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200357}
358
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700359static int write_arch(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300360 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200361{
362 struct utsname uts;
363 int ret;
364
365 ret = uname(&uts);
366 if (ret < 0)
367 return -1;
368
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700369 return do_write_string(ff, uts.machine);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200370}
371
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700372static int write_version(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300373 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200374{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700375 return do_write_string(ff, perf_version_string);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200376}
377
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700378static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200379{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200380 FILE *file;
381 char *buf = NULL;
382 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800383 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200384 size_t len = 0;
385 int ret = -1;
386
387 if (!search)
388 return -1;
389
390 file = fopen("/proc/cpuinfo", "r");
391 if (!file)
392 return -1;
393
394 while (getline(&buf, &len, file) > 0) {
395 ret = strncmp(buf, search, strlen(search));
396 if (!ret)
397 break;
398 }
399
Wang Naned307752014-10-16 11:08:29 +0800400 if (ret) {
401 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200402 goto done;
Wang Naned307752014-10-16 11:08:29 +0800403 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200404
405 s = buf;
406
407 p = strchr(buf, ':');
408 if (p && *(p+1) == ' ' && *(p+2))
409 s = p + 2;
410 p = strchr(s, '\n');
411 if (p)
412 *p = '\0';
413
414 /* squash extra space characters (branding string) */
415 p = s;
416 while (*p) {
417 if (isspace(*p)) {
418 char *r = p + 1;
419 char *q = r;
420 *p = ' ';
421 while (*q && isspace(*q))
422 q++;
423 if (q != (p+1))
424 while ((*r++ = *q++));
425 }
426 p++;
427 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700428 ret = do_write_string(ff, s);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200429done:
430 free(buf);
431 fclose(file);
432 return ret;
433}
434
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700435static int write_cpudesc(struct feat_fd *ff,
Wang Nan493c3032014-10-24 09:45:26 +0800436 struct perf_evlist *evlist __maybe_unused)
437{
Wang Nan493c3032014-10-24 09:45:26 +0800438 const char *cpuinfo_procs[] = CPUINFO_PROC;
439 unsigned int i;
440
441 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
442 int ret;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700443 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
Wang Nan493c3032014-10-24 09:45:26 +0800444 if (ret >= 0)
445 return ret;
446 }
447 return -1;
448}
449
450
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700451static int write_nrcpus(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300452 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200453{
454 long nr;
455 u32 nrc, nra;
456 int ret;
457
Jan Stancekda8a58b2017-02-17 12:10:26 +0100458 nrc = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200459
460 nr = sysconf(_SC_NPROCESSORS_ONLN);
461 if (nr < 0)
462 return -1;
463
464 nra = (u32)(nr & UINT_MAX);
465
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700466 ret = do_write(ff, &nrc, sizeof(nrc));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200467 if (ret < 0)
468 return ret;
469
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700470 return do_write(ff, &nra, sizeof(nra));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200471}
472
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700473static int write_event_desc(struct feat_fd *ff,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200474 struct perf_evlist *evlist)
475{
Robert Richter6606f872012-08-16 21:10:19 +0200476 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900477 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200478 int ret;
479
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900480 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200481
482 /*
483 * write number of events
484 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700485 ret = do_write(ff, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200486 if (ret < 0)
487 return ret;
488
489 /*
490 * size of perf_event_attr struct
491 */
Robert Richter6606f872012-08-16 21:10:19 +0200492 sz = (u32)sizeof(evsel->attr);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700493 ret = do_write(ff, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200494 if (ret < 0)
495 return ret;
496
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300497 evlist__for_each_entry(evlist, evsel) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700498 ret = do_write(ff, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200499 if (ret < 0)
500 return ret;
501 /*
502 * write number of unique id per event
503 * there is one id per instance of an event
504 *
505 * copy into an nri to be independent of the
506 * type of ids,
507 */
Robert Richter6606f872012-08-16 21:10:19 +0200508 nri = evsel->ids;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700509 ret = do_write(ff, &nri, sizeof(nri));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200510 if (ret < 0)
511 return ret;
512
513 /*
514 * write event string as passed on cmdline
515 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700516 ret = do_write_string(ff, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200517 if (ret < 0)
518 return ret;
519 /*
520 * write unique ids for this event
521 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700522 ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200523 if (ret < 0)
524 return ret;
525 }
526 return 0;
527}
528
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700529static int write_cmdline(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300530 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200531{
Andi Kleen94816ad2019-02-24 07:37:19 -0800532 char pbuf[MAXPATHLEN], *buf;
533 int i, ret, n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200534
Tommi Rantala55f771282017-03-22 15:06:24 +0200535 /* actual path to perf binary */
Andi Kleen94816ad2019-02-24 07:37:19 -0800536 buf = perf_exe(pbuf, MAXPATHLEN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200537
538 /* account for binary path */
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300539 n = perf_env.nr_cmdline + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200540
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700541 ret = do_write(ff, &n, sizeof(n));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200542 if (ret < 0)
543 return ret;
544
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700545 ret = do_write_string(ff, buf);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200546 if (ret < 0)
547 return ret;
548
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300549 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700550 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200551 if (ret < 0)
552 return ret;
553 }
554 return 0;
555}
556
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200557
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700558static int write_cpu_topology(struct feat_fd *ff,
559 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200560{
Jiri Olsa5135d5e2019-02-19 10:58:13 +0100561 struct cpu_topology *tp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200562 u32 i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300563 int ret, j;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200564
Jiri Olsa5135d5e2019-02-19 10:58:13 +0100565 tp = cpu_topology__new();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200566 if (!tp)
567 return -1;
568
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700569 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200570 if (ret < 0)
571 goto done;
572
573 for (i = 0; i < tp->core_sib; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700574 ret = do_write_string(ff, tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200575 if (ret < 0)
576 goto done;
577 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700578 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200579 if (ret < 0)
580 goto done;
581
582 for (i = 0; i < tp->thread_sib; i++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700583 ret = do_write_string(ff, tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200584 if (ret < 0)
585 break;
586 }
Kan Liang2bb00d22015-09-01 09:58:12 -0400587
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300588 ret = perf_env__read_cpu_topology_map(&perf_env);
589 if (ret < 0)
590 goto done;
591
592 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700593 ret = do_write(ff, &perf_env.cpu[j].core_id,
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300594 sizeof(perf_env.cpu[j].core_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400595 if (ret < 0)
596 return ret;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700597 ret = do_write(ff, &perf_env.cpu[j].socket_id,
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300598 sizeof(perf_env.cpu[j].socket_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400599 if (ret < 0)
600 return ret;
601 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200602done:
Jiri Olsa5135d5e2019-02-19 10:58:13 +0100603 cpu_topology__delete(tp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200604 return ret;
605}
606
607
608
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700609static int write_total_mem(struct feat_fd *ff,
610 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200611{
612 char *buf = NULL;
613 FILE *fp;
614 size_t len = 0;
615 int ret = -1, n;
616 uint64_t mem;
617
618 fp = fopen("/proc/meminfo", "r");
619 if (!fp)
620 return -1;
621
622 while (getline(&buf, &len, fp) > 0) {
623 ret = strncmp(buf, "MemTotal:", 9);
624 if (!ret)
625 break;
626 }
627 if (!ret) {
628 n = sscanf(buf, "%*s %"PRIu64, &mem);
629 if (n == 1)
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700630 ret = do_write(ff, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800631 } else
632 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200633 free(buf);
634 fclose(fp);
635 return ret;
636}
637
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700638static int write_numa_topology(struct feat_fd *ff,
639 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200640{
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100641 struct numa_topology *tp;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200642 int ret = -1;
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100643 u32 i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200644
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100645 tp = numa_topology__new();
646 if (!tp)
647 return -ENOMEM;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200648
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100649 ret = do_write(ff, &tp->nr, sizeof(u32));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200650 if (ret < 0)
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100651 goto err;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200652
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100653 for (i = 0; i < tp->nr; i++) {
654 struct numa_topology_node *n = &tp->nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200655
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100656 ret = do_write(ff, &n->node, sizeof(u32));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200657 if (ret < 0)
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100658 goto err;
659
660 ret = do_write(ff, &n->mem_total, sizeof(u64));
661 if (ret)
662 goto err;
663
664 ret = do_write(ff, &n->mem_free, sizeof(u64));
665 if (ret)
666 goto err;
667
668 ret = do_write_string(ff, n->cpus);
669 if (ret < 0)
670 goto err;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200671 }
Jiri Olsa48e6c5a2019-02-19 10:58:14 +0100672
673 ret = 0;
674
675err:
676 numa_topology__delete(tp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200677 return ret;
678}
679
680/*
Robert Richter50a96672012-08-16 21:10:24 +0200681 * File format:
682 *
683 * struct pmu_mappings {
684 * u32 pmu_num;
685 * struct pmu_map {
686 * u32 type;
687 * char name[];
688 * }[pmu_num];
689 * };
690 */
691
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700692static int write_pmu_mappings(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300693 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +0200694{
695 struct perf_pmu *pmu = NULL;
David Carrillo-Cisnerosa02c3952017-07-17 21:25:44 -0700696 u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900697 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200698
David Carrillo-Cisnerosa02c3952017-07-17 21:25:44 -0700699 /*
700 * Do a first pass to count number of pmu to avoid lseek so this
701 * works in pipe mode as well.
702 */
703 while ((pmu = perf_pmu__scan(pmu))) {
704 if (!pmu->name)
705 continue;
706 pmu_num++;
707 }
708
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700709 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
Namhyung Kim5323f602012-12-17 15:38:54 +0900710 if (ret < 0)
711 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200712
713 while ((pmu = perf_pmu__scan(pmu))) {
714 if (!pmu->name)
715 continue;
Namhyung Kim5323f602012-12-17 15:38:54 +0900716
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700717 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
Namhyung Kim5323f602012-12-17 15:38:54 +0900718 if (ret < 0)
719 return ret;
720
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700721 ret = do_write_string(ff, pmu->name);
Namhyung Kim5323f602012-12-17 15:38:54 +0900722 if (ret < 0)
723 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200724 }
725
Robert Richter50a96672012-08-16 21:10:24 +0200726 return 0;
727}
728
729/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900730 * File format:
731 *
732 * struct group_descs {
733 * u32 nr_groups;
734 * struct group_desc {
735 * char name[];
736 * u32 leader_idx;
737 * u32 nr_members;
738 * }[nr_groups];
739 * };
740 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700741static int write_group_desc(struct feat_fd *ff,
Namhyung Kima8bb5592013-01-22 18:09:31 +0900742 struct perf_evlist *evlist)
743{
744 u32 nr_groups = evlist->nr_groups;
745 struct perf_evsel *evsel;
746 int ret;
747
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700748 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900749 if (ret < 0)
750 return ret;
751
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300752 evlist__for_each_entry(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900753 if (perf_evsel__is_group_leader(evsel) &&
754 evsel->nr_members > 1) {
755 const char *name = evsel->group_name ?: "{anon_group}";
756 u32 leader_idx = evsel->idx;
757 u32 nr_members = evsel->nr_members;
758
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700759 ret = do_write_string(ff, name);
Namhyung Kima8bb5592013-01-22 18:09:31 +0900760 if (ret < 0)
761 return ret;
762
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700763 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900764 if (ret < 0)
765 return ret;
766
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700767 ret = do_write(ff, &nr_members, sizeof(nr_members));
Namhyung Kima8bb5592013-01-22 18:09:31 +0900768 if (ret < 0)
769 return ret;
770 }
771 }
772 return 0;
773}
774
775/*
Kan Liangf4a07422018-11-21 08:49:39 -0800776 * Return the CPU id as a raw string.
777 *
778 * Each architecture should provide a more precise id string that
779 * can be use to match the architecture's "mapfile".
780 */
781char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused)
782{
783 return NULL;
784}
785
786/* Return zero when the cpuid from the mapfile.csv matches the
787 * cpuid string generated on this platform.
788 * Otherwise return non-zero.
789 */
790int __weak strcmp_cpuid_str(const char *mapcpuid, const char *cpuid)
791{
792 regex_t re;
793 regmatch_t pmatch[1];
794 int match;
795
796 if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) {
797 /* Warn unable to generate match particular string. */
798 pr_info("Invalid regular expression %s\n", mapcpuid);
799 return 1;
800 }
801
802 match = !regexec(&re, cpuid, 1, pmatch, 0);
803 regfree(&re);
804 if (match) {
805 size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so);
806
807 /* Verify the entire string matched. */
808 if (match_len == strlen(cpuid))
809 return 0;
810 }
811 return 1;
812}
813
814/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200815 * default get_cpuid(): nothing gets recorded
Jiada Wang7a759cd2017-04-09 20:02:37 -0700816 * actual implementation must be in arch/$(SRCARCH)/util/header.c
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200817 */
Rui Teng11d8f872016-07-28 10:05:57 +0800818int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200819{
820 return -1;
821}
822
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700823static int write_cpuid(struct feat_fd *ff,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300824 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200825{
826 char buffer[64];
827 int ret;
828
829 ret = get_cpuid(buffer, sizeof(buffer));
Jiri Olsaa9aeb872019-02-13 13:32:43 +0100830 if (ret)
831 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200832
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700833 return do_write_string(ff, buffer);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200834}
835
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700836static int write_branch_stack(struct feat_fd *ff __maybe_unused,
837 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +0100838{
839 return 0;
840}
841
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700842static int write_auxtrace(struct feat_fd *ff,
Adrian Hunter4025ea42015-04-09 18:53:41 +0300843 struct perf_evlist *evlist __maybe_unused)
844{
Adrian Hunter99fa2982015-04-30 17:37:25 +0300845 struct perf_session *session;
846 int err;
847
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -0700848 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
849 return -1;
850
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700851 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300852
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -0700853 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
Adrian Hunter99fa2982015-04-30 17:37:25 +0300854 if (err < 0)
855 pr_err("Failed to write auxtrace index\n");
856 return err;
Adrian Hunter4025ea42015-04-09 18:53:41 +0300857}
858
Alexey Budankovcf790512018-10-09 17:36:24 +0300859static int write_clockid(struct feat_fd *ff,
860 struct perf_evlist *evlist __maybe_unused)
861{
862 return do_write(ff, &ff->ph->env.clockid_res_ns,
863 sizeof(ff->ph->env.clockid_res_ns));
864}
865
Jiri Olsa258031c2019-03-08 14:47:39 +0100866static int write_dir_format(struct feat_fd *ff,
867 struct perf_evlist *evlist __maybe_unused)
868{
869 struct perf_session *session;
870 struct perf_data *data;
871
872 session = container_of(ff->ph, struct perf_session, header);
873 data = session->data;
874
875 if (WARN_ON(!perf_data__is_dir(data)))
876 return -1;
877
878 return do_write(ff, &data->dir.version, sizeof(data->dir.version));
879}
880
Song Liu606f9722019-03-11 22:30:43 -0700881#ifdef HAVE_LIBBPF_SUPPORT
882static int write_bpf_prog_info(struct feat_fd *ff,
883 struct perf_evlist *evlist __maybe_unused)
884{
885 struct perf_env *env = &ff->ph->env;
886 struct rb_root *root;
887 struct rb_node *next;
888 int ret;
889
890 down_read(&env->bpf_progs.lock);
891
892 ret = do_write(ff, &env->bpf_progs.infos_cnt,
893 sizeof(env->bpf_progs.infos_cnt));
894 if (ret < 0)
895 goto out;
896
897 root = &env->bpf_progs.infos;
898 next = rb_first(root);
899 while (next) {
900 struct bpf_prog_info_node *node;
901 size_t len;
902
903 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
904 next = rb_next(&node->rb_node);
905 len = sizeof(struct bpf_prog_info_linear) +
906 node->info_linear->data_len;
907
908 /* before writing to file, translate address to offset */
909 bpf_program__bpil_addr_to_offs(node->info_linear);
910 ret = do_write(ff, node->info_linear, len);
911 /*
912 * translate back to address even when do_write() fails,
913 * so that this function never changes the data.
914 */
915 bpf_program__bpil_offs_to_addr(node->info_linear);
916 if (ret < 0)
917 goto out;
918 }
919out:
920 up_read(&env->bpf_progs.lock);
921 return ret;
922}
923#else // HAVE_LIBBPF_SUPPORT
924static int write_bpf_prog_info(struct feat_fd *ff __maybe_unused,
925 struct perf_evlist *evlist __maybe_unused)
926{
927 return 0;
928}
929#endif // HAVE_LIBBPF_SUPPORT
930
Song Liua70a112312019-03-11 22:30:45 -0700931static int write_bpf_btf(struct feat_fd *ff,
932 struct perf_evlist *evlist __maybe_unused)
933{
934 struct perf_env *env = &ff->ph->env;
935 struct rb_root *root;
936 struct rb_node *next;
937 int ret;
938
939 down_read(&env->bpf_progs.lock);
940
941 ret = do_write(ff, &env->bpf_progs.btfs_cnt,
942 sizeof(env->bpf_progs.btfs_cnt));
943
944 if (ret < 0)
945 goto out;
946
947 root = &env->bpf_progs.btfs;
948 next = rb_first(root);
949 while (next) {
950 struct btf_node *node;
951
952 node = rb_entry(next, struct btf_node, rb_node);
953 next = rb_next(&node->rb_node);
954 ret = do_write(ff, &node->id,
955 sizeof(u32) * 2 + node->data_size);
956 if (ret < 0)
957 goto out;
958 }
959out:
960 up_read(&env->bpf_progs.lock);
961 return ret;
962}
963
Jiri Olsa720e98b2016-02-16 16:01:43 +0100964static int cpu_cache_level__sort(const void *a, const void *b)
965{
966 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
967 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
968
969 return cache_a->level - cache_b->level;
970}
971
972static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
973{
974 if (a->level != b->level)
975 return false;
976
977 if (a->line_size != b->line_size)
978 return false;
979
980 if (a->sets != b->sets)
981 return false;
982
983 if (a->ways != b->ways)
984 return false;
985
986 if (strcmp(a->type, b->type))
987 return false;
988
989 if (strcmp(a->size, b->size))
990 return false;
991
992 if (strcmp(a->map, b->map))
993 return false;
994
995 return true;
996}
997
998static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
999{
1000 char path[PATH_MAX], file[PATH_MAX];
1001 struct stat st;
1002 size_t len;
1003
1004 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1005 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1006
1007 if (stat(file, &st))
1008 return 1;
1009
1010 scnprintf(file, PATH_MAX, "%s/level", path);
1011 if (sysfs__read_int(file, (int *) &cache->level))
1012 return -1;
1013
1014 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1015 if (sysfs__read_int(file, (int *) &cache->line_size))
1016 return -1;
1017
1018 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1019 if (sysfs__read_int(file, (int *) &cache->sets))
1020 return -1;
1021
1022 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1023 if (sysfs__read_int(file, (int *) &cache->ways))
1024 return -1;
1025
1026 scnprintf(file, PATH_MAX, "%s/type", path);
1027 if (sysfs__read_str(file, &cache->type, &len))
1028 return -1;
1029
1030 cache->type[len] = 0;
1031 cache->type = rtrim(cache->type);
1032
1033 scnprintf(file, PATH_MAX, "%s/size", path);
1034 if (sysfs__read_str(file, &cache->size, &len)) {
1035 free(cache->type);
1036 return -1;
1037 }
1038
1039 cache->size[len] = 0;
1040 cache->size = rtrim(cache->size);
1041
1042 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1043 if (sysfs__read_str(file, &cache->map, &len)) {
1044 free(cache->map);
1045 free(cache->type);
1046 return -1;
1047 }
1048
1049 cache->map[len] = 0;
1050 cache->map = rtrim(cache->map);
1051 return 0;
1052}
1053
1054static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1055{
1056 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1057}
1058
1059static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1060{
1061 u32 i, cnt = 0;
1062 long ncpus;
1063 u32 nr, cpu;
1064 u16 level;
1065
1066 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1067 if (ncpus < 0)
1068 return -1;
1069
1070 nr = (u32)(ncpus & UINT_MAX);
1071
1072 for (cpu = 0; cpu < nr; cpu++) {
1073 for (level = 0; level < 10; level++) {
1074 struct cpu_cache_level c;
1075 int err;
1076
1077 err = cpu_cache_level__read(&c, cpu, level);
1078 if (err < 0)
1079 return err;
1080
1081 if (err == 1)
1082 break;
1083
1084 for (i = 0; i < cnt; i++) {
1085 if (cpu_cache_level__cmp(&c, &caches[i]))
1086 break;
1087 }
1088
1089 if (i == cnt)
1090 caches[cnt++] = c;
1091 else
1092 cpu_cache_level__free(&c);
1093
1094 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1095 goto out;
1096 }
1097 }
1098 out:
1099 *cntp = cnt;
1100 return 0;
1101}
1102
1103#define MAX_CACHES 2000
1104
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001105static int write_cache(struct feat_fd *ff,
1106 struct perf_evlist *evlist __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001107{
1108 struct cpu_cache_level caches[MAX_CACHES];
1109 u32 cnt = 0, i, version = 1;
1110 int ret;
1111
1112 ret = build_caches(caches, MAX_CACHES, &cnt);
1113 if (ret)
1114 goto out;
1115
1116 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1117
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001118 ret = do_write(ff, &version, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001119 if (ret < 0)
1120 goto out;
1121
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001122 ret = do_write(ff, &cnt, sizeof(u32));
Jiri Olsa720e98b2016-02-16 16:01:43 +01001123 if (ret < 0)
1124 goto out;
1125
1126 for (i = 0; i < cnt; i++) {
1127 struct cpu_cache_level *c = &caches[i];
1128
1129 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001130 ret = do_write(ff, &c->v, sizeof(u32)); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001131 if (ret < 0) \
1132 goto out;
1133
1134 _W(level)
1135 _W(line_size)
1136 _W(sets)
1137 _W(ways)
1138 #undef _W
1139
1140 #define _W(v) \
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001141 ret = do_write_string(ff, (const char *) c->v); \
Jiri Olsa720e98b2016-02-16 16:01:43 +01001142 if (ret < 0) \
1143 goto out;
1144
1145 _W(type)
1146 _W(size)
1147 _W(map)
1148 #undef _W
1149 }
1150
1151out:
1152 for (i = 0; i < cnt; i++)
1153 cpu_cache_level__free(&caches[i]);
1154 return ret;
1155}
1156
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07001157static int write_stat(struct feat_fd *ff __maybe_unused,
Jiri Olsaffa517a2015-10-25 15:51:43 +01001158 struct perf_evlist *evlist __maybe_unused)
1159{
1160 return 0;
1161}
1162
Jin Yao60115182017-12-08 21:13:41 +08001163static int write_sample_time(struct feat_fd *ff,
1164 struct perf_evlist *evlist)
1165{
1166 int ret;
1167
1168 ret = do_write(ff, &evlist->first_sample_time,
1169 sizeof(evlist->first_sample_time));
1170 if (ret < 0)
1171 return ret;
1172
1173 return do_write(ff, &evlist->last_sample_time,
1174 sizeof(evlist->last_sample_time));
1175}
1176
Jiri Olsae2091ce2018-03-07 16:50:08 +01001177
1178static int memory_node__read(struct memory_node *n, unsigned long idx)
1179{
1180 unsigned int phys, size = 0;
1181 char path[PATH_MAX];
1182 struct dirent *ent;
1183 DIR *dir;
1184
1185#define for_each_memory(mem, dir) \
1186 while ((ent = readdir(dir))) \
1187 if (strcmp(ent->d_name, ".") && \
1188 strcmp(ent->d_name, "..") && \
1189 sscanf(ent->d_name, "memory%u", &mem) == 1)
1190
1191 scnprintf(path, PATH_MAX,
1192 "%s/devices/system/node/node%lu",
1193 sysfs__mountpoint(), idx);
1194
1195 dir = opendir(path);
1196 if (!dir) {
1197 pr_warning("failed: cant' open memory sysfs data\n");
1198 return -1;
1199 }
1200
1201 for_each_memory(phys, dir) {
1202 size = max(phys, size);
1203 }
1204
1205 size++;
1206
1207 n->set = bitmap_alloc(size);
1208 if (!n->set) {
1209 closedir(dir);
1210 return -ENOMEM;
1211 }
1212
Jiri Olsae2091ce2018-03-07 16:50:08 +01001213 n->node = idx;
1214 n->size = size;
1215
1216 rewinddir(dir);
1217
1218 for_each_memory(phys, dir) {
1219 set_bit(phys, n->set);
1220 }
1221
1222 closedir(dir);
1223 return 0;
1224}
1225
1226static int memory_node__sort(const void *a, const void *b)
1227{
1228 const struct memory_node *na = a;
1229 const struct memory_node *nb = b;
1230
1231 return na->node - nb->node;
1232}
1233
1234static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp)
1235{
1236 char path[PATH_MAX];
1237 struct dirent *ent;
1238 DIR *dir;
1239 u64 cnt = 0;
1240 int ret = 0;
1241
1242 scnprintf(path, PATH_MAX, "%s/devices/system/node/",
1243 sysfs__mountpoint());
1244
1245 dir = opendir(path);
1246 if (!dir) {
Thomas Richter4f75f1cb2018-04-12 15:32:46 +02001247 pr_debug2("%s: could't read %s, does this arch have topology information?\n",
1248 __func__, path);
Jiri Olsae2091ce2018-03-07 16:50:08 +01001249 return -1;
1250 }
1251
1252 while (!ret && (ent = readdir(dir))) {
1253 unsigned int idx;
1254 int r;
1255
1256 if (!strcmp(ent->d_name, ".") ||
1257 !strcmp(ent->d_name, ".."))
1258 continue;
1259
1260 r = sscanf(ent->d_name, "node%u", &idx);
1261 if (r != 1)
1262 continue;
1263
1264 if (WARN_ONCE(cnt >= size,
1265 "failed to write MEM_TOPOLOGY, way too many nodes\n"))
1266 return -1;
1267
1268 ret = memory_node__read(&nodes[cnt++], idx);
1269 }
1270
1271 *cntp = cnt;
1272 closedir(dir);
1273
1274 if (!ret)
1275 qsort(nodes, cnt, sizeof(nodes[0]), memory_node__sort);
1276
1277 return ret;
1278}
1279
1280#define MAX_MEMORY_NODES 2000
1281
1282/*
1283 * The MEM_TOPOLOGY holds physical memory map for every
1284 * node in system. The format of data is as follows:
1285 *
1286 * 0 - version | for future changes
1287 * 8 - block_size_bytes | /sys/devices/system/memory/block_size_bytes
1288 * 16 - count | number of nodes
1289 *
1290 * For each node we store map of physical indexes for
1291 * each node:
1292 *
1293 * 32 - node id | node index
1294 * 40 - size | size of bitmap
1295 * 48 - bitmap | bitmap of memory indexes that belongs to node
1296 */
1297static int write_mem_topology(struct feat_fd *ff __maybe_unused,
1298 struct perf_evlist *evlist __maybe_unused)
1299{
1300 static struct memory_node nodes[MAX_MEMORY_NODES];
1301 u64 bsize, version = 1, i, nr;
1302 int ret;
1303
1304 ret = sysfs__read_xll("devices/system/memory/block_size_bytes",
1305 (unsigned long long *) &bsize);
1306 if (ret)
1307 return ret;
1308
1309 ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr);
1310 if (ret)
1311 return ret;
1312
1313 ret = do_write(ff, &version, sizeof(version));
1314 if (ret < 0)
1315 goto out;
1316
1317 ret = do_write(ff, &bsize, sizeof(bsize));
1318 if (ret < 0)
1319 goto out;
1320
1321 ret = do_write(ff, &nr, sizeof(nr));
1322 if (ret < 0)
1323 goto out;
1324
1325 for (i = 0; i < nr; i++) {
1326 struct memory_node *n = &nodes[i];
1327
1328 #define _W(v) \
1329 ret = do_write(ff, &n->v, sizeof(n->v)); \
1330 if (ret < 0) \
1331 goto out;
1332
1333 _W(node)
1334 _W(size)
1335
1336 #undef _W
1337
1338 ret = do_write_bitmap(ff, n->set, n->size);
1339 if (ret < 0)
1340 goto out;
1341 }
1342
1343out:
1344 return ret;
1345}
1346
Alexey Budankov42e1fd82019-03-18 20:41:33 +03001347static int write_compressed(struct feat_fd *ff __maybe_unused,
1348 struct perf_evlist *evlist __maybe_unused)
1349{
1350 int ret;
1351
1352 ret = do_write(ff, &(ff->ph->env.comp_ver), sizeof(ff->ph->env.comp_ver));
1353 if (ret)
1354 return ret;
1355
1356 ret = do_write(ff, &(ff->ph->env.comp_type), sizeof(ff->ph->env.comp_type));
1357 if (ret)
1358 return ret;
1359
1360 ret = do_write(ff, &(ff->ph->env.comp_level), sizeof(ff->ph->env.comp_level));
1361 if (ret)
1362 return ret;
1363
1364 ret = do_write(ff, &(ff->ph->env.comp_ratio), sizeof(ff->ph->env.comp_ratio));
1365 if (ret)
1366 return ret;
1367
1368 return do_write(ff, &(ff->ph->env.comp_mmap_len), sizeof(ff->ph->env.comp_mmap_len));
1369}
1370
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001371static void print_hostname(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001372{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001373 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001374}
1375
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001376static void print_osrelease(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001377{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001378 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001379}
1380
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001381static void print_arch(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001382{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001383 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001384}
1385
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001386static void print_cpudesc(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001387{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001388 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001389}
1390
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001391static void print_nrcpus(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001392{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001393 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1394 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001395}
1396
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001397static void print_version(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001398{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001399 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001400}
1401
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001402static void print_cmdline(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001403{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001404 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001405
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001406 nr = ff->ph->env.nr_cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001407
1408 fprintf(fp, "# cmdline : ");
1409
Alexey Budankovf92da712018-06-04 09:50:56 +03001410 for (i = 0; i < nr; i++) {
1411 char *argv_i = strdup(ff->ph->env.cmdline_argv[i]);
1412 if (!argv_i) {
1413 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1414 } else {
1415 char *mem = argv_i;
1416 do {
1417 char *quote = strchr(argv_i, '\'');
1418 if (!quote)
1419 break;
1420 *quote++ = '\0';
1421 fprintf(fp, "%s\\\'", argv_i);
1422 argv_i = quote;
1423 } while (1);
1424 fprintf(fp, "%s ", argv_i);
1425 free(mem);
1426 }
1427 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001428 fputc('\n', fp);
1429}
1430
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001431static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001432{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001433 struct perf_header *ph = ff->ph;
1434 int cpu_nr = ph->env.nr_cpus_avail;
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001435 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001436 char *str;
1437
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001438 nr = ph->env.nr_sibling_cores;
1439 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001440
1441 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001442 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001443 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001444 }
1445
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001446 nr = ph->env.nr_sibling_threads;
1447 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001448
1449 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001450 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001451 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001452 }
Kan Liang2bb00d22015-09-01 09:58:12 -04001453
1454 if (ph->env.cpu != NULL) {
1455 for (i = 0; i < cpu_nr; i++)
1456 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1457 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1458 } else
1459 fprintf(fp, "# Core ID and Socket ID information is not available\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001460}
1461
Alexey Budankovcf790512018-10-09 17:36:24 +03001462static void print_clockid(struct feat_fd *ff, FILE *fp)
1463{
1464 fprintf(fp, "# clockid frequency: %"PRIu64" MHz\n",
1465 ff->ph->env.clockid_res_ns * 1000);
1466}
1467
Jiri Olsa258031c2019-03-08 14:47:39 +01001468static void print_dir_format(struct feat_fd *ff, FILE *fp)
1469{
1470 struct perf_session *session;
1471 struct perf_data *data;
1472
1473 session = container_of(ff->ph, struct perf_session, header);
1474 data = session->data;
1475
1476 fprintf(fp, "# directory data version : %"PRIu64"\n", data->dir.version);
1477}
1478
Song Liu606f9722019-03-11 22:30:43 -07001479static void print_bpf_prog_info(struct feat_fd *ff, FILE *fp)
1480{
1481 struct perf_env *env = &ff->ph->env;
1482 struct rb_root *root;
1483 struct rb_node *next;
1484
1485 down_read(&env->bpf_progs.lock);
1486
1487 root = &env->bpf_progs.infos;
1488 next = rb_first(root);
1489
1490 while (next) {
1491 struct bpf_prog_info_node *node;
1492
1493 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
1494 next = rb_next(&node->rb_node);
Song Liuf8dfeae2019-03-19 09:54:54 -07001495
1496 bpf_event__print_bpf_prog_info(&node->info_linear->info,
1497 env, fp);
Song Liu606f9722019-03-11 22:30:43 -07001498 }
1499
1500 up_read(&env->bpf_progs.lock);
1501}
1502
Song Liua70a112312019-03-11 22:30:45 -07001503static void print_bpf_btf(struct feat_fd *ff, FILE *fp)
1504{
1505 struct perf_env *env = &ff->ph->env;
1506 struct rb_root *root;
1507 struct rb_node *next;
1508
1509 down_read(&env->bpf_progs.lock);
1510
1511 root = &env->bpf_progs.btfs;
1512 next = rb_first(root);
1513
1514 while (next) {
1515 struct btf_node *node;
1516
1517 node = rb_entry(next, struct btf_node, rb_node);
1518 next = rb_next(&node->rb_node);
1519 fprintf(fp, "# btf info of id %u\n", node->id);
1520 }
1521
1522 up_read(&env->bpf_progs.lock);
1523}
1524
Robert Richter4e1b9c62012-08-16 21:10:22 +02001525static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001526{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001527 struct perf_evsel *evsel;
1528
1529 if (!events)
1530 return;
1531
1532 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001533 zfree(&evsel->name);
1534 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001535 }
1536
1537 free(events);
1538}
1539
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001540static struct perf_evsel *read_event_desc(struct feat_fd *ff)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001541{
1542 struct perf_evsel *evsel, *events = NULL;
1543 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001544 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001545 u32 nre, sz, nr, i, j;
Stephane Eranian62db9062012-02-09 23:21:07 +01001546 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001547
1548 /* number of events */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001549 if (do_read_u32(ff, &nre))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001550 goto error;
1551
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001552 if (do_read_u32(ff, &sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001553 goto error;
1554
Stephane Eranian62db9062012-02-09 23:21:07 +01001555 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001556 buf = malloc(sz);
1557 if (!buf)
1558 goto error;
1559
Robert Richter4e1b9c62012-08-16 21:10:22 +02001560 /* the last event terminates with evsel->attr.size == 0: */
1561 events = calloc(nre + 1, sizeof(*events));
1562 if (!events)
1563 goto error;
1564
1565 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001566 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001567 msz = sz;
1568
Robert Richter4e1b9c62012-08-16 21:10:22 +02001569 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1570 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001571
Stephane Eranian62db9062012-02-09 23:21:07 +01001572 /*
1573 * must read entire on-file attr struct to
1574 * sync up with layout.
1575 */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001576 if (__do_read(ff, buf, sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001577 goto error;
1578
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001579 if (ff->ph->needs_swap)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001580 perf_event__attr_swap(buf);
1581
Robert Richter4e1b9c62012-08-16 21:10:22 +02001582 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001583
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001584 if (do_read_u32(ff, &nr))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001585 goto error;
1586
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001587 if (ff->ph->needs_swap)
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001588 evsel->needs_swap = true;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001589
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001590 evsel->name = do_read_string(ff);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001591 if (!evsel->name)
1592 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001593
1594 if (!nr)
1595 continue;
1596
1597 id = calloc(nr, sizeof(*id));
1598 if (!id)
1599 goto error;
1600 evsel->ids = nr;
1601 evsel->id = id;
1602
1603 for (j = 0 ; j < nr; j++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001604 if (do_read_u64(ff, id))
Robert Richter4e1b9c62012-08-16 21:10:22 +02001605 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001606 id++;
1607 }
1608 }
1609out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001610 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001611 return events;
1612error:
Markus Elfring4cc97612015-06-25 17:12:32 +02001613 free_event_desc(events);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001614 events = NULL;
1615 goto out;
1616}
1617
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001618static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -03001619 void *priv __maybe_unused)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001620{
1621 return fprintf(fp, ", %s = %s", name, val);
1622}
1623
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001624static void print_event_desc(struct feat_fd *ff, FILE *fp)
Robert Richter4e1b9c62012-08-16 21:10:22 +02001625{
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001626 struct perf_evsel *evsel, *events;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001627 u32 j;
1628 u64 *id;
1629
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001630 if (ff->events)
1631 events = ff->events;
1632 else
1633 events = read_event_desc(ff);
1634
Robert Richter4e1b9c62012-08-16 21:10:22 +02001635 if (!events) {
1636 fprintf(fp, "# event desc: not available or unable to read\n");
1637 return;
1638 }
1639
1640 for (evsel = events; evsel->attr.size; evsel++) {
1641 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001642
Robert Richter4e1b9c62012-08-16 21:10:22 +02001643 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001644 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001645 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1646 if (j)
1647 fputc(',', fp);
1648 fprintf(fp, " %"PRIu64, *id);
1649 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001650 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001651 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001652
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001653 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001654
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001655 fputc('\n', fp);
1656 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001657
1658 free_event_desc(events);
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07001659 ff->events = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001660}
1661
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001662static void print_total_mem(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001663{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001664 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001665}
1666
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001667static void print_numa_topology(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001668{
Jiri Olsac60da222016-07-04 14:16:20 +02001669 int i;
1670 struct numa_node *n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001671
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001672 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1673 n = &ff->ph->env.numa_nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001674
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001675 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1676 " free = %"PRIu64" kB\n",
Jiri Olsac60da222016-07-04 14:16:20 +02001677 n->node, n->mem_total, n->mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001678
Jiri Olsac60da222016-07-04 14:16:20 +02001679 fprintf(fp, "# node%u cpu list : ", n->node);
1680 cpu_map__fprintf(n->map, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001681 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001682}
1683
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001684static void print_cpuid(struct feat_fd *ff, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001685{
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001686 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001687}
1688
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001689static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001690{
1691 fprintf(fp, "# contains samples with branch stack\n");
1692}
1693
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001694static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
Adrian Hunter4025ea42015-04-09 18:53:41 +03001695{
1696 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1697}
1698
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001699static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
Jiri Olsaffa517a2015-10-25 15:51:43 +01001700{
1701 fprintf(fp, "# contains stat data\n");
1702}
1703
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001704static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01001705{
1706 int i;
1707
1708 fprintf(fp, "# CPU cache info:\n");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001709 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
Jiri Olsa720e98b2016-02-16 16:01:43 +01001710 fprintf(fp, "# ");
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001711 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
Jiri Olsa720e98b2016-02-16 16:01:43 +01001712 }
1713}
1714
Alexey Budankov42e1fd82019-03-18 20:41:33 +03001715static void print_compressed(struct feat_fd *ff, FILE *fp)
1716{
1717 fprintf(fp, "# compressed : %s, level = %d, ratio = %d\n",
1718 ff->ph->env.comp_type == PERF_COMP_ZSTD ? "Zstd" : "Unknown",
1719 ff->ph->env.comp_level, ff->ph->env.comp_ratio);
1720}
1721
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001722static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001723{
1724 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001725 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001726 u32 pmu_num;
1727 u32 type;
1728
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001729 pmu_num = ff->ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001730 if (!pmu_num) {
1731 fprintf(fp, "# pmu mappings: not available\n");
1732 return;
1733 }
1734
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001735 str = ff->ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001736
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001737 while (pmu_num) {
1738 type = strtoul(str, &tmp, 0);
1739 if (*tmp != ':')
1740 goto error;
1741
1742 str = tmp + 1;
1743 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1744
Robert Richter50a96672012-08-16 21:10:24 +02001745 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001746 str += strlen(str) + 1;
1747 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001748 }
1749
1750 fprintf(fp, "\n");
1751
1752 if (!pmu_num)
1753 return;
1754error:
1755 fprintf(fp, "# pmu mappings: unable to read\n");
1756}
1757
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001758static void print_group_desc(struct feat_fd *ff, FILE *fp)
Namhyung Kima8bb5592013-01-22 18:09:31 +09001759{
1760 struct perf_session *session;
1761 struct perf_evsel *evsel;
1762 u32 nr = 0;
1763
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07001764 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09001765
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001766 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001767 if (perf_evsel__is_group_leader(evsel) &&
1768 evsel->nr_members > 1) {
1769 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1770 perf_evsel__name(evsel));
1771
1772 nr = evsel->nr_members - 1;
1773 } else if (nr) {
1774 fprintf(fp, ",%s", perf_evsel__name(evsel));
1775
1776 if (--nr == 0)
1777 fprintf(fp, "}\n");
1778 }
1779 }
1780}
1781
Jin Yao60115182017-12-08 21:13:41 +08001782static void print_sample_time(struct feat_fd *ff, FILE *fp)
1783{
1784 struct perf_session *session;
1785 char time_buf[32];
1786 double d;
1787
1788 session = container_of(ff->ph, struct perf_session, header);
1789
1790 timestamp__scnprintf_usec(session->evlist->first_sample_time,
1791 time_buf, sizeof(time_buf));
1792 fprintf(fp, "# time of first sample : %s\n", time_buf);
1793
1794 timestamp__scnprintf_usec(session->evlist->last_sample_time,
1795 time_buf, sizeof(time_buf));
1796 fprintf(fp, "# time of last sample : %s\n", time_buf);
1797
1798 d = (double)(session->evlist->last_sample_time -
1799 session->evlist->first_sample_time) / NSEC_PER_MSEC;
1800
1801 fprintf(fp, "# sample duration : %10.3f ms\n", d);
1802}
1803
Jiri Olsae2091ce2018-03-07 16:50:08 +01001804static void memory_node__fprintf(struct memory_node *n,
1805 unsigned long long bsize, FILE *fp)
1806{
1807 char buf_map[100], buf_size[50];
1808 unsigned long long size;
1809
1810 size = bsize * bitmap_weight(n->set, n->size);
1811 unit_number__scnprintf(buf_size, 50, size);
1812
1813 bitmap_scnprintf(n->set, n->size, buf_map, 100);
1814 fprintf(fp, "# %3" PRIu64 " [%s]: %s\n", n->node, buf_size, buf_map);
1815}
1816
1817static void print_mem_topology(struct feat_fd *ff, FILE *fp)
1818{
1819 struct memory_node *nodes;
1820 int i, nr;
1821
1822 nodes = ff->ph->env.memory_nodes;
1823 nr = ff->ph->env.nr_memory_nodes;
1824
1825 fprintf(fp, "# memory nodes (nr %d, block size 0x%llx):\n",
1826 nr, ff->ph->env.memory_bsize);
1827
1828 for (i = 0; i < nr; i++) {
1829 memory_node__fprintf(&nodes[i], ff->ph->env.memory_bsize, fp);
1830 }
1831}
1832
Robert Richter08d95bd2012-02-10 15:41:55 +01001833static int __event_process_build_id(struct build_id_event *bev,
1834 char *filename,
1835 struct perf_session *session)
1836{
1837 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01001838 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00001839 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01001840 struct dso *dso;
1841 enum dso_kernel_type dso_type;
1842
1843 machine = perf_session__findnew_machine(session, bev->pid);
1844 if (!machine)
1845 goto out;
1846
Wang Nan1f121b02015-06-03 08:52:21 +00001847 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01001848
Wang Nan1f121b02015-06-03 08:52:21 +00001849 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01001850 case PERF_RECORD_MISC_KERNEL:
1851 dso_type = DSO_TYPE_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001852 break;
1853 case PERF_RECORD_MISC_GUEST_KERNEL:
1854 dso_type = DSO_TYPE_GUEST_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001855 break;
1856 case PERF_RECORD_MISC_USER:
1857 case PERF_RECORD_MISC_GUEST_USER:
1858 dso_type = DSO_TYPE_USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01001859 break;
1860 default:
1861 goto out;
1862 }
1863
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001864 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001865 if (dso != NULL) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001866 char sbuild_id[SBUILD_ID_SIZE];
Robert Richter08d95bd2012-02-10 15:41:55 +01001867
1868 dso__set_build_id(dso, &bev->build_id);
1869
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001870 if (dso_type != DSO_TYPE_USER) {
1871 struct kmod_path m = { .name = NULL, };
1872
1873 if (!kmod_path__parse_name(&m, filename) && m.kmod)
Namhyung Kim6b335e82017-05-31 21:01:04 +09001874 dso__set_module_info(dso, &m, machine);
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001875 else
1876 dso->kernel = dso_type;
1877
1878 free(m.name);
1879 }
Robert Richter08d95bd2012-02-10 15:41:55 +01001880
1881 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1882 sbuild_id);
1883 pr_debug("build id event received for %s: %s\n",
1884 dso->long_name, sbuild_id);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001885 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01001886 }
1887
1888 err = 0;
1889out:
1890 return err;
1891}
1892
1893static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1894 int input, u64 offset, u64 size)
1895{
1896 struct perf_session *session = container_of(header, struct perf_session, header);
1897 struct {
1898 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001899 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001900 char filename[0];
1901 } old_bev;
1902 struct build_id_event bev;
1903 char filename[PATH_MAX];
1904 u64 limit = offset + size;
1905
1906 while (offset < limit) {
1907 ssize_t len;
1908
Namhyung Kim5323f602012-12-17 15:38:54 +09001909 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001910 return -1;
1911
1912 if (header->needs_swap)
1913 perf_event_header__bswap(&old_bev.header);
1914
1915 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001916 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001917 return -1;
1918
1919 bev.header = old_bev.header;
1920
1921 /*
1922 * As the pid is the missing value, we need to fill
1923 * it properly. The header.misc value give us nice hint.
1924 */
1925 bev.pid = HOST_KERNEL_ID;
1926 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1927 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1928 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1929
1930 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1931 __event_process_build_id(&bev, filename, session);
1932
1933 offset += bev.header.size;
1934 }
1935
1936 return 0;
1937}
1938
1939static int perf_header__read_build_ids(struct perf_header *header,
1940 int input, u64 offset, u64 size)
1941{
1942 struct perf_session *session = container_of(header, struct perf_session, header);
1943 struct build_id_event bev;
1944 char filename[PATH_MAX];
1945 u64 limit = offset + size, orig_offset = offset;
1946 int err = -1;
1947
1948 while (offset < limit) {
1949 ssize_t len;
1950
Namhyung Kim5323f602012-12-17 15:38:54 +09001951 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001952 goto out;
1953
1954 if (header->needs_swap)
1955 perf_event_header__bswap(&bev.header);
1956
1957 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001958 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001959 goto out;
1960 /*
1961 * The a1645ce1 changeset:
1962 *
1963 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1964 *
1965 * Added a field to struct build_id_event that broke the file
1966 * format.
1967 *
1968 * Since the kernel build-id is the first entry, process the
1969 * table using the old format if the well known
1970 * '[kernel.kallsyms]' string for the kernel build-id has the
1971 * first 4 characters chopped off (where the pid_t sits).
1972 */
1973 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1974 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1975 return -1;
1976 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1977 }
1978
1979 __event_process_build_id(&bev, filename, session);
1980
1981 offset += bev.header.size;
1982 }
1983 err = 0;
1984out:
1985 return err;
1986}
1987
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001988/* Macro for features that simply need to read and store a string. */
1989#define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07001990static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001991{\
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07001992 ff->ph->env.__feat_env = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07001993 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001994}
1995
1996FEAT_PROCESS_STR_FUN(hostname, hostname);
1997FEAT_PROCESS_STR_FUN(osrelease, os_release);
1998FEAT_PROCESS_STR_FUN(version, version);
1999FEAT_PROCESS_STR_FUN(arch, arch);
2000FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
2001FEAT_PROCESS_STR_FUN(cpuid, cpuid);
2002
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002003static int process_tracing_data(struct feat_fd *ff, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01002004{
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002005 ssize_t ret = trace_report(ff->fd, data, false);
2006
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002007 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01002008}
2009
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002010static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01002011{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002012 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
Robert Richterf1c67db2012-02-10 15:41:56 +01002013 pr_debug("Failed to read buildids, continuing...\n");
2014 return 0;
2015}
2016
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002017static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002018{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002019 int ret;
2020 u32 nr_cpus_avail, nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002021
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002022 ret = do_read_u32(ff, &nr_cpus_avail);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002023 if (ret)
2024 return ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002025
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002026 ret = do_read_u32(ff, &nr_cpus_online);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002027 if (ret)
2028 return ret;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002029 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
2030 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002031 return 0;
2032}
2033
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002034static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002035{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002036 u64 total_mem;
2037 int ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002038
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002039 ret = do_read_u64(ff, &total_mem);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002040 if (ret)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002041 return -1;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002042 ff->ph->env.total_mem = (unsigned long long)total_mem;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002043 return 0;
2044}
2045
Robert Richter7c2f7af2012-08-16 21:10:23 +02002046static struct perf_evsel *
2047perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
2048{
2049 struct perf_evsel *evsel;
2050
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002051 evlist__for_each_entry(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02002052 if (evsel->idx == idx)
2053 return evsel;
2054 }
2055
2056 return NULL;
2057}
2058
2059static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002060perf_evlist__set_event_name(struct perf_evlist *evlist,
2061 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02002062{
2063 struct perf_evsel *evsel;
2064
2065 if (!event->name)
2066 return;
2067
2068 evsel = perf_evlist__find_by_index(evlist, event->idx);
2069 if (!evsel)
2070 return;
2071
2072 if (evsel->name)
2073 return;
2074
2075 evsel->name = strdup(event->name);
2076}
2077
2078static int
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002079process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02002080{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002081 struct perf_session *session;
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002082 struct perf_evsel *evsel, *events = read_event_desc(ff);
Robert Richter7c2f7af2012-08-16 21:10:23 +02002083
2084 if (!events)
2085 return 0;
2086
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002087 session = container_of(ff->ph, struct perf_session, header);
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07002088
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002089 if (session->data->is_pipe) {
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07002090 /* Save events for reading later by print_event_desc,
2091 * since they can't be read again in pipe mode. */
2092 ff->events = events;
2093 }
2094
Robert Richter7c2f7af2012-08-16 21:10:23 +02002095 for (evsel = events; evsel->attr.size; evsel++)
2096 perf_evlist__set_event_name(session->evlist, evsel);
2097
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002098 if (!session->data->is_pipe)
David Carrillo-Cisnerosf9ebdcc2017-07-17 21:25:49 -07002099 free_event_desc(events);
Robert Richter7c2f7af2012-08-16 21:10:23 +02002100
2101 return 0;
2102}
2103
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002104static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002105{
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002106 char *str, *cmdline = NULL, **argv = NULL;
2107 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002108
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002109 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002110 return -1;
2111
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002112 ff->ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002113
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002114 cmdline = zalloc(ff->size + nr + 1);
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002115 if (!cmdline)
2116 return -1;
2117
2118 argv = zalloc(sizeof(char *) * (nr + 1));
2119 if (!argv)
2120 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002121
2122 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002123 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002124 if (!str)
2125 goto error;
2126
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002127 argv[i] = cmdline + len;
2128 memcpy(argv[i], str, strlen(str) + 1);
2129 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002130 free(str);
2131 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002132 ff->ph->env.cmdline = cmdline;
2133 ff->ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002134 return 0;
2135
2136error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02002137 free(argv);
2138 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002139 return -1;
2140}
2141
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002142static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002143{
Namhyung Kima1ae5652012-09-24 17:14:59 +09002144 u32 nr, i;
2145 char *str;
2146 struct strbuf sb;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002147 int cpu_nr = ff->ph->env.nr_cpus_avail;
Kan Liang2bb00d22015-09-01 09:58:12 -04002148 u64 size = 0;
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002149 struct perf_header *ph = ff->ph;
Thomas Richter01766222018-06-11 09:31:52 +02002150 bool do_core_id_test = true;
Kan Liang2bb00d22015-09-01 09:58:12 -04002151
2152 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
2153 if (!ph->env.cpu)
2154 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002155
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002156 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04002157 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002158
Namhyung Kima1ae5652012-09-24 17:14:59 +09002159 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04002160 size += sizeof(u32);
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002161 if (strbuf_init(&sb, 128) < 0)
2162 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002163
2164 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002165 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002166 if (!str)
2167 goto error;
2168
2169 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002170 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2171 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04002172 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002173 free(str);
2174 }
2175 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
2176
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002177 if (do_read_u32(ff, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002178 return -1;
2179
Namhyung Kima1ae5652012-09-24 17:14:59 +09002180 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04002181 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002182
2183 for (i = 0; i < nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002184 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002185 if (!str)
2186 goto error;
2187
2188 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002189 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
2190 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04002191 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002192 free(str);
2193 }
2194 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04002195
2196 /*
2197 * The header may be from old perf,
2198 * which doesn't include core id and socket id information.
2199 */
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002200 if (ff->size <= size) {
Kan Liang2bb00d22015-09-01 09:58:12 -04002201 zfree(&ph->env.cpu);
2202 return 0;
2203 }
2204
Thomas Richter01766222018-06-11 09:31:52 +02002205 /* On s390 the socket_id number is not related to the numbers of cpus.
2206 * The socket_id number might be higher than the numbers of cpus.
2207 * This depends on the configuration.
2208 */
2209 if (ph->env.arch && !strncmp(ph->env.arch, "s390", 4))
2210 do_core_id_test = false;
2211
Kan Liang2bb00d22015-09-01 09:58:12 -04002212 for (i = 0; i < (u32)cpu_nr; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002213 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04002214 goto free_cpu;
2215
Kan Liang2bb00d22015-09-01 09:58:12 -04002216 ph->env.cpu[i].core_id = nr;
2217
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002218 if (do_read_u32(ff, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04002219 goto free_cpu;
2220
Thomas Richter01766222018-06-11 09:31:52 +02002221 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
Kan Liang2bb00d22015-09-01 09:58:12 -04002222 pr_debug("socket_id number is too big."
2223 "You may need to upgrade the perf tool.\n");
2224 goto free_cpu;
2225 }
2226
2227 ph->env.cpu[i].socket_id = nr;
2228 }
2229
Namhyung Kima1ae5652012-09-24 17:14:59 +09002230 return 0;
2231
2232error:
2233 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04002234free_cpu:
2235 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002236 return -1;
2237}
2238
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002239static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002240{
Jiri Olsac60da222016-07-04 14:16:20 +02002241 struct numa_node *nodes, *n;
Jiri Olsac60da222016-07-04 14:16:20 +02002242 u32 nr, i;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002243 char *str;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002244
2245 /* nr nodes */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002246 if (do_read_u32(ff, &nr))
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002247 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002248
Jiri Olsac60da222016-07-04 14:16:20 +02002249 nodes = zalloc(sizeof(*nodes) * nr);
2250 if (!nodes)
2251 return -ENOMEM;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002252
2253 for (i = 0; i < nr; i++) {
Jiri Olsac60da222016-07-04 14:16:20 +02002254 n = &nodes[i];
2255
Namhyung Kima1ae5652012-09-24 17:14:59 +09002256 /* node number */
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002257 if (do_read_u32(ff, &n->node))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002258 goto error;
2259
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002260 if (do_read_u64(ff, &n->mem_total))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002261 goto error;
2262
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002263 if (do_read_u64(ff, &n->mem_free))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002264 goto error;
2265
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002266 str = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002267 if (!str)
2268 goto error;
2269
Jiri Olsac60da222016-07-04 14:16:20 +02002270 n->map = cpu_map__new(str);
2271 if (!n->map)
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002272 goto error;
Jiri Olsac60da222016-07-04 14:16:20 +02002273
Namhyung Kima1ae5652012-09-24 17:14:59 +09002274 free(str);
2275 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002276 ff->ph->env.nr_numa_nodes = nr;
2277 ff->ph->env.numa_nodes = nodes;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002278 return 0;
2279
2280error:
Jiri Olsac60da222016-07-04 14:16:20 +02002281 free(nodes);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002282 return -1;
2283}
2284
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002285static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09002286{
Namhyung Kima1ae5652012-09-24 17:14:59 +09002287 char *name;
2288 u32 pmu_num;
2289 u32 type;
2290 struct strbuf sb;
2291
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002292 if (do_read_u32(ff, &pmu_num))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002293 return -1;
2294
Namhyung Kima1ae5652012-09-24 17:14:59 +09002295 if (!pmu_num) {
2296 pr_debug("pmu mappings not available\n");
2297 return 0;
2298 }
2299
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002300 ff->ph->env.nr_pmu_mappings = pmu_num;
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002301 if (strbuf_init(&sb, 128) < 0)
2302 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002303
2304 while (pmu_num) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002305 if (do_read_u32(ff, &type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09002306 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002307
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002308 name = do_read_string(ff);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002309 if (!name)
2310 goto error;
2311
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002312 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
2313 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002314 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09002315 if (strbuf_add(&sb, "", 1) < 0)
2316 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09002317
Kan Liange0838e02015-09-10 11:03:05 -03002318 if (!strcmp(name, "msr"))
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002319 ff->ph->env.msr_pmu_type = type;
Kan Liange0838e02015-09-10 11:03:05 -03002320
Namhyung Kima1ae5652012-09-24 17:14:59 +09002321 free(name);
2322 pmu_num--;
2323 }
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002324 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
Namhyung Kima1ae5652012-09-24 17:14:59 +09002325 return 0;
2326
2327error:
2328 strbuf_release(&sb);
2329 return -1;
2330}
2331
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002332static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
Namhyung Kima8bb5592013-01-22 18:09:31 +09002333{
2334 size_t ret = -1;
2335 u32 i, nr, nr_groups;
2336 struct perf_session *session;
2337 struct perf_evsel *evsel, *leader = NULL;
2338 struct group_desc {
2339 char *name;
2340 u32 leader_idx;
2341 u32 nr_members;
2342 } *desc;
2343
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002344 if (do_read_u32(ff, &nr_groups))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002345 return -1;
2346
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002347 ff->ph->env.nr_groups = nr_groups;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002348 if (!nr_groups) {
2349 pr_debug("group desc not available\n");
2350 return 0;
2351 }
2352
2353 desc = calloc(nr_groups, sizeof(*desc));
2354 if (!desc)
2355 return -1;
2356
2357 for (i = 0; i < nr_groups; i++) {
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002358 desc[i].name = do_read_string(ff);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002359 if (!desc[i].name)
2360 goto out_free;
2361
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002362 if (do_read_u32(ff, &desc[i].leader_idx))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002363 goto out_free;
2364
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002365 if (do_read_u32(ff, &desc[i].nr_members))
Namhyung Kima8bb5592013-01-22 18:09:31 +09002366 goto out_free;
Namhyung Kima8bb5592013-01-22 18:09:31 +09002367 }
2368
2369 /*
2370 * Rebuild group relationship based on the group_desc
2371 */
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002372 session = container_of(ff->ph, struct perf_session, header);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002373 session->evlist->nr_groups = nr_groups;
2374
2375 i = nr = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002376 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002377 if (evsel->idx == (int) desc[i].leader_idx) {
2378 evsel->leader = evsel;
2379 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09002380 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002381 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09002382 desc[i].name = NULL;
2383 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09002384 evsel->nr_members = desc[i].nr_members;
2385
2386 if (i >= nr_groups || nr > 0) {
2387 pr_debug("invalid group desc\n");
2388 goto out_free;
2389 }
2390
2391 leader = evsel;
2392 nr = evsel->nr_members - 1;
2393 i++;
2394 } else if (nr) {
2395 /* This is a group member */
2396 evsel->leader = leader;
2397
2398 nr--;
2399 }
2400 }
2401
2402 if (i != nr_groups || nr != 0) {
2403 pr_debug("invalid group desc\n");
2404 goto out_free;
2405 }
2406
2407 ret = 0;
2408out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002409 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002410 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002411 free(desc);
2412
2413 return ret;
2414}
2415
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002416static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
Adrian Hunter99fa2982015-04-30 17:37:25 +03002417{
2418 struct perf_session *session;
2419 int err;
2420
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002421 session = container_of(ff->ph, struct perf_session, header);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002422
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002423 err = auxtrace_index__process(ff->fd, ff->size, session,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002424 ff->ph->needs_swap);
Adrian Hunter99fa2982015-04-30 17:37:25 +03002425 if (err < 0)
2426 pr_err("Failed to process auxtrace index\n");
2427 return err;
2428}
2429
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002430static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
Jiri Olsa720e98b2016-02-16 16:01:43 +01002431{
2432 struct cpu_cache_level *caches;
2433 u32 cnt, i, version;
2434
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002435 if (do_read_u32(ff, &version))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002436 return -1;
2437
Jiri Olsa720e98b2016-02-16 16:01:43 +01002438 if (version != 1)
2439 return -1;
2440
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002441 if (do_read_u32(ff, &cnt))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002442 return -1;
2443
Jiri Olsa720e98b2016-02-16 16:01:43 +01002444 caches = zalloc(sizeof(*caches) * cnt);
2445 if (!caches)
2446 return -1;
2447
2448 for (i = 0; i < cnt; i++) {
2449 struct cpu_cache_level c;
2450
2451 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002452 if (do_read_u32(ff, &c.v))\
Jiri Olsa720e98b2016-02-16 16:01:43 +01002453 goto out_free_caches; \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002454
2455 _R(level)
2456 _R(line_size)
2457 _R(sets)
2458 _R(ways)
2459 #undef _R
2460
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002461 #define _R(v) \
David Carrillo-Cisneros48e5fce2017-07-17 21:25:43 -07002462 c.v = do_read_string(ff); \
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002463 if (!c.v) \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002464 goto out_free_caches;
2465
2466 _R(type)
2467 _R(size)
2468 _R(map)
2469 #undef _R
2470
2471 caches[i] = c;
2472 }
2473
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07002474 ff->ph->env.caches = caches;
2475 ff->ph->env.caches_cnt = cnt;
Jiri Olsa720e98b2016-02-16 16:01:43 +01002476 return 0;
2477out_free_caches:
2478 free(caches);
2479 return -1;
2480}
2481
Jin Yao60115182017-12-08 21:13:41 +08002482static int process_sample_time(struct feat_fd *ff, void *data __maybe_unused)
2483{
2484 struct perf_session *session;
2485 u64 first_sample_time, last_sample_time;
2486 int ret;
2487
2488 session = container_of(ff->ph, struct perf_session, header);
2489
2490 ret = do_read_u64(ff, &first_sample_time);
2491 if (ret)
2492 return -1;
2493
2494 ret = do_read_u64(ff, &last_sample_time);
2495 if (ret)
2496 return -1;
2497
2498 session->evlist->first_sample_time = first_sample_time;
2499 session->evlist->last_sample_time = last_sample_time;
2500 return 0;
2501}
2502
Jiri Olsae2091ce2018-03-07 16:50:08 +01002503static int process_mem_topology(struct feat_fd *ff,
2504 void *data __maybe_unused)
2505{
2506 struct memory_node *nodes;
2507 u64 version, i, nr, bsize;
2508 int ret = -1;
2509
2510 if (do_read_u64(ff, &version))
2511 return -1;
2512
2513 if (version != 1)
2514 return -1;
2515
2516 if (do_read_u64(ff, &bsize))
2517 return -1;
2518
2519 if (do_read_u64(ff, &nr))
2520 return -1;
2521
2522 nodes = zalloc(sizeof(*nodes) * nr);
2523 if (!nodes)
2524 return -1;
2525
2526 for (i = 0; i < nr; i++) {
2527 struct memory_node n;
2528
2529 #define _R(v) \
2530 if (do_read_u64(ff, &n.v)) \
2531 goto out; \
2532
2533 _R(node)
2534 _R(size)
2535
2536 #undef _R
2537
2538 if (do_read_bitmap(ff, &n.set, &n.size))
2539 goto out;
2540
2541 nodes[i] = n;
2542 }
2543
2544 ff->ph->env.memory_bsize = bsize;
2545 ff->ph->env.memory_nodes = nodes;
2546 ff->ph->env.nr_memory_nodes = nr;
2547 ret = 0;
2548
2549out:
2550 if (ret)
2551 free(nodes);
2552 return ret;
2553}
2554
Alexey Budankovcf790512018-10-09 17:36:24 +03002555static int process_clockid(struct feat_fd *ff,
2556 void *data __maybe_unused)
2557{
2558 if (do_read_u64(ff, &ff->ph->env.clockid_res_ns))
2559 return -1;
2560
2561 return 0;
2562}
2563
Jiri Olsa258031c2019-03-08 14:47:39 +01002564static int process_dir_format(struct feat_fd *ff,
2565 void *_data __maybe_unused)
2566{
2567 struct perf_session *session;
2568 struct perf_data *data;
2569
2570 session = container_of(ff->ph, struct perf_session, header);
2571 data = session->data;
2572
2573 if (WARN_ON(!perf_data__is_dir(data)))
2574 return -1;
2575
2576 return do_read_u64(ff, &data->dir.version);
2577}
2578
Song Liu606f9722019-03-11 22:30:43 -07002579#ifdef HAVE_LIBBPF_SUPPORT
2580static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)
2581{
2582 struct bpf_prog_info_linear *info_linear;
2583 struct bpf_prog_info_node *info_node;
2584 struct perf_env *env = &ff->ph->env;
2585 u32 count, i;
2586 int err = -1;
2587
2588 if (ff->ph->needs_swap) {
2589 pr_warning("interpreting bpf_prog_info from systems with endianity is not yet supported\n");
2590 return 0;
2591 }
2592
2593 if (do_read_u32(ff, &count))
2594 return -1;
2595
2596 down_write(&env->bpf_progs.lock);
2597
2598 for (i = 0; i < count; ++i) {
2599 u32 info_len, data_len;
2600
2601 info_linear = NULL;
2602 info_node = NULL;
2603 if (do_read_u32(ff, &info_len))
2604 goto out;
2605 if (do_read_u32(ff, &data_len))
2606 goto out;
2607
2608 if (info_len > sizeof(struct bpf_prog_info)) {
2609 pr_warning("detected invalid bpf_prog_info\n");
2610 goto out;
2611 }
2612
2613 info_linear = malloc(sizeof(struct bpf_prog_info_linear) +
2614 data_len);
2615 if (!info_linear)
2616 goto out;
2617 info_linear->info_len = sizeof(struct bpf_prog_info);
2618 info_linear->data_len = data_len;
2619 if (do_read_u64(ff, (u64 *)(&info_linear->arrays)))
2620 goto out;
2621 if (__do_read(ff, &info_linear->info, info_len))
2622 goto out;
2623 if (info_len < sizeof(struct bpf_prog_info))
2624 memset(((void *)(&info_linear->info)) + info_len, 0,
2625 sizeof(struct bpf_prog_info) - info_len);
2626
2627 if (__do_read(ff, info_linear->data, data_len))
2628 goto out;
2629
2630 info_node = malloc(sizeof(struct bpf_prog_info_node));
2631 if (!info_node)
2632 goto out;
2633
2634 /* after reading from file, translate offset to address */
2635 bpf_program__bpil_offs_to_addr(info_linear);
2636 info_node->info_linear = info_linear;
2637 perf_env__insert_bpf_prog_info(env, info_node);
2638 }
2639
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002640 up_write(&env->bpf_progs.lock);
Song Liu606f9722019-03-11 22:30:43 -07002641 return 0;
2642out:
2643 free(info_linear);
2644 free(info_node);
2645 up_write(&env->bpf_progs.lock);
2646 return err;
2647}
2648#else // HAVE_LIBBPF_SUPPORT
2649static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data __maybe_unused)
2650{
2651 return 0;
2652}
2653#endif // HAVE_LIBBPF_SUPPORT
2654
Song Liua70a112312019-03-11 22:30:45 -07002655static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)
2656{
2657 struct perf_env *env = &ff->ph->env;
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002658 struct btf_node *node = NULL;
Song Liua70a112312019-03-11 22:30:45 -07002659 u32 count, i;
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002660 int err = -1;
Song Liua70a112312019-03-11 22:30:45 -07002661
2662 if (ff->ph->needs_swap) {
2663 pr_warning("interpreting btf from systems with endianity is not yet supported\n");
2664 return 0;
2665 }
2666
2667 if (do_read_u32(ff, &count))
2668 return -1;
2669
2670 down_write(&env->bpf_progs.lock);
2671
2672 for (i = 0; i < count; ++i) {
Song Liua70a112312019-03-11 22:30:45 -07002673 u32 id, data_size;
2674
2675 if (do_read_u32(ff, &id))
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002676 goto out;
Song Liua70a112312019-03-11 22:30:45 -07002677 if (do_read_u32(ff, &data_size))
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002678 goto out;
Song Liua70a112312019-03-11 22:30:45 -07002679
2680 node = malloc(sizeof(struct btf_node) + data_size);
2681 if (!node)
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002682 goto out;
Song Liua70a112312019-03-11 22:30:45 -07002683
2684 node->id = id;
2685 node->data_size = data_size;
2686
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002687 if (__do_read(ff, node->data, data_size))
2688 goto out;
Song Liua70a112312019-03-11 22:30:45 -07002689
2690 perf_env__insert_btf(env, node);
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002691 node = NULL;
Song Liua70a112312019-03-11 22:30:45 -07002692 }
2693
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002694 err = 0;
2695out:
Song Liua70a112312019-03-11 22:30:45 -07002696 up_write(&env->bpf_progs.lock);
Gustavo A. R. Silva14c9b312019-04-08 12:33:55 -05002697 free(node);
2698 return err;
Song Liua70a112312019-03-11 22:30:45 -07002699}
2700
Alexey Budankov42e1fd82019-03-18 20:41:33 +03002701static int process_compressed(struct feat_fd *ff,
2702 void *data __maybe_unused)
2703{
2704 if (do_read_u32(ff, &(ff->ph->env.comp_ver)))
2705 return -1;
2706
2707 if (do_read_u32(ff, &(ff->ph->env.comp_type)))
2708 return -1;
2709
2710 if (do_read_u32(ff, &(ff->ph->env.comp_level)))
2711 return -1;
2712
2713 if (do_read_u32(ff, &(ff->ph->env.comp_ratio)))
2714 return -1;
2715
2716 if (do_read_u32(ff, &(ff->ph->env.comp_mmap_len)))
2717 return -1;
2718
2719 return 0;
2720}
2721
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002722struct feature_ops {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002723 int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002724 void (*print)(struct feat_fd *ff, FILE *fp);
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07002725 int (*process)(struct feat_fd *ff, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002726 const char *name;
2727 bool full_only;
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002728 bool synthesize;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002729};
2730
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002731#define FEAT_OPR(n, func, __full_only) \
2732 [HEADER_##n] = { \
2733 .name = __stringify(n), \
2734 .write = write_##func, \
2735 .print = print_##func, \
2736 .full_only = __full_only, \
2737 .process = process_##func, \
2738 .synthesize = true \
2739 }
2740
2741#define FEAT_OPN(n, func, __full_only) \
2742 [HEADER_##n] = { \
2743 .name = __stringify(n), \
2744 .write = write_##func, \
2745 .print = print_##func, \
2746 .full_only = __full_only, \
2747 .process = process_##func \
2748 }
Robert Richter8cdfa782011-12-07 10:02:56 +01002749
2750/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002751#define print_tracing_data NULL
2752#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002753
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002754#define process_branch_stack NULL
2755#define process_stat NULL
2756
2757
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002758static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002759 FEAT_OPN(TRACING_DATA, tracing_data, false),
2760 FEAT_OPN(BUILD_ID, build_id, false),
2761 FEAT_OPR(HOSTNAME, hostname, false),
2762 FEAT_OPR(OSRELEASE, osrelease, false),
2763 FEAT_OPR(VERSION, version, false),
2764 FEAT_OPR(ARCH, arch, false),
2765 FEAT_OPR(NRCPUS, nrcpus, false),
2766 FEAT_OPR(CPUDESC, cpudesc, false),
2767 FEAT_OPR(CPUID, cpuid, false),
2768 FEAT_OPR(TOTAL_MEM, total_mem, false),
2769 FEAT_OPR(EVENT_DESC, event_desc, false),
2770 FEAT_OPR(CMDLINE, cmdline, false),
2771 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
2772 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
2773 FEAT_OPN(BRANCH_STACK, branch_stack, false),
2774 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
Jiri Olsae8fedff2018-07-12 15:52:02 +02002775 FEAT_OPR(GROUP_DESC, group_desc, false),
David Carrillo-Cisnerosa4d8c982017-07-17 21:25:46 -07002776 FEAT_OPN(AUXTRACE, auxtrace, false),
2777 FEAT_OPN(STAT, stat, false),
2778 FEAT_OPN(CACHE, cache, true),
Jin Yao60115182017-12-08 21:13:41 +08002779 FEAT_OPR(SAMPLE_TIME, sample_time, false),
Jiri Olsae2091ce2018-03-07 16:50:08 +01002780 FEAT_OPR(MEM_TOPOLOGY, mem_topology, true),
Jiri Olsa258031c2019-03-08 14:47:39 +01002781 FEAT_OPR(CLOCKID, clockid, false),
Song Liu606f9722019-03-11 22:30:43 -07002782 FEAT_OPN(DIR_FORMAT, dir_format, false),
Song Liua70a112312019-03-11 22:30:45 -07002783 FEAT_OPR(BPF_PROG_INFO, bpf_prog_info, false),
2784 FEAT_OPR(BPF_BTF, bpf_btf, false),
Alexey Budankov42e1fd82019-03-18 20:41:33 +03002785 FEAT_OPR(COMPRESSED, compressed, false),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002786};
2787
2788struct header_print_data {
2789 FILE *fp;
2790 bool full; /* extended list of headers */
2791};
2792
2793static int perf_file_section__fprintf_info(struct perf_file_section *section,
2794 struct perf_header *ph,
2795 int feat, int fd, void *data)
2796{
2797 struct header_print_data *hd = data;
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002798 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002799
2800 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2801 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2802 "%d, continuing...\n", section->offset, feat);
2803 return 0;
2804 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002805 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002806 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002807 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002808 }
2809 if (!feat_ops[feat].print)
2810 return 0;
2811
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002812 ff = (struct feat_fd) {
2813 .fd = fd,
2814 .ph = ph,
2815 };
2816
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002817 if (!feat_ops[feat].full_only || hd->full)
David Carrillo-Cisneroscfc65422017-07-17 21:25:40 -07002818 feat_ops[feat].print(&ff, hd->fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002819 else
2820 fprintf(hd->fp, "# %s info available, use -I to display\n",
2821 feat_ops[feat].name);
2822
2823 return 0;
2824}
2825
2826int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2827{
2828 struct header_print_data hd;
2829 struct perf_header *header = &session->header;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002830 int fd = perf_data__fd(session->data);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002831 struct stat st;
Arnaldo Carvalho de Melo0afcf292018-12-11 16:11:54 -03002832 time_t stctime;
Jiri Olsaaabae162016-10-10 09:35:50 +02002833 int ret, bit;
Jiri Olsaf45f5612016-10-10 09:03:07 +02002834
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002835 hd.fp = fp;
2836 hd.full = full;
2837
Jiri Olsaf45f5612016-10-10 09:03:07 +02002838 ret = fstat(fd, &st);
2839 if (ret == -1)
2840 return -1;
2841
Arnaldo Carvalho de Melo0afcf292018-12-11 16:11:54 -03002842 stctime = st.st_ctime;
2843 fprintf(fp, "# captured on : %s", ctime(&stctime));
Jiri Olsae971a5a2018-03-07 16:50:03 +01002844
2845 fprintf(fp, "# header version : %u\n", header->version);
2846 fprintf(fp, "# data offset : %" PRIu64 "\n", header->data_offset);
2847 fprintf(fp, "# data size : %" PRIu64 "\n", header->data_size);
2848 fprintf(fp, "# feat offset : %" PRIu64 "\n", header->feat_offset);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002849
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002850 perf_header__process_sections(header, fd, &hd,
2851 perf_file_section__fprintf_info);
Jiri Olsaaabae162016-10-10 09:35:50 +02002852
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002853 if (session->data->is_pipe)
David Carrillo-Cisnerosc9d1c932017-04-10 13:14:32 -07002854 return 0;
2855
Jiri Olsaaabae162016-10-10 09:35:50 +02002856 fprintf(fp, "# missing features: ");
2857 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2858 if (bit)
2859 fprintf(fp, "%s ", feat_ops[bit].name);
2860 }
2861
2862 fprintf(fp, "\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002863 return 0;
2864}
2865
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002866static int do_write_feat(struct feat_fd *ff, int type,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002867 struct perf_file_section **p,
2868 struct perf_evlist *evlist)
2869{
2870 int err;
2871 int ret = 0;
2872
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002873 if (perf_header__has_feat(ff->ph, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002874 if (!feat_ops[type].write)
2875 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002876
David Carrillo-Cisneros0b3d3412017-07-17 21:25:45 -07002877 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
2878 return -1;
2879
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002880 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002881
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002882 err = feat_ops[type].write(ff, evlist);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002883 if (err < 0) {
Jiri Olsa0c2aff42016-10-10 09:38:02 +02002884 pr_debug("failed to write feature %s\n", feat_ops[type].name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002885
2886 /* undo anything written */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002887 lseek(ff->fd, (*p)->offset, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002888
2889 return -1;
2890 }
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002891 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002892 (*p)++;
2893 }
2894 return ret;
2895}
2896
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002897static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002898 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002899{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002900 int nr_sections;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002901 struct feat_fd ff;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002902 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002903 int sec_size;
2904 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002905 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002906 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002907
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002908 ff = (struct feat_fd){
2909 .fd = fd,
2910 .ph = header,
2911 };
2912
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002913 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002914 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002915 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002916
Paul Gortmaker91b98802013-01-30 20:05:49 -05002917 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002918 if (feat_sec == NULL)
2919 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002920
2921 sec_size = sizeof(*feat_sec) * nr_sections;
2922
Jiri Olsa8d541e92013-07-17 19:49:44 +02002923 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002924 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002925
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002926 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002927 if (do_write_feat(&ff, feat, &p, evlist))
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002928 perf_header__clear_feat(header, feat);
2929 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002930
Xiao Guangrongf887f302010-02-04 16:46:42 +08002931 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002932 /*
2933 * may write more than needed due to dropped feature, but
Ingo Molnaradba1632018-12-03 11:22:00 +01002934 * this is okay, reader will skip the missing entries
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002935 */
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002936 err = do_write(&ff, feat_sec, sec_size);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002937 if (err < 0)
2938 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002939 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002940 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002941}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002942
Tom Zanussi8dc58102010-04-01 23:59:15 -05002943int perf_header__write_pipe(int fd)
2944{
2945 struct perf_pipe_file_header f_header;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002946 struct feat_fd ff;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002947 int err;
2948
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002949 ff = (struct feat_fd){ .fd = fd };
2950
Tom Zanussi8dc58102010-04-01 23:59:15 -05002951 f_header = (struct perf_pipe_file_header){
2952 .magic = PERF_MAGIC,
2953 .size = sizeof(f_header),
2954 };
2955
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002956 err = do_write(&ff, &f_header, sizeof(f_header));
Tom Zanussi8dc58102010-04-01 23:59:15 -05002957 if (err < 0) {
2958 pr_debug("failed to write perf pipe header\n");
2959 return err;
2960 }
2961
2962 return 0;
2963}
2964
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002965int perf_session__write_header(struct perf_session *session,
2966 struct perf_evlist *evlist,
2967 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002968{
2969 struct perf_file_header f_header;
2970 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002971 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002972 struct perf_evsel *evsel;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002973 struct feat_fd ff;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002974 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002975 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002976
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002977 ff = (struct feat_fd){ .fd = fd};
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002978 lseek(fd, sizeof(f_header), SEEK_SET);
2979
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002980 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002981 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002982 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002983 if (err < 0) {
2984 pr_debug("failed to write perf header\n");
2985 return err;
2986 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002987 }
2988
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002989 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002990
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002991 evlist__for_each_entry(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002992 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002993 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002994 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002995 .offset = evsel->id_offset,
2996 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002997 }
2998 };
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07002999 err = do_write(&ff, &f_attr, sizeof(f_attr));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003000 if (err < 0) {
3001 pr_debug("failed to write perf header attribute\n");
3002 return err;
3003 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003004 }
3005
Adrian Hunterd645c442013-12-11 14:36:28 +02003006 if (!header->data_offset)
3007 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02003008 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003009
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003010 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003011 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003012 if (err < 0)
3013 return err;
3014 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003015
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003016 f_header = (struct perf_file_header){
3017 .magic = PERF_MAGIC,
3018 .size = sizeof(f_header),
3019 .attr_size = sizeof(f_attr),
3020 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02003021 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003022 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003023 },
3024 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003025 .offset = header->data_offset,
3026 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003027 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02003028 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003029 };
3030
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003031 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003032
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003033 lseek(fd, 0, SEEK_SET);
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003034 err = do_write(&ff, &f_header, sizeof(f_header));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003035 if (err < 0) {
3036 pr_debug("failed to write perf header\n");
3037 return err;
3038 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003039 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003040
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02003041 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003042}
3043
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003044static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003045 int fd, void *buf, size_t size)
3046{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02003047 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003048 return -1;
3049
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003050 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003051 mem_bswap_64(buf, size);
3052
3053 return 0;
3054}
3055
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003056int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003057 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003058 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003059 struct perf_header *ph,
3060 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003061{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003062 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003063 int nr_sections;
3064 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003065 int feat;
3066 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003067
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003068 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003069 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003070 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003071
Paul Gortmaker91b98802013-01-30 20:05:49 -05003072 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003073 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003074 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003075
3076 sec_size = sizeof(*feat_sec) * nr_sections;
3077
Jiri Olsa8d541e92013-07-17 19:49:44 +02003078 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003079
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003080 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
3081 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003082 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003083
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003084 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
3085 err = process(sec++, header, feat, fd, data);
3086 if (err < 0)
3087 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01003088 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003089 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003090out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01003091 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003092 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003093}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02003094
Stephane Eranian114382a2012-02-09 23:21:08 +01003095static const int attr_file_abi_sizes[] = {
3096 [0] = PERF_ATTR_SIZE_VER0,
3097 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02003098 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02003099 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02003100 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01003101 0,
3102};
3103
3104/*
3105 * In the legacy file format, the magic number is not used to encode endianness.
3106 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
3107 * on ABI revisions, we need to try all combinations for all endianness to
3108 * detect the endianness.
3109 */
3110static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
3111{
3112 uint64_t ref_size, attr_size;
3113 int i;
3114
3115 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
3116 ref_size = attr_file_abi_sizes[i]
3117 + sizeof(struct perf_file_section);
3118 if (hdr_sz != ref_size) {
3119 attr_size = bswap_64(hdr_sz);
3120 if (attr_size != ref_size)
3121 continue;
3122
3123 ph->needs_swap = true;
3124 }
3125 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
3126 i,
3127 ph->needs_swap);
3128 return 0;
3129 }
3130 /* could not determine endianness */
3131 return -1;
3132}
3133
3134#define PERF_PIPE_HDR_VER0 16
3135
3136static const size_t attr_pipe_abi_sizes[] = {
3137 [0] = PERF_PIPE_HDR_VER0,
3138 0,
3139};
3140
3141/*
3142 * In the legacy pipe format, there is an implicit assumption that endiannesss
3143 * between host recording the samples, and host parsing the samples is the
3144 * same. This is not always the case given that the pipe output may always be
3145 * redirected into a file and analyzed on a different machine with possibly a
3146 * different endianness and perf_event ABI revsions in the perf tool itself.
3147 */
3148static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
3149{
3150 u64 attr_size;
3151 int i;
3152
3153 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
3154 if (hdr_sz != attr_pipe_abi_sizes[i]) {
3155 attr_size = bswap_64(hdr_sz);
3156 if (attr_size != hdr_sz)
3157 continue;
3158
3159 ph->needs_swap = true;
3160 }
3161 pr_debug("Pipe ABI%d perf.data file detected\n", i);
3162 return 0;
3163 }
3164 return -1;
3165}
3166
Feng Tange84ba4e2012-10-30 11:56:07 +08003167bool is_perf_magic(u64 magic)
3168{
3169 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
3170 || magic == __perf_magic2
3171 || magic == __perf_magic2_sw)
3172 return true;
3173
3174 return false;
3175}
3176
Stephane Eranian114382a2012-02-09 23:21:08 +01003177static int check_magic_endian(u64 magic, uint64_t hdr_sz,
3178 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01003179{
3180 int ret;
3181
3182 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01003183 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01003184 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02003185 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01003186 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01003187 if (is_pipe)
3188 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01003189
Stephane Eranian114382a2012-02-09 23:21:08 +01003190 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01003191 }
Stephane Eranian114382a2012-02-09 23:21:08 +01003192 /*
3193 * the new magic number serves two purposes:
3194 * - unique number to identify actual perf.data files
3195 * - encode endianness of file
3196 */
Namhyung Kimf7913972015-01-29 17:06:45 +09003197 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01003198
Stephane Eranian114382a2012-02-09 23:21:08 +01003199 /* check magic number with one endianness */
3200 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01003201 return 0;
3202
Stephane Eranian114382a2012-02-09 23:21:08 +01003203 /* check magic number with opposite endianness */
3204 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01003205 return -1;
3206
3207 ph->needs_swap = true;
3208
3209 return 0;
3210}
3211
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003212int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003213 struct perf_header *ph, int fd)
3214{
Jiri Olsa727ebd52013-11-28 11:30:14 +01003215 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01003216
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003217 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003218
Stephane Eranian73323f52012-02-02 13:54:44 +01003219 ret = readn(fd, header, sizeof(*header));
3220 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003221 return -1;
3222
Stephane Eranian114382a2012-02-09 23:21:08 +01003223 if (check_magic_endian(header->magic,
3224 header->attr_size, false, ph) < 0) {
3225 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01003226 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01003227 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003228
Stephane Eranian73323f52012-02-02 13:54:44 +01003229 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003230 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01003231 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003232 }
3233
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003234 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003235 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003236 if (header->size == offsetof(typeof(*header), adds_features))
3237 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003238 else
3239 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06003240 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06003241 /*
3242 * feature bitmap is declared as an array of unsigned longs --
3243 * not good since its size can differ between the host that
3244 * generated the data file and the host analyzing the file.
3245 *
3246 * We need to handle endianness, but we don't know the size of
3247 * the unsigned long where the file was generated. Take a best
3248 * guess at determining it: try 64-bit swap first (ie., file
3249 * created on a 64-bit host), and check if the hostname feature
3250 * bit is set (this feature bit is forced on as of fbe96f2).
3251 * If the bit is not, undo the 64-bit swap and try a 32-bit
3252 * swap. If the hostname bit is still not set (e.g., older data
3253 * file), punt and fallback to the original behavior --
3254 * clearing all feature bits and setting buildid.
3255 */
David Ahern80c01202012-06-08 11:47:51 -03003256 mem_bswap_64(&header->adds_features,
3257 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06003258
3259 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03003260 /* unswap as u64 */
3261 mem_bswap_64(&header->adds_features,
3262 BITS_TO_U64(HEADER_FEAT_BITS));
3263
3264 /* unswap as u32 */
3265 mem_bswap_32(&header->adds_features,
3266 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06003267 }
3268
3269 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
3270 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
3271 set_bit(HEADER_BUILD_ID, header->adds_features);
3272 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003273 }
3274
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003275 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003276 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003277
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003278 ph->data_offset = header->data.offset;
3279 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02003280 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003281 return 0;
3282}
3283
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003284static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003285 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003286 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003287{
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07003288 struct feat_fd fdd = {
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07003289 .fd = fd,
3290 .ph = ph,
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07003291 .size = section->size,
3292 .offset = section->offset,
David Carrillo-Cisneros1a222752017-07-17 21:25:41 -07003293 };
3294
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003295 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02003296 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003297 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003298 return 0;
3299 }
3300
Robert Richterb1e5a9b2011-12-07 10:02:57 +01003301 if (feat >= HEADER_LAST_FEATURE) {
3302 pr_debug("unknown feature %d, continuing...\n", feat);
3303 return 0;
3304 }
3305
Robert Richterf1c67db2012-02-10 15:41:56 +01003306 if (!feat_ops[feat].process)
3307 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003308
David Carrillo-Cisneros62552452017-07-17 21:25:42 -07003309 return feat_ops[feat].process(&fdd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02003310}
3311
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003312static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05003313 struct perf_header *ph, int fd,
3314 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003315{
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003316 struct feat_fd ff = {
3317 .fd = STDOUT_FILENO,
3318 .ph = ph,
3319 };
Jiri Olsa727ebd52013-11-28 11:30:14 +01003320 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01003321
3322 ret = readn(fd, header, sizeof(*header));
3323 if (ret <= 0)
3324 return -1;
3325
Stephane Eranian114382a2012-02-09 23:21:08 +01003326 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
3327 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05003328 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01003329 }
3330
3331 if (ph->needs_swap)
3332 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05003333
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07003334 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05003335 return -1;
3336
Tom Zanussi8dc58102010-04-01 23:59:15 -05003337 return 0;
3338}
3339
Jiri Olsad4339562013-07-17 19:49:41 +02003340static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05003341{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003342 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05003343 struct perf_pipe_file_header f_header;
3344
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003345 if (perf_file_header__read_pipe(&f_header, header,
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003346 perf_data__fd(session->data),
Tom Zanussi454c4072010-05-01 01:41:20 -05003347 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05003348 pr_debug("incompatible file format\n");
3349 return -EINVAL;
3350 }
3351
Tom Zanussi8dc58102010-04-01 23:59:15 -05003352 return 0;
3353}
3354
Stephane Eranian69996df2012-02-09 23:21:06 +01003355static int read_attr(int fd, struct perf_header *ph,
3356 struct perf_file_attr *f_attr)
3357{
3358 struct perf_event_attr *attr = &f_attr->attr;
3359 size_t sz, left;
3360 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01003361 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01003362
3363 memset(f_attr, 0, sizeof(*f_attr));
3364
3365 /* read minimal guaranteed structure */
3366 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
3367 if (ret <= 0) {
3368 pr_debug("cannot read %d bytes of header attr\n",
3369 PERF_ATTR_SIZE_VER0);
3370 return -1;
3371 }
3372
3373 /* on file perf_event_attr size */
3374 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01003375
Stephane Eranian69996df2012-02-09 23:21:06 +01003376 if (ph->needs_swap)
3377 sz = bswap_32(sz);
3378
3379 if (sz == 0) {
3380 /* assume ABI0 */
3381 sz = PERF_ATTR_SIZE_VER0;
3382 } else if (sz > our_sz) {
3383 pr_debug("file uses a more recent and unsupported ABI"
3384 " (%zu bytes extra)\n", sz - our_sz);
3385 return -1;
3386 }
3387 /* what we have not yet read and that we know about */
3388 left = sz - PERF_ATTR_SIZE_VER0;
3389 if (left) {
3390 void *ptr = attr;
3391 ptr += PERF_ATTR_SIZE_VER0;
3392
3393 ret = readn(fd, ptr, left);
3394 }
3395 /* read perf_file_section, ids are read in caller */
3396 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
3397
3398 return ret <= 0 ? -1 : 0;
3399}
3400
Namhyung Kim831394b2012-09-06 11:10:46 +09003401static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003402 struct tep_handle *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003403{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003404 struct tep_event *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003405 char bf[128];
3406
Namhyung Kim831394b2012-09-06 11:10:46 +09003407 /* already prepared */
3408 if (evsel->tp_format)
3409 return 0;
3410
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09003411 if (pevent == NULL) {
3412 pr_debug("broken or missing trace data\n");
3413 return -1;
3414 }
3415
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04003416 event = tep_find_event(pevent, evsel->attr.config);
Namhyung Kima7619ae2013-04-18 21:24:16 +09003417 if (event == NULL) {
3418 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003419 return -1;
Namhyung Kima7619ae2013-04-18 21:24:16 +09003420 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003421
Namhyung Kim831394b2012-09-06 11:10:46 +09003422 if (!evsel->name) {
3423 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
3424 evsel->name = strdup(bf);
3425 if (evsel->name == NULL)
3426 return -1;
3427 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003428
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03003429 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003430 return 0;
3431}
3432
Namhyung Kim831394b2012-09-06 11:10:46 +09003433static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003434 struct tep_handle *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003435{
3436 struct perf_evsel *pos;
3437
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003438 evlist__for_each_entry(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09003439 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
3440 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003441 return -1;
3442 }
3443
3444 return 0;
3445}
3446
Jiri Olsad4339562013-07-17 19:49:41 +02003447int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05003448{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003449 struct perf_data *data = session->data;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003450 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003451 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003452 struct perf_file_attr f_attr;
3453 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003454 int nr_attrs, nr_ids, i, j;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003455 int fd = perf_data__fd(data);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003456
Namhyung Kim334fe7a2013-03-11 16:43:12 +09003457 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003458 if (session->evlist == NULL)
3459 return -ENOMEM;
3460
Kan Liang2c071442015-08-28 05:48:05 -04003461 session->evlist->env = &header->env;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -03003462 session->machines.host.env = &header->env;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003463 if (perf_data__is_pipe(data))
Jiri Olsad4339562013-07-17 19:49:41 +02003464 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05003465
Stephane Eranian69996df2012-02-09 23:21:06 +01003466 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02003467 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003468
Namhyung Kimb314e5c2013-09-30 17:19:48 +09003469 /*
3470 * Sanity check that perf.data was written cleanly; data size is
3471 * initialized to 0 and updated only if the on_exit function is run.
3472 * If data size is still 0 then the file contains only partial
3473 * information. Just warn user and process it as much as it can.
3474 */
3475 if (f_header.data.size == 0) {
3476 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
3477 "Was the 'perf record' command properly terminated?\n",
Jiri Olsaeae8ad82017-01-23 22:25:41 +01003478 data->file.path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09003479 }
3480
Stephane Eranian69996df2012-02-09 23:21:06 +01003481 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003482 lseek(fd, f_header.attrs.offset, SEEK_SET);
3483
3484 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003485 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02003486 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003487
Stephane Eranian69996df2012-02-09 23:21:06 +01003488 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003489 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02003490
David Ahern1060ab82015-04-09 16:15:46 -04003491 if (header->needs_swap) {
3492 f_attr.ids.size = bswap_64(f_attr.ids.size);
3493 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06003494 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04003495 }
David Aherneda39132011-07-15 12:34:09 -06003496
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02003497 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03003498 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003499
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003500 if (evsel == NULL)
3501 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03003502
3503 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003504 /*
3505 * Do it before so that if perf_evsel__alloc_id fails, this
3506 * entry gets purged too at perf_evlist__delete().
3507 */
3508 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003509
3510 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003511 /*
3512 * We don't have the cpu and thread maps on the header, so
3513 * for allocating the perf_sample_id table we fake 1 cpu and
3514 * hattr->ids threads.
3515 */
3516 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
3517 goto out_delete_evlist;
3518
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003519 lseek(fd, f_attr.ids.offset, SEEK_SET);
3520
3521 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03003522 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003523 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003524
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003525 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02003526 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02003527
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003528 lseek(fd, tmp, SEEK_SET);
3529 }
3530
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003531 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02003532 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01003533
Namhyung Kim831394b2012-09-06 11:10:46 +09003534 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003535 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03003536 goto out_delete_evlist;
3537
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02003538 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02003539out_errno:
3540 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003541
3542out_delete_evlist:
3543 perf_evlist__delete(session->evlist);
3544 session->evlist = NULL;
3545 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02003546}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02003547
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003548int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02003549 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003550 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02003551{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003552 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003553 size_t size;
3554 int err;
3555
3556 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003557 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003558 size += sizeof(struct perf_event_header);
3559 size += ids * sizeof(u64);
3560
3561 ev = malloc(size);
3562
Chris Samuelce47dc52010-11-13 13:35:06 +11003563 if (ev == NULL)
3564 return -ENOMEM;
3565
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003566 ev->attr.attr = *attr;
3567 memcpy(ev->attr.id, id, ids * sizeof(u64));
3568
3569 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02003570 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003571
Robert Richterf4d83432012-08-16 21:10:17 +02003572 if (ev->attr.header.size == size)
3573 err = process(tool, ev, NULL, NULL);
3574 else
3575 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003576
3577 free(ev);
3578
3579 return err;
3580}
3581
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003582int perf_event__synthesize_features(struct perf_tool *tool,
3583 struct perf_session *session,
3584 struct perf_evlist *evlist,
3585 perf_event__handler_t process)
3586{
3587 struct perf_header *header = &session->header;
3588 struct feat_fd ff;
3589 struct feature_event *fe;
3590 size_t sz, sz_hdr;
3591 int feat, ret;
3592
3593 sz_hdr = sizeof(fe->header);
3594 sz = sizeof(union perf_event);
3595 /* get a nice alignment */
3596 sz = PERF_ALIGN(sz, page_size);
3597
3598 memset(&ff, 0, sizeof(ff));
3599
3600 ff.buf = malloc(sz);
3601 if (!ff.buf)
3602 return -ENOMEM;
3603
3604 ff.size = sz - sz_hdr;
3605
3606 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3607 if (!feat_ops[feat].synthesize) {
3608 pr_debug("No record header feature for header :%d\n", feat);
3609 continue;
3610 }
3611
3612 ff.offset = sizeof(*fe);
3613
3614 ret = feat_ops[feat].write(&ff, evlist);
3615 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
3616 pr_debug("Error writing feature\n");
3617 continue;
3618 }
3619 /* ff.buf may have changed due to realloc in do_write() */
3620 fe = ff.buf;
3621 memset(fe, 0, sizeof(*fe));
3622
3623 fe->feat_id = feat;
3624 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3625 fe->header.size = ff.offset;
3626
3627 ret = process(tool, ff.buf, NULL, NULL);
3628 if (ret) {
3629 free(ff.buf);
3630 return ret;
3631 }
3632 }
Jiri Olsa57b5de42018-03-14 10:22:05 +01003633
3634 /* Send HEADER_LAST_FEATURE mark. */
3635 fe = ff.buf;
3636 fe->feat_id = HEADER_LAST_FEATURE;
3637 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3638 fe->header.size = sizeof(*fe);
3639
3640 ret = process(tool, ff.buf, NULL, NULL);
3641
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003642 free(ff.buf);
Jiri Olsa57b5de42018-03-14 10:22:05 +01003643 return ret;
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003644}
3645
Jiri Olsa89f16882018-09-13 14:54:03 +02003646int perf_event__process_feature(struct perf_session *session,
3647 union perf_event *event)
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003648{
Jiri Olsa89f16882018-09-13 14:54:03 +02003649 struct perf_tool *tool = session->tool;
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003650 struct feat_fd ff = { .fd = 0 };
3651 struct feature_event *fe = (struct feature_event *)event;
3652 int type = fe->header.type;
3653 u64 feat = fe->feat_id;
3654
3655 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3656 pr_warning("invalid record type %d in pipe-mode\n", type);
3657 return 0;
3658 }
Ravi Bangoria92ead7e2018-06-25 18:12:20 +05303659 if (feat == HEADER_RESERVED || feat >= HEADER_LAST_FEATURE) {
David Carrillo-Cisnerose9def1b2017-07-17 21:25:48 -07003660 pr_warning("invalid record type %d in pipe-mode\n", type);
3661 return -1;
3662 }
3663
3664 if (!feat_ops[feat].process)
3665 return 0;
3666
3667 ff.buf = (void *)fe->data;
3668 ff.size = event->header.size - sizeof(event->header);
3669 ff.ph = &session->header;
3670
3671 if (feat_ops[feat].process(&ff, NULL))
3672 return -1;
3673
3674 if (!feat_ops[feat].print || !tool->show_feat_hdr)
3675 return 0;
3676
3677 if (!feat_ops[feat].full_only ||
3678 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3679 feat_ops[feat].print(&ff, stdout);
3680 } else {
3681 fprintf(stdout, "# %s info available, use -I to display\n",
3682 feat_ops[feat].name);
3683 }
3684
3685 return 0;
3686}
3687
Jiri Olsaa6e52812015-10-25 15:51:37 +01003688static struct event_update_event *
3689event_update_event__new(size_t size, u64 type, u64 id)
3690{
3691 struct event_update_event *ev;
3692
3693 size += sizeof(*ev);
3694 size = PERF_ALIGN(size, sizeof(u64));
3695
3696 ev = zalloc(size);
3697 if (ev) {
3698 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3699 ev->header.size = (u16)size;
3700 ev->type = type;
3701 ev->id = id;
3702 }
3703 return ev;
3704}
3705
3706int
3707perf_event__synthesize_event_update_unit(struct perf_tool *tool,
3708 struct perf_evsel *evsel,
3709 perf_event__handler_t process)
3710{
3711 struct event_update_event *ev;
3712 size_t size = strlen(evsel->unit);
3713 int err;
3714
3715 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
3716 if (ev == NULL)
3717 return -ENOMEM;
3718
Arnaldo Carvalho de Melo75725882018-12-06 11:02:57 -03003719 strlcpy(ev->data, evsel->unit, size + 1);
Jiri Olsaa6e52812015-10-25 15:51:37 +01003720 err = process(tool, (union perf_event *)ev, NULL, NULL);
3721 free(ev);
3722 return err;
3723}
3724
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003725int
3726perf_event__synthesize_event_update_scale(struct perf_tool *tool,
3727 struct perf_evsel *evsel,
3728 perf_event__handler_t process)
3729{
3730 struct event_update_event *ev;
3731 struct event_update_event_scale *ev_data;
3732 int err;
3733
3734 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
3735 if (ev == NULL)
3736 return -ENOMEM;
3737
3738 ev_data = (struct event_update_event_scale *) ev->data;
3739 ev_data->scale = evsel->scale;
3740 err = process(tool, (union perf_event*) ev, NULL, NULL);
3741 free(ev);
3742 return err;
3743}
3744
Jiri Olsa802c9042015-10-25 15:51:39 +01003745int
3746perf_event__synthesize_event_update_name(struct perf_tool *tool,
3747 struct perf_evsel *evsel,
3748 perf_event__handler_t process)
3749{
3750 struct event_update_event *ev;
3751 size_t len = strlen(evsel->name);
3752 int err;
3753
3754 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
3755 if (ev == NULL)
3756 return -ENOMEM;
3757
Arnaldo Carvalho de Melo5192bde2018-12-06 11:09:46 -03003758 strlcpy(ev->data, evsel->name, len + 1);
Jiri Olsa802c9042015-10-25 15:51:39 +01003759 err = process(tool, (union perf_event*) ev, NULL, NULL);
3760 free(ev);
3761 return err;
3762}
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003763
Jiri Olsa86ebb092015-10-25 15:51:40 +01003764int
3765perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
3766 struct perf_evsel *evsel,
3767 perf_event__handler_t process)
3768{
3769 size_t size = sizeof(struct event_update_event);
3770 struct event_update_event *ev;
3771 int max, err;
3772 u16 type;
3773
3774 if (!evsel->own_cpus)
3775 return 0;
3776
3777 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3778 if (!ev)
3779 return -ENOMEM;
3780
3781 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3782 ev->header.size = (u16)size;
3783 ev->type = PERF_EVENT_UPDATE__CPUS;
3784 ev->id = evsel->id[0];
3785
3786 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3787 evsel->own_cpus,
3788 type, max);
3789
3790 err = process(tool, (union perf_event*) ev, NULL, NULL);
3791 free(ev);
3792 return err;
3793}
3794
Jiri Olsac853f932015-10-25 15:51:41 +01003795size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3796{
3797 struct event_update_event *ev = &event->event_update;
3798 struct event_update_event_scale *ev_scale;
3799 struct event_update_event_cpus *ev_cpus;
3800 struct cpu_map *map;
3801 size_t ret;
3802
3803 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3804
3805 switch (ev->type) {
3806 case PERF_EVENT_UPDATE__SCALE:
3807 ev_scale = (struct event_update_event_scale *) ev->data;
3808 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3809 break;
3810 case PERF_EVENT_UPDATE__UNIT:
3811 ret += fprintf(fp, "... unit: %s\n", ev->data);
3812 break;
3813 case PERF_EVENT_UPDATE__NAME:
3814 ret += fprintf(fp, "... name: %s\n", ev->data);
3815 break;
3816 case PERF_EVENT_UPDATE__CPUS:
3817 ev_cpus = (struct event_update_event_cpus *) ev->data;
3818 ret += fprintf(fp, "... ");
3819
3820 map = cpu_map__new_data(&ev_cpus->cpus);
3821 if (map)
3822 ret += cpu_map__fprintf(map, fp);
3823 else
3824 ret += fprintf(fp, "failed to get cpus\n");
3825 break;
3826 default:
3827 ret += fprintf(fp, "... unknown type\n");
3828 break;
3829 }
3830
3831 return ret;
3832}
Jiri Olsa86ebb092015-10-25 15:51:40 +01003833
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003834int perf_event__synthesize_attrs(struct perf_tool *tool,
Jiri Olsa318ec182018-08-30 08:32:15 +02003835 struct perf_evlist *evlist,
3836 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003837{
Robert Richter6606f872012-08-16 21:10:19 +02003838 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003839 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003840
Jiri Olsa318ec182018-08-30 08:32:15 +02003841 evlist__for_each_entry(evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02003842 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3843 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003844 if (err) {
3845 pr_debug("failed to create perf header attribute\n");
3846 return err;
3847 }
3848 }
3849
3850 return err;
3851}
3852
Andi Kleenbfd8f722017-11-17 13:42:58 -08003853static bool has_unit(struct perf_evsel *counter)
3854{
3855 return counter->unit && *counter->unit;
3856}
3857
3858static bool has_scale(struct perf_evsel *counter)
3859{
3860 return counter->scale != 1;
3861}
3862
3863int perf_event__synthesize_extra_attr(struct perf_tool *tool,
3864 struct perf_evlist *evsel_list,
3865 perf_event__handler_t process,
3866 bool is_pipe)
3867{
3868 struct perf_evsel *counter;
3869 int err;
3870
3871 /*
3872 * Synthesize other events stuff not carried within
3873 * attr event - unit, scale, name
3874 */
3875 evlist__for_each_entry(evsel_list, counter) {
3876 if (!counter->supported)
3877 continue;
3878
3879 /*
3880 * Synthesize unit and scale only if it's defined.
3881 */
3882 if (has_unit(counter)) {
3883 err = perf_event__synthesize_event_update_unit(tool, counter, process);
3884 if (err < 0) {
3885 pr_err("Couldn't synthesize evsel unit.\n");
3886 return err;
3887 }
3888 }
3889
3890 if (has_scale(counter)) {
3891 err = perf_event__synthesize_event_update_scale(tool, counter, process);
3892 if (err < 0) {
3893 pr_err("Couldn't synthesize evsel counter.\n");
3894 return err;
3895 }
3896 }
3897
3898 if (counter->own_cpus) {
3899 err = perf_event__synthesize_event_update_cpus(tool, counter, process);
3900 if (err < 0) {
3901 pr_err("Couldn't synthesize evsel cpus.\n");
3902 return err;
3903 }
3904 }
3905
3906 /*
3907 * Name is needed only for pipe output,
3908 * perf.data carries event names.
3909 */
3910 if (is_pipe) {
3911 err = perf_event__synthesize_event_update_name(tool, counter, process);
3912 if (err < 0) {
3913 pr_err("Couldn't synthesize evsel name.\n");
3914 return err;
3915 }
3916 }
3917 }
3918 return 0;
3919}
3920
Adrian Hunter47c3d102013-07-04 16:20:21 +03003921int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3922 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003923 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003924{
Robert Richterf4d83432012-08-16 21:10:17 +02003925 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003926 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003927 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003928
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003929 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09003930 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003931 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003932 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003933 }
3934
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03003935 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003936 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003937 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003938
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003939 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003940
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003941 ids = event->header.size;
3942 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003943 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003944 /*
3945 * We don't have the cpu and thread maps on the header, so
3946 * for allocating the perf_sample_id table we fake 1 cpu and
3947 * hattr->ids threads.
3948 */
3949 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3950 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003951
3952 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003953 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003954 }
3955
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003956 return 0;
3957}
Tom Zanussicd19a032010-04-01 23:59:20 -05003958
Jiri Olsaffe777252015-10-25 15:51:36 +01003959int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3960 union perf_event *event,
3961 struct perf_evlist **pevlist)
3962{
3963 struct event_update_event *ev = &event->event_update;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003964 struct event_update_event_scale *ev_scale;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003965 struct event_update_event_cpus *ev_cpus;
Jiri Olsaffe777252015-10-25 15:51:36 +01003966 struct perf_evlist *evlist;
3967 struct perf_evsel *evsel;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003968 struct cpu_map *map;
Jiri Olsaffe777252015-10-25 15:51:36 +01003969
3970 if (!pevlist || *pevlist == NULL)
3971 return -EINVAL;
3972
3973 evlist = *pevlist;
3974
3975 evsel = perf_evlist__id2evsel(evlist, ev->id);
3976 if (evsel == NULL)
3977 return -EINVAL;
3978
Jiri Olsaa6e52812015-10-25 15:51:37 +01003979 switch (ev->type) {
3980 case PERF_EVENT_UPDATE__UNIT:
3981 evsel->unit = strdup(ev->data);
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003982 break;
Jiri Olsa802c9042015-10-25 15:51:39 +01003983 case PERF_EVENT_UPDATE__NAME:
3984 evsel->name = strdup(ev->data);
3985 break;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003986 case PERF_EVENT_UPDATE__SCALE:
3987 ev_scale = (struct event_update_event_scale *) ev->data;
3988 evsel->scale = ev_scale->scale;
Arnaldo Carvalho de Melo8434a2e2017-02-08 21:57:22 -03003989 break;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003990 case PERF_EVENT_UPDATE__CPUS:
3991 ev_cpus = (struct event_update_event_cpus *) ev->data;
3992
3993 map = cpu_map__new_data(&ev_cpus->cpus);
3994 if (map)
3995 evsel->own_cpus = map;
3996 else
3997 pr_err("failed to get event_update cpus\n");
Jiri Olsaa6e52812015-10-25 15:51:37 +01003998 default:
3999 break;
4000 }
4001
Jiri Olsaffe777252015-10-25 15:51:36 +01004002 return 0;
4003}
4004
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02004005int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02004006 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02004007 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05004008{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02004009 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02004010 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05004011 ssize_t size = 0, aligned_size = 0, padding;
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07004012 struct feat_fd ff;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03004013 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05004014
Jiri Olsa29208e52011-10-20 15:59:43 +02004015 /*
4016 * We are going to store the size of the data followed
4017 * by the data contents. Since the fd descriptor is a pipe,
4018 * we cannot seek back to store the size of the data once
4019 * we know it. Instead we:
4020 *
4021 * - write the tracing data to the temp file
4022 * - get/write the data size to pipe
4023 * - write the tracing data from the temp file
4024 * to the pipe
4025 */
4026 tdata = tracing_data_get(&evlist->entries, fd, true);
4027 if (!tdata)
4028 return -1;
4029
Tom Zanussi92155452010-04-01 23:59:21 -05004030 memset(&ev, 0, sizeof(ev));
4031
4032 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02004033 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03004034 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05004035 padding = aligned_size - size;
4036 ev.tracing_data.header.size = sizeof(ev.tracing_data);
4037 ev.tracing_data.size = aligned_size;
4038
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02004039 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05004040
Jiri Olsa29208e52011-10-20 15:59:43 +02004041 /*
4042 * The put function will copy all the tracing data
4043 * stored in temp file to the pipe.
4044 */
4045 tracing_data_put(tdata);
4046
David Carrillo-Cisnerosccebbeb2017-07-17 21:25:39 -07004047 ff = (struct feat_fd){ .fd = fd };
4048 if (write_padded(&ff, NULL, 0, padding))
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -07004049 return -1;
Tom Zanussi92155452010-04-01 23:59:21 -05004050
4051 return aligned_size;
4052}
4053
Jiri Olsa89f16882018-09-13 14:54:03 +02004054int perf_event__process_tracing_data(struct perf_session *session,
4055 union perf_event *event)
Tom Zanussi92155452010-04-01 23:59:21 -05004056{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02004057 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01004058 int fd = perf_data__fd(session->data);
Jiri Olsacc9784bd2013-10-15 16:27:34 +02004059 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05004060 char buf[BUFSIZ];
4061
4062 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02004063 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05004064 SEEK_SET);
4065
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01004066 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03004067 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03004068 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05004069
Jiri Olsacc9784bd2013-10-15 16:27:34 +02004070 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03004071 pr_err("%s: reading input file", __func__);
4072 return -1;
4073 }
Tom Zanussi454c4072010-05-01 01:41:20 -05004074 if (session->repipe) {
4075 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03004076 if (retw <= 0 || retw != padding) {
4077 pr_err("%s: repiping tracing data padding", __func__);
4078 return -1;
4079 }
Tom Zanussi454c4072010-05-01 01:41:20 -05004080 }
Tom Zanussi92155452010-04-01 23:59:21 -05004081
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03004082 if (size_read + padding != size) {
4083 pr_err("%s: tracing data size mismatch", __func__);
4084 return -1;
4085 }
Tom Zanussi92155452010-04-01 23:59:21 -05004086
Namhyung Kim831394b2012-09-06 11:10:46 +09004087 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01004088 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03004089
Tom Zanussi92155452010-04-01 23:59:21 -05004090 return size_read + padding;
4091}
Tom Zanussic7929e42010-04-01 23:59:22 -05004092
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02004093int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02004094 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02004095 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02004096 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05004097{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02004098 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05004099 size_t len;
4100 int err = 0;
4101
4102 if (!pos->hit)
4103 return err;
4104
4105 memset(&ev, 0, sizeof(ev));
4106
4107 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03004108 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05004109 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
4110 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
4111 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03004112 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05004113 ev.build_id.header.size = sizeof(ev.build_id) + len;
4114 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
4115
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02004116 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05004117
4118 return err;
4119}
4120
Jiri Olsa89f16882018-09-13 14:54:03 +02004121int perf_event__process_build_id(struct perf_session *session,
4122 union perf_event *event)
Tom Zanussic7929e42010-04-01 23:59:22 -05004123{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02004124 __event_process_build_id(&event->build_id,
4125 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08004126 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05004127 return 0;
4128}