1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <inttypes.h>
4 #include "util.h"
5 #include "string2.h"
6 #include <sys/param.h>
7 #include <sys/types.h>
8 #include <byteswap.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <linux/compiler.h>
13 #include <linux/list.h>
14 #include <linux/kernel.h>
15 #include <linux/bitops.h>
16 #include <linux/stringify.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <sys/utsname.h>
20 #include <unistd.h>
21
22 #include "evlist.h"
23 #include "evsel.h"
24 #include "header.h"
25 #include "memswap.h"
26 #include "../perf.h"
27 #include "trace-event.h"
28 #include "session.h"
29 #include "symbol.h"
30 #include "debug.h"
31 #include "cpumap.h"
32 #include "pmu.h"
33 #include "vdso.h"
34 #include "strbuf.h"
35 #include "build-id.h"
36 #include "data.h"
37 #include <api/fs/fs.h>
38 #include "asm/bug.h"
39 #include "tool.h"
40
41 #include "sane_ctype.h"
42
43 /*
44 * magic2 = "PERFILE2"
45 * must be a numerical value to let the endianness
46 * determine the memory layout. That way we are able
47 * to detect endianness when reading the perf.data file
48 * back.
49 *
50 * we check for legacy (PERFFILE) format.
51 */
52 static const char *__perf_magic1 = "PERFFILE";
53 static const u64 __perf_magic2 = 0x32454c4946524550ULL;
54 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
55
56 #define PERF_MAGIC __perf_magic2
57
58 const char perf_version_string[] = PERF_VERSION;
59
60 struct perf_file_attr {
61 struct perf_event_attr attr;
62 struct perf_file_section ids;
63 };
64
65 struct feat_fd {
66 struct perf_header *ph;
67 int fd;
68 void *buf; /* Either buf != NULL or fd >= 0 */
69 ssize_t offset;
70 size_t size;
71 struct perf_evsel *events;
72 };
73
perf_header__set_feat(struct perf_header * header,int feat)74 void perf_header__set_feat(struct perf_header *header, int feat)
75 {
76 set_bit(feat, header->adds_features);
77 }
78
perf_header__clear_feat(struct perf_header * header,int feat)79 void perf_header__clear_feat(struct perf_header *header, int feat)
80 {
81 clear_bit(feat, header->adds_features);
82 }
83
perf_header__has_feat(const struct perf_header * header,int feat)84 bool perf_header__has_feat(const struct perf_header *header, int feat)
85 {
86 return test_bit(feat, header->adds_features);
87 }
88
__do_write_fd(struct feat_fd * ff,const void * buf,size_t size)89 static int __do_write_fd(struct feat_fd *ff, const void *buf, size_t size)
90 {
91 ssize_t ret = writen(ff->fd, buf, size);
92
93 if (ret != (ssize_t)size)
94 return ret < 0 ? (int)ret : -1;
95 return 0;
96 }
97
__do_write_buf(struct feat_fd * ff,const void * buf,size_t size)98 static int __do_write_buf(struct feat_fd *ff, const void *buf, size_t size)
99 {
100 /* struct perf_event_header::size is u16 */
101 const size_t max_size = 0xffff - sizeof(struct perf_event_header);
102 size_t new_size = ff->size;
103 void *addr;
104
105 if (size + ff->offset > max_size)
106 return -E2BIG;
107
108 while (size > (new_size - ff->offset))
109 new_size <<= 1;
110 new_size = min(max_size, new_size);
111
112 if (ff->size < new_size) {
113 addr = realloc(ff->buf, new_size);
114 if (!addr)
115 return -ENOMEM;
116 ff->buf = addr;
117 ff->size = new_size;
118 }
119
120 memcpy(ff->buf + ff->offset, buf, size);
121 ff->offset += size;
122
123 return 0;
124 }
125
126 /* Return: 0 if succeded, -ERR if failed. */
do_write(struct feat_fd * ff,const void * buf,size_t size)127 int do_write(struct feat_fd *ff, const void *buf, size_t size)
128 {
129 if (!ff->buf)
130 return __do_write_fd(ff, buf, size);
131 return __do_write_buf(ff, buf, size);
132 }
133
134 /* Return: 0 if succeded, -ERR if failed. */
write_padded(struct feat_fd * ff,const void * bf,size_t count,size_t count_aligned)135 int write_padded(struct feat_fd *ff, const void *bf,
136 size_t count, size_t count_aligned)
137 {
138 static const char zero_buf[NAME_ALIGN];
139 int err = do_write(ff, bf, count);
140
141 if (!err)
142 err = do_write(ff, zero_buf, count_aligned - count);
143
144 return err;
145 }
146
147 #define string_size(str) \
148 (PERF_ALIGN((strlen(str) + 1), NAME_ALIGN) + sizeof(u32))
149
150 /* Return: 0 if succeded, -ERR if failed. */
do_write_string(struct feat_fd * ff,const char * str)151 static int do_write_string(struct feat_fd *ff, const char *str)
152 {
153 u32 len, olen;
154 int ret;
155
156 olen = strlen(str) + 1;
157 len = PERF_ALIGN(olen, NAME_ALIGN);
158
159 /* write len, incl. \0 */
160 ret = do_write(ff, &len, sizeof(len));
161 if (ret < 0)
162 return ret;
163
164 return write_padded(ff, str, olen, len);
165 }
166
__do_read_fd(struct feat_fd * ff,void * addr,ssize_t size)167 static int __do_read_fd(struct feat_fd *ff, void *addr, ssize_t size)
168 {
169 ssize_t ret = readn(ff->fd, addr, size);
170
171 if (ret != size)
172 return ret < 0 ? (int)ret : -1;
173 return 0;
174 }
175
__do_read_buf(struct feat_fd * ff,void * addr,ssize_t size)176 static int __do_read_buf(struct feat_fd *ff, void *addr, ssize_t size)
177 {
178 if (size > (ssize_t)ff->size - ff->offset)
179 return -1;
180
181 memcpy(addr, ff->buf + ff->offset, size);
182 ff->offset += size;
183
184 return 0;
185
186 }
187
__do_read(struct feat_fd * ff,void * addr,ssize_t size)188 static int __do_read(struct feat_fd *ff, void *addr, ssize_t size)
189 {
190 if (!ff->buf)
191 return __do_read_fd(ff, addr, size);
192 return __do_read_buf(ff, addr, size);
193 }
194
do_read_u32(struct feat_fd * ff,u32 * addr)195 static int do_read_u32(struct feat_fd *ff, u32 *addr)
196 {
197 int ret;
198
199 ret = __do_read(ff, addr, sizeof(*addr));
200 if (ret)
201 return ret;
202
203 if (ff->ph->needs_swap)
204 *addr = bswap_32(*addr);
205 return 0;
206 }
207
do_read_u64(struct feat_fd * ff,u64 * addr)208 static int do_read_u64(struct feat_fd *ff, u64 *addr)
209 {
210 int ret;
211
212 ret = __do_read(ff, addr, sizeof(*addr));
213 if (ret)
214 return ret;
215
216 if (ff->ph->needs_swap)
217 *addr = bswap_64(*addr);
218 return 0;
219 }
220
do_read_string(struct feat_fd * ff)221 static char *do_read_string(struct feat_fd *ff)
222 {
223 u32 len;
224 char *buf;
225
226 if (do_read_u32(ff, &len))
227 return NULL;
228
229 buf = malloc(len);
230 if (!buf)
231 return NULL;
232
233 if (!__do_read(ff, buf, len)) {
234 /*
235 * strings are padded by zeroes
236 * thus the actual strlen of buf
237 * may be less than len
238 */
239 return buf;
240 }
241
242 free(buf);
243 return NULL;
244 }
245
write_tracing_data(struct feat_fd * ff,struct perf_evlist * evlist)246 static int write_tracing_data(struct feat_fd *ff,
247 struct perf_evlist *evlist)
248 {
249 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
250 return -1;
251
252 return read_tracing_data(ff->fd, &evlist->entries);
253 }
254
write_build_id(struct feat_fd * ff,struct perf_evlist * evlist __maybe_unused)255 static int write_build_id(struct feat_fd *ff,
256 struct perf_evlist *evlist __maybe_unused)
257 {
258 struct perf_session *session;
259 int err;
260
261 session = container_of(ff->ph, struct perf_session, header);
262
263 if (!perf_session__read_build_ids(session, true))
264 return -1;
265
266 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
267 return -1;
268
269 err = perf_session__write_buildid_table(session, ff);
270 if (err < 0) {
271 pr_debug("failed to write buildid table\n");
272 return err;
273 }
274 perf_session__cache_build_ids(session);
275
276 return 0;
277 }
278
write_hostname(struct feat_fd * ff,struct perf_evlist * evlist __maybe_unused)279 static int write_hostname(struct feat_fd *ff,
280 struct perf_evlist *evlist __maybe_unused)
281 {
282 struct utsname uts;
283 int ret;
284
285 ret = uname(&uts);
286 if (ret < 0)
287 return -1;
288
289 return do_write_string(ff, uts.nodename);
290 }
291
write_osrelease(struct feat_fd * ff,struct perf_evlist * evlist __maybe_unused)292 static int write_osrelease(struct feat_fd *ff,
293 struct perf_evlist *evlist __maybe_unused)
294 {
295 struct utsname uts;
296 int ret;
297
298 ret = uname(&uts);
299 if (ret < 0)
300 return -1;
301
302 return do_write_string(ff, uts.release);
303 }
304
write_arch(struct feat_fd * ff,struct perf_evlist * evlist __maybe_unused)305 static int write_arch(struct feat_fd *ff,
306 struct perf_evlist *evlist __maybe_unused)
307 {
308 struct utsname uts;
309 int ret;
310
311 ret = uname(&uts);
312 if (ret < 0)
313 return -1;
314
315 return do_write_string(ff, uts.machine);
316 }
317
write_version(struct feat_fd * ff,struct perf_evlist * evlist __maybe_unused)318 static int write_version(struct feat_fd *ff,
319 struct perf_evlist *evlist __maybe_unused)
320 {
321 return do_write_string(ff, perf_version_string);
322 }
323
__write_cpudesc(struct feat_fd * ff,const char * cpuinfo_proc)324 static int __write_cpudesc(struct feat_fd *ff, const char *cpuinfo_proc)
325 {
326 FILE *file;
327 char *buf = NULL;
328 char *s, *p;
329 const char *search = cpuinfo_proc;
330 size_t len = 0;
331 int ret = -1;
332
333 if (!search)
334 return -1;
335
336 file = fopen("/proc/cpuinfo", "r");
337 if (!file)
338 return -1;
339
340 while (getline(&buf, &len, file) > 0) {
341 ret = strncmp(buf, search, strlen(search));
342 if (!ret)
343 break;
344 }
345
346 if (ret) {
347 ret = -1;
348 goto done;
349 }
350
351 s = buf;
352
353 p = strchr(buf, ':');
354 if (p && *(p+1) == ' ' && *(p+2))
355 s = p + 2;
356 p = strchr(s, '\n');
357 if (p)
358 *p = '\0';
359
360 /* squash extra space characters (branding string) */
361 p = s;
362 while (*p) {
363 if (isspace(*p)) {
364 char *r = p + 1;
365 char *q = r;
366 *p = ' ';
367 while (*q && isspace(*q))
368 q++;
369 if (q != (p+1))
370 while ((*r++ = *q++));
371 }
372 p++;
373 }
374 ret = do_write_string(ff, s);
375 done:
376 free(buf);
377 fclose(file);
378 return ret;
379 }
380
write_cpudesc(struct feat_fd * ff,struct perf_evlist * evlist __maybe_unused)381 static int write_cpudesc(struct feat_fd *ff,
382 struct perf_evlist *evlist __maybe_unused)
383 {
384 const char *cpuinfo_procs[] = CPUINFO_PROC;
385 unsigned int i;
386
387 for (i = 0; i < ARRAY_SIZE(cpuinfo_procs); i++) {
388 int ret;
389 ret = __write_cpudesc(ff, cpuinfo_procs[i]);
390 if (ret >= 0)
391 return ret;
392 }
393 return -1;
394 }
395
396
write_nrcpus(struct feat_fd * ff,struct perf_evlist * evlist __maybe_unused)397 static int write_nrcpus(struct feat_fd *ff,
398 struct perf_evlist *evlist __maybe_unused)
399 {
400 long nr;
401 u32 nrc, nra;
402 int ret;
403
404 nrc = cpu__max_present_cpu();
405
406 nr = sysconf(_SC_NPROCESSORS_ONLN);
407 if (nr < 0)
408 return -1;
409
410 nra = (u32)(nr & UINT_MAX);
411
412 ret = do_write(ff, &nrc, sizeof(nrc));
413 if (ret < 0)
414 return ret;
415
416 return do_write(ff, &nra, sizeof(nra));
417 }
418
write_event_desc(struct feat_fd * ff,struct perf_evlist * evlist)419 static int write_event_desc(struct feat_fd *ff,
420 struct perf_evlist *evlist)
421 {
422 struct perf_evsel *evsel;
423 u32 nre, nri, sz;
424 int ret;
425
426 nre = evlist->nr_entries;
427
428 /*
429 * write number of events
430 */
431 ret = do_write(ff, &nre, sizeof(nre));
432 if (ret < 0)
433 return ret;
434
435 /*
436 * size of perf_event_attr struct
437 */
438 sz = (u32)sizeof(evsel->attr);
439 ret = do_write(ff, &sz, sizeof(sz));
440 if (ret < 0)
441 return ret;
442
443 evlist__for_each_entry(evlist, evsel) {
444 ret = do_write(ff, &evsel->attr, sz);
445 if (ret < 0)
446 return ret;
447 /*
448 * write number of unique id per event
449 * there is one id per instance of an event
450 *
451 * copy into an nri to be independent of the
452 * type of ids,
453 */
454 nri = evsel->ids;
455 ret = do_write(ff, &nri, sizeof(nri));
456 if (ret < 0)
457 return ret;
458
459 /*
460 * write event string as passed on cmdline
461 */
462 ret = do_write_string(ff, perf_evsel__name(evsel));
463 if (ret < 0)
464 return ret;
465 /*
466 * write unique ids for this event
467 */
468 ret = do_write(ff, evsel->id, evsel->ids * sizeof(u64));
469 if (ret < 0)
470 return ret;
471 }
472 return 0;
473 }
474
write_cmdline(struct feat_fd * ff,struct perf_evlist * evlist __maybe_unused)475 static int write_cmdline(struct feat_fd *ff,
476 struct perf_evlist *evlist __maybe_unused)
477 {
478 char buf[MAXPATHLEN];
479 u32 n;
480 int i, ret;
481
482 /* actual path to perf binary */
483 ret = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
484 if (ret <= 0)
485 return -1;
486
487 /* readlink() does not add null termination */
488 buf[ret] = '\0';
489
490 /* account for binary path */
491 n = perf_env.nr_cmdline + 1;
492
493 ret = do_write(ff, &n, sizeof(n));
494 if (ret < 0)
495 return ret;
496
497 ret = do_write_string(ff, buf);
498 if (ret < 0)
499 return ret;
500
501 for (i = 0 ; i < perf_env.nr_cmdline; i++) {
502 ret = do_write_string(ff, perf_env.cmdline_argv[i]);
503 if (ret < 0)
504 return ret;
505 }
506 return 0;
507 }
508
509 #define CORE_SIB_FMT \
510 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
511 #define THRD_SIB_FMT \
512 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
513
514 struct cpu_topo {
515 u32 cpu_nr;
516 u32 core_sib;
517 u32 thread_sib;
518 char **core_siblings;
519 char **thread_siblings;
520 };
521
build_cpu_topo(struct cpu_topo * tp,int cpu)522 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
523 {
524 FILE *fp;
525 char filename[MAXPATHLEN];
526 char *buf = NULL, *p;
527 size_t len = 0;
528 ssize_t sret;
529 u32 i = 0;
530 int ret = -1;
531
532 sprintf(filename, CORE_SIB_FMT, cpu);
533 fp = fopen(filename, "r");
534 if (!fp)
535 goto try_threads;
536
537 sret = getline(&buf, &len, fp);
538 fclose(fp);
539 if (sret <= 0)
540 goto try_threads;
541
542 p = strchr(buf, '\n');
543 if (p)
544 *p = '\0';
545
546 for (i = 0; i < tp->core_sib; i++) {
547 if (!strcmp(buf, tp->core_siblings[i]))
548 break;
549 }
550 if (i == tp->core_sib) {
551 tp->core_siblings[i] = buf;
552 tp->core_sib++;
553 buf = NULL;
554 len = 0;
555 }
556 ret = 0;
557
558 try_threads:
559 sprintf(filename, THRD_SIB_FMT, cpu);
560 fp = fopen(filename, "r");
561 if (!fp)
562 goto done;
563
564 if (getline(&buf, &len, fp) <= 0)
565 goto done;
566
567 p = strchr(buf, '\n');
568 if (p)
569 *p = '\0';
570
571 for (i = 0; i < tp->thread_sib; i++) {
572 if (!strcmp(buf, tp->thread_siblings[i]))
573 break;
574 }
575 if (i == tp->thread_sib) {
576 tp->thread_siblings[i] = buf;
577 tp->thread_sib++;
578 buf = NULL;
579 }
580 ret = 0;
581 done:
582 if(fp)
583 fclose(fp);
584 free(buf);
585 return ret;
586 }
587
free_cpu_topo(struct cpu_topo * tp)588 static void free_cpu_topo(struct cpu_topo *tp)
589 {
590 u32 i;
591
592 if (!tp)
593 return;
594
595 for (i = 0 ; i < tp->core_sib; i++)
596 zfree(&tp->core_siblings[i]);
597
598 for (i = 0 ; i < tp->thread_sib; i++)
599 zfree(&tp->thread_siblings[i]);
600
601 free(tp);
602 }
603
build_cpu_topology(void)604 static struct cpu_topo *build_cpu_topology(void)
605 {
606 struct cpu_topo *tp = NULL;
607 void *addr;
608 u32 nr, i;
609 size_t sz;
610 long ncpus;
611 int ret = -1;
612 struct cpu_map *map;
613
614 ncpus = cpu__max_present_cpu();
615
616 /* build online CPU map */
617 map = cpu_map__new(NULL);
618 if (map == NULL) {
619 pr_debug("failed to get system cpumap\n");
620 return NULL;
621 }
622
623 nr = (u32)(ncpus & UINT_MAX);
624
625 sz = nr * sizeof(char *);
626 addr = calloc(1, sizeof(*tp) + 2 * sz);
627 if (!addr)
628 goto out_free;
629
630 tp = addr;
631 tp->cpu_nr = nr;
632 addr += sizeof(*tp);
633 tp->core_siblings = addr;
634 addr += sz;
635 tp->thread_siblings = addr;
636
637 for (i = 0; i < nr; i++) {
638 if (!cpu_map__has(map, i))
639 continue;
640
641 ret = build_cpu_topo(tp, i);
642 if (ret < 0)
643 break;
644 }
645
646 out_free:
647 cpu_map__put(map);
648 if (ret) {
649 free_cpu_topo(tp);
650 tp = NULL;
651 }
652 return tp;
653 }
654
write_cpu_topology(struct feat_fd * ff,struct perf_evlist * evlist __maybe_unused)655 static int write_cpu_topology(struct feat_fd *ff,
656 struct perf_evlist *evlist __maybe_unused)
657 {
658 struct cpu_topo *tp;
659 u32 i;
660 int ret, j;
661
662 tp = build_cpu_topology();
663 if (!tp)
664 return -1;
665
666 ret = do_write(ff, &tp->core_sib, sizeof(tp->core_sib));
667 if (ret < 0)
668 goto done;
669
670 for (i = 0; i < tp->core_sib; i++) {
671 ret = do_write_string(ff, tp->core_siblings[i]);
672 if (ret < 0)
673 goto done;
674 }
675 ret = do_write(ff, &tp->thread_sib, sizeof(tp->thread_sib));
676 if (ret < 0)
677 goto done;
678
679 for (i = 0; i < tp->thread_sib; i++) {
680 ret = do_write_string(ff, tp->thread_siblings[i]);
681 if (ret < 0)
682 break;
683 }
684
685 ret = perf_env__read_cpu_topology_map(&perf_env);
686 if (ret < 0)
687 goto done;
688
689 for (j = 0; j < perf_env.nr_cpus_avail; j++) {
690 ret = do_write(ff, &perf_env.cpu[j].core_id,
691 sizeof(perf_env.cpu[j].core_id));
692 if (ret < 0)
693 return ret;
694 ret = do_write(ff, &perf_env.cpu[j].socket_id,
695 sizeof(perf_env.cpu[j].socket_id));
696 if (ret < 0)
697 return ret;
698 }
699 done:
700 free_cpu_topo(tp);
701 return ret;
702 }
703
704
705
write_total_mem(struct feat_fd * ff,struct perf_evlist * evlist __maybe_unused)706 static int write_total_mem(struct feat_fd *ff,
707 struct perf_evlist *evlist __maybe_unused)
708 {
709 char *buf = NULL;
710 FILE *fp;
711 size_t len = 0;
712 int ret = -1, n;
713 uint64_t mem;
714
715 fp = fopen("/proc/meminfo", "r");
716 if (!fp)
717 return -1;
718
719 while (getline(&buf, &len, fp) > 0) {
720 ret = strncmp(buf, "MemTotal:", 9);
721 if (!ret)
722 break;
723 }
724 if (!ret) {
725 n = sscanf(buf, "%*s %"PRIu64, &mem);
726 if (n == 1)
727 ret = do_write(ff, &mem, sizeof(mem));
728 } else
729 ret = -1;
730 free(buf);
731 fclose(fp);
732 return ret;
733 }
734
write_topo_node(struct feat_fd * ff,int node)735 static int write_topo_node(struct feat_fd *ff, int node)
736 {
737 char str[MAXPATHLEN];
738 char field[32];
739 char *buf = NULL, *p;
740 size_t len = 0;
741 FILE *fp;
742 u64 mem_total, mem_free, mem;
743 int ret = -1;
744
745 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
746 fp = fopen(str, "r");
747 if (!fp)
748 return -1;
749
750 while (getline(&buf, &len, fp) > 0) {
751 /* skip over invalid lines */
752 if (!strchr(buf, ':'))
753 continue;
754 if (sscanf(buf, "%*s %*d %31s %"PRIu64, field, &mem) != 2)
755 goto done;
756 if (!strcmp(field, "MemTotal:"))
757 mem_total = mem;
758 if (!strcmp(field, "MemFree:"))
759 mem_free = mem;
760 }
761
762 fclose(fp);
763 fp = NULL;
764
765 ret = do_write(ff, &mem_total, sizeof(u64));
766 if (ret)
767 goto done;
768
769 ret = do_write(ff, &mem_free, sizeof(u64));
770 if (ret)
771 goto done;
772
773 ret = -1;
774 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
775
776 fp = fopen(str, "r");
777 if (!fp)
778 goto done;
779
780 if (getline(&buf, &len, fp) <= 0)
781 goto done;
782
783 p = strchr(buf, '\n');
784 if (p)
785 *p = '\0';
786
787 ret = do_write_string(ff, buf);
788 done:
789 free(buf);
790 if (fp)
791 fclose(fp);
792 return ret;
793 }
794
write_numa_topology(struct feat_fd * ff,struct perf_evlist * evlist __maybe_unused)795 static int write_numa_topology(struct feat_fd *ff,
796 struct perf_evlist *evlist __maybe_unused)
797 {
798 char *buf = NULL;
799 size_t len = 0;
800 FILE *fp;
801 struct cpu_map *node_map = NULL;
802 char *c;
803 u32 nr, i, j;
804 int ret = -1;
805
806 fp = fopen("/sys/devices/system/node/online", "r");
807 if (!fp)
808 return -1;
809
810 if (getline(&buf, &len, fp) <= 0)
811 goto done;
812
813 c = strchr(buf, '\n');
814 if (c)
815 *c = '\0';
816
817 node_map = cpu_map__new(buf);
818 if (!node_map)
819 goto done;
820
821 nr = (u32)node_map->nr;
822
823 ret = do_write(ff, &nr, sizeof(nr));
824 if (ret < 0)
825 goto done;
826
827 for (i = 0; i < nr; i++) {
828 j = (u32)node_map->map[i];
829 ret = do_write(ff, &j, sizeof(j));
830 if (ret < 0)
831 break;
832
833 ret = write_topo_node(ff, i);
834 if (ret < 0)
835 break;
836 }
837 done:
838 free(buf);
839 fclose(fp);
840 cpu_map__put(node_map);
841 return ret;
842 }
843
844 /*
845 * File format:
846 *
847 * struct pmu_mappings {
848 * u32 pmu_num;
849 * struct pmu_map {
850 * u32 type;
851 * char name[];
852 * }[pmu_num];
853 * };
854 */
855
write_pmu_mappings(struct feat_fd * ff,struct perf_evlist * evlist __maybe_unused)856 static int write_pmu_mappings(struct feat_fd *ff,
857 struct perf_evlist *evlist __maybe_unused)
858 {
859 struct perf_pmu *pmu = NULL;
860 u32 pmu_num = 0;
861 int ret;
862
863 /*
864 * Do a first pass to count number of pmu to avoid lseek so this
865 * works in pipe mode as well.
866 */
867 while ((pmu = perf_pmu__scan(pmu))) {
868 if (!pmu->name)
869 continue;
870 pmu_num++;
871 }
872
873 ret = do_write(ff, &pmu_num, sizeof(pmu_num));
874 if (ret < 0)
875 return ret;
876
877 while ((pmu = perf_pmu__scan(pmu))) {
878 if (!pmu->name)
879 continue;
880
881 ret = do_write(ff, &pmu->type, sizeof(pmu->type));
882 if (ret < 0)
883 return ret;
884
885 ret = do_write_string(ff, pmu->name);
886 if (ret < 0)
887 return ret;
888 }
889
890 return 0;
891 }
892
893 /*
894 * File format:
895 *
896 * struct group_descs {
897 * u32 nr_groups;
898 * struct group_desc {
899 * char name[];
900 * u32 leader_idx;
901 * u32 nr_members;
902 * }[nr_groups];
903 * };
904 */
write_group_desc(struct feat_fd * ff,struct perf_evlist * evlist)905 static int write_group_desc(struct feat_fd *ff,
906 struct perf_evlist *evlist)
907 {
908 u32 nr_groups = evlist->nr_groups;
909 struct perf_evsel *evsel;
910 int ret;
911
912 ret = do_write(ff, &nr_groups, sizeof(nr_groups));
913 if (ret < 0)
914 return ret;
915
916 evlist__for_each_entry(evlist, evsel) {
917 if (perf_evsel__is_group_leader(evsel) &&
918 evsel->nr_members > 1) {
919 const char *name = evsel->group_name ?: "{anon_group}";
920 u32 leader_idx = evsel->idx;
921 u32 nr_members = evsel->nr_members;
922
923 ret = do_write_string(ff, name);
924 if (ret < 0)
925 return ret;
926
927 ret = do_write(ff, &leader_idx, sizeof(leader_idx));
928 if (ret < 0)
929 return ret;
930
931 ret = do_write(ff, &nr_members, sizeof(nr_members));
932 if (ret < 0)
933 return ret;
934 }
935 }
936 return 0;
937 }
938
939 /*
940 * default get_cpuid(): nothing gets recorded
941 * actual implementation must be in arch/$(SRCARCH)/util/header.c
942 */
get_cpuid(char * buffer __maybe_unused,size_t sz __maybe_unused)943 int __weak get_cpuid(char *buffer __maybe_unused, size_t sz __maybe_unused)
944 {
945 return -1;
946 }
947
write_cpuid(struct feat_fd * ff,struct perf_evlist * evlist __maybe_unused)948 static int write_cpuid(struct feat_fd *ff,
949 struct perf_evlist *evlist __maybe_unused)
950 {
951 char buffer[64];
952 int ret;
953
954 ret = get_cpuid(buffer, sizeof(buffer));
955 if (!ret)
956 goto write_it;
957
958 return -1;
959 write_it:
960 return do_write_string(ff, buffer);
961 }
962
write_branch_stack(struct feat_fd * ff __maybe_unused,struct perf_evlist * evlist __maybe_unused)963 static int write_branch_stack(struct feat_fd *ff __maybe_unused,
964 struct perf_evlist *evlist __maybe_unused)
965 {
966 return 0;
967 }
968
write_auxtrace(struct feat_fd * ff,struct perf_evlist * evlist __maybe_unused)969 static int write_auxtrace(struct feat_fd *ff,
970 struct perf_evlist *evlist __maybe_unused)
971 {
972 struct perf_session *session;
973 int err;
974
975 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
976 return -1;
977
978 session = container_of(ff->ph, struct perf_session, header);
979
980 err = auxtrace_index__write(ff->fd, &session->auxtrace_index);
981 if (err < 0)
982 pr_err("Failed to write auxtrace index\n");
983 return err;
984 }
985
cpu_cache_level__sort(const void * a,const void * b)986 static int cpu_cache_level__sort(const void *a, const void *b)
987 {
988 struct cpu_cache_level *cache_a = (struct cpu_cache_level *)a;
989 struct cpu_cache_level *cache_b = (struct cpu_cache_level *)b;
990
991 return cache_a->level - cache_b->level;
992 }
993
cpu_cache_level__cmp(struct cpu_cache_level * a,struct cpu_cache_level * b)994 static bool cpu_cache_level__cmp(struct cpu_cache_level *a, struct cpu_cache_level *b)
995 {
996 if (a->level != b->level)
997 return false;
998
999 if (a->line_size != b->line_size)
1000 return false;
1001
1002 if (a->sets != b->sets)
1003 return false;
1004
1005 if (a->ways != b->ways)
1006 return false;
1007
1008 if (strcmp(a->type, b->type))
1009 return false;
1010
1011 if (strcmp(a->size, b->size))
1012 return false;
1013
1014 if (strcmp(a->map, b->map))
1015 return false;
1016
1017 return true;
1018 }
1019
cpu_cache_level__read(struct cpu_cache_level * cache,u32 cpu,u16 level)1020 static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 level)
1021 {
1022 char path[PATH_MAX], file[PATH_MAX];
1023 struct stat st;
1024 size_t len;
1025
1026 scnprintf(path, PATH_MAX, "devices/system/cpu/cpu%d/cache/index%d/", cpu, level);
1027 scnprintf(file, PATH_MAX, "%s/%s", sysfs__mountpoint(), path);
1028
1029 if (stat(file, &st))
1030 return 1;
1031
1032 scnprintf(file, PATH_MAX, "%s/level", path);
1033 if (sysfs__read_int(file, (int *) &cache->level))
1034 return -1;
1035
1036 scnprintf(file, PATH_MAX, "%s/coherency_line_size", path);
1037 if (sysfs__read_int(file, (int *) &cache->line_size))
1038 return -1;
1039
1040 scnprintf(file, PATH_MAX, "%s/number_of_sets", path);
1041 if (sysfs__read_int(file, (int *) &cache->sets))
1042 return -1;
1043
1044 scnprintf(file, PATH_MAX, "%s/ways_of_associativity", path);
1045 if (sysfs__read_int(file, (int *) &cache->ways))
1046 return -1;
1047
1048 scnprintf(file, PATH_MAX, "%s/type", path);
1049 if (sysfs__read_str(file, &cache->type, &len))
1050 return -1;
1051
1052 cache->type[len] = 0;
1053 cache->type = rtrim(cache->type);
1054
1055 scnprintf(file, PATH_MAX, "%s/size", path);
1056 if (sysfs__read_str(file, &cache->size, &len)) {
1057 free(cache->type);
1058 return -1;
1059 }
1060
1061 cache->size[len] = 0;
1062 cache->size = rtrim(cache->size);
1063
1064 scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
1065 if (sysfs__read_str(file, &cache->map, &len)) {
1066 free(cache->size);
1067 free(cache->type);
1068 return -1;
1069 }
1070
1071 cache->map[len] = 0;
1072 cache->map = rtrim(cache->map);
1073 return 0;
1074 }
1075
cpu_cache_level__fprintf(FILE * out,struct cpu_cache_level * c)1076 static void cpu_cache_level__fprintf(FILE *out, struct cpu_cache_level *c)
1077 {
1078 fprintf(out, "L%d %-15s %8s [%s]\n", c->level, c->type, c->size, c->map);
1079 }
1080
build_caches(struct cpu_cache_level caches[],u32 size,u32 * cntp)1081 static int build_caches(struct cpu_cache_level caches[], u32 size, u32 *cntp)
1082 {
1083 u32 i, cnt = 0;
1084 long ncpus;
1085 u32 nr, cpu;
1086 u16 level;
1087
1088 ncpus = sysconf(_SC_NPROCESSORS_CONF);
1089 if (ncpus < 0)
1090 return -1;
1091
1092 nr = (u32)(ncpus & UINT_MAX);
1093
1094 for (cpu = 0; cpu < nr; cpu++) {
1095 for (level = 0; level < 10; level++) {
1096 struct cpu_cache_level c;
1097 int err;
1098
1099 err = cpu_cache_level__read(&c, cpu, level);
1100 if (err < 0)
1101 return err;
1102
1103 if (err == 1)
1104 break;
1105
1106 for (i = 0; i < cnt; i++) {
1107 if (cpu_cache_level__cmp(&c, &caches[i]))
1108 break;
1109 }
1110
1111 if (i == cnt)
1112 caches[cnt++] = c;
1113 else
1114 cpu_cache_level__free(&c);
1115
1116 if (WARN_ONCE(cnt == size, "way too many cpu caches.."))
1117 goto out;
1118 }
1119 }
1120 out:
1121 *cntp = cnt;
1122 return 0;
1123 }
1124
1125 #define MAX_CACHES (MAX_NR_CPUS * 4)
1126
write_cache(struct feat_fd * ff,struct perf_evlist * evlist __maybe_unused)1127 static int write_cache(struct feat_fd *ff,
1128 struct perf_evlist *evlist __maybe_unused)
1129 {
1130 struct cpu_cache_level caches[MAX_CACHES];
1131 u32 cnt = 0, i, version = 1;
1132 int ret;
1133
1134 ret = build_caches(caches, MAX_CACHES, &cnt);
1135 if (ret)
1136 goto out;
1137
1138 qsort(&caches, cnt, sizeof(struct cpu_cache_level), cpu_cache_level__sort);
1139
1140 ret = do_write(ff, &version, sizeof(u32));
1141 if (ret < 0)
1142 goto out;
1143
1144 ret = do_write(ff, &cnt, sizeof(u32));
1145 if (ret < 0)
1146 goto out;
1147
1148 for (i = 0; i < cnt; i++) {
1149 struct cpu_cache_level *c = &caches[i];
1150
1151 #define _W(v) \
1152 ret = do_write(ff, &c->v, sizeof(u32)); \
1153 if (ret < 0) \
1154 goto out;
1155
1156 _W(level)
1157 _W(line_size)
1158 _W(sets)
1159 _W(ways)
1160 #undef _W
1161
1162 #define _W(v) \
1163 ret = do_write_string(ff, (const char *) c->v); \
1164 if (ret < 0) \
1165 goto out;
1166
1167 _W(type)
1168 _W(size)
1169 _W(map)
1170 #undef _W
1171 }
1172
1173 out:
1174 for (i = 0; i < cnt; i++)
1175 cpu_cache_level__free(&caches[i]);
1176 return ret;
1177 }
1178
write_stat(struct feat_fd * ff __maybe_unused,struct perf_evlist * evlist __maybe_unused)1179 static int write_stat(struct feat_fd *ff __maybe_unused,
1180 struct perf_evlist *evlist __maybe_unused)
1181 {
1182 return 0;
1183 }
1184
print_hostname(struct feat_fd * ff,FILE * fp)1185 static void print_hostname(struct feat_fd *ff, FILE *fp)
1186 {
1187 fprintf(fp, "# hostname : %s\n", ff->ph->env.hostname);
1188 }
1189
print_osrelease(struct feat_fd * ff,FILE * fp)1190 static void print_osrelease(struct feat_fd *ff, FILE *fp)
1191 {
1192 fprintf(fp, "# os release : %s\n", ff->ph->env.os_release);
1193 }
1194
print_arch(struct feat_fd * ff,FILE * fp)1195 static void print_arch(struct feat_fd *ff, FILE *fp)
1196 {
1197 fprintf(fp, "# arch : %s\n", ff->ph->env.arch);
1198 }
1199
print_cpudesc(struct feat_fd * ff,FILE * fp)1200 static void print_cpudesc(struct feat_fd *ff, FILE *fp)
1201 {
1202 fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc);
1203 }
1204
print_nrcpus(struct feat_fd * ff,FILE * fp)1205 static void print_nrcpus(struct feat_fd *ff, FILE *fp)
1206 {
1207 fprintf(fp, "# nrcpus online : %u\n", ff->ph->env.nr_cpus_online);
1208 fprintf(fp, "# nrcpus avail : %u\n", ff->ph->env.nr_cpus_avail);
1209 }
1210
print_version(struct feat_fd * ff,FILE * fp)1211 static void print_version(struct feat_fd *ff, FILE *fp)
1212 {
1213 fprintf(fp, "# perf version : %s\n", ff->ph->env.version);
1214 }
1215
print_cmdline(struct feat_fd * ff,FILE * fp)1216 static void print_cmdline(struct feat_fd *ff, FILE *fp)
1217 {
1218 int nr, i;
1219
1220 nr = ff->ph->env.nr_cmdline;
1221
1222 fprintf(fp, "# cmdline : ");
1223
1224 for (i = 0; i < nr; i++)
1225 fprintf(fp, "%s ", ff->ph->env.cmdline_argv[i]);
1226 fputc('\n', fp);
1227 }
1228
print_cpu_topology(struct feat_fd * ff,FILE * fp)1229 static void print_cpu_topology(struct feat_fd *ff, FILE *fp)
1230 {
1231 struct perf_header *ph = ff->ph;
1232 int cpu_nr = ph->env.nr_cpus_avail;
1233 int nr, i;
1234 char *str;
1235
1236 nr = ph->env.nr_sibling_cores;
1237 str = ph->env.sibling_cores;
1238
1239 for (i = 0; i < nr; i++) {
1240 fprintf(fp, "# sibling cores : %s\n", str);
1241 str += strlen(str) + 1;
1242 }
1243
1244 nr = ph->env.nr_sibling_threads;
1245 str = ph->env.sibling_threads;
1246
1247 for (i = 0; i < nr; i++) {
1248 fprintf(fp, "# sibling threads : %s\n", str);
1249 str += strlen(str) + 1;
1250 }
1251
1252 if (ph->env.cpu != NULL) {
1253 for (i = 0; i < cpu_nr; i++)
1254 fprintf(fp, "# CPU %d: Core ID %d, Socket ID %d\n", i,
1255 ph->env.cpu[i].core_id, ph->env.cpu[i].socket_id);
1256 } else
1257 fprintf(fp, "# Core ID and Socket ID information is not available\n");
1258 }
1259
free_event_desc(struct perf_evsel * events)1260 static void free_event_desc(struct perf_evsel *events)
1261 {
1262 struct perf_evsel *evsel;
1263
1264 if (!events)
1265 return;
1266
1267 for (evsel = events; evsel->attr.size; evsel++) {
1268 zfree(&evsel->name);
1269 zfree(&evsel->id);
1270 }
1271
1272 free(events);
1273 }
1274
read_event_desc(struct feat_fd * ff)1275 static struct perf_evsel *read_event_desc(struct feat_fd *ff)
1276 {
1277 struct perf_evsel *evsel, *events = NULL;
1278 u64 *id;
1279 void *buf = NULL;
1280 u32 nre, sz, nr, i, j;
1281 size_t msz;
1282
1283 /* number of events */
1284 if (do_read_u32(ff, &nre))
1285 goto error;
1286
1287 if (do_read_u32(ff, &sz))
1288 goto error;
1289
1290 /* buffer to hold on file attr struct */
1291 buf = malloc(sz);
1292 if (!buf)
1293 goto error;
1294
1295 /* the last event terminates with evsel->attr.size == 0: */
1296 events = calloc(nre + 1, sizeof(*events));
1297 if (!events)
1298 goto error;
1299
1300 msz = sizeof(evsel->attr);
1301 if (sz < msz)
1302 msz = sz;
1303
1304 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1305 evsel->idx = i;
1306
1307 /*
1308 * must read entire on-file attr struct to
1309 * sync up with layout.
1310 */
1311 if (__do_read(ff, buf, sz))
1312 goto error;
1313
1314 if (ff->ph->needs_swap)
1315 perf_event__attr_swap(buf);
1316
1317 memcpy(&evsel->attr, buf, msz);
1318
1319 if (do_read_u32(ff, &nr))
1320 goto error;
1321
1322 if (ff->ph->needs_swap)
1323 evsel->needs_swap = true;
1324
1325 evsel->name = do_read_string(ff);
1326 if (!evsel->name)
1327 goto error;
1328
1329 if (!nr)
1330 continue;
1331
1332 id = calloc(nr, sizeof(*id));
1333 if (!id)
1334 goto error;
1335 evsel->ids = nr;
1336 evsel->id = id;
1337
1338 for (j = 0 ; j < nr; j++) {
1339 if (do_read_u64(ff, id))
1340 goto error;
1341 id++;
1342 }
1343 }
1344 out:
1345 free(buf);
1346 return events;
1347 error:
1348 free_event_desc(events);
1349 events = NULL;
1350 goto out;
1351 }
1352
__desc_attr__fprintf(FILE * fp,const char * name,const char * val,void * priv __maybe_unused)1353 static int __desc_attr__fprintf(FILE *fp, const char *name, const char *val,
1354 void *priv __maybe_unused)
1355 {
1356 return fprintf(fp, ", %s = %s", name, val);
1357 }
1358
print_event_desc(struct feat_fd * ff,FILE * fp)1359 static void print_event_desc(struct feat_fd *ff, FILE *fp)
1360 {
1361 struct perf_evsel *evsel, *events;
1362 u32 j;
1363 u64 *id;
1364
1365 if (ff->events)
1366 events = ff->events;
1367 else
1368 events = read_event_desc(ff);
1369
1370 if (!events) {
1371 fprintf(fp, "# event desc: not available or unable to read\n");
1372 return;
1373 }
1374
1375 for (evsel = events; evsel->attr.size; evsel++) {
1376 fprintf(fp, "# event : name = %s, ", evsel->name);
1377
1378 if (evsel->ids) {
1379 fprintf(fp, ", id = {");
1380 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1381 if (j)
1382 fputc(',', fp);
1383 fprintf(fp, " %"PRIu64, *id);
1384 }
1385 fprintf(fp, " }");
1386 }
1387
1388 perf_event_attr__fprintf(fp, &evsel->attr, __desc_attr__fprintf, NULL);
1389
1390 fputc('\n', fp);
1391 }
1392
1393 free_event_desc(events);
1394 ff->events = NULL;
1395 }
1396
print_total_mem(struct feat_fd * ff,FILE * fp)1397 static void print_total_mem(struct feat_fd *ff, FILE *fp)
1398 {
1399 fprintf(fp, "# total memory : %llu kB\n", ff->ph->env.total_mem);
1400 }
1401
print_numa_topology(struct feat_fd * ff,FILE * fp)1402 static void print_numa_topology(struct feat_fd *ff, FILE *fp)
1403 {
1404 int i;
1405 struct numa_node *n;
1406
1407 for (i = 0; i < ff->ph->env.nr_numa_nodes; i++) {
1408 n = &ff->ph->env.numa_nodes[i];
1409
1410 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1411 " free = %"PRIu64" kB\n",
1412 n->node, n->mem_total, n->mem_free);
1413
1414 fprintf(fp, "# node%u cpu list : ", n->node);
1415 cpu_map__fprintf(n->map, fp);
1416 }
1417 }
1418
print_cpuid(struct feat_fd * ff,FILE * fp)1419 static void print_cpuid(struct feat_fd *ff, FILE *fp)
1420 {
1421 fprintf(fp, "# cpuid : %s\n", ff->ph->env.cpuid);
1422 }
1423
print_branch_stack(struct feat_fd * ff __maybe_unused,FILE * fp)1424 static void print_branch_stack(struct feat_fd *ff __maybe_unused, FILE *fp)
1425 {
1426 fprintf(fp, "# contains samples with branch stack\n");
1427 }
1428
print_auxtrace(struct feat_fd * ff __maybe_unused,FILE * fp)1429 static void print_auxtrace(struct feat_fd *ff __maybe_unused, FILE *fp)
1430 {
1431 fprintf(fp, "# contains AUX area data (e.g. instruction trace)\n");
1432 }
1433
print_stat(struct feat_fd * ff __maybe_unused,FILE * fp)1434 static void print_stat(struct feat_fd *ff __maybe_unused, FILE *fp)
1435 {
1436 fprintf(fp, "# contains stat data\n");
1437 }
1438
print_cache(struct feat_fd * ff,FILE * fp __maybe_unused)1439 static void print_cache(struct feat_fd *ff, FILE *fp __maybe_unused)
1440 {
1441 int i;
1442
1443 fprintf(fp, "# CPU cache info:\n");
1444 for (i = 0; i < ff->ph->env.caches_cnt; i++) {
1445 fprintf(fp, "# ");
1446 cpu_cache_level__fprintf(fp, &ff->ph->env.caches[i]);
1447 }
1448 }
1449
print_pmu_mappings(struct feat_fd * ff,FILE * fp)1450 static void print_pmu_mappings(struct feat_fd *ff, FILE *fp)
1451 {
1452 const char *delimiter = "# pmu mappings: ";
1453 char *str, *tmp;
1454 u32 pmu_num;
1455 u32 type;
1456
1457 pmu_num = ff->ph->env.nr_pmu_mappings;
1458 if (!pmu_num) {
1459 fprintf(fp, "# pmu mappings: not available\n");
1460 return;
1461 }
1462
1463 str = ff->ph->env.pmu_mappings;
1464
1465 while (pmu_num) {
1466 type = strtoul(str, &tmp, 0);
1467 if (*tmp != ':')
1468 goto error;
1469
1470 str = tmp + 1;
1471 fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type);
1472
1473 delimiter = ", ";
1474 str += strlen(str) + 1;
1475 pmu_num--;
1476 }
1477
1478 fprintf(fp, "\n");
1479
1480 if (!pmu_num)
1481 return;
1482 error:
1483 fprintf(fp, "# pmu mappings: unable to read\n");
1484 }
1485
print_group_desc(struct feat_fd * ff,FILE * fp)1486 static void print_group_desc(struct feat_fd *ff, FILE *fp)
1487 {
1488 struct perf_session *session;
1489 struct perf_evsel *evsel;
1490 u32 nr = 0;
1491
1492 session = container_of(ff->ph, struct perf_session, header);
1493
1494 evlist__for_each_entry(session->evlist, evsel) {
1495 if (perf_evsel__is_group_leader(evsel) &&
1496 evsel->nr_members > 1) {
1497 fprintf(fp, "# group: %s{%s", evsel->group_name ?: "",
1498 perf_evsel__name(evsel));
1499
1500 nr = evsel->nr_members - 1;
1501 } else if (nr) {
1502 fprintf(fp, ",%s", perf_evsel__name(evsel));
1503
1504 if (--nr == 0)
1505 fprintf(fp, "}\n");
1506 }
1507 }
1508 }
1509
__event_process_build_id(struct build_id_event * bev,char * filename,struct perf_session * session)1510 static int __event_process_build_id(struct build_id_event *bev,
1511 char *filename,
1512 struct perf_session *session)
1513 {
1514 int err = -1;
1515 struct machine *machine;
1516 u16 cpumode;
1517 struct dso *dso;
1518 enum dso_kernel_type dso_type;
1519
1520 machine = perf_session__findnew_machine(session, bev->pid);
1521 if (!machine)
1522 goto out;
1523
1524 cpumode = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1525
1526 switch (cpumode) {
1527 case PERF_RECORD_MISC_KERNEL:
1528 dso_type = DSO_TYPE_KERNEL;
1529 break;
1530 case PERF_RECORD_MISC_GUEST_KERNEL:
1531 dso_type = DSO_TYPE_GUEST_KERNEL;
1532 break;
1533 case PERF_RECORD_MISC_USER:
1534 case PERF_RECORD_MISC_GUEST_USER:
1535 dso_type = DSO_TYPE_USER;
1536 break;
1537 default:
1538 goto out;
1539 }
1540
1541 dso = machine__findnew_dso(machine, filename);
1542 if (dso != NULL) {
1543 char sbuild_id[SBUILD_ID_SIZE];
1544
1545 dso__set_build_id(dso, &bev->build_id);
1546
1547 if (dso_type != DSO_TYPE_USER) {
1548 struct kmod_path m = { .name = NULL, };
1549
1550 if (!kmod_path__parse_name(&m, filename) && m.kmod)
1551 dso__set_module_info(dso, &m, machine);
1552 else
1553 dso->kernel = dso_type;
1554
1555 free(m.name);
1556 }
1557
1558 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1559 sbuild_id);
1560 pr_debug("build id event received for %s: %s\n",
1561 dso->long_name, sbuild_id);
1562 dso__put(dso);
1563 }
1564
1565 err = 0;
1566 out:
1567 return err;
1568 }
1569
perf_header__read_build_ids_abi_quirk(struct perf_header * header,int input,u64 offset,u64 size)1570 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1571 int input, u64 offset, u64 size)
1572 {
1573 struct perf_session *session = container_of(header, struct perf_session, header);
1574 struct {
1575 struct perf_event_header header;
1576 u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1577 char filename[0];
1578 } old_bev;
1579 struct build_id_event bev;
1580 char filename[PATH_MAX];
1581 u64 limit = offset + size;
1582
1583 while (offset < limit) {
1584 ssize_t len;
1585
1586 if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1587 return -1;
1588
1589 if (header->needs_swap)
1590 perf_event_header__bswap(&old_bev.header);
1591
1592 len = old_bev.header.size - sizeof(old_bev);
1593 if (readn(input, filename, len) != len)
1594 return -1;
1595
1596 bev.header = old_bev.header;
1597
1598 /*
1599 * As the pid is the missing value, we need to fill
1600 * it properly. The header.misc value give us nice hint.
1601 */
1602 bev.pid = HOST_KERNEL_ID;
1603 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1604 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1605 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1606
1607 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1608 __event_process_build_id(&bev, filename, session);
1609
1610 offset += bev.header.size;
1611 }
1612
1613 return 0;
1614 }
1615
perf_header__read_build_ids(struct perf_header * header,int input,u64 offset,u64 size)1616 static int perf_header__read_build_ids(struct perf_header *header,
1617 int input, u64 offset, u64 size)
1618 {
1619 struct perf_session *session = container_of(header, struct perf_session, header);
1620 struct build_id_event bev;
1621 char filename[PATH_MAX];
1622 u64 limit = offset + size, orig_offset = offset;
1623 int err = -1;
1624
1625 while (offset < limit) {
1626 ssize_t len;
1627
1628 if (readn(input, &bev, sizeof(bev)) != sizeof(bev))
1629 goto out;
1630
1631 if (header->needs_swap)
1632 perf_event_header__bswap(&bev.header);
1633
1634 len = bev.header.size - sizeof(bev);
1635 if (readn(input, filename, len) != len)
1636 goto out;
1637 /*
1638 * The a1645ce1 changeset:
1639 *
1640 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1641 *
1642 * Added a field to struct build_id_event that broke the file
1643 * format.
1644 *
1645 * Since the kernel build-id is the first entry, process the
1646 * table using the old format if the well known
1647 * '[kernel.kallsyms]' string for the kernel build-id has the
1648 * first 4 characters chopped off (where the pid_t sits).
1649 */
1650 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1651 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1652 return -1;
1653 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1654 }
1655
1656 __event_process_build_id(&bev, filename, session);
1657
1658 offset += bev.header.size;
1659 }
1660 err = 0;
1661 out:
1662 return err;
1663 }
1664
1665 /* Macro for features that simply need to read and store a string. */
1666 #define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
1667 static int process_##__feat(struct feat_fd *ff, void *data __maybe_unused) \
1668 {\
1669 ff->ph->env.__feat_env = do_read_string(ff); \
1670 return ff->ph->env.__feat_env ? 0 : -ENOMEM; \
1671 }
1672
1673 FEAT_PROCESS_STR_FUN(hostname, hostname);
1674 FEAT_PROCESS_STR_FUN(osrelease, os_release);
1675 FEAT_PROCESS_STR_FUN(version, version);
1676 FEAT_PROCESS_STR_FUN(arch, arch);
1677 FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
1678 FEAT_PROCESS_STR_FUN(cpuid, cpuid);
1679
process_tracing_data(struct feat_fd * ff,void * data)1680 static int process_tracing_data(struct feat_fd *ff, void *data)
1681 {
1682 ssize_t ret = trace_report(ff->fd, data, false);
1683
1684 return ret < 0 ? -1 : 0;
1685 }
1686
process_build_id(struct feat_fd * ff,void * data __maybe_unused)1687 static int process_build_id(struct feat_fd *ff, void *data __maybe_unused)
1688 {
1689 if (perf_header__read_build_ids(ff->ph, ff->fd, ff->offset, ff->size))
1690 pr_debug("Failed to read buildids, continuing...\n");
1691 return 0;
1692 }
1693
process_nrcpus(struct feat_fd * ff,void * data __maybe_unused)1694 static int process_nrcpus(struct feat_fd *ff, void *data __maybe_unused)
1695 {
1696 int ret;
1697 u32 nr_cpus_avail, nr_cpus_online;
1698
1699 ret = do_read_u32(ff, &nr_cpus_avail);
1700 if (ret)
1701 return ret;
1702
1703 ret = do_read_u32(ff, &nr_cpus_online);
1704 if (ret)
1705 return ret;
1706 ff->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
1707 ff->ph->env.nr_cpus_online = (int)nr_cpus_online;
1708 return 0;
1709 }
1710
process_total_mem(struct feat_fd * ff,void * data __maybe_unused)1711 static int process_total_mem(struct feat_fd *ff, void *data __maybe_unused)
1712 {
1713 u64 total_mem;
1714 int ret;
1715
1716 ret = do_read_u64(ff, &total_mem);
1717 if (ret)
1718 return -1;
1719 ff->ph->env.total_mem = (unsigned long long)total_mem;
1720 return 0;
1721 }
1722
1723 static struct perf_evsel *
perf_evlist__find_by_index(struct perf_evlist * evlist,int idx)1724 perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1725 {
1726 struct perf_evsel *evsel;
1727
1728 evlist__for_each_entry(evlist, evsel) {
1729 if (evsel->idx == idx)
1730 return evsel;
1731 }
1732
1733 return NULL;
1734 }
1735
1736 static void
perf_evlist__set_event_name(struct perf_evlist * evlist,struct perf_evsel * event)1737 perf_evlist__set_event_name(struct perf_evlist *evlist,
1738 struct perf_evsel *event)
1739 {
1740 struct perf_evsel *evsel;
1741
1742 if (!event->name)
1743 return;
1744
1745 evsel = perf_evlist__find_by_index(evlist, event->idx);
1746 if (!evsel)
1747 return;
1748
1749 if (evsel->name)
1750 return;
1751
1752 evsel->name = strdup(event->name);
1753 }
1754
1755 static int
process_event_desc(struct feat_fd * ff,void * data __maybe_unused)1756 process_event_desc(struct feat_fd *ff, void *data __maybe_unused)
1757 {
1758 struct perf_session *session;
1759 struct perf_evsel *evsel, *events = read_event_desc(ff);
1760
1761 if (!events)
1762 return 0;
1763
1764 session = container_of(ff->ph, struct perf_session, header);
1765
1766 if (session->file->is_pipe) {
1767 /* Save events for reading later by print_event_desc,
1768 * since they can't be read again in pipe mode. */
1769 ff->events = events;
1770 }
1771
1772 for (evsel = events; evsel->attr.size; evsel++)
1773 perf_evlist__set_event_name(session->evlist, evsel);
1774
1775 if (!session->file->is_pipe)
1776 free_event_desc(events);
1777
1778 return 0;
1779 }
1780
process_cmdline(struct feat_fd * ff,void * data __maybe_unused)1781 static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)
1782 {
1783 char *str, *cmdline = NULL, **argv = NULL;
1784 u32 nr, i, len = 0;
1785
1786 if (do_read_u32(ff, &nr))
1787 return -1;
1788
1789 ff->ph->env.nr_cmdline = nr;
1790
1791 cmdline = zalloc(ff->size + nr + 1);
1792 if (!cmdline)
1793 return -1;
1794
1795 argv = zalloc(sizeof(char *) * (nr + 1));
1796 if (!argv)
1797 goto error;
1798
1799 for (i = 0; i < nr; i++) {
1800 str = do_read_string(ff);
1801 if (!str)
1802 goto error;
1803
1804 argv[i] = cmdline + len;
1805 memcpy(argv[i], str, strlen(str) + 1);
1806 len += strlen(str) + 1;
1807 free(str);
1808 }
1809 ff->ph->env.cmdline = cmdline;
1810 ff->ph->env.cmdline_argv = (const char **) argv;
1811 return 0;
1812
1813 error:
1814 free(argv);
1815 free(cmdline);
1816 return -1;
1817 }
1818
process_cpu_topology(struct feat_fd * ff,void * data __maybe_unused)1819 static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)
1820 {
1821 u32 nr, i;
1822 char *str;
1823 struct strbuf sb;
1824 int cpu_nr = ff->ph->env.nr_cpus_avail;
1825 u64 size = 0;
1826 struct perf_header *ph = ff->ph;
1827 bool do_core_id_test = true;
1828
1829 ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
1830 if (!ph->env.cpu)
1831 return -1;
1832
1833 if (do_read_u32(ff, &nr))
1834 goto free_cpu;
1835
1836 ph->env.nr_sibling_cores = nr;
1837 size += sizeof(u32);
1838 if (strbuf_init(&sb, 128) < 0)
1839 goto free_cpu;
1840
1841 for (i = 0; i < nr; i++) {
1842 str = do_read_string(ff);
1843 if (!str)
1844 goto error;
1845
1846 /* include a NULL character at the end */
1847 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1848 goto error;
1849 size += string_size(str);
1850 free(str);
1851 }
1852 ph->env.sibling_cores = strbuf_detach(&sb, NULL);
1853
1854 if (do_read_u32(ff, &nr))
1855 return -1;
1856
1857 ph->env.nr_sibling_threads = nr;
1858 size += sizeof(u32);
1859
1860 for (i = 0; i < nr; i++) {
1861 str = do_read_string(ff);
1862 if (!str)
1863 goto error;
1864
1865 /* include a NULL character at the end */
1866 if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
1867 goto error;
1868 size += string_size(str);
1869 free(str);
1870 }
1871 ph->env.sibling_threads = strbuf_detach(&sb, NULL);
1872
1873 /*
1874 * The header may be from old perf,
1875 * which doesn't include core id and socket id information.
1876 */
1877 if (ff->size <= size) {
1878 zfree(&ph->env.cpu);
1879 return 0;
1880 }
1881
1882 /* On s390 the socket_id number is not related to the numbers of cpus.
1883 * The socket_id number might be higher than the numbers of cpus.
1884 * This depends on the configuration.
1885 * AArch64 is the same.
1886 */
1887 if (ph->env.arch && (!strncmp(ph->env.arch, "s390", 4)
1888 || !strncmp(ph->env.arch, "aarch64", 7)))
1889 do_core_id_test = false;
1890
1891 for (i = 0; i < (u32)cpu_nr; i++) {
1892 if (do_read_u32(ff, &nr))
1893 goto free_cpu;
1894
1895 ph->env.cpu[i].core_id = nr;
1896
1897 if (do_read_u32(ff, &nr))
1898 goto free_cpu;
1899
1900 if (do_core_id_test && nr != (u32)-1 && nr > (u32)cpu_nr) {
1901 pr_debug("socket_id number is too big."
1902 "You may need to upgrade the perf tool.\n");
1903 goto free_cpu;
1904 }
1905
1906 ph->env.cpu[i].socket_id = nr;
1907 }
1908
1909 return 0;
1910
1911 error:
1912 strbuf_release(&sb);
1913 free_cpu:
1914 zfree(&ph->env.cpu);
1915 return -1;
1916 }
1917
process_numa_topology(struct feat_fd * ff,void * data __maybe_unused)1918 static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused)
1919 {
1920 struct numa_node *nodes, *n;
1921 u32 nr, i;
1922 char *str;
1923
1924 /* nr nodes */
1925 if (do_read_u32(ff, &nr))
1926 return -1;
1927
1928 nodes = zalloc(sizeof(*nodes) * nr);
1929 if (!nodes)
1930 return -ENOMEM;
1931
1932 for (i = 0; i < nr; i++) {
1933 n = &nodes[i];
1934
1935 /* node number */
1936 if (do_read_u32(ff, &n->node))
1937 goto error;
1938
1939 if (do_read_u64(ff, &n->mem_total))
1940 goto error;
1941
1942 if (do_read_u64(ff, &n->mem_free))
1943 goto error;
1944
1945 str = do_read_string(ff);
1946 if (!str)
1947 goto error;
1948
1949 n->map = cpu_map__new(str);
1950 if (!n->map)
1951 goto error;
1952
1953 free(str);
1954 }
1955 ff->ph->env.nr_numa_nodes = nr;
1956 ff->ph->env.numa_nodes = nodes;
1957 return 0;
1958
1959 error:
1960 free(nodes);
1961 return -1;
1962 }
1963
process_pmu_mappings(struct feat_fd * ff,void * data __maybe_unused)1964 static int process_pmu_mappings(struct feat_fd *ff, void *data __maybe_unused)
1965 {
1966 char *name;
1967 u32 pmu_num;
1968 u32 type;
1969 struct strbuf sb;
1970
1971 if (do_read_u32(ff, &pmu_num))
1972 return -1;
1973
1974 if (!pmu_num) {
1975 pr_debug("pmu mappings not available\n");
1976 return 0;
1977 }
1978
1979 ff->ph->env.nr_pmu_mappings = pmu_num;
1980 if (strbuf_init(&sb, 128) < 0)
1981 return -1;
1982
1983 while (pmu_num) {
1984 if (do_read_u32(ff, &type))
1985 goto error;
1986
1987 name = do_read_string(ff);
1988 if (!name)
1989 goto error;
1990
1991 if (strbuf_addf(&sb, "%u:%s", type, name) < 0)
1992 goto error;
1993 /* include a NULL character at the end */
1994 if (strbuf_add(&sb, "", 1) < 0)
1995 goto error;
1996
1997 if (!strcmp(name, "msr"))
1998 ff->ph->env.msr_pmu_type = type;
1999
2000 free(name);
2001 pmu_num--;
2002 }
2003 ff->ph->env.pmu_mappings = strbuf_detach(&sb, NULL);
2004 return 0;
2005
2006 error:
2007 strbuf_release(&sb);
2008 return -1;
2009 }
2010
process_group_desc(struct feat_fd * ff,void * data __maybe_unused)2011 static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
2012 {
2013 size_t ret = -1;
2014 u32 i, nr, nr_groups;
2015 struct perf_session *session;
2016 struct perf_evsel *evsel, *leader = NULL;
2017 struct group_desc {
2018 char *name;
2019 u32 leader_idx;
2020 u32 nr_members;
2021 } *desc;
2022
2023 if (do_read_u32(ff, &nr_groups))
2024 return -1;
2025
2026 ff->ph->env.nr_groups = nr_groups;
2027 if (!nr_groups) {
2028 pr_debug("group desc not available\n");
2029 return 0;
2030 }
2031
2032 desc = calloc(nr_groups, sizeof(*desc));
2033 if (!desc)
2034 return -1;
2035
2036 for (i = 0; i < nr_groups; i++) {
2037 desc[i].name = do_read_string(ff);
2038 if (!desc[i].name)
2039 goto out_free;
2040
2041 if (do_read_u32(ff, &desc[i].leader_idx))
2042 goto out_free;
2043
2044 if (do_read_u32(ff, &desc[i].nr_members))
2045 goto out_free;
2046 }
2047
2048 /*
2049 * Rebuild group relationship based on the group_desc
2050 */
2051 session = container_of(ff->ph, struct perf_session, header);
2052 session->evlist->nr_groups = nr_groups;
2053
2054 i = nr = 0;
2055 evlist__for_each_entry(session->evlist, evsel) {
2056 if (evsel->idx == (int) desc[i].leader_idx) {
2057 evsel->leader = evsel;
2058 /* {anon_group} is a dummy name */
2059 if (strcmp(desc[i].name, "{anon_group}")) {
2060 evsel->group_name = desc[i].name;
2061 desc[i].name = NULL;
2062 }
2063 evsel->nr_members = desc[i].nr_members;
2064
2065 if (i >= nr_groups || nr > 0) {
2066 pr_debug("invalid group desc\n");
2067 goto out_free;
2068 }
2069
2070 leader = evsel;
2071 nr = evsel->nr_members - 1;
2072 i++;
2073 } else if (nr) {
2074 /* This is a group member */
2075 evsel->leader = leader;
2076
2077 nr--;
2078 }
2079 }
2080
2081 if (i != nr_groups || nr != 0) {
2082 pr_debug("invalid group desc\n");
2083 goto out_free;
2084 }
2085
2086 ret = 0;
2087 out_free:
2088 for (i = 0; i < nr_groups; i++)
2089 zfree(&desc[i].name);
2090 free(desc);
2091
2092 return ret;
2093 }
2094
process_auxtrace(struct feat_fd * ff,void * data __maybe_unused)2095 static int process_auxtrace(struct feat_fd *ff, void *data __maybe_unused)
2096 {
2097 struct perf_session *session;
2098 int err;
2099
2100 session = container_of(ff->ph, struct perf_session, header);
2101
2102 err = auxtrace_index__process(ff->fd, ff->size, session,
2103 ff->ph->needs_swap);
2104 if (err < 0)
2105 pr_err("Failed to process auxtrace index\n");
2106 return err;
2107 }
2108
process_cache(struct feat_fd * ff,void * data __maybe_unused)2109 static int process_cache(struct feat_fd *ff, void *data __maybe_unused)
2110 {
2111 struct cpu_cache_level *caches;
2112 u32 cnt, i, version;
2113
2114 if (do_read_u32(ff, &version))
2115 return -1;
2116
2117 if (version != 1)
2118 return -1;
2119
2120 if (do_read_u32(ff, &cnt))
2121 return -1;
2122
2123 caches = zalloc(sizeof(*caches) * cnt);
2124 if (!caches)
2125 return -1;
2126
2127 for (i = 0; i < cnt; i++) {
2128 struct cpu_cache_level c;
2129
2130 #define _R(v) \
2131 if (do_read_u32(ff, &c.v))\
2132 goto out_free_caches; \
2133
2134 _R(level)
2135 _R(line_size)
2136 _R(sets)
2137 _R(ways)
2138 #undef _R
2139
2140 #define _R(v) \
2141 c.v = do_read_string(ff); \
2142 if (!c.v) \
2143 goto out_free_caches;
2144
2145 _R(type)
2146 _R(size)
2147 _R(map)
2148 #undef _R
2149
2150 caches[i] = c;
2151 }
2152
2153 ff->ph->env.caches = caches;
2154 ff->ph->env.caches_cnt = cnt;
2155 return 0;
2156 out_free_caches:
2157 free(caches);
2158 return -1;
2159 }
2160
2161 struct feature_ops {
2162 int (*write)(struct feat_fd *ff, struct perf_evlist *evlist);
2163 void (*print)(struct feat_fd *ff, FILE *fp);
2164 int (*process)(struct feat_fd *ff, void *data);
2165 const char *name;
2166 bool full_only;
2167 bool synthesize;
2168 };
2169
2170 #define FEAT_OPR(n, func, __full_only) \
2171 [HEADER_##n] = { \
2172 .name = __stringify(n), \
2173 .write = write_##func, \
2174 .print = print_##func, \
2175 .full_only = __full_only, \
2176 .process = process_##func, \
2177 .synthesize = true \
2178 }
2179
2180 #define FEAT_OPN(n, func, __full_only) \
2181 [HEADER_##n] = { \
2182 .name = __stringify(n), \
2183 .write = write_##func, \
2184 .print = print_##func, \
2185 .full_only = __full_only, \
2186 .process = process_##func \
2187 }
2188
2189 /* feature_ops not implemented: */
2190 #define print_tracing_data NULL
2191 #define print_build_id NULL
2192
2193 #define process_branch_stack NULL
2194 #define process_stat NULL
2195
2196
2197 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
2198 FEAT_OPN(TRACING_DATA, tracing_data, false),
2199 FEAT_OPN(BUILD_ID, build_id, false),
2200 FEAT_OPR(HOSTNAME, hostname, false),
2201 FEAT_OPR(OSRELEASE, osrelease, false),
2202 FEAT_OPR(VERSION, version, false),
2203 FEAT_OPR(ARCH, arch, false),
2204 FEAT_OPR(NRCPUS, nrcpus, false),
2205 FEAT_OPR(CPUDESC, cpudesc, false),
2206 FEAT_OPR(CPUID, cpuid, false),
2207 FEAT_OPR(TOTAL_MEM, total_mem, false),
2208 FEAT_OPR(EVENT_DESC, event_desc, false),
2209 FEAT_OPR(CMDLINE, cmdline, false),
2210 FEAT_OPR(CPU_TOPOLOGY, cpu_topology, true),
2211 FEAT_OPR(NUMA_TOPOLOGY, numa_topology, true),
2212 FEAT_OPN(BRANCH_STACK, branch_stack, false),
2213 FEAT_OPR(PMU_MAPPINGS, pmu_mappings, false),
2214 FEAT_OPR(GROUP_DESC, group_desc, false),
2215 FEAT_OPN(AUXTRACE, auxtrace, false),
2216 FEAT_OPN(STAT, stat, false),
2217 FEAT_OPN(CACHE, cache, true),
2218 };
2219
2220 struct header_print_data {
2221 FILE *fp;
2222 bool full; /* extended list of headers */
2223 };
2224
perf_file_section__fprintf_info(struct perf_file_section * section,struct perf_header * ph,int feat,int fd,void * data)2225 static int perf_file_section__fprintf_info(struct perf_file_section *section,
2226 struct perf_header *ph,
2227 int feat, int fd, void *data)
2228 {
2229 struct header_print_data *hd = data;
2230 struct feat_fd ff;
2231
2232 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2233 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2234 "%d, continuing...\n", section->offset, feat);
2235 return 0;
2236 }
2237 if (feat >= HEADER_LAST_FEATURE) {
2238 pr_warning("unknown feature %d\n", feat);
2239 return 0;
2240 }
2241 if (!feat_ops[feat].print)
2242 return 0;
2243
2244 ff = (struct feat_fd) {
2245 .fd = fd,
2246 .ph = ph,
2247 };
2248
2249 if (!feat_ops[feat].full_only || hd->full)
2250 feat_ops[feat].print(&ff, hd->fp);
2251 else
2252 fprintf(hd->fp, "# %s info available, use -I to display\n",
2253 feat_ops[feat].name);
2254
2255 return 0;
2256 }
2257
perf_header__fprintf_info(struct perf_session * session,FILE * fp,bool full)2258 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
2259 {
2260 struct header_print_data hd;
2261 struct perf_header *header = &session->header;
2262 int fd = perf_data_file__fd(session->file);
2263 struct stat st;
2264 int ret, bit;
2265
2266 hd.fp = fp;
2267 hd.full = full;
2268
2269 ret = fstat(fd, &st);
2270 if (ret == -1)
2271 return -1;
2272
2273 fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
2274
2275 perf_header__process_sections(header, fd, &hd,
2276 perf_file_section__fprintf_info);
2277
2278 if (session->file->is_pipe)
2279 return 0;
2280
2281 fprintf(fp, "# missing features: ");
2282 for_each_clear_bit(bit, header->adds_features, HEADER_LAST_FEATURE) {
2283 if (bit)
2284 fprintf(fp, "%s ", feat_ops[bit].name);
2285 }
2286
2287 fprintf(fp, "\n");
2288 return 0;
2289 }
2290
do_write_feat(struct feat_fd * ff,int type,struct perf_file_section ** p,struct perf_evlist * evlist)2291 static int do_write_feat(struct feat_fd *ff, int type,
2292 struct perf_file_section **p,
2293 struct perf_evlist *evlist)
2294 {
2295 int err;
2296 int ret = 0;
2297
2298 if (perf_header__has_feat(ff->ph, type)) {
2299 if (!feat_ops[type].write)
2300 return -1;
2301
2302 if (WARN(ff->buf, "Error: calling %s in pipe-mode.\n", __func__))
2303 return -1;
2304
2305 (*p)->offset = lseek(ff->fd, 0, SEEK_CUR);
2306
2307 err = feat_ops[type].write(ff, evlist);
2308 if (err < 0) {
2309 pr_debug("failed to write feature %s\n", feat_ops[type].name);
2310
2311 /* undo anything written */
2312 lseek(ff->fd, (*p)->offset, SEEK_SET);
2313
2314 return -1;
2315 }
2316 (*p)->size = lseek(ff->fd, 0, SEEK_CUR) - (*p)->offset;
2317 (*p)++;
2318 }
2319 return ret;
2320 }
2321
perf_header__adds_write(struct perf_header * header,struct perf_evlist * evlist,int fd)2322 static int perf_header__adds_write(struct perf_header *header,
2323 struct perf_evlist *evlist, int fd)
2324 {
2325 int nr_sections;
2326 struct feat_fd ff;
2327 struct perf_file_section *feat_sec, *p;
2328 int sec_size;
2329 u64 sec_start;
2330 int feat;
2331 int err;
2332
2333 ff = (struct feat_fd){
2334 .fd = fd,
2335 .ph = header,
2336 };
2337
2338 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2339 if (!nr_sections)
2340 return 0;
2341
2342 feat_sec = p = calloc(nr_sections, sizeof(*feat_sec));
2343 if (feat_sec == NULL)
2344 return -ENOMEM;
2345
2346 sec_size = sizeof(*feat_sec) * nr_sections;
2347
2348 sec_start = header->feat_offset;
2349 lseek(fd, sec_start + sec_size, SEEK_SET);
2350
2351 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
2352 if (do_write_feat(&ff, feat, &p, evlist))
2353 perf_header__clear_feat(header, feat);
2354 }
2355
2356 lseek(fd, sec_start, SEEK_SET);
2357 /*
2358 * may write more than needed due to dropped feature, but
2359 * this is okay, reader will skip the mising entries
2360 */
2361 err = do_write(&ff, feat_sec, sec_size);
2362 if (err < 0)
2363 pr_debug("failed to write feature section\n");
2364 free(feat_sec);
2365 return err;
2366 }
2367
perf_header__write_pipe(int fd)2368 int perf_header__write_pipe(int fd)
2369 {
2370 struct perf_pipe_file_header f_header;
2371 struct feat_fd ff;
2372 int err;
2373
2374 ff = (struct feat_fd){ .fd = fd };
2375
2376 f_header = (struct perf_pipe_file_header){
2377 .magic = PERF_MAGIC,
2378 .size = sizeof(f_header),
2379 };
2380
2381 err = do_write(&ff, &f_header, sizeof(f_header));
2382 if (err < 0) {
2383 pr_debug("failed to write perf pipe header\n");
2384 return err;
2385 }
2386
2387 return 0;
2388 }
2389
perf_session__write_header(struct perf_session * session,struct perf_evlist * evlist,int fd,bool at_exit)2390 int perf_session__write_header(struct perf_session *session,
2391 struct perf_evlist *evlist,
2392 int fd, bool at_exit)
2393 {
2394 struct perf_file_header f_header;
2395 struct perf_file_attr f_attr;
2396 struct perf_header *header = &session->header;
2397 struct perf_evsel *evsel;
2398 struct feat_fd ff;
2399 u64 attr_offset;
2400 int err;
2401
2402 ff = (struct feat_fd){ .fd = fd};
2403 lseek(fd, sizeof(f_header), SEEK_SET);
2404
2405 evlist__for_each_entry(session->evlist, evsel) {
2406 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
2407 err = do_write(&ff, evsel->id, evsel->ids * sizeof(u64));
2408 if (err < 0) {
2409 pr_debug("failed to write perf header\n");
2410 return err;
2411 }
2412 }
2413
2414 attr_offset = lseek(ff.fd, 0, SEEK_CUR);
2415
2416 evlist__for_each_entry(evlist, evsel) {
2417 f_attr = (struct perf_file_attr){
2418 .attr = evsel->attr,
2419 .ids = {
2420 .offset = evsel->id_offset,
2421 .size = evsel->ids * sizeof(u64),
2422 }
2423 };
2424 err = do_write(&ff, &f_attr, sizeof(f_attr));
2425 if (err < 0) {
2426 pr_debug("failed to write perf header attribute\n");
2427 return err;
2428 }
2429 }
2430
2431 if (!header->data_offset)
2432 header->data_offset = lseek(fd, 0, SEEK_CUR);
2433 header->feat_offset = header->data_offset + header->data_size;
2434
2435 if (at_exit) {
2436 err = perf_header__adds_write(header, evlist, fd);
2437 if (err < 0)
2438 return err;
2439 }
2440
2441 f_header = (struct perf_file_header){
2442 .magic = PERF_MAGIC,
2443 .size = sizeof(f_header),
2444 .attr_size = sizeof(f_attr),
2445 .attrs = {
2446 .offset = attr_offset,
2447 .size = evlist->nr_entries * sizeof(f_attr),
2448 },
2449 .data = {
2450 .offset = header->data_offset,
2451 .size = header->data_size,
2452 },
2453 /* event_types is ignored, store zeros */
2454 };
2455
2456 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
2457
2458 lseek(fd, 0, SEEK_SET);
2459 err = do_write(&ff, &f_header, sizeof(f_header));
2460 if (err < 0) {
2461 pr_debug("failed to write perf header\n");
2462 return err;
2463 }
2464 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
2465
2466 return 0;
2467 }
2468
perf_header__getbuffer64(struct perf_header * header,int fd,void * buf,size_t size)2469 static int perf_header__getbuffer64(struct perf_header *header,
2470 int fd, void *buf, size_t size)
2471 {
2472 if (readn(fd, buf, size) <= 0)
2473 return -1;
2474
2475 if (header->needs_swap)
2476 mem_bswap_64(buf, size);
2477
2478 return 0;
2479 }
2480
perf_header__process_sections(struct perf_header * header,int fd,void * data,int (* process)(struct perf_file_section * section,struct perf_header * ph,int feat,int fd,void * data))2481 int perf_header__process_sections(struct perf_header *header, int fd,
2482 void *data,
2483 int (*process)(struct perf_file_section *section,
2484 struct perf_header *ph,
2485 int feat, int fd, void *data))
2486 {
2487 struct perf_file_section *feat_sec, *sec;
2488 int nr_sections;
2489 int sec_size;
2490 int feat;
2491 int err;
2492
2493 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
2494 if (!nr_sections)
2495 return 0;
2496
2497 feat_sec = sec = calloc(nr_sections, sizeof(*feat_sec));
2498 if (!feat_sec)
2499 return -1;
2500
2501 sec_size = sizeof(*feat_sec) * nr_sections;
2502
2503 lseek(fd, header->feat_offset, SEEK_SET);
2504
2505 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
2506 if (err < 0)
2507 goto out_free;
2508
2509 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
2510 err = process(sec++, header, feat, fd, data);
2511 if (err < 0)
2512 goto out_free;
2513 }
2514 err = 0;
2515 out_free:
2516 free(feat_sec);
2517 return err;
2518 }
2519
2520 static const int attr_file_abi_sizes[] = {
2521 [0] = PERF_ATTR_SIZE_VER0,
2522 [1] = PERF_ATTR_SIZE_VER1,
2523 [2] = PERF_ATTR_SIZE_VER2,
2524 [3] = PERF_ATTR_SIZE_VER3,
2525 [4] = PERF_ATTR_SIZE_VER4,
2526 0,
2527 };
2528
2529 /*
2530 * In the legacy file format, the magic number is not used to encode endianness.
2531 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2532 * on ABI revisions, we need to try all combinations for all endianness to
2533 * detect the endianness.
2534 */
try_all_file_abis(uint64_t hdr_sz,struct perf_header * ph)2535 static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2536 {
2537 uint64_t ref_size, attr_size;
2538 int i;
2539
2540 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2541 ref_size = attr_file_abi_sizes[i]
2542 + sizeof(struct perf_file_section);
2543 if (hdr_sz != ref_size) {
2544 attr_size = bswap_64(hdr_sz);
2545 if (attr_size != ref_size)
2546 continue;
2547
2548 ph->needs_swap = true;
2549 }
2550 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2551 i,
2552 ph->needs_swap);
2553 return 0;
2554 }
2555 /* could not determine endianness */
2556 return -1;
2557 }
2558
2559 #define PERF_PIPE_HDR_VER0 16
2560
2561 static const size_t attr_pipe_abi_sizes[] = {
2562 [0] = PERF_PIPE_HDR_VER0,
2563 0,
2564 };
2565
2566 /*
2567 * In the legacy pipe format, there is an implicit assumption that endiannesss
2568 * between host recording the samples, and host parsing the samples is the
2569 * same. This is not always the case given that the pipe output may always be
2570 * redirected into a file and analyzed on a different machine with possibly a
2571 * different endianness and perf_event ABI revsions in the perf tool itself.
2572 */
try_all_pipe_abis(uint64_t hdr_sz,struct perf_header * ph)2573 static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2574 {
2575 u64 attr_size;
2576 int i;
2577
2578 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2579 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2580 attr_size = bswap_64(hdr_sz);
2581 if (attr_size != hdr_sz)
2582 continue;
2583
2584 ph->needs_swap = true;
2585 }
2586 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2587 return 0;
2588 }
2589 return -1;
2590 }
2591
is_perf_magic(u64 magic)2592 bool is_perf_magic(u64 magic)
2593 {
2594 if (!memcmp(&magic, __perf_magic1, sizeof(magic))
2595 || magic == __perf_magic2
2596 || magic == __perf_magic2_sw)
2597 return true;
2598
2599 return false;
2600 }
2601
check_magic_endian(u64 magic,uint64_t hdr_sz,bool is_pipe,struct perf_header * ph)2602 static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2603 bool is_pipe, struct perf_header *ph)
2604 {
2605 int ret;
2606
2607 /* check for legacy format */
2608 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
2609 if (ret == 0) {
2610 ph->version = PERF_HEADER_VERSION_1;
2611 pr_debug("legacy perf.data format\n");
2612 if (is_pipe)
2613 return try_all_pipe_abis(hdr_sz, ph);
2614
2615 return try_all_file_abis(hdr_sz, ph);
2616 }
2617 /*
2618 * the new magic number serves two purposes:
2619 * - unique number to identify actual perf.data files
2620 * - encode endianness of file
2621 */
2622 ph->version = PERF_HEADER_VERSION_2;
2623
2624 /* check magic number with one endianness */
2625 if (magic == __perf_magic2)
2626 return 0;
2627
2628 /* check magic number with opposite endianness */
2629 if (magic != __perf_magic2_sw)
2630 return -1;
2631
2632 ph->needs_swap = true;
2633
2634 return 0;
2635 }
2636
perf_file_header__read(struct perf_file_header * header,struct perf_header * ph,int fd)2637 int perf_file_header__read(struct perf_file_header *header,
2638 struct perf_header *ph, int fd)
2639 {
2640 ssize_t ret;
2641
2642 lseek(fd, 0, SEEK_SET);
2643
2644 ret = readn(fd, header, sizeof(*header));
2645 if (ret <= 0)
2646 return -1;
2647
2648 if (check_magic_endian(header->magic,
2649 header->attr_size, false, ph) < 0) {
2650 pr_debug("magic/endian check failed\n");
2651 return -1;
2652 }
2653
2654 if (ph->needs_swap) {
2655 mem_bswap_64(header, offsetof(struct perf_file_header,
2656 adds_features));
2657 }
2658
2659 if (header->size != sizeof(*header)) {
2660 /* Support the previous format */
2661 if (header->size == offsetof(typeof(*header), adds_features))
2662 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2663 else
2664 return -1;
2665 } else if (ph->needs_swap) {
2666 /*
2667 * feature bitmap is declared as an array of unsigned longs --
2668 * not good since its size can differ between the host that
2669 * generated the data file and the host analyzing the file.
2670 *
2671 * We need to handle endianness, but we don't know the size of
2672 * the unsigned long where the file was generated. Take a best
2673 * guess at determining it: try 64-bit swap first (ie., file
2674 * created on a 64-bit host), and check if the hostname feature
2675 * bit is set (this feature bit is forced on as of fbe96f2).
2676 * If the bit is not, undo the 64-bit swap and try a 32-bit
2677 * swap. If the hostname bit is still not set (e.g., older data
2678 * file), punt and fallback to the original behavior --
2679 * clearing all feature bits and setting buildid.
2680 */
2681 mem_bswap_64(&header->adds_features,
2682 BITS_TO_U64(HEADER_FEAT_BITS));
2683
2684 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2685 /* unswap as u64 */
2686 mem_bswap_64(&header->adds_features,
2687 BITS_TO_U64(HEADER_FEAT_BITS));
2688
2689 /* unswap as u32 */
2690 mem_bswap_32(&header->adds_features,
2691 BITS_TO_U32(HEADER_FEAT_BITS));
2692 }
2693
2694 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2695 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2696 set_bit(HEADER_BUILD_ID, header->adds_features);
2697 }
2698 }
2699
2700 memcpy(&ph->adds_features, &header->adds_features,
2701 sizeof(ph->adds_features));
2702
2703 ph->data_offset = header->data.offset;
2704 ph->data_size = header->data.size;
2705 ph->feat_offset = header->data.offset + header->data.size;
2706 return 0;
2707 }
2708
perf_file_section__process(struct perf_file_section * section,struct perf_header * ph,int feat,int fd,void * data)2709 static int perf_file_section__process(struct perf_file_section *section,
2710 struct perf_header *ph,
2711 int feat, int fd, void *data)
2712 {
2713 struct feat_fd fdd = {
2714 .fd = fd,
2715 .ph = ph,
2716 .size = section->size,
2717 .offset = section->offset,
2718 };
2719
2720 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
2721 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
2722 "%d, continuing...\n", section->offset, feat);
2723 return 0;
2724 }
2725
2726 if (feat >= HEADER_LAST_FEATURE) {
2727 pr_debug("unknown feature %d, continuing...\n", feat);
2728 return 0;
2729 }
2730
2731 if (!feat_ops[feat].process)
2732 return 0;
2733
2734 return feat_ops[feat].process(&fdd, data);
2735 }
2736
perf_file_header__read_pipe(struct perf_pipe_file_header * header,struct perf_header * ph,int fd,bool repipe)2737 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
2738 struct perf_header *ph, int fd,
2739 bool repipe)
2740 {
2741 struct feat_fd ff = {
2742 .fd = STDOUT_FILENO,
2743 .ph = ph,
2744 };
2745 ssize_t ret;
2746
2747 ret = readn(fd, header, sizeof(*header));
2748 if (ret <= 0)
2749 return -1;
2750
2751 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2752 pr_debug("endian/magic failed\n");
2753 return -1;
2754 }
2755
2756 if (ph->needs_swap)
2757 header->size = bswap_64(header->size);
2758
2759 if (repipe && do_write(&ff, header, sizeof(*header)) < 0)
2760 return -1;
2761
2762 return 0;
2763 }
2764
perf_header__read_pipe(struct perf_session * session)2765 static int perf_header__read_pipe(struct perf_session *session)
2766 {
2767 struct perf_header *header = &session->header;
2768 struct perf_pipe_file_header f_header;
2769
2770 if (perf_file_header__read_pipe(&f_header, header,
2771 perf_data_file__fd(session->file),
2772 session->repipe) < 0) {
2773 pr_debug("incompatible file format\n");
2774 return -EINVAL;
2775 }
2776
2777 return 0;
2778 }
2779
read_attr(int fd,struct perf_header * ph,struct perf_file_attr * f_attr)2780 static int read_attr(int fd, struct perf_header *ph,
2781 struct perf_file_attr *f_attr)
2782 {
2783 struct perf_event_attr *attr = &f_attr->attr;
2784 size_t sz, left;
2785 size_t our_sz = sizeof(f_attr->attr);
2786 ssize_t ret;
2787
2788 memset(f_attr, 0, sizeof(*f_attr));
2789
2790 /* read minimal guaranteed structure */
2791 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2792 if (ret <= 0) {
2793 pr_debug("cannot read %d bytes of header attr\n",
2794 PERF_ATTR_SIZE_VER0);
2795 return -1;
2796 }
2797
2798 /* on file perf_event_attr size */
2799 sz = attr->size;
2800
2801 if (ph->needs_swap)
2802 sz = bswap_32(sz);
2803
2804 if (sz == 0) {
2805 /* assume ABI0 */
2806 sz = PERF_ATTR_SIZE_VER0;
2807 } else if (sz > our_sz) {
2808 pr_debug("file uses a more recent and unsupported ABI"
2809 " (%zu bytes extra)\n", sz - our_sz);
2810 return -1;
2811 }
2812 /* what we have not yet read and that we know about */
2813 left = sz - PERF_ATTR_SIZE_VER0;
2814 if (left) {
2815 void *ptr = attr;
2816 ptr += PERF_ATTR_SIZE_VER0;
2817
2818 ret = readn(fd, ptr, left);
2819 }
2820 /* read perf_file_section, ids are read in caller */
2821 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2822
2823 return ret <= 0 ? -1 : 0;
2824 }
2825
perf_evsel__prepare_tracepoint_event(struct perf_evsel * evsel,struct pevent * pevent)2826 static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel,
2827 struct pevent *pevent)
2828 {
2829 struct event_format *event;
2830 char bf[128];
2831
2832 /* already prepared */
2833 if (evsel->tp_format)
2834 return 0;
2835
2836 if (pevent == NULL) {
2837 pr_debug("broken or missing trace data\n");
2838 return -1;
2839 }
2840
2841 event = pevent_find_event(pevent, evsel->attr.config);
2842 if (event == NULL) {
2843 pr_debug("cannot find event format for %d\n", (int)evsel->attr.config);
2844 return -1;
2845 }
2846
2847 if (!evsel->name) {
2848 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2849 evsel->name = strdup(bf);
2850 if (evsel->name == NULL)
2851 return -1;
2852 }
2853
2854 evsel->tp_format = event;
2855 return 0;
2856 }
2857
perf_evlist__prepare_tracepoint_events(struct perf_evlist * evlist,struct pevent * pevent)2858 static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist,
2859 struct pevent *pevent)
2860 {
2861 struct perf_evsel *pos;
2862
2863 evlist__for_each_entry(evlist, pos) {
2864 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
2865 perf_evsel__prepare_tracepoint_event(pos, pevent))
2866 return -1;
2867 }
2868
2869 return 0;
2870 }
2871
perf_session__read_header(struct perf_session * session)2872 int perf_session__read_header(struct perf_session *session)
2873 {
2874 struct perf_data_file *file = session->file;
2875 struct perf_header *header = &session->header;
2876 struct perf_file_header f_header;
2877 struct perf_file_attr f_attr;
2878 u64 f_id;
2879 int nr_attrs, nr_ids, i, j;
2880 int fd = perf_data_file__fd(file);
2881
2882 session->evlist = perf_evlist__new();
2883 if (session->evlist == NULL)
2884 return -ENOMEM;
2885
2886 session->evlist->env = &header->env;
2887 session->machines.host.env = &header->env;
2888 if (perf_data_file__is_pipe(file))
2889 return perf_header__read_pipe(session);
2890
2891 if (perf_file_header__read(&f_header, header, fd) < 0)
2892 return -EINVAL;
2893
2894 /*
2895 * Sanity check that perf.data was written cleanly; data size is
2896 * initialized to 0 and updated only if the on_exit function is run.
2897 * If data size is still 0 then the file contains only partial
2898 * information. Just warn user and process it as much as it can.
2899 */
2900 if (f_header.data.size == 0) {
2901 pr_warning("WARNING: The %s file's data size field is 0 which is unexpected.\n"
2902 "Was the 'perf record' command properly terminated?\n",
2903 file->path);
2904 }
2905
2906 if (f_header.attr_size == 0) {
2907 pr_err("ERROR: The %s file's attr size field is 0 which is unexpected.\n"
2908 "Was the 'perf record' command properly terminated?\n",
2909 file->path);
2910 return -EINVAL;
2911 }
2912
2913 nr_attrs = f_header.attrs.size / f_header.attr_size;
2914 lseek(fd, f_header.attrs.offset, SEEK_SET);
2915
2916 for (i = 0; i < nr_attrs; i++) {
2917 struct perf_evsel *evsel;
2918 off_t tmp;
2919
2920 if (read_attr(fd, header, &f_attr) < 0)
2921 goto out_errno;
2922
2923 if (header->needs_swap) {
2924 f_attr.ids.size = bswap_64(f_attr.ids.size);
2925 f_attr.ids.offset = bswap_64(f_attr.ids.offset);
2926 perf_event__attr_swap(&f_attr.attr);
2927 }
2928
2929 tmp = lseek(fd, 0, SEEK_CUR);
2930 evsel = perf_evsel__new(&f_attr.attr);
2931
2932 if (evsel == NULL)
2933 goto out_delete_evlist;
2934
2935 evsel->needs_swap = header->needs_swap;
2936 /*
2937 * Do it before so that if perf_evsel__alloc_id fails, this
2938 * entry gets purged too at perf_evlist__delete().
2939 */
2940 perf_evlist__add(session->evlist, evsel);
2941
2942 nr_ids = f_attr.ids.size / sizeof(u64);
2943 /*
2944 * We don't have the cpu and thread maps on the header, so
2945 * for allocating the perf_sample_id table we fake 1 cpu and
2946 * hattr->ids threads.
2947 */
2948 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2949 goto out_delete_evlist;
2950
2951 lseek(fd, f_attr.ids.offset, SEEK_SET);
2952
2953 for (j = 0; j < nr_ids; j++) {
2954 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2955 goto out_errno;
2956
2957 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2958 }
2959
2960 lseek(fd, tmp, SEEK_SET);
2961 }
2962
2963 symbol_conf.nr_events = nr_attrs;
2964
2965 perf_header__process_sections(header, fd, &session->tevent,
2966 perf_file_section__process);
2967
2968 if (perf_evlist__prepare_tracepoint_events(session->evlist,
2969 session->tevent.pevent))
2970 goto out_delete_evlist;
2971
2972 return 0;
2973 out_errno:
2974 return -errno;
2975
2976 out_delete_evlist:
2977 perf_evlist__delete(session->evlist);
2978 session->evlist = NULL;
2979 return -ENOMEM;
2980 }
2981
perf_event__synthesize_attr(struct perf_tool * tool,struct perf_event_attr * attr,u32 ids,u64 * id,perf_event__handler_t process)2982 int perf_event__synthesize_attr(struct perf_tool *tool,
2983 struct perf_event_attr *attr, u32 ids, u64 *id,
2984 perf_event__handler_t process)
2985 {
2986 union perf_event *ev;
2987 size_t size;
2988 int err;
2989
2990 size = sizeof(struct perf_event_attr);
2991 size = PERF_ALIGN(size, sizeof(u64));
2992 size += sizeof(struct perf_event_header);
2993 size += ids * sizeof(u64);
2994
2995 ev = zalloc(size);
2996
2997 if (ev == NULL)
2998 return -ENOMEM;
2999
3000 ev->attr.attr = *attr;
3001 memcpy(ev->attr.id, id, ids * sizeof(u64));
3002
3003 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
3004 ev->attr.header.size = (u16)size;
3005
3006 if (ev->attr.header.size == size)
3007 err = process(tool, ev, NULL, NULL);
3008 else
3009 err = -E2BIG;
3010
3011 free(ev);
3012
3013 return err;
3014 }
3015
perf_event__synthesize_features(struct perf_tool * tool,struct perf_session * session,struct perf_evlist * evlist,perf_event__handler_t process)3016 int perf_event__synthesize_features(struct perf_tool *tool,
3017 struct perf_session *session,
3018 struct perf_evlist *evlist,
3019 perf_event__handler_t process)
3020 {
3021 struct perf_header *header = &session->header;
3022 struct feat_fd ff;
3023 struct feature_event *fe;
3024 size_t sz, sz_hdr;
3025 int feat, ret;
3026
3027 sz_hdr = sizeof(fe->header);
3028 sz = sizeof(union perf_event);
3029 /* get a nice alignment */
3030 sz = PERF_ALIGN(sz, page_size);
3031
3032 memset(&ff, 0, sizeof(ff));
3033
3034 ff.buf = malloc(sz);
3035 if (!ff.buf)
3036 return -ENOMEM;
3037
3038 ff.size = sz - sz_hdr;
3039
3040 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
3041 if (!feat_ops[feat].synthesize) {
3042 pr_debug("No record header feature for header :%d\n", feat);
3043 continue;
3044 }
3045
3046 ff.offset = sizeof(*fe);
3047
3048 ret = feat_ops[feat].write(&ff, evlist);
3049 if (ret || ff.offset <= (ssize_t)sizeof(*fe)) {
3050 pr_debug("Error writing feature\n");
3051 continue;
3052 }
3053 /* ff.buf may have changed due to realloc in do_write() */
3054 fe = ff.buf;
3055 memset(fe, 0, sizeof(*fe));
3056
3057 fe->feat_id = feat;
3058 fe->header.type = PERF_RECORD_HEADER_FEATURE;
3059 fe->header.size = ff.offset;
3060
3061 ret = process(tool, ff.buf, NULL, NULL);
3062 if (ret) {
3063 free(ff.buf);
3064 return ret;
3065 }
3066 }
3067 free(ff.buf);
3068 return 0;
3069 }
3070
perf_event__process_feature(struct perf_tool * tool,union perf_event * event,struct perf_session * session __maybe_unused)3071 int perf_event__process_feature(struct perf_tool *tool,
3072 union perf_event *event,
3073 struct perf_session *session __maybe_unused)
3074 {
3075 struct feat_fd ff = { .fd = 0 };
3076 struct feature_event *fe = (struct feature_event *)event;
3077 int type = fe->header.type;
3078 u64 feat = fe->feat_id;
3079
3080 if (type < 0 || type >= PERF_RECORD_HEADER_MAX) {
3081 pr_warning("invalid record type %d in pipe-mode\n", type);
3082 return 0;
3083 }
3084 if (feat == HEADER_RESERVED || feat > HEADER_LAST_FEATURE) {
3085 pr_warning("invalid record type %d in pipe-mode\n", type);
3086 return -1;
3087 }
3088
3089 if (!feat_ops[feat].process)
3090 return 0;
3091
3092 ff.buf = (void *)fe->data;
3093 ff.size = event->header.size - sizeof(*fe);
3094 ff.ph = &session->header;
3095
3096 if (feat_ops[feat].process(&ff, NULL))
3097 return -1;
3098
3099 if (!feat_ops[feat].print || !tool->show_feat_hdr)
3100 return 0;
3101
3102 if (!feat_ops[feat].full_only ||
3103 tool->show_feat_hdr >= SHOW_FEAT_HEADER_FULL_INFO) {
3104 feat_ops[feat].print(&ff, stdout);
3105 } else {
3106 fprintf(stdout, "# %s info available, use -I to display\n",
3107 feat_ops[feat].name);
3108 }
3109
3110 return 0;
3111 }
3112
3113 static struct event_update_event *
event_update_event__new(size_t size,u64 type,u64 id)3114 event_update_event__new(size_t size, u64 type, u64 id)
3115 {
3116 struct event_update_event *ev;
3117
3118 size += sizeof(*ev);
3119 size = PERF_ALIGN(size, sizeof(u64));
3120
3121 ev = zalloc(size);
3122 if (ev) {
3123 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3124 ev->header.size = (u16)size;
3125 ev->type = type;
3126 ev->id = id;
3127 }
3128 return ev;
3129 }
3130
3131 int
perf_event__synthesize_event_update_unit(struct perf_tool * tool,struct perf_evsel * evsel,perf_event__handler_t process)3132 perf_event__synthesize_event_update_unit(struct perf_tool *tool,
3133 struct perf_evsel *evsel,
3134 perf_event__handler_t process)
3135 {
3136 struct event_update_event *ev;
3137 size_t size = strlen(evsel->unit);
3138 int err;
3139
3140 ev = event_update_event__new(size + 1, PERF_EVENT_UPDATE__UNIT, evsel->id[0]);
3141 if (ev == NULL)
3142 return -ENOMEM;
3143
3144 strlcpy(ev->data, evsel->unit, size + 1);
3145 err = process(tool, (union perf_event *)ev, NULL, NULL);
3146 free(ev);
3147 return err;
3148 }
3149
3150 int
perf_event__synthesize_event_update_scale(struct perf_tool * tool,struct perf_evsel * evsel,perf_event__handler_t process)3151 perf_event__synthesize_event_update_scale(struct perf_tool *tool,
3152 struct perf_evsel *evsel,
3153 perf_event__handler_t process)
3154 {
3155 struct event_update_event *ev;
3156 struct event_update_event_scale *ev_data;
3157 int err;
3158
3159 ev = event_update_event__new(sizeof(*ev_data), PERF_EVENT_UPDATE__SCALE, evsel->id[0]);
3160 if (ev == NULL)
3161 return -ENOMEM;
3162
3163 ev_data = (struct event_update_event_scale *) ev->data;
3164 ev_data->scale = evsel->scale;
3165 err = process(tool, (union perf_event*) ev, NULL, NULL);
3166 free(ev);
3167 return err;
3168 }
3169
3170 int
perf_event__synthesize_event_update_name(struct perf_tool * tool,struct perf_evsel * evsel,perf_event__handler_t process)3171 perf_event__synthesize_event_update_name(struct perf_tool *tool,
3172 struct perf_evsel *evsel,
3173 perf_event__handler_t process)
3174 {
3175 struct event_update_event *ev;
3176 size_t len = strlen(evsel->name);
3177 int err;
3178
3179 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]);
3180 if (ev == NULL)
3181 return -ENOMEM;
3182
3183 strlcpy(ev->data, evsel->name, len + 1);
3184 err = process(tool, (union perf_event*) ev, NULL, NULL);
3185 free(ev);
3186 return err;
3187 }
3188
3189 int
perf_event__synthesize_event_update_cpus(struct perf_tool * tool,struct perf_evsel * evsel,perf_event__handler_t process)3190 perf_event__synthesize_event_update_cpus(struct perf_tool *tool,
3191 struct perf_evsel *evsel,
3192 perf_event__handler_t process)
3193 {
3194 size_t size = sizeof(struct event_update_event);
3195 struct event_update_event *ev;
3196 int max, err;
3197 u16 type;
3198
3199 if (!evsel->own_cpus)
3200 return 0;
3201
3202 ev = cpu_map_data__alloc(evsel->own_cpus, &size, &type, &max);
3203 if (!ev)
3204 return -ENOMEM;
3205
3206 ev->header.type = PERF_RECORD_EVENT_UPDATE;
3207 ev->header.size = (u16)size;
3208 ev->type = PERF_EVENT_UPDATE__CPUS;
3209 ev->id = evsel->id[0];
3210
3211 cpu_map_data__synthesize((struct cpu_map_data *) ev->data,
3212 evsel->own_cpus,
3213 type, max);
3214
3215 err = process(tool, (union perf_event*) ev, NULL, NULL);
3216 free(ev);
3217 return err;
3218 }
3219
perf_event__fprintf_event_update(union perf_event * event,FILE * fp)3220 size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
3221 {
3222 struct event_update_event *ev = &event->event_update;
3223 struct event_update_event_scale *ev_scale;
3224 struct event_update_event_cpus *ev_cpus;
3225 struct cpu_map *map;
3226 size_t ret;
3227
3228 ret = fprintf(fp, "\n... id: %" PRIu64 "\n", ev->id);
3229
3230 switch (ev->type) {
3231 case PERF_EVENT_UPDATE__SCALE:
3232 ev_scale = (struct event_update_event_scale *) ev->data;
3233 ret += fprintf(fp, "... scale: %f\n", ev_scale->scale);
3234 break;
3235 case PERF_EVENT_UPDATE__UNIT:
3236 ret += fprintf(fp, "... unit: %s\n", ev->data);
3237 break;
3238 case PERF_EVENT_UPDATE__NAME:
3239 ret += fprintf(fp, "... name: %s\n", ev->data);
3240 break;
3241 case PERF_EVENT_UPDATE__CPUS:
3242 ev_cpus = (struct event_update_event_cpus *) ev->data;
3243 ret += fprintf(fp, "... ");
3244
3245 map = cpu_map__new_data(&ev_cpus->cpus);
3246 if (map)
3247 ret += cpu_map__fprintf(map, fp);
3248 else
3249 ret += fprintf(fp, "failed to get cpus\n");
3250 break;
3251 default:
3252 ret += fprintf(fp, "... unknown type\n");
3253 break;
3254 }
3255
3256 return ret;
3257 }
3258
perf_event__synthesize_attrs(struct perf_tool * tool,struct perf_session * session,perf_event__handler_t process)3259 int perf_event__synthesize_attrs(struct perf_tool *tool,
3260 struct perf_session *session,
3261 perf_event__handler_t process)
3262 {
3263 struct perf_evsel *evsel;
3264 int err = 0;
3265
3266 evlist__for_each_entry(session->evlist, evsel) {
3267 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
3268 evsel->id, process);
3269 if (err) {
3270 pr_debug("failed to create perf header attribute\n");
3271 return err;
3272 }
3273 }
3274
3275 return err;
3276 }
3277
perf_event__process_attr(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_evlist ** pevlist)3278 int perf_event__process_attr(struct perf_tool *tool __maybe_unused,
3279 union perf_event *event,
3280 struct perf_evlist **pevlist)
3281 {
3282 u32 i, ids, n_ids;
3283 struct perf_evsel *evsel;
3284 struct perf_evlist *evlist = *pevlist;
3285
3286 if (evlist == NULL) {
3287 *pevlist = evlist = perf_evlist__new();
3288 if (evlist == NULL)
3289 return -ENOMEM;
3290 }
3291
3292 evsel = perf_evsel__new(&event->attr.attr);
3293 if (evsel == NULL)
3294 return -ENOMEM;
3295
3296 perf_evlist__add(evlist, evsel);
3297
3298 ids = event->header.size;
3299 ids -= (void *)&event->attr.id - (void *)event;
3300 n_ids = ids / sizeof(u64);
3301 /*
3302 * We don't have the cpu and thread maps on the header, so
3303 * for allocating the perf_sample_id table we fake 1 cpu and
3304 * hattr->ids threads.
3305 */
3306 if (perf_evsel__alloc_id(evsel, 1, n_ids))
3307 return -ENOMEM;
3308
3309 for (i = 0; i < n_ids; i++) {
3310 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
3311 }
3312
3313 symbol_conf.nr_events = evlist->nr_entries;
3314
3315 return 0;
3316 }
3317
perf_event__process_event_update(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_evlist ** pevlist)3318 int perf_event__process_event_update(struct perf_tool *tool __maybe_unused,
3319 union perf_event *event,
3320 struct perf_evlist **pevlist)
3321 {
3322 struct event_update_event *ev = &event->event_update;
3323 struct event_update_event_scale *ev_scale;
3324 struct event_update_event_cpus *ev_cpus;
3325 struct perf_evlist *evlist;
3326 struct perf_evsel *evsel;
3327 struct cpu_map *map;
3328
3329 if (!pevlist || *pevlist == NULL)
3330 return -EINVAL;
3331
3332 evlist = *pevlist;
3333
3334 evsel = perf_evlist__id2evsel(evlist, ev->id);
3335 if (evsel == NULL)
3336 return -EINVAL;
3337
3338 switch (ev->type) {
3339 case PERF_EVENT_UPDATE__UNIT:
3340 evsel->unit = strdup(ev->data);
3341 break;
3342 case PERF_EVENT_UPDATE__NAME:
3343 evsel->name = strdup(ev->data);
3344 break;
3345 case PERF_EVENT_UPDATE__SCALE:
3346 ev_scale = (struct event_update_event_scale *) ev->data;
3347 evsel->scale = ev_scale->scale;
3348 break;
3349 case PERF_EVENT_UPDATE__CPUS:
3350 ev_cpus = (struct event_update_event_cpus *) ev->data;
3351
3352 map = cpu_map__new_data(&ev_cpus->cpus);
3353 if (map)
3354 evsel->own_cpus = map;
3355 else
3356 pr_err("failed to get event_update cpus\n");
3357 default:
3358 break;
3359 }
3360
3361 return 0;
3362 }
3363
perf_event__synthesize_tracing_data(struct perf_tool * tool,int fd,struct perf_evlist * evlist,perf_event__handler_t process)3364 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
3365 struct perf_evlist *evlist,
3366 perf_event__handler_t process)
3367 {
3368 union perf_event ev;
3369 struct tracing_data *tdata;
3370 ssize_t size = 0, aligned_size = 0, padding;
3371 struct feat_fd ff;
3372 int err __maybe_unused = 0;
3373
3374 /*
3375 * We are going to store the size of the data followed
3376 * by the data contents. Since the fd descriptor is a pipe,
3377 * we cannot seek back to store the size of the data once
3378 * we know it. Instead we:
3379 *
3380 * - write the tracing data to the temp file
3381 * - get/write the data size to pipe
3382 * - write the tracing data from the temp file
3383 * to the pipe
3384 */
3385 tdata = tracing_data_get(&evlist->entries, fd, true);
3386 if (!tdata)
3387 return -1;
3388
3389 memset(&ev, 0, sizeof(ev));
3390
3391 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
3392 size = tdata->size;
3393 aligned_size = PERF_ALIGN(size, sizeof(u64));
3394 padding = aligned_size - size;
3395 ev.tracing_data.header.size = sizeof(ev.tracing_data);
3396 ev.tracing_data.size = aligned_size;
3397
3398 process(tool, &ev, NULL, NULL);
3399
3400 /*
3401 * The put function will copy all the tracing data
3402 * stored in temp file to the pipe.
3403 */
3404 tracing_data_put(tdata);
3405
3406 ff = (struct feat_fd){ .fd = fd };
3407 if (write_padded(&ff, NULL, 0, padding))
3408 return -1;
3409
3410 return aligned_size;
3411 }
3412
perf_event__process_tracing_data(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_session * session)3413 int perf_event__process_tracing_data(struct perf_tool *tool __maybe_unused,
3414 union perf_event *event,
3415 struct perf_session *session)
3416 {
3417 ssize_t size_read, padding, size = event->tracing_data.size;
3418 int fd = perf_data_file__fd(session->file);
3419 off_t offset = lseek(fd, 0, SEEK_CUR);
3420 char buf[BUFSIZ];
3421
3422 /* setup for reading amidst mmap */
3423 lseek(fd, offset + sizeof(struct tracing_data_event),
3424 SEEK_SET);
3425
3426 size_read = trace_report(fd, &session->tevent,
3427 session->repipe);
3428 padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read;
3429
3430 if (readn(fd, buf, padding) < 0) {
3431 pr_err("%s: reading input file", __func__);
3432 return -1;
3433 }
3434 if (session->repipe) {
3435 int retw = write(STDOUT_FILENO, buf, padding);
3436 if (retw <= 0 || retw != padding) {
3437 pr_err("%s: repiping tracing data padding", __func__);
3438 return -1;
3439 }
3440 }
3441
3442 if (size_read + padding != size) {
3443 pr_err("%s: tracing data size mismatch", __func__);
3444 return -1;
3445 }
3446
3447 perf_evlist__prepare_tracepoint_events(session->evlist,
3448 session->tevent.pevent);
3449
3450 return size_read + padding;
3451 }
3452
perf_event__synthesize_build_id(struct perf_tool * tool,struct dso * pos,u16 misc,perf_event__handler_t process,struct machine * machine)3453 int perf_event__synthesize_build_id(struct perf_tool *tool,
3454 struct dso *pos, u16 misc,
3455 perf_event__handler_t process,
3456 struct machine *machine)
3457 {
3458 union perf_event ev;
3459 size_t len;
3460 int err = 0;
3461
3462 if (!pos->hit)
3463 return err;
3464
3465 memset(&ev, 0, sizeof(ev));
3466
3467 len = pos->long_name_len + 1;
3468 len = PERF_ALIGN(len, NAME_ALIGN);
3469 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
3470 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
3471 ev.build_id.header.misc = misc;
3472 ev.build_id.pid = machine->pid;
3473 ev.build_id.header.size = sizeof(ev.build_id) + len;
3474 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
3475
3476 err = process(tool, &ev, NULL, machine);
3477
3478 return err;
3479 }
3480
perf_event__process_build_id(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_session * session)3481 int perf_event__process_build_id(struct perf_tool *tool __maybe_unused,
3482 union perf_event *event,
3483 struct perf_session *session)
3484 {
3485 __event_process_build_id(&event->build_id,
3486 event->build_id.filename,
3487 session);
3488 return 0;
3489 }
3490