perf tools: Factor out feature op to process header sections
[linux-flexiantxendom0-3.2.10.git] / tools / perf / util / header.c
1 #define _FILE_OFFSET_BITS 64
2
3 #include "util.h"
4 #include <sys/types.h>
5 #include <byteswap.h>
6 #include <unistd.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <linux/list.h>
10 #include <linux/kernel.h>
11 #include <linux/bitops.h>
12 #include <sys/utsname.h>
13
14 #include "evlist.h"
15 #include "evsel.h"
16 #include "header.h"
17 #include "../perf.h"
18 #include "trace-event.h"
19 #include "session.h"
20 #include "symbol.h"
21 #include "debug.h"
22 #include "cpumap.h"
23
24 static bool no_buildid_cache = false;
25
26 static int event_count;
27 static struct perf_trace_event_type *events;
28
29 static u32 header_argc;
30 static const char **header_argv;
31
32 int perf_header__push_event(u64 id, const char *name)
33 {
34         if (strlen(name) > MAX_EVENT_NAME)
35                 pr_warning("Event %s will be truncated\n", name);
36
37         if (!events) {
38                 events = malloc(sizeof(struct perf_trace_event_type));
39                 if (events == NULL)
40                         return -ENOMEM;
41         } else {
42                 struct perf_trace_event_type *nevents;
43
44                 nevents = realloc(events, (event_count + 1) * sizeof(*events));
45                 if (nevents == NULL)
46                         return -ENOMEM;
47                 events = nevents;
48         }
49         memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
50         events[event_count].event_id = id;
51         strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
52         event_count++;
53         return 0;
54 }
55
56 char *perf_header__find_event(u64 id)
57 {
58         int i;
59         for (i = 0 ; i < event_count; i++) {
60                 if (events[i].event_id == id)
61                         return events[i].name;
62         }
63         return NULL;
64 }
65
66 /*
67  * magic2 = "PERFILE2"
68  * must be a numerical value to let the endianness
69  * determine the memory layout. That way we are able
70  * to detect endianness when reading the perf.data file
71  * back.
72  *
73  * we check for legacy (PERFFILE) format.
74  */
75 static const char *__perf_magic1 = "PERFFILE";
76 static const u64 __perf_magic2    = 0x32454c4946524550ULL;
77 static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
78
79 #define PERF_MAGIC      __perf_magic2
80
81 struct perf_file_attr {
82         struct perf_event_attr  attr;
83         struct perf_file_section        ids;
84 };
85
86 void perf_header__set_feat(struct perf_header *header, int feat)
87 {
88         set_bit(feat, header->adds_features);
89 }
90
91 void perf_header__clear_feat(struct perf_header *header, int feat)
92 {
93         clear_bit(feat, header->adds_features);
94 }
95
96 bool perf_header__has_feat(const struct perf_header *header, int feat)
97 {
98         return test_bit(feat, header->adds_features);
99 }
100
101 static int do_write(int fd, const void *buf, size_t size)
102 {
103         while (size) {
104                 int ret = write(fd, buf, size);
105
106                 if (ret < 0)
107                         return -errno;
108
109                 size -= ret;
110                 buf += ret;
111         }
112
113         return 0;
114 }
115
116 #define NAME_ALIGN 64
117
118 static int write_padded(int fd, const void *bf, size_t count,
119                         size_t count_aligned)
120 {
121         static const char zero_buf[NAME_ALIGN];
122         int err = do_write(fd, bf, count);
123
124         if (!err)
125                 err = do_write(fd, zero_buf, count_aligned - count);
126
127         return err;
128 }
129
130 static int do_write_string(int fd, const char *str)
131 {
132         u32 len, olen;
133         int ret;
134
135         olen = strlen(str) + 1;
136         len = ALIGN(olen, NAME_ALIGN);
137
138         /* write len, incl. \0 */
139         ret = do_write(fd, &len, sizeof(len));
140         if (ret < 0)
141                 return ret;
142
143         return write_padded(fd, str, olen, len);
144 }
145
146 static char *do_read_string(int fd, struct perf_header *ph)
147 {
148         ssize_t sz, ret;
149         u32 len;
150         char *buf;
151
152         sz = read(fd, &len, sizeof(len));
153         if (sz < (ssize_t)sizeof(len))
154                 return NULL;
155
156         if (ph->needs_swap)
157                 len = bswap_32(len);
158
159         buf = malloc(len);
160         if (!buf)
161                 return NULL;
162
163         ret = read(fd, buf, len);
164         if (ret == (ssize_t)len) {
165                 /*
166                  * strings are padded by zeroes
167                  * thus the actual strlen of buf
168                  * may be less than len
169                  */
170                 return buf;
171         }
172
173         free(buf);
174         return NULL;
175 }
176
177 int
178 perf_header__set_cmdline(int argc, const char **argv)
179 {
180         int i;
181
182         header_argc = (u32)argc;
183
184         /* do not include NULL termination */
185         header_argv = calloc(argc, sizeof(char *));
186         if (!header_argv)
187                 return -ENOMEM;
188
189         /*
190          * must copy argv contents because it gets moved
191          * around during option parsing
192          */
193         for (i = 0; i < argc ; i++)
194                 header_argv[i] = argv[i];
195
196         return 0;
197 }
198
199 #define dsos__for_each_with_build_id(pos, head) \
200         list_for_each_entry(pos, head, node)    \
201                 if (!pos->has_build_id)         \
202                         continue;               \
203                 else
204
205 static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
206                                 u16 misc, int fd)
207 {
208         struct dso *pos;
209
210         dsos__for_each_with_build_id(pos, head) {
211                 int err;
212                 struct build_id_event b;
213                 size_t len;
214
215                 if (!pos->hit)
216                         continue;
217                 len = pos->long_name_len + 1;
218                 len = ALIGN(len, NAME_ALIGN);
219                 memset(&b, 0, sizeof(b));
220                 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
221                 b.pid = pid;
222                 b.header.misc = misc;
223                 b.header.size = sizeof(b) + len;
224                 err = do_write(fd, &b, sizeof(b));
225                 if (err < 0)
226                         return err;
227                 err = write_padded(fd, pos->long_name,
228                                    pos->long_name_len + 1, len);
229                 if (err < 0)
230                         return err;
231         }
232
233         return 0;
234 }
235
236 static int machine__write_buildid_table(struct machine *machine, int fd)
237 {
238         int err;
239         u16 kmisc = PERF_RECORD_MISC_KERNEL,
240             umisc = PERF_RECORD_MISC_USER;
241
242         if (!machine__is_host(machine)) {
243                 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
244                 umisc = PERF_RECORD_MISC_GUEST_USER;
245         }
246
247         err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid,
248                                           kmisc, fd);
249         if (err == 0)
250                 err = __dsos__write_buildid_table(&machine->user_dsos,
251                                                   machine->pid, umisc, fd);
252         return err;
253 }
254
255 static int dsos__write_buildid_table(struct perf_header *header, int fd)
256 {
257         struct perf_session *session = container_of(header,
258                         struct perf_session, header);
259         struct rb_node *nd;
260         int err = machine__write_buildid_table(&session->host_machine, fd);
261
262         if (err)
263                 return err;
264
265         for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
266                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
267                 err = machine__write_buildid_table(pos, fd);
268                 if (err)
269                         break;
270         }
271         return err;
272 }
273
274 int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
275                           const char *name, bool is_kallsyms)
276 {
277         const size_t size = PATH_MAX;
278         char *realname, *filename = zalloc(size),
279              *linkname = zalloc(size), *targetname;
280         int len, err = -1;
281
282         if (is_kallsyms) {
283                 if (symbol_conf.kptr_restrict) {
284                         pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
285                         return 0;
286                 }
287                 realname = (char *)name;
288         } else
289                 realname = realpath(name, NULL);
290
291         if (realname == NULL || filename == NULL || linkname == NULL)
292                 goto out_free;
293
294         len = snprintf(filename, size, "%s%s%s",
295                        debugdir, is_kallsyms ? "/" : "", realname);
296         if (mkdir_p(filename, 0755))
297                 goto out_free;
298
299         snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id);
300
301         if (access(filename, F_OK)) {
302                 if (is_kallsyms) {
303                          if (copyfile("/proc/kallsyms", filename))
304                                 goto out_free;
305                 } else if (link(realname, filename) && copyfile(name, filename))
306                         goto out_free;
307         }
308
309         len = snprintf(linkname, size, "%s/.build-id/%.2s",
310                        debugdir, sbuild_id);
311
312         if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
313                 goto out_free;
314
315         snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
316         targetname = filename + strlen(debugdir) - 5;
317         memcpy(targetname, "../..", 5);
318
319         if (symlink(targetname, linkname) == 0)
320                 err = 0;
321 out_free:
322         if (!is_kallsyms)
323                 free(realname);
324         free(filename);
325         free(linkname);
326         return err;
327 }
328
329 static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
330                                  const char *name, const char *debugdir,
331                                  bool is_kallsyms)
332 {
333         char sbuild_id[BUILD_ID_SIZE * 2 + 1];
334
335         build_id__sprintf(build_id, build_id_size, sbuild_id);
336
337         return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms);
338 }
339
340 int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
341 {
342         const size_t size = PATH_MAX;
343         char *filename = zalloc(size),
344              *linkname = zalloc(size);
345         int err = -1;
346
347         if (filename == NULL || linkname == NULL)
348                 goto out_free;
349
350         snprintf(linkname, size, "%s/.build-id/%.2s/%s",
351                  debugdir, sbuild_id, sbuild_id + 2);
352
353         if (access(linkname, F_OK))
354                 goto out_free;
355
356         if (readlink(linkname, filename, size - 1) < 0)
357                 goto out_free;
358
359         if (unlink(linkname))
360                 goto out_free;
361
362         /*
363          * Since the link is relative, we must make it absolute:
364          */
365         snprintf(linkname, size, "%s/.build-id/%.2s/%s",
366                  debugdir, sbuild_id, filename);
367
368         if (unlink(linkname))
369                 goto out_free;
370
371         err = 0;
372 out_free:
373         free(filename);
374         free(linkname);
375         return err;
376 }
377
378 static int dso__cache_build_id(struct dso *dso, const char *debugdir)
379 {
380         bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
381
382         return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id),
383                                      dso->long_name, debugdir, is_kallsyms);
384 }
385
386 static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
387 {
388         struct dso *pos;
389         int err = 0;
390
391         dsos__for_each_with_build_id(pos, head)
392                 if (dso__cache_build_id(pos, debugdir))
393                         err = -1;
394
395         return err;
396 }
397
398 static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
399 {
400         int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir);
401         ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir);
402         return ret;
403 }
404
405 static int perf_session__cache_build_ids(struct perf_session *session)
406 {
407         struct rb_node *nd;
408         int ret;
409         char debugdir[PATH_MAX];
410
411         snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
412
413         if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
414                 return -1;
415
416         ret = machine__cache_build_ids(&session->host_machine, debugdir);
417
418         for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
419                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
420                 ret |= machine__cache_build_ids(pos, debugdir);
421         }
422         return ret ? -1 : 0;
423 }
424
425 static bool machine__read_build_ids(struct machine *machine, bool with_hits)
426 {
427         bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
428         ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
429         return ret;
430 }
431
432 static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
433 {
434         struct rb_node *nd;
435         bool ret = machine__read_build_ids(&session->host_machine, with_hits);
436
437         for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
438                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
439                 ret |= machine__read_build_ids(pos, with_hits);
440         }
441
442         return ret;
443 }
444
445 static int write_trace_info(int fd, struct perf_header *h __used,
446                             struct perf_evlist *evlist)
447 {
448         return read_tracing_data(fd, &evlist->entries);
449 }
450
451
452 static int write_build_id(int fd, struct perf_header *h,
453                           struct perf_evlist *evlist __used)
454 {
455         struct perf_session *session;
456         int err;
457
458         session = container_of(h, struct perf_session, header);
459
460         if (!perf_session__read_build_ids(session, true))
461                 return -1;
462
463         err = dsos__write_buildid_table(h, fd);
464         if (err < 0) {
465                 pr_debug("failed to write buildid table\n");
466                 return err;
467         }
468         if (!no_buildid_cache)
469                 perf_session__cache_build_ids(session);
470
471         return 0;
472 }
473
474 static int write_hostname(int fd, struct perf_header *h __used,
475                           struct perf_evlist *evlist __used)
476 {
477         struct utsname uts;
478         int ret;
479
480         ret = uname(&uts);
481         if (ret < 0)
482                 return -1;
483
484         return do_write_string(fd, uts.nodename);
485 }
486
487 static int write_osrelease(int fd, struct perf_header *h __used,
488                            struct perf_evlist *evlist __used)
489 {
490         struct utsname uts;
491         int ret;
492
493         ret = uname(&uts);
494         if (ret < 0)
495                 return -1;
496
497         return do_write_string(fd, uts.release);
498 }
499
500 static int write_arch(int fd, struct perf_header *h __used,
501                       struct perf_evlist *evlist __used)
502 {
503         struct utsname uts;
504         int ret;
505
506         ret = uname(&uts);
507         if (ret < 0)
508                 return -1;
509
510         return do_write_string(fd, uts.machine);
511 }
512
513 static int write_version(int fd, struct perf_header *h __used,
514                          struct perf_evlist *evlist __used)
515 {
516         return do_write_string(fd, perf_version_string);
517 }
518
519 static int write_cpudesc(int fd, struct perf_header *h __used,
520                        struct perf_evlist *evlist __used)
521 {
522 #ifndef CPUINFO_PROC
523 #define CPUINFO_PROC NULL
524 #endif
525         FILE *file;
526         char *buf = NULL;
527         char *s, *p;
528         const char *search = CPUINFO_PROC;
529         size_t len = 0;
530         int ret = -1;
531
532         if (!search)
533                 return -1;
534
535         file = fopen("/proc/cpuinfo", "r");
536         if (!file)
537                 return -1;
538
539         while (getline(&buf, &len, file) > 0) {
540                 ret = strncmp(buf, search, strlen(search));
541                 if (!ret)
542                         break;
543         }
544
545         if (ret)
546                 goto done;
547
548         s = buf;
549
550         p = strchr(buf, ':');
551         if (p && *(p+1) == ' ' && *(p+2))
552                 s = p + 2;
553         p = strchr(s, '\n');
554         if (p)
555                 *p = '\0';
556
557         /* squash extra space characters (branding string) */
558         p = s;
559         while (*p) {
560                 if (isspace(*p)) {
561                         char *r = p + 1;
562                         char *q = r;
563                         *p = ' ';
564                         while (*q && isspace(*q))
565                                 q++;
566                         if (q != (p+1))
567                                 while ((*r++ = *q++));
568                 }
569                 p++;
570         }
571         ret = do_write_string(fd, s);
572 done:
573         free(buf);
574         fclose(file);
575         return ret;
576 }
577
578 static int write_nrcpus(int fd, struct perf_header *h __used,
579                         struct perf_evlist *evlist __used)
580 {
581         long nr;
582         u32 nrc, nra;
583         int ret;
584
585         nr = sysconf(_SC_NPROCESSORS_CONF);
586         if (nr < 0)
587                 return -1;
588
589         nrc = (u32)(nr & UINT_MAX);
590
591         nr = sysconf(_SC_NPROCESSORS_ONLN);
592         if (nr < 0)
593                 return -1;
594
595         nra = (u32)(nr & UINT_MAX);
596
597         ret = do_write(fd, &nrc, sizeof(nrc));
598         if (ret < 0)
599                 return ret;
600
601         return do_write(fd, &nra, sizeof(nra));
602 }
603
604 static int write_event_desc(int fd, struct perf_header *h __used,
605                             struct perf_evlist *evlist)
606 {
607         struct perf_evsel *attr;
608         u32 nre = 0, nri, sz;
609         int ret;
610
611         list_for_each_entry(attr, &evlist->entries, node)
612                 nre++;
613
614         /*
615          * write number of events
616          */
617         ret = do_write(fd, &nre, sizeof(nre));
618         if (ret < 0)
619                 return ret;
620
621         /*
622          * size of perf_event_attr struct
623          */
624         sz = (u32)sizeof(attr->attr);
625         ret = do_write(fd, &sz, sizeof(sz));
626         if (ret < 0)
627                 return ret;
628
629         list_for_each_entry(attr, &evlist->entries, node) {
630
631                 ret = do_write(fd, &attr->attr, sz);
632                 if (ret < 0)
633                         return ret;
634                 /*
635                  * write number of unique id per event
636                  * there is one id per instance of an event
637                  *
638                  * copy into an nri to be independent of the
639                  * type of ids,
640                  */
641                 nri = attr->ids;
642                 ret = do_write(fd, &nri, sizeof(nri));
643                 if (ret < 0)
644                         return ret;
645
646                 /*
647                  * write event string as passed on cmdline
648                  */
649                 ret = do_write_string(fd, event_name(attr));
650                 if (ret < 0)
651                         return ret;
652                 /*
653                  * write unique ids for this event
654                  */
655                 ret = do_write(fd, attr->id, attr->ids * sizeof(u64));
656                 if (ret < 0)
657                         return ret;
658         }
659         return 0;
660 }
661
662 static int write_cmdline(int fd, struct perf_header *h __used,
663                          struct perf_evlist *evlist __used)
664 {
665         char buf[MAXPATHLEN];
666         char proc[32];
667         u32 i, n;
668         int ret;
669
670         /*
671          * actual atual path to perf binary
672          */
673         sprintf(proc, "/proc/%d/exe", getpid());
674         ret = readlink(proc, buf, sizeof(buf));
675         if (ret <= 0)
676                 return -1;
677
678         /* readlink() does not add null termination */
679         buf[ret] = '\0';
680
681         /* account for binary path */
682         n = header_argc + 1;
683
684         ret = do_write(fd, &n, sizeof(n));
685         if (ret < 0)
686                 return ret;
687
688         ret = do_write_string(fd, buf);
689         if (ret < 0)
690                 return ret;
691
692         for (i = 0 ; i < header_argc; i++) {
693                 ret = do_write_string(fd, header_argv[i]);
694                 if (ret < 0)
695                         return ret;
696         }
697         return 0;
698 }
699
700 #define CORE_SIB_FMT \
701         "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
702 #define THRD_SIB_FMT \
703         "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
704
705 struct cpu_topo {
706         u32 core_sib;
707         u32 thread_sib;
708         char **core_siblings;
709         char **thread_siblings;
710 };
711
712 static int build_cpu_topo(struct cpu_topo *tp, int cpu)
713 {
714         FILE *fp;
715         char filename[MAXPATHLEN];
716         char *buf = NULL, *p;
717         size_t len = 0;
718         u32 i = 0;
719         int ret = -1;
720
721         sprintf(filename, CORE_SIB_FMT, cpu);
722         fp = fopen(filename, "r");
723         if (!fp)
724                 return -1;
725
726         if (getline(&buf, &len, fp) <= 0)
727                 goto done;
728
729         fclose(fp);
730
731         p = strchr(buf, '\n');
732         if (p)
733                 *p = '\0';
734
735         for (i = 0; i < tp->core_sib; i++) {
736                 if (!strcmp(buf, tp->core_siblings[i]))
737                         break;
738         }
739         if (i == tp->core_sib) {
740                 tp->core_siblings[i] = buf;
741                 tp->core_sib++;
742                 buf = NULL;
743                 len = 0;
744         }
745
746         sprintf(filename, THRD_SIB_FMT, cpu);
747         fp = fopen(filename, "r");
748         if (!fp)
749                 goto done;
750
751         if (getline(&buf, &len, fp) <= 0)
752                 goto done;
753
754         p = strchr(buf, '\n');
755         if (p)
756                 *p = '\0';
757
758         for (i = 0; i < tp->thread_sib; i++) {
759                 if (!strcmp(buf, tp->thread_siblings[i]))
760                         break;
761         }
762         if (i == tp->thread_sib) {
763                 tp->thread_siblings[i] = buf;
764                 tp->thread_sib++;
765                 buf = NULL;
766         }
767         ret = 0;
768 done:
769         if(fp)
770                 fclose(fp);
771         free(buf);
772         return ret;
773 }
774
775 static void free_cpu_topo(struct cpu_topo *tp)
776 {
777         u32 i;
778
779         if (!tp)
780                 return;
781
782         for (i = 0 ; i < tp->core_sib; i++)
783                 free(tp->core_siblings[i]);
784
785         for (i = 0 ; i < tp->thread_sib; i++)
786                 free(tp->thread_siblings[i]);
787
788         free(tp);
789 }
790
791 static struct cpu_topo *build_cpu_topology(void)
792 {
793         struct cpu_topo *tp;
794         void *addr;
795         u32 nr, i;
796         size_t sz;
797         long ncpus;
798         int ret = -1;
799
800         ncpus = sysconf(_SC_NPROCESSORS_CONF);
801         if (ncpus < 0)
802                 return NULL;
803
804         nr = (u32)(ncpus & UINT_MAX);
805
806         sz = nr * sizeof(char *);
807
808         addr = calloc(1, sizeof(*tp) + 2 * sz);
809         if (!addr)
810                 return NULL;
811
812         tp = addr;
813
814         addr += sizeof(*tp);
815         tp->core_siblings = addr;
816         addr += sz;
817         tp->thread_siblings = addr;
818
819         for (i = 0; i < nr; i++) {
820                 ret = build_cpu_topo(tp, i);
821                 if (ret < 0)
822                         break;
823         }
824         if (ret) {
825                 free_cpu_topo(tp);
826                 tp = NULL;
827         }
828         return tp;
829 }
830
831 static int write_cpu_topology(int fd, struct perf_header *h __used,
832                           struct perf_evlist *evlist __used)
833 {
834         struct cpu_topo *tp;
835         u32 i;
836         int ret;
837
838         tp = build_cpu_topology();
839         if (!tp)
840                 return -1;
841
842         ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
843         if (ret < 0)
844                 goto done;
845
846         for (i = 0; i < tp->core_sib; i++) {
847                 ret = do_write_string(fd, tp->core_siblings[i]);
848                 if (ret < 0)
849                         goto done;
850         }
851         ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
852         if (ret < 0)
853                 goto done;
854
855         for (i = 0; i < tp->thread_sib; i++) {
856                 ret = do_write_string(fd, tp->thread_siblings[i]);
857                 if (ret < 0)
858                         break;
859         }
860 done:
861         free_cpu_topo(tp);
862         return ret;
863 }
864
865
866
867 static int write_total_mem(int fd, struct perf_header *h __used,
868                           struct perf_evlist *evlist __used)
869 {
870         char *buf = NULL;
871         FILE *fp;
872         size_t len = 0;
873         int ret = -1, n;
874         uint64_t mem;
875
876         fp = fopen("/proc/meminfo", "r");
877         if (!fp)
878                 return -1;
879
880         while (getline(&buf, &len, fp) > 0) {
881                 ret = strncmp(buf, "MemTotal:", 9);
882                 if (!ret)
883                         break;
884         }
885         if (!ret) {
886                 n = sscanf(buf, "%*s %"PRIu64, &mem);
887                 if (n == 1)
888                         ret = do_write(fd, &mem, sizeof(mem));
889         }
890         free(buf);
891         fclose(fp);
892         return ret;
893 }
894
895 static int write_topo_node(int fd, int node)
896 {
897         char str[MAXPATHLEN];
898         char field[32];
899         char *buf = NULL, *p;
900         size_t len = 0;
901         FILE *fp;
902         u64 mem_total, mem_free, mem;
903         int ret = -1;
904
905         sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
906         fp = fopen(str, "r");
907         if (!fp)
908                 return -1;
909
910         while (getline(&buf, &len, fp) > 0) {
911                 /* skip over invalid lines */
912                 if (!strchr(buf, ':'))
913                         continue;
914                 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
915                         goto done;
916                 if (!strcmp(field, "MemTotal:"))
917                         mem_total = mem;
918                 if (!strcmp(field, "MemFree:"))
919                         mem_free = mem;
920         }
921
922         fclose(fp);
923
924         ret = do_write(fd, &mem_total, sizeof(u64));
925         if (ret)
926                 goto done;
927
928         ret = do_write(fd, &mem_free, sizeof(u64));
929         if (ret)
930                 goto done;
931
932         ret = -1;
933         sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
934
935         fp = fopen(str, "r");
936         if (!fp)
937                 goto done;
938
939         if (getline(&buf, &len, fp) <= 0)
940                 goto done;
941
942         p = strchr(buf, '\n');
943         if (p)
944                 *p = '\0';
945
946         ret = do_write_string(fd, buf);
947 done:
948         free(buf);
949         fclose(fp);
950         return ret;
951 }
952
953 static int write_numa_topology(int fd, struct perf_header *h __used,
954                           struct perf_evlist *evlist __used)
955 {
956         char *buf = NULL;
957         size_t len = 0;
958         FILE *fp;
959         struct cpu_map *node_map = NULL;
960         char *c;
961         u32 nr, i, j;
962         int ret = -1;
963
964         fp = fopen("/sys/devices/system/node/online", "r");
965         if (!fp)
966                 return -1;
967
968         if (getline(&buf, &len, fp) <= 0)
969                 goto done;
970
971         c = strchr(buf, '\n');
972         if (c)
973                 *c = '\0';
974
975         node_map = cpu_map__new(buf);
976         if (!node_map)
977                 goto done;
978
979         nr = (u32)node_map->nr;
980
981         ret = do_write(fd, &nr, sizeof(nr));
982         if (ret < 0)
983                 goto done;
984
985         for (i = 0; i < nr; i++) {
986                 j = (u32)node_map->map[i];
987                 ret = do_write(fd, &j, sizeof(j));
988                 if (ret < 0)
989                         break;
990
991                 ret = write_topo_node(fd, i);
992                 if (ret < 0)
993                         break;
994         }
995 done:
996         free(buf);
997         fclose(fp);
998         free(node_map);
999         return ret;
1000 }
1001
1002 /*
1003  * default get_cpuid(): nothing gets recorded
1004  * actual implementation must be in arch/$(ARCH)/util/header.c
1005  */
1006 int __attribute__((weak)) get_cpuid(char *buffer __used, size_t sz __used)
1007 {
1008         return -1;
1009 }
1010
1011 static int write_cpuid(int fd, struct perf_header *h __used,
1012                        struct perf_evlist *evlist __used)
1013 {
1014         char buffer[64];
1015         int ret;
1016
1017         ret = get_cpuid(buffer, sizeof(buffer));
1018         if (!ret)
1019                 goto write_it;
1020
1021         return -1;
1022 write_it:
1023         return do_write_string(fd, buffer);
1024 }
1025
1026 static void print_hostname(struct perf_header *ph, int fd, FILE *fp)
1027 {
1028         char *str = do_read_string(fd, ph);
1029         fprintf(fp, "# hostname : %s\n", str);
1030         free(str);
1031 }
1032
1033 static void print_osrelease(struct perf_header *ph, int fd, FILE *fp)
1034 {
1035         char *str = do_read_string(fd, ph);
1036         fprintf(fp, "# os release : %s\n", str);
1037         free(str);
1038 }
1039
1040 static void print_arch(struct perf_header *ph, int fd, FILE *fp)
1041 {
1042         char *str = do_read_string(fd, ph);
1043         fprintf(fp, "# arch : %s\n", str);
1044         free(str);
1045 }
1046
1047 static void print_cpudesc(struct perf_header *ph, int fd, FILE *fp)
1048 {
1049         char *str = do_read_string(fd, ph);
1050         fprintf(fp, "# cpudesc : %s\n", str);
1051         free(str);
1052 }
1053
1054 static void print_nrcpus(struct perf_header *ph, int fd, FILE *fp)
1055 {
1056         ssize_t ret;
1057         u32 nr;
1058
1059         ret = read(fd, &nr, sizeof(nr));
1060         if (ret != (ssize_t)sizeof(nr))
1061                 nr = -1; /* interpreted as error */
1062
1063         if (ph->needs_swap)
1064                 nr = bswap_32(nr);
1065
1066         fprintf(fp, "# nrcpus online : %u\n", nr);
1067
1068         ret = read(fd, &nr, sizeof(nr));
1069         if (ret != (ssize_t)sizeof(nr))
1070                 nr = -1; /* interpreted as error */
1071
1072         if (ph->needs_swap)
1073                 nr = bswap_32(nr);
1074
1075         fprintf(fp, "# nrcpus avail : %u\n", nr);
1076 }
1077
1078 static void print_version(struct perf_header *ph, int fd, FILE *fp)
1079 {
1080         char *str = do_read_string(fd, ph);
1081         fprintf(fp, "# perf version : %s\n", str);
1082         free(str);
1083 }
1084
1085 static void print_cmdline(struct perf_header *ph, int fd, FILE *fp)
1086 {
1087         ssize_t ret;
1088         char *str;
1089         u32 nr, i;
1090
1091         ret = read(fd, &nr, sizeof(nr));
1092         if (ret != (ssize_t)sizeof(nr))
1093                 return;
1094
1095         if (ph->needs_swap)
1096                 nr = bswap_32(nr);
1097
1098         fprintf(fp, "# cmdline : ");
1099
1100         for (i = 0; i < nr; i++) {
1101                 str = do_read_string(fd, ph);
1102                 fprintf(fp, "%s ", str);
1103                 free(str);
1104         }
1105         fputc('\n', fp);
1106 }
1107
1108 static void print_cpu_topology(struct perf_header *ph, int fd, FILE *fp)
1109 {
1110         ssize_t ret;
1111         u32 nr, i;
1112         char *str;
1113
1114         ret = read(fd, &nr, sizeof(nr));
1115         if (ret != (ssize_t)sizeof(nr))
1116                 return;
1117
1118         if (ph->needs_swap)
1119                 nr = bswap_32(nr);
1120
1121         for (i = 0; i < nr; i++) {
1122                 str = do_read_string(fd, ph);
1123                 fprintf(fp, "# sibling cores   : %s\n", str);
1124                 free(str);
1125         }
1126
1127         ret = read(fd, &nr, sizeof(nr));
1128         if (ret != (ssize_t)sizeof(nr))
1129                 return;
1130
1131         if (ph->needs_swap)
1132                 nr = bswap_32(nr);
1133
1134         for (i = 0; i < nr; i++) {
1135                 str = do_read_string(fd, ph);
1136                 fprintf(fp, "# sibling threads : %s\n", str);
1137                 free(str);
1138         }
1139 }
1140
1141 static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1142 {
1143         struct perf_event_attr attr;
1144         uint64_t id;
1145         void *buf = NULL;
1146         char *str;
1147         u32 nre, sz, nr, i, j, msz;
1148         int ret;
1149
1150         /* number of events */
1151         ret = read(fd, &nre, sizeof(nre));
1152         if (ret != (ssize_t)sizeof(nre))
1153                 goto error;
1154
1155         if (ph->needs_swap)
1156                 nre = bswap_32(nre);
1157
1158         ret = read(fd, &sz, sizeof(sz));
1159         if (ret != (ssize_t)sizeof(sz))
1160                 goto error;
1161
1162         if (ph->needs_swap)
1163                 sz = bswap_32(sz);
1164
1165         /*
1166          * ensure it is at least to our ABI rev
1167          */
1168         if (sz < (u32)sizeof(attr))
1169                 goto error;
1170
1171         memset(&attr, 0, sizeof(attr));
1172
1173         /* read entire region to sync up to next field */
1174         buf = malloc(sz);
1175         if (!buf)
1176                 goto error;
1177
1178         msz = sizeof(attr);
1179         if (sz < msz)
1180                 msz = sz;
1181
1182         for (i = 0 ; i < nre; i++) {
1183
1184                 ret = read(fd, buf, sz);
1185                 if (ret != (ssize_t)sz)
1186                         goto error;
1187
1188                 if (ph->needs_swap)
1189                         perf_event__attr_swap(buf);
1190
1191                 memcpy(&attr, buf, msz);
1192
1193                 ret = read(fd, &nr, sizeof(nr));
1194                 if (ret != (ssize_t)sizeof(nr))
1195                         goto error;
1196
1197                 if (ph->needs_swap)
1198                         nr = bswap_32(nr);
1199
1200                 str = do_read_string(fd, ph);
1201                 fprintf(fp, "# event : name = %s, ", str);
1202                 free(str);
1203
1204                 fprintf(fp, "type = %d, config = 0x%"PRIx64
1205                             ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
1206                                 attr.type,
1207                                 (u64)attr.config,
1208                                 (u64)attr.config1,
1209                                 (u64)attr.config2);
1210
1211                 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
1212                                 attr.exclude_user,
1213                                 attr.exclude_kernel);
1214
1215                 if (nr)
1216                         fprintf(fp, ", id = {");
1217
1218                 for (j = 0 ; j < nr; j++) {
1219                         ret = read(fd, &id, sizeof(id));
1220                         if (ret != (ssize_t)sizeof(id))
1221                                 goto error;
1222
1223                         if (ph->needs_swap)
1224                                 id = bswap_64(id);
1225
1226                         if (j)
1227                                 fputc(',', fp);
1228
1229                         fprintf(fp, " %"PRIu64, id);
1230                 }
1231                 if (nr && j == nr)
1232                         fprintf(fp, " }");
1233                 fputc('\n', fp);
1234         }
1235         free(buf);
1236         return;
1237 error:
1238         fprintf(fp, "# event desc: not available or unable to read\n");
1239 }
1240
1241 static void print_total_mem(struct perf_header *h __used, int fd, FILE *fp)
1242 {
1243         uint64_t mem;
1244         ssize_t ret;
1245
1246         ret = read(fd, &mem, sizeof(mem));
1247         if (ret != sizeof(mem))
1248                 goto error;
1249
1250         if (h->needs_swap)
1251                 mem = bswap_64(mem);
1252
1253         fprintf(fp, "# total memory : %"PRIu64" kB\n", mem);
1254         return;
1255 error:
1256         fprintf(fp, "# total memory : unknown\n");
1257 }
1258
1259 static void print_numa_topology(struct perf_header *h __used, int fd, FILE *fp)
1260 {
1261         ssize_t ret;
1262         u32 nr, c, i;
1263         char *str;
1264         uint64_t mem_total, mem_free;
1265
1266         /* nr nodes */
1267         ret = read(fd, &nr, sizeof(nr));
1268         if (ret != (ssize_t)sizeof(nr))
1269                 goto error;
1270
1271         if (h->needs_swap)
1272                 nr = bswap_32(nr);
1273
1274         for (i = 0; i < nr; i++) {
1275
1276                 /* node number */
1277                 ret = read(fd, &c, sizeof(c));
1278                 if (ret != (ssize_t)sizeof(c))
1279                         goto error;
1280
1281                 if (h->needs_swap)
1282                         c = bswap_32(c);
1283
1284                 ret = read(fd, &mem_total, sizeof(u64));
1285                 if (ret != sizeof(u64))
1286                         goto error;
1287
1288                 ret = read(fd, &mem_free, sizeof(u64));
1289                 if (ret != sizeof(u64))
1290                         goto error;
1291
1292                 if (h->needs_swap) {
1293                         mem_total = bswap_64(mem_total);
1294                         mem_free = bswap_64(mem_free);
1295                 }
1296
1297                 fprintf(fp, "# node%u meminfo  : total = %"PRIu64" kB,"
1298                             " free = %"PRIu64" kB\n",
1299                         c,
1300                         mem_total,
1301                         mem_free);
1302
1303                 str = do_read_string(fd, h);
1304                 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1305                 free(str);
1306         }
1307         return;
1308 error:
1309         fprintf(fp, "# numa topology : not available\n");
1310 }
1311
1312 static void print_cpuid(struct perf_header *ph, int fd, FILE *fp)
1313 {
1314         char *str = do_read_string(fd, ph);
1315         fprintf(fp, "# cpuid : %s\n", str);
1316         free(str);
1317 }
1318
1319 static int __event_process_build_id(struct build_id_event *bev,
1320                                     char *filename,
1321                                     struct perf_session *session)
1322 {
1323         int err = -1;
1324         struct list_head *head;
1325         struct machine *machine;
1326         u16 misc;
1327         struct dso *dso;
1328         enum dso_kernel_type dso_type;
1329
1330         machine = perf_session__findnew_machine(session, bev->pid);
1331         if (!machine)
1332                 goto out;
1333
1334         misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1335
1336         switch (misc) {
1337         case PERF_RECORD_MISC_KERNEL:
1338                 dso_type = DSO_TYPE_KERNEL;
1339                 head = &machine->kernel_dsos;
1340                 break;
1341         case PERF_RECORD_MISC_GUEST_KERNEL:
1342                 dso_type = DSO_TYPE_GUEST_KERNEL;
1343                 head = &machine->kernel_dsos;
1344                 break;
1345         case PERF_RECORD_MISC_USER:
1346         case PERF_RECORD_MISC_GUEST_USER:
1347                 dso_type = DSO_TYPE_USER;
1348                 head = &machine->user_dsos;
1349                 break;
1350         default:
1351                 goto out;
1352         }
1353
1354         dso = __dsos__findnew(head, filename);
1355         if (dso != NULL) {
1356                 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1357
1358                 dso__set_build_id(dso, &bev->build_id);
1359
1360                 if (filename[0] == '[')
1361                         dso->kernel = dso_type;
1362
1363                 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1364                                   sbuild_id);
1365                 pr_debug("build id event received for %s: %s\n",
1366                          dso->long_name, sbuild_id);
1367         }
1368
1369         err = 0;
1370 out:
1371         return err;
1372 }
1373
1374 static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1375                                                  int input, u64 offset, u64 size)
1376 {
1377         struct perf_session *session = container_of(header, struct perf_session, header);
1378         struct {
1379                 struct perf_event_header   header;
1380                 u8                         build_id[ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1381                 char                       filename[0];
1382         } old_bev;
1383         struct build_id_event bev;
1384         char filename[PATH_MAX];
1385         u64 limit = offset + size;
1386
1387         while (offset < limit) {
1388                 ssize_t len;
1389
1390                 if (read(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1391                         return -1;
1392
1393                 if (header->needs_swap)
1394                         perf_event_header__bswap(&old_bev.header);
1395
1396                 len = old_bev.header.size - sizeof(old_bev);
1397                 if (read(input, filename, len) != len)
1398                         return -1;
1399
1400                 bev.header = old_bev.header;
1401
1402                 /*
1403                  * As the pid is the missing value, we need to fill
1404                  * it properly. The header.misc value give us nice hint.
1405                  */
1406                 bev.pid = HOST_KERNEL_ID;
1407                 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1408                     bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1409                         bev.pid = DEFAULT_GUEST_KERNEL_ID;
1410
1411                 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1412                 __event_process_build_id(&bev, filename, session);
1413
1414                 offset += bev.header.size;
1415         }
1416
1417         return 0;
1418 }
1419
1420 static int perf_header__read_build_ids(struct perf_header *header,
1421                                        int input, u64 offset, u64 size)
1422 {
1423         struct perf_session *session = container_of(header, struct perf_session, header);
1424         struct build_id_event bev;
1425         char filename[PATH_MAX];
1426         u64 limit = offset + size, orig_offset = offset;
1427         int err = -1;
1428
1429         while (offset < limit) {
1430                 ssize_t len;
1431
1432                 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
1433                         goto out;
1434
1435                 if (header->needs_swap)
1436                         perf_event_header__bswap(&bev.header);
1437
1438                 len = bev.header.size - sizeof(bev);
1439                 if (read(input, filename, len) != len)
1440                         goto out;
1441                 /*
1442                  * The a1645ce1 changeset:
1443                  *
1444                  * "perf: 'perf kvm' tool for monitoring guest performance from host"
1445                  *
1446                  * Added a field to struct build_id_event that broke the file
1447                  * format.
1448                  *
1449                  * Since the kernel build-id is the first entry, process the
1450                  * table using the old format if the well known
1451                  * '[kernel.kallsyms]' string for the kernel build-id has the
1452                  * first 4 characters chopped off (where the pid_t sits).
1453                  */
1454                 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1455                         if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1456                                 return -1;
1457                         return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1458                 }
1459
1460                 __event_process_build_id(&bev, filename, session);
1461
1462                 offset += bev.header.size;
1463         }
1464         err = 0;
1465 out:
1466         return err;
1467 }
1468
1469 static int process_trace_info(struct perf_file_section *section __unused,
1470                               struct perf_header *ph __unused,
1471                               int feat __unused, int fd)
1472 {
1473         trace_report(fd, false);
1474         return 0;
1475 }
1476
1477 static int process_build_id(struct perf_file_section *section,
1478                             struct perf_header *ph,
1479                             int feat __unused, int fd)
1480 {
1481         if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1482                 pr_debug("Failed to read buildids, continuing...\n");
1483         return 0;
1484 }
1485
1486 struct feature_ops {
1487         int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1488         void (*print)(struct perf_header *h, int fd, FILE *fp);
1489         int (*process)(struct perf_file_section *section,
1490                        struct perf_header *h, int feat, int fd);
1491         const char *name;
1492         bool full_only;
1493 };
1494
1495 #define FEAT_OPA(n, func) \
1496         [n] = { .name = #n, .write = write_##func, .print = print_##func }
1497 #define FEAT_OPP(n, func) \
1498         [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1499                 .process = process_##func }
1500 #define FEAT_OPF(n, func) \
1501         [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1502                 .full_only = true }
1503
1504 /* feature_ops not implemented: */
1505 #define print_trace_info                NULL
1506 #define print_build_id                  NULL
1507
1508 static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
1509         FEAT_OPP(HEADER_TRACE_INFO,     trace_info),
1510         FEAT_OPP(HEADER_BUILD_ID,       build_id),
1511         FEAT_OPA(HEADER_HOSTNAME,       hostname),
1512         FEAT_OPA(HEADER_OSRELEASE,      osrelease),
1513         FEAT_OPA(HEADER_VERSION,        version),
1514         FEAT_OPA(HEADER_ARCH,           arch),
1515         FEAT_OPA(HEADER_NRCPUS,         nrcpus),
1516         FEAT_OPA(HEADER_CPUDESC,        cpudesc),
1517         FEAT_OPA(HEADER_CPUID,          cpuid),
1518         FEAT_OPA(HEADER_TOTAL_MEM,      total_mem),
1519         FEAT_OPA(HEADER_EVENT_DESC,     event_desc),
1520         FEAT_OPA(HEADER_CMDLINE,        cmdline),
1521         FEAT_OPF(HEADER_CPU_TOPOLOGY,   cpu_topology),
1522         FEAT_OPF(HEADER_NUMA_TOPOLOGY,  numa_topology),
1523 };
1524
1525 struct header_print_data {
1526         FILE *fp;
1527         bool full; /* extended list of headers */
1528 };
1529
1530 static int perf_file_section__fprintf_info(struct perf_file_section *section,
1531                                            struct perf_header *ph,
1532                                            int feat, int fd, void *data)
1533 {
1534         struct header_print_data *hd = data;
1535
1536         if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1537                 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1538                                 "%d, continuing...\n", section->offset, feat);
1539                 return 0;
1540         }
1541         if (feat >= HEADER_LAST_FEATURE) {
1542                 pr_warning("unknown feature %d\n", feat);
1543                 return 0;
1544         }
1545         if (!feat_ops[feat].print)
1546                 return 0;
1547
1548         if (!feat_ops[feat].full_only || hd->full)
1549                 feat_ops[feat].print(ph, fd, hd->fp);
1550         else
1551                 fprintf(hd->fp, "# %s info available, use -I to display\n",
1552                         feat_ops[feat].name);
1553
1554         return 0;
1555 }
1556
1557 int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
1558 {
1559         struct header_print_data hd;
1560         struct perf_header *header = &session->header;
1561         int fd = session->fd;
1562         hd.fp = fp;
1563         hd.full = full;
1564
1565         perf_header__process_sections(header, fd, &hd,
1566                                       perf_file_section__fprintf_info);
1567         return 0;
1568 }
1569
1570 static int do_write_feat(int fd, struct perf_header *h, int type,
1571                          struct perf_file_section **p,
1572                          struct perf_evlist *evlist)
1573 {
1574         int err;
1575         int ret = 0;
1576
1577         if (perf_header__has_feat(h, type)) {
1578                 if (!feat_ops[type].write)
1579                         return -1;
1580
1581                 (*p)->offset = lseek(fd, 0, SEEK_CUR);
1582
1583                 err = feat_ops[type].write(fd, h, evlist);
1584                 if (err < 0) {
1585                         pr_debug("failed to write feature %d\n", type);
1586
1587                         /* undo anything written */
1588                         lseek(fd, (*p)->offset, SEEK_SET);
1589
1590                         return -1;
1591                 }
1592                 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
1593                 (*p)++;
1594         }
1595         return ret;
1596 }
1597
1598 static int perf_header__adds_write(struct perf_header *header,
1599                                    struct perf_evlist *evlist, int fd)
1600 {
1601         int nr_sections;
1602         struct perf_file_section *feat_sec, *p;
1603         int sec_size;
1604         u64 sec_start;
1605         int feat;
1606         int err;
1607
1608         nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
1609         if (!nr_sections)
1610                 return 0;
1611
1612         feat_sec = p = calloc(sizeof(*feat_sec), nr_sections);
1613         if (feat_sec == NULL)
1614                 return -ENOMEM;
1615
1616         sec_size = sizeof(*feat_sec) * nr_sections;
1617
1618         sec_start = header->data_offset + header->data_size;
1619         lseek(fd, sec_start + sec_size, SEEK_SET);
1620
1621         for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
1622                 if (do_write_feat(fd, header, feat, &p, evlist))
1623                         perf_header__clear_feat(header, feat);
1624         }
1625
1626         lseek(fd, sec_start, SEEK_SET);
1627         /*
1628          * may write more than needed due to dropped feature, but
1629          * this is okay, reader will skip the mising entries
1630          */
1631         err = do_write(fd, feat_sec, sec_size);
1632         if (err < 0)
1633                 pr_debug("failed to write feature section\n");
1634         free(feat_sec);
1635         return err;
1636 }
1637
1638 int perf_header__write_pipe(int fd)
1639 {
1640         struct perf_pipe_file_header f_header;
1641         int err;
1642
1643         f_header = (struct perf_pipe_file_header){
1644                 .magic     = PERF_MAGIC,
1645                 .size      = sizeof(f_header),
1646         };
1647
1648         err = do_write(fd, &f_header, sizeof(f_header));
1649         if (err < 0) {
1650                 pr_debug("failed to write perf pipe header\n");
1651                 return err;
1652         }
1653
1654         return 0;
1655 }
1656
1657 int perf_session__write_header(struct perf_session *session,
1658                                struct perf_evlist *evlist,
1659                                int fd, bool at_exit)
1660 {
1661         struct perf_file_header f_header;
1662         struct perf_file_attr   f_attr;
1663         struct perf_header *header = &session->header;
1664         struct perf_evsel *attr, *pair = NULL;
1665         int err;
1666
1667         lseek(fd, sizeof(f_header), SEEK_SET);
1668
1669         if (session->evlist != evlist)
1670                 pair = list_entry(session->evlist->entries.next, struct perf_evsel, node);
1671
1672         list_for_each_entry(attr, &evlist->entries, node) {
1673                 attr->id_offset = lseek(fd, 0, SEEK_CUR);
1674                 err = do_write(fd, attr->id, attr->ids * sizeof(u64));
1675                 if (err < 0) {
1676 out_err_write:
1677                         pr_debug("failed to write perf header\n");
1678                         return err;
1679                 }
1680                 if (session->evlist != evlist) {
1681                         err = do_write(fd, pair->id, pair->ids * sizeof(u64));
1682                         if (err < 0)
1683                                 goto out_err_write;
1684                         attr->ids += pair->ids;
1685                         pair = list_entry(pair->node.next, struct perf_evsel, node);
1686                 }
1687         }
1688
1689         header->attr_offset = lseek(fd, 0, SEEK_CUR);
1690
1691         list_for_each_entry(attr, &evlist->entries, node) {
1692                 f_attr = (struct perf_file_attr){
1693                         .attr = attr->attr,
1694                         .ids  = {
1695                                 .offset = attr->id_offset,
1696                                 .size   = attr->ids * sizeof(u64),
1697                         }
1698                 };
1699                 err = do_write(fd, &f_attr, sizeof(f_attr));
1700                 if (err < 0) {
1701                         pr_debug("failed to write perf header attribute\n");
1702                         return err;
1703                 }
1704         }
1705
1706         header->event_offset = lseek(fd, 0, SEEK_CUR);
1707         header->event_size = event_count * sizeof(struct perf_trace_event_type);
1708         if (events) {
1709                 err = do_write(fd, events, header->event_size);
1710                 if (err < 0) {
1711                         pr_debug("failed to write perf header events\n");
1712                         return err;
1713                 }
1714         }
1715
1716         header->data_offset = lseek(fd, 0, SEEK_CUR);
1717
1718         if (at_exit) {
1719                 err = perf_header__adds_write(header, evlist, fd);
1720                 if (err < 0)
1721                         return err;
1722         }
1723
1724         f_header = (struct perf_file_header){
1725                 .magic     = PERF_MAGIC,
1726                 .size      = sizeof(f_header),
1727                 .attr_size = sizeof(f_attr),
1728                 .attrs = {
1729                         .offset = header->attr_offset,
1730                         .size   = evlist->nr_entries * sizeof(f_attr),
1731                 },
1732                 .data = {
1733                         .offset = header->data_offset,
1734                         .size   = header->data_size,
1735                 },
1736                 .event_types = {
1737                         .offset = header->event_offset,
1738                         .size   = header->event_size,
1739                 },
1740         };
1741
1742         memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
1743
1744         lseek(fd, 0, SEEK_SET);
1745         err = do_write(fd, &f_header, sizeof(f_header));
1746         if (err < 0) {
1747                 pr_debug("failed to write perf header\n");
1748                 return err;
1749         }
1750         lseek(fd, header->data_offset + header->data_size, SEEK_SET);
1751
1752         header->frozen = 1;
1753         return 0;
1754 }
1755
1756 static int perf_header__getbuffer64(struct perf_header *header,
1757                                     int fd, void *buf, size_t size)
1758 {
1759         if (readn(fd, buf, size) <= 0)
1760                 return -1;
1761
1762         if (header->needs_swap)
1763                 mem_bswap_64(buf, size);
1764
1765         return 0;
1766 }
1767
1768 int perf_header__process_sections(struct perf_header *header, int fd,
1769                                   void *data,
1770                                   int (*process)(struct perf_file_section *section,
1771                                                  struct perf_header *ph,
1772                                                  int feat, int fd, void *data))
1773 {
1774         struct perf_file_section *feat_sec, *sec;
1775         int nr_sections;
1776         int sec_size;
1777         int feat;
1778         int err;
1779
1780         nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
1781         if (!nr_sections)
1782                 return 0;
1783
1784         feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections);
1785         if (!feat_sec)
1786                 return -1;
1787
1788         sec_size = sizeof(*feat_sec) * nr_sections;
1789
1790         lseek(fd, header->data_offset + header->data_size, SEEK_SET);
1791
1792         err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
1793         if (err < 0)
1794                 goto out_free;
1795
1796         for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
1797                 err = process(sec++, header, feat, fd, data);
1798                 if (err < 0)
1799                         goto out_free;
1800         }
1801         err = 0;
1802 out_free:
1803         free(feat_sec);
1804         return err;
1805 }
1806
1807 static int check_magic_endian(u64 *magic, struct perf_file_header *header,
1808                               struct perf_header *ph)
1809 {
1810         int ret;
1811
1812         /* check for legacy format */
1813         ret = memcmp(magic, __perf_magic1, sizeof(*magic));
1814         if (ret == 0) {
1815                 pr_debug("legacy perf.data format\n");
1816                 if (!header)
1817                         return -1;
1818
1819                 if (header->attr_size != sizeof(struct perf_file_attr)) {
1820                         u64 attr_size = bswap_64(header->attr_size);
1821
1822                         if (attr_size != sizeof(struct perf_file_attr))
1823                                 return -1;
1824
1825                         ph->needs_swap = true;
1826                 }
1827                 return 0;
1828         }
1829
1830         /* check magic number with same endianness */
1831         if (*magic == __perf_magic2)
1832                 return 0;
1833
1834         /* check magic number but opposite endianness */
1835         if (*magic != __perf_magic2_sw)
1836                 return -1;
1837
1838         ph->needs_swap = true;
1839
1840         return 0;
1841 }
1842
1843 int perf_file_header__read(struct perf_file_header *header,
1844                            struct perf_header *ph, int fd)
1845 {
1846         int ret;
1847
1848         lseek(fd, 0, SEEK_SET);
1849
1850         ret = readn(fd, header, sizeof(*header));
1851         if (ret <= 0)
1852                 return -1;
1853
1854         if (check_magic_endian(&header->magic, header, ph) < 0)
1855                 return -1;
1856
1857         if (ph->needs_swap) {
1858                 mem_bswap_64(header, offsetof(struct perf_file_header,
1859                              adds_features));
1860         }
1861
1862         if (header->size != sizeof(*header)) {
1863                 /* Support the previous format */
1864                 if (header->size == offsetof(typeof(*header), adds_features))
1865                         bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
1866                 else
1867                         return -1;
1868         } else if (ph->needs_swap) {
1869                 unsigned int i;
1870                 /*
1871                  * feature bitmap is declared as an array of unsigned longs --
1872                  * not good since its size can differ between the host that
1873                  * generated the data file and the host analyzing the file.
1874                  *
1875                  * We need to handle endianness, but we don't know the size of
1876                  * the unsigned long where the file was generated. Take a best
1877                  * guess at determining it: try 64-bit swap first (ie., file
1878                  * created on a 64-bit host), and check if the hostname feature
1879                  * bit is set (this feature bit is forced on as of fbe96f2).
1880                  * If the bit is not, undo the 64-bit swap and try a 32-bit
1881                  * swap. If the hostname bit is still not set (e.g., older data
1882                  * file), punt and fallback to the original behavior --
1883                  * clearing all feature bits and setting buildid.
1884                  */
1885                 for (i = 0; i < BITS_TO_LONGS(HEADER_FEAT_BITS); ++i)
1886                         header->adds_features[i] = bswap_64(header->adds_features[i]);
1887
1888                 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
1889                         for (i = 0; i < BITS_TO_LONGS(HEADER_FEAT_BITS); ++i) {
1890                                 header->adds_features[i] = bswap_64(header->adds_features[i]);
1891                                 header->adds_features[i] = bswap_32(header->adds_features[i]);
1892                         }
1893                 }
1894
1895                 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
1896                         bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
1897                         set_bit(HEADER_BUILD_ID, header->adds_features);
1898                 }
1899         }
1900
1901         memcpy(&ph->adds_features, &header->adds_features,
1902                sizeof(ph->adds_features));
1903
1904         ph->event_offset = header->event_types.offset;
1905         ph->event_size   = header->event_types.size;
1906         ph->data_offset  = header->data.offset;
1907         ph->data_size    = header->data.size;
1908         return 0;
1909 }
1910
1911 static int perf_file_section__process(struct perf_file_section *section,
1912                                       struct perf_header *ph,
1913                                       int feat, int fd, void *data __used)
1914 {
1915         if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1916                 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1917                           "%d, continuing...\n", section->offset, feat);
1918                 return 0;
1919         }
1920
1921         if (feat >= HEADER_LAST_FEATURE) {
1922                 pr_debug("unknown feature %d, continuing...\n", feat);
1923                 return 0;
1924         }
1925
1926         if (!feat_ops[feat].process)
1927                 return 0;
1928
1929         return feat_ops[feat].process(section, ph, feat, fd);
1930 }
1931
1932 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
1933                                        struct perf_header *ph, int fd,
1934                                        bool repipe)
1935 {
1936         int ret;
1937
1938         ret = readn(fd, header, sizeof(*header));
1939         if (ret <= 0)
1940                 return -1;
1941
1942          if (check_magic_endian(&header->magic, NULL, ph) < 0)
1943                 return -1;
1944
1945         if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
1946                 return -1;
1947
1948         if (header->size != sizeof(*header)) {
1949                 u64 size = bswap_64(header->size);
1950
1951                 if (size != sizeof(*header))
1952                         return -1;
1953
1954                 ph->needs_swap = true;
1955         }
1956
1957         return 0;
1958 }
1959
1960 static int perf_header__read_pipe(struct perf_session *session, int fd)
1961 {
1962         struct perf_header *header = &session->header;
1963         struct perf_pipe_file_header f_header;
1964
1965         if (perf_file_header__read_pipe(&f_header, header, fd,
1966                                         session->repipe) < 0) {
1967                 pr_debug("incompatible file format\n");
1968                 return -EINVAL;
1969         }
1970
1971         session->fd = fd;
1972
1973         return 0;
1974 }
1975
1976 int perf_session__read_header(struct perf_session *session, int fd)
1977 {
1978         struct perf_header *header = &session->header;
1979         struct perf_file_header f_header;
1980         struct perf_file_attr   f_attr;
1981         u64                     f_id;
1982         int nr_attrs, nr_ids, i, j;
1983
1984         session->evlist = perf_evlist__new(NULL, NULL);
1985         if (session->evlist == NULL)
1986                 return -ENOMEM;
1987
1988         if (session->fd_pipe)
1989                 return perf_header__read_pipe(session, fd);
1990
1991         if (perf_file_header__read(&f_header, header, fd) < 0) {
1992                 pr_debug("incompatible file format\n");
1993                 return -EINVAL;
1994         }
1995
1996         nr_attrs = f_header.attrs.size / sizeof(f_attr);
1997         lseek(fd, f_header.attrs.offset, SEEK_SET);
1998
1999         for (i = 0; i < nr_attrs; i++) {
2000                 struct perf_evsel *evsel;
2001                 off_t tmp;
2002
2003                 if (readn(fd, &f_attr, sizeof(f_attr)) <= 0)
2004                         goto out_errno;
2005
2006                 if (header->needs_swap)
2007                         perf_event__attr_swap(&f_attr.attr);
2008
2009                 tmp = lseek(fd, 0, SEEK_CUR);
2010                 evsel = perf_evsel__new(&f_attr.attr, i);
2011
2012                 if (evsel == NULL)
2013                         goto out_delete_evlist;
2014                 /*
2015                  * Do it before so that if perf_evsel__alloc_id fails, this
2016                  * entry gets purged too at perf_evlist__delete().
2017                  */
2018                 perf_evlist__add(session->evlist, evsel);
2019
2020                 nr_ids = f_attr.ids.size / sizeof(u64);
2021                 /*
2022                  * We don't have the cpu and thread maps on the header, so
2023                  * for allocating the perf_sample_id table we fake 1 cpu and
2024                  * hattr->ids threads.
2025                  */
2026                 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2027                         goto out_delete_evlist;
2028
2029                 lseek(fd, f_attr.ids.offset, SEEK_SET);
2030
2031                 for (j = 0; j < nr_ids; j++) {
2032                         if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
2033                                 goto out_errno;
2034
2035                         perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
2036                 }
2037
2038                 lseek(fd, tmp, SEEK_SET);
2039         }
2040
2041         symbol_conf.nr_events = nr_attrs;
2042
2043         if (f_header.event_types.size) {
2044                 lseek(fd, f_header.event_types.offset, SEEK_SET);
2045                 events = malloc(f_header.event_types.size);
2046                 if (events == NULL)
2047                         return -ENOMEM;
2048                 if (perf_header__getbuffer64(header, fd, events,
2049                                              f_header.event_types.size))
2050                         goto out_errno;
2051                 event_count =  f_header.event_types.size / sizeof(struct perf_trace_event_type);
2052         }
2053
2054         perf_header__process_sections(header, fd, NULL,
2055                                       perf_file_section__process);
2056
2057         lseek(fd, header->data_offset, SEEK_SET);
2058
2059         header->frozen = 1;
2060         return 0;
2061 out_errno:
2062         return -errno;
2063
2064 out_delete_evlist:
2065         perf_evlist__delete(session->evlist);
2066         session->evlist = NULL;
2067         return -ENOMEM;
2068 }
2069
2070 int perf_event__synthesize_attr(struct perf_tool *tool,
2071                                 struct perf_event_attr *attr, u16 ids, u64 *id,
2072                                 perf_event__handler_t process)
2073 {
2074         union perf_event *ev;
2075         size_t size;
2076         int err;
2077
2078         size = sizeof(struct perf_event_attr);
2079         size = ALIGN(size, sizeof(u64));
2080         size += sizeof(struct perf_event_header);
2081         size += ids * sizeof(u64);
2082
2083         ev = malloc(size);
2084
2085         if (ev == NULL)
2086                 return -ENOMEM;
2087
2088         ev->attr.attr = *attr;
2089         memcpy(ev->attr.id, id, ids * sizeof(u64));
2090
2091         ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2092         ev->attr.header.size = size;
2093
2094         err = process(tool, ev, NULL, NULL);
2095
2096         free(ev);
2097
2098         return err;
2099 }
2100
2101 int perf_event__synthesize_attrs(struct perf_tool *tool,
2102                                    struct perf_session *session,
2103                                    perf_event__handler_t process)
2104 {
2105         struct perf_evsel *attr;
2106         int err = 0;
2107
2108         list_for_each_entry(attr, &session->evlist->entries, node) {
2109                 err = perf_event__synthesize_attr(tool, &attr->attr, attr->ids,
2110                                                   attr->id, process);
2111                 if (err) {
2112                         pr_debug("failed to create perf header attribute\n");
2113                         return err;
2114                 }
2115         }
2116
2117         return err;
2118 }
2119
2120 int perf_event__process_attr(union perf_event *event,
2121                              struct perf_evlist **pevlist)
2122 {
2123         unsigned int i, ids, n_ids;
2124         struct perf_evsel *evsel;
2125         struct perf_evlist *evlist = *pevlist;
2126
2127         if (evlist == NULL) {
2128                 *pevlist = evlist = perf_evlist__new(NULL, NULL);
2129                 if (evlist == NULL)
2130                         return -ENOMEM;
2131         }
2132
2133         evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
2134         if (evsel == NULL)
2135                 return -ENOMEM;
2136
2137         perf_evlist__add(evlist, evsel);
2138
2139         ids = event->header.size;
2140         ids -= (void *)&event->attr.id - (void *)event;
2141         n_ids = ids / sizeof(u64);
2142         /*
2143          * We don't have the cpu and thread maps on the header, so
2144          * for allocating the perf_sample_id table we fake 1 cpu and
2145          * hattr->ids threads.
2146          */
2147         if (perf_evsel__alloc_id(evsel, 1, n_ids))
2148                 return -ENOMEM;
2149
2150         for (i = 0; i < n_ids; i++) {
2151                 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
2152         }
2153
2154         return 0;
2155 }
2156
2157 int perf_event__synthesize_event_type(struct perf_tool *tool,
2158                                       u64 event_id, char *name,
2159                                       perf_event__handler_t process,
2160                                       struct machine *machine)
2161 {
2162         union perf_event ev;
2163         size_t size = 0;
2164         int err = 0;
2165
2166         memset(&ev, 0, sizeof(ev));
2167
2168         ev.event_type.event_type.event_id = event_id;
2169         memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
2170         strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
2171
2172         ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
2173         size = strlen(ev.event_type.event_type.name);
2174         size = ALIGN(size, sizeof(u64));
2175         ev.event_type.header.size = sizeof(ev.event_type) -
2176                 (sizeof(ev.event_type.event_type.name) - size);
2177
2178         err = process(tool, &ev, NULL, machine);
2179
2180         return err;
2181 }
2182
2183 int perf_event__synthesize_event_types(struct perf_tool *tool,
2184                                        perf_event__handler_t process,
2185                                        struct machine *machine)
2186 {
2187         struct perf_trace_event_type *type;
2188         int i, err = 0;
2189
2190         for (i = 0; i < event_count; i++) {
2191                 type = &events[i];
2192
2193                 err = perf_event__synthesize_event_type(tool, type->event_id,
2194                                                         type->name, process,
2195                                                         machine);
2196                 if (err) {
2197                         pr_debug("failed to create perf header event type\n");
2198                         return err;
2199                 }
2200         }
2201
2202         return err;
2203 }
2204
2205 int perf_event__process_event_type(struct perf_tool *tool __unused,
2206                                    union perf_event *event)
2207 {
2208         if (perf_header__push_event(event->event_type.event_type.event_id,
2209                                     event->event_type.event_type.name) < 0)
2210                 return -ENOMEM;
2211
2212         return 0;
2213 }
2214
2215 int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
2216                                         struct perf_evlist *evlist,
2217                                         perf_event__handler_t process)
2218 {
2219         union perf_event ev;
2220         struct tracing_data *tdata;
2221         ssize_t size = 0, aligned_size = 0, padding;
2222         int err __used = 0;
2223
2224         /*
2225          * We are going to store the size of the data followed
2226          * by the data contents. Since the fd descriptor is a pipe,
2227          * we cannot seek back to store the size of the data once
2228          * we know it. Instead we:
2229          *
2230          * - write the tracing data to the temp file
2231          * - get/write the data size to pipe
2232          * - write the tracing data from the temp file
2233          *   to the pipe
2234          */
2235         tdata = tracing_data_get(&evlist->entries, fd, true);
2236         if (!tdata)
2237                 return -1;
2238
2239         memset(&ev, 0, sizeof(ev));
2240
2241         ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
2242         size = tdata->size;
2243         aligned_size = ALIGN(size, sizeof(u64));
2244         padding = aligned_size - size;
2245         ev.tracing_data.header.size = sizeof(ev.tracing_data);
2246         ev.tracing_data.size = aligned_size;
2247
2248         process(tool, &ev, NULL, NULL);
2249
2250         /*
2251          * The put function will copy all the tracing data
2252          * stored in temp file to the pipe.
2253          */
2254         tracing_data_put(tdata);
2255
2256         write_padded(fd, NULL, 0, padding);
2257
2258         return aligned_size;
2259 }
2260
2261 int perf_event__process_tracing_data(union perf_event *event,
2262                                      struct perf_session *session)
2263 {
2264         ssize_t size_read, padding, size = event->tracing_data.size;
2265         off_t offset = lseek(session->fd, 0, SEEK_CUR);
2266         char buf[BUFSIZ];
2267
2268         /* setup for reading amidst mmap */
2269         lseek(session->fd, offset + sizeof(struct tracing_data_event),
2270               SEEK_SET);
2271
2272         size_read = trace_report(session->fd, session->repipe);
2273
2274         padding = ALIGN(size_read, sizeof(u64)) - size_read;
2275
2276         if (read(session->fd, buf, padding) < 0)
2277                 die("reading input file");
2278         if (session->repipe) {
2279                 int retw = write(STDOUT_FILENO, buf, padding);
2280                 if (retw <= 0 || retw != padding)
2281                         die("repiping tracing data padding");
2282         }
2283
2284         if (size_read + padding != size)
2285                 die("tracing data size mismatch");
2286
2287         return size_read + padding;
2288 }
2289
2290 int perf_event__synthesize_build_id(struct perf_tool *tool,
2291                                     struct dso *pos, u16 misc,
2292                                     perf_event__handler_t process,
2293                                     struct machine *machine)
2294 {
2295         union perf_event ev;
2296         size_t len;
2297         int err = 0;
2298
2299         if (!pos->hit)
2300                 return err;
2301
2302         memset(&ev, 0, sizeof(ev));
2303
2304         len = pos->long_name_len + 1;
2305         len = ALIGN(len, NAME_ALIGN);
2306         memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2307         ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2308         ev.build_id.header.misc = misc;
2309         ev.build_id.pid = machine->pid;
2310         ev.build_id.header.size = sizeof(ev.build_id) + len;
2311         memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2312
2313         err = process(tool, &ev, NULL, machine);
2314
2315         return err;
2316 }
2317
2318 int perf_event__process_build_id(struct perf_tool *tool __used,
2319                                  union perf_event *event,
2320                                  struct perf_session *session)
2321 {
2322         __event_process_build_id(&event->build_id,
2323                                  event->build_id.filename,
2324                                  session);
2325         return 0;
2326 }
2327
2328 void disable_buildid_cache(void)
2329 {
2330         no_buildid_cache = true;
2331 }