blob: 8dda19b68ac455e4ff57c80d9e491b155c996094 [file] [log] [blame]
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03001#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03002#include <inttypes.h>
Arnaldo Carvalho de Meloa9072bc2011-10-26 12:41:38 -02003#include "util.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -03004#include "string2.h"
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -03005#include <sys/param.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02006#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02007#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02008#include <unistd.h>
9#include <stdio.h>
10#include <stdlib.h>
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -030011#include <linux/compiler.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010012#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -020013#include <linux/kernel.h>
Robert Richterb1e5a9b2011-12-07 10:02:57 +010014#include <linux/bitops.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030015#include <sys/stat.h>
16#include <sys/types.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020017#include <sys/utsname.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030018#include <unistd.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020019
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020020#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030021#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020022#include "header.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030023#include "memswap.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020024#include "../perf.h"
25#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020026#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010027#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010028#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020029#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020030#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020031#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090032#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020033#include "build-id.h"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020034#include "data.h"
Jiri Olsa720e98b2016-02-16 16:01:43 +010035#include <api/fs/fs.h>
36#include "asm/bug.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020037
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030038#include "sane_ctype.h"
39
Stephane Eranian73323f52012-02-02 13:54:44 +010040/*
41 * magic2 = "PERFILE2"
42 * must be a numerical value to let the endianness
43 * determine the memory layout. That way we are able
44 * to detect endianness when reading the perf.data file
45 * back.
46 *
47 * we check for legacy (PERFFILE) format.
48 */
49static const char *__perf_magic1 = "PERFFILE";
50static const u64 __perf_magic2 = 0x32454c4946524550ULL;
51static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020052
Stephane Eranian73323f52012-02-02 13:54:44 +010053#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020054
Soramichi AKIYAMAd25ed5d2017-01-17 00:22:37 +090055const char perf_version_string[] = PERF_VERSION;
56
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020057struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020058 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020059 struct perf_file_section ids;
60};
61
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030062void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020063{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030064 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020065}
66
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030067void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020068{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030069 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020070}
71
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030072bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020073{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030074 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020075}
76
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -070077/* Return: 0 if succeded, -ERR if failed. */
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020078static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020079{
80 while (size) {
81 int ret = write(fd, buf, size);
82
83 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -020084 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020085
86 size -= ret;
87 buf += ret;
88 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020089
90 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020091}
92
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -070093/* Return: 0 if succeded, -ERR if failed. */
Namhyung Kime195fac2014-11-04 10:14:30 +090094int write_padded(int fd, const void *bf, size_t count, size_t count_aligned)
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -020095{
96 static const char zero_buf[NAME_ALIGN];
97 int err = do_write(fd, bf, count);
98
99 if (!err)
100 err = do_write(fd, zero_buf, count_aligned - count);
101
102 return err;
103}
104
Kan Liang2bb00d22015-09-01 09:58:12 -0400105#define string_size(str) \
106 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
107
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -0700108/* Return: 0 if succeded, -ERR if failed. */
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200109static int do_write_string(int fd, const char *str)
110{
111 u32 len, olen;
112 int ret;
113
114 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300115 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200116
117 /* write len, incl. \0 */
118 ret = do_write(fd, &len, sizeof(len));
119 if (ret < 0)
120 return ret;
121
122 return write_padded(fd, str, olen, len);
123}
124
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700125static int __do_read(int fd, void *addr, ssize_t size)
126{
127 ssize_t ret = readn(fd, addr, size);
128
129 if (ret != size)
130 return ret < 0 ? (int)ret : -1;
131 return 0;
132}
133
134static int do_read_u32(int fd, struct perf_header *ph, u32 *addr)
135{
136 int ret;
137
138 ret = __do_read(fd, addr, sizeof(*addr));
139 if (ret)
140 return ret;
141
142 if (ph->needs_swap)
143 *addr = bswap_32(*addr);
144 return 0;
145}
146
147static int do_read_u64(int fd, struct perf_header *ph, u64 *addr)
148{
149 int ret;
150
151 ret = __do_read(fd, addr, sizeof(*addr));
152 if (ret)
153 return ret;
154
155 if (ph->needs_swap)
156 *addr = bswap_64(*addr);
157 return 0;
158}
159
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200160static char *do_read_string(int fd, struct perf_header *ph)
161{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200162 u32 len;
163 char *buf;
164
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700165 if (do_read_u32(fd, ph, &len))
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200166 return NULL;
167
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200168 buf = malloc(len);
169 if (!buf)
170 return NULL;
171
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -0700172 if (!__do_read(fd, buf, len)) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200173 /*
174 * strings are padded by zeroes
175 * thus the actual strlen of buf
176 * may be less than len
177 */
178 return buf;
179 }
180
181 free(buf);
182 return NULL;
183}
184
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300185static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200186 struct perf_evlist *evlist)
187{
188 return read_tracing_data(fd, &evlist->entries);
189}
190
191
192static int write_build_id(int fd, struct perf_header *h,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300193 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200194{
195 struct perf_session *session;
196 int err;
197
198 session = container_of(h, struct perf_session, header);
199
Robert Richtere20960c2011-12-07 10:02:55 +0100200 if (!perf_session__read_build_ids(session, true))
201 return -1;
202
Namhyung Kim714c9c42014-11-04 10:14:29 +0900203 err = perf_session__write_buildid_table(session, fd);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200204 if (err < 0) {
205 pr_debug("failed to write buildid table\n");
206 return err;
207 }
Namhyung Kim73c5d222014-11-07 22:57:56 +0900208 perf_session__cache_build_ids(session);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200209
210 return 0;
211}
212
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300213static int write_hostname(int fd, struct perf_header *h __maybe_unused,
214 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200215{
216 struct utsname uts;
217 int ret;
218
219 ret = uname(&uts);
220 if (ret < 0)
221 return -1;
222
223 return do_write_string(fd, uts.nodename);
224}
225
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300226static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
227 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200228{
229 struct utsname uts;
230 int ret;
231
232 ret = uname(&uts);
233 if (ret < 0)
234 return -1;
235
236 return do_write_string(fd, uts.release);
237}
238
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300239static int write_arch(int fd, struct perf_header *h __maybe_unused,
240 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200241{
242 struct utsname uts;
243 int ret;
244
245 ret = uname(&uts);
246 if (ret < 0)
247 return -1;
248
249 return do_write_string(fd, uts.machine);
250}
251
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300252static int write_version(int fd, struct perf_header *h __maybe_unused,
253 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200254{
255 return do_write_string(fd, perf_version_string);
256}
257
Wang Nan493c3032014-10-24 09:45:26 +0800258static int __write_cpudesc(int fd, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200259{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200260 FILE *file;
261 char *buf = NULL;
262 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800263 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200264 size_t len = 0;
265 int ret = -1;
266
267 if (!search)
268 return -1;
269
270 file = fopen("/proc/cpuinfo", "r");
271 if (!file)
272 return -1;
273
274 while (getline(&buf, &len, file) > 0) {
275 ret = strncmp(buf, search, strlen(search));
276 if (!ret)
277 break;
278 }
279
Wang Naned307752014-10-16 11:08:29 +0800280 if (ret) {
281 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200282 goto done;
Wang Naned307752014-10-16 11:08:29 +0800283 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200284
285 s = buf;
286
287 p = strchr(buf, ':');
288 if (p && *(p+1) == ' ' && *(p+2))
289 s = p + 2;
290 p = strchr(s, '\n');
291 if (p)
292 *p = '\0';
293
294 /* squash extra space characters (branding string) */
295 p = s;
296 while (*p) {
297 if (isspace(*p)) {
298 char *r = p + 1;
299 char *q = r;
300 *p = ' ';
301 while (*q && isspace(*q))
302 q++;
303 if (q != (p+1))
304 while ((*r++ = *q++));
305 }
306 p++;
307 }
308 ret = do_write_string(fd, s);
309done:
310 free(buf);
311 fclose(file);
312 return ret;
313}
314
Wang Nan493c3032014-10-24 09:45:26 +0800315static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
316 struct perf_evlist *evlist __maybe_unused)
317{
318#ifndef CPUINFO_PROC
319#define CPUINFO_PROC {"model name", }
320#endif
321 const char *cpuinfo_procs[] = CPUINFO_PROC;
322 unsigned int i;
323
324 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
325 int ret;
326 ret = __write_cpudesc(fd, cpuinfo_procs[i]);
327 if (ret >= 0)
328 return ret;
329 }
330 return -1;
331}
332
333
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300334static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
335 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200336{
337 long nr;
338 u32 nrc, nra;
339 int ret;
340
Jan Stancekda8a58b2017-02-17 12:10:26 +0100341 nrc = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200342
343 nr = sysconf(_SC_NPROCESSORS_ONLN);
344 if (nr < 0)
345 return -1;
346
347 nra = (u32)(nr & UINT_MAX);
348
349 ret = do_write(fd, &nrc, sizeof(nrc));
350 if (ret < 0)
351 return ret;
352
353 return do_write(fd, &nra, sizeof(nra));
354}
355
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300356static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200357 struct perf_evlist *evlist)
358{
Robert Richter6606f872012-08-16 21:10:19 +0200359 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900360 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200361 int ret;
362
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900363 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200364
365 /*
366 * write number of events
367 */
368 ret = do_write(fd, &nre, sizeof(nre));
369 if (ret < 0)
370 return ret;
371
372 /*
373 * size of perf_event_attr struct
374 */
Robert Richter6606f872012-08-16 21:10:19 +0200375 sz = (u32)sizeof(evsel->attr);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200376 ret = do_write(fd, &sz, sizeof(sz));
377 if (ret < 0)
378 return ret;
379
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300380 evlist__for_each_entry(evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +0200381 ret = do_write(fd, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200382 if (ret < 0)
383 return ret;
384 /*
385 * write number of unique id per event
386 * there is one id per instance of an event
387 *
388 * copy into an nri to be independent of the
389 * type of ids,
390 */
Robert Richter6606f872012-08-16 21:10:19 +0200391 nri = evsel->ids;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200392 ret = do_write(fd, &nri, sizeof(nri));
393 if (ret < 0)
394 return ret;
395
396 /*
397 * write event string as passed on cmdline
398 */
Robert Richter6606f872012-08-16 21:10:19 +0200399 ret = do_write_string(fd, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200400 if (ret < 0)
401 return ret;
402 /*
403 * write unique ids for this event
404 */
Robert Richter6606f872012-08-16 21:10:19 +0200405 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200406 if (ret < 0)
407 return ret;
408 }
409 return 0;
410}
411
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300412static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
413 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200414{
415 char buf[MAXPATHLEN];
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300416 u32 n;
417 int i, ret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200418
Tommi Rantala55f771282017-03-22 15:06:24 +0200419 /* actual path to perf binary */
420 ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200421 if (ret <= 0)
422 return -1;
423
424 /* readlink() does not add null termination */
425 buf[ret] = '\0';
426
427 /* account for binary path */
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300428 n = perf_env.nr_cmdline + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200429
430 ret = do_write(fd, &n, sizeof(n));
431 if (ret < 0)
432 return ret;
433
434 ret = do_write_string(fd, buf);
435 if (ret < 0)
436 return ret;
437
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300438 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
439 ret = do_write_string(fd, perf_env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200440 if (ret < 0)
441 return ret;
442 }
443 return 0;
444}
445
446#define CORE_SIB_FMT \
447 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
448#define THRD_SIB_FMT \
449 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
450
451struct cpu_topo {
Kan Liang2bb00d22015-09-01 09:58:12 -0400452 u32 cpu_nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200453 u32 core_sib;
454 u32 thread_sib;
455 char **core_siblings;
456 char **thread_siblings;
457};
458
459static int build_cpu_topo(struct cpu_topo *tp, int cpu)
460{
461 FILE *fp;
462 char filename[MAXPATHLEN];
463 char *buf = NULL, *p;
464 size_t len = 0;
Stephane Eranianc5885742013-08-14 12:04:26 +0200465 ssize_t sret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200466 u32 i = 0;
467 int ret = -1;
468
469 sprintf(filename, CORE_SIB_FMT, cpu);
470 fp = fopen(filename, "r");
471 if (!fp)
Stephane Eranianc5885742013-08-14 12:04:26 +0200472 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200473
Stephane Eranianc5885742013-08-14 12:04:26 +0200474 sret = getline(&buf, &len, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200475 fclose(fp);
Stephane Eranianc5885742013-08-14 12:04:26 +0200476 if (sret <= 0)
477 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200478
479 p = strchr(buf, '\n');
480 if (p)
481 *p = '\0';
482
483 for (i = 0; i < tp->core_sib; i++) {
484 if (!strcmp(buf, tp->core_siblings[i]))
485 break;
486 }
487 if (i == tp->core_sib) {
488 tp->core_siblings[i] = buf;
489 tp->core_sib++;
490 buf = NULL;
491 len = 0;
492 }
Stephane Eranianc5885742013-08-14 12:04:26 +0200493 ret = 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200494
Stephane Eranianc5885742013-08-14 12:04:26 +0200495try_threads:
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200496 sprintf(filename, THRD_SIB_FMT, cpu);
497 fp = fopen(filename, "r");
498 if (!fp)
499 goto done;
500
501 if (getline(&buf, &len, fp) <= 0)
502 goto done;
503
504 p = strchr(buf, '\n');
505 if (p)
506 *p = '\0';
507
508 for (i = 0; i < tp->thread_sib; i++) {
509 if (!strcmp(buf, tp->thread_siblings[i]))
510 break;
511 }
512 if (i == tp->thread_sib) {
513 tp->thread_siblings[i] = buf;
514 tp->thread_sib++;
515 buf = NULL;
516 }
517 ret = 0;
518done:
519 if(fp)
520 fclose(fp);
521 free(buf);
522 return ret;
523}
524
525static void free_cpu_topo(struct cpu_topo *tp)
526{
527 u32 i;
528
529 if (!tp)
530 return;
531
532 for (i = 0 ; i < tp->core_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300533 zfree(&tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200534
535 for (i = 0 ; i < tp->thread_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300536 zfree(&tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200537
538 free(tp);
539}
540
541static struct cpu_topo *build_cpu_topology(void)
542{
Jan Stancek43db2842017-02-17 12:10:25 +0100543 struct cpu_topo *tp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200544 void *addr;
545 u32 nr, i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300546 size_t sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200547 long ncpus;
548 int ret = -1;
Jan Stancek43db2842017-02-17 12:10:25 +0100549 struct cpu_map *map;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200550
Jan Stancekda8a58b2017-02-17 12:10:26 +0100551 ncpus = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200552
Jan Stancek43db2842017-02-17 12:10:25 +0100553 /* build online CPU map */
554 map = cpu_map__new(NULL);
555 if (map == NULL) {
556 pr_debug("failed to get system cpumap\n");
557 return NULL;
558 }
559
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200560 nr = (u32)(ncpus & UINT_MAX);
561
562 sz = nr * sizeof(char *);
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300563 addr = calloc(1, sizeof(*tp) + 2 * sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200564 if (!addr)
Jan Stancek43db2842017-02-17 12:10:25 +0100565 goto out_free;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200566
567 tp = addr;
Kan Liang2bb00d22015-09-01 09:58:12 -0400568 tp->cpu_nr = nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200569 addr += sizeof(*tp);
570 tp->core_siblings = addr;
571 addr += sz;
572 tp->thread_siblings = addr;
573
574 for (i = 0; i < nr; i++) {
Jan Stancek43db2842017-02-17 12:10:25 +0100575 if (!cpu_map__has(map, i))
576 continue;
577
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200578 ret = build_cpu_topo(tp, i);
579 if (ret < 0)
580 break;
581 }
Jan Stancek43db2842017-02-17 12:10:25 +0100582
583out_free:
584 cpu_map__put(map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200585 if (ret) {
586 free_cpu_topo(tp);
587 tp = NULL;
588 }
589 return tp;
590}
591
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300592static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
593 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200594{
595 struct cpu_topo *tp;
596 u32 i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300597 int ret, j;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200598
599 tp = build_cpu_topology();
600 if (!tp)
601 return -1;
602
603 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
604 if (ret < 0)
605 goto done;
606
607 for (i = 0; i < tp->core_sib; i++) {
608 ret = do_write_string(fd, tp->core_siblings[i]);
609 if (ret < 0)
610 goto done;
611 }
612 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
613 if (ret < 0)
614 goto done;
615
616 for (i = 0; i < tp->thread_sib; i++) {
617 ret = do_write_string(fd, tp->thread_siblings[i]);
618 if (ret < 0)
619 break;
620 }
Kan Liang2bb00d22015-09-01 09:58:12 -0400621
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300622 ret = perf_env__read_cpu_topology_map(&perf_env);
623 if (ret < 0)
624 goto done;
625
626 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
627 ret = do_write(fd, &perf_env.cpu[j].core_id,
628 sizeof(perf_env.cpu[j].core_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400629 if (ret < 0)
630 return ret;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300631 ret = do_write(fd, &perf_env.cpu[j].socket_id,
632 sizeof(perf_env.cpu[j].socket_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400633 if (ret < 0)
634 return ret;
635 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200636done:
637 free_cpu_topo(tp);
638 return ret;
639}
640
641
642
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300643static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
644 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200645{
646 char *buf = NULL;
647 FILE *fp;
648 size_t len = 0;
649 int ret = -1, n;
650 uint64_t mem;
651
652 fp = fopen("/proc/meminfo", "r");
653 if (!fp)
654 return -1;
655
656 while (getline(&buf, &len, fp) > 0) {
657 ret = strncmp(buf, "MemTotal:", 9);
658 if (!ret)
659 break;
660 }
661 if (!ret) {
662 n = sscanf(buf, "%*s %"PRIu64, &mem);
663 if (n == 1)
664 ret = do_write(fd, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800665 } else
666 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200667 free(buf);
668 fclose(fp);
669 return ret;
670}
671
672static int write_topo_node(int fd, int node)
673{
674 char str[MAXPATHLEN];
675 char field[32];
676 char *buf = NULL, *p;
677 size_t len = 0;
678 FILE *fp;
679 u64 mem_total, mem_free, mem;
680 int ret = -1;
681
682 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
683 fp = fopen(str, "r");
684 if (!fp)
685 return -1;
686
687 while (getline(&buf, &len, fp) > 0) {
688 /* skip over invalid lines */
689 if (!strchr(buf, ':'))
690 continue;
Alan Coxa761a2d2014-01-20 19:10:11 +0100691 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200692 goto done;
693 if (!strcmp(field, "MemTotal:"))
694 mem_total = mem;
695 if (!strcmp(field, "MemFree:"))
696 mem_free = mem;
697 }
698
699 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100700 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200701
702 ret = do_write(fd, &mem_total, sizeof(u64));
703 if (ret)
704 goto done;
705
706 ret = do_write(fd, &mem_free, sizeof(u64));
707 if (ret)
708 goto done;
709
710 ret = -1;
711 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
712
713 fp = fopen(str, "r");
714 if (!fp)
715 goto done;
716
717 if (getline(&buf, &len, fp) <= 0)
718 goto done;
719
720 p = strchr(buf, '\n');
721 if (p)
722 *p = '\0';
723
724 ret = do_write_string(fd, buf);
725done:
726 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100727 if (fp)
728 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200729 return ret;
730}
731
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300732static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
733 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200734{
735 char *buf = NULL;
736 size_t len = 0;
737 FILE *fp;
738 struct cpu_map *node_map = NULL;
739 char *c;
740 u32 nr, i, j;
741 int ret = -1;
742
743 fp = fopen("/sys/devices/system/node/online", "r");
744 if (!fp)
745 return -1;
746
747 if (getline(&buf, &len, fp) <= 0)
748 goto done;
749
750 c = strchr(buf, '\n');
751 if (c)
752 *c = '\0';
753
754 node_map = cpu_map__new(buf);
755 if (!node_map)
756 goto done;
757
758 nr = (u32)node_map->nr;
759
760 ret = do_write(fd, &nr, sizeof(nr));
761 if (ret < 0)
762 goto done;
763
764 for (i = 0; i < nr; i++) {
765 j = (u32)node_map->map[i];
766 ret = do_write(fd, &j, sizeof(j));
767 if (ret < 0)
768 break;
769
770 ret = write_topo_node(fd, i);
771 if (ret < 0)
772 break;
773 }
774done:
775 free(buf);
776 fclose(fp);
Masami Hiramatsu5191d8872015-12-09 11:11:35 +0900777 cpu_map__put(node_map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200778 return ret;
779}
780
781/*
Robert Richter50a96672012-08-16 21:10:24 +0200782 * File format:
783 *
784 * struct pmu_mappings {
785 * u32 pmu_num;
786 * struct pmu_map {
787 * u32 type;
788 * char name[];
789 * }[pmu_num];
790 * };
791 */
792
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300793static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
794 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +0200795{
796 struct perf_pmu *pmu = NULL;
797 off_t offset = lseek(fd, 0, SEEK_CUR);
798 __u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900799 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200800
801 /* write real pmu_num later */
Namhyung Kim5323f602012-12-17 15:38:54 +0900802 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
803 if (ret < 0)
804 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200805
806 while ((pmu = perf_pmu__scan(pmu))) {
807 if (!pmu->name)
808 continue;
809 pmu_num++;
Namhyung Kim5323f602012-12-17 15:38:54 +0900810
811 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
812 if (ret < 0)
813 return ret;
814
815 ret = do_write_string(fd, pmu->name);
816 if (ret < 0)
817 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200818 }
819
820 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
821 /* discard all */
822 lseek(fd, offset, SEEK_SET);
823 return -1;
824 }
825
826 return 0;
827}
828
829/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900830 * File format:
831 *
832 * struct group_descs {
833 * u32 nr_groups;
834 * struct group_desc {
835 * char name[];
836 * u32 leader_idx;
837 * u32 nr_members;
838 * }[nr_groups];
839 * };
840 */
841static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
842 struct perf_evlist *evlist)
843{
844 u32 nr_groups = evlist->nr_groups;
845 struct perf_evsel *evsel;
846 int ret;
847
848 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
849 if (ret < 0)
850 return ret;
851
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300852 evlist__for_each_entry(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900853 if (perf_evsel__is_group_leader(evsel) &&
854 evsel->nr_members > 1) {
855 const char *name = evsel->group_name ?: "{anon_group}";
856 u32 leader_idx = evsel->idx;
857 u32 nr_members = evsel->nr_members;
858
859 ret = do_write_string(fd, name);
860 if (ret < 0)
861 return ret;
862
863 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
864 if (ret < 0)
865 return ret;
866
867 ret = do_write(fd, &nr_members, sizeof(nr_members));
868 if (ret < 0)
869 return ret;
870 }
871 }
872 return 0;
873}
874
875/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200876 * default get_cpuid(): nothing gets recorded
Jiada Wang7a759cd2017-04-09 20:02:37 -0700877 * actual implementation must be in arch/$(SRCARCH)/util/header.c
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200878 */
Rui Teng11d8f872016-07-28 10:05:57 +0800879int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200880{
881 return -1;
882}
883
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300884static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
885 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200886{
887 char buffer[64];
888 int ret;
889
890 ret = get_cpuid(buffer, sizeof(buffer));
891 if (!ret)
892 goto write_it;
893
894 return -1;
895write_it:
896 return do_write_string(fd, buffer);
897}
898
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300899static int write_branch_stack(int fd __maybe_unused,
900 struct perf_header *h __maybe_unused,
901 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +0100902{
903 return 0;
904}
905
Adrian Hunter99fa2982015-04-30 17:37:25 +0300906static int write_auxtrace(int fd, struct perf_header *h,
Adrian Hunter4025ea42015-04-09 18:53:41 +0300907 struct perf_evlist *evlist __maybe_unused)
908{
Adrian Hunter99fa2982015-04-30 17:37:25 +0300909 struct perf_session *session;
910 int err;
911
912 session = container_of(h, struct perf_session, header);
913
914 err = auxtrace_index__write(fd, &session->auxtrace_index);
915 if (err < 0)
916 pr_err("Failed to write auxtrace index\n");
917 return err;
Adrian Hunter4025ea42015-04-09 18:53:41 +0300918}
919
Jiri Olsa720e98b2016-02-16 16:01:43 +0100920static int cpu_cache_level__sort(const void *a, const void *b)
921{
922 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
923 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
924
925 return cache_a->level - cache_b->level;
926}
927
928static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
929{
930 if (a->level != b->level)
931 return false;
932
933 if (a->line_size != b->line_size)
934 return false;
935
936 if (a->sets != b->sets)
937 return false;
938
939 if (a->ways != b->ways)
940 return false;
941
942 if (strcmp(a->type, b->type))
943 return false;
944
945 if (strcmp(a->size, b->size))
946 return false;
947
948 if (strcmp(a->map, b->map))
949 return false;
950
951 return true;
952}
953
954static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
955{
956 char path[PATH_MAX], file[PATH_MAX];
957 struct stat st;
958 size_t len;
959
960 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
961 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
962
963 if (stat(file, &st))
964 return 1;
965
966 scnprintf(file, PATH_MAX, "%s/level", path);
967 if (sysfs__read_int(file, (int *) &cache->level))
968 return -1;
969
970 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
971 if (sysfs__read_int(file, (int *) &cache->line_size))
972 return -1;
973
974 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
975 if (sysfs__read_int(file, (int *) &cache->sets))
976 return -1;
977
978 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
979 if (sysfs__read_int(file, (int *) &cache->ways))
980 return -1;
981
982 scnprintf(file, PATH_MAX, "%s/type", path);
983 if (sysfs__read_str(file, &cache->type, &len))
984 return -1;
985
986 cache->type[len] = 0;
987 cache->type = rtrim(cache->type);
988
989 scnprintf(file, PATH_MAX, "%s/size", path);
990 if (sysfs__read_str(file, &cache->size, &len)) {
991 free(cache->type);
992 return -1;
993 }
994
995 cache->size[len] = 0;
996 cache->size = rtrim(cache->size);
997
998 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
999 if (sysfs__read_str(file, &cache->map, &len)) {
1000 free(cache->map);
1001 free(cache->type);
1002 return -1;
1003 }
1004
1005 cache->map[len] = 0;
1006 cache->map = rtrim(cache->map);
1007 return 0;
1008}
1009
1010static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1011{
1012 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1013}
1014
1015static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1016{
1017 u32 i, cnt = 0;
1018 long ncpus;
1019 u32 nr, cpu;
1020 u16 level;
1021
1022 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1023 if (ncpus < 0)
1024 return -1;
1025
1026 nr = (u32)(ncpus & UINT_MAX);
1027
1028 for (cpu = 0; cpu < nr; cpu++) {
1029 for (level = 0; level < 10; level++) {
1030 struct cpu_cache_level c;
1031 int err;
1032
1033 err = cpu_cache_level__read(&c, cpu, level);
1034 if (err < 0)
1035 return err;
1036
1037 if (err == 1)
1038 break;
1039
1040 for (i = 0; i < cnt; i++) {
1041 if (cpu_cache_level__cmp(&c, &caches[i]))
1042 break;
1043 }
1044
1045 if (i == cnt)
1046 caches[cnt++] = c;
1047 else
1048 cpu_cache_level__free(&c);
1049
1050 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1051 goto out;
1052 }
1053 }
1054 out:
1055 *cntp = cnt;
1056 return 0;
1057}
1058
1059#define MAX_CACHES 2000
1060
1061static int write_cache(int fd, struct perf_header *h __maybe_unused,
1062 struct perf_evlist *evlist __maybe_unused)
1063{
1064 struct cpu_cache_level caches[MAX_CACHES];
1065 u32 cnt = 0, i, version = 1;
1066 int ret;
1067
1068 ret = build_caches(caches, MAX_CACHES, &cnt);
1069 if (ret)
1070 goto out;
1071
1072 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1073
1074 ret = do_write(fd, &version, sizeof(u32));
1075 if (ret < 0)
1076 goto out;
1077
1078 ret = do_write(fd, &cnt, sizeof(u32));
1079 if (ret < 0)
1080 goto out;
1081
1082 for (i = 0; i < cnt; i++) {
1083 struct cpu_cache_level *c = &caches[i];
1084
1085 #define _W(v) \
1086 ret = do_write(fd, &c->v, sizeof(u32)); \
1087 if (ret < 0) \
1088 goto out;
1089
1090 _W(level)
1091 _W(line_size)
1092 _W(sets)
1093 _W(ways)
1094 #undef _W
1095
1096 #define _W(v) \
1097 ret = do_write_string(fd, (const char *) c->v); \
1098 if (ret < 0) \
1099 goto out;
1100
1101 _W(type)
1102 _W(size)
1103 _W(map)
1104 #undef _W
1105 }
1106
1107out:
1108 for (i = 0; i < cnt; i++)
1109 cpu_cache_level__free(&caches[i]);
1110 return ret;
1111}
1112
Jiri Olsaffa517a2015-10-25 15:51:43 +01001113static int write_stat(int fd __maybe_unused,
1114 struct perf_header *h __maybe_unused,
1115 struct perf_evlist *evlist __maybe_unused)
1116{
1117 return 0;
1118}
1119
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001120static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1121 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001122{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001123 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001124}
1125
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001126static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1127 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001128{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001129 fprintf(fp, "# os release : %s\n", ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001130}
1131
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001132static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001133{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001134 fprintf(fp, "# arch : %s\n", ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001135}
1136
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001137static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1138 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001139{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001140 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001141}
1142
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001143static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1144 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001145{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001146 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1147 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001148}
1149
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001150static void print_version(struct perf_header *ph, int fd __maybe_unused,
1151 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001152{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001153 fprintf(fp, "# perf version : %s\n", ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001154}
1155
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001156static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1157 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001158{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001159 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001160
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001161 nr = ph->env.nr_cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001162
1163 fprintf(fp, "# cmdline : ");
1164
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001165 for (i = 0; i < nr; i++)
1166 fprintf(fp, "%s ", ph->env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001167 fputc('\n', fp);
1168}
1169
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001170static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1171 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001172{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001173 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001174 char *str;
Jan Stancekda8a58b2017-02-17 12:10:26 +01001175 int cpu_nr = ph->env.nr_cpus_avail;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001176
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001177 nr = ph->env.nr_sibling_cores;
1178 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001179
1180 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001181 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001182 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001183 }
1184
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001185 nr = ph->env.nr_sibling_threads;
1186 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001187
1188 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001189 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001190 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001191 }
Kan Liang2bb00d22015-09-01 09:58:12 -04001192
1193 if (ph->env.cpu != NULL) {
1194 for (i = 0; i < cpu_nr; i++)
1195 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1196 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1197 } else
1198 fprintf(fp, "# Core ID and Socket ID information is not available\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001199}
1200
Robert Richter4e1b9c62012-08-16 21:10:22 +02001201static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001202{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001203 struct perf_evsel *evsel;
1204
1205 if (!events)
1206 return;
1207
1208 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001209 zfree(&evsel->name);
1210 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001211 }
1212
1213 free(events);
1214}
1215
1216static struct perf_evsel *
1217read_event_desc(struct perf_header *ph, int fd)
1218{
1219 struct perf_evsel *evsel, *events = NULL;
1220 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001221 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001222 u32 nre, sz, nr, i, j;
Stephane Eranian62db9062012-02-09 23:21:07 +01001223 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001224
1225 /* number of events */
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001226 if (do_read_u32(fd, ph, &nre))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001227 goto error;
1228
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001229 if (do_read_u32(fd, ph, &sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001230 goto error;
1231
Stephane Eranian62db9062012-02-09 23:21:07 +01001232 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001233 buf = malloc(sz);
1234 if (!buf)
1235 goto error;
1236
Robert Richter4e1b9c62012-08-16 21:10:22 +02001237 /* the last event terminates with evsel->attr.size == 0: */
1238 events = calloc(nre + 1, sizeof(*events));
1239 if (!events)
1240 goto error;
1241
1242 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001243 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001244 msz = sz;
1245
Robert Richter4e1b9c62012-08-16 21:10:22 +02001246 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1247 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001248
Stephane Eranian62db9062012-02-09 23:21:07 +01001249 /*
1250 * must read entire on-file attr struct to
1251 * sync up with layout.
1252 */
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001253 if (__do_read(fd, buf, sz))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001254 goto error;
1255
1256 if (ph->needs_swap)
1257 perf_event__attr_swap(buf);
1258
Robert Richter4e1b9c62012-08-16 21:10:22 +02001259 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001260
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001261 if (do_read_u32(fd, ph, &nr))
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001262 goto error;
1263
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001264 if (ph->needs_swap)
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001265 evsel->needs_swap = true;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001266
Robert Richter4e1b9c62012-08-16 21:10:22 +02001267 evsel->name = do_read_string(fd, ph);
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001268 if (!evsel->name)
1269 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001270
1271 if (!nr)
1272 continue;
1273
1274 id = calloc(nr, sizeof(*id));
1275 if (!id)
1276 goto error;
1277 evsel->ids = nr;
1278 evsel->id = id;
1279
1280 for (j = 0 ; j < nr; j++) {
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001281 if (do_read_u64(fd, ph, id))
Robert Richter4e1b9c62012-08-16 21:10:22 +02001282 goto error;
Robert Richter4e1b9c62012-08-16 21:10:22 +02001283 id++;
1284 }
1285 }
1286out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001287 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001288 return events;
1289error:
Markus Elfring4cc97612015-06-25 17:12:32 +02001290 free_event_desc(events);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001291 events = NULL;
1292 goto out;
1293}
1294
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001295static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -03001296 void *priv __maybe_unused)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001297{
1298 return fprintf(fp, ", %s = %s", name, val);
1299}
1300
Robert Richter4e1b9c62012-08-16 21:10:22 +02001301static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1302{
1303 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1304 u32 j;
1305 u64 *id;
1306
1307 if (!events) {
1308 fprintf(fp, "# event desc: not available or unable to read\n");
1309 return;
1310 }
1311
1312 for (evsel = events; evsel->attr.size; evsel++) {
1313 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001314
Robert Richter4e1b9c62012-08-16 21:10:22 +02001315 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001316 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001317 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1318 if (j)
1319 fputc(',', fp);
1320 fprintf(fp, " %"PRIu64, *id);
1321 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001322 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001323 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001324
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001325 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001326
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001327 fputc('\n', fp);
1328 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001329
1330 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001331}
1332
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001333static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001334 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001335{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001336 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001337}
1338
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001339static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001340 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001341{
Jiri Olsac60da222016-07-04 14:16:20 +02001342 int i;
1343 struct numa_node *n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001344
Jiri Olsac60da222016-07-04 14:16:20 +02001345 for (i = 0; i < ph->env.nr_numa_nodes; i++) {
1346 n = &ph->env.numa_nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001347
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001348 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1349 " free = %"PRIu64" kB\n",
Jiri Olsac60da222016-07-04 14:16:20 +02001350 n->node, n->mem_total, n->mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001351
Jiri Olsac60da222016-07-04 14:16:20 +02001352 fprintf(fp, "# node%u cpu list : ", n->node);
1353 cpu_map__fprintf(n->map, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001354 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001355}
1356
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001357static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001358{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001359 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001360}
1361
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001362static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001363 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001364{
1365 fprintf(fp, "# contains samples with branch stack\n");
1366}
1367
Adrian Hunter4025ea42015-04-09 18:53:41 +03001368static void print_auxtrace(struct perf_header *ph __maybe_unused,
1369 int fd __maybe_unused, FILE *fp)
1370{
1371 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1372}
1373
Jiri Olsaffa517a2015-10-25 15:51:43 +01001374static void print_stat(struct perf_header *ph __maybe_unused,
1375 int fd __maybe_unused, FILE *fp)
1376{
1377 fprintf(fp, "# contains stat data\n");
1378}
1379
Jiri Olsa720e98b2016-02-16 16:01:43 +01001380static void print_cache(struct perf_header *ph __maybe_unused,
1381 int fd __maybe_unused, FILE *fp __maybe_unused)
1382{
1383 int i;
1384
1385 fprintf(fp, "# CPU cache info:\n");
1386 for (i = 0; i < ph->env.caches_cnt; i++) {
1387 fprintf(fp, "# ");
1388 cpu_cache_level__fprintf(fp, &ph->env.caches[i]);
1389 }
1390}
1391
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001392static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1393 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001394{
1395 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001396 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001397 u32 pmu_num;
1398 u32 type;
1399
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001400 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001401 if (!pmu_num) {
1402 fprintf(fp, "# pmu mappings: not available\n");
1403 return;
1404 }
1405
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001406 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001407
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001408 while (pmu_num) {
1409 type = strtoul(str, &tmp, 0);
1410 if (*tmp != ':')
1411 goto error;
1412
1413 str = tmp + 1;
1414 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1415
Robert Richter50a96672012-08-16 21:10:24 +02001416 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001417 str += strlen(str) + 1;
1418 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001419 }
1420
1421 fprintf(fp, "\n");
1422
1423 if (!pmu_num)
1424 return;
1425error:
1426 fprintf(fp, "# pmu mappings: unable to read\n");
1427}
1428
Namhyung Kima8bb5592013-01-22 18:09:31 +09001429static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1430 FILE *fp)
1431{
1432 struct perf_session *session;
1433 struct perf_evsel *evsel;
1434 u32 nr = 0;
1435
1436 session = container_of(ph, struct perf_session, header);
1437
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001438 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001439 if (perf_evsel__is_group_leader(evsel) &&
1440 evsel->nr_members > 1) {
1441 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1442 perf_evsel__name(evsel));
1443
1444 nr = evsel->nr_members - 1;
1445 } else if (nr) {
1446 fprintf(fp, ",%s", perf_evsel__name(evsel));
1447
1448 if (--nr == 0)
1449 fprintf(fp, "}\n");
1450 }
1451 }
1452}
1453
Robert Richter08d95bd2012-02-10 15:41:55 +01001454static int __event_process_build_id(struct build_id_event *bev,
1455 char *filename,
1456 struct perf_session *session)
1457{
1458 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01001459 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00001460 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01001461 struct dso *dso;
1462 enum dso_kernel_type dso_type;
1463
1464 machine = perf_session__findnew_machine(session, bev->pid);
1465 if (!machine)
1466 goto out;
1467
Wang Nan1f121b02015-06-03 08:52:21 +00001468 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01001469
Wang Nan1f121b02015-06-03 08:52:21 +00001470 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01001471 case PERF_RECORD_MISC_KERNEL:
1472 dso_type = DSO_TYPE_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001473 break;
1474 case PERF_RECORD_MISC_GUEST_KERNEL:
1475 dso_type = DSO_TYPE_GUEST_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001476 break;
1477 case PERF_RECORD_MISC_USER:
1478 case PERF_RECORD_MISC_GUEST_USER:
1479 dso_type = DSO_TYPE_USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01001480 break;
1481 default:
1482 goto out;
1483 }
1484
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001485 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001486 if (dso != NULL) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001487 char sbuild_id[SBUILD_ID_SIZE];
Robert Richter08d95bd2012-02-10 15:41:55 +01001488
1489 dso__set_build_id(dso, &bev->build_id);
1490
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001491 if (dso_type != DSO_TYPE_USER) {
1492 struct kmod_path m = { .name = NULL, };
1493
1494 if (!kmod_path__parse_name(&m, filename) && m.kmod)
Namhyung Kim6b335e82017-05-31 21:01:04 +09001495 dso__set_module_info(dso, &m, machine);
Namhyung Kim1deec1b2017-05-31 21:01:03 +09001496 else
1497 dso->kernel = dso_type;
1498
1499 free(m.name);
1500 }
Robert Richter08d95bd2012-02-10 15:41:55 +01001501
1502 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1503 sbuild_id);
1504 pr_debug("build id event received for %s: %s\n",
1505 dso->long_name, sbuild_id);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001506 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01001507 }
1508
1509 err = 0;
1510out:
1511 return err;
1512}
1513
1514static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1515 int input, u64 offset, u64 size)
1516{
1517 struct perf_session *session = container_of(header, struct perf_session, header);
1518 struct {
1519 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001520 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001521 char filename[0];
1522 } old_bev;
1523 struct build_id_event bev;
1524 char filename[PATH_MAX];
1525 u64 limit = offset + size;
1526
1527 while (offset < limit) {
1528 ssize_t len;
1529
Namhyung Kim5323f602012-12-17 15:38:54 +09001530 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001531 return -1;
1532
1533 if (header->needs_swap)
1534 perf_event_header__bswap(&old_bev.header);
1535
1536 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001537 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001538 return -1;
1539
1540 bev.header = old_bev.header;
1541
1542 /*
1543 * As the pid is the missing value, we need to fill
1544 * it properly. The header.misc value give us nice hint.
1545 */
1546 bev.pid = HOST_KERNEL_ID;
1547 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1548 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1549 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1550
1551 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1552 __event_process_build_id(&bev, filename, session);
1553
1554 offset += bev.header.size;
1555 }
1556
1557 return 0;
1558}
1559
1560static int perf_header__read_build_ids(struct perf_header *header,
1561 int input, u64 offset, u64 size)
1562{
1563 struct perf_session *session = container_of(header, struct perf_session, header);
1564 struct build_id_event bev;
1565 char filename[PATH_MAX];
1566 u64 limit = offset + size, orig_offset = offset;
1567 int err = -1;
1568
1569 while (offset < limit) {
1570 ssize_t len;
1571
Namhyung Kim5323f602012-12-17 15:38:54 +09001572 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001573 goto out;
1574
1575 if (header->needs_swap)
1576 perf_event_header__bswap(&bev.header);
1577
1578 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001579 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001580 goto out;
1581 /*
1582 * The a1645ce1 changeset:
1583 *
1584 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1585 *
1586 * Added a field to struct build_id_event that broke the file
1587 * format.
1588 *
1589 * Since the kernel build-id is the first entry, process the
1590 * table using the old format if the well known
1591 * '[kernel.kallsyms]' string for the kernel build-id has the
1592 * first 4 characters chopped off (where the pid_t sits).
1593 */
1594 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1595 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1596 return -1;
1597 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1598 }
1599
1600 __event_process_build_id(&bev, filename, session);
1601
1602 offset += bev.header.size;
1603 }
1604 err = 0;
1605out:
1606 return err;
1607}
1608
David Carrillo-Cisnerosdfaa1582017-07-17 21:25:35 -07001609/* Macro for features that simply need to read and store a string. */
1610#define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
1611static int process_##__feat(struct perf_file_section *section __maybe_unused, \
1612 struct perf_header *ph, int fd, \
1613 void *data __maybe_unused) \
1614{\
1615 ph->env.__feat_env = do_read_string(fd, ph); \
1616 return ph->env.__feat_env ? 0 : -ENOMEM; \
1617}
1618
1619FEAT_PROCESS_STR_FUN(hostname, hostname);
1620FEAT_PROCESS_STR_FUN(osrelease, os_release);
1621FEAT_PROCESS_STR_FUN(version, version);
1622FEAT_PROCESS_STR_FUN(arch, arch);
1623FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
1624FEAT_PROCESS_STR_FUN(cpuid, cpuid);
1625
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001626static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1627 struct perf_header *ph __maybe_unused,
1628 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001629{
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001630 ssize_t ret = trace_report(fd, data, false);
1631 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001632}
1633
1634static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001635 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001636 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001637{
1638 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1639 pr_debug("Failed to read buildids, continuing...\n");
1640 return 0;
1641}
1642
Namhyung Kima1ae5652012-09-24 17:14:59 +09001643static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001644 struct perf_header *ph, int fd,
1645 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001646{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001647 int ret;
1648 u32 nr_cpus_avail, nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001649
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001650 ret = do_read_u32(fd, ph, &nr_cpus_avail);
1651 if (ret)
1652 return ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001653
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001654 ret = do_read_u32(fd, ph, &nr_cpus_online);
1655 if (ret)
1656 return ret;
1657 ph->env.nr_cpus_avail = (int)nr_cpus_avail;
1658 ph->env.nr_cpus_online = (int)nr_cpus_online;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001659 return 0;
1660}
1661
Namhyung Kima1ae5652012-09-24 17:14:59 +09001662static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001663 struct perf_header *ph, int fd,
1664 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001665{
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001666 u64 total_mem;
1667 int ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001668
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001669 ret = do_read_u64(fd, ph, &total_mem);
1670 if (ret)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001671 return -1;
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001672 ph->env.total_mem = (unsigned long long)total_mem;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001673 return 0;
1674}
1675
Robert Richter7c2f7af2012-08-16 21:10:23 +02001676static struct perf_evsel *
1677perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1678{
1679 struct perf_evsel *evsel;
1680
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001681 evlist__for_each_entry(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001682 if (evsel->idx == idx)
1683 return evsel;
1684 }
1685
1686 return NULL;
1687}
1688
1689static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001690perf_evlist__set_event_name(struct perf_evlist *evlist,
1691 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001692{
1693 struct perf_evsel *evsel;
1694
1695 if (!event->name)
1696 return;
1697
1698 evsel = perf_evlist__find_by_index(evlist, event->idx);
1699 if (!evsel)
1700 return;
1701
1702 if (evsel->name)
1703 return;
1704
1705 evsel->name = strdup(event->name);
1706}
1707
1708static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001709process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001710 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001711 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001712{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001713 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001714 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1715
1716 if (!events)
1717 return 0;
1718
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001719 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001720 for (evsel = events; evsel->attr.size; evsel++)
1721 perf_evlist__set_event_name(session->evlist, evsel);
1722
1723 free_event_desc(events);
1724
1725 return 0;
1726}
1727
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001728static int process_cmdline(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001729 struct perf_header *ph, int fd,
1730 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001731{
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001732 char *str, *cmdline = NULL, **argv = NULL;
1733 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001734
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001735 if (do_read_u32(fd, ph, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001736 return -1;
1737
Namhyung Kima1ae5652012-09-24 17:14:59 +09001738 ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001739
1740 cmdline = zalloc(section->size + nr + 1);
1741 if (!cmdline)
1742 return -1;
1743
1744 argv = zalloc(sizeof(char *) * (nr + 1));
1745 if (!argv)
1746 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001747
1748 for (i = 0; i < nr; i++) {
1749 str = do_read_string(fd, ph);
1750 if (!str)
1751 goto error;
1752
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001753 argv[i] = cmdline + len;
1754 memcpy(argv[i], str, strlen(str) + 1);
1755 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001756 free(str);
1757 }
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001758 ph->env.cmdline = cmdline;
1759 ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001760 return 0;
1761
1762error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001763 free(argv);
1764 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001765 return -1;
1766}
1767
Kan Liang2bb00d22015-09-01 09:58:12 -04001768static int process_cpu_topology(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001769 struct perf_header *ph, int fd,
1770 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001771{
Namhyung Kima1ae5652012-09-24 17:14:59 +09001772 u32 nr, i;
1773 char *str;
1774 struct strbuf sb;
Jan Stancekda8a58b2017-02-17 12:10:26 +01001775 int cpu_nr = ph->env.nr_cpus_avail;
Kan Liang2bb00d22015-09-01 09:58:12 -04001776 u64 size = 0;
1777
1778 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1779 if (!ph->env.cpu)
1780 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001781
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001782 if (do_read_u32(fd, ph, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001783 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001784
Namhyung Kima1ae5652012-09-24 17:14:59 +09001785 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001786 size += sizeof(u32);
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001787 if (strbuf_init(&sb, 128) < 0)
1788 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001789
1790 for (i = 0; i < nr; i++) {
1791 str = do_read_string(fd, ph);
1792 if (!str)
1793 goto error;
1794
1795 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001796 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1797 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001798 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001799 free(str);
1800 }
1801 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1802
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001803 if (do_read_u32(fd, ph, &nr))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001804 return -1;
1805
Namhyung Kima1ae5652012-09-24 17:14:59 +09001806 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001807 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001808
1809 for (i = 0; i < nr; i++) {
1810 str = do_read_string(fd, ph);
1811 if (!str)
1812 goto error;
1813
1814 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001815 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1816 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001817 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001818 free(str);
1819 }
1820 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04001821
1822 /*
1823 * The header may be from old perf,
1824 * which doesn't include core id and socket id information.
1825 */
1826 if (section->size <= size) {
1827 zfree(&ph->env.cpu);
1828 return 0;
1829 }
1830
1831 for (i = 0; i < (u32)cpu_nr; i++) {
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001832 if (do_read_u32(fd, ph, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001833 goto free_cpu;
1834
Kan Liang2bb00d22015-09-01 09:58:12 -04001835 ph->env.cpu[i].core_id = nr;
1836
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001837 if (do_read_u32(fd, ph, &nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001838 goto free_cpu;
1839
Jan Stancekda8a58b2017-02-17 12:10:26 +01001840 if (nr != (u32)-1 && nr > (u32)cpu_nr) {
Kan Liang2bb00d22015-09-01 09:58:12 -04001841 pr_debug("socket_id number is too big."
1842 "You may need to upgrade the perf tool.\n");
1843 goto free_cpu;
1844 }
1845
1846 ph->env.cpu[i].socket_id = nr;
1847 }
1848
Namhyung Kima1ae5652012-09-24 17:14:59 +09001849 return 0;
1850
1851error:
1852 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04001853free_cpu:
1854 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001855 return -1;
1856}
1857
1858static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001859 struct perf_header *ph, int fd,
1860 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001861{
Jiri Olsac60da222016-07-04 14:16:20 +02001862 struct numa_node *nodes, *n;
Jiri Olsac60da222016-07-04 14:16:20 +02001863 u32 nr, i;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001864 char *str;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001865
1866 /* nr nodes */
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001867 if (do_read_u32(fd, ph, &nr))
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001868 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001869
Jiri Olsac60da222016-07-04 14:16:20 +02001870 nodes = zalloc(sizeof(*nodes) * nr);
1871 if (!nodes)
1872 return -ENOMEM;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001873
1874 for (i = 0; i < nr; i++) {
Jiri Olsac60da222016-07-04 14:16:20 +02001875 n = &nodes[i];
1876
Namhyung Kima1ae5652012-09-24 17:14:59 +09001877 /* node number */
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001878 if (do_read_u32(fd, ph, &n->node))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001879 goto error;
1880
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001881 if (do_read_u64(fd, ph, &n->mem_total))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001882 goto error;
1883
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001884 if (do_read_u64(fd, ph, &n->mem_free))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001885 goto error;
1886
Namhyung Kima1ae5652012-09-24 17:14:59 +09001887 str = do_read_string(fd, ph);
1888 if (!str)
1889 goto error;
1890
Jiri Olsac60da222016-07-04 14:16:20 +02001891 n->map = cpu_map__new(str);
1892 if (!n->map)
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001893 goto error;
Jiri Olsac60da222016-07-04 14:16:20 +02001894
Namhyung Kima1ae5652012-09-24 17:14:59 +09001895 free(str);
1896 }
Jiri Olsaf957a532016-10-10 09:56:32 +02001897 ph->env.nr_numa_nodes = nr;
Jiri Olsac60da222016-07-04 14:16:20 +02001898 ph->env.numa_nodes = nodes;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001899 return 0;
1900
1901error:
Jiri Olsac60da222016-07-04 14:16:20 +02001902 free(nodes);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001903 return -1;
1904}
1905
1906static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001907 struct perf_header *ph, int fd,
1908 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001909{
Namhyung Kima1ae5652012-09-24 17:14:59 +09001910 char *name;
1911 u32 pmu_num;
1912 u32 type;
1913 struct strbuf sb;
1914
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001915 if (do_read_u32(fd, ph, &pmu_num))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001916 return -1;
1917
Namhyung Kima1ae5652012-09-24 17:14:59 +09001918 if (!pmu_num) {
1919 pr_debug("pmu mappings not available\n");
1920 return 0;
1921 }
1922
1923 ph->env.nr_pmu_mappings = pmu_num;
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001924 if (strbuf_init(&sb, 128) < 0)
1925 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001926
1927 while (pmu_num) {
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001928 if (do_read_u32(fd, ph, &type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001929 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001930
1931 name = do_read_string(fd, ph);
1932 if (!name)
1933 goto error;
1934
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001935 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
1936 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001937 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001938 if (strbuf_add(&sb, "", 1) < 0)
1939 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001940
Kan Liange0838e02015-09-10 11:03:05 -03001941 if (!strcmp(name, "msr"))
1942 ph->env.msr_pmu_type = type;
1943
Namhyung Kima1ae5652012-09-24 17:14:59 +09001944 free(name);
1945 pmu_num--;
1946 }
1947 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
1948 return 0;
1949
1950error:
1951 strbuf_release(&sb);
1952 return -1;
1953}
1954
Namhyung Kima8bb5592013-01-22 18:09:31 +09001955static int process_group_desc(struct perf_file_section *section __maybe_unused,
1956 struct perf_header *ph, int fd,
1957 void *data __maybe_unused)
1958{
1959 size_t ret = -1;
1960 u32 i, nr, nr_groups;
1961 struct perf_session *session;
1962 struct perf_evsel *evsel, *leader = NULL;
1963 struct group_desc {
1964 char *name;
1965 u32 leader_idx;
1966 u32 nr_members;
1967 } *desc;
1968
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001969 if (do_read_u32(fd, ph, &nr_groups))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001970 return -1;
1971
Namhyung Kima8bb5592013-01-22 18:09:31 +09001972 ph->env.nr_groups = nr_groups;
1973 if (!nr_groups) {
1974 pr_debug("group desc not available\n");
1975 return 0;
1976 }
1977
1978 desc = calloc(nr_groups, sizeof(*desc));
1979 if (!desc)
1980 return -1;
1981
1982 for (i = 0; i < nr_groups; i++) {
1983 desc[i].name = do_read_string(fd, ph);
1984 if (!desc[i].name)
1985 goto out_free;
1986
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001987 if (do_read_u32(fd, ph, &desc[i].leader_idx))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001988 goto out_free;
1989
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07001990 if (do_read_u32(fd, ph, &desc[i].nr_members))
Namhyung Kima8bb5592013-01-22 18:09:31 +09001991 goto out_free;
Namhyung Kima8bb5592013-01-22 18:09:31 +09001992 }
1993
1994 /*
1995 * Rebuild group relationship based on the group_desc
1996 */
1997 session = container_of(ph, struct perf_session, header);
1998 session->evlist->nr_groups = nr_groups;
1999
2000 i = nr = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002001 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002002 if (evsel->idx == (int) desc[i].leader_idx) {
2003 evsel->leader = evsel;
2004 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09002005 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002006 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09002007 desc[i].name = NULL;
2008 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09002009 evsel->nr_members = desc[i].nr_members;
2010
2011 if (i >= nr_groups || nr > 0) {
2012 pr_debug("invalid group desc\n");
2013 goto out_free;
2014 }
2015
2016 leader = evsel;
2017 nr = evsel->nr_members - 1;
2018 i++;
2019 } else if (nr) {
2020 /* This is a group member */
2021 evsel->leader = leader;
2022
2023 nr--;
2024 }
2025 }
2026
2027 if (i != nr_groups || nr != 0) {
2028 pr_debug("invalid group desc\n");
2029 goto out_free;
2030 }
2031
2032 ret = 0;
2033out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002034 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002035 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002036 free(desc);
2037
2038 return ret;
2039}
2040
Adrian Hunter99fa2982015-04-30 17:37:25 +03002041static int process_auxtrace(struct perf_file_section *section,
2042 struct perf_header *ph, int fd,
2043 void *data __maybe_unused)
2044{
2045 struct perf_session *session;
2046 int err;
2047
2048 session = container_of(ph, struct perf_session, header);
2049
2050 err = auxtrace_index__process(fd, section->size, session,
2051 ph->needs_swap);
2052 if (err < 0)
2053 pr_err("Failed to process auxtrace index\n");
2054 return err;
2055}
2056
Jiri Olsa720e98b2016-02-16 16:01:43 +01002057static int process_cache(struct perf_file_section *section __maybe_unused,
2058 struct perf_header *ph __maybe_unused, int fd __maybe_unused,
2059 void *data __maybe_unused)
2060{
2061 struct cpu_cache_level *caches;
2062 u32 cnt, i, version;
2063
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002064 if (do_read_u32(fd, ph, &version))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002065 return -1;
2066
Jiri Olsa720e98b2016-02-16 16:01:43 +01002067 if (version != 1)
2068 return -1;
2069
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002070 if (do_read_u32(fd, ph, &cnt))
Jiri Olsa720e98b2016-02-16 16:01:43 +01002071 return -1;
2072
Jiri Olsa720e98b2016-02-16 16:01:43 +01002073 caches = zalloc(sizeof(*caches) * cnt);
2074 if (!caches)
2075 return -1;
2076
2077 for (i = 0; i < cnt; i++) {
2078 struct cpu_cache_level c;
2079
2080 #define _R(v) \
David Carrillo-Cisneros6200e4942017-07-17 21:25:34 -07002081 if (do_read_u32(fd, ph, &c.v))\
Jiri Olsa720e98b2016-02-16 16:01:43 +01002082 goto out_free_caches; \
Jiri Olsa720e98b2016-02-16 16:01:43 +01002083
2084 _R(level)
2085 _R(line_size)
2086 _R(sets)
2087 _R(ways)
2088 #undef _R
2089
2090 #define _R(v) \
2091 c.v = do_read_string(fd, ph); \
2092 if (!c.v) \
2093 goto out_free_caches;
2094
2095 _R(type)
2096 _R(size)
2097 _R(map)
2098 #undef _R
2099
2100 caches[i] = c;
2101 }
2102
2103 ph->env.caches = caches;
2104 ph->env.caches_cnt = cnt;
2105 return 0;
2106out_free_caches:
2107 free(caches);
2108 return -1;
2109}
2110
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002111struct feature_ops {
2112 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2113 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01002114 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002115 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002116 const char *name;
2117 bool full_only;
2118};
2119
Robert Richter8cdfa782011-12-07 10:02:56 +01002120#define FEAT_OPA(n, func) \
2121 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01002122#define FEAT_OPP(n, func) \
2123 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2124 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01002125#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01002126 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09002127 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01002128
2129/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002130#define print_tracing_data NULL
2131#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002132
2133static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002134 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01002135 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002136 FEAT_OPP(HEADER_HOSTNAME, hostname),
2137 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2138 FEAT_OPP(HEADER_VERSION, version),
2139 FEAT_OPP(HEADER_ARCH, arch),
2140 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2141 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09002142 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002143 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02002144 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002145 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01002146 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2147 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01002148 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002149 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09002150 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Adrian Hunter99fa2982015-04-30 17:37:25 +03002151 FEAT_OPP(HEADER_AUXTRACE, auxtrace),
Jiri Olsaffa517a2015-10-25 15:51:43 +01002152 FEAT_OPA(HEADER_STAT, stat),
Jiri Olsa720e98b2016-02-16 16:01:43 +01002153 FEAT_OPF(HEADER_CACHE, cache),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002154};
2155
2156struct header_print_data {
2157 FILE *fp;
2158 bool full; /* extended list of headers */
2159};
2160
2161static int perf_file_section__fprintf_info(struct perf_file_section *section,
2162 struct perf_header *ph,
2163 int feat, int fd, void *data)
2164{
2165 struct header_print_data *hd = data;
2166
2167 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2168 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2169 "%d, continuing...\n", section->offset, feat);
2170 return 0;
2171 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002172 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002173 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002174 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002175 }
2176 if (!feat_ops[feat].print)
2177 return 0;
2178
2179 if (!feat_ops[feat].full_only || hd->full)
2180 feat_ops[feat].print(ph, fd, hd->fp);
2181 else
2182 fprintf(hd->fp, "# %s info available, use -I to display\n",
2183 feat_ops[feat].name);
2184
2185 return 0;
2186}
2187
2188int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2189{
2190 struct header_print_data hd;
2191 struct perf_header *header = &session->header;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002192 int fd = perf_data_file__fd(session->file);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002193 struct stat st;
Jiri Olsaaabae162016-10-10 09:35:50 +02002194 int ret, bit;
Jiri Olsaf45f5612016-10-10 09:03:07 +02002195
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002196 hd.fp = fp;
2197 hd.full = full;
2198
Jiri Olsaf45f5612016-10-10 09:03:07 +02002199 ret = fstat(fd, &st);
2200 if (ret == -1)
2201 return -1;
2202
2203 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
2204
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002205 perf_header__process_sections(header, fd, &hd,
2206 perf_file_section__fprintf_info);
Jiri Olsaaabae162016-10-10 09:35:50 +02002207
David Carrillo-Cisnerosc9d1c932017-04-10 13:14:32 -07002208 if (session->file->is_pipe)
2209 return 0;
2210
Jiri Olsaaabae162016-10-10 09:35:50 +02002211 fprintf(fp, "# missing features: ");
2212 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2213 if (bit)
2214 fprintf(fp, "%s ", feat_ops[bit].name);
2215 }
2216
2217 fprintf(fp, "\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002218 return 0;
2219}
2220
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002221static int do_write_feat(int fd, struct perf_header *h, int type,
2222 struct perf_file_section **p,
2223 struct perf_evlist *evlist)
2224{
2225 int err;
2226 int ret = 0;
2227
2228 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002229 if (!feat_ops[type].write)
2230 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002231
2232 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2233
2234 err = feat_ops[type].write(fd, h, evlist);
2235 if (err < 0) {
Jiri Olsa0c2aff42016-10-10 09:38:02 +02002236 pr_debug("failed to write feature %s\n", feat_ops[type].name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002237
2238 /* undo anything written */
2239 lseek(fd, (*p)->offset, SEEK_SET);
2240
2241 return -1;
2242 }
2243 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2244 (*p)++;
2245 }
2246 return ret;
2247}
2248
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002249static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002250 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002251{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002252 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002253 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002254 int sec_size;
2255 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002256 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002257 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002258
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002259 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002260 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002261 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002262
Paul Gortmaker91b98802013-01-30 20:05:49 -05002263 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002264 if (feat_sec == NULL)
2265 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002266
2267 sec_size = sizeof(*feat_sec) * nr_sections;
2268
Jiri Olsa8d541e92013-07-17 19:49:44 +02002269 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002270 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002271
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002272 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2273 if (do_write_feat(fd, header, feat, &p, evlist))
2274 perf_header__clear_feat(header, feat);
2275 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002276
Xiao Guangrongf887f302010-02-04 16:46:42 +08002277 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002278 /*
2279 * may write more than needed due to dropped feature, but
2280 * this is okay, reader will skip the mising entries
2281 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002282 err = do_write(fd, feat_sec, sec_size);
2283 if (err < 0)
2284 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002285 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002286 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002287}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002288
Tom Zanussi8dc58102010-04-01 23:59:15 -05002289int perf_header__write_pipe(int fd)
2290{
2291 struct perf_pipe_file_header f_header;
2292 int err;
2293
2294 f_header = (struct perf_pipe_file_header){
2295 .magic = PERF_MAGIC,
2296 .size = sizeof(f_header),
2297 };
2298
2299 err = do_write(fd, &f_header, sizeof(f_header));
2300 if (err < 0) {
2301 pr_debug("failed to write perf pipe header\n");
2302 return err;
2303 }
2304
2305 return 0;
2306}
2307
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002308int perf_session__write_header(struct perf_session *session,
2309 struct perf_evlist *evlist,
2310 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002311{
2312 struct perf_file_header f_header;
2313 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002314 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002315 struct perf_evsel *evsel;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002316 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002317 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002318
2319 lseek(fd, sizeof(f_header), SEEK_SET);
2320
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002321 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002322 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2323 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002324 if (err < 0) {
2325 pr_debug("failed to write perf header\n");
2326 return err;
2327 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002328 }
2329
Jiri Olsa944d62b2013-07-17 19:49:43 +02002330 attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002331
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002332 evlist__for_each_entry(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002333 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002334 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002335 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002336 .offset = evsel->id_offset,
2337 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002338 }
2339 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002340 err = do_write(fd, &f_attr, sizeof(f_attr));
2341 if (err < 0) {
2342 pr_debug("failed to write perf header attribute\n");
2343 return err;
2344 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002345 }
2346
Adrian Hunterd645c442013-12-11 14:36:28 +02002347 if (!header->data_offset)
2348 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002349 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002350
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002351 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002352 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002353 if (err < 0)
2354 return err;
2355 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002356
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002357 f_header = (struct perf_file_header){
2358 .magic = PERF_MAGIC,
2359 .size = sizeof(f_header),
2360 .attr_size = sizeof(f_attr),
2361 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002362 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002363 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002364 },
2365 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002366 .offset = header->data_offset,
2367 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002368 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002369 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002370 };
2371
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002372 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002373
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002374 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002375 err = do_write(fd, &f_header, sizeof(f_header));
2376 if (err < 0) {
2377 pr_debug("failed to write perf header\n");
2378 return err;
2379 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002380 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002381
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002382 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002383}
2384
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002385static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002386 int fd, void *buf, size_t size)
2387{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002388 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002389 return -1;
2390
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002391 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002392 mem_bswap_64(buf, size);
2393
2394 return 0;
2395}
2396
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002397int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002398 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002399 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002400 struct perf_header *ph,
2401 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002402{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002403 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002404 int nr_sections;
2405 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002406 int feat;
2407 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002408
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002409 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002410 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002411 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002412
Paul Gortmaker91b98802013-01-30 20:05:49 -05002413 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002414 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002415 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002416
2417 sec_size = sizeof(*feat_sec) * nr_sections;
2418
Jiri Olsa8d541e92013-07-17 19:49:44 +02002419 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002420
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002421 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2422 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002423 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002424
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002425 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2426 err = process(sec++, header, feat, fd, data);
2427 if (err < 0)
2428 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002429 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002430 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002431out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002432 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002433 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002434}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002435
Stephane Eranian114382a2012-02-09 23:21:08 +01002436static const int attr_file_abi_sizes[] = {
2437 [0] = PERF_ATTR_SIZE_VER0,
2438 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002439 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002440 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002441 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002442 0,
2443};
2444
2445/*
2446 * In the legacy file format, the magic number is not used to encode endianness.
2447 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2448 * on ABI revisions, we need to try all combinations for all endianness to
2449 * detect the endianness.
2450 */
2451static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2452{
2453 uint64_t ref_size, attr_size;
2454 int i;
2455
2456 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2457 ref_size = attr_file_abi_sizes[i]
2458 + sizeof(struct perf_file_section);
2459 if (hdr_sz != ref_size) {
2460 attr_size = bswap_64(hdr_sz);
2461 if (attr_size != ref_size)
2462 continue;
2463
2464 ph->needs_swap = true;
2465 }
2466 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2467 i,
2468 ph->needs_swap);
2469 return 0;
2470 }
2471 /* could not determine endianness */
2472 return -1;
2473}
2474
2475#define PERF_PIPE_HDR_VER0 16
2476
2477static const size_t attr_pipe_abi_sizes[] = {
2478 [0] = PERF_PIPE_HDR_VER0,
2479 0,
2480};
2481
2482/*
2483 * In the legacy pipe format, there is an implicit assumption that endiannesss
2484 * between host recording the samples, and host parsing the samples is the
2485 * same. This is not always the case given that the pipe output may always be
2486 * redirected into a file and analyzed on a different machine with possibly a
2487 * different endianness and perf_event ABI revsions in the perf tool itself.
2488 */
2489static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2490{
2491 u64 attr_size;
2492 int i;
2493
2494 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2495 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2496 attr_size = bswap_64(hdr_sz);
2497 if (attr_size != hdr_sz)
2498 continue;
2499
2500 ph->needs_swap = true;
2501 }
2502 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2503 return 0;
2504 }
2505 return -1;
2506}
2507
Feng Tange84ba4e2012-10-30 11:56:07 +08002508bool is_perf_magic(u64 magic)
2509{
2510 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2511 || magic == __perf_magic2
2512 || magic == __perf_magic2_sw)
2513 return true;
2514
2515 return false;
2516}
2517
Stephane Eranian114382a2012-02-09 23:21:08 +01002518static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2519 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002520{
2521 int ret;
2522
2523 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002524 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002525 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002526 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002527 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002528 if (is_pipe)
2529 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002530
Stephane Eranian114382a2012-02-09 23:21:08 +01002531 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002532 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002533 /*
2534 * the new magic number serves two purposes:
2535 * - unique number to identify actual perf.data files
2536 * - encode endianness of file
2537 */
Namhyung Kimf7913972015-01-29 17:06:45 +09002538 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002539
Stephane Eranian114382a2012-02-09 23:21:08 +01002540 /* check magic number with one endianness */
2541 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002542 return 0;
2543
Stephane Eranian114382a2012-02-09 23:21:08 +01002544 /* check magic number with opposite endianness */
2545 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002546 return -1;
2547
2548 ph->needs_swap = true;
2549
2550 return 0;
2551}
2552
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002553int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002554 struct perf_header *ph, int fd)
2555{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002556 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002557
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002558 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002559
Stephane Eranian73323f52012-02-02 13:54:44 +01002560 ret = readn(fd, header, sizeof(*header));
2561 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002562 return -1;
2563
Stephane Eranian114382a2012-02-09 23:21:08 +01002564 if (check_magic_endian(header->magic,
2565 header->attr_size, false, ph) < 0) {
2566 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002567 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002568 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002569
Stephane Eranian73323f52012-02-02 13:54:44 +01002570 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002571 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002572 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002573 }
2574
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002575 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002576 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002577 if (header->size == offsetof(typeof(*header), adds_features))
2578 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002579 else
2580 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002581 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002582 /*
2583 * feature bitmap is declared as an array of unsigned longs --
2584 * not good since its size can differ between the host that
2585 * generated the data file and the host analyzing the file.
2586 *
2587 * We need to handle endianness, but we don't know the size of
2588 * the unsigned long where the file was generated. Take a best
2589 * guess at determining it: try 64-bit swap first (ie., file
2590 * created on a 64-bit host), and check if the hostname feature
2591 * bit is set (this feature bit is forced on as of fbe96f2).
2592 * If the bit is not, undo the 64-bit swap and try a 32-bit
2593 * swap. If the hostname bit is still not set (e.g., older data
2594 * file), punt and fallback to the original behavior --
2595 * clearing all feature bits and setting buildid.
2596 */
David Ahern80c01202012-06-08 11:47:51 -03002597 mem_bswap_64(&header->adds_features,
2598 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002599
2600 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002601 /* unswap as u64 */
2602 mem_bswap_64(&header->adds_features,
2603 BITS_TO_U64(HEADER_FEAT_BITS));
2604
2605 /* unswap as u32 */
2606 mem_bswap_32(&header->adds_features,
2607 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002608 }
2609
2610 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2611 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2612 set_bit(HEADER_BUILD_ID, header->adds_features);
2613 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002614 }
2615
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002616 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002617 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002618
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002619 ph->data_offset = header->data.offset;
2620 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002621 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002622 return 0;
2623}
2624
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002625static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002626 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002627 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002628{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002629 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002630 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002631 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002632 return 0;
2633 }
2634
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002635 if (feat >= HEADER_LAST_FEATURE) {
2636 pr_debug("unknown feature %d, continuing...\n", feat);
2637 return 0;
2638 }
2639
Robert Richterf1c67db2012-02-10 15:41:56 +01002640 if (!feat_ops[feat].process)
2641 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002642
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002643 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002644}
2645
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002646static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002647 struct perf_header *ph, int fd,
2648 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002649{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002650 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002651
2652 ret = readn(fd, header, sizeof(*header));
2653 if (ret <= 0)
2654 return -1;
2655
Stephane Eranian114382a2012-02-09 23:21:08 +01002656 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2657 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002658 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002659 }
2660
2661 if (ph->needs_swap)
2662 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002663
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002664 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002665 return -1;
2666
Tom Zanussi8dc58102010-04-01 23:59:15 -05002667 return 0;
2668}
2669
Jiri Olsad4339562013-07-17 19:49:41 +02002670static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002671{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002672 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002673 struct perf_pipe_file_header f_header;
2674
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002675 if (perf_file_header__read_pipe(&f_header, header,
2676 perf_data_file__fd(session->file),
Tom Zanussi454c4072010-05-01 01:41:20 -05002677 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002678 pr_debug("incompatible file format\n");
2679 return -EINVAL;
2680 }
2681
Tom Zanussi8dc58102010-04-01 23:59:15 -05002682 return 0;
2683}
2684
Stephane Eranian69996df2012-02-09 23:21:06 +01002685static int read_attr(int fd, struct perf_header *ph,
2686 struct perf_file_attr *f_attr)
2687{
2688 struct perf_event_attr *attr = &f_attr->attr;
2689 size_t sz, left;
2690 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002691 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002692
2693 memset(f_attr, 0, sizeof(*f_attr));
2694
2695 /* read minimal guaranteed structure */
2696 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2697 if (ret <= 0) {
2698 pr_debug("cannot read %d bytes of header attr\n",
2699 PERF_ATTR_SIZE_VER0);
2700 return -1;
2701 }
2702
2703 /* on file perf_event_attr size */
2704 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002705
Stephane Eranian69996df2012-02-09 23:21:06 +01002706 if (ph->needs_swap)
2707 sz = bswap_32(sz);
2708
2709 if (sz == 0) {
2710 /* assume ABI0 */
2711 sz = PERF_ATTR_SIZE_VER0;
2712 } else if (sz > our_sz) {
2713 pr_debug("file uses a more recent and unsupported ABI"
2714 " (%zu bytes extra)\n", sz - our_sz);
2715 return -1;
2716 }
2717 /* what we have not yet read and that we know about */
2718 left = sz - PERF_ATTR_SIZE_VER0;
2719 if (left) {
2720 void *ptr = attr;
2721 ptr += PERF_ATTR_SIZE_VER0;
2722
2723 ret = readn(fd, ptr, left);
2724 }
2725 /* read perf_file_section, ids are read in caller */
2726 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2727
2728 return ret <= 0 ? -1 : 0;
2729}
2730
Namhyung Kim831394b2012-09-06 11:10:46 +09002731static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2732 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002733{
Namhyung Kim831394b2012-09-06 11:10:46 +09002734 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002735 char bf[128];
2736
Namhyung Kim831394b2012-09-06 11:10:46 +09002737 /* already prepared */
2738 if (evsel->tp_format)
2739 return 0;
2740
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002741 if (pevent == NULL) {
2742 pr_debug("broken or missing trace data\n");
2743 return -1;
2744 }
2745
Namhyung Kim831394b2012-09-06 11:10:46 +09002746 event = pevent_find_event(pevent, evsel->attr.config);
Namhyung Kima7619ae2013-04-18 21:24:16 +09002747 if (event == NULL) {
2748 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002749 return -1;
Namhyung Kima7619ae2013-04-18 21:24:16 +09002750 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002751
Namhyung Kim831394b2012-09-06 11:10:46 +09002752 if (!evsel->name) {
2753 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2754 evsel->name = strdup(bf);
2755 if (evsel->name == NULL)
2756 return -1;
2757 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002758
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002759 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002760 return 0;
2761}
2762
Namhyung Kim831394b2012-09-06 11:10:46 +09002763static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2764 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002765{
2766 struct perf_evsel *pos;
2767
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002768 evlist__for_each_entry(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002769 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2770 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002771 return -1;
2772 }
2773
2774 return 0;
2775}
2776
Jiri Olsad4339562013-07-17 19:49:41 +02002777int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002778{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002779 struct perf_data_file *file = session->file;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002780 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002781 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002782 struct perf_file_attr f_attr;
2783 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002784 int nr_attrs, nr_ids, i, j;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002785 int fd = perf_data_file__fd(file);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002786
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002787 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002788 if (session->evlist == NULL)
2789 return -ENOMEM;
2790
Kan Liang2c071442015-08-28 05:48:05 -04002791 session->evlist->env = &header->env;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -03002792 session->machines.host.env = &header->env;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002793 if (perf_data_file__is_pipe(file))
Jiri Olsad4339562013-07-17 19:49:41 +02002794 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002795
Stephane Eranian69996df2012-02-09 23:21:06 +01002796 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002797 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002798
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002799 /*
2800 * Sanity check that perf.data was written cleanly; data size is
2801 * initialized to 0 and updated only if the on_exit function is run.
2802 * If data size is still 0 then the file contains only partial
2803 * information. Just warn user and process it as much as it can.
2804 */
2805 if (f_header.data.size == 0) {
2806 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2807 "Was the 'perf record' command properly terminated?\n",
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002808 file->path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002809 }
2810
Stephane Eranian69996df2012-02-09 23:21:06 +01002811 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002812 lseek(fd, f_header.attrs.offset, SEEK_SET);
2813
2814 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002815 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002816 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002817
Stephane Eranian69996df2012-02-09 23:21:06 +01002818 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002819 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002820
David Ahern1060ab82015-04-09 16:15:46 -04002821 if (header->needs_swap) {
2822 f_attr.ids.size = bswap_64(f_attr.ids.size);
2823 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06002824 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04002825 }
David Aherneda39132011-07-15 12:34:09 -06002826
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002827 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002828 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002829
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002830 if (evsel == NULL)
2831 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002832
2833 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002834 /*
2835 * Do it before so that if perf_evsel__alloc_id fails, this
2836 * entry gets purged too at perf_evlist__delete().
2837 */
2838 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002839
2840 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002841 /*
2842 * We don't have the cpu and thread maps on the header, so
2843 * for allocating the perf_sample_id table we fake 1 cpu and
2844 * hattr->ids threads.
2845 */
2846 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2847 goto out_delete_evlist;
2848
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002849 lseek(fd, f_attr.ids.offset, SEEK_SET);
2850
2851 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002852 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002853 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002854
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002855 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002856 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002857
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002858 lseek(fd, tmp, SEEK_SET);
2859 }
2860
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002861 symbol_conf.nr_events = nr_attrs;
2862
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002863 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002864 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002865
Namhyung Kim831394b2012-09-06 11:10:46 +09002866 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002867 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002868 goto out_delete_evlist;
2869
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002870 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002871out_errno:
2872 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002873
2874out_delete_evlist:
2875 perf_evlist__delete(session->evlist);
2876 session->evlist = NULL;
2877 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002878}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002879
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002880int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002881 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002882 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002883{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002884 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002885 size_t size;
2886 int err;
2887
2888 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002889 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002890 size += sizeof(struct perf_event_header);
2891 size += ids * sizeof(u64);
2892
2893 ev = malloc(size);
2894
Chris Samuelce47dc52010-11-13 13:35:06 +11002895 if (ev == NULL)
2896 return -ENOMEM;
2897
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002898 ev->attr.attr = *attr;
2899 memcpy(ev->attr.id, id, ids * sizeof(u64));
2900
2901 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002902 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002903
Robert Richterf4d83432012-08-16 21:10:17 +02002904 if (ev->attr.header.size == size)
2905 err = process(tool, ev, NULL, NULL);
2906 else
2907 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002908
2909 free(ev);
2910
2911 return err;
2912}
2913
Jiri Olsaa6e52812015-10-25 15:51:37 +01002914static struct event_update_event *
2915event_update_event__new(size_t size, u64 type, u64 id)
2916{
2917 struct event_update_event *ev;
2918
2919 size += sizeof(*ev);
2920 size = PERF_ALIGN(size, sizeof(u64));
2921
2922 ev = zalloc(size);
2923 if (ev) {
2924 ev->header.type = PERF_RECORD_EVENT_UPDATE;
2925 ev->header.size = (u16)size;
2926 ev->type = type;
2927 ev->id = id;
2928 }
2929 return ev;
2930}
2931
2932int
2933perf_event__synthesize_event_update_unit(struct perf_tool *tool,
2934 struct perf_evsel *evsel,
2935 perf_event__handler_t process)
2936{
2937 struct event_update_event *ev;
2938 size_t size = strlen(evsel->unit);
2939 int err;
2940
2941 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
2942 if (ev == NULL)
2943 return -ENOMEM;
2944
2945 strncpy(ev->data, evsel->unit, size);
2946 err = process(tool, (union perf_event *)ev, NULL, NULL);
2947 free(ev);
2948 return err;
2949}
2950
Jiri Olsadaeecbc2015-10-25 15:51:38 +01002951int
2952perf_event__synthesize_event_update_scale(struct perf_tool *tool,
2953 struct perf_evsel *evsel,
2954 perf_event__handler_t process)
2955{
2956 struct event_update_event *ev;
2957 struct event_update_event_scale *ev_data;
2958 int err;
2959
2960 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
2961 if (ev == NULL)
2962 return -ENOMEM;
2963
2964 ev_data = (struct event_update_event_scale *) ev->data;
2965 ev_data->scale = evsel->scale;
2966 err = process(tool, (union perf_event*) ev, NULL, NULL);
2967 free(ev);
2968 return err;
2969}
2970
Jiri Olsa802c9042015-10-25 15:51:39 +01002971int
2972perf_event__synthesize_event_update_name(struct perf_tool *tool,
2973 struct perf_evsel *evsel,
2974 perf_event__handler_t process)
2975{
2976 struct event_update_event *ev;
2977 size_t len = strlen(evsel->name);
2978 int err;
2979
2980 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
2981 if (ev == NULL)
2982 return -ENOMEM;
2983
2984 strncpy(ev->data, evsel->name, len);
2985 err = process(tool, (union perf_event*) ev, NULL, NULL);
2986 free(ev);
2987 return err;
2988}
Jiri Olsadaeecbc2015-10-25 15:51:38 +01002989
Jiri Olsa86ebb092015-10-25 15:51:40 +01002990int
2991perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
2992 struct perf_evsel *evsel,
2993 perf_event__handler_t process)
2994{
2995 size_t size = sizeof(struct event_update_event);
2996 struct event_update_event *ev;
2997 int max, err;
2998 u16 type;
2999
3000 if (!evsel->own_cpus)
3001 return 0;
3002
3003 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3004 if (!ev)
3005 return -ENOMEM;
3006
3007 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3008 ev->header.size = (u16)size;
3009 ev->type = PERF_EVENT_UPDATE__CPUS;
3010 ev->id = evsel->id[0];
3011
3012 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3013 evsel->own_cpus,
3014 type, max);
3015
3016 err = process(tool, (union perf_event*) ev, NULL, NULL);
3017 free(ev);
3018 return err;
3019}
3020
Jiri Olsac853f932015-10-25 15:51:41 +01003021size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3022{
3023 struct event_update_event *ev = &event->event_update;
3024 struct event_update_event_scale *ev_scale;
3025 struct event_update_event_cpus *ev_cpus;
3026 struct cpu_map *map;
3027 size_t ret;
3028
3029 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3030
3031 switch (ev->type) {
3032 case PERF_EVENT_UPDATE__SCALE:
3033 ev_scale = (struct event_update_event_scale *) ev->data;
3034 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3035 break;
3036 case PERF_EVENT_UPDATE__UNIT:
3037 ret += fprintf(fp, "... unit: %s\n", ev->data);
3038 break;
3039 case PERF_EVENT_UPDATE__NAME:
3040 ret += fprintf(fp, "... name: %s\n", ev->data);
3041 break;
3042 case PERF_EVENT_UPDATE__CPUS:
3043 ev_cpus = (struct event_update_event_cpus *) ev->data;
3044 ret += fprintf(fp, "... ");
3045
3046 map = cpu_map__new_data(&ev_cpus->cpus);
3047 if (map)
3048 ret += cpu_map__fprintf(map, fp);
3049 else
3050 ret += fprintf(fp, "failed to get cpus\n");
3051 break;
3052 default:
3053 ret += fprintf(fp, "... unknown type\n");
3054 break;
3055 }
3056
3057 return ret;
3058}
Jiri Olsa86ebb092015-10-25 15:51:40 +01003059
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003060int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003061 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003062 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003063{
Robert Richter6606f872012-08-16 21:10:19 +02003064 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003065 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003066
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003067 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02003068 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3069 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003070 if (err) {
3071 pr_debug("failed to create perf header attribute\n");
3072 return err;
3073 }
3074 }
3075
3076 return err;
3077}
3078
Adrian Hunter47c3d102013-07-04 16:20:21 +03003079int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3080 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003081 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003082{
Robert Richterf4d83432012-08-16 21:10:17 +02003083 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003084 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003085 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003086
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003087 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09003088 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003089 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003090 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003091 }
3092
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03003093 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003094 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003095 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003096
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003097 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003098
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003099 ids = event->header.size;
3100 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003101 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003102 /*
3103 * We don't have the cpu and thread maps on the header, so
3104 * for allocating the perf_sample_id table we fake 1 cpu and
3105 * hattr->ids threads.
3106 */
3107 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3108 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003109
3110 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003111 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003112 }
3113
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03003114 symbol_conf.nr_events = evlist->nr_entries;
3115
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003116 return 0;
3117}
Tom Zanussicd19a032010-04-01 23:59:20 -05003118
Jiri Olsaffe777252015-10-25 15:51:36 +01003119int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3120 union perf_event *event,
3121 struct perf_evlist **pevlist)
3122{
3123 struct event_update_event *ev = &event->event_update;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003124 struct event_update_event_scale *ev_scale;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003125 struct event_update_event_cpus *ev_cpus;
Jiri Olsaffe777252015-10-25 15:51:36 +01003126 struct perf_evlist *evlist;
3127 struct perf_evsel *evsel;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003128 struct cpu_map *map;
Jiri Olsaffe777252015-10-25 15:51:36 +01003129
3130 if (!pevlist || *pevlist == NULL)
3131 return -EINVAL;
3132
3133 evlist = *pevlist;
3134
3135 evsel = perf_evlist__id2evsel(evlist, ev->id);
3136 if (evsel == NULL)
3137 return -EINVAL;
3138
Jiri Olsaa6e52812015-10-25 15:51:37 +01003139 switch (ev->type) {
3140 case PERF_EVENT_UPDATE__UNIT:
3141 evsel->unit = strdup(ev->data);
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003142 break;
Jiri Olsa802c9042015-10-25 15:51:39 +01003143 case PERF_EVENT_UPDATE__NAME:
3144 evsel->name = strdup(ev->data);
3145 break;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003146 case PERF_EVENT_UPDATE__SCALE:
3147 ev_scale = (struct event_update_event_scale *) ev->data;
3148 evsel->scale = ev_scale->scale;
Arnaldo Carvalho de Melo8434a2e2017-02-08 21:57:22 -03003149 break;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003150 case PERF_EVENT_UPDATE__CPUS:
3151 ev_cpus = (struct event_update_event_cpus *) ev->data;
3152
3153 map = cpu_map__new_data(&ev_cpus->cpus);
3154 if (map)
3155 evsel->own_cpus = map;
3156 else
3157 pr_err("failed to get event_update cpus\n");
Jiri Olsaa6e52812015-10-25 15:51:37 +01003158 default:
3159 break;
3160 }
3161
Jiri Olsaffe777252015-10-25 15:51:36 +01003162 return 0;
3163}
3164
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003165int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003166 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003167 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05003168{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003169 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02003170 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05003171 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003172 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05003173
Jiri Olsa29208e52011-10-20 15:59:43 +02003174 /*
3175 * We are going to store the size of the data followed
3176 * by the data contents. Since the fd descriptor is a pipe,
3177 * we cannot seek back to store the size of the data once
3178 * we know it. Instead we:
3179 *
3180 * - write the tracing data to the temp file
3181 * - get/write the data size to pipe
3182 * - write the tracing data from the temp file
3183 * to the pipe
3184 */
3185 tdata = tracing_data_get(&evlist->entries, fd, true);
3186 if (!tdata)
3187 return -1;
3188
Tom Zanussi92155452010-04-01 23:59:21 -05003189 memset(&ev, 0, sizeof(ev));
3190
3191 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02003192 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003193 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05003194 padding = aligned_size - size;
3195 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3196 ev.tracing_data.size = aligned_size;
3197
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003198 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05003199
Jiri Olsa29208e52011-10-20 15:59:43 +02003200 /*
3201 * The put function will copy all the tracing data
3202 * stored in temp file to the pipe.
3203 */
3204 tracing_data_put(tdata);
3205
David Carrillo-Cisneros2ff53652017-07-17 21:25:36 -07003206 if (write_padded(fd, NULL, 0, padding))
3207 return -1;
Tom Zanussi92155452010-04-01 23:59:21 -05003208
3209 return aligned_size;
3210}
3211
Adrian Hunter47c3d102013-07-04 16:20:21 +03003212int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3213 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003214 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05003215{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003216 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003217 int fd = perf_data_file__fd(session->file);
3218 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05003219 char buf[BUFSIZ];
3220
3221 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003222 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05003223 SEEK_SET);
3224
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003225 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003226 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003227 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003228
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003229 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003230 pr_err("%s: reading input file", __func__);
3231 return -1;
3232 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003233 if (session->repipe) {
3234 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003235 if (retw <= 0 || retw != padding) {
3236 pr_err("%s: repiping tracing data padding", __func__);
3237 return -1;
3238 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003239 }
Tom Zanussi92155452010-04-01 23:59:21 -05003240
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003241 if (size_read + padding != size) {
3242 pr_err("%s: tracing data size mismatch", __func__);
3243 return -1;
3244 }
Tom Zanussi92155452010-04-01 23:59:21 -05003245
Namhyung Kim831394b2012-09-06 11:10:46 +09003246 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003247 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003248
Tom Zanussi92155452010-04-01 23:59:21 -05003249 return size_read + padding;
3250}
Tom Zanussic7929e42010-04-01 23:59:22 -05003251
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003252int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003253 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003254 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003255 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003256{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003257 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003258 size_t len;
3259 int err = 0;
3260
3261 if (!pos->hit)
3262 return err;
3263
3264 memset(&ev, 0, sizeof(ev));
3265
3266 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003267 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003268 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3269 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3270 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003271 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003272 ev.build_id.header.size = sizeof(ev.build_id) + len;
3273 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3274
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003275 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003276
3277 return err;
3278}
3279
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003280int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003281 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003282 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003283{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003284 __event_process_build_id(&event->build_id,
3285 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003286 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003287 return 0;
3288}