blob: 51865b8cb9f355ae18d581862fd05400996e7165 [file] [log] [blame]
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03001#include <inttypes.h>
Arnaldo Carvalho de Meloa9072bc2011-10-26 12:41:38 -02002#include "util.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -03003#include "string2.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02004#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02005#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02006#include <unistd.h>
7#include <stdio.h>
8#include <stdlib.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +01009#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -020010#include <linux/kernel.h>
Robert Richterb1e5a9b2011-12-07 10:02:57 +010011#include <linux/bitops.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020012#include <sys/utsname.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020013
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020014#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030015#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020016#include "header.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020017#include "../perf.h"
18#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020019#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010020#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010021#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020022#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020023#include "pmu.h"
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020024#include "vdso.h"
Namhyung Kima1ae5652012-09-24 17:14:59 +090025#include "strbuf.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020026#include "build-id.h"
Jiri Olsacc9784bd2013-10-15 16:27:34 +020027#include "data.h"
Jiri Olsa720e98b2016-02-16 16:01:43 +010028#include <api/fs/fs.h>
29#include "asm/bug.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020030
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030031#include "sane_ctype.h"
32
Stephane Eranian73323f52012-02-02 13:54:44 +010033/*
34 * magic2 = "PERFILE2"
35 * must be a numerical value to let the endianness
36 * determine the memory layout. That way we are able
37 * to detect endianness when reading the perf.data file
38 * back.
39 *
40 * we check for legacy (PERFFILE) format.
41 */
42static const char *__perf_magic1 = "PERFFILE";
43static const u64 __perf_magic2 = 0x32454c4946524550ULL;
44static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020045
Stephane Eranian73323f52012-02-02 13:54:44 +010046#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020047
Soramichi AKIYAMAd25ed5d2017-01-17 00:22:37 +090048const char perf_version_string[] = PERF_VERSION;
49
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020050struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020051 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020052 struct perf_file_section ids;
53};
54
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030055void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020056{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030057 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020058}
59
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030060void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020061{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030062 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020063}
64
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030065bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020066{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030067 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020068}
69
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020070static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020071{
72 while (size) {
73 int ret = write(fd, buf, size);
74
75 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -020076 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020077
78 size -= ret;
79 buf += ret;
80 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020081
82 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020083}
84
Namhyung Kime195fac2014-11-04 10:14:30 +090085int write_padded(int fd, const void *bf, size_t count, size_t count_aligned)
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -020086{
87 static const char zero_buf[NAME_ALIGN];
88 int err = do_write(fd, bf, count);
89
90 if (!err)
91 err = do_write(fd, zero_buf, count_aligned - count);
92
93 return err;
94}
95
Kan Liang2bb00d22015-09-01 09:58:12 -040096#define string_size(str) \
97 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
98
Stephane Eranianfbe96f22011-09-30 15:40:40 +020099static int do_write_string(int fd, const char *str)
100{
101 u32 len, olen;
102 int ret;
103
104 olen = strlen(str) + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +0300105 len = PERF_ALIGN(olen, NAME_ALIGN);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200106
107 /* write len, incl. \0 */
108 ret = do_write(fd, &len, sizeof(len));
109 if (ret < 0)
110 return ret;
111
112 return write_padded(fd, str, olen, len);
113}
114
115static char *do_read_string(int fd, struct perf_header *ph)
116{
117 ssize_t sz, ret;
118 u32 len;
119 char *buf;
120
Namhyung Kim5323f602012-12-17 15:38:54 +0900121 sz = readn(fd, &len, sizeof(len));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200122 if (sz < (ssize_t)sizeof(len))
123 return NULL;
124
125 if (ph->needs_swap)
126 len = bswap_32(len);
127
128 buf = malloc(len);
129 if (!buf)
130 return NULL;
131
Namhyung Kim5323f602012-12-17 15:38:54 +0900132 ret = readn(fd, buf, len);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200133 if (ret == (ssize_t)len) {
134 /*
135 * strings are padded by zeroes
136 * thus the actual strlen of buf
137 * may be less than len
138 */
139 return buf;
140 }
141
142 free(buf);
143 return NULL;
144}
145
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300146static int write_tracing_data(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200147 struct perf_evlist *evlist)
148{
149 return read_tracing_data(fd, &evlist->entries);
150}
151
152
153static int write_build_id(int fd, struct perf_header *h,
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300154 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200155{
156 struct perf_session *session;
157 int err;
158
159 session = container_of(h, struct perf_session, header);
160
Robert Richtere20960c2011-12-07 10:02:55 +0100161 if (!perf_session__read_build_ids(session, true))
162 return -1;
163
Namhyung Kim714c9c42014-11-04 10:14:29 +0900164 err = perf_session__write_buildid_table(session, fd);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200165 if (err < 0) {
166 pr_debug("failed to write buildid table\n");
167 return err;
168 }
Namhyung Kim73c5d222014-11-07 22:57:56 +0900169 perf_session__cache_build_ids(session);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200170
171 return 0;
172}
173
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300174static int write_hostname(int fd, struct perf_header *h __maybe_unused,
175 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200176{
177 struct utsname uts;
178 int ret;
179
180 ret = uname(&uts);
181 if (ret < 0)
182 return -1;
183
184 return do_write_string(fd, uts.nodename);
185}
186
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300187static int write_osrelease(int fd, struct perf_header *h __maybe_unused,
188 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200189{
190 struct utsname uts;
191 int ret;
192
193 ret = uname(&uts);
194 if (ret < 0)
195 return -1;
196
197 return do_write_string(fd, uts.release);
198}
199
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300200static int write_arch(int fd, struct perf_header *h __maybe_unused,
201 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200202{
203 struct utsname uts;
204 int ret;
205
206 ret = uname(&uts);
207 if (ret < 0)
208 return -1;
209
210 return do_write_string(fd, uts.machine);
211}
212
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300213static int write_version(int fd, struct perf_header *h __maybe_unused,
214 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200215{
216 return do_write_string(fd, perf_version_string);
217}
218
Wang Nan493c3032014-10-24 09:45:26 +0800219static int __write_cpudesc(int fd, const char *cpuinfo_proc)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200220{
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200221 FILE *file;
222 char *buf = NULL;
223 char *s, *p;
Wang Nan493c3032014-10-24 09:45:26 +0800224 const char *search = cpuinfo_proc;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200225 size_t len = 0;
226 int ret = -1;
227
228 if (!search)
229 return -1;
230
231 file = fopen("/proc/cpuinfo", "r");
232 if (!file)
233 return -1;
234
235 while (getline(&buf, &len, file) > 0) {
236 ret = strncmp(buf, search, strlen(search));
237 if (!ret)
238 break;
239 }
240
Wang Naned307752014-10-16 11:08:29 +0800241 if (ret) {
242 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200243 goto done;
Wang Naned307752014-10-16 11:08:29 +0800244 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200245
246 s = buf;
247
248 p = strchr(buf, ':');
249 if (p && *(p+1) == ' ' && *(p+2))
250 s = p + 2;
251 p = strchr(s, '\n');
252 if (p)
253 *p = '\0';
254
255 /* squash extra space characters (branding string) */
256 p = s;
257 while (*p) {
258 if (isspace(*p)) {
259 char *r = p + 1;
260 char *q = r;
261 *p = ' ';
262 while (*q && isspace(*q))
263 q++;
264 if (q != (p+1))
265 while ((*r++ = *q++));
266 }
267 p++;
268 }
269 ret = do_write_string(fd, s);
270done:
271 free(buf);
272 fclose(file);
273 return ret;
274}
275
Wang Nan493c3032014-10-24 09:45:26 +0800276static int write_cpudesc(int fd, struct perf_header *h __maybe_unused,
277 struct perf_evlist *evlist __maybe_unused)
278{
279#ifndef CPUINFO_PROC
280#define CPUINFO_PROC {"model name", }
281#endif
282 const char *cpuinfo_procs[] = CPUINFO_PROC;
283 unsigned int i;
284
285 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
286 int ret;
287 ret = __write_cpudesc(fd, cpuinfo_procs[i]);
288 if (ret >= 0)
289 return ret;
290 }
291 return -1;
292}
293
294
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300295static int write_nrcpus(int fd, struct perf_header *h __maybe_unused,
296 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200297{
298 long nr;
299 u32 nrc, nra;
300 int ret;
301
Jan Stancekda8a58b2017-02-17 12:10:26 +0100302 nrc = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200303
304 nr = sysconf(_SC_NPROCESSORS_ONLN);
305 if (nr < 0)
306 return -1;
307
308 nra = (u32)(nr & UINT_MAX);
309
310 ret = do_write(fd, &nrc, sizeof(nrc));
311 if (ret < 0)
312 return ret;
313
314 return do_write(fd, &nra, sizeof(nra));
315}
316
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300317static int write_event_desc(int fd, struct perf_header *h __maybe_unused,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200318 struct perf_evlist *evlist)
319{
Robert Richter6606f872012-08-16 21:10:19 +0200320 struct perf_evsel *evsel;
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900321 u32 nre, nri, sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200322 int ret;
323
Namhyung Kim74ba9e12012-09-05 14:02:47 +0900324 nre = evlist->nr_entries;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200325
326 /*
327 * write number of events
328 */
329 ret = do_write(fd, &nre, sizeof(nre));
330 if (ret < 0)
331 return ret;
332
333 /*
334 * size of perf_event_attr struct
335 */
Robert Richter6606f872012-08-16 21:10:19 +0200336 sz = (u32)sizeof(evsel->attr);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200337 ret = do_write(fd, &sz, sizeof(sz));
338 if (ret < 0)
339 return ret;
340
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300341 evlist__for_each_entry(evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +0200342 ret = do_write(fd, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200343 if (ret < 0)
344 return ret;
345 /*
346 * write number of unique id per event
347 * there is one id per instance of an event
348 *
349 * copy into an nri to be independent of the
350 * type of ids,
351 */
Robert Richter6606f872012-08-16 21:10:19 +0200352 nri = evsel->ids;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200353 ret = do_write(fd, &nri, sizeof(nri));
354 if (ret < 0)
355 return ret;
356
357 /*
358 * write event string as passed on cmdline
359 */
Robert Richter6606f872012-08-16 21:10:19 +0200360 ret = do_write_string(fd, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200361 if (ret < 0)
362 return ret;
363 /*
364 * write unique ids for this event
365 */
Robert Richter6606f872012-08-16 21:10:19 +0200366 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200367 if (ret < 0)
368 return ret;
369 }
370 return 0;
371}
372
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300373static int write_cmdline(int fd, struct perf_header *h __maybe_unused,
374 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200375{
376 char buf[MAXPATHLEN];
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300377 u32 n;
378 int i, ret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200379
Tommi Rantala55f771282017-03-22 15:06:24 +0200380 /* actual path to perf binary */
381 ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200382 if (ret <= 0)
383 return -1;
384
385 /* readlink() does not add null termination */
386 buf[ret] = '\0';
387
388 /* account for binary path */
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300389 n = perf_env.nr_cmdline + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200390
391 ret = do_write(fd, &n, sizeof(n));
392 if (ret < 0)
393 return ret;
394
395 ret = do_write_string(fd, buf);
396 if (ret < 0)
397 return ret;
398
Arnaldo Carvalho de Melob6998692015-09-08 16:58:20 -0300399 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
400 ret = do_write_string(fd, perf_env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200401 if (ret < 0)
402 return ret;
403 }
404 return 0;
405}
406
407#define CORE_SIB_FMT \
408 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
409#define THRD_SIB_FMT \
410 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
411
412struct cpu_topo {
Kan Liang2bb00d22015-09-01 09:58:12 -0400413 u32 cpu_nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200414 u32 core_sib;
415 u32 thread_sib;
416 char **core_siblings;
417 char **thread_siblings;
418};
419
420static int build_cpu_topo(struct cpu_topo *tp, int cpu)
421{
422 FILE *fp;
423 char filename[MAXPATHLEN];
424 char *buf = NULL, *p;
425 size_t len = 0;
Stephane Eranianc5885742013-08-14 12:04:26 +0200426 ssize_t sret;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200427 u32 i = 0;
428 int ret = -1;
429
430 sprintf(filename, CORE_SIB_FMT, cpu);
431 fp = fopen(filename, "r");
432 if (!fp)
Stephane Eranianc5885742013-08-14 12:04:26 +0200433 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200434
Stephane Eranianc5885742013-08-14 12:04:26 +0200435 sret = getline(&buf, &len, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200436 fclose(fp);
Stephane Eranianc5885742013-08-14 12:04:26 +0200437 if (sret <= 0)
438 goto try_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200439
440 p = strchr(buf, '\n');
441 if (p)
442 *p = '\0';
443
444 for (i = 0; i < tp->core_sib; i++) {
445 if (!strcmp(buf, tp->core_siblings[i]))
446 break;
447 }
448 if (i == tp->core_sib) {
449 tp->core_siblings[i] = buf;
450 tp->core_sib++;
451 buf = NULL;
452 len = 0;
453 }
Stephane Eranianc5885742013-08-14 12:04:26 +0200454 ret = 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200455
Stephane Eranianc5885742013-08-14 12:04:26 +0200456try_threads:
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200457 sprintf(filename, THRD_SIB_FMT, cpu);
458 fp = fopen(filename, "r");
459 if (!fp)
460 goto done;
461
462 if (getline(&buf, &len, fp) <= 0)
463 goto done;
464
465 p = strchr(buf, '\n');
466 if (p)
467 *p = '\0';
468
469 for (i = 0; i < tp->thread_sib; i++) {
470 if (!strcmp(buf, tp->thread_siblings[i]))
471 break;
472 }
473 if (i == tp->thread_sib) {
474 tp->thread_siblings[i] = buf;
475 tp->thread_sib++;
476 buf = NULL;
477 }
478 ret = 0;
479done:
480 if(fp)
481 fclose(fp);
482 free(buf);
483 return ret;
484}
485
486static void free_cpu_topo(struct cpu_topo *tp)
487{
488 u32 i;
489
490 if (!tp)
491 return;
492
493 for (i = 0 ; i < tp->core_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300494 zfree(&tp->core_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200495
496 for (i = 0 ; i < tp->thread_sib; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300497 zfree(&tp->thread_siblings[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200498
499 free(tp);
500}
501
502static struct cpu_topo *build_cpu_topology(void)
503{
Jan Stancek43db2842017-02-17 12:10:25 +0100504 struct cpu_topo *tp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200505 void *addr;
506 u32 nr, i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300507 size_t sz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200508 long ncpus;
509 int ret = -1;
Jan Stancek43db2842017-02-17 12:10:25 +0100510 struct cpu_map *map;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200511
Jan Stancekda8a58b2017-02-17 12:10:26 +0100512 ncpus = cpu__max_present_cpu();
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200513
Jan Stancek43db2842017-02-17 12:10:25 +0100514 /* build online CPU map */
515 map = cpu_map__new(NULL);
516 if (map == NULL) {
517 pr_debug("failed to get system cpumap\n");
518 return NULL;
519 }
520
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200521 nr = (u32)(ncpus & UINT_MAX);
522
523 sz = nr * sizeof(char *);
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300524 addr = calloc(1, sizeof(*tp) + 2 * sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200525 if (!addr)
Jan Stancek43db2842017-02-17 12:10:25 +0100526 goto out_free;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200527
528 tp = addr;
Kan Liang2bb00d22015-09-01 09:58:12 -0400529 tp->cpu_nr = nr;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200530 addr += sizeof(*tp);
531 tp->core_siblings = addr;
532 addr += sz;
533 tp->thread_siblings = addr;
534
535 for (i = 0; i < nr; i++) {
Jan Stancek43db2842017-02-17 12:10:25 +0100536 if (!cpu_map__has(map, i))
537 continue;
538
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200539 ret = build_cpu_topo(tp, i);
540 if (ret < 0)
541 break;
542 }
Jan Stancek43db2842017-02-17 12:10:25 +0100543
544out_free:
545 cpu_map__put(map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200546 if (ret) {
547 free_cpu_topo(tp);
548 tp = NULL;
549 }
550 return tp;
551}
552
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300553static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused,
554 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200555{
556 struct cpu_topo *tp;
557 u32 i;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300558 int ret, j;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200559
560 tp = build_cpu_topology();
561 if (!tp)
562 return -1;
563
564 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
565 if (ret < 0)
566 goto done;
567
568 for (i = 0; i < tp->core_sib; i++) {
569 ret = do_write_string(fd, tp->core_siblings[i]);
570 if (ret < 0)
571 goto done;
572 }
573 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
574 if (ret < 0)
575 goto done;
576
577 for (i = 0; i < tp->thread_sib; i++) {
578 ret = do_write_string(fd, tp->thread_siblings[i]);
579 if (ret < 0)
580 break;
581 }
Kan Liang2bb00d22015-09-01 09:58:12 -0400582
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300583 ret = perf_env__read_cpu_topology_map(&perf_env);
584 if (ret < 0)
585 goto done;
586
587 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
588 ret = do_write(fd, &perf_env.cpu[j].core_id,
589 sizeof(perf_env.cpu[j].core_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400590 if (ret < 0)
591 return ret;
Arnaldo Carvalho de Meloaa36ddd2015-09-09 10:37:01 -0300592 ret = do_write(fd, &perf_env.cpu[j].socket_id,
593 sizeof(perf_env.cpu[j].socket_id));
Kan Liang2bb00d22015-09-01 09:58:12 -0400594 if (ret < 0)
595 return ret;
596 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200597done:
598 free_cpu_topo(tp);
599 return ret;
600}
601
602
603
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300604static int write_total_mem(int fd, struct perf_header *h __maybe_unused,
605 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200606{
607 char *buf = NULL;
608 FILE *fp;
609 size_t len = 0;
610 int ret = -1, n;
611 uint64_t mem;
612
613 fp = fopen("/proc/meminfo", "r");
614 if (!fp)
615 return -1;
616
617 while (getline(&buf, &len, fp) > 0) {
618 ret = strncmp(buf, "MemTotal:", 9);
619 if (!ret)
620 break;
621 }
622 if (!ret) {
623 n = sscanf(buf, "%*s %"PRIu64, &mem);
624 if (n == 1)
625 ret = do_write(fd, &mem, sizeof(mem));
Wang Naned307752014-10-16 11:08:29 +0800626 } else
627 ret = -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200628 free(buf);
629 fclose(fp);
630 return ret;
631}
632
633static int write_topo_node(int fd, int node)
634{
635 char str[MAXPATHLEN];
636 char field[32];
637 char *buf = NULL, *p;
638 size_t len = 0;
639 FILE *fp;
640 u64 mem_total, mem_free, mem;
641 int ret = -1;
642
643 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
644 fp = fopen(str, "r");
645 if (!fp)
646 return -1;
647
648 while (getline(&buf, &len, fp) > 0) {
649 /* skip over invalid lines */
650 if (!strchr(buf, ':'))
651 continue;
Alan Coxa761a2d2014-01-20 19:10:11 +0100652 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200653 goto done;
654 if (!strcmp(field, "MemTotal:"))
655 mem_total = mem;
656 if (!strcmp(field, "MemFree:"))
657 mem_free = mem;
658 }
659
660 fclose(fp);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100661 fp = NULL;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200662
663 ret = do_write(fd, &mem_total, sizeof(u64));
664 if (ret)
665 goto done;
666
667 ret = do_write(fd, &mem_free, sizeof(u64));
668 if (ret)
669 goto done;
670
671 ret = -1;
672 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
673
674 fp = fopen(str, "r");
675 if (!fp)
676 goto done;
677
678 if (getline(&buf, &len, fp) <= 0)
679 goto done;
680
681 p = strchr(buf, '\n');
682 if (p)
683 *p = '\0';
684
685 ret = do_write_string(fd, buf);
686done:
687 free(buf);
Thomas Jarosch5809fde2013-01-28 10:21:14 +0100688 if (fp)
689 fclose(fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200690 return ret;
691}
692
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300693static int write_numa_topology(int fd, struct perf_header *h __maybe_unused,
694 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200695{
696 char *buf = NULL;
697 size_t len = 0;
698 FILE *fp;
699 struct cpu_map *node_map = NULL;
700 char *c;
701 u32 nr, i, j;
702 int ret = -1;
703
704 fp = fopen("/sys/devices/system/node/online", "r");
705 if (!fp)
706 return -1;
707
708 if (getline(&buf, &len, fp) <= 0)
709 goto done;
710
711 c = strchr(buf, '\n');
712 if (c)
713 *c = '\0';
714
715 node_map = cpu_map__new(buf);
716 if (!node_map)
717 goto done;
718
719 nr = (u32)node_map->nr;
720
721 ret = do_write(fd, &nr, sizeof(nr));
722 if (ret < 0)
723 goto done;
724
725 for (i = 0; i < nr; i++) {
726 j = (u32)node_map->map[i];
727 ret = do_write(fd, &j, sizeof(j));
728 if (ret < 0)
729 break;
730
731 ret = write_topo_node(fd, i);
732 if (ret < 0)
733 break;
734 }
735done:
736 free(buf);
737 fclose(fp);
Masami Hiramatsu5191d8872015-12-09 11:11:35 +0900738 cpu_map__put(node_map);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200739 return ret;
740}
741
742/*
Robert Richter50a96672012-08-16 21:10:24 +0200743 * File format:
744 *
745 * struct pmu_mappings {
746 * u32 pmu_num;
747 * struct pmu_map {
748 * u32 type;
749 * char name[];
750 * }[pmu_num];
751 * };
752 */
753
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300754static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused,
755 struct perf_evlist *evlist __maybe_unused)
Robert Richter50a96672012-08-16 21:10:24 +0200756{
757 struct perf_pmu *pmu = NULL;
758 off_t offset = lseek(fd, 0, SEEK_CUR);
759 __u32 pmu_num = 0;
Namhyung Kim5323f602012-12-17 15:38:54 +0900760 int ret;
Robert Richter50a96672012-08-16 21:10:24 +0200761
762 /* write real pmu_num later */
Namhyung Kim5323f602012-12-17 15:38:54 +0900763 ret = do_write(fd, &pmu_num, sizeof(pmu_num));
764 if (ret < 0)
765 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200766
767 while ((pmu = perf_pmu__scan(pmu))) {
768 if (!pmu->name)
769 continue;
770 pmu_num++;
Namhyung Kim5323f602012-12-17 15:38:54 +0900771
772 ret = do_write(fd, &pmu->type, sizeof(pmu->type));
773 if (ret < 0)
774 return ret;
775
776 ret = do_write_string(fd, pmu->name);
777 if (ret < 0)
778 return ret;
Robert Richter50a96672012-08-16 21:10:24 +0200779 }
780
781 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
782 /* discard all */
783 lseek(fd, offset, SEEK_SET);
784 return -1;
785 }
786
787 return 0;
788}
789
790/*
Namhyung Kima8bb5592013-01-22 18:09:31 +0900791 * File format:
792 *
793 * struct group_descs {
794 * u32 nr_groups;
795 * struct group_desc {
796 * char name[];
797 * u32 leader_idx;
798 * u32 nr_members;
799 * }[nr_groups];
800 * };
801 */
802static int write_group_desc(int fd, struct perf_header *h __maybe_unused,
803 struct perf_evlist *evlist)
804{
805 u32 nr_groups = evlist->nr_groups;
806 struct perf_evsel *evsel;
807 int ret;
808
809 ret = do_write(fd, &nr_groups, sizeof(nr_groups));
810 if (ret < 0)
811 return ret;
812
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300813 evlist__for_each_entry(evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +0900814 if (perf_evsel__is_group_leader(evsel) &&
815 evsel->nr_members > 1) {
816 const char *name = evsel->group_name ?: "{anon_group}";
817 u32 leader_idx = evsel->idx;
818 u32 nr_members = evsel->nr_members;
819
820 ret = do_write_string(fd, name);
821 if (ret < 0)
822 return ret;
823
824 ret = do_write(fd, &leader_idx, sizeof(leader_idx));
825 if (ret < 0)
826 return ret;
827
828 ret = do_write(fd, &nr_members, sizeof(nr_members));
829 if (ret < 0)
830 return ret;
831 }
832 }
833 return 0;
834}
835
836/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200837 * default get_cpuid(): nothing gets recorded
838 * actual implementation must be in arch/$(ARCH)/util/header.c
839 */
Rui Teng11d8f872016-07-28 10:05:57 +0800840int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200841{
842 return -1;
843}
844
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300845static int write_cpuid(int fd, struct perf_header *h __maybe_unused,
846 struct perf_evlist *evlist __maybe_unused)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200847{
848 char buffer[64];
849 int ret;
850
851 ret = get_cpuid(buffer, sizeof(buffer));
852 if (!ret)
853 goto write_it;
854
855 return -1;
856write_it:
857 return do_write_string(fd, buffer);
858}
859
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300860static int write_branch_stack(int fd __maybe_unused,
861 struct perf_header *h __maybe_unused,
862 struct perf_evlist *evlist __maybe_unused)
Stephane Eranian330aa672012-03-08 23:47:46 +0100863{
864 return 0;
865}
866
Adrian Hunter99fa2982015-04-30 17:37:25 +0300867static int write_auxtrace(int fd, struct perf_header *h,
Adrian Hunter4025ea42015-04-09 18:53:41 +0300868 struct perf_evlist *evlist __maybe_unused)
869{
Adrian Hunter99fa2982015-04-30 17:37:25 +0300870 struct perf_session *session;
871 int err;
872
873 session = container_of(h, struct perf_session, header);
874
875 err = auxtrace_index__write(fd, &session->auxtrace_index);
876 if (err < 0)
877 pr_err("Failed to write auxtrace index\n");
878 return err;
Adrian Hunter4025ea42015-04-09 18:53:41 +0300879}
880
Jiri Olsa720e98b2016-02-16 16:01:43 +0100881static int cpu_cache_level__sort(const void *a, const void *b)
882{
883 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
884 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
885
886 return cache_a->level - cache_b->level;
887}
888
889static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
890{
891 if (a->level != b->level)
892 return false;
893
894 if (a->line_size != b->line_size)
895 return false;
896
897 if (a->sets != b->sets)
898 return false;
899
900 if (a->ways != b->ways)
901 return false;
902
903 if (strcmp(a->type, b->type))
904 return false;
905
906 if (strcmp(a->size, b->size))
907 return false;
908
909 if (strcmp(a->map, b->map))
910 return false;
911
912 return true;
913}
914
915static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
916{
917 char path[PATH_MAX], file[PATH_MAX];
918 struct stat st;
919 size_t len;
920
921 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
922 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
923
924 if (stat(file, &st))
925 return 1;
926
927 scnprintf(file, PATH_MAX, "%s/level", path);
928 if (sysfs__read_int(file, (int *) &cache->level))
929 return -1;
930
931 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
932 if (sysfs__read_int(file, (int *) &cache->line_size))
933 return -1;
934
935 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
936 if (sysfs__read_int(file, (int *) &cache->sets))
937 return -1;
938
939 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
940 if (sysfs__read_int(file, (int *) &cache->ways))
941 return -1;
942
943 scnprintf(file, PATH_MAX, "%s/type", path);
944 if (sysfs__read_str(file, &cache->type, &len))
945 return -1;
946
947 cache->type[len] = 0;
948 cache->type = rtrim(cache->type);
949
950 scnprintf(file, PATH_MAX, "%s/size", path);
951 if (sysfs__read_str(file, &cache->size, &len)) {
952 free(cache->type);
953 return -1;
954 }
955
956 cache->size[len] = 0;
957 cache->size = rtrim(cache->size);
958
959 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
960 if (sysfs__read_str(file, &cache->map, &len)) {
961 free(cache->map);
962 free(cache->type);
963 return -1;
964 }
965
966 cache->map[len] = 0;
967 cache->map = rtrim(cache->map);
968 return 0;
969}
970
971static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
972{
973 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
974}
975
976static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
977{
978 u32 i, cnt = 0;
979 long ncpus;
980 u32 nr, cpu;
981 u16 level;
982
983 ncpus = sysconf(_SC_NPROCESSORS_CONF);
984 if (ncpus < 0)
985 return -1;
986
987 nr = (u32)(ncpus & UINT_MAX);
988
989 for (cpu = 0; cpu < nr; cpu++) {
990 for (level = 0; level < 10; level++) {
991 struct cpu_cache_level c;
992 int err;
993
994 err = cpu_cache_level__read(&c, cpu, level);
995 if (err < 0)
996 return err;
997
998 if (err == 1)
999 break;
1000
1001 for (i = 0; i < cnt; i++) {
1002 if (cpu_cache_level__cmp(&c, &caches[i]))
1003 break;
1004 }
1005
1006 if (i == cnt)
1007 caches[cnt++] = c;
1008 else
1009 cpu_cache_level__free(&c);
1010
1011 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1012 goto out;
1013 }
1014 }
1015 out:
1016 *cntp = cnt;
1017 return 0;
1018}
1019
1020#define MAX_CACHES 2000
1021
1022static int write_cache(int fd, struct perf_header *h __maybe_unused,
1023 struct perf_evlist *evlist __maybe_unused)
1024{
1025 struct cpu_cache_level caches[MAX_CACHES];
1026 u32 cnt = 0, i, version = 1;
1027 int ret;
1028
1029 ret = build_caches(caches, MAX_CACHES, &cnt);
1030 if (ret)
1031 goto out;
1032
1033 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1034
1035 ret = do_write(fd, &version, sizeof(u32));
1036 if (ret < 0)
1037 goto out;
1038
1039 ret = do_write(fd, &cnt, sizeof(u32));
1040 if (ret < 0)
1041 goto out;
1042
1043 for (i = 0; i < cnt; i++) {
1044 struct cpu_cache_level *c = &caches[i];
1045
1046 #define _W(v) \
1047 ret = do_write(fd, &c->v, sizeof(u32)); \
1048 if (ret < 0) \
1049 goto out;
1050
1051 _W(level)
1052 _W(line_size)
1053 _W(sets)
1054 _W(ways)
1055 #undef _W
1056
1057 #define _W(v) \
1058 ret = do_write_string(fd, (const char *) c->v); \
1059 if (ret < 0) \
1060 goto out;
1061
1062 _W(type)
1063 _W(size)
1064 _W(map)
1065 #undef _W
1066 }
1067
1068out:
1069 for (i = 0; i < cnt; i++)
1070 cpu_cache_level__free(&caches[i]);
1071 return ret;
1072}
1073
Jiri Olsaffa517a2015-10-25 15:51:43 +01001074static int write_stat(int fd __maybe_unused,
1075 struct perf_header *h __maybe_unused,
1076 struct perf_evlist *evlist __maybe_unused)
1077{
1078 return 0;
1079}
1080
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001081static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
1082 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001083{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001084 fprintf(fp, "# hostname : %s\n", ph->env.hostname);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001085}
1086
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001087static void print_osrelease(struct perf_header *ph, int fd __maybe_unused,
1088 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001089{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001090 fprintf(fp, "# os release : %s\n", ph->env.os_release);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001091}
1092
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001093static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001094{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001095 fprintf(fp, "# arch : %s\n", ph->env.arch);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001096}
1097
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001098static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused,
1099 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001100{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001101 fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001102}
1103
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001104static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused,
1105 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001106{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001107 fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online);
1108 fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001109}
1110
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001111static void print_version(struct perf_header *ph, int fd __maybe_unused,
1112 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001113{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001114 fprintf(fp, "# perf version : %s\n", ph->env.version);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001115}
1116
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001117static void print_cmdline(struct perf_header *ph, int fd __maybe_unused,
1118 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001119{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001120 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001121
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001122 nr = ph->env.nr_cmdline;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001123
1124 fprintf(fp, "# cmdline : ");
1125
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001126 for (i = 0; i < nr; i++)
1127 fprintf(fp, "%s ", ph->env.cmdline_argv[i]);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001128 fputc('\n', fp);
1129}
1130
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001131static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused,
1132 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001133{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001134 int nr, i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001135 char *str;
Jan Stancekda8a58b2017-02-17 12:10:26 +01001136 int cpu_nr = ph->env.nr_cpus_avail;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001137
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001138 nr = ph->env.nr_sibling_cores;
1139 str = ph->env.sibling_cores;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001140
1141 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001142 fprintf(fp, "# sibling cores : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001143 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001144 }
1145
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001146 nr = ph->env.nr_sibling_threads;
1147 str = ph->env.sibling_threads;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001148
1149 for (i = 0; i < nr; i++) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001150 fprintf(fp, "# sibling threads : %s\n", str);
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001151 str += strlen(str) + 1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001152 }
Kan Liang2bb00d22015-09-01 09:58:12 -04001153
1154 if (ph->env.cpu != NULL) {
1155 for (i = 0; i < cpu_nr; i++)
1156 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1157 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1158 } else
1159 fprintf(fp, "# Core ID and Socket ID information is not available\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001160}
1161
Robert Richter4e1b9c62012-08-16 21:10:22 +02001162static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001163{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001164 struct perf_evsel *evsel;
1165
1166 if (!events)
1167 return;
1168
1169 for (evsel = events; evsel->attr.size; evsel++) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001170 zfree(&evsel->name);
1171 zfree(&evsel->id);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001172 }
1173
1174 free(events);
1175}
1176
1177static struct perf_evsel *
1178read_event_desc(struct perf_header *ph, int fd)
1179{
1180 struct perf_evsel *evsel, *events = NULL;
1181 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001182 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001183 u32 nre, sz, nr, i, j;
1184 ssize_t ret;
1185 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001186
1187 /* number of events */
Namhyung Kim5323f602012-12-17 15:38:54 +09001188 ret = readn(fd, &nre, sizeof(nre));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001189 if (ret != (ssize_t)sizeof(nre))
1190 goto error;
1191
1192 if (ph->needs_swap)
1193 nre = bswap_32(nre);
1194
Namhyung Kim5323f602012-12-17 15:38:54 +09001195 ret = readn(fd, &sz, sizeof(sz));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001196 if (ret != (ssize_t)sizeof(sz))
1197 goto error;
1198
1199 if (ph->needs_swap)
1200 sz = bswap_32(sz);
1201
Stephane Eranian62db9062012-02-09 23:21:07 +01001202 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001203 buf = malloc(sz);
1204 if (!buf)
1205 goto error;
1206
Robert Richter4e1b9c62012-08-16 21:10:22 +02001207 /* the last event terminates with evsel->attr.size == 0: */
1208 events = calloc(nre + 1, sizeof(*events));
1209 if (!events)
1210 goto error;
1211
1212 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001213 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001214 msz = sz;
1215
Robert Richter4e1b9c62012-08-16 21:10:22 +02001216 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1217 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001218
Stephane Eranian62db9062012-02-09 23:21:07 +01001219 /*
1220 * must read entire on-file attr struct to
1221 * sync up with layout.
1222 */
Namhyung Kim5323f602012-12-17 15:38:54 +09001223 ret = readn(fd, buf, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001224 if (ret != (ssize_t)sz)
1225 goto error;
1226
1227 if (ph->needs_swap)
1228 perf_event__attr_swap(buf);
1229
Robert Richter4e1b9c62012-08-16 21:10:22 +02001230 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001231
Namhyung Kim5323f602012-12-17 15:38:54 +09001232 ret = readn(fd, &nr, sizeof(nr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001233 if (ret != (ssize_t)sizeof(nr))
1234 goto error;
1235
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001236 if (ph->needs_swap) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001237 nr = bswap_32(nr);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001238 evsel->needs_swap = true;
1239 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001240
Robert Richter4e1b9c62012-08-16 21:10:22 +02001241 evsel->name = do_read_string(fd, ph);
1242
1243 if (!nr)
1244 continue;
1245
1246 id = calloc(nr, sizeof(*id));
1247 if (!id)
1248 goto error;
1249 evsel->ids = nr;
1250 evsel->id = id;
1251
1252 for (j = 0 ; j < nr; j++) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001253 ret = readn(fd, id, sizeof(*id));
Robert Richter4e1b9c62012-08-16 21:10:22 +02001254 if (ret != (ssize_t)sizeof(*id))
1255 goto error;
1256 if (ph->needs_swap)
1257 *id = bswap_64(*id);
1258 id++;
1259 }
1260 }
1261out:
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001262 free(buf);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001263 return events;
1264error:
Markus Elfring4cc97612015-06-25 17:12:32 +02001265 free_event_desc(events);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001266 events = NULL;
1267 goto out;
1268}
1269
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001270static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1271 void *priv __attribute__((unused)))
1272{
1273 return fprintf(fp, ", %s = %s", name, val);
1274}
1275
Robert Richter4e1b9c62012-08-16 21:10:22 +02001276static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1277{
1278 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1279 u32 j;
1280 u64 *id;
1281
1282 if (!events) {
1283 fprintf(fp, "# event desc: not available or unable to read\n");
1284 return;
1285 }
1286
1287 for (evsel = events; evsel->attr.size; evsel++) {
1288 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001289
Robert Richter4e1b9c62012-08-16 21:10:22 +02001290 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001291 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001292 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1293 if (j)
1294 fputc(',', fp);
1295 fprintf(fp, " %"PRIu64, *id);
1296 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001297 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001298 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001299
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001300 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
Robert Richter4e1b9c62012-08-16 21:10:22 +02001301
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001302 fputc('\n', fp);
1303 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001304
1305 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001306}
1307
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001308static void print_total_mem(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001309 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001310{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001311 fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001312}
1313
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001314static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001315 FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001316{
Jiri Olsac60da222016-07-04 14:16:20 +02001317 int i;
1318 struct numa_node *n;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001319
Jiri Olsac60da222016-07-04 14:16:20 +02001320 for (i = 0; i < ph->env.nr_numa_nodes; i++) {
1321 n = &ph->env.numa_nodes[i];
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001322
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001323 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1324 " free = %"PRIu64" kB\n",
Jiri Olsac60da222016-07-04 14:16:20 +02001325 n->node, n->mem_total, n->mem_free);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001326
Jiri Olsac60da222016-07-04 14:16:20 +02001327 fprintf(fp, "# node%u cpu list : ", n->node);
1328 cpu_map__fprintf(n->map, fp);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001329 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001330}
1331
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001332static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001333{
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001334 fprintf(fp, "# cpuid : %s\n", ph->env.cpuid);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001335}
1336
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001337static void print_branch_stack(struct perf_header *ph __maybe_unused,
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001338 int fd __maybe_unused, FILE *fp)
Stephane Eranian330aa672012-03-08 23:47:46 +01001339{
1340 fprintf(fp, "# contains samples with branch stack\n");
1341}
1342
Adrian Hunter4025ea42015-04-09 18:53:41 +03001343static void print_auxtrace(struct perf_header *ph __maybe_unused,
1344 int fd __maybe_unused, FILE *fp)
1345{
1346 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1347}
1348
Jiri Olsaffa517a2015-10-25 15:51:43 +01001349static void print_stat(struct perf_header *ph __maybe_unused,
1350 int fd __maybe_unused, FILE *fp)
1351{
1352 fprintf(fp, "# contains stat data\n");
1353}
1354
Jiri Olsa720e98b2016-02-16 16:01:43 +01001355static void print_cache(struct perf_header *ph __maybe_unused,
1356 int fd __maybe_unused, FILE *fp __maybe_unused)
1357{
1358 int i;
1359
1360 fprintf(fp, "# CPU cache info:\n");
1361 for (i = 0; i < ph->env.caches_cnt; i++) {
1362 fprintf(fp, "# ");
1363 cpu_cache_level__fprintf(fp, &ph->env.caches[i]);
1364 }
1365}
1366
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001367static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
1368 FILE *fp)
Robert Richter50a96672012-08-16 21:10:24 +02001369{
1370 const char *delimiter = "# pmu mappings: ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001371 char *str, *tmp;
Robert Richter50a96672012-08-16 21:10:24 +02001372 u32 pmu_num;
1373 u32 type;
1374
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001375 pmu_num = ph->env.nr_pmu_mappings;
Robert Richter50a96672012-08-16 21:10:24 +02001376 if (!pmu_num) {
1377 fprintf(fp, "# pmu mappings: not available\n");
1378 return;
1379 }
1380
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001381 str = ph->env.pmu_mappings;
Namhyung Kimbe4a2de2012-09-05 14:02:49 +09001382
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001383 while (pmu_num) {
1384 type = strtoul(str, &tmp, 0);
1385 if (*tmp != ':')
1386 goto error;
1387
1388 str = tmp + 1;
1389 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1390
Robert Richter50a96672012-08-16 21:10:24 +02001391 delimiter = ", ";
Namhyung Kim7e94cfc2012-09-24 17:15:00 +09001392 str += strlen(str) + 1;
1393 pmu_num--;
Robert Richter50a96672012-08-16 21:10:24 +02001394 }
1395
1396 fprintf(fp, "\n");
1397
1398 if (!pmu_num)
1399 return;
1400error:
1401 fprintf(fp, "# pmu mappings: unable to read\n");
1402}
1403
Namhyung Kima8bb5592013-01-22 18:09:31 +09001404static void print_group_desc(struct perf_header *ph, int fd __maybe_unused,
1405 FILE *fp)
1406{
1407 struct perf_session *session;
1408 struct perf_evsel *evsel;
1409 u32 nr = 0;
1410
1411 session = container_of(ph, struct perf_session, header);
1412
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001413 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09001414 if (perf_evsel__is_group_leader(evsel) &&
1415 evsel->nr_members > 1) {
1416 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1417 perf_evsel__name(evsel));
1418
1419 nr = evsel->nr_members - 1;
1420 } else if (nr) {
1421 fprintf(fp, ",%s", perf_evsel__name(evsel));
1422
1423 if (--nr == 0)
1424 fprintf(fp, "}\n");
1425 }
1426 }
1427}
1428
Robert Richter08d95bd2012-02-10 15:41:55 +01001429static int __event_process_build_id(struct build_id_event *bev,
1430 char *filename,
1431 struct perf_session *session)
1432{
1433 int err = -1;
Robert Richter08d95bd2012-02-10 15:41:55 +01001434 struct machine *machine;
Wang Nan1f121b02015-06-03 08:52:21 +00001435 u16 cpumode;
Robert Richter08d95bd2012-02-10 15:41:55 +01001436 struct dso *dso;
1437 enum dso_kernel_type dso_type;
1438
1439 machine = perf_session__findnew_machine(session, bev->pid);
1440 if (!machine)
1441 goto out;
1442
Wang Nan1f121b02015-06-03 08:52:21 +00001443 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Robert Richter08d95bd2012-02-10 15:41:55 +01001444
Wang Nan1f121b02015-06-03 08:52:21 +00001445 switch (cpumode) {
Robert Richter08d95bd2012-02-10 15:41:55 +01001446 case PERF_RECORD_MISC_KERNEL:
1447 dso_type = DSO_TYPE_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001448 break;
1449 case PERF_RECORD_MISC_GUEST_KERNEL:
1450 dso_type = DSO_TYPE_GUEST_KERNEL;
Robert Richter08d95bd2012-02-10 15:41:55 +01001451 break;
1452 case PERF_RECORD_MISC_USER:
1453 case PERF_RECORD_MISC_GUEST_USER:
1454 dso_type = DSO_TYPE_USER;
Robert Richter08d95bd2012-02-10 15:41:55 +01001455 break;
1456 default:
1457 goto out;
1458 }
1459
Arnaldo Carvalho de Meloaa7cc2a2015-05-29 11:31:12 -03001460 dso = machine__findnew_dso(machine, filename);
Robert Richter08d95bd2012-02-10 15:41:55 +01001461 if (dso != NULL) {
Masami Hiramatsub5d8bbe2016-05-11 22:51:59 +09001462 char sbuild_id[SBUILD_ID_SIZE];
Robert Richter08d95bd2012-02-10 15:41:55 +01001463
1464 dso__set_build_id(dso, &bev->build_id);
1465
Wang Nan1f121b02015-06-03 08:52:21 +00001466 if (!is_kernel_module(filename, cpumode))
Robert Richter08d95bd2012-02-10 15:41:55 +01001467 dso->kernel = dso_type;
1468
1469 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1470 sbuild_id);
1471 pr_debug("build id event received for %s: %s\n",
1472 dso->long_name, sbuild_id);
Arnaldo Carvalho de Melod3a7c482015-06-02 11:53:26 -03001473 dso__put(dso);
Robert Richter08d95bd2012-02-10 15:41:55 +01001474 }
1475
1476 err = 0;
1477out:
1478 return err;
1479}
1480
1481static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1482 int input, u64 offset, u64 size)
1483{
1484 struct perf_session *session = container_of(header, struct perf_session, header);
1485 struct {
1486 struct perf_event_header header;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03001487 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
Robert Richter08d95bd2012-02-10 15:41:55 +01001488 char filename[0];
1489 } old_bev;
1490 struct build_id_event bev;
1491 char filename[PATH_MAX];
1492 u64 limit = offset + size;
1493
1494 while (offset < limit) {
1495 ssize_t len;
1496
Namhyung Kim5323f602012-12-17 15:38:54 +09001497 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001498 return -1;
1499
1500 if (header->needs_swap)
1501 perf_event_header__bswap(&old_bev.header);
1502
1503 len = old_bev.header.size - sizeof(old_bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001504 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001505 return -1;
1506
1507 bev.header = old_bev.header;
1508
1509 /*
1510 * As the pid is the missing value, we need to fill
1511 * it properly. The header.misc value give us nice hint.
1512 */
1513 bev.pid = HOST_KERNEL_ID;
1514 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1515 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1516 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1517
1518 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1519 __event_process_build_id(&bev, filename, session);
1520
1521 offset += bev.header.size;
1522 }
1523
1524 return 0;
1525}
1526
1527static int perf_header__read_build_ids(struct perf_header *header,
1528 int input, u64 offset, u64 size)
1529{
1530 struct perf_session *session = container_of(header, struct perf_session, header);
1531 struct build_id_event bev;
1532 char filename[PATH_MAX];
1533 u64 limit = offset + size, orig_offset = offset;
1534 int err = -1;
1535
1536 while (offset < limit) {
1537 ssize_t len;
1538
Namhyung Kim5323f602012-12-17 15:38:54 +09001539 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
Robert Richter08d95bd2012-02-10 15:41:55 +01001540 goto out;
1541
1542 if (header->needs_swap)
1543 perf_event_header__bswap(&bev.header);
1544
1545 len = bev.header.size - sizeof(bev);
Namhyung Kim5323f602012-12-17 15:38:54 +09001546 if (readn(input, filename, len) != len)
Robert Richter08d95bd2012-02-10 15:41:55 +01001547 goto out;
1548 /*
1549 * The a1645ce1 changeset:
1550 *
1551 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1552 *
1553 * Added a field to struct build_id_event that broke the file
1554 * format.
1555 *
1556 * Since the kernel build-id is the first entry, process the
1557 * table using the old format if the well known
1558 * '[kernel.kallsyms]' string for the kernel build-id has the
1559 * first 4 characters chopped off (where the pid_t sits).
1560 */
1561 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1562 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1563 return -1;
1564 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1565 }
1566
1567 __event_process_build_id(&bev, filename, session);
1568
1569 offset += bev.header.size;
1570 }
1571 err = 0;
1572out:
1573 return err;
1574}
1575
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001576static int process_tracing_data(struct perf_file_section *section __maybe_unused,
1577 struct perf_header *ph __maybe_unused,
1578 int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001579{
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09001580 ssize_t ret = trace_report(fd, data, false);
1581 return ret < 0 ? -1 : 0;
Robert Richterf1c67db2012-02-10 15:41:56 +01001582}
1583
1584static int process_build_id(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001585 struct perf_header *ph, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001586 void *data __maybe_unused)
Robert Richterf1c67db2012-02-10 15:41:56 +01001587{
1588 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1589 pr_debug("Failed to read buildids, continuing...\n");
1590 return 0;
1591}
1592
Namhyung Kima1ae5652012-09-24 17:14:59 +09001593static int process_hostname(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001594 struct perf_header *ph, int fd,
1595 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001596{
1597 ph->env.hostname = do_read_string(fd, ph);
1598 return ph->env.hostname ? 0 : -ENOMEM;
1599}
1600
1601static int process_osrelease(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001602 struct perf_header *ph, int fd,
1603 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001604{
1605 ph->env.os_release = do_read_string(fd, ph);
1606 return ph->env.os_release ? 0 : -ENOMEM;
1607}
1608
1609static int process_version(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001610 struct perf_header *ph, int fd,
1611 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001612{
1613 ph->env.version = do_read_string(fd, ph);
1614 return ph->env.version ? 0 : -ENOMEM;
1615}
1616
1617static int process_arch(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001618 struct perf_header *ph, int fd,
1619 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001620{
1621 ph->env.arch = do_read_string(fd, ph);
1622 return ph->env.arch ? 0 : -ENOMEM;
1623}
1624
1625static int process_nrcpus(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001626 struct perf_header *ph, int fd,
1627 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001628{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001629 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001630 u32 nr;
1631
Namhyung Kim5323f602012-12-17 15:38:54 +09001632 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001633 if (ret != sizeof(nr))
1634 return -1;
1635
1636 if (ph->needs_swap)
1637 nr = bswap_32(nr);
1638
Arnaldo Carvalho de Melocaa47042015-09-11 12:36:12 -03001639 ph->env.nr_cpus_avail = nr;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001640
Namhyung Kim5323f602012-12-17 15:38:54 +09001641 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001642 if (ret != sizeof(nr))
1643 return -1;
1644
1645 if (ph->needs_swap)
1646 nr = bswap_32(nr);
1647
Arnaldo Carvalho de Melocaa47042015-09-11 12:36:12 -03001648 ph->env.nr_cpus_online = nr;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001649 return 0;
1650}
1651
1652static int process_cpudesc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001653 struct perf_header *ph, int fd,
1654 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001655{
1656 ph->env.cpu_desc = do_read_string(fd, ph);
1657 return ph->env.cpu_desc ? 0 : -ENOMEM;
1658}
1659
1660static int process_cpuid(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001661 struct perf_header *ph, int fd,
1662 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001663{
1664 ph->env.cpuid = do_read_string(fd, ph);
1665 return ph->env.cpuid ? 0 : -ENOMEM;
1666}
1667
1668static int process_total_mem(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001669 struct perf_header *ph, int fd,
1670 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001671{
1672 uint64_t mem;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001673 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001674
Namhyung Kim5323f602012-12-17 15:38:54 +09001675 ret = readn(fd, &mem, sizeof(mem));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001676 if (ret != sizeof(mem))
1677 return -1;
1678
1679 if (ph->needs_swap)
1680 mem = bswap_64(mem);
1681
1682 ph->env.total_mem = mem;
1683 return 0;
1684}
1685
Robert Richter7c2f7af2012-08-16 21:10:23 +02001686static struct perf_evsel *
1687perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1688{
1689 struct perf_evsel *evsel;
1690
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001691 evlist__for_each_entry(evlist, evsel) {
Robert Richter7c2f7af2012-08-16 21:10:23 +02001692 if (evsel->idx == idx)
1693 return evsel;
1694 }
1695
1696 return NULL;
1697}
1698
1699static void
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001700perf_evlist__set_event_name(struct perf_evlist *evlist,
1701 struct perf_evsel *event)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001702{
1703 struct perf_evsel *evsel;
1704
1705 if (!event->name)
1706 return;
1707
1708 evsel = perf_evlist__find_by_index(evlist, event->idx);
1709 if (!evsel)
1710 return;
1711
1712 if (evsel->name)
1713 return;
1714
1715 evsel->name = strdup(event->name);
1716}
1717
1718static int
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001719process_event_desc(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001720 struct perf_header *header, int fd,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001721 void *data __maybe_unused)
Robert Richter7c2f7af2012-08-16 21:10:23 +02001722{
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001723 struct perf_session *session;
Robert Richter7c2f7af2012-08-16 21:10:23 +02001724 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1725
1726 if (!events)
1727 return 0;
1728
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001729 session = container_of(header, struct perf_session, header);
Robert Richter7c2f7af2012-08-16 21:10:23 +02001730 for (evsel = events; evsel->attr.size; evsel++)
1731 perf_evlist__set_event_name(session->evlist, evsel);
1732
1733 free_event_desc(events);
1734
1735 return 0;
1736}
1737
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001738static int process_cmdline(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001739 struct perf_header *ph, int fd,
1740 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001741{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001742 ssize_t ret;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001743 char *str, *cmdline = NULL, **argv = NULL;
1744 u32 nr, i, len = 0;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001745
Namhyung Kim5323f602012-12-17 15:38:54 +09001746 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001747 if (ret != sizeof(nr))
1748 return -1;
1749
1750 if (ph->needs_swap)
1751 nr = bswap_32(nr);
1752
1753 ph->env.nr_cmdline = nr;
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001754
1755 cmdline = zalloc(section->size + nr + 1);
1756 if (!cmdline)
1757 return -1;
1758
1759 argv = zalloc(sizeof(char *) * (nr + 1));
1760 if (!argv)
1761 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001762
1763 for (i = 0; i < nr; i++) {
1764 str = do_read_string(fd, ph);
1765 if (!str)
1766 goto error;
1767
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001768 argv[i] = cmdline + len;
1769 memcpy(argv[i], str, strlen(str) + 1);
1770 len += strlen(str) + 1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001771 free(str);
1772 }
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001773 ph->env.cmdline = cmdline;
1774 ph->env.cmdline_argv = (const char **) argv;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001775 return 0;
1776
1777error:
Jiri Olsa768dd3f2015-07-21 14:31:31 +02001778 free(argv);
1779 free(cmdline);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001780 return -1;
1781}
1782
Kan Liang2bb00d22015-09-01 09:58:12 -04001783static int process_cpu_topology(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001784 struct perf_header *ph, int fd,
1785 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001786{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001787 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001788 u32 nr, i;
1789 char *str;
1790 struct strbuf sb;
Jan Stancekda8a58b2017-02-17 12:10:26 +01001791 int cpu_nr = ph->env.nr_cpus_avail;
Kan Liang2bb00d22015-09-01 09:58:12 -04001792 u64 size = 0;
1793
1794 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1795 if (!ph->env.cpu)
1796 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001797
Namhyung Kim5323f602012-12-17 15:38:54 +09001798 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001799 if (ret != sizeof(nr))
Kan Liang2bb00d22015-09-01 09:58:12 -04001800 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001801
1802 if (ph->needs_swap)
1803 nr = bswap_32(nr);
1804
1805 ph->env.nr_sibling_cores = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001806 size += sizeof(u32);
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001807 if (strbuf_init(&sb, 128) < 0)
1808 goto free_cpu;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001809
1810 for (i = 0; i < nr; i++) {
1811 str = do_read_string(fd, ph);
1812 if (!str)
1813 goto error;
1814
1815 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001816 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1817 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001818 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001819 free(str);
1820 }
1821 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1822
Namhyung Kim5323f602012-12-17 15:38:54 +09001823 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001824 if (ret != sizeof(nr))
1825 return -1;
1826
1827 if (ph->needs_swap)
1828 nr = bswap_32(nr);
1829
1830 ph->env.nr_sibling_threads = nr;
Kan Liang2bb00d22015-09-01 09:58:12 -04001831 size += sizeof(u32);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001832
1833 for (i = 0; i < nr; i++) {
1834 str = do_read_string(fd, ph);
1835 if (!str)
1836 goto error;
1837
1838 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001839 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1840 goto error;
Kan Liang2bb00d22015-09-01 09:58:12 -04001841 size += string_size(str);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001842 free(str);
1843 }
1844 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
Kan Liang2bb00d22015-09-01 09:58:12 -04001845
1846 /*
1847 * The header may be from old perf,
1848 * which doesn't include core id and socket id information.
1849 */
1850 if (section->size <= size) {
1851 zfree(&ph->env.cpu);
1852 return 0;
1853 }
1854
1855 for (i = 0; i < (u32)cpu_nr; i++) {
1856 ret = readn(fd, &nr, sizeof(nr));
1857 if (ret != sizeof(nr))
1858 goto free_cpu;
1859
1860 if (ph->needs_swap)
1861 nr = bswap_32(nr);
1862
Kan Liang2bb00d22015-09-01 09:58:12 -04001863 ph->env.cpu[i].core_id = nr;
1864
1865 ret = readn(fd, &nr, sizeof(nr));
1866 if (ret != sizeof(nr))
1867 goto free_cpu;
1868
1869 if (ph->needs_swap)
1870 nr = bswap_32(nr);
1871
Jan Stancekda8a58b2017-02-17 12:10:26 +01001872 if (nr != (u32)-1 && nr > (u32)cpu_nr) {
Kan Liang2bb00d22015-09-01 09:58:12 -04001873 pr_debug("socket_id number is too big."
1874 "You may need to upgrade the perf tool.\n");
1875 goto free_cpu;
1876 }
1877
1878 ph->env.cpu[i].socket_id = nr;
1879 }
1880
Namhyung Kima1ae5652012-09-24 17:14:59 +09001881 return 0;
1882
1883error:
1884 strbuf_release(&sb);
Kan Liang2bb00d22015-09-01 09:58:12 -04001885free_cpu:
1886 zfree(&ph->env.cpu);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001887 return -1;
1888}
1889
1890static int process_numa_topology(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001891 struct perf_header *ph, int fd,
1892 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001893{
Jiri Olsac60da222016-07-04 14:16:20 +02001894 struct numa_node *nodes, *n;
Jiri Olsa727ebd52013-11-28 11:30:14 +01001895 ssize_t ret;
Jiri Olsac60da222016-07-04 14:16:20 +02001896 u32 nr, i;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001897 char *str;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001898
1899 /* nr nodes */
Namhyung Kim5323f602012-12-17 15:38:54 +09001900 ret = readn(fd, &nr, sizeof(nr));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001901 if (ret != sizeof(nr))
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001902 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001903
1904 if (ph->needs_swap)
1905 nr = bswap_32(nr);
1906
Jiri Olsac60da222016-07-04 14:16:20 +02001907 nodes = zalloc(sizeof(*nodes) * nr);
1908 if (!nodes)
1909 return -ENOMEM;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001910
1911 for (i = 0; i < nr; i++) {
Jiri Olsac60da222016-07-04 14:16:20 +02001912 n = &nodes[i];
1913
Namhyung Kima1ae5652012-09-24 17:14:59 +09001914 /* node number */
Jiri Olsac60da222016-07-04 14:16:20 +02001915 ret = readn(fd, &n->node, sizeof(u32));
1916 if (ret != sizeof(n->node))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001917 goto error;
1918
Jiri Olsac60da222016-07-04 14:16:20 +02001919 ret = readn(fd, &n->mem_total, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001920 if (ret != sizeof(u64))
1921 goto error;
1922
Jiri Olsac60da222016-07-04 14:16:20 +02001923 ret = readn(fd, &n->mem_free, sizeof(u64));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001924 if (ret != sizeof(u64))
1925 goto error;
1926
1927 if (ph->needs_swap) {
Jiri Olsac60da222016-07-04 14:16:20 +02001928 n->node = bswap_32(n->node);
1929 n->mem_total = bswap_64(n->mem_total);
1930 n->mem_free = bswap_64(n->mem_free);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001931 }
1932
Namhyung Kima1ae5652012-09-24 17:14:59 +09001933 str = do_read_string(fd, ph);
1934 if (!str)
1935 goto error;
1936
Jiri Olsac60da222016-07-04 14:16:20 +02001937 n->map = cpu_map__new(str);
1938 if (!n->map)
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001939 goto error;
Jiri Olsac60da222016-07-04 14:16:20 +02001940
Namhyung Kima1ae5652012-09-24 17:14:59 +09001941 free(str);
1942 }
Jiri Olsaf957a532016-10-10 09:56:32 +02001943 ph->env.nr_numa_nodes = nr;
Jiri Olsac60da222016-07-04 14:16:20 +02001944 ph->env.numa_nodes = nodes;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001945 return 0;
1946
1947error:
Jiri Olsac60da222016-07-04 14:16:20 +02001948 free(nodes);
Namhyung Kima1ae5652012-09-24 17:14:59 +09001949 return -1;
1950}
1951
1952static int process_pmu_mappings(struct perf_file_section *section __maybe_unused,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09001953 struct perf_header *ph, int fd,
1954 void *data __maybe_unused)
Namhyung Kima1ae5652012-09-24 17:14:59 +09001955{
Jiri Olsa727ebd52013-11-28 11:30:14 +01001956 ssize_t ret;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001957 char *name;
1958 u32 pmu_num;
1959 u32 type;
1960 struct strbuf sb;
1961
Namhyung Kim5323f602012-12-17 15:38:54 +09001962 ret = readn(fd, &pmu_num, sizeof(pmu_num));
Namhyung Kima1ae5652012-09-24 17:14:59 +09001963 if (ret != sizeof(pmu_num))
1964 return -1;
1965
1966 if (ph->needs_swap)
1967 pmu_num = bswap_32(pmu_num);
1968
1969 if (!pmu_num) {
1970 pr_debug("pmu mappings not available\n");
1971 return 0;
1972 }
1973
1974 ph->env.nr_pmu_mappings = pmu_num;
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001975 if (strbuf_init(&sb, 128) < 0)
1976 return -1;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001977
1978 while (pmu_num) {
Namhyung Kim5323f602012-12-17 15:38:54 +09001979 if (readn(fd, &type, sizeof(type)) != sizeof(type))
Namhyung Kima1ae5652012-09-24 17:14:59 +09001980 goto error;
1981 if (ph->needs_swap)
1982 type = bswap_32(type);
1983
1984 name = do_read_string(fd, ph);
1985 if (!name)
1986 goto error;
1987
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001988 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
1989 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001990 /* include a NULL character at the end */
Masami Hiramatsu642aada2016-05-10 14:47:35 +09001991 if (strbuf_add(&sb, "", 1) < 0)
1992 goto error;
Namhyung Kima1ae5652012-09-24 17:14:59 +09001993
Kan Liange0838e02015-09-10 11:03:05 -03001994 if (!strcmp(name, "msr"))
1995 ph->env.msr_pmu_type = type;
1996
Namhyung Kima1ae5652012-09-24 17:14:59 +09001997 free(name);
1998 pmu_num--;
1999 }
2000 ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2001 return 0;
2002
2003error:
2004 strbuf_release(&sb);
2005 return -1;
2006}
2007
Namhyung Kima8bb5592013-01-22 18:09:31 +09002008static int process_group_desc(struct perf_file_section *section __maybe_unused,
2009 struct perf_header *ph, int fd,
2010 void *data __maybe_unused)
2011{
2012 size_t ret = -1;
2013 u32 i, nr, nr_groups;
2014 struct perf_session *session;
2015 struct perf_evsel *evsel, *leader = NULL;
2016 struct group_desc {
2017 char *name;
2018 u32 leader_idx;
2019 u32 nr_members;
2020 } *desc;
2021
2022 if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
2023 return -1;
2024
2025 if (ph->needs_swap)
2026 nr_groups = bswap_32(nr_groups);
2027
2028 ph->env.nr_groups = nr_groups;
2029 if (!nr_groups) {
2030 pr_debug("group desc not available\n");
2031 return 0;
2032 }
2033
2034 desc = calloc(nr_groups, sizeof(*desc));
2035 if (!desc)
2036 return -1;
2037
2038 for (i = 0; i < nr_groups; i++) {
2039 desc[i].name = do_read_string(fd, ph);
2040 if (!desc[i].name)
2041 goto out_free;
2042
2043 if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
2044 goto out_free;
2045
2046 if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
2047 goto out_free;
2048
2049 if (ph->needs_swap) {
2050 desc[i].leader_idx = bswap_32(desc[i].leader_idx);
2051 desc[i].nr_members = bswap_32(desc[i].nr_members);
2052 }
2053 }
2054
2055 /*
2056 * Rebuild group relationship based on the group_desc
2057 */
2058 session = container_of(ph, struct perf_session, header);
2059 session->evlist->nr_groups = nr_groups;
2060
2061 i = nr = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002062 evlist__for_each_entry(session->evlist, evsel) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002063 if (evsel->idx == (int) desc[i].leader_idx) {
2064 evsel->leader = evsel;
2065 /* {anon_group} is a dummy name */
Namhyung Kim210e8122013-11-18 11:20:43 +09002066 if (strcmp(desc[i].name, "{anon_group}")) {
Namhyung Kima8bb5592013-01-22 18:09:31 +09002067 evsel->group_name = desc[i].name;
Namhyung Kim210e8122013-11-18 11:20:43 +09002068 desc[i].name = NULL;
2069 }
Namhyung Kima8bb5592013-01-22 18:09:31 +09002070 evsel->nr_members = desc[i].nr_members;
2071
2072 if (i >= nr_groups || nr > 0) {
2073 pr_debug("invalid group desc\n");
2074 goto out_free;
2075 }
2076
2077 leader = evsel;
2078 nr = evsel->nr_members - 1;
2079 i++;
2080 } else if (nr) {
2081 /* This is a group member */
2082 evsel->leader = leader;
2083
2084 nr--;
2085 }
2086 }
2087
2088 if (i != nr_groups || nr != 0) {
2089 pr_debug("invalid group desc\n");
2090 goto out_free;
2091 }
2092
2093 ret = 0;
2094out_free:
Namhyung Kim50a27402013-11-18 11:20:44 +09002095 for (i = 0; i < nr_groups; i++)
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03002096 zfree(&desc[i].name);
Namhyung Kima8bb5592013-01-22 18:09:31 +09002097 free(desc);
2098
2099 return ret;
2100}
2101
Adrian Hunter99fa2982015-04-30 17:37:25 +03002102static int process_auxtrace(struct perf_file_section *section,
2103 struct perf_header *ph, int fd,
2104 void *data __maybe_unused)
2105{
2106 struct perf_session *session;
2107 int err;
2108
2109 session = container_of(ph, struct perf_session, header);
2110
2111 err = auxtrace_index__process(fd, section->size, session,
2112 ph->needs_swap);
2113 if (err < 0)
2114 pr_err("Failed to process auxtrace index\n");
2115 return err;
2116}
2117
Jiri Olsa720e98b2016-02-16 16:01:43 +01002118static int process_cache(struct perf_file_section *section __maybe_unused,
2119 struct perf_header *ph __maybe_unused, int fd __maybe_unused,
2120 void *data __maybe_unused)
2121{
2122 struct cpu_cache_level *caches;
2123 u32 cnt, i, version;
2124
2125 if (readn(fd, &version, sizeof(version)) != sizeof(version))
2126 return -1;
2127
2128 if (ph->needs_swap)
2129 version = bswap_32(version);
2130
2131 if (version != 1)
2132 return -1;
2133
2134 if (readn(fd, &cnt, sizeof(cnt)) != sizeof(cnt))
2135 return -1;
2136
2137 if (ph->needs_swap)
2138 cnt = bswap_32(cnt);
2139
2140 caches = zalloc(sizeof(*caches) * cnt);
2141 if (!caches)
2142 return -1;
2143
2144 for (i = 0; i < cnt; i++) {
2145 struct cpu_cache_level c;
2146
2147 #define _R(v) \
2148 if (readn(fd, &c.v, sizeof(u32)) != sizeof(u32))\
2149 goto out_free_caches; \
2150 if (ph->needs_swap) \
2151 c.v = bswap_32(c.v); \
2152
2153 _R(level)
2154 _R(line_size)
2155 _R(sets)
2156 _R(ways)
2157 #undef _R
2158
2159 #define _R(v) \
2160 c.v = do_read_string(fd, ph); \
2161 if (!c.v) \
2162 goto out_free_caches;
2163
2164 _R(type)
2165 _R(size)
2166 _R(map)
2167 #undef _R
2168
2169 caches[i] = c;
2170 }
2171
2172 ph->env.caches = caches;
2173 ph->env.caches_cnt = cnt;
2174 return 0;
2175out_free_caches:
2176 free(caches);
2177 return -1;
2178}
2179
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002180struct feature_ops {
2181 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
2182 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01002183 int (*process)(struct perf_file_section *section,
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002184 struct perf_header *h, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002185 const char *name;
2186 bool full_only;
2187};
2188
Robert Richter8cdfa782011-12-07 10:02:56 +01002189#define FEAT_OPA(n, func) \
2190 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01002191#define FEAT_OPP(n, func) \
2192 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
2193 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01002194#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01002195 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
Namhyung Kima1ae5652012-09-24 17:14:59 +09002196 .process = process_##func, .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01002197
2198/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002199#define print_tracing_data NULL
2200#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002201
2202static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02002203 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01002204 FEAT_OPP(HEADER_BUILD_ID, build_id),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002205 FEAT_OPP(HEADER_HOSTNAME, hostname),
2206 FEAT_OPP(HEADER_OSRELEASE, osrelease),
2207 FEAT_OPP(HEADER_VERSION, version),
2208 FEAT_OPP(HEADER_ARCH, arch),
2209 FEAT_OPP(HEADER_NRCPUS, nrcpus),
2210 FEAT_OPP(HEADER_CPUDESC, cpudesc),
Namhyung Kim37e9d752012-09-24 17:15:03 +09002211 FEAT_OPP(HEADER_CPUID, cpuid),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002212 FEAT_OPP(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02002213 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002214 FEAT_OPP(HEADER_CMDLINE, cmdline),
Robert Richter8cdfa782011-12-07 10:02:56 +01002215 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
2216 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01002217 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Namhyung Kima1ae5652012-09-24 17:14:59 +09002218 FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings),
Namhyung Kima8bb5592013-01-22 18:09:31 +09002219 FEAT_OPP(HEADER_GROUP_DESC, group_desc),
Adrian Hunter99fa2982015-04-30 17:37:25 +03002220 FEAT_OPP(HEADER_AUXTRACE, auxtrace),
Jiri Olsaffa517a2015-10-25 15:51:43 +01002221 FEAT_OPA(HEADER_STAT, stat),
Jiri Olsa720e98b2016-02-16 16:01:43 +01002222 FEAT_OPF(HEADER_CACHE, cache),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002223};
2224
2225struct header_print_data {
2226 FILE *fp;
2227 bool full; /* extended list of headers */
2228};
2229
2230static int perf_file_section__fprintf_info(struct perf_file_section *section,
2231 struct perf_header *ph,
2232 int feat, int fd, void *data)
2233{
2234 struct header_print_data *hd = data;
2235
2236 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2237 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2238 "%d, continuing...\n", section->offset, feat);
2239 return 0;
2240 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002241 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002242 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01002243 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002244 }
2245 if (!feat_ops[feat].print)
2246 return 0;
2247
2248 if (!feat_ops[feat].full_only || hd->full)
2249 feat_ops[feat].print(ph, fd, hd->fp);
2250 else
2251 fprintf(hd->fp, "# %s info available, use -I to display\n",
2252 feat_ops[feat].name);
2253
2254 return 0;
2255}
2256
2257int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2258{
2259 struct header_print_data hd;
2260 struct perf_header *header = &session->header;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002261 int fd = perf_data_file__fd(session->file);
Jiri Olsaf45f5612016-10-10 09:03:07 +02002262 struct stat st;
Jiri Olsaaabae162016-10-10 09:35:50 +02002263 int ret, bit;
Jiri Olsaf45f5612016-10-10 09:03:07 +02002264
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002265 hd.fp = fp;
2266 hd.full = full;
2267
Jiri Olsaf45f5612016-10-10 09:03:07 +02002268 ret = fstat(fd, &st);
2269 if (ret == -1)
2270 return -1;
2271
2272 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
2273
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002274 perf_header__process_sections(header, fd, &hd,
2275 perf_file_section__fprintf_info);
Jiri Olsaaabae162016-10-10 09:35:50 +02002276
David Carrillo-Cisnerosc9d1c932017-04-10 13:14:32 -07002277 if (session->file->is_pipe)
2278 return 0;
2279
Jiri Olsaaabae162016-10-10 09:35:50 +02002280 fprintf(fp, "# missing features: ");
2281 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2282 if (bit)
2283 fprintf(fp, "%s ", feat_ops[bit].name);
2284 }
2285
2286 fprintf(fp, "\n");
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002287 return 0;
2288}
2289
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002290static int do_write_feat(int fd, struct perf_header *h, int type,
2291 struct perf_file_section **p,
2292 struct perf_evlist *evlist)
2293{
2294 int err;
2295 int ret = 0;
2296
2297 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002298 if (!feat_ops[type].write)
2299 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002300
2301 (*p)->offset = lseek(fd, 0, SEEK_CUR);
2302
2303 err = feat_ops[type].write(fd, h, evlist);
2304 if (err < 0) {
Jiri Olsa0c2aff42016-10-10 09:38:02 +02002305 pr_debug("failed to write feature %s\n", feat_ops[type].name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002306
2307 /* undo anything written */
2308 lseek(fd, (*p)->offset, SEEK_SET);
2309
2310 return -1;
2311 }
2312 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
2313 (*p)++;
2314 }
2315 return ret;
2316}
2317
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002318static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002319 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002320{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002321 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002322 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002323 int sec_size;
2324 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002325 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002326 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002327
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002328 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002329 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002330 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002331
Paul Gortmaker91b98802013-01-30 20:05:49 -05002332 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002333 if (feat_sec == NULL)
2334 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002335
2336 sec_size = sizeof(*feat_sec) * nr_sections;
2337
Jiri Olsa8d541e92013-07-17 19:49:44 +02002338 sec_start = header->feat_offset;
Xiao Guangrongf887f302010-02-04 16:46:42 +08002339 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002340
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002341 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2342 if (do_write_feat(fd, header, feat, &p, evlist))
2343 perf_header__clear_feat(header, feat);
2344 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002345
Xiao Guangrongf887f302010-02-04 16:46:42 +08002346 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002347 /*
2348 * may write more than needed due to dropped feature, but
2349 * this is okay, reader will skip the mising entries
2350 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002351 err = do_write(fd, feat_sec, sec_size);
2352 if (err < 0)
2353 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002354 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002355 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002356}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002357
Tom Zanussi8dc58102010-04-01 23:59:15 -05002358int perf_header__write_pipe(int fd)
2359{
2360 struct perf_pipe_file_header f_header;
2361 int err;
2362
2363 f_header = (struct perf_pipe_file_header){
2364 .magic = PERF_MAGIC,
2365 .size = sizeof(f_header),
2366 };
2367
2368 err = do_write(fd, &f_header, sizeof(f_header));
2369 if (err < 0) {
2370 pr_debug("failed to write perf pipe header\n");
2371 return err;
2372 }
2373
2374 return 0;
2375}
2376
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002377int perf_session__write_header(struct perf_session *session,
2378 struct perf_evlist *evlist,
2379 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002380{
2381 struct perf_file_header f_header;
2382 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002383 struct perf_header *header = &session->header;
Jiri Olsa563aecb2013-06-05 13:35:06 +02002384 struct perf_evsel *evsel;
Jiri Olsa944d62b2013-07-17 19:49:43 +02002385 u64 attr_offset;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002386 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002387
2388 lseek(fd, sizeof(f_header), SEEK_SET);
2389
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002390 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02002391 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2392 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002393 if (err < 0) {
2394 pr_debug("failed to write perf header\n");
2395 return err;
2396 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002397 }
2398
Jiri Olsa944d62b2013-07-17 19:49:43 +02002399 attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002400
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002401 evlist__for_each_entry(evlist, evsel) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002402 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02002403 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002404 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02002405 .offset = evsel->id_offset,
2406 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002407 }
2408 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002409 err = do_write(fd, &f_attr, sizeof(f_attr));
2410 if (err < 0) {
2411 pr_debug("failed to write perf header attribute\n");
2412 return err;
2413 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002414 }
2415
Adrian Hunterd645c442013-12-11 14:36:28 +02002416 if (!header->data_offset)
2417 header->data_offset = lseek(fd, 0, SEEK_CUR);
Jiri Olsa8d541e92013-07-17 19:49:44 +02002418 header->feat_offset = header->data_offset + header->data_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002419
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002420 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002421 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002422 if (err < 0)
2423 return err;
2424 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002425
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002426 f_header = (struct perf_file_header){
2427 .magic = PERF_MAGIC,
2428 .size = sizeof(f_header),
2429 .attr_size = sizeof(f_attr),
2430 .attrs = {
Jiri Olsa944d62b2013-07-17 19:49:43 +02002431 .offset = attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002432 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002433 },
2434 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002435 .offset = header->data_offset,
2436 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002437 },
Jiri Olsa44b3c572013-07-11 17:28:31 +02002438 /* event_types is ignored, store zeros */
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002439 };
2440
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002441 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002442
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002443 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002444 err = do_write(fd, &f_header, sizeof(f_header));
2445 if (err < 0) {
2446 pr_debug("failed to write perf header\n");
2447 return err;
2448 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002449 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002450
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02002451 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002452}
2453
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002454static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002455 int fd, void *buf, size_t size)
2456{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02002457 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002458 return -1;
2459
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002460 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002461 mem_bswap_64(buf, size);
2462
2463 return 0;
2464}
2465
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002466int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002467 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002468 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002469 struct perf_header *ph,
2470 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002471{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002472 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002473 int nr_sections;
2474 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002475 int feat;
2476 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002477
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002478 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002479 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002480 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002481
Paul Gortmaker91b98802013-01-30 20:05:49 -05002482 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002483 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002484 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002485
2486 sec_size = sizeof(*feat_sec) * nr_sections;
2487
Jiri Olsa8d541e92013-07-17 19:49:44 +02002488 lseek(fd, header->feat_offset, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002489
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002490 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2491 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002492 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002493
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002494 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2495 err = process(sec++, header, feat, fd, data);
2496 if (err < 0)
2497 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002498 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002499 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002500out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002501 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002502 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002503}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002504
Stephane Eranian114382a2012-02-09 23:21:08 +01002505static const int attr_file_abi_sizes[] = {
2506 [0] = PERF_ATTR_SIZE_VER0,
2507 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002508 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002509 [3] = PERF_ATTR_SIZE_VER3,
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002510 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002511 0,
2512};
2513
2514/*
2515 * In the legacy file format, the magic number is not used to encode endianness.
2516 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2517 * on ABI revisions, we need to try all combinations for all endianness to
2518 * detect the endianness.
2519 */
2520static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2521{
2522 uint64_t ref_size, attr_size;
2523 int i;
2524
2525 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2526 ref_size = attr_file_abi_sizes[i]
2527 + sizeof(struct perf_file_section);
2528 if (hdr_sz != ref_size) {
2529 attr_size = bswap_64(hdr_sz);
2530 if (attr_size != ref_size)
2531 continue;
2532
2533 ph->needs_swap = true;
2534 }
2535 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2536 i,
2537 ph->needs_swap);
2538 return 0;
2539 }
2540 /* could not determine endianness */
2541 return -1;
2542}
2543
2544#define PERF_PIPE_HDR_VER0 16
2545
2546static const size_t attr_pipe_abi_sizes[] = {
2547 [0] = PERF_PIPE_HDR_VER0,
2548 0,
2549};
2550
2551/*
2552 * In the legacy pipe format, there is an implicit assumption that endiannesss
2553 * between host recording the samples, and host parsing the samples is the
2554 * same. This is not always the case given that the pipe output may always be
2555 * redirected into a file and analyzed on a different machine with possibly a
2556 * different endianness and perf_event ABI revsions in the perf tool itself.
2557 */
2558static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2559{
2560 u64 attr_size;
2561 int i;
2562
2563 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2564 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2565 attr_size = bswap_64(hdr_sz);
2566 if (attr_size != hdr_sz)
2567 continue;
2568
2569 ph->needs_swap = true;
2570 }
2571 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2572 return 0;
2573 }
2574 return -1;
2575}
2576
Feng Tange84ba4e2012-10-30 11:56:07 +08002577bool is_perf_magic(u64 magic)
2578{
2579 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2580 || magic == __perf_magic2
2581 || magic == __perf_magic2_sw)
2582 return true;
2583
2584 return false;
2585}
2586
Stephane Eranian114382a2012-02-09 23:21:08 +01002587static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2588 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002589{
2590 int ret;
2591
2592 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002593 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002594 if (ret == 0) {
Jiri Olsa2a08c3e2013-07-17 19:49:47 +02002595 ph->version = PERF_HEADER_VERSION_1;
Stephane Eranian73323f52012-02-02 13:54:44 +01002596 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002597 if (is_pipe)
2598 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002599
Stephane Eranian114382a2012-02-09 23:21:08 +01002600 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002601 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002602 /*
2603 * the new magic number serves two purposes:
2604 * - unique number to identify actual perf.data files
2605 * - encode endianness of file
2606 */
Namhyung Kimf7913972015-01-29 17:06:45 +09002607 ph->version = PERF_HEADER_VERSION_2;
Stephane Eranian73323f52012-02-02 13:54:44 +01002608
Stephane Eranian114382a2012-02-09 23:21:08 +01002609 /* check magic number with one endianness */
2610 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002611 return 0;
2612
Stephane Eranian114382a2012-02-09 23:21:08 +01002613 /* check magic number with opposite endianness */
2614 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002615 return -1;
2616
2617 ph->needs_swap = true;
2618
2619 return 0;
2620}
2621
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002622int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002623 struct perf_header *ph, int fd)
2624{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002625 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002626
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002627 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002628
Stephane Eranian73323f52012-02-02 13:54:44 +01002629 ret = readn(fd, header, sizeof(*header));
2630 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002631 return -1;
2632
Stephane Eranian114382a2012-02-09 23:21:08 +01002633 if (check_magic_endian(header->magic,
2634 header->attr_size, false, ph) < 0) {
2635 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002636 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002637 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002638
Stephane Eranian73323f52012-02-02 13:54:44 +01002639 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002640 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002641 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002642 }
2643
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002644 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002645 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002646 if (header->size == offsetof(typeof(*header), adds_features))
2647 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002648 else
2649 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002650 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002651 /*
2652 * feature bitmap is declared as an array of unsigned longs --
2653 * not good since its size can differ between the host that
2654 * generated the data file and the host analyzing the file.
2655 *
2656 * We need to handle endianness, but we don't know the size of
2657 * the unsigned long where the file was generated. Take a best
2658 * guess at determining it: try 64-bit swap first (ie., file
2659 * created on a 64-bit host), and check if the hostname feature
2660 * bit is set (this feature bit is forced on as of fbe96f2).
2661 * If the bit is not, undo the 64-bit swap and try a 32-bit
2662 * swap. If the hostname bit is still not set (e.g., older data
2663 * file), punt and fallback to the original behavior --
2664 * clearing all feature bits and setting buildid.
2665 */
David Ahern80c01202012-06-08 11:47:51 -03002666 mem_bswap_64(&header->adds_features,
2667 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002668
2669 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002670 /* unswap as u64 */
2671 mem_bswap_64(&header->adds_features,
2672 BITS_TO_U64(HEADER_FEAT_BITS));
2673
2674 /* unswap as u32 */
2675 mem_bswap_32(&header->adds_features,
2676 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002677 }
2678
2679 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2680 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2681 set_bit(HEADER_BUILD_ID, header->adds_features);
2682 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002683 }
2684
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002685 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002686 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002687
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002688 ph->data_offset = header->data.offset;
2689 ph->data_size = header->data.size;
Jiri Olsa8d541e92013-07-17 19:49:44 +02002690 ph->feat_offset = header->data.offset + header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002691 return 0;
2692}
2693
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002694static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002695 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002696 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002697{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002698 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002699 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002700 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002701 return 0;
2702 }
2703
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002704 if (feat >= HEADER_LAST_FEATURE) {
2705 pr_debug("unknown feature %d, continuing...\n", feat);
2706 return 0;
2707 }
2708
Robert Richterf1c67db2012-02-10 15:41:56 +01002709 if (!feat_ops[feat].process)
2710 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002711
Namhyung Kim3d7eb862012-09-24 17:15:01 +09002712 return feat_ops[feat].process(section, ph, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002713}
2714
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002715static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002716 struct perf_header *ph, int fd,
2717 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002718{
Jiri Olsa727ebd52013-11-28 11:30:14 +01002719 ssize_t ret;
Stephane Eranian73323f52012-02-02 13:54:44 +01002720
2721 ret = readn(fd, header, sizeof(*header));
2722 if (ret <= 0)
2723 return -1;
2724
Stephane Eranian114382a2012-02-09 23:21:08 +01002725 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2726 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002727 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002728 }
2729
2730 if (ph->needs_swap)
2731 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002732
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002733 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002734 return -1;
2735
Tom Zanussi8dc58102010-04-01 23:59:15 -05002736 return 0;
2737}
2738
Jiri Olsad4339562013-07-17 19:49:41 +02002739static int perf_header__read_pipe(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002740{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002741 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002742 struct perf_pipe_file_header f_header;
2743
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002744 if (perf_file_header__read_pipe(&f_header, header,
2745 perf_data_file__fd(session->file),
Tom Zanussi454c4072010-05-01 01:41:20 -05002746 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002747 pr_debug("incompatible file format\n");
2748 return -EINVAL;
2749 }
2750
Tom Zanussi8dc58102010-04-01 23:59:15 -05002751 return 0;
2752}
2753
Stephane Eranian69996df2012-02-09 23:21:06 +01002754static int read_attr(int fd, struct perf_header *ph,
2755 struct perf_file_attr *f_attr)
2756{
2757 struct perf_event_attr *attr = &f_attr->attr;
2758 size_t sz, left;
2759 size_t our_sz = sizeof(f_attr->attr);
Jiri Olsa727ebd52013-11-28 11:30:14 +01002760 ssize_t ret;
Stephane Eranian69996df2012-02-09 23:21:06 +01002761
2762 memset(f_attr, 0, sizeof(*f_attr));
2763
2764 /* read minimal guaranteed structure */
2765 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2766 if (ret <= 0) {
2767 pr_debug("cannot read %d bytes of header attr\n",
2768 PERF_ATTR_SIZE_VER0);
2769 return -1;
2770 }
2771
2772 /* on file perf_event_attr size */
2773 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002774
Stephane Eranian69996df2012-02-09 23:21:06 +01002775 if (ph->needs_swap)
2776 sz = bswap_32(sz);
2777
2778 if (sz == 0) {
2779 /* assume ABI0 */
2780 sz = PERF_ATTR_SIZE_VER0;
2781 } else if (sz > our_sz) {
2782 pr_debug("file uses a more recent and unsupported ABI"
2783 " (%zu bytes extra)\n", sz - our_sz);
2784 return -1;
2785 }
2786 /* what we have not yet read and that we know about */
2787 left = sz - PERF_ATTR_SIZE_VER0;
2788 if (left) {
2789 void *ptr = attr;
2790 ptr += PERF_ATTR_SIZE_VER0;
2791
2792 ret = readn(fd, ptr, left);
2793 }
2794 /* read perf_file_section, ids are read in caller */
2795 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2796
2797 return ret <= 0 ? -1 : 0;
2798}
2799
Namhyung Kim831394b2012-09-06 11:10:46 +09002800static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2801 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002802{
Namhyung Kim831394b2012-09-06 11:10:46 +09002803 struct event_format *event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002804 char bf[128];
2805
Namhyung Kim831394b2012-09-06 11:10:46 +09002806 /* already prepared */
2807 if (evsel->tp_format)
2808 return 0;
2809
Namhyung Kim3dce2ce2013-03-21 16:18:48 +09002810 if (pevent == NULL) {
2811 pr_debug("broken or missing trace data\n");
2812 return -1;
2813 }
2814
Namhyung Kim831394b2012-09-06 11:10:46 +09002815 event = pevent_find_event(pevent, evsel->attr.config);
Namhyung Kima7619ae2013-04-18 21:24:16 +09002816 if (event == NULL) {
2817 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002818 return -1;
Namhyung Kima7619ae2013-04-18 21:24:16 +09002819 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002820
Namhyung Kim831394b2012-09-06 11:10:46 +09002821 if (!evsel->name) {
2822 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2823 evsel->name = strdup(bf);
2824 if (evsel->name == NULL)
2825 return -1;
2826 }
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002827
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002828 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002829 return 0;
2830}
2831
Namhyung Kim831394b2012-09-06 11:10:46 +09002832static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2833 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002834{
2835 struct perf_evsel *pos;
2836
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002837 evlist__for_each_entry(evlist, pos) {
Namhyung Kim831394b2012-09-06 11:10:46 +09002838 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2839 perf_evsel__prepare_tracepoint_event(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002840 return -1;
2841 }
2842
2843 return 0;
2844}
2845
Jiri Olsad4339562013-07-17 19:49:41 +02002846int perf_session__read_header(struct perf_session *session)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002847{
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002848 struct perf_data_file *file = session->file;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002849 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002850 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002851 struct perf_file_attr f_attr;
2852 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002853 int nr_attrs, nr_ids, i, j;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002854 int fd = perf_data_file__fd(file);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002855
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002856 session->evlist = perf_evlist__new();
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002857 if (session->evlist == NULL)
2858 return -ENOMEM;
2859
Kan Liang2c071442015-08-28 05:48:05 -04002860 session->evlist->env = &header->env;
Arnaldo Carvalho de Melo4cde9982015-09-09 12:25:00 -03002861 session->machines.host.env = &header->env;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002862 if (perf_data_file__is_pipe(file))
Jiri Olsad4339562013-07-17 19:49:41 +02002863 return perf_header__read_pipe(session);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002864
Stephane Eranian69996df2012-02-09 23:21:06 +01002865 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002866 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002867
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002868 /*
2869 * Sanity check that perf.data was written cleanly; data size is
2870 * initialized to 0 and updated only if the on_exit function is run.
2871 * If data size is still 0 then the file contains only partial
2872 * information. Just warn user and process it as much as it can.
2873 */
2874 if (f_header.data.size == 0) {
2875 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2876 "Was the 'perf record' command properly terminated?\n",
Jiri Olsacc9784bd2013-10-15 16:27:34 +02002877 file->path);
Namhyung Kimb314e5c2013-09-30 17:19:48 +09002878 }
2879
Stephane Eranian69996df2012-02-09 23:21:06 +01002880 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002881 lseek(fd, f_header.attrs.offset, SEEK_SET);
2882
2883 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002884 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002885 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002886
Stephane Eranian69996df2012-02-09 23:21:06 +01002887 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002888 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002889
David Ahern1060ab82015-04-09 16:15:46 -04002890 if (header->needs_swap) {
2891 f_attr.ids.size = bswap_64(f_attr.ids.size);
2892 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
David Aherneda39132011-07-15 12:34:09 -06002893 perf_event__attr_swap(&f_attr.attr);
David Ahern1060ab82015-04-09 16:15:46 -04002894 }
David Aherneda39132011-07-15 12:34:09 -06002895
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002896 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03002897 evsel = perf_evsel__new(&f_attr.attr);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002898
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002899 if (evsel == NULL)
2900 goto out_delete_evlist;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03002901
2902 evsel->needs_swap = header->needs_swap;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002903 /*
2904 * Do it before so that if perf_evsel__alloc_id fails, this
2905 * entry gets purged too at perf_evlist__delete().
2906 */
2907 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002908
2909 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002910 /*
2911 * We don't have the cpu and thread maps on the header, so
2912 * for allocating the perf_sample_id table we fake 1 cpu and
2913 * hattr->ids threads.
2914 */
2915 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2916 goto out_delete_evlist;
2917
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002918 lseek(fd, f_attr.ids.offset, SEEK_SET);
2919
2920 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002921 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002922 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002923
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002924 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002925 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002926
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002927 lseek(fd, tmp, SEEK_SET);
2928 }
2929
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002930 symbol_conf.nr_events = nr_attrs;
2931
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002932 perf_header__process_sections(header, fd, &session->tevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002933 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002934
Namhyung Kim831394b2012-09-06 11:10:46 +09002935 if (perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01002936 session->tevent.pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002937 goto out_delete_evlist;
2938
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002939 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002940out_errno:
2941 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002942
2943out_delete_evlist:
2944 perf_evlist__delete(session->evlist);
2945 session->evlist = NULL;
2946 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002947}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002948
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002949int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002950 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002951 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002952{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002953 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002954 size_t size;
2955 int err;
2956
2957 size = sizeof(struct perf_event_attr);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03002958 size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002959 size += sizeof(struct perf_event_header);
2960 size += ids * sizeof(u64);
2961
2962 ev = malloc(size);
2963
Chris Samuelce47dc52010-11-13 13:35:06 +11002964 if (ev == NULL)
2965 return -ENOMEM;
2966
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002967 ev->attr.attr = *attr;
2968 memcpy(ev->attr.id, id, ids * sizeof(u64));
2969
2970 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002971 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002972
Robert Richterf4d83432012-08-16 21:10:17 +02002973 if (ev->attr.header.size == size)
2974 err = process(tool, ev, NULL, NULL);
2975 else
2976 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002977
2978 free(ev);
2979
2980 return err;
2981}
2982
Jiri Olsaa6e52812015-10-25 15:51:37 +01002983static struct event_update_event *
2984event_update_event__new(size_t size, u64 type, u64 id)
2985{
2986 struct event_update_event *ev;
2987
2988 size += sizeof(*ev);
2989 size = PERF_ALIGN(size, sizeof(u64));
2990
2991 ev = zalloc(size);
2992 if (ev) {
2993 ev->header.type = PERF_RECORD_EVENT_UPDATE;
2994 ev->header.size = (u16)size;
2995 ev->type = type;
2996 ev->id = id;
2997 }
2998 return ev;
2999}
3000
3001int
3002perf_event__synthesize_event_update_unit(struct perf_tool *tool,
3003 struct perf_evsel *evsel,
3004 perf_event__handler_t process)
3005{
3006 struct event_update_event *ev;
3007 size_t size = strlen(evsel->unit);
3008 int err;
3009
3010 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
3011 if (ev == NULL)
3012 return -ENOMEM;
3013
3014 strncpy(ev->data, evsel->unit, size);
3015 err = process(tool, (union perf_event *)ev, NULL, NULL);
3016 free(ev);
3017 return err;
3018}
3019
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003020int
3021perf_event__synthesize_event_update_scale(struct perf_tool *tool,
3022 struct perf_evsel *evsel,
3023 perf_event__handler_t process)
3024{
3025 struct event_update_event *ev;
3026 struct event_update_event_scale *ev_data;
3027 int err;
3028
3029 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
3030 if (ev == NULL)
3031 return -ENOMEM;
3032
3033 ev_data = (struct event_update_event_scale *) ev->data;
3034 ev_data->scale = evsel->scale;
3035 err = process(tool, (union perf_event*) ev, NULL, NULL);
3036 free(ev);
3037 return err;
3038}
3039
Jiri Olsa802c9042015-10-25 15:51:39 +01003040int
3041perf_event__synthesize_event_update_name(struct perf_tool *tool,
3042 struct perf_evsel *evsel,
3043 perf_event__handler_t process)
3044{
3045 struct event_update_event *ev;
3046 size_t len = strlen(evsel->name);
3047 int err;
3048
3049 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
3050 if (ev == NULL)
3051 return -ENOMEM;
3052
3053 strncpy(ev->data, evsel->name, len);
3054 err = process(tool, (union perf_event*) ev, NULL, NULL);
3055 free(ev);
3056 return err;
3057}
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003058
Jiri Olsa86ebb092015-10-25 15:51:40 +01003059int
3060perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
3061 struct perf_evsel *evsel,
3062 perf_event__handler_t process)
3063{
3064 size_t size = sizeof(struct event_update_event);
3065 struct event_update_event *ev;
3066 int max, err;
3067 u16 type;
3068
3069 if (!evsel->own_cpus)
3070 return 0;
3071
3072 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3073 if (!ev)
3074 return -ENOMEM;
3075
3076 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3077 ev->header.size = (u16)size;
3078 ev->type = PERF_EVENT_UPDATE__CPUS;
3079 ev->id = evsel->id[0];
3080
3081 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3082 evsel->own_cpus,
3083 type, max);
3084
3085 err = process(tool, (union perf_event*) ev, NULL, NULL);
3086 free(ev);
3087 return err;
3088}
3089
Jiri Olsac853f932015-10-25 15:51:41 +01003090size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3091{
3092 struct event_update_event *ev = &event->event_update;
3093 struct event_update_event_scale *ev_scale;
3094 struct event_update_event_cpus *ev_cpus;
3095 struct cpu_map *map;
3096 size_t ret;
3097
3098 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3099
3100 switch (ev->type) {
3101 case PERF_EVENT_UPDATE__SCALE:
3102 ev_scale = (struct event_update_event_scale *) ev->data;
3103 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3104 break;
3105 case PERF_EVENT_UPDATE__UNIT:
3106 ret += fprintf(fp, "... unit: %s\n", ev->data);
3107 break;
3108 case PERF_EVENT_UPDATE__NAME:
3109 ret += fprintf(fp, "... name: %s\n", ev->data);
3110 break;
3111 case PERF_EVENT_UPDATE__CPUS:
3112 ev_cpus = (struct event_update_event_cpus *) ev->data;
3113 ret += fprintf(fp, "... ");
3114
3115 map = cpu_map__new_data(&ev_cpus->cpus);
3116 if (map)
3117 ret += cpu_map__fprintf(map, fp);
3118 else
3119 ret += fprintf(fp, "failed to get cpus\n");
3120 break;
3121 default:
3122 ret += fprintf(fp, "... unknown type\n");
3123 break;
3124 }
3125
3126 return ret;
3127}
Jiri Olsa86ebb092015-10-25 15:51:40 +01003128
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003129int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003130 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003131 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003132{
Robert Richter6606f872012-08-16 21:10:19 +02003133 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003134 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003135
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03003136 evlist__for_each_entry(session->evlist, evsel) {
Robert Richter6606f872012-08-16 21:10:19 +02003137 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3138 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003139 if (err) {
3140 pr_debug("failed to create perf header attribute\n");
3141 return err;
3142 }
3143 }
3144
3145 return err;
3146}
3147
Adrian Hunter47c3d102013-07-04 16:20:21 +03003148int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3149 union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003150 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003151{
Robert Richterf4d83432012-08-16 21:10:17 +02003152 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003153 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003154 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003155
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003156 if (evlist == NULL) {
Namhyung Kim334fe7a2013-03-11 16:43:12 +09003157 *pevlist = evlist = perf_evlist__new();
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003158 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003159 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003160 }
3161
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -03003162 evsel = perf_evsel__new(&event->attr.attr);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003163 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003164 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003165
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003166 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003167
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003168 ids = event->header.size;
3169 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003170 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03003171 /*
3172 * We don't have the cpu and thread maps on the header, so
3173 * for allocating the perf_sample_id table we fake 1 cpu and
3174 * hattr->ids threads.
3175 */
3176 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3177 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003178
3179 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02003180 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003181 }
3182
Adrian Hunter7e0d6fc2013-07-04 16:20:29 +03003183 symbol_conf.nr_events = evlist->nr_entries;
3184
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05003185 return 0;
3186}
Tom Zanussicd19a032010-04-01 23:59:20 -05003187
Jiri Olsaffe777252015-10-25 15:51:36 +01003188int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3189 union perf_event *event,
3190 struct perf_evlist **pevlist)
3191{
3192 struct event_update_event *ev = &event->event_update;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003193 struct event_update_event_scale *ev_scale;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003194 struct event_update_event_cpus *ev_cpus;
Jiri Olsaffe777252015-10-25 15:51:36 +01003195 struct perf_evlist *evlist;
3196 struct perf_evsel *evsel;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003197 struct cpu_map *map;
Jiri Olsaffe777252015-10-25 15:51:36 +01003198
3199 if (!pevlist || *pevlist == NULL)
3200 return -EINVAL;
3201
3202 evlist = *pevlist;
3203
3204 evsel = perf_evlist__id2evsel(evlist, ev->id);
3205 if (evsel == NULL)
3206 return -EINVAL;
3207
Jiri Olsaa6e52812015-10-25 15:51:37 +01003208 switch (ev->type) {
3209 case PERF_EVENT_UPDATE__UNIT:
3210 evsel->unit = strdup(ev->data);
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003211 break;
Jiri Olsa802c9042015-10-25 15:51:39 +01003212 case PERF_EVENT_UPDATE__NAME:
3213 evsel->name = strdup(ev->data);
3214 break;
Jiri Olsadaeecbc2015-10-25 15:51:38 +01003215 case PERF_EVENT_UPDATE__SCALE:
3216 ev_scale = (struct event_update_event_scale *) ev->data;
3217 evsel->scale = ev_scale->scale;
Arnaldo Carvalho de Melo8434a2e2017-02-08 21:57:22 -03003218 break;
Jiri Olsa86ebb092015-10-25 15:51:40 +01003219 case PERF_EVENT_UPDATE__CPUS:
3220 ev_cpus = (struct event_update_event_cpus *) ev->data;
3221
3222 map = cpu_map__new_data(&ev_cpus->cpus);
3223 if (map)
3224 evsel->own_cpus = map;
3225 else
3226 pr_err("failed to get event_update cpus\n");
Jiri Olsaa6e52812015-10-25 15:51:37 +01003227 default:
3228 break;
3229 }
3230
Jiri Olsaffe777252015-10-25 15:51:36 +01003231 return 0;
3232}
3233
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003234int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003235 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003236 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05003237{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003238 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02003239 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05003240 ssize_t size = 0, aligned_size = 0, padding;
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003241 int err __maybe_unused = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05003242
Jiri Olsa29208e52011-10-20 15:59:43 +02003243 /*
3244 * We are going to store the size of the data followed
3245 * by the data contents. Since the fd descriptor is a pipe,
3246 * we cannot seek back to store the size of the data once
3247 * we know it. Instead we:
3248 *
3249 * - write the tracing data to the temp file
3250 * - get/write the data size to pipe
3251 * - write the tracing data from the temp file
3252 * to the pipe
3253 */
3254 tdata = tracing_data_get(&evlist->entries, fd, true);
3255 if (!tdata)
3256 return -1;
3257
Tom Zanussi92155452010-04-01 23:59:21 -05003258 memset(&ev, 0, sizeof(ev));
3259
3260 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02003261 size = tdata->size;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003262 aligned_size = PERF_ALIGN(size, sizeof(u64));
Tom Zanussi92155452010-04-01 23:59:21 -05003263 padding = aligned_size - size;
3264 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3265 ev.tracing_data.size = aligned_size;
3266
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003267 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05003268
Jiri Olsa29208e52011-10-20 15:59:43 +02003269 /*
3270 * The put function will copy all the tracing data
3271 * stored in temp file to the pipe.
3272 */
3273 tracing_data_put(tdata);
3274
Tom Zanussi92155452010-04-01 23:59:21 -05003275 write_padded(fd, NULL, 0, padding);
3276
3277 return aligned_size;
3278}
3279
Adrian Hunter47c3d102013-07-04 16:20:21 +03003280int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3281 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003282 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05003283{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003284 ssize_t size_read, padding, size = event->tracing_data.size;
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003285 int fd = perf_data_file__fd(session->file);
3286 off_t offset = lseek(fd, 0, SEEK_CUR);
Tom Zanussi92155452010-04-01 23:59:21 -05003287 char buf[BUFSIZ];
3288
3289 /* setup for reading amidst mmap */
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003290 lseek(fd, offset + sizeof(struct tracing_data_event),
Tom Zanussi92155452010-04-01 23:59:21 -05003291 SEEK_SET);
3292
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003293 size_read = trace_report(fd, &session->tevent,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03003294 session->repipe);
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003295 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
Tom Zanussi92155452010-04-01 23:59:21 -05003296
Jiri Olsacc9784bd2013-10-15 16:27:34 +02003297 if (readn(fd, buf, padding) < 0) {
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003298 pr_err("%s: reading input file", __func__);
3299 return -1;
3300 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003301 if (session->repipe) {
3302 int retw = write(STDOUT_FILENO, buf, padding);
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003303 if (retw <= 0 || retw != padding) {
3304 pr_err("%s: repiping tracing data padding", __func__);
3305 return -1;
3306 }
Tom Zanussi454c4072010-05-01 01:41:20 -05003307 }
Tom Zanussi92155452010-04-01 23:59:21 -05003308
Arnaldo Carvalho de Melo2caa48a2013-01-24 22:34:33 -03003309 if (size_read + padding != size) {
3310 pr_err("%s: tracing data size mismatch", __func__);
3311 return -1;
3312 }
Tom Zanussi92155452010-04-01 23:59:21 -05003313
Namhyung Kim831394b2012-09-06 11:10:46 +09003314 perf_evlist__prepare_tracepoint_events(session->evlist,
Jiri Olsa29f5ffd2013-12-03 14:09:23 +01003315 session->tevent.pevent);
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03003316
Tom Zanussi92155452010-04-01 23:59:21 -05003317 return size_read + padding;
3318}
Tom Zanussic7929e42010-04-01 23:59:22 -05003319
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003320int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003321 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003322 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02003323 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05003324{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003325 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05003326 size_t len;
3327 int err = 0;
3328
3329 if (!pos->hit)
3330 return err;
3331
3332 memset(&ev, 0, sizeof(ev));
3333
3334 len = pos->long_name_len + 1;
Irina Tirdea9ac3e482012-09-11 01:15:01 +03003335 len = PERF_ALIGN(len, NAME_ALIGN);
Tom Zanussic7929e42010-04-01 23:59:22 -05003336 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3337 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3338 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03003339 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05003340 ev.build_id.header.size = sizeof(ev.build_id) + len;
3341 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3342
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02003343 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05003344
3345 return err;
3346}
3347
Irina Tirdea1d037ca2012-09-11 01:15:03 +03003348int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02003349 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003350 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05003351{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02003352 __event_process_build_id(&event->build_id,
3353 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08003354 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05003355 return 0;
3356}