blob: 63aad3109e347544d8b5eac2d974247e6763e879 [file] [log] [blame]
Xiao Guangrongb8f46c52010-02-03 11:53:14 +08001#define _FILE_OFFSET_BITS 64
2
Arnaldo Carvalho de Meloa9072bc2011-10-26 12:41:38 -02003#include "util.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02004#include <sys/types.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02005#include <byteswap.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02006#include <unistd.h>
7#include <stdio.h>
8#include <stdlib.h>
Frederic Weisbecker8671dab2009-11-11 04:51:03 +01009#include <linux/list.h>
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -020010#include <linux/kernel.h>
Robert Richterb1e5a9b2011-12-07 10:02:57 +010011#include <linux/bitops.h>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020012#include <sys/utsname.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020013
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020014#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030015#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020016#include "header.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020017#include "../perf.h"
18#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020019#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010020#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010021#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020022#include "cpumap.h"
Robert Richter50a96672012-08-16 21:10:24 +020023#include "pmu.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020024
Stephane Eraniana1ac1d32010-06-17 11:39:01 +020025static bool no_buildid_cache = false;
26
Robert Richterdb146f02012-08-16 21:10:20 +020027static int trace_event_count;
28static struct perf_trace_event_type *trace_events;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020029
Stephane Eranianfbe96f22011-09-30 15:40:40 +020030static u32 header_argc;
31static const char **header_argv;
32
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -020033int perf_header__push_event(u64 id, const char *name)
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020034{
Ulrich Drepperf3054c72012-04-23 09:04:29 -040035 struct perf_trace_event_type *nevents;
36
Arjan van de Venec60a3f2009-09-19 13:36:30 +020037 if (strlen(name) > MAX_EVENT_NAME)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020038 pr_warning("Event %s will be truncated\n", name);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020039
Robert Richterdb146f02012-08-16 21:10:20 +020040 nevents = realloc(trace_events, (trace_event_count + 1) * sizeof(*trace_events));
Ulrich Drepperf3054c72012-04-23 09:04:29 -040041 if (nevents == NULL)
42 return -ENOMEM;
Robert Richterdb146f02012-08-16 21:10:20 +020043 trace_events = nevents;
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -020044
Robert Richterdb146f02012-08-16 21:10:20 +020045 memset(&trace_events[trace_event_count], 0, sizeof(struct perf_trace_event_type));
46 trace_events[trace_event_count].event_id = id;
47 strncpy(trace_events[trace_event_count].name, name, MAX_EVENT_NAME - 1);
48 trace_event_count++;
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -020049 return 0;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020050}
51
52char *perf_header__find_event(u64 id)
53{
54 int i;
Robert Richterdb146f02012-08-16 21:10:20 +020055 for (i = 0 ; i < trace_event_count; i++) {
56 if (trace_events[i].event_id == id)
57 return trace_events[i].name;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020058 }
59 return NULL;
60}
61
Stephane Eranian73323f52012-02-02 13:54:44 +010062/*
63 * magic2 = "PERFILE2"
64 * must be a numerical value to let the endianness
65 * determine the memory layout. That way we are able
66 * to detect endianness when reading the perf.data file
67 * back.
68 *
69 * we check for legacy (PERFFILE) format.
70 */
71static const char *__perf_magic1 = "PERFFILE";
72static const u64 __perf_magic2 = 0x32454c4946524550ULL;
73static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020074
Stephane Eranian73323f52012-02-02 13:54:44 +010075#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020076
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020077struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020078 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020079 struct perf_file_section ids;
80};
81
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030082void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020083{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030084 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020085}
86
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030087void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020088{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030089 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020090}
91
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030092bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020093{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030094 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020095}
96
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020097static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020098{
99 while (size) {
100 int ret = write(fd, buf, size);
101
102 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200103 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200104
105 size -= ret;
106 buf += ret;
107 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200108
109 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200110}
111
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200112#define NAME_ALIGN 64
113
114static int write_padded(int fd, const void *bf, size_t count,
115 size_t count_aligned)
116{
117 static const char zero_buf[NAME_ALIGN];
118 int err = do_write(fd, bf, count);
119
120 if (!err)
121 err = do_write(fd, zero_buf, count_aligned - count);
122
123 return err;
124}
125
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200126static int do_write_string(int fd, const char *str)
127{
128 u32 len, olen;
129 int ret;
130
131 olen = strlen(str) + 1;
132 len = ALIGN(olen, NAME_ALIGN);
133
134 /* write len, incl. \0 */
135 ret = do_write(fd, &len, sizeof(len));
136 if (ret < 0)
137 return ret;
138
139 return write_padded(fd, str, olen, len);
140}
141
142static char *do_read_string(int fd, struct perf_header *ph)
143{
144 ssize_t sz, ret;
145 u32 len;
146 char *buf;
147
148 sz = read(fd, &len, sizeof(len));
149 if (sz < (ssize_t)sizeof(len))
150 return NULL;
151
152 if (ph->needs_swap)
153 len = bswap_32(len);
154
155 buf = malloc(len);
156 if (!buf)
157 return NULL;
158
159 ret = read(fd, buf, len);
160 if (ret == (ssize_t)len) {
161 /*
162 * strings are padded by zeroes
163 * thus the actual strlen of buf
164 * may be less than len
165 */
166 return buf;
167 }
168
169 free(buf);
170 return NULL;
171}
172
173int
174perf_header__set_cmdline(int argc, const char **argv)
175{
176 int i;
177
David Ahern56e6f602012-07-29 20:53:51 -0600178 /*
179 * If header_argv has already been set, do not override it.
180 * This allows a command to set the cmdline, parse args and
181 * then call another builtin function that implements a
182 * command -- e.g, cmd_kvm calling cmd_record.
183 */
184 if (header_argv)
185 return 0;
186
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200187 header_argc = (u32)argc;
188
189 /* do not include NULL termination */
190 header_argv = calloc(argc, sizeof(char *));
191 if (!header_argv)
192 return -ENOMEM;
193
194 /*
195 * must copy argv contents because it gets moved
196 * around during option parsing
197 */
198 for (i = 0; i < argc ; i++)
199 header_argv[i] = argv[i];
200
201 return 0;
202}
203
Robert Richter1b549502011-12-07 10:02:53 +0100204#define dsos__for_each_with_build_id(pos, head) \
205 list_for_each_entry(pos, head, node) \
206 if (!pos->has_build_id) \
207 continue; \
208 else
209
210static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
211 u16 misc, int fd)
212{
213 struct dso *pos;
214
215 dsos__for_each_with_build_id(pos, head) {
216 int err;
217 struct build_id_event b;
218 size_t len;
219
220 if (!pos->hit)
221 continue;
222 len = pos->long_name_len + 1;
223 len = ALIGN(len, NAME_ALIGN);
224 memset(&b, 0, sizeof(b));
225 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
226 b.pid = pid;
227 b.header.misc = misc;
228 b.header.size = sizeof(b) + len;
229 err = do_write(fd, &b, sizeof(b));
230 if (err < 0)
231 return err;
232 err = write_padded(fd, pos->long_name,
233 pos->long_name_len + 1, len);
234 if (err < 0)
235 return err;
236 }
237
238 return 0;
239}
240
241static int machine__write_buildid_table(struct machine *machine, int fd)
242{
243 int err;
244 u16 kmisc = PERF_RECORD_MISC_KERNEL,
245 umisc = PERF_RECORD_MISC_USER;
246
247 if (!machine__is_host(machine)) {
248 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
249 umisc = PERF_RECORD_MISC_GUEST_USER;
250 }
251
252 err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid,
253 kmisc, fd);
254 if (err == 0)
255 err = __dsos__write_buildid_table(&machine->user_dsos,
256 machine->pid, umisc, fd);
257 return err;
258}
259
260static int dsos__write_buildid_table(struct perf_header *header, int fd)
261{
262 struct perf_session *session = container_of(header,
263 struct perf_session, header);
264 struct rb_node *nd;
265 int err = machine__write_buildid_table(&session->host_machine, fd);
266
267 if (err)
268 return err;
269
270 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
271 struct machine *pos = rb_entry(nd, struct machine, rb_node);
272 err = machine__write_buildid_table(pos, fd);
273 if (err)
274 break;
275 }
276 return err;
277}
278
279int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
280 const char *name, bool is_kallsyms)
281{
282 const size_t size = PATH_MAX;
283 char *realname, *filename = zalloc(size),
284 *linkname = zalloc(size), *targetname;
285 int len, err = -1;
286
287 if (is_kallsyms) {
288 if (symbol_conf.kptr_restrict) {
289 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
290 return 0;
291 }
292 realname = (char *)name;
293 } else
294 realname = realpath(name, NULL);
295
296 if (realname == NULL || filename == NULL || linkname == NULL)
297 goto out_free;
298
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300299 len = scnprintf(filename, size, "%s%s%s",
Robert Richter1b549502011-12-07 10:02:53 +0100300 debugdir, is_kallsyms ? "/" : "", realname);
301 if (mkdir_p(filename, 0755))
302 goto out_free;
303
Namhyung Kimafda0f92012-05-01 23:19:36 +0900304 snprintf(filename + len, size - len, "/%s", sbuild_id);
Robert Richter1b549502011-12-07 10:02:53 +0100305
306 if (access(filename, F_OK)) {
307 if (is_kallsyms) {
308 if (copyfile("/proc/kallsyms", filename))
309 goto out_free;
310 } else if (link(realname, filename) && copyfile(name, filename))
311 goto out_free;
312 }
313
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300314 len = scnprintf(linkname, size, "%s/.build-id/%.2s",
Robert Richter1b549502011-12-07 10:02:53 +0100315 debugdir, sbuild_id);
316
317 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
318 goto out_free;
319
320 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
321 targetname = filename + strlen(debugdir) - 5;
322 memcpy(targetname, "../..", 5);
323
324 if (symlink(targetname, linkname) == 0)
325 err = 0;
326out_free:
327 if (!is_kallsyms)
328 free(realname);
329 free(filename);
330 free(linkname);
331 return err;
332}
333
334static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
335 const char *name, const char *debugdir,
336 bool is_kallsyms)
337{
338 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
339
340 build_id__sprintf(build_id, build_id_size, sbuild_id);
341
342 return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms);
343}
344
345int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
346{
347 const size_t size = PATH_MAX;
348 char *filename = zalloc(size),
349 *linkname = zalloc(size);
350 int err = -1;
351
352 if (filename == NULL || linkname == NULL)
353 goto out_free;
354
355 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
356 debugdir, sbuild_id, sbuild_id + 2);
357
358 if (access(linkname, F_OK))
359 goto out_free;
360
361 if (readlink(linkname, filename, size - 1) < 0)
362 goto out_free;
363
364 if (unlink(linkname))
365 goto out_free;
366
367 /*
368 * Since the link is relative, we must make it absolute:
369 */
370 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
371 debugdir, sbuild_id, filename);
372
373 if (unlink(linkname))
374 goto out_free;
375
376 err = 0;
377out_free:
378 free(filename);
379 free(linkname);
380 return err;
381}
382
383static int dso__cache_build_id(struct dso *dso, const char *debugdir)
384{
385 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
386
387 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id),
388 dso->long_name, debugdir, is_kallsyms);
389}
390
391static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
392{
393 struct dso *pos;
394 int err = 0;
395
396 dsos__for_each_with_build_id(pos, head)
397 if (dso__cache_build_id(pos, debugdir))
398 err = -1;
399
400 return err;
401}
402
403static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
404{
405 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir);
406 ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir);
407 return ret;
408}
409
410static int perf_session__cache_build_ids(struct perf_session *session)
411{
412 struct rb_node *nd;
413 int ret;
414 char debugdir[PATH_MAX];
415
416 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
417
418 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
419 return -1;
420
421 ret = machine__cache_build_ids(&session->host_machine, debugdir);
422
423 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
424 struct machine *pos = rb_entry(nd, struct machine, rb_node);
425 ret |= machine__cache_build_ids(pos, debugdir);
426 }
427 return ret ? -1 : 0;
428}
429
430static bool machine__read_build_ids(struct machine *machine, bool with_hits)
431{
432 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
433 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
434 return ret;
435}
436
437static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
438{
439 struct rb_node *nd;
440 bool ret = machine__read_build_ids(&session->host_machine, with_hits);
441
442 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
443 struct machine *pos = rb_entry(nd, struct machine, rb_node);
444 ret |= machine__read_build_ids(pos, with_hits);
445 }
446
447 return ret;
448}
449
Stephane Eranian2eeaaa02012-05-15 13:28:13 +0200450static int write_tracing_data(int fd, struct perf_header *h __used,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200451 struct perf_evlist *evlist)
452{
453 return read_tracing_data(fd, &evlist->entries);
454}
455
456
457static int write_build_id(int fd, struct perf_header *h,
458 struct perf_evlist *evlist __used)
459{
460 struct perf_session *session;
461 int err;
462
463 session = container_of(h, struct perf_session, header);
464
Robert Richtere20960c2011-12-07 10:02:55 +0100465 if (!perf_session__read_build_ids(session, true))
466 return -1;
467
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200468 err = dsos__write_buildid_table(h, fd);
469 if (err < 0) {
470 pr_debug("failed to write buildid table\n");
471 return err;
472 }
473 if (!no_buildid_cache)
474 perf_session__cache_build_ids(session);
475
476 return 0;
477}
478
479static int write_hostname(int fd, struct perf_header *h __used,
480 struct perf_evlist *evlist __used)
481{
482 struct utsname uts;
483 int ret;
484
485 ret = uname(&uts);
486 if (ret < 0)
487 return -1;
488
489 return do_write_string(fd, uts.nodename);
490}
491
492static int write_osrelease(int fd, struct perf_header *h __used,
493 struct perf_evlist *evlist __used)
494{
495 struct utsname uts;
496 int ret;
497
498 ret = uname(&uts);
499 if (ret < 0)
500 return -1;
501
502 return do_write_string(fd, uts.release);
503}
504
505static int write_arch(int fd, struct perf_header *h __used,
506 struct perf_evlist *evlist __used)
507{
508 struct utsname uts;
509 int ret;
510
511 ret = uname(&uts);
512 if (ret < 0)
513 return -1;
514
515 return do_write_string(fd, uts.machine);
516}
517
518static int write_version(int fd, struct perf_header *h __used,
519 struct perf_evlist *evlist __used)
520{
521 return do_write_string(fd, perf_version_string);
522}
523
524static int write_cpudesc(int fd, struct perf_header *h __used,
525 struct perf_evlist *evlist __used)
526{
527#ifndef CPUINFO_PROC
528#define CPUINFO_PROC NULL
529#endif
530 FILE *file;
531 char *buf = NULL;
532 char *s, *p;
533 const char *search = CPUINFO_PROC;
534 size_t len = 0;
535 int ret = -1;
536
537 if (!search)
538 return -1;
539
540 file = fopen("/proc/cpuinfo", "r");
541 if (!file)
542 return -1;
543
544 while (getline(&buf, &len, file) > 0) {
545 ret = strncmp(buf, search, strlen(search));
546 if (!ret)
547 break;
548 }
549
550 if (ret)
551 goto done;
552
553 s = buf;
554
555 p = strchr(buf, ':');
556 if (p && *(p+1) == ' ' && *(p+2))
557 s = p + 2;
558 p = strchr(s, '\n');
559 if (p)
560 *p = '\0';
561
562 /* squash extra space characters (branding string) */
563 p = s;
564 while (*p) {
565 if (isspace(*p)) {
566 char *r = p + 1;
567 char *q = r;
568 *p = ' ';
569 while (*q && isspace(*q))
570 q++;
571 if (q != (p+1))
572 while ((*r++ = *q++));
573 }
574 p++;
575 }
576 ret = do_write_string(fd, s);
577done:
578 free(buf);
579 fclose(file);
580 return ret;
581}
582
583static int write_nrcpus(int fd, struct perf_header *h __used,
584 struct perf_evlist *evlist __used)
585{
586 long nr;
587 u32 nrc, nra;
588 int ret;
589
590 nr = sysconf(_SC_NPROCESSORS_CONF);
591 if (nr < 0)
592 return -1;
593
594 nrc = (u32)(nr & UINT_MAX);
595
596 nr = sysconf(_SC_NPROCESSORS_ONLN);
597 if (nr < 0)
598 return -1;
599
600 nra = (u32)(nr & UINT_MAX);
601
602 ret = do_write(fd, &nrc, sizeof(nrc));
603 if (ret < 0)
604 return ret;
605
606 return do_write(fd, &nra, sizeof(nra));
607}
608
609static int write_event_desc(int fd, struct perf_header *h __used,
610 struct perf_evlist *evlist)
611{
Robert Richter6606f872012-08-16 21:10:19 +0200612 struct perf_evsel *evsel;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200613 u32 nre = 0, nri, sz;
614 int ret;
615
Robert Richter6606f872012-08-16 21:10:19 +0200616 list_for_each_entry(evsel, &evlist->entries, node)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200617 nre++;
618
619 /*
620 * write number of events
621 */
622 ret = do_write(fd, &nre, sizeof(nre));
623 if (ret < 0)
624 return ret;
625
626 /*
627 * size of perf_event_attr struct
628 */
Robert Richter6606f872012-08-16 21:10:19 +0200629 sz = (u32)sizeof(evsel->attr);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200630 ret = do_write(fd, &sz, sizeof(sz));
631 if (ret < 0)
632 return ret;
633
Robert Richter6606f872012-08-16 21:10:19 +0200634 list_for_each_entry(evsel, &evlist->entries, node) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200635
Robert Richter6606f872012-08-16 21:10:19 +0200636 ret = do_write(fd, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200637 if (ret < 0)
638 return ret;
639 /*
640 * write number of unique id per event
641 * there is one id per instance of an event
642 *
643 * copy into an nri to be independent of the
644 * type of ids,
645 */
Robert Richter6606f872012-08-16 21:10:19 +0200646 nri = evsel->ids;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200647 ret = do_write(fd, &nri, sizeof(nri));
648 if (ret < 0)
649 return ret;
650
651 /*
652 * write event string as passed on cmdline
653 */
Robert Richter6606f872012-08-16 21:10:19 +0200654 ret = do_write_string(fd, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200655 if (ret < 0)
656 return ret;
657 /*
658 * write unique ids for this event
659 */
Robert Richter6606f872012-08-16 21:10:19 +0200660 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200661 if (ret < 0)
662 return ret;
663 }
664 return 0;
665}
666
667static int write_cmdline(int fd, struct perf_header *h __used,
668 struct perf_evlist *evlist __used)
669{
670 char buf[MAXPATHLEN];
671 char proc[32];
672 u32 i, n;
673 int ret;
674
675 /*
676 * actual atual path to perf binary
677 */
678 sprintf(proc, "/proc/%d/exe", getpid());
679 ret = readlink(proc, buf, sizeof(buf));
680 if (ret <= 0)
681 return -1;
682
683 /* readlink() does not add null termination */
684 buf[ret] = '\0';
685
686 /* account for binary path */
687 n = header_argc + 1;
688
689 ret = do_write(fd, &n, sizeof(n));
690 if (ret < 0)
691 return ret;
692
693 ret = do_write_string(fd, buf);
694 if (ret < 0)
695 return ret;
696
697 for (i = 0 ; i < header_argc; i++) {
698 ret = do_write_string(fd, header_argv[i]);
699 if (ret < 0)
700 return ret;
701 }
702 return 0;
703}
704
705#define CORE_SIB_FMT \
706 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
707#define THRD_SIB_FMT \
708 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
709
710struct cpu_topo {
711 u32 core_sib;
712 u32 thread_sib;
713 char **core_siblings;
714 char **thread_siblings;
715};
716
717static int build_cpu_topo(struct cpu_topo *tp, int cpu)
718{
719 FILE *fp;
720 char filename[MAXPATHLEN];
721 char *buf = NULL, *p;
722 size_t len = 0;
723 u32 i = 0;
724 int ret = -1;
725
726 sprintf(filename, CORE_SIB_FMT, cpu);
727 fp = fopen(filename, "r");
728 if (!fp)
729 return -1;
730
731 if (getline(&buf, &len, fp) <= 0)
732 goto done;
733
734 fclose(fp);
735
736 p = strchr(buf, '\n');
737 if (p)
738 *p = '\0';
739
740 for (i = 0; i < tp->core_sib; i++) {
741 if (!strcmp(buf, tp->core_siblings[i]))
742 break;
743 }
744 if (i == tp->core_sib) {
745 tp->core_siblings[i] = buf;
746 tp->core_sib++;
747 buf = NULL;
748 len = 0;
749 }
750
751 sprintf(filename, THRD_SIB_FMT, cpu);
752 fp = fopen(filename, "r");
753 if (!fp)
754 goto done;
755
756 if (getline(&buf, &len, fp) <= 0)
757 goto done;
758
759 p = strchr(buf, '\n');
760 if (p)
761 *p = '\0';
762
763 for (i = 0; i < tp->thread_sib; i++) {
764 if (!strcmp(buf, tp->thread_siblings[i]))
765 break;
766 }
767 if (i == tp->thread_sib) {
768 tp->thread_siblings[i] = buf;
769 tp->thread_sib++;
770 buf = NULL;
771 }
772 ret = 0;
773done:
774 if(fp)
775 fclose(fp);
776 free(buf);
777 return ret;
778}
779
780static void free_cpu_topo(struct cpu_topo *tp)
781{
782 u32 i;
783
784 if (!tp)
785 return;
786
787 for (i = 0 ; i < tp->core_sib; i++)
788 free(tp->core_siblings[i]);
789
790 for (i = 0 ; i < tp->thread_sib; i++)
791 free(tp->thread_siblings[i]);
792
793 free(tp);
794}
795
796static struct cpu_topo *build_cpu_topology(void)
797{
798 struct cpu_topo *tp;
799 void *addr;
800 u32 nr, i;
801 size_t sz;
802 long ncpus;
803 int ret = -1;
804
805 ncpus = sysconf(_SC_NPROCESSORS_CONF);
806 if (ncpus < 0)
807 return NULL;
808
809 nr = (u32)(ncpus & UINT_MAX);
810
811 sz = nr * sizeof(char *);
812
813 addr = calloc(1, sizeof(*tp) + 2 * sz);
814 if (!addr)
815 return NULL;
816
817 tp = addr;
818
819 addr += sizeof(*tp);
820 tp->core_siblings = addr;
821 addr += sz;
822 tp->thread_siblings = addr;
823
824 for (i = 0; i < nr; i++) {
825 ret = build_cpu_topo(tp, i);
826 if (ret < 0)
827 break;
828 }
829 if (ret) {
830 free_cpu_topo(tp);
831 tp = NULL;
832 }
833 return tp;
834}
835
836static int write_cpu_topology(int fd, struct perf_header *h __used,
837 struct perf_evlist *evlist __used)
838{
839 struct cpu_topo *tp;
840 u32 i;
841 int ret;
842
843 tp = build_cpu_topology();
844 if (!tp)
845 return -1;
846
847 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
848 if (ret < 0)
849 goto done;
850
851 for (i = 0; i < tp->core_sib; i++) {
852 ret = do_write_string(fd, tp->core_siblings[i]);
853 if (ret < 0)
854 goto done;
855 }
856 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
857 if (ret < 0)
858 goto done;
859
860 for (i = 0; i < tp->thread_sib; i++) {
861 ret = do_write_string(fd, tp->thread_siblings[i]);
862 if (ret < 0)
863 break;
864 }
865done:
866 free_cpu_topo(tp);
867 return ret;
868}
869
870
871
872static int write_total_mem(int fd, struct perf_header *h __used,
873 struct perf_evlist *evlist __used)
874{
875 char *buf = NULL;
876 FILE *fp;
877 size_t len = 0;
878 int ret = -1, n;
879 uint64_t mem;
880
881 fp = fopen("/proc/meminfo", "r");
882 if (!fp)
883 return -1;
884
885 while (getline(&buf, &len, fp) > 0) {
886 ret = strncmp(buf, "MemTotal:", 9);
887 if (!ret)
888 break;
889 }
890 if (!ret) {
891 n = sscanf(buf, "%*s %"PRIu64, &mem);
892 if (n == 1)
893 ret = do_write(fd, &mem, sizeof(mem));
894 }
895 free(buf);
896 fclose(fp);
897 return ret;
898}
899
900static int write_topo_node(int fd, int node)
901{
902 char str[MAXPATHLEN];
903 char field[32];
904 char *buf = NULL, *p;
905 size_t len = 0;
906 FILE *fp;
907 u64 mem_total, mem_free, mem;
908 int ret = -1;
909
910 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
911 fp = fopen(str, "r");
912 if (!fp)
913 return -1;
914
915 while (getline(&buf, &len, fp) > 0) {
916 /* skip over invalid lines */
917 if (!strchr(buf, ':'))
918 continue;
919 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
920 goto done;
921 if (!strcmp(field, "MemTotal:"))
922 mem_total = mem;
923 if (!strcmp(field, "MemFree:"))
924 mem_free = mem;
925 }
926
927 fclose(fp);
928
929 ret = do_write(fd, &mem_total, sizeof(u64));
930 if (ret)
931 goto done;
932
933 ret = do_write(fd, &mem_free, sizeof(u64));
934 if (ret)
935 goto done;
936
937 ret = -1;
938 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
939
940 fp = fopen(str, "r");
941 if (!fp)
942 goto done;
943
944 if (getline(&buf, &len, fp) <= 0)
945 goto done;
946
947 p = strchr(buf, '\n');
948 if (p)
949 *p = '\0';
950
951 ret = do_write_string(fd, buf);
952done:
953 free(buf);
954 fclose(fp);
955 return ret;
956}
957
958static int write_numa_topology(int fd, struct perf_header *h __used,
959 struct perf_evlist *evlist __used)
960{
961 char *buf = NULL;
962 size_t len = 0;
963 FILE *fp;
964 struct cpu_map *node_map = NULL;
965 char *c;
966 u32 nr, i, j;
967 int ret = -1;
968
969 fp = fopen("/sys/devices/system/node/online", "r");
970 if (!fp)
971 return -1;
972
973 if (getline(&buf, &len, fp) <= 0)
974 goto done;
975
976 c = strchr(buf, '\n');
977 if (c)
978 *c = '\0';
979
980 node_map = cpu_map__new(buf);
981 if (!node_map)
982 goto done;
983
984 nr = (u32)node_map->nr;
985
986 ret = do_write(fd, &nr, sizeof(nr));
987 if (ret < 0)
988 goto done;
989
990 for (i = 0; i < nr; i++) {
991 j = (u32)node_map->map[i];
992 ret = do_write(fd, &j, sizeof(j));
993 if (ret < 0)
994 break;
995
996 ret = write_topo_node(fd, i);
997 if (ret < 0)
998 break;
999 }
1000done:
1001 free(buf);
1002 fclose(fp);
1003 free(node_map);
1004 return ret;
1005}
1006
1007/*
Robert Richter50a96672012-08-16 21:10:24 +02001008 * File format:
1009 *
1010 * struct pmu_mappings {
1011 * u32 pmu_num;
1012 * struct pmu_map {
1013 * u32 type;
1014 * char name[];
1015 * }[pmu_num];
1016 * };
1017 */
1018
1019static int write_pmu_mappings(int fd, struct perf_header *h __used,
1020 struct perf_evlist *evlist __used)
1021{
1022 struct perf_pmu *pmu = NULL;
1023 off_t offset = lseek(fd, 0, SEEK_CUR);
1024 __u32 pmu_num = 0;
1025
1026 /* write real pmu_num later */
1027 do_write(fd, &pmu_num, sizeof(pmu_num));
1028
1029 while ((pmu = perf_pmu__scan(pmu))) {
1030 if (!pmu->name)
1031 continue;
1032 pmu_num++;
1033 do_write(fd, &pmu->type, sizeof(pmu->type));
1034 do_write_string(fd, pmu->name);
1035 }
1036
1037 if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) {
1038 /* discard all */
1039 lseek(fd, offset, SEEK_SET);
1040 return -1;
1041 }
1042
1043 return 0;
1044}
1045
1046/*
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001047 * default get_cpuid(): nothing gets recorded
1048 * actual implementation must be in arch/$(ARCH)/util/header.c
1049 */
1050int __attribute__((weak)) get_cpuid(char *buffer __used, size_t sz __used)
1051{
1052 return -1;
1053}
1054
1055static int write_cpuid(int fd, struct perf_header *h __used,
1056 struct perf_evlist *evlist __used)
1057{
1058 char buffer[64];
1059 int ret;
1060
1061 ret = get_cpuid(buffer, sizeof(buffer));
1062 if (!ret)
1063 goto write_it;
1064
1065 return -1;
1066write_it:
1067 return do_write_string(fd, buffer);
1068}
1069
Stephane Eranian330aa672012-03-08 23:47:46 +01001070static int write_branch_stack(int fd __used, struct perf_header *h __used,
1071 struct perf_evlist *evlist __used)
1072{
1073 return 0;
1074}
1075
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001076static void print_hostname(struct perf_header *ph, int fd, FILE *fp)
1077{
1078 char *str = do_read_string(fd, ph);
1079 fprintf(fp, "# hostname : %s\n", str);
1080 free(str);
1081}
1082
1083static void print_osrelease(struct perf_header *ph, int fd, FILE *fp)
1084{
1085 char *str = do_read_string(fd, ph);
1086 fprintf(fp, "# os release : %s\n", str);
1087 free(str);
1088}
1089
1090static void print_arch(struct perf_header *ph, int fd, FILE *fp)
1091{
1092 char *str = do_read_string(fd, ph);
1093 fprintf(fp, "# arch : %s\n", str);
1094 free(str);
1095}
1096
1097static void print_cpudesc(struct perf_header *ph, int fd, FILE *fp)
1098{
1099 char *str = do_read_string(fd, ph);
1100 fprintf(fp, "# cpudesc : %s\n", str);
1101 free(str);
1102}
1103
1104static void print_nrcpus(struct perf_header *ph, int fd, FILE *fp)
1105{
1106 ssize_t ret;
1107 u32 nr;
1108
1109 ret = read(fd, &nr, sizeof(nr));
1110 if (ret != (ssize_t)sizeof(nr))
1111 nr = -1; /* interpreted as error */
1112
1113 if (ph->needs_swap)
1114 nr = bswap_32(nr);
1115
1116 fprintf(fp, "# nrcpus online : %u\n", nr);
1117
1118 ret = read(fd, &nr, sizeof(nr));
1119 if (ret != (ssize_t)sizeof(nr))
1120 nr = -1; /* interpreted as error */
1121
1122 if (ph->needs_swap)
1123 nr = bswap_32(nr);
1124
1125 fprintf(fp, "# nrcpus avail : %u\n", nr);
1126}
1127
1128static void print_version(struct perf_header *ph, int fd, FILE *fp)
1129{
1130 char *str = do_read_string(fd, ph);
1131 fprintf(fp, "# perf version : %s\n", str);
1132 free(str);
1133}
1134
1135static void print_cmdline(struct perf_header *ph, int fd, FILE *fp)
1136{
1137 ssize_t ret;
1138 char *str;
1139 u32 nr, i;
1140
1141 ret = read(fd, &nr, sizeof(nr));
1142 if (ret != (ssize_t)sizeof(nr))
1143 return;
1144
1145 if (ph->needs_swap)
1146 nr = bswap_32(nr);
1147
1148 fprintf(fp, "# cmdline : ");
1149
1150 for (i = 0; i < nr; i++) {
1151 str = do_read_string(fd, ph);
1152 fprintf(fp, "%s ", str);
1153 free(str);
1154 }
1155 fputc('\n', fp);
1156}
1157
1158static void print_cpu_topology(struct perf_header *ph, int fd, FILE *fp)
1159{
1160 ssize_t ret;
1161 u32 nr, i;
1162 char *str;
1163
1164 ret = read(fd, &nr, sizeof(nr));
1165 if (ret != (ssize_t)sizeof(nr))
1166 return;
1167
1168 if (ph->needs_swap)
1169 nr = bswap_32(nr);
1170
1171 for (i = 0; i < nr; i++) {
1172 str = do_read_string(fd, ph);
1173 fprintf(fp, "# sibling cores : %s\n", str);
1174 free(str);
1175 }
1176
1177 ret = read(fd, &nr, sizeof(nr));
1178 if (ret != (ssize_t)sizeof(nr))
1179 return;
1180
1181 if (ph->needs_swap)
1182 nr = bswap_32(nr);
1183
1184 for (i = 0; i < nr; i++) {
1185 str = do_read_string(fd, ph);
1186 fprintf(fp, "# sibling threads : %s\n", str);
1187 free(str);
1188 }
1189}
1190
Robert Richter4e1b9c62012-08-16 21:10:22 +02001191static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001192{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001193 struct perf_evsel *evsel;
1194
1195 if (!events)
1196 return;
1197
1198 for (evsel = events; evsel->attr.size; evsel++) {
1199 if (evsel->name)
1200 free(evsel->name);
1201 if (evsel->id)
1202 free(evsel->id);
1203 }
1204
1205 free(events);
1206}
1207
1208static struct perf_evsel *
1209read_event_desc(struct perf_header *ph, int fd)
1210{
1211 struct perf_evsel *evsel, *events = NULL;
1212 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001213 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001214 u32 nre, sz, nr, i, j;
1215 ssize_t ret;
1216 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001217
1218 /* number of events */
1219 ret = read(fd, &nre, sizeof(nre));
1220 if (ret != (ssize_t)sizeof(nre))
1221 goto error;
1222
1223 if (ph->needs_swap)
1224 nre = bswap_32(nre);
1225
1226 ret = read(fd, &sz, sizeof(sz));
1227 if (ret != (ssize_t)sizeof(sz))
1228 goto error;
1229
1230 if (ph->needs_swap)
1231 sz = bswap_32(sz);
1232
Stephane Eranian62db9062012-02-09 23:21:07 +01001233 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001234 buf = malloc(sz);
1235 if (!buf)
1236 goto error;
1237
Robert Richter4e1b9c62012-08-16 21:10:22 +02001238 /* the last event terminates with evsel->attr.size == 0: */
1239 events = calloc(nre + 1, sizeof(*events));
1240 if (!events)
1241 goto error;
1242
1243 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001244 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001245 msz = sz;
1246
Robert Richter4e1b9c62012-08-16 21:10:22 +02001247 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1248 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001249
Stephane Eranian62db9062012-02-09 23:21:07 +01001250 /*
1251 * must read entire on-file attr struct to
1252 * sync up with layout.
1253 */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001254 ret = read(fd, buf, sz);
1255 if (ret != (ssize_t)sz)
1256 goto error;
1257
1258 if (ph->needs_swap)
1259 perf_event__attr_swap(buf);
1260
Robert Richter4e1b9c62012-08-16 21:10:22 +02001261 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001262
1263 ret = read(fd, &nr, sizeof(nr));
1264 if (ret != (ssize_t)sizeof(nr))
1265 goto error;
1266
1267 if (ph->needs_swap)
1268 nr = bswap_32(nr);
1269
Robert Richter4e1b9c62012-08-16 21:10:22 +02001270 evsel->name = do_read_string(fd, ph);
1271
1272 if (!nr)
1273 continue;
1274
1275 id = calloc(nr, sizeof(*id));
1276 if (!id)
1277 goto error;
1278 evsel->ids = nr;
1279 evsel->id = id;
1280
1281 for (j = 0 ; j < nr; j++) {
1282 ret = read(fd, id, sizeof(*id));
1283 if (ret != (ssize_t)sizeof(*id))
1284 goto error;
1285 if (ph->needs_swap)
1286 *id = bswap_64(*id);
1287 id++;
1288 }
1289 }
1290out:
1291 if (buf)
1292 free(buf);
1293 return events;
1294error:
1295 if (events)
1296 free_event_desc(events);
1297 events = NULL;
1298 goto out;
1299}
1300
1301static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1302{
1303 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1304 u32 j;
1305 u64 *id;
1306
1307 if (!events) {
1308 fprintf(fp, "# event desc: not available or unable to read\n");
1309 return;
1310 }
1311
1312 for (evsel = events; evsel->attr.size; evsel++) {
1313 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001314
1315 fprintf(fp, "type = %d, config = 0x%"PRIx64
1316 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
Robert Richter4e1b9c62012-08-16 21:10:22 +02001317 evsel->attr.type,
1318 (u64)evsel->attr.config,
1319 (u64)evsel->attr.config1,
1320 (u64)evsel->attr.config2);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001321
1322 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001323 evsel->attr.exclude_user,
1324 evsel->attr.exclude_kernel);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001325
David Ahern78b961f2012-07-20 17:25:52 -06001326 fprintf(fp, ", excl_host = %d, excl_guest = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001327 evsel->attr.exclude_host,
1328 evsel->attr.exclude_guest);
David Ahern78b961f2012-07-20 17:25:52 -06001329
Robert Richter4e1b9c62012-08-16 21:10:22 +02001330 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
David Ahern78b961f2012-07-20 17:25:52 -06001331
Robert Richter4e1b9c62012-08-16 21:10:22 +02001332 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001333 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001334 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1335 if (j)
1336 fputc(',', fp);
1337 fprintf(fp, " %"PRIu64, *id);
1338 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001339 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001340 }
1341
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001342 fputc('\n', fp);
1343 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001344
1345 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001346}
1347
1348static void print_total_mem(struct perf_header *h __used, int fd, FILE *fp)
1349{
1350 uint64_t mem;
1351 ssize_t ret;
1352
1353 ret = read(fd, &mem, sizeof(mem));
1354 if (ret != sizeof(mem))
1355 goto error;
1356
1357 if (h->needs_swap)
1358 mem = bswap_64(mem);
1359
1360 fprintf(fp, "# total memory : %"PRIu64" kB\n", mem);
1361 return;
1362error:
1363 fprintf(fp, "# total memory : unknown\n");
1364}
1365
1366static void print_numa_topology(struct perf_header *h __used, int fd, FILE *fp)
1367{
1368 ssize_t ret;
1369 u32 nr, c, i;
1370 char *str;
1371 uint64_t mem_total, mem_free;
1372
1373 /* nr nodes */
1374 ret = read(fd, &nr, sizeof(nr));
1375 if (ret != (ssize_t)sizeof(nr))
1376 goto error;
1377
1378 if (h->needs_swap)
1379 nr = bswap_32(nr);
1380
1381 for (i = 0; i < nr; i++) {
1382
1383 /* node number */
1384 ret = read(fd, &c, sizeof(c));
1385 if (ret != (ssize_t)sizeof(c))
1386 goto error;
1387
1388 if (h->needs_swap)
1389 c = bswap_32(c);
1390
1391 ret = read(fd, &mem_total, sizeof(u64));
1392 if (ret != sizeof(u64))
1393 goto error;
1394
1395 ret = read(fd, &mem_free, sizeof(u64));
1396 if (ret != sizeof(u64))
1397 goto error;
1398
1399 if (h->needs_swap) {
1400 mem_total = bswap_64(mem_total);
1401 mem_free = bswap_64(mem_free);
1402 }
1403
1404 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1405 " free = %"PRIu64" kB\n",
1406 c,
1407 mem_total,
1408 mem_free);
1409
1410 str = do_read_string(fd, h);
1411 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1412 free(str);
1413 }
1414 return;
1415error:
1416 fprintf(fp, "# numa topology : not available\n");
1417}
1418
1419static void print_cpuid(struct perf_header *ph, int fd, FILE *fp)
1420{
1421 char *str = do_read_string(fd, ph);
1422 fprintf(fp, "# cpuid : %s\n", str);
1423 free(str);
1424}
1425
Stephane Eranian330aa672012-03-08 23:47:46 +01001426static void print_branch_stack(struct perf_header *ph __used, int fd __used,
1427 FILE *fp)
1428{
1429 fprintf(fp, "# contains samples with branch stack\n");
1430}
1431
Robert Richter50a96672012-08-16 21:10:24 +02001432static void print_pmu_mappings(struct perf_header *ph, int fd, FILE *fp)
1433{
1434 const char *delimiter = "# pmu mappings: ";
1435 char *name;
1436 int ret;
1437 u32 pmu_num;
1438 u32 type;
1439
1440 ret = read(fd, &pmu_num, sizeof(pmu_num));
1441 if (ret != sizeof(pmu_num))
1442 goto error;
1443
1444 if (!pmu_num) {
1445 fprintf(fp, "# pmu mappings: not available\n");
1446 return;
1447 }
1448
1449 while (pmu_num) {
1450 if (read(fd, &type, sizeof(type)) != sizeof(type))
1451 break;
1452 name = do_read_string(fd, ph);
1453 if (!name)
1454 break;
1455 pmu_num--;
1456 fprintf(fp, "%s%s = %" PRIu32, delimiter, name, type);
1457 free(name);
1458 delimiter = ", ";
1459 }
1460
1461 fprintf(fp, "\n");
1462
1463 if (!pmu_num)
1464 return;
1465error:
1466 fprintf(fp, "# pmu mappings: unable to read\n");
1467}
1468
Robert Richter08d95bd2012-02-10 15:41:55 +01001469static int __event_process_build_id(struct build_id_event *bev,
1470 char *filename,
1471 struct perf_session *session)
1472{
1473 int err = -1;
1474 struct list_head *head;
1475 struct machine *machine;
1476 u16 misc;
1477 struct dso *dso;
1478 enum dso_kernel_type dso_type;
1479
1480 machine = perf_session__findnew_machine(session, bev->pid);
1481 if (!machine)
1482 goto out;
1483
1484 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1485
1486 switch (misc) {
1487 case PERF_RECORD_MISC_KERNEL:
1488 dso_type = DSO_TYPE_KERNEL;
1489 head = &machine->kernel_dsos;
1490 break;
1491 case PERF_RECORD_MISC_GUEST_KERNEL:
1492 dso_type = DSO_TYPE_GUEST_KERNEL;
1493 head = &machine->kernel_dsos;
1494 break;
1495 case PERF_RECORD_MISC_USER:
1496 case PERF_RECORD_MISC_GUEST_USER:
1497 dso_type = DSO_TYPE_USER;
1498 head = &machine->user_dsos;
1499 break;
1500 default:
1501 goto out;
1502 }
1503
1504 dso = __dsos__findnew(head, filename);
1505 if (dso != NULL) {
1506 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1507
1508 dso__set_build_id(dso, &bev->build_id);
1509
1510 if (filename[0] == '[')
1511 dso->kernel = dso_type;
1512
1513 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1514 sbuild_id);
1515 pr_debug("build id event received for %s: %s\n",
1516 dso->long_name, sbuild_id);
1517 }
1518
1519 err = 0;
1520out:
1521 return err;
1522}
1523
1524static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1525 int input, u64 offset, u64 size)
1526{
1527 struct perf_session *session = container_of(header, struct perf_session, header);
1528 struct {
1529 struct perf_event_header header;
1530 u8 build_id[ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1531 char filename[0];
1532 } old_bev;
1533 struct build_id_event bev;
1534 char filename[PATH_MAX];
1535 u64 limit = offset + size;
1536
1537 while (offset < limit) {
1538 ssize_t len;
1539
1540 if (read(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1541 return -1;
1542
1543 if (header->needs_swap)
1544 perf_event_header__bswap(&old_bev.header);
1545
1546 len = old_bev.header.size - sizeof(old_bev);
1547 if (read(input, filename, len) != len)
1548 return -1;
1549
1550 bev.header = old_bev.header;
1551
1552 /*
1553 * As the pid is the missing value, we need to fill
1554 * it properly. The header.misc value give us nice hint.
1555 */
1556 bev.pid = HOST_KERNEL_ID;
1557 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1558 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1559 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1560
1561 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1562 __event_process_build_id(&bev, filename, session);
1563
1564 offset += bev.header.size;
1565 }
1566
1567 return 0;
1568}
1569
1570static int perf_header__read_build_ids(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 build_id_event bev;
1575 char filename[PATH_MAX];
1576 u64 limit = offset + size, orig_offset = offset;
1577 int err = -1;
1578
1579 while (offset < limit) {
1580 ssize_t len;
1581
1582 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
1583 goto out;
1584
1585 if (header->needs_swap)
1586 perf_event_header__bswap(&bev.header);
1587
1588 len = bev.header.size - sizeof(bev);
1589 if (read(input, filename, len) != len)
1590 goto out;
1591 /*
1592 * The a1645ce1 changeset:
1593 *
1594 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1595 *
1596 * Added a field to struct build_id_event that broke the file
1597 * format.
1598 *
1599 * Since the kernel build-id is the first entry, process the
1600 * table using the old format if the well known
1601 * '[kernel.kallsyms]' string for the kernel build-id has the
1602 * first 4 characters chopped off (where the pid_t sits).
1603 */
1604 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1605 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1606 return -1;
1607 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1608 }
1609
1610 __event_process_build_id(&bev, filename, session);
1611
1612 offset += bev.header.size;
1613 }
1614 err = 0;
1615out:
1616 return err;
1617}
1618
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001619static int process_tracing_data(struct perf_file_section *section __unused,
Robert Richterf1c67db2012-02-10 15:41:56 +01001620 struct perf_header *ph __unused,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001621 int feat __unused, int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001622{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001623 trace_report(fd, data, false);
Robert Richterf1c67db2012-02-10 15:41:56 +01001624 return 0;
1625}
1626
1627static int process_build_id(struct perf_file_section *section,
1628 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001629 int feat __unused, int fd, void *data __used)
Robert Richterf1c67db2012-02-10 15:41:56 +01001630{
1631 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1632 pr_debug("Failed to read buildids, continuing...\n");
1633 return 0;
1634}
1635
Robert Richter7c2f7af2012-08-16 21:10:23 +02001636static struct perf_evsel *
1637perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1638{
1639 struct perf_evsel *evsel;
1640
1641 list_for_each_entry(evsel, &evlist->entries, node) {
1642 if (evsel->idx == idx)
1643 return evsel;
1644 }
1645
1646 return NULL;
1647}
1648
1649static void
1650perf_evlist__set_event_name(struct perf_evlist *evlist, struct perf_evsel *event)
1651{
1652 struct perf_evsel *evsel;
1653
1654 if (!event->name)
1655 return;
1656
1657 evsel = perf_evlist__find_by_index(evlist, event->idx);
1658 if (!evsel)
1659 return;
1660
1661 if (evsel->name)
1662 return;
1663
1664 evsel->name = strdup(event->name);
1665}
1666
1667static int
1668process_event_desc(struct perf_file_section *section __unused,
1669 struct perf_header *header, int feat __unused, int fd,
1670 void *data __used)
1671{
1672 struct perf_session *session = container_of(header, struct perf_session, header);
1673 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1674
1675 if (!events)
1676 return 0;
1677
1678 for (evsel = events; evsel->attr.size; evsel++)
1679 perf_evlist__set_event_name(session->evlist, evsel);
1680
1681 free_event_desc(events);
1682
1683 return 0;
1684}
1685
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001686struct feature_ops {
1687 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1688 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01001689 int (*process)(struct perf_file_section *section,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001690 struct perf_header *h, int feat, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001691 const char *name;
1692 bool full_only;
1693};
1694
Robert Richter8cdfa782011-12-07 10:02:56 +01001695#define FEAT_OPA(n, func) \
1696 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01001697#define FEAT_OPP(n, func) \
1698 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1699 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01001700#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01001701 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1702 .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01001703
1704/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001705#define print_tracing_data NULL
1706#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001707
1708static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001709 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01001710 FEAT_OPP(HEADER_BUILD_ID, build_id),
Robert Richter8cdfa782011-12-07 10:02:56 +01001711 FEAT_OPA(HEADER_HOSTNAME, hostname),
1712 FEAT_OPA(HEADER_OSRELEASE, osrelease),
1713 FEAT_OPA(HEADER_VERSION, version),
1714 FEAT_OPA(HEADER_ARCH, arch),
1715 FEAT_OPA(HEADER_NRCPUS, nrcpus),
1716 FEAT_OPA(HEADER_CPUDESC, cpudesc),
1717 FEAT_OPA(HEADER_CPUID, cpuid),
1718 FEAT_OPA(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02001719 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Robert Richter8cdfa782011-12-07 10:02:56 +01001720 FEAT_OPA(HEADER_CMDLINE, cmdline),
1721 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1722 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01001723 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Robert Richter50a96672012-08-16 21:10:24 +02001724 FEAT_OPA(HEADER_PMU_MAPPINGS, pmu_mappings),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001725};
1726
1727struct header_print_data {
1728 FILE *fp;
1729 bool full; /* extended list of headers */
1730};
1731
1732static int perf_file_section__fprintf_info(struct perf_file_section *section,
1733 struct perf_header *ph,
1734 int feat, int fd, void *data)
1735{
1736 struct header_print_data *hd = data;
1737
1738 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1739 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1740 "%d, continuing...\n", section->offset, feat);
1741 return 0;
1742 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001743 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001744 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01001745 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001746 }
1747 if (!feat_ops[feat].print)
1748 return 0;
1749
1750 if (!feat_ops[feat].full_only || hd->full)
1751 feat_ops[feat].print(ph, fd, hd->fp);
1752 else
1753 fprintf(hd->fp, "# %s info available, use -I to display\n",
1754 feat_ops[feat].name);
1755
1756 return 0;
1757}
1758
1759int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
1760{
1761 struct header_print_data hd;
1762 struct perf_header *header = &session->header;
1763 int fd = session->fd;
1764 hd.fp = fp;
1765 hd.full = full;
1766
1767 perf_header__process_sections(header, fd, &hd,
1768 perf_file_section__fprintf_info);
1769 return 0;
1770}
1771
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001772static int do_write_feat(int fd, struct perf_header *h, int type,
1773 struct perf_file_section **p,
1774 struct perf_evlist *evlist)
1775{
1776 int err;
1777 int ret = 0;
1778
1779 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001780 if (!feat_ops[type].write)
1781 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001782
1783 (*p)->offset = lseek(fd, 0, SEEK_CUR);
1784
1785 err = feat_ops[type].write(fd, h, evlist);
1786 if (err < 0) {
1787 pr_debug("failed to write feature %d\n", type);
1788
1789 /* undo anything written */
1790 lseek(fd, (*p)->offset, SEEK_SET);
1791
1792 return -1;
1793 }
1794 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
1795 (*p)++;
1796 }
1797 return ret;
1798}
1799
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001800static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001801 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001802{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001803 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001804 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001805 int sec_size;
1806 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001807 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001808 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001809
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001810 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001811 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001812 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001813
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001814 feat_sec = p = calloc(sizeof(*feat_sec), nr_sections);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001815 if (feat_sec == NULL)
1816 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001817
1818 sec_size = sizeof(*feat_sec) * nr_sections;
1819
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001820 sec_start = header->data_offset + header->data_size;
Xiao Guangrongf887f302010-02-04 16:46:42 +08001821 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001822
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001823 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
1824 if (do_write_feat(fd, header, feat, &p, evlist))
1825 perf_header__clear_feat(header, feat);
1826 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001827
Xiao Guangrongf887f302010-02-04 16:46:42 +08001828 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001829 /*
1830 * may write more than needed due to dropped feature, but
1831 * this is okay, reader will skip the mising entries
1832 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001833 err = do_write(fd, feat_sec, sec_size);
1834 if (err < 0)
1835 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001836 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001837 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001838}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001839
Tom Zanussi8dc58102010-04-01 23:59:15 -05001840int perf_header__write_pipe(int fd)
1841{
1842 struct perf_pipe_file_header f_header;
1843 int err;
1844
1845 f_header = (struct perf_pipe_file_header){
1846 .magic = PERF_MAGIC,
1847 .size = sizeof(f_header),
1848 };
1849
1850 err = do_write(fd, &f_header, sizeof(f_header));
1851 if (err < 0) {
1852 pr_debug("failed to write perf pipe header\n");
1853 return err;
1854 }
1855
1856 return 0;
1857}
1858
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001859int perf_session__write_header(struct perf_session *session,
1860 struct perf_evlist *evlist,
1861 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001862{
1863 struct perf_file_header f_header;
1864 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001865 struct perf_header *header = &session->header;
Robert Richter6606f872012-08-16 21:10:19 +02001866 struct perf_evsel *evsel, *pair = NULL;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001867 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001868
1869 lseek(fd, sizeof(f_header), SEEK_SET);
1870
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001871 if (session->evlist != evlist)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001872 pair = perf_evlist__first(session->evlist);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001873
Robert Richter6606f872012-08-16 21:10:19 +02001874 list_for_each_entry(evsel, &evlist->entries, node) {
1875 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
1876 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001877 if (err < 0) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001878out_err_write:
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001879 pr_debug("failed to write perf header\n");
1880 return err;
1881 }
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001882 if (session->evlist != evlist) {
1883 err = do_write(fd, pair->id, pair->ids * sizeof(u64));
1884 if (err < 0)
1885 goto out_err_write;
Robert Richter6606f872012-08-16 21:10:19 +02001886 evsel->ids += pair->ids;
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001887 pair = perf_evsel__next(pair);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001888 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001889 }
1890
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001891 header->attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001892
Robert Richter6606f872012-08-16 21:10:19 +02001893 list_for_each_entry(evsel, &evlist->entries, node) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001894 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02001895 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001896 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02001897 .offset = evsel->id_offset,
1898 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001899 }
1900 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001901 err = do_write(fd, &f_attr, sizeof(f_attr));
1902 if (err < 0) {
1903 pr_debug("failed to write perf header attribute\n");
1904 return err;
1905 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001906 }
1907
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001908 header->event_offset = lseek(fd, 0, SEEK_CUR);
Robert Richterdb146f02012-08-16 21:10:20 +02001909 header->event_size = trace_event_count * sizeof(struct perf_trace_event_type);
1910 if (trace_events) {
1911 err = do_write(fd, trace_events, header->event_size);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001912 if (err < 0) {
1913 pr_debug("failed to write perf header events\n");
1914 return err;
1915 }
1916 }
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02001917
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001918 header->data_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001919
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001920 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001921 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001922 if (err < 0)
1923 return err;
1924 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001925
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001926 f_header = (struct perf_file_header){
1927 .magic = PERF_MAGIC,
1928 .size = sizeof(f_header),
1929 .attr_size = sizeof(f_attr),
1930 .attrs = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001931 .offset = header->attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001932 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001933 },
1934 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001935 .offset = header->data_offset,
1936 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001937 },
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02001938 .event_types = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001939 .offset = header->event_offset,
1940 .size = header->event_size,
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02001941 },
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001942 };
1943
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001944 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001945
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001946 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001947 err = do_write(fd, &f_header, sizeof(f_header));
1948 if (err < 0) {
1949 pr_debug("failed to write perf header\n");
1950 return err;
1951 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001952 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001953
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001954 header->frozen = 1;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001955 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001956}
1957
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001958static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001959 int fd, void *buf, size_t size)
1960{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02001961 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001962 return -1;
1963
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001964 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001965 mem_bswap_64(buf, size);
1966
1967 return 0;
1968}
1969
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001970int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001971 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001972 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001973 struct perf_header *ph,
1974 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001975{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001976 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001977 int nr_sections;
1978 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001979 int feat;
1980 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001981
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001982 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001983 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001984 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001985
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001986 feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001987 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001988 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001989
1990 sec_size = sizeof(*feat_sec) * nr_sections;
1991
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001992 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001993
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001994 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
1995 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02001996 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001997
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001998 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
1999 err = process(sec++, header, feat, fd, data);
2000 if (err < 0)
2001 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002002 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002003 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002004out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01002005 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002006 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002007}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02002008
Stephane Eranian114382a2012-02-09 23:21:08 +01002009static const int attr_file_abi_sizes[] = {
2010 [0] = PERF_ATTR_SIZE_VER0,
2011 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02002012 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002013 [3] = PERF_ATTR_SIZE_VER3,
2014 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01002015 0,
2016};
2017
2018/*
2019 * In the legacy file format, the magic number is not used to encode endianness.
2020 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
2021 * on ABI revisions, we need to try all combinations for all endianness to
2022 * detect the endianness.
2023 */
2024static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
2025{
2026 uint64_t ref_size, attr_size;
2027 int i;
2028
2029 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
2030 ref_size = attr_file_abi_sizes[i]
2031 + sizeof(struct perf_file_section);
2032 if (hdr_sz != ref_size) {
2033 attr_size = bswap_64(hdr_sz);
2034 if (attr_size != ref_size)
2035 continue;
2036
2037 ph->needs_swap = true;
2038 }
2039 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
2040 i,
2041 ph->needs_swap);
2042 return 0;
2043 }
2044 /* could not determine endianness */
2045 return -1;
2046}
2047
2048#define PERF_PIPE_HDR_VER0 16
2049
2050static const size_t attr_pipe_abi_sizes[] = {
2051 [0] = PERF_PIPE_HDR_VER0,
2052 0,
2053};
2054
2055/*
2056 * In the legacy pipe format, there is an implicit assumption that endiannesss
2057 * between host recording the samples, and host parsing the samples is the
2058 * same. This is not always the case given that the pipe output may always be
2059 * redirected into a file and analyzed on a different machine with possibly a
2060 * different endianness and perf_event ABI revsions in the perf tool itself.
2061 */
2062static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
2063{
2064 u64 attr_size;
2065 int i;
2066
2067 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
2068 if (hdr_sz != attr_pipe_abi_sizes[i]) {
2069 attr_size = bswap_64(hdr_sz);
2070 if (attr_size != hdr_sz)
2071 continue;
2072
2073 ph->needs_swap = true;
2074 }
2075 pr_debug("Pipe ABI%d perf.data file detected\n", i);
2076 return 0;
2077 }
2078 return -1;
2079}
2080
2081static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2082 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002083{
2084 int ret;
2085
2086 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002087 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002088 if (ret == 0) {
2089 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002090 if (is_pipe)
2091 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002092
Stephane Eranian114382a2012-02-09 23:21:08 +01002093 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002094 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002095 /*
2096 * the new magic number serves two purposes:
2097 * - unique number to identify actual perf.data files
2098 * - encode endianness of file
2099 */
Stephane Eranian73323f52012-02-02 13:54:44 +01002100
Stephane Eranian114382a2012-02-09 23:21:08 +01002101 /* check magic number with one endianness */
2102 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002103 return 0;
2104
Stephane Eranian114382a2012-02-09 23:21:08 +01002105 /* check magic number with opposite endianness */
2106 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002107 return -1;
2108
2109 ph->needs_swap = true;
2110
2111 return 0;
2112}
2113
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002114int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002115 struct perf_header *ph, int fd)
2116{
Stephane Eranian73323f52012-02-02 13:54:44 +01002117 int ret;
2118
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002119 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002120
Stephane Eranian73323f52012-02-02 13:54:44 +01002121 ret = readn(fd, header, sizeof(*header));
2122 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002123 return -1;
2124
Stephane Eranian114382a2012-02-09 23:21:08 +01002125 if (check_magic_endian(header->magic,
2126 header->attr_size, false, ph) < 0) {
2127 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002128 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002129 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002130
Stephane Eranian73323f52012-02-02 13:54:44 +01002131 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002132 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002133 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002134 }
2135
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002136 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002137 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002138 if (header->size == offsetof(typeof(*header), adds_features))
2139 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002140 else
2141 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002142 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002143 /*
2144 * feature bitmap is declared as an array of unsigned longs --
2145 * not good since its size can differ between the host that
2146 * generated the data file and the host analyzing the file.
2147 *
2148 * We need to handle endianness, but we don't know the size of
2149 * the unsigned long where the file was generated. Take a best
2150 * guess at determining it: try 64-bit swap first (ie., file
2151 * created on a 64-bit host), and check if the hostname feature
2152 * bit is set (this feature bit is forced on as of fbe96f2).
2153 * If the bit is not, undo the 64-bit swap and try a 32-bit
2154 * swap. If the hostname bit is still not set (e.g., older data
2155 * file), punt and fallback to the original behavior --
2156 * clearing all feature bits and setting buildid.
2157 */
David Ahern80c01202012-06-08 11:47:51 -03002158 mem_bswap_64(&header->adds_features,
2159 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002160
2161 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002162 /* unswap as u64 */
2163 mem_bswap_64(&header->adds_features,
2164 BITS_TO_U64(HEADER_FEAT_BITS));
2165
2166 /* unswap as u32 */
2167 mem_bswap_32(&header->adds_features,
2168 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002169 }
2170
2171 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2172 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2173 set_bit(HEADER_BUILD_ID, header->adds_features);
2174 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002175 }
2176
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002177 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002178 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002179
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002180 ph->event_offset = header->event_types.offset;
2181 ph->event_size = header->event_types.size;
2182 ph->data_offset = header->data.offset;
2183 ph->data_size = header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002184 return 0;
2185}
2186
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002187static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002188 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002189 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002190{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002191 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002192 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002193 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002194 return 0;
2195 }
2196
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002197 if (feat >= HEADER_LAST_FEATURE) {
2198 pr_debug("unknown feature %d, continuing...\n", feat);
2199 return 0;
2200 }
2201
Robert Richterf1c67db2012-02-10 15:41:56 +01002202 if (!feat_ops[feat].process)
2203 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002204
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002205 return feat_ops[feat].process(section, ph, feat, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002206}
2207
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002208static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002209 struct perf_header *ph, int fd,
2210 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002211{
Stephane Eranian73323f52012-02-02 13:54:44 +01002212 int ret;
2213
2214 ret = readn(fd, header, sizeof(*header));
2215 if (ret <= 0)
2216 return -1;
2217
Stephane Eranian114382a2012-02-09 23:21:08 +01002218 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2219 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002220 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002221 }
2222
2223 if (ph->needs_swap)
2224 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002225
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002226 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002227 return -1;
2228
Tom Zanussi8dc58102010-04-01 23:59:15 -05002229 return 0;
2230}
2231
2232static int perf_header__read_pipe(struct perf_session *session, int fd)
2233{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002234 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002235 struct perf_pipe_file_header f_header;
2236
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002237 if (perf_file_header__read_pipe(&f_header, header, fd,
Tom Zanussi454c4072010-05-01 01:41:20 -05002238 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002239 pr_debug("incompatible file format\n");
2240 return -EINVAL;
2241 }
2242
2243 session->fd = fd;
2244
2245 return 0;
2246}
2247
Stephane Eranian69996df2012-02-09 23:21:06 +01002248static int read_attr(int fd, struct perf_header *ph,
2249 struct perf_file_attr *f_attr)
2250{
2251 struct perf_event_attr *attr = &f_attr->attr;
2252 size_t sz, left;
2253 size_t our_sz = sizeof(f_attr->attr);
2254 int ret;
2255
2256 memset(f_attr, 0, sizeof(*f_attr));
2257
2258 /* read minimal guaranteed structure */
2259 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2260 if (ret <= 0) {
2261 pr_debug("cannot read %d bytes of header attr\n",
2262 PERF_ATTR_SIZE_VER0);
2263 return -1;
2264 }
2265
2266 /* on file perf_event_attr size */
2267 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002268
Stephane Eranian69996df2012-02-09 23:21:06 +01002269 if (ph->needs_swap)
2270 sz = bswap_32(sz);
2271
2272 if (sz == 0) {
2273 /* assume ABI0 */
2274 sz = PERF_ATTR_SIZE_VER0;
2275 } else if (sz > our_sz) {
2276 pr_debug("file uses a more recent and unsupported ABI"
2277 " (%zu bytes extra)\n", sz - our_sz);
2278 return -1;
2279 }
2280 /* what we have not yet read and that we know about */
2281 left = sz - PERF_ATTR_SIZE_VER0;
2282 if (left) {
2283 void *ptr = attr;
2284 ptr += PERF_ATTR_SIZE_VER0;
2285
2286 ret = readn(fd, ptr, left);
2287 }
2288 /* read perf_file_section, ids are read in caller */
2289 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2290
2291 return ret <= 0 ? -1 : 0;
2292}
2293
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002294static int perf_evsel__set_tracepoint_name(struct perf_evsel *evsel,
2295 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002296{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002297 struct event_format *event = pevent_find_event(pevent,
2298 evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002299 char bf[128];
2300
2301 if (event == NULL)
2302 return -1;
2303
2304 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2305 evsel->name = strdup(bf);
2306 if (event->name == NULL)
2307 return -1;
2308
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002309 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002310 return 0;
2311}
2312
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002313static int perf_evlist__set_tracepoint_names(struct perf_evlist *evlist,
2314 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002315{
2316 struct perf_evsel *pos;
2317
2318 list_for_each_entry(pos, &evlist->entries, node) {
2319 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002320 perf_evsel__set_tracepoint_name(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002321 return -1;
2322 }
2323
2324 return 0;
2325}
2326
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002327int perf_session__read_header(struct perf_session *session, int fd)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002328{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002329 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002330 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002331 struct perf_file_attr f_attr;
2332 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002333 int nr_attrs, nr_ids, i, j;
2334
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002335 session->evlist = perf_evlist__new(NULL, NULL);
2336 if (session->evlist == NULL)
2337 return -ENOMEM;
2338
Tom Zanussi8dc58102010-04-01 23:59:15 -05002339 if (session->fd_pipe)
2340 return perf_header__read_pipe(session, fd);
2341
Stephane Eranian69996df2012-02-09 23:21:06 +01002342 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002343 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002344
Stephane Eranian69996df2012-02-09 23:21:06 +01002345 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002346 lseek(fd, f_header.attrs.offset, SEEK_SET);
2347
2348 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002349 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002350 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002351
Stephane Eranian69996df2012-02-09 23:21:06 +01002352 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002353 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002354
David Aherneda39132011-07-15 12:34:09 -06002355 if (header->needs_swap)
2356 perf_event__attr_swap(&f_attr.attr);
2357
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002358 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002359 evsel = perf_evsel__new(&f_attr.attr, i);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002360
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002361 if (evsel == NULL)
2362 goto out_delete_evlist;
2363 /*
2364 * Do it before so that if perf_evsel__alloc_id fails, this
2365 * entry gets purged too at perf_evlist__delete().
2366 */
2367 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002368
2369 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002370 /*
2371 * We don't have the cpu and thread maps on the header, so
2372 * for allocating the perf_sample_id table we fake 1 cpu and
2373 * hattr->ids threads.
2374 */
2375 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2376 goto out_delete_evlist;
2377
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002378 lseek(fd, f_attr.ids.offset, SEEK_SET);
2379
2380 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002381 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002382 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002383
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002384 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002385 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002386
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002387 lseek(fd, tmp, SEEK_SET);
2388 }
2389
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002390 symbol_conf.nr_events = nr_attrs;
2391
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02002392 if (f_header.event_types.size) {
2393 lseek(fd, f_header.event_types.offset, SEEK_SET);
Robert Richterdb146f02012-08-16 21:10:20 +02002394 trace_events = malloc(f_header.event_types.size);
2395 if (trace_events == NULL)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002396 return -ENOMEM;
Robert Richterdb146f02012-08-16 21:10:20 +02002397 if (perf_header__getbuffer64(header, fd, trace_events,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002398 f_header.event_types.size))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002399 goto out_errno;
Robert Richterdb146f02012-08-16 21:10:20 +02002400 trace_event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02002401 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +02002402
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002403 perf_header__process_sections(header, fd, &session->pevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002404 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002405
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002406 lseek(fd, header->data_offset, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002407
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002408 if (perf_evlist__set_tracepoint_names(session->evlist, session->pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002409 goto out_delete_evlist;
2410
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002411 header->frozen = 1;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002412 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002413out_errno:
2414 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002415
2416out_delete_evlist:
2417 perf_evlist__delete(session->evlist);
2418 session->evlist = NULL;
2419 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002420}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002421
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002422int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002423 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002424 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002425{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002426 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002427 size_t size;
2428 int err;
2429
2430 size = sizeof(struct perf_event_attr);
2431 size = ALIGN(size, sizeof(u64));
2432 size += sizeof(struct perf_event_header);
2433 size += ids * sizeof(u64);
2434
2435 ev = malloc(size);
2436
Chris Samuelce47dc52010-11-13 13:35:06 +11002437 if (ev == NULL)
2438 return -ENOMEM;
2439
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002440 ev->attr.attr = *attr;
2441 memcpy(ev->attr.id, id, ids * sizeof(u64));
2442
2443 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002444 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002445
Robert Richterf4d83432012-08-16 21:10:17 +02002446 if (ev->attr.header.size == size)
2447 err = process(tool, ev, NULL, NULL);
2448 else
2449 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002450
2451 free(ev);
2452
2453 return err;
2454}
2455
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002456int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002457 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002458 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002459{
Robert Richter6606f872012-08-16 21:10:19 +02002460 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002461 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002462
Robert Richter6606f872012-08-16 21:10:19 +02002463 list_for_each_entry(evsel, &session->evlist->entries, node) {
2464 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2465 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002466 if (err) {
2467 pr_debug("failed to create perf header attribute\n");
2468 return err;
2469 }
2470 }
2471
2472 return err;
2473}
2474
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002475int perf_event__process_attr(union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002476 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002477{
Robert Richterf4d83432012-08-16 21:10:17 +02002478 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002479 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002480 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002481
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002482 if (evlist == NULL) {
2483 *pevlist = evlist = perf_evlist__new(NULL, NULL);
2484 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002485 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002486 }
2487
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002488 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002489 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002490 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002491
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002492 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002493
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002494 ids = event->header.size;
2495 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002496 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002497 /*
2498 * We don't have the cpu and thread maps on the header, so
2499 * for allocating the perf_sample_id table we fake 1 cpu and
2500 * hattr->ids threads.
2501 */
2502 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2503 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002504
2505 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002506 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002507 }
2508
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002509 return 0;
2510}
Tom Zanussicd19a032010-04-01 23:59:20 -05002511
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002512int perf_event__synthesize_event_type(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002513 u64 event_id, char *name,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002514 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002515 struct machine *machine)
Tom Zanussicd19a032010-04-01 23:59:20 -05002516{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002517 union perf_event ev;
Tom Zanussicd19a032010-04-01 23:59:20 -05002518 size_t size = 0;
2519 int err = 0;
2520
2521 memset(&ev, 0, sizeof(ev));
2522
2523 ev.event_type.event_type.event_id = event_id;
2524 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
2525 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
2526
2527 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
Stephane Eranianf8f4b282012-01-20 10:49:12 +01002528 size = strlen(ev.event_type.event_type.name);
Tom Zanussicd19a032010-04-01 23:59:20 -05002529 size = ALIGN(size, sizeof(u64));
2530 ev.event_type.header.size = sizeof(ev.event_type) -
2531 (sizeof(ev.event_type.event_type.name) - size);
2532
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002533 err = process(tool, &ev, NULL, machine);
Tom Zanussicd19a032010-04-01 23:59:20 -05002534
2535 return err;
2536}
2537
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002538int perf_event__synthesize_event_types(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002539 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002540 struct machine *machine)
Tom Zanussicd19a032010-04-01 23:59:20 -05002541{
2542 struct perf_trace_event_type *type;
2543 int i, err = 0;
2544
Robert Richterdb146f02012-08-16 21:10:20 +02002545 for (i = 0; i < trace_event_count; i++) {
2546 type = &trace_events[i];
Tom Zanussicd19a032010-04-01 23:59:20 -05002547
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002548 err = perf_event__synthesize_event_type(tool, type->event_id,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002549 type->name, process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002550 machine);
Tom Zanussicd19a032010-04-01 23:59:20 -05002551 if (err) {
2552 pr_debug("failed to create perf header event type\n");
2553 return err;
2554 }
2555 }
2556
2557 return err;
2558}
2559
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002560int perf_event__process_event_type(struct perf_tool *tool __unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002561 union perf_event *event)
Tom Zanussicd19a032010-04-01 23:59:20 -05002562{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002563 if (perf_header__push_event(event->event_type.event_type.event_id,
2564 event->event_type.event_type.name) < 0)
Tom Zanussicd19a032010-04-01 23:59:20 -05002565 return -ENOMEM;
2566
2567 return 0;
2568}
Tom Zanussi92155452010-04-01 23:59:21 -05002569
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002570int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002571 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002572 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05002573{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002574 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02002575 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05002576 ssize_t size = 0, aligned_size = 0, padding;
Kyle McMartinfb7d0b32011-01-24 11:13:04 -05002577 int err __used = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05002578
Jiri Olsa29208e52011-10-20 15:59:43 +02002579 /*
2580 * We are going to store the size of the data followed
2581 * by the data contents. Since the fd descriptor is a pipe,
2582 * we cannot seek back to store the size of the data once
2583 * we know it. Instead we:
2584 *
2585 * - write the tracing data to the temp file
2586 * - get/write the data size to pipe
2587 * - write the tracing data from the temp file
2588 * to the pipe
2589 */
2590 tdata = tracing_data_get(&evlist->entries, fd, true);
2591 if (!tdata)
2592 return -1;
2593
Tom Zanussi92155452010-04-01 23:59:21 -05002594 memset(&ev, 0, sizeof(ev));
2595
2596 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02002597 size = tdata->size;
Tom Zanussi92155452010-04-01 23:59:21 -05002598 aligned_size = ALIGN(size, sizeof(u64));
2599 padding = aligned_size - size;
2600 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2601 ev.tracing_data.size = aligned_size;
2602
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002603 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05002604
Jiri Olsa29208e52011-10-20 15:59:43 +02002605 /*
2606 * The put function will copy all the tracing data
2607 * stored in temp file to the pipe.
2608 */
2609 tracing_data_put(tdata);
2610
Tom Zanussi92155452010-04-01 23:59:21 -05002611 write_padded(fd, NULL, 0, padding);
2612
2613 return aligned_size;
2614}
2615
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002616int perf_event__process_tracing_data(union perf_event *event,
2617 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05002618{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002619 ssize_t size_read, padding, size = event->tracing_data.size;
Tom Zanussi92155452010-04-01 23:59:21 -05002620 off_t offset = lseek(session->fd, 0, SEEK_CUR);
2621 char buf[BUFSIZ];
2622
2623 /* setup for reading amidst mmap */
2624 lseek(session->fd, offset + sizeof(struct tracing_data_event),
2625 SEEK_SET);
2626
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002627 size_read = trace_report(session->fd, &session->pevent,
2628 session->repipe);
Tom Zanussi92155452010-04-01 23:59:21 -05002629 padding = ALIGN(size_read, sizeof(u64)) - size_read;
2630
2631 if (read(session->fd, buf, padding) < 0)
2632 die("reading input file");
Tom Zanussi454c4072010-05-01 01:41:20 -05002633 if (session->repipe) {
2634 int retw = write(STDOUT_FILENO, buf, padding);
2635 if (retw <= 0 || retw != padding)
2636 die("repiping tracing data padding");
2637 }
Tom Zanussi92155452010-04-01 23:59:21 -05002638
2639 if (size_read + padding != size)
2640 die("tracing data size mismatch");
2641
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03002642 perf_evlist__set_tracepoint_names(session->evlist, session->pevent);
2643
Tom Zanussi92155452010-04-01 23:59:21 -05002644 return size_read + padding;
2645}
Tom Zanussic7929e42010-04-01 23:59:22 -05002646
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002647int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002648 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002649 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002650 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05002651{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002652 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05002653 size_t len;
2654 int err = 0;
2655
2656 if (!pos->hit)
2657 return err;
2658
2659 memset(&ev, 0, sizeof(ev));
2660
2661 len = pos->long_name_len + 1;
2662 len = ALIGN(len, NAME_ALIGN);
2663 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2664 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2665 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002666 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05002667 ev.build_id.header.size = sizeof(ev.build_id) + len;
2668 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2669
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002670 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05002671
2672 return err;
2673}
2674
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002675int perf_event__process_build_id(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002676 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002677 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05002678{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002679 __event_process_build_id(&event->build_id,
2680 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002681 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05002682 return 0;
2683}
Stephane Eraniana1ac1d32010-06-17 11:39:01 +02002684
2685void disable_buildid_cache(void)
2686{
2687 no_buildid_cache = true;
2688}