blob: 69374deeb4a81c28faa256b69d17369a497eeb2e [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"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020023
Stephane Eraniana1ac1d32010-06-17 11:39:01 +020024static bool no_buildid_cache = false;
25
Robert Richterdb146f02012-08-16 21:10:20 +020026static int trace_event_count;
27static struct perf_trace_event_type *trace_events;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020028
Stephane Eranianfbe96f22011-09-30 15:40:40 +020029static u32 header_argc;
30static const char **header_argv;
31
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -020032int perf_header__push_event(u64 id, const char *name)
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020033{
Ulrich Drepperf3054c72012-04-23 09:04:29 -040034 struct perf_trace_event_type *nevents;
35
Arjan van de Venec60a3f2009-09-19 13:36:30 +020036 if (strlen(name) > MAX_EVENT_NAME)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020037 pr_warning("Event %s will be truncated\n", name);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020038
Robert Richterdb146f02012-08-16 21:10:20 +020039 nevents = realloc(trace_events, (trace_event_count + 1) * sizeof(*trace_events));
Ulrich Drepperf3054c72012-04-23 09:04:29 -040040 if (nevents == NULL)
41 return -ENOMEM;
Robert Richterdb146f02012-08-16 21:10:20 +020042 trace_events = nevents;
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -020043
Robert Richterdb146f02012-08-16 21:10:20 +020044 memset(&trace_events[trace_event_count], 0, sizeof(struct perf_trace_event_type));
45 trace_events[trace_event_count].event_id = id;
46 strncpy(trace_events[trace_event_count].name, name, MAX_EVENT_NAME - 1);
47 trace_event_count++;
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -020048 return 0;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020049}
50
51char *perf_header__find_event(u64 id)
52{
53 int i;
Robert Richterdb146f02012-08-16 21:10:20 +020054 for (i = 0 ; i < trace_event_count; i++) {
55 if (trace_events[i].event_id == id)
56 return trace_events[i].name;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020057 }
58 return NULL;
59}
60
Stephane Eranian73323f52012-02-02 13:54:44 +010061/*
62 * magic2 = "PERFILE2"
63 * must be a numerical value to let the endianness
64 * determine the memory layout. That way we are able
65 * to detect endianness when reading the perf.data file
66 * back.
67 *
68 * we check for legacy (PERFFILE) format.
69 */
70static const char *__perf_magic1 = "PERFFILE";
71static const u64 __perf_magic2 = 0x32454c4946524550ULL;
72static const u64 __perf_magic2_sw = 0x50455246494c4532ULL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020073
Stephane Eranian73323f52012-02-02 13:54:44 +010074#define PERF_MAGIC __perf_magic2
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020075
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020076struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020077 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020078 struct perf_file_section ids;
79};
80
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030081void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020082{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030083 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020084}
85
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030086void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020087{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030088 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020089}
90
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030091bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020092{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030093 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020094}
95
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020096static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020097{
98 while (size) {
99 int ret = write(fd, buf, size);
100
101 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -0200102 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200103
104 size -= ret;
105 buf += ret;
106 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200107
108 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200109}
110
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200111#define NAME_ALIGN 64
112
113static int write_padded(int fd, const void *bf, size_t count,
114 size_t count_aligned)
115{
116 static const char zero_buf[NAME_ALIGN];
117 int err = do_write(fd, bf, count);
118
119 if (!err)
120 err = do_write(fd, zero_buf, count_aligned - count);
121
122 return err;
123}
124
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200125static int do_write_string(int fd, const char *str)
126{
127 u32 len, olen;
128 int ret;
129
130 olen = strlen(str) + 1;
131 len = ALIGN(olen, NAME_ALIGN);
132
133 /* write len, incl. \0 */
134 ret = do_write(fd, &len, sizeof(len));
135 if (ret < 0)
136 return ret;
137
138 return write_padded(fd, str, olen, len);
139}
140
141static char *do_read_string(int fd, struct perf_header *ph)
142{
143 ssize_t sz, ret;
144 u32 len;
145 char *buf;
146
147 sz = read(fd, &len, sizeof(len));
148 if (sz < (ssize_t)sizeof(len))
149 return NULL;
150
151 if (ph->needs_swap)
152 len = bswap_32(len);
153
154 buf = malloc(len);
155 if (!buf)
156 return NULL;
157
158 ret = read(fd, buf, len);
159 if (ret == (ssize_t)len) {
160 /*
161 * strings are padded by zeroes
162 * thus the actual strlen of buf
163 * may be less than len
164 */
165 return buf;
166 }
167
168 free(buf);
169 return NULL;
170}
171
172int
173perf_header__set_cmdline(int argc, const char **argv)
174{
175 int i;
176
David Ahern56e6f602012-07-29 20:53:51 -0600177 /*
178 * If header_argv has already been set, do not override it.
179 * This allows a command to set the cmdline, parse args and
180 * then call another builtin function that implements a
181 * command -- e.g, cmd_kvm calling cmd_record.
182 */
183 if (header_argv)
184 return 0;
185
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200186 header_argc = (u32)argc;
187
188 /* do not include NULL termination */
189 header_argv = calloc(argc, sizeof(char *));
190 if (!header_argv)
191 return -ENOMEM;
192
193 /*
194 * must copy argv contents because it gets moved
195 * around during option parsing
196 */
197 for (i = 0; i < argc ; i++)
198 header_argv[i] = argv[i];
199
200 return 0;
201}
202
Robert Richter1b549502011-12-07 10:02:53 +0100203#define dsos__for_each_with_build_id(pos, head) \
204 list_for_each_entry(pos, head, node) \
205 if (!pos->has_build_id) \
206 continue; \
207 else
208
209static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
210 u16 misc, int fd)
211{
212 struct dso *pos;
213
214 dsos__for_each_with_build_id(pos, head) {
215 int err;
216 struct build_id_event b;
217 size_t len;
218
219 if (!pos->hit)
220 continue;
221 len = pos->long_name_len + 1;
222 len = ALIGN(len, NAME_ALIGN);
223 memset(&b, 0, sizeof(b));
224 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
225 b.pid = pid;
226 b.header.misc = misc;
227 b.header.size = sizeof(b) + len;
228 err = do_write(fd, &b, sizeof(b));
229 if (err < 0)
230 return err;
231 err = write_padded(fd, pos->long_name,
232 pos->long_name_len + 1, len);
233 if (err < 0)
234 return err;
235 }
236
237 return 0;
238}
239
240static int machine__write_buildid_table(struct machine *machine, int fd)
241{
242 int err;
243 u16 kmisc = PERF_RECORD_MISC_KERNEL,
244 umisc = PERF_RECORD_MISC_USER;
245
246 if (!machine__is_host(machine)) {
247 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
248 umisc = PERF_RECORD_MISC_GUEST_USER;
249 }
250
251 err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid,
252 kmisc, fd);
253 if (err == 0)
254 err = __dsos__write_buildid_table(&machine->user_dsos,
255 machine->pid, umisc, fd);
256 return err;
257}
258
259static int dsos__write_buildid_table(struct perf_header *header, int fd)
260{
261 struct perf_session *session = container_of(header,
262 struct perf_session, header);
263 struct rb_node *nd;
264 int err = machine__write_buildid_table(&session->host_machine, fd);
265
266 if (err)
267 return err;
268
269 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
270 struct machine *pos = rb_entry(nd, struct machine, rb_node);
271 err = machine__write_buildid_table(pos, fd);
272 if (err)
273 break;
274 }
275 return err;
276}
277
278int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
279 const char *name, bool is_kallsyms)
280{
281 const size_t size = PATH_MAX;
282 char *realname, *filename = zalloc(size),
283 *linkname = zalloc(size), *targetname;
284 int len, err = -1;
285
286 if (is_kallsyms) {
287 if (symbol_conf.kptr_restrict) {
288 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
289 return 0;
290 }
291 realname = (char *)name;
292 } else
293 realname = realpath(name, NULL);
294
295 if (realname == NULL || filename == NULL || linkname == NULL)
296 goto out_free;
297
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300298 len = scnprintf(filename, size, "%s%s%s",
Robert Richter1b549502011-12-07 10:02:53 +0100299 debugdir, is_kallsyms ? "/" : "", realname);
300 if (mkdir_p(filename, 0755))
301 goto out_free;
302
Namhyung Kimafda0f92012-05-01 23:19:36 +0900303 snprintf(filename + len, size - len, "/%s", sbuild_id);
Robert Richter1b549502011-12-07 10:02:53 +0100304
305 if (access(filename, F_OK)) {
306 if (is_kallsyms) {
307 if (copyfile("/proc/kallsyms", filename))
308 goto out_free;
309 } else if (link(realname, filename) && copyfile(name, filename))
310 goto out_free;
311 }
312
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -0300313 len = scnprintf(linkname, size, "%s/.build-id/%.2s",
Robert Richter1b549502011-12-07 10:02:53 +0100314 debugdir, sbuild_id);
315
316 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
317 goto out_free;
318
319 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
320 targetname = filename + strlen(debugdir) - 5;
321 memcpy(targetname, "../..", 5);
322
323 if (symlink(targetname, linkname) == 0)
324 err = 0;
325out_free:
326 if (!is_kallsyms)
327 free(realname);
328 free(filename);
329 free(linkname);
330 return err;
331}
332
333static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
334 const char *name, const char *debugdir,
335 bool is_kallsyms)
336{
337 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
338
339 build_id__sprintf(build_id, build_id_size, sbuild_id);
340
341 return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms);
342}
343
344int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
345{
346 const size_t size = PATH_MAX;
347 char *filename = zalloc(size),
348 *linkname = zalloc(size);
349 int err = -1;
350
351 if (filename == NULL || linkname == NULL)
352 goto out_free;
353
354 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
355 debugdir, sbuild_id, sbuild_id + 2);
356
357 if (access(linkname, F_OK))
358 goto out_free;
359
360 if (readlink(linkname, filename, size - 1) < 0)
361 goto out_free;
362
363 if (unlink(linkname))
364 goto out_free;
365
366 /*
367 * Since the link is relative, we must make it absolute:
368 */
369 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
370 debugdir, sbuild_id, filename);
371
372 if (unlink(linkname))
373 goto out_free;
374
375 err = 0;
376out_free:
377 free(filename);
378 free(linkname);
379 return err;
380}
381
382static int dso__cache_build_id(struct dso *dso, const char *debugdir)
383{
384 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
385
386 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id),
387 dso->long_name, debugdir, is_kallsyms);
388}
389
390static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
391{
392 struct dso *pos;
393 int err = 0;
394
395 dsos__for_each_with_build_id(pos, head)
396 if (dso__cache_build_id(pos, debugdir))
397 err = -1;
398
399 return err;
400}
401
402static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
403{
404 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir);
405 ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir);
406 return ret;
407}
408
409static int perf_session__cache_build_ids(struct perf_session *session)
410{
411 struct rb_node *nd;
412 int ret;
413 char debugdir[PATH_MAX];
414
415 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
416
417 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
418 return -1;
419
420 ret = machine__cache_build_ids(&session->host_machine, debugdir);
421
422 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
423 struct machine *pos = rb_entry(nd, struct machine, rb_node);
424 ret |= machine__cache_build_ids(pos, debugdir);
425 }
426 return ret ? -1 : 0;
427}
428
429static bool machine__read_build_ids(struct machine *machine, bool with_hits)
430{
431 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
432 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
433 return ret;
434}
435
436static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
437{
438 struct rb_node *nd;
439 bool ret = machine__read_build_ids(&session->host_machine, with_hits);
440
441 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
442 struct machine *pos = rb_entry(nd, struct machine, rb_node);
443 ret |= machine__read_build_ids(pos, with_hits);
444 }
445
446 return ret;
447}
448
Stephane Eranian2eeaaa02012-05-15 13:28:13 +0200449static int write_tracing_data(int fd, struct perf_header *h __used,
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200450 struct perf_evlist *evlist)
451{
452 return read_tracing_data(fd, &evlist->entries);
453}
454
455
456static int write_build_id(int fd, struct perf_header *h,
457 struct perf_evlist *evlist __used)
458{
459 struct perf_session *session;
460 int err;
461
462 session = container_of(h, struct perf_session, header);
463
Robert Richtere20960c2011-12-07 10:02:55 +0100464 if (!perf_session__read_build_ids(session, true))
465 return -1;
466
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200467 err = dsos__write_buildid_table(h, fd);
468 if (err < 0) {
469 pr_debug("failed to write buildid table\n");
470 return err;
471 }
472 if (!no_buildid_cache)
473 perf_session__cache_build_ids(session);
474
475 return 0;
476}
477
478static int write_hostname(int fd, struct perf_header *h __used,
479 struct perf_evlist *evlist __used)
480{
481 struct utsname uts;
482 int ret;
483
484 ret = uname(&uts);
485 if (ret < 0)
486 return -1;
487
488 return do_write_string(fd, uts.nodename);
489}
490
491static int write_osrelease(int fd, struct perf_header *h __used,
492 struct perf_evlist *evlist __used)
493{
494 struct utsname uts;
495 int ret;
496
497 ret = uname(&uts);
498 if (ret < 0)
499 return -1;
500
501 return do_write_string(fd, uts.release);
502}
503
504static int write_arch(int fd, struct perf_header *h __used,
505 struct perf_evlist *evlist __used)
506{
507 struct utsname uts;
508 int ret;
509
510 ret = uname(&uts);
511 if (ret < 0)
512 return -1;
513
514 return do_write_string(fd, uts.machine);
515}
516
517static int write_version(int fd, struct perf_header *h __used,
518 struct perf_evlist *evlist __used)
519{
520 return do_write_string(fd, perf_version_string);
521}
522
523static int write_cpudesc(int fd, struct perf_header *h __used,
524 struct perf_evlist *evlist __used)
525{
526#ifndef CPUINFO_PROC
527#define CPUINFO_PROC NULL
528#endif
529 FILE *file;
530 char *buf = NULL;
531 char *s, *p;
532 const char *search = CPUINFO_PROC;
533 size_t len = 0;
534 int ret = -1;
535
536 if (!search)
537 return -1;
538
539 file = fopen("/proc/cpuinfo", "r");
540 if (!file)
541 return -1;
542
543 while (getline(&buf, &len, file) > 0) {
544 ret = strncmp(buf, search, strlen(search));
545 if (!ret)
546 break;
547 }
548
549 if (ret)
550 goto done;
551
552 s = buf;
553
554 p = strchr(buf, ':');
555 if (p && *(p+1) == ' ' && *(p+2))
556 s = p + 2;
557 p = strchr(s, '\n');
558 if (p)
559 *p = '\0';
560
561 /* squash extra space characters (branding string) */
562 p = s;
563 while (*p) {
564 if (isspace(*p)) {
565 char *r = p + 1;
566 char *q = r;
567 *p = ' ';
568 while (*q && isspace(*q))
569 q++;
570 if (q != (p+1))
571 while ((*r++ = *q++));
572 }
573 p++;
574 }
575 ret = do_write_string(fd, s);
576done:
577 free(buf);
578 fclose(file);
579 return ret;
580}
581
582static int write_nrcpus(int fd, struct perf_header *h __used,
583 struct perf_evlist *evlist __used)
584{
585 long nr;
586 u32 nrc, nra;
587 int ret;
588
589 nr = sysconf(_SC_NPROCESSORS_CONF);
590 if (nr < 0)
591 return -1;
592
593 nrc = (u32)(nr & UINT_MAX);
594
595 nr = sysconf(_SC_NPROCESSORS_ONLN);
596 if (nr < 0)
597 return -1;
598
599 nra = (u32)(nr & UINT_MAX);
600
601 ret = do_write(fd, &nrc, sizeof(nrc));
602 if (ret < 0)
603 return ret;
604
605 return do_write(fd, &nra, sizeof(nra));
606}
607
608static int write_event_desc(int fd, struct perf_header *h __used,
609 struct perf_evlist *evlist)
610{
Robert Richter6606f872012-08-16 21:10:19 +0200611 struct perf_evsel *evsel;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200612 u32 nre = 0, nri, sz;
613 int ret;
614
Robert Richter6606f872012-08-16 21:10:19 +0200615 list_for_each_entry(evsel, &evlist->entries, node)
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200616 nre++;
617
618 /*
619 * write number of events
620 */
621 ret = do_write(fd, &nre, sizeof(nre));
622 if (ret < 0)
623 return ret;
624
625 /*
626 * size of perf_event_attr struct
627 */
Robert Richter6606f872012-08-16 21:10:19 +0200628 sz = (u32)sizeof(evsel->attr);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200629 ret = do_write(fd, &sz, sizeof(sz));
630 if (ret < 0)
631 return ret;
632
Robert Richter6606f872012-08-16 21:10:19 +0200633 list_for_each_entry(evsel, &evlist->entries, node) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200634
Robert Richter6606f872012-08-16 21:10:19 +0200635 ret = do_write(fd, &evsel->attr, sz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200636 if (ret < 0)
637 return ret;
638 /*
639 * write number of unique id per event
640 * there is one id per instance of an event
641 *
642 * copy into an nri to be independent of the
643 * type of ids,
644 */
Robert Richter6606f872012-08-16 21:10:19 +0200645 nri = evsel->ids;
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200646 ret = do_write(fd, &nri, sizeof(nri));
647 if (ret < 0)
648 return ret;
649
650 /*
651 * write event string as passed on cmdline
652 */
Robert Richter6606f872012-08-16 21:10:19 +0200653 ret = do_write_string(fd, perf_evsel__name(evsel));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200654 if (ret < 0)
655 return ret;
656 /*
657 * write unique ids for this event
658 */
Robert Richter6606f872012-08-16 21:10:19 +0200659 ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200660 if (ret < 0)
661 return ret;
662 }
663 return 0;
664}
665
666static int write_cmdline(int fd, struct perf_header *h __used,
667 struct perf_evlist *evlist __used)
668{
669 char buf[MAXPATHLEN];
670 char proc[32];
671 u32 i, n;
672 int ret;
673
674 /*
675 * actual atual path to perf binary
676 */
677 sprintf(proc, "/proc/%d/exe", getpid());
678 ret = readlink(proc, buf, sizeof(buf));
679 if (ret <= 0)
680 return -1;
681
682 /* readlink() does not add null termination */
683 buf[ret] = '\0';
684
685 /* account for binary path */
686 n = header_argc + 1;
687
688 ret = do_write(fd, &n, sizeof(n));
689 if (ret < 0)
690 return ret;
691
692 ret = do_write_string(fd, buf);
693 if (ret < 0)
694 return ret;
695
696 for (i = 0 ; i < header_argc; i++) {
697 ret = do_write_string(fd, header_argv[i]);
698 if (ret < 0)
699 return ret;
700 }
701 return 0;
702}
703
704#define CORE_SIB_FMT \
705 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
706#define THRD_SIB_FMT \
707 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
708
709struct cpu_topo {
710 u32 core_sib;
711 u32 thread_sib;
712 char **core_siblings;
713 char **thread_siblings;
714};
715
716static int build_cpu_topo(struct cpu_topo *tp, int cpu)
717{
718 FILE *fp;
719 char filename[MAXPATHLEN];
720 char *buf = NULL, *p;
721 size_t len = 0;
722 u32 i = 0;
723 int ret = -1;
724
725 sprintf(filename, CORE_SIB_FMT, cpu);
726 fp = fopen(filename, "r");
727 if (!fp)
728 return -1;
729
730 if (getline(&buf, &len, fp) <= 0)
731 goto done;
732
733 fclose(fp);
734
735 p = strchr(buf, '\n');
736 if (p)
737 *p = '\0';
738
739 for (i = 0; i < tp->core_sib; i++) {
740 if (!strcmp(buf, tp->core_siblings[i]))
741 break;
742 }
743 if (i == tp->core_sib) {
744 tp->core_siblings[i] = buf;
745 tp->core_sib++;
746 buf = NULL;
747 len = 0;
748 }
749
750 sprintf(filename, THRD_SIB_FMT, cpu);
751 fp = fopen(filename, "r");
752 if (!fp)
753 goto done;
754
755 if (getline(&buf, &len, fp) <= 0)
756 goto done;
757
758 p = strchr(buf, '\n');
759 if (p)
760 *p = '\0';
761
762 for (i = 0; i < tp->thread_sib; i++) {
763 if (!strcmp(buf, tp->thread_siblings[i]))
764 break;
765 }
766 if (i == tp->thread_sib) {
767 tp->thread_siblings[i] = buf;
768 tp->thread_sib++;
769 buf = NULL;
770 }
771 ret = 0;
772done:
773 if(fp)
774 fclose(fp);
775 free(buf);
776 return ret;
777}
778
779static void free_cpu_topo(struct cpu_topo *tp)
780{
781 u32 i;
782
783 if (!tp)
784 return;
785
786 for (i = 0 ; i < tp->core_sib; i++)
787 free(tp->core_siblings[i]);
788
789 for (i = 0 ; i < tp->thread_sib; i++)
790 free(tp->thread_siblings[i]);
791
792 free(tp);
793}
794
795static struct cpu_topo *build_cpu_topology(void)
796{
797 struct cpu_topo *tp;
798 void *addr;
799 u32 nr, i;
800 size_t sz;
801 long ncpus;
802 int ret = -1;
803
804 ncpus = sysconf(_SC_NPROCESSORS_CONF);
805 if (ncpus < 0)
806 return NULL;
807
808 nr = (u32)(ncpus & UINT_MAX);
809
810 sz = nr * sizeof(char *);
811
812 addr = calloc(1, sizeof(*tp) + 2 * sz);
813 if (!addr)
814 return NULL;
815
816 tp = addr;
817
818 addr += sizeof(*tp);
819 tp->core_siblings = addr;
820 addr += sz;
821 tp->thread_siblings = addr;
822
823 for (i = 0; i < nr; i++) {
824 ret = build_cpu_topo(tp, i);
825 if (ret < 0)
826 break;
827 }
828 if (ret) {
829 free_cpu_topo(tp);
830 tp = NULL;
831 }
832 return tp;
833}
834
835static int write_cpu_topology(int fd, struct perf_header *h __used,
836 struct perf_evlist *evlist __used)
837{
838 struct cpu_topo *tp;
839 u32 i;
840 int ret;
841
842 tp = build_cpu_topology();
843 if (!tp)
844 return -1;
845
846 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
847 if (ret < 0)
848 goto done;
849
850 for (i = 0; i < tp->core_sib; i++) {
851 ret = do_write_string(fd, tp->core_siblings[i]);
852 if (ret < 0)
853 goto done;
854 }
855 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
856 if (ret < 0)
857 goto done;
858
859 for (i = 0; i < tp->thread_sib; i++) {
860 ret = do_write_string(fd, tp->thread_siblings[i]);
861 if (ret < 0)
862 break;
863 }
864done:
865 free_cpu_topo(tp);
866 return ret;
867}
868
869
870
871static int write_total_mem(int fd, struct perf_header *h __used,
872 struct perf_evlist *evlist __used)
873{
874 char *buf = NULL;
875 FILE *fp;
876 size_t len = 0;
877 int ret = -1, n;
878 uint64_t mem;
879
880 fp = fopen("/proc/meminfo", "r");
881 if (!fp)
882 return -1;
883
884 while (getline(&buf, &len, fp) > 0) {
885 ret = strncmp(buf, "MemTotal:", 9);
886 if (!ret)
887 break;
888 }
889 if (!ret) {
890 n = sscanf(buf, "%*s %"PRIu64, &mem);
891 if (n == 1)
892 ret = do_write(fd, &mem, sizeof(mem));
893 }
894 free(buf);
895 fclose(fp);
896 return ret;
897}
898
899static int write_topo_node(int fd, int node)
900{
901 char str[MAXPATHLEN];
902 char field[32];
903 char *buf = NULL, *p;
904 size_t len = 0;
905 FILE *fp;
906 u64 mem_total, mem_free, mem;
907 int ret = -1;
908
909 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
910 fp = fopen(str, "r");
911 if (!fp)
912 return -1;
913
914 while (getline(&buf, &len, fp) > 0) {
915 /* skip over invalid lines */
916 if (!strchr(buf, ':'))
917 continue;
918 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
919 goto done;
920 if (!strcmp(field, "MemTotal:"))
921 mem_total = mem;
922 if (!strcmp(field, "MemFree:"))
923 mem_free = mem;
924 }
925
926 fclose(fp);
927
928 ret = do_write(fd, &mem_total, sizeof(u64));
929 if (ret)
930 goto done;
931
932 ret = do_write(fd, &mem_free, sizeof(u64));
933 if (ret)
934 goto done;
935
936 ret = -1;
937 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
938
939 fp = fopen(str, "r");
940 if (!fp)
941 goto done;
942
943 if (getline(&buf, &len, fp) <= 0)
944 goto done;
945
946 p = strchr(buf, '\n');
947 if (p)
948 *p = '\0';
949
950 ret = do_write_string(fd, buf);
951done:
952 free(buf);
953 fclose(fp);
954 return ret;
955}
956
957static int write_numa_topology(int fd, struct perf_header *h __used,
958 struct perf_evlist *evlist __used)
959{
960 char *buf = NULL;
961 size_t len = 0;
962 FILE *fp;
963 struct cpu_map *node_map = NULL;
964 char *c;
965 u32 nr, i, j;
966 int ret = -1;
967
968 fp = fopen("/sys/devices/system/node/online", "r");
969 if (!fp)
970 return -1;
971
972 if (getline(&buf, &len, fp) <= 0)
973 goto done;
974
975 c = strchr(buf, '\n');
976 if (c)
977 *c = '\0';
978
979 node_map = cpu_map__new(buf);
980 if (!node_map)
981 goto done;
982
983 nr = (u32)node_map->nr;
984
985 ret = do_write(fd, &nr, sizeof(nr));
986 if (ret < 0)
987 goto done;
988
989 for (i = 0; i < nr; i++) {
990 j = (u32)node_map->map[i];
991 ret = do_write(fd, &j, sizeof(j));
992 if (ret < 0)
993 break;
994
995 ret = write_topo_node(fd, i);
996 if (ret < 0)
997 break;
998 }
999done:
1000 free(buf);
1001 fclose(fp);
1002 free(node_map);
1003 return ret;
1004}
1005
1006/*
1007 * default get_cpuid(): nothing gets recorded
1008 * actual implementation must be in arch/$(ARCH)/util/header.c
1009 */
1010int __attribute__((weak)) get_cpuid(char *buffer __used, size_t sz __used)
1011{
1012 return -1;
1013}
1014
1015static int write_cpuid(int fd, struct perf_header *h __used,
1016 struct perf_evlist *evlist __used)
1017{
1018 char buffer[64];
1019 int ret;
1020
1021 ret = get_cpuid(buffer, sizeof(buffer));
1022 if (!ret)
1023 goto write_it;
1024
1025 return -1;
1026write_it:
1027 return do_write_string(fd, buffer);
1028}
1029
Stephane Eranian330aa672012-03-08 23:47:46 +01001030static int write_branch_stack(int fd __used, struct perf_header *h __used,
1031 struct perf_evlist *evlist __used)
1032{
1033 return 0;
1034}
1035
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001036static void print_hostname(struct perf_header *ph, int fd, FILE *fp)
1037{
1038 char *str = do_read_string(fd, ph);
1039 fprintf(fp, "# hostname : %s\n", str);
1040 free(str);
1041}
1042
1043static void print_osrelease(struct perf_header *ph, int fd, FILE *fp)
1044{
1045 char *str = do_read_string(fd, ph);
1046 fprintf(fp, "# os release : %s\n", str);
1047 free(str);
1048}
1049
1050static void print_arch(struct perf_header *ph, int fd, FILE *fp)
1051{
1052 char *str = do_read_string(fd, ph);
1053 fprintf(fp, "# arch : %s\n", str);
1054 free(str);
1055}
1056
1057static void print_cpudesc(struct perf_header *ph, int fd, FILE *fp)
1058{
1059 char *str = do_read_string(fd, ph);
1060 fprintf(fp, "# cpudesc : %s\n", str);
1061 free(str);
1062}
1063
1064static void print_nrcpus(struct perf_header *ph, int fd, FILE *fp)
1065{
1066 ssize_t ret;
1067 u32 nr;
1068
1069 ret = read(fd, &nr, sizeof(nr));
1070 if (ret != (ssize_t)sizeof(nr))
1071 nr = -1; /* interpreted as error */
1072
1073 if (ph->needs_swap)
1074 nr = bswap_32(nr);
1075
1076 fprintf(fp, "# nrcpus online : %u\n", nr);
1077
1078 ret = read(fd, &nr, sizeof(nr));
1079 if (ret != (ssize_t)sizeof(nr))
1080 nr = -1; /* interpreted as error */
1081
1082 if (ph->needs_swap)
1083 nr = bswap_32(nr);
1084
1085 fprintf(fp, "# nrcpus avail : %u\n", nr);
1086}
1087
1088static void print_version(struct perf_header *ph, int fd, FILE *fp)
1089{
1090 char *str = do_read_string(fd, ph);
1091 fprintf(fp, "# perf version : %s\n", str);
1092 free(str);
1093}
1094
1095static void print_cmdline(struct perf_header *ph, int fd, FILE *fp)
1096{
1097 ssize_t ret;
1098 char *str;
1099 u32 nr, i;
1100
1101 ret = read(fd, &nr, sizeof(nr));
1102 if (ret != (ssize_t)sizeof(nr))
1103 return;
1104
1105 if (ph->needs_swap)
1106 nr = bswap_32(nr);
1107
1108 fprintf(fp, "# cmdline : ");
1109
1110 for (i = 0; i < nr; i++) {
1111 str = do_read_string(fd, ph);
1112 fprintf(fp, "%s ", str);
1113 free(str);
1114 }
1115 fputc('\n', fp);
1116}
1117
1118static void print_cpu_topology(struct perf_header *ph, int fd, FILE *fp)
1119{
1120 ssize_t ret;
1121 u32 nr, i;
1122 char *str;
1123
1124 ret = read(fd, &nr, sizeof(nr));
1125 if (ret != (ssize_t)sizeof(nr))
1126 return;
1127
1128 if (ph->needs_swap)
1129 nr = bswap_32(nr);
1130
1131 for (i = 0; i < nr; i++) {
1132 str = do_read_string(fd, ph);
1133 fprintf(fp, "# sibling cores : %s\n", str);
1134 free(str);
1135 }
1136
1137 ret = read(fd, &nr, sizeof(nr));
1138 if (ret != (ssize_t)sizeof(nr))
1139 return;
1140
1141 if (ph->needs_swap)
1142 nr = bswap_32(nr);
1143
1144 for (i = 0; i < nr; i++) {
1145 str = do_read_string(fd, ph);
1146 fprintf(fp, "# sibling threads : %s\n", str);
1147 free(str);
1148 }
1149}
1150
Robert Richter4e1b9c62012-08-16 21:10:22 +02001151static void free_event_desc(struct perf_evsel *events)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001152{
Robert Richter4e1b9c62012-08-16 21:10:22 +02001153 struct perf_evsel *evsel;
1154
1155 if (!events)
1156 return;
1157
1158 for (evsel = events; evsel->attr.size; evsel++) {
1159 if (evsel->name)
1160 free(evsel->name);
1161 if (evsel->id)
1162 free(evsel->id);
1163 }
1164
1165 free(events);
1166}
1167
1168static struct perf_evsel *
1169read_event_desc(struct perf_header *ph, int fd)
1170{
1171 struct perf_evsel *evsel, *events = NULL;
1172 u64 *id;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001173 void *buf = NULL;
Stephane Eranian62db9062012-02-09 23:21:07 +01001174 u32 nre, sz, nr, i, j;
1175 ssize_t ret;
1176 size_t msz;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001177
1178 /* number of events */
1179 ret = read(fd, &nre, sizeof(nre));
1180 if (ret != (ssize_t)sizeof(nre))
1181 goto error;
1182
1183 if (ph->needs_swap)
1184 nre = bswap_32(nre);
1185
1186 ret = read(fd, &sz, sizeof(sz));
1187 if (ret != (ssize_t)sizeof(sz))
1188 goto error;
1189
1190 if (ph->needs_swap)
1191 sz = bswap_32(sz);
1192
Stephane Eranian62db9062012-02-09 23:21:07 +01001193 /* buffer to hold on file attr struct */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001194 buf = malloc(sz);
1195 if (!buf)
1196 goto error;
1197
Robert Richter4e1b9c62012-08-16 21:10:22 +02001198 /* the last event terminates with evsel->attr.size == 0: */
1199 events = calloc(nre + 1, sizeof(*events));
1200 if (!events)
1201 goto error;
1202
1203 msz = sizeof(evsel->attr);
Jiri Olsa9fafd982012-03-20 19:15:39 +01001204 if (sz < msz)
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001205 msz = sz;
1206
Robert Richter4e1b9c62012-08-16 21:10:22 +02001207 for (i = 0, evsel = events; i < nre; evsel++, i++) {
1208 evsel->idx = i;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001209
Stephane Eranian62db9062012-02-09 23:21:07 +01001210 /*
1211 * must read entire on-file attr struct to
1212 * sync up with layout.
1213 */
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001214 ret = read(fd, buf, sz);
1215 if (ret != (ssize_t)sz)
1216 goto error;
1217
1218 if (ph->needs_swap)
1219 perf_event__attr_swap(buf);
1220
Robert Richter4e1b9c62012-08-16 21:10:22 +02001221 memcpy(&evsel->attr, buf, msz);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001222
1223 ret = read(fd, &nr, sizeof(nr));
1224 if (ret != (ssize_t)sizeof(nr))
1225 goto error;
1226
1227 if (ph->needs_swap)
1228 nr = bswap_32(nr);
1229
Robert Richter4e1b9c62012-08-16 21:10:22 +02001230 evsel->name = do_read_string(fd, ph);
1231
1232 if (!nr)
1233 continue;
1234
1235 id = calloc(nr, sizeof(*id));
1236 if (!id)
1237 goto error;
1238 evsel->ids = nr;
1239 evsel->id = id;
1240
1241 for (j = 0 ; j < nr; j++) {
1242 ret = read(fd, id, sizeof(*id));
1243 if (ret != (ssize_t)sizeof(*id))
1244 goto error;
1245 if (ph->needs_swap)
1246 *id = bswap_64(*id);
1247 id++;
1248 }
1249 }
1250out:
1251 if (buf)
1252 free(buf);
1253 return events;
1254error:
1255 if (events)
1256 free_event_desc(events);
1257 events = NULL;
1258 goto out;
1259}
1260
1261static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1262{
1263 struct perf_evsel *evsel, *events = read_event_desc(ph, fd);
1264 u32 j;
1265 u64 *id;
1266
1267 if (!events) {
1268 fprintf(fp, "# event desc: not available or unable to read\n");
1269 return;
1270 }
1271
1272 for (evsel = events; evsel->attr.size; evsel++) {
1273 fprintf(fp, "# event : name = %s, ", evsel->name);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001274
1275 fprintf(fp, "type = %d, config = 0x%"PRIx64
1276 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
Robert Richter4e1b9c62012-08-16 21:10:22 +02001277 evsel->attr.type,
1278 (u64)evsel->attr.config,
1279 (u64)evsel->attr.config1,
1280 (u64)evsel->attr.config2);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001281
1282 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001283 evsel->attr.exclude_user,
1284 evsel->attr.exclude_kernel);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001285
David Ahern78b961f2012-07-20 17:25:52 -06001286 fprintf(fp, ", excl_host = %d, excl_guest = %d",
Robert Richter4e1b9c62012-08-16 21:10:22 +02001287 evsel->attr.exclude_host,
1288 evsel->attr.exclude_guest);
David Ahern78b961f2012-07-20 17:25:52 -06001289
Robert Richter4e1b9c62012-08-16 21:10:22 +02001290 fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip);
David Ahern78b961f2012-07-20 17:25:52 -06001291
Robert Richter4e1b9c62012-08-16 21:10:22 +02001292 if (evsel->ids) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001293 fprintf(fp, ", id = {");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001294 for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) {
1295 if (j)
1296 fputc(',', fp);
1297 fprintf(fp, " %"PRIu64, *id);
1298 }
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001299 fprintf(fp, " }");
Robert Richter4e1b9c62012-08-16 21:10:22 +02001300 }
1301
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001302 fputc('\n', fp);
1303 }
Robert Richter4e1b9c62012-08-16 21:10:22 +02001304
1305 free_event_desc(events);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001306}
1307
1308static void print_total_mem(struct perf_header *h __used, int fd, FILE *fp)
1309{
1310 uint64_t mem;
1311 ssize_t ret;
1312
1313 ret = read(fd, &mem, sizeof(mem));
1314 if (ret != sizeof(mem))
1315 goto error;
1316
1317 if (h->needs_swap)
1318 mem = bswap_64(mem);
1319
1320 fprintf(fp, "# total memory : %"PRIu64" kB\n", mem);
1321 return;
1322error:
1323 fprintf(fp, "# total memory : unknown\n");
1324}
1325
1326static void print_numa_topology(struct perf_header *h __used, int fd, FILE *fp)
1327{
1328 ssize_t ret;
1329 u32 nr, c, i;
1330 char *str;
1331 uint64_t mem_total, mem_free;
1332
1333 /* nr nodes */
1334 ret = read(fd, &nr, sizeof(nr));
1335 if (ret != (ssize_t)sizeof(nr))
1336 goto error;
1337
1338 if (h->needs_swap)
1339 nr = bswap_32(nr);
1340
1341 for (i = 0; i < nr; i++) {
1342
1343 /* node number */
1344 ret = read(fd, &c, sizeof(c));
1345 if (ret != (ssize_t)sizeof(c))
1346 goto error;
1347
1348 if (h->needs_swap)
1349 c = bswap_32(c);
1350
1351 ret = read(fd, &mem_total, sizeof(u64));
1352 if (ret != sizeof(u64))
1353 goto error;
1354
1355 ret = read(fd, &mem_free, sizeof(u64));
1356 if (ret != sizeof(u64))
1357 goto error;
1358
1359 if (h->needs_swap) {
1360 mem_total = bswap_64(mem_total);
1361 mem_free = bswap_64(mem_free);
1362 }
1363
1364 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1365 " free = %"PRIu64" kB\n",
1366 c,
1367 mem_total,
1368 mem_free);
1369
1370 str = do_read_string(fd, h);
1371 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1372 free(str);
1373 }
1374 return;
1375error:
1376 fprintf(fp, "# numa topology : not available\n");
1377}
1378
1379static void print_cpuid(struct perf_header *ph, int fd, FILE *fp)
1380{
1381 char *str = do_read_string(fd, ph);
1382 fprintf(fp, "# cpuid : %s\n", str);
1383 free(str);
1384}
1385
Stephane Eranian330aa672012-03-08 23:47:46 +01001386static void print_branch_stack(struct perf_header *ph __used, int fd __used,
1387 FILE *fp)
1388{
1389 fprintf(fp, "# contains samples with branch stack\n");
1390}
1391
Robert Richter08d95bd2012-02-10 15:41:55 +01001392static int __event_process_build_id(struct build_id_event *bev,
1393 char *filename,
1394 struct perf_session *session)
1395{
1396 int err = -1;
1397 struct list_head *head;
1398 struct machine *machine;
1399 u16 misc;
1400 struct dso *dso;
1401 enum dso_kernel_type dso_type;
1402
1403 machine = perf_session__findnew_machine(session, bev->pid);
1404 if (!machine)
1405 goto out;
1406
1407 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1408
1409 switch (misc) {
1410 case PERF_RECORD_MISC_KERNEL:
1411 dso_type = DSO_TYPE_KERNEL;
1412 head = &machine->kernel_dsos;
1413 break;
1414 case PERF_RECORD_MISC_GUEST_KERNEL:
1415 dso_type = DSO_TYPE_GUEST_KERNEL;
1416 head = &machine->kernel_dsos;
1417 break;
1418 case PERF_RECORD_MISC_USER:
1419 case PERF_RECORD_MISC_GUEST_USER:
1420 dso_type = DSO_TYPE_USER;
1421 head = &machine->user_dsos;
1422 break;
1423 default:
1424 goto out;
1425 }
1426
1427 dso = __dsos__findnew(head, filename);
1428 if (dso != NULL) {
1429 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1430
1431 dso__set_build_id(dso, &bev->build_id);
1432
1433 if (filename[0] == '[')
1434 dso->kernel = dso_type;
1435
1436 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1437 sbuild_id);
1438 pr_debug("build id event received for %s: %s\n",
1439 dso->long_name, sbuild_id);
1440 }
1441
1442 err = 0;
1443out:
1444 return err;
1445}
1446
1447static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1448 int input, u64 offset, u64 size)
1449{
1450 struct perf_session *session = container_of(header, struct perf_session, header);
1451 struct {
1452 struct perf_event_header header;
1453 u8 build_id[ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1454 char filename[0];
1455 } old_bev;
1456 struct build_id_event bev;
1457 char filename[PATH_MAX];
1458 u64 limit = offset + size;
1459
1460 while (offset < limit) {
1461 ssize_t len;
1462
1463 if (read(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1464 return -1;
1465
1466 if (header->needs_swap)
1467 perf_event_header__bswap(&old_bev.header);
1468
1469 len = old_bev.header.size - sizeof(old_bev);
1470 if (read(input, filename, len) != len)
1471 return -1;
1472
1473 bev.header = old_bev.header;
1474
1475 /*
1476 * As the pid is the missing value, we need to fill
1477 * it properly. The header.misc value give us nice hint.
1478 */
1479 bev.pid = HOST_KERNEL_ID;
1480 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1481 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1482 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1483
1484 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1485 __event_process_build_id(&bev, filename, session);
1486
1487 offset += bev.header.size;
1488 }
1489
1490 return 0;
1491}
1492
1493static int perf_header__read_build_ids(struct perf_header *header,
1494 int input, u64 offset, u64 size)
1495{
1496 struct perf_session *session = container_of(header, struct perf_session, header);
1497 struct build_id_event bev;
1498 char filename[PATH_MAX];
1499 u64 limit = offset + size, orig_offset = offset;
1500 int err = -1;
1501
1502 while (offset < limit) {
1503 ssize_t len;
1504
1505 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
1506 goto out;
1507
1508 if (header->needs_swap)
1509 perf_event_header__bswap(&bev.header);
1510
1511 len = bev.header.size - sizeof(bev);
1512 if (read(input, filename, len) != len)
1513 goto out;
1514 /*
1515 * The a1645ce1 changeset:
1516 *
1517 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1518 *
1519 * Added a field to struct build_id_event that broke the file
1520 * format.
1521 *
1522 * Since the kernel build-id is the first entry, process the
1523 * table using the old format if the well known
1524 * '[kernel.kallsyms]' string for the kernel build-id has the
1525 * first 4 characters chopped off (where the pid_t sits).
1526 */
1527 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1528 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1529 return -1;
1530 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1531 }
1532
1533 __event_process_build_id(&bev, filename, session);
1534
1535 offset += bev.header.size;
1536 }
1537 err = 0;
1538out:
1539 return err;
1540}
1541
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001542static int process_tracing_data(struct perf_file_section *section __unused,
Robert Richterf1c67db2012-02-10 15:41:56 +01001543 struct perf_header *ph __unused,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001544 int feat __unused, int fd, void *data)
Robert Richterf1c67db2012-02-10 15:41:56 +01001545{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001546 trace_report(fd, data, false);
Robert Richterf1c67db2012-02-10 15:41:56 +01001547 return 0;
1548}
1549
1550static int process_build_id(struct perf_file_section *section,
1551 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001552 int feat __unused, int fd, void *data __used)
Robert Richterf1c67db2012-02-10 15:41:56 +01001553{
1554 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
1555 pr_debug("Failed to read buildids, continuing...\n");
1556 return 0;
1557}
1558
Robert Richter7c2f7af2012-08-16 21:10:23 +02001559static struct perf_evsel *
1560perf_evlist__find_by_index(struct perf_evlist *evlist, int idx)
1561{
1562 struct perf_evsel *evsel;
1563
1564 list_for_each_entry(evsel, &evlist->entries, node) {
1565 if (evsel->idx == idx)
1566 return evsel;
1567 }
1568
1569 return NULL;
1570}
1571
1572static void
1573perf_evlist__set_event_name(struct perf_evlist *evlist, struct perf_evsel *event)
1574{
1575 struct perf_evsel *evsel;
1576
1577 if (!event->name)
1578 return;
1579
1580 evsel = perf_evlist__find_by_index(evlist, event->idx);
1581 if (!evsel)
1582 return;
1583
1584 if (evsel->name)
1585 return;
1586
1587 evsel->name = strdup(event->name);
1588}
1589
1590static int
1591process_event_desc(struct perf_file_section *section __unused,
1592 struct perf_header *header, int feat __unused, int fd,
1593 void *data __used)
1594{
1595 struct perf_session *session = container_of(header, struct perf_session, header);
1596 struct perf_evsel *evsel, *events = read_event_desc(header, fd);
1597
1598 if (!events)
1599 return 0;
1600
1601 for (evsel = events; evsel->attr.size; evsel++)
1602 perf_evlist__set_event_name(session->evlist, evsel);
1603
1604 free_event_desc(events);
1605
1606 return 0;
1607}
1608
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001609struct feature_ops {
1610 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1611 void (*print)(struct perf_header *h, int fd, FILE *fp);
Robert Richterf1c67db2012-02-10 15:41:56 +01001612 int (*process)(struct perf_file_section *section,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03001613 struct perf_header *h, int feat, int fd, void *data);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001614 const char *name;
1615 bool full_only;
1616};
1617
Robert Richter8cdfa782011-12-07 10:02:56 +01001618#define FEAT_OPA(n, func) \
1619 [n] = { .name = #n, .write = write_##func, .print = print_##func }
Robert Richterf1c67db2012-02-10 15:41:56 +01001620#define FEAT_OPP(n, func) \
1621 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1622 .process = process_##func }
Robert Richter8cdfa782011-12-07 10:02:56 +01001623#define FEAT_OPF(n, func) \
Robert Richterf1c67db2012-02-10 15:41:56 +01001624 [n] = { .name = #n, .write = write_##func, .print = print_##func, \
1625 .full_only = true }
Robert Richter8cdfa782011-12-07 10:02:56 +01001626
1627/* feature_ops not implemented: */
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001628#define print_tracing_data NULL
1629#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001630
1631static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Stephane Eranian2eeaaa02012-05-15 13:28:13 +02001632 FEAT_OPP(HEADER_TRACING_DATA, tracing_data),
Robert Richterf1c67db2012-02-10 15:41:56 +01001633 FEAT_OPP(HEADER_BUILD_ID, build_id),
Robert Richter8cdfa782011-12-07 10:02:56 +01001634 FEAT_OPA(HEADER_HOSTNAME, hostname),
1635 FEAT_OPA(HEADER_OSRELEASE, osrelease),
1636 FEAT_OPA(HEADER_VERSION, version),
1637 FEAT_OPA(HEADER_ARCH, arch),
1638 FEAT_OPA(HEADER_NRCPUS, nrcpus),
1639 FEAT_OPA(HEADER_CPUDESC, cpudesc),
1640 FEAT_OPA(HEADER_CPUID, cpuid),
1641 FEAT_OPA(HEADER_TOTAL_MEM, total_mem),
Robert Richter7c2f7af2012-08-16 21:10:23 +02001642 FEAT_OPP(HEADER_EVENT_DESC, event_desc),
Robert Richter8cdfa782011-12-07 10:02:56 +01001643 FEAT_OPA(HEADER_CMDLINE, cmdline),
1644 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1645 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranian330aa672012-03-08 23:47:46 +01001646 FEAT_OPA(HEADER_BRANCH_STACK, branch_stack),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001647};
1648
1649struct header_print_data {
1650 FILE *fp;
1651 bool full; /* extended list of headers */
1652};
1653
1654static int perf_file_section__fprintf_info(struct perf_file_section *section,
1655 struct perf_header *ph,
1656 int feat, int fd, void *data)
1657{
1658 struct header_print_data *hd = data;
1659
1660 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1661 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1662 "%d, continuing...\n", section->offset, feat);
1663 return 0;
1664 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001665 if (feat >= HEADER_LAST_FEATURE) {
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001666 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01001667 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001668 }
1669 if (!feat_ops[feat].print)
1670 return 0;
1671
1672 if (!feat_ops[feat].full_only || hd->full)
1673 feat_ops[feat].print(ph, fd, hd->fp);
1674 else
1675 fprintf(hd->fp, "# %s info available, use -I to display\n",
1676 feat_ops[feat].name);
1677
1678 return 0;
1679}
1680
1681int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
1682{
1683 struct header_print_data hd;
1684 struct perf_header *header = &session->header;
1685 int fd = session->fd;
1686 hd.fp = fp;
1687 hd.full = full;
1688
1689 perf_header__process_sections(header, fd, &hd,
1690 perf_file_section__fprintf_info);
1691 return 0;
1692}
1693
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001694static int do_write_feat(int fd, struct perf_header *h, int type,
1695 struct perf_file_section **p,
1696 struct perf_evlist *evlist)
1697{
1698 int err;
1699 int ret = 0;
1700
1701 if (perf_header__has_feat(h, type)) {
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001702 if (!feat_ops[type].write)
1703 return -1;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001704
1705 (*p)->offset = lseek(fd, 0, SEEK_CUR);
1706
1707 err = feat_ops[type].write(fd, h, evlist);
1708 if (err < 0) {
1709 pr_debug("failed to write feature %d\n", type);
1710
1711 /* undo anything written */
1712 lseek(fd, (*p)->offset, SEEK_SET);
1713
1714 return -1;
1715 }
1716 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
1717 (*p)++;
1718 }
1719 return ret;
1720}
1721
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001722static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001723 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001724{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001725 int nr_sections;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001726 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001727 int sec_size;
1728 u64 sec_start;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001729 int feat;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001730 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001731
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001732 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001733 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001734 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001735
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001736 feat_sec = p = calloc(sizeof(*feat_sec), nr_sections);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001737 if (feat_sec == NULL)
1738 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001739
1740 sec_size = sizeof(*feat_sec) * nr_sections;
1741
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001742 sec_start = header->data_offset + header->data_size;
Xiao Guangrongf887f302010-02-04 16:46:42 +08001743 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001744
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001745 for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) {
1746 if (do_write_feat(fd, header, feat, &p, evlist))
1747 perf_header__clear_feat(header, feat);
1748 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001749
Xiao Guangrongf887f302010-02-04 16:46:42 +08001750 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001751 /*
1752 * may write more than needed due to dropped feature, but
1753 * this is okay, reader will skip the mising entries
1754 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001755 err = do_write(fd, feat_sec, sec_size);
1756 if (err < 0)
1757 pr_debug("failed to write feature section\n");
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001758 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001759 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001760}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001761
Tom Zanussi8dc58102010-04-01 23:59:15 -05001762int perf_header__write_pipe(int fd)
1763{
1764 struct perf_pipe_file_header f_header;
1765 int err;
1766
1767 f_header = (struct perf_pipe_file_header){
1768 .magic = PERF_MAGIC,
1769 .size = sizeof(f_header),
1770 };
1771
1772 err = do_write(fd, &f_header, sizeof(f_header));
1773 if (err < 0) {
1774 pr_debug("failed to write perf pipe header\n");
1775 return err;
1776 }
1777
1778 return 0;
1779}
1780
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001781int perf_session__write_header(struct perf_session *session,
1782 struct perf_evlist *evlist,
1783 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001784{
1785 struct perf_file_header f_header;
1786 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001787 struct perf_header *header = &session->header;
Robert Richter6606f872012-08-16 21:10:19 +02001788 struct perf_evsel *evsel, *pair = NULL;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001789 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001790
1791 lseek(fd, sizeof(f_header), SEEK_SET);
1792
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001793 if (session->evlist != evlist)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001794 pair = perf_evlist__first(session->evlist);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001795
Robert Richter6606f872012-08-16 21:10:19 +02001796 list_for_each_entry(evsel, &evlist->entries, node) {
1797 evsel->id_offset = lseek(fd, 0, SEEK_CUR);
1798 err = do_write(fd, evsel->id, evsel->ids * sizeof(u64));
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001799 if (err < 0) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001800out_err_write:
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001801 pr_debug("failed to write perf header\n");
1802 return err;
1803 }
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001804 if (session->evlist != evlist) {
1805 err = do_write(fd, pair->id, pair->ids * sizeof(u64));
1806 if (err < 0)
1807 goto out_err_write;
Robert Richter6606f872012-08-16 21:10:19 +02001808 evsel->ids += pair->ids;
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -03001809 pair = perf_evsel__next(pair);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001810 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001811 }
1812
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001813 header->attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001814
Robert Richter6606f872012-08-16 21:10:19 +02001815 list_for_each_entry(evsel, &evlist->entries, node) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001816 f_attr = (struct perf_file_attr){
Robert Richter6606f872012-08-16 21:10:19 +02001817 .attr = evsel->attr,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001818 .ids = {
Robert Richter6606f872012-08-16 21:10:19 +02001819 .offset = evsel->id_offset,
1820 .size = evsel->ids * sizeof(u64),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001821 }
1822 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001823 err = do_write(fd, &f_attr, sizeof(f_attr));
1824 if (err < 0) {
1825 pr_debug("failed to write perf header attribute\n");
1826 return err;
1827 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001828 }
1829
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001830 header->event_offset = lseek(fd, 0, SEEK_CUR);
Robert Richterdb146f02012-08-16 21:10:20 +02001831 header->event_size = trace_event_count * sizeof(struct perf_trace_event_type);
1832 if (trace_events) {
1833 err = do_write(fd, trace_events, header->event_size);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001834 if (err < 0) {
1835 pr_debug("failed to write perf header events\n");
1836 return err;
1837 }
1838 }
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02001839
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001840 header->data_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001841
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001842 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001843 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001844 if (err < 0)
1845 return err;
1846 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001847
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001848 f_header = (struct perf_file_header){
1849 .magic = PERF_MAGIC,
1850 .size = sizeof(f_header),
1851 .attr_size = sizeof(f_attr),
1852 .attrs = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001853 .offset = header->attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001854 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001855 },
1856 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001857 .offset = header->data_offset,
1858 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001859 },
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02001860 .event_types = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001861 .offset = header->event_offset,
1862 .size = header->event_size,
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02001863 },
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001864 };
1865
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001866 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001867
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001868 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001869 err = do_write(fd, &f_header, sizeof(f_header));
1870 if (err < 0) {
1871 pr_debug("failed to write perf header\n");
1872 return err;
1873 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001874 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001875
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001876 header->frozen = 1;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001877 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001878}
1879
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001880static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001881 int fd, void *buf, size_t size)
1882{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02001883 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001884 return -1;
1885
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001886 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001887 mem_bswap_64(buf, size);
1888
1889 return 0;
1890}
1891
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001892int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001893 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001894 int (*process)(struct perf_file_section *section,
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001895 struct perf_header *ph,
1896 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001897{
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001898 struct perf_file_section *feat_sec, *sec;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001899 int nr_sections;
1900 int sec_size;
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001901 int feat;
1902 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001903
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001904 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001905 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001906 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001907
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001908 feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001909 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001910 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001911
1912 sec_size = sizeof(*feat_sec) * nr_sections;
1913
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001914 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001915
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001916 err = perf_header__getbuffer64(header, fd, feat_sec, sec_size);
1917 if (err < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02001918 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001919
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001920 for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) {
1921 err = process(sec++, header, feat, fd, data);
1922 if (err < 0)
1923 goto out_free;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01001924 }
Robert Richterb1e5a9b2011-12-07 10:02:57 +01001925 err = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02001926out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001927 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001928 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02001929}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001930
Stephane Eranian114382a2012-02-09 23:21:08 +01001931static const int attr_file_abi_sizes[] = {
1932 [0] = PERF_ATTR_SIZE_VER0,
1933 [1] = PERF_ATTR_SIZE_VER1,
Jiri Olsa239cc472012-08-07 15:20:42 +02001934 [2] = PERF_ATTR_SIZE_VER2,
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001935 [3] = PERF_ATTR_SIZE_VER3,
1936 [4] = PERF_ATTR_SIZE_VER4,
Stephane Eranian114382a2012-02-09 23:21:08 +01001937 0,
1938};
1939
1940/*
1941 * In the legacy file format, the magic number is not used to encode endianness.
1942 * hdr_sz was used to encode endianness. But given that hdr_sz can vary based
1943 * on ABI revisions, we need to try all combinations for all endianness to
1944 * detect the endianness.
1945 */
1946static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph)
1947{
1948 uint64_t ref_size, attr_size;
1949 int i;
1950
1951 for (i = 0 ; attr_file_abi_sizes[i]; i++) {
1952 ref_size = attr_file_abi_sizes[i]
1953 + sizeof(struct perf_file_section);
1954 if (hdr_sz != ref_size) {
1955 attr_size = bswap_64(hdr_sz);
1956 if (attr_size != ref_size)
1957 continue;
1958
1959 ph->needs_swap = true;
1960 }
1961 pr_debug("ABI%d perf.data file detected, need_swap=%d\n",
1962 i,
1963 ph->needs_swap);
1964 return 0;
1965 }
1966 /* could not determine endianness */
1967 return -1;
1968}
1969
1970#define PERF_PIPE_HDR_VER0 16
1971
1972static const size_t attr_pipe_abi_sizes[] = {
1973 [0] = PERF_PIPE_HDR_VER0,
1974 0,
1975};
1976
1977/*
1978 * In the legacy pipe format, there is an implicit assumption that endiannesss
1979 * between host recording the samples, and host parsing the samples is the
1980 * same. This is not always the case given that the pipe output may always be
1981 * redirected into a file and analyzed on a different machine with possibly a
1982 * different endianness and perf_event ABI revsions in the perf tool itself.
1983 */
1984static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph)
1985{
1986 u64 attr_size;
1987 int i;
1988
1989 for (i = 0 ; attr_pipe_abi_sizes[i]; i++) {
1990 if (hdr_sz != attr_pipe_abi_sizes[i]) {
1991 attr_size = bswap_64(hdr_sz);
1992 if (attr_size != hdr_sz)
1993 continue;
1994
1995 ph->needs_swap = true;
1996 }
1997 pr_debug("Pipe ABI%d perf.data file detected\n", i);
1998 return 0;
1999 }
2000 return -1;
2001}
2002
2003static int check_magic_endian(u64 magic, uint64_t hdr_sz,
2004 bool is_pipe, struct perf_header *ph)
Stephane Eranian73323f52012-02-02 13:54:44 +01002005{
2006 int ret;
2007
2008 /* check for legacy format */
Stephane Eranian114382a2012-02-09 23:21:08 +01002009 ret = memcmp(&magic, __perf_magic1, sizeof(magic));
Stephane Eranian73323f52012-02-02 13:54:44 +01002010 if (ret == 0) {
2011 pr_debug("legacy perf.data format\n");
Stephane Eranian114382a2012-02-09 23:21:08 +01002012 if (is_pipe)
2013 return try_all_pipe_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002014
Stephane Eranian114382a2012-02-09 23:21:08 +01002015 return try_all_file_abis(hdr_sz, ph);
Stephane Eranian73323f52012-02-02 13:54:44 +01002016 }
Stephane Eranian114382a2012-02-09 23:21:08 +01002017 /*
2018 * the new magic number serves two purposes:
2019 * - unique number to identify actual perf.data files
2020 * - encode endianness of file
2021 */
Stephane Eranian73323f52012-02-02 13:54:44 +01002022
Stephane Eranian114382a2012-02-09 23:21:08 +01002023 /* check magic number with one endianness */
2024 if (magic == __perf_magic2)
Stephane Eranian73323f52012-02-02 13:54:44 +01002025 return 0;
2026
Stephane Eranian114382a2012-02-09 23:21:08 +01002027 /* check magic number with opposite endianness */
2028 if (magic != __perf_magic2_sw)
Stephane Eranian73323f52012-02-02 13:54:44 +01002029 return -1;
2030
2031 ph->needs_swap = true;
2032
2033 return 0;
2034}
2035
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002036int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002037 struct perf_header *ph, int fd)
2038{
Stephane Eranian73323f52012-02-02 13:54:44 +01002039 int ret;
2040
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002041 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002042
Stephane Eranian73323f52012-02-02 13:54:44 +01002043 ret = readn(fd, header, sizeof(*header));
2044 if (ret <= 0)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002045 return -1;
2046
Stephane Eranian114382a2012-02-09 23:21:08 +01002047 if (check_magic_endian(header->magic,
2048 header->attr_size, false, ph) < 0) {
2049 pr_debug("magic/endian check failed\n");
Stephane Eranian73323f52012-02-02 13:54:44 +01002050 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002051 }
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002052
Stephane Eranian73323f52012-02-02 13:54:44 +01002053 if (ph->needs_swap) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002054 mem_bswap_64(header, offsetof(struct perf_file_header,
Stephane Eranian73323f52012-02-02 13:54:44 +01002055 adds_features));
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002056 }
2057
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002058 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002059 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002060 if (header->size == offsetof(typeof(*header), adds_features))
2061 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002062 else
2063 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06002064 } else if (ph->needs_swap) {
David Ahernd327fa42011-10-18 17:34:01 -06002065 /*
2066 * feature bitmap is declared as an array of unsigned longs --
2067 * not good since its size can differ between the host that
2068 * generated the data file and the host analyzing the file.
2069 *
2070 * We need to handle endianness, but we don't know the size of
2071 * the unsigned long where the file was generated. Take a best
2072 * guess at determining it: try 64-bit swap first (ie., file
2073 * created on a 64-bit host), and check if the hostname feature
2074 * bit is set (this feature bit is forced on as of fbe96f2).
2075 * If the bit is not, undo the 64-bit swap and try a 32-bit
2076 * swap. If the hostname bit is still not set (e.g., older data
2077 * file), punt and fallback to the original behavior --
2078 * clearing all feature bits and setting buildid.
2079 */
David Ahern80c01202012-06-08 11:47:51 -03002080 mem_bswap_64(&header->adds_features,
2081 BITS_TO_U64(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002082
2083 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
David Ahern80c01202012-06-08 11:47:51 -03002084 /* unswap as u64 */
2085 mem_bswap_64(&header->adds_features,
2086 BITS_TO_U64(HEADER_FEAT_BITS));
2087
2088 /* unswap as u32 */
2089 mem_bswap_32(&header->adds_features,
2090 BITS_TO_U32(HEADER_FEAT_BITS));
David Ahernd327fa42011-10-18 17:34:01 -06002091 }
2092
2093 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
2094 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
2095 set_bit(HEADER_BUILD_ID, header->adds_features);
2096 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002097 }
2098
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002099 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002100 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002101
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002102 ph->event_offset = header->event_types.offset;
2103 ph->event_size = header->event_types.size;
2104 ph->data_offset = header->data.offset;
2105 ph->data_size = header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002106 return 0;
2107}
2108
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002109static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002110 struct perf_header *ph,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002111 int feat, int fd, void *data)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002112{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002113 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02002114 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002115 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002116 return 0;
2117 }
2118
Robert Richterb1e5a9b2011-12-07 10:02:57 +01002119 if (feat >= HEADER_LAST_FEATURE) {
2120 pr_debug("unknown feature %d, continuing...\n", feat);
2121 return 0;
2122 }
2123
Robert Richterf1c67db2012-02-10 15:41:56 +01002124 if (!feat_ops[feat].process)
2125 return 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002126
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002127 return feat_ops[feat].process(section, ph, feat, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02002128}
2129
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002130static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05002131 struct perf_header *ph, int fd,
2132 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002133{
Stephane Eranian73323f52012-02-02 13:54:44 +01002134 int ret;
2135
2136 ret = readn(fd, header, sizeof(*header));
2137 if (ret <= 0)
2138 return -1;
2139
Stephane Eranian114382a2012-02-09 23:21:08 +01002140 if (check_magic_endian(header->magic, header->size, true, ph) < 0) {
2141 pr_debug("endian/magic failed\n");
Tom Zanussi8dc58102010-04-01 23:59:15 -05002142 return -1;
Stephane Eranian114382a2012-02-09 23:21:08 +01002143 }
2144
2145 if (ph->needs_swap)
2146 header->size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05002147
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002148 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05002149 return -1;
2150
Tom Zanussi8dc58102010-04-01 23:59:15 -05002151 return 0;
2152}
2153
2154static int perf_header__read_pipe(struct perf_session *session, int fd)
2155{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002156 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05002157 struct perf_pipe_file_header f_header;
2158
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002159 if (perf_file_header__read_pipe(&f_header, header, fd,
Tom Zanussi454c4072010-05-01 01:41:20 -05002160 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05002161 pr_debug("incompatible file format\n");
2162 return -EINVAL;
2163 }
2164
2165 session->fd = fd;
2166
2167 return 0;
2168}
2169
Stephane Eranian69996df2012-02-09 23:21:06 +01002170static int read_attr(int fd, struct perf_header *ph,
2171 struct perf_file_attr *f_attr)
2172{
2173 struct perf_event_attr *attr = &f_attr->attr;
2174 size_t sz, left;
2175 size_t our_sz = sizeof(f_attr->attr);
2176 int ret;
2177
2178 memset(f_attr, 0, sizeof(*f_attr));
2179
2180 /* read minimal guaranteed structure */
2181 ret = readn(fd, attr, PERF_ATTR_SIZE_VER0);
2182 if (ret <= 0) {
2183 pr_debug("cannot read %d bytes of header attr\n",
2184 PERF_ATTR_SIZE_VER0);
2185 return -1;
2186 }
2187
2188 /* on file perf_event_attr size */
2189 sz = attr->size;
Stephane Eranian114382a2012-02-09 23:21:08 +01002190
Stephane Eranian69996df2012-02-09 23:21:06 +01002191 if (ph->needs_swap)
2192 sz = bswap_32(sz);
2193
2194 if (sz == 0) {
2195 /* assume ABI0 */
2196 sz = PERF_ATTR_SIZE_VER0;
2197 } else if (sz > our_sz) {
2198 pr_debug("file uses a more recent and unsupported ABI"
2199 " (%zu bytes extra)\n", sz - our_sz);
2200 return -1;
2201 }
2202 /* what we have not yet read and that we know about */
2203 left = sz - PERF_ATTR_SIZE_VER0;
2204 if (left) {
2205 void *ptr = attr;
2206 ptr += PERF_ATTR_SIZE_VER0;
2207
2208 ret = readn(fd, ptr, left);
2209 }
2210 /* read perf_file_section, ids are read in caller */
2211 ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids));
2212
2213 return ret <= 0 ? -1 : 0;
2214}
2215
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002216static int perf_evsel__set_tracepoint_name(struct perf_evsel *evsel,
2217 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002218{
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002219 struct event_format *event = pevent_find_event(pevent,
2220 evsel->attr.config);
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002221 char bf[128];
2222
2223 if (event == NULL)
2224 return -1;
2225
2226 snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name);
2227 evsel->name = strdup(bf);
2228 if (event->name == NULL)
2229 return -1;
2230
Arnaldo Carvalho de Melofcf65bf2012-08-07 09:58:03 -03002231 evsel->tp_format = event;
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002232 return 0;
2233}
2234
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002235static int perf_evlist__set_tracepoint_names(struct perf_evlist *evlist,
2236 struct pevent *pevent)
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002237{
2238 struct perf_evsel *pos;
2239
2240 list_for_each_entry(pos, &evlist->entries, node) {
2241 if (pos->attr.type == PERF_TYPE_TRACEPOINT &&
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002242 perf_evsel__set_tracepoint_name(pos, pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002243 return -1;
2244 }
2245
2246 return 0;
2247}
2248
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002249int perf_session__read_header(struct perf_session *session, int fd)
Tom Zanussi8dc58102010-04-01 23:59:15 -05002250{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002251 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002252 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002253 struct perf_file_attr f_attr;
2254 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002255 int nr_attrs, nr_ids, i, j;
2256
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002257 session->evlist = perf_evlist__new(NULL, NULL);
2258 if (session->evlist == NULL)
2259 return -ENOMEM;
2260
Tom Zanussi8dc58102010-04-01 23:59:15 -05002261 if (session->fd_pipe)
2262 return perf_header__read_pipe(session, fd);
2263
Stephane Eranian69996df2012-02-09 23:21:06 +01002264 if (perf_file_header__read(&f_header, header, fd) < 0)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002265 return -EINVAL;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002266
Stephane Eranian69996df2012-02-09 23:21:06 +01002267 nr_attrs = f_header.attrs.size / f_header.attr_size;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002268 lseek(fd, f_header.attrs.offset, SEEK_SET);
2269
2270 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002271 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002272 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002273
Stephane Eranian69996df2012-02-09 23:21:06 +01002274 if (read_attr(fd, header, &f_attr) < 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002275 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002276
David Aherneda39132011-07-15 12:34:09 -06002277 if (header->needs_swap)
2278 perf_event__attr_swap(&f_attr.attr);
2279
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002280 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002281 evsel = perf_evsel__new(&f_attr.attr, i);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002282
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002283 if (evsel == NULL)
2284 goto out_delete_evlist;
2285 /*
2286 * Do it before so that if perf_evsel__alloc_id fails, this
2287 * entry gets purged too at perf_evlist__delete().
2288 */
2289 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002290
2291 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002292 /*
2293 * We don't have the cpu and thread maps on the header, so
2294 * for allocating the perf_sample_id table we fake 1 cpu and
2295 * hattr->ids threads.
2296 */
2297 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2298 goto out_delete_evlist;
2299
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002300 lseek(fd, f_attr.ids.offset, SEEK_SET);
2301
2302 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002303 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002304 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002305
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002306 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002307 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002308
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002309 lseek(fd, tmp, SEEK_SET);
2310 }
2311
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002312 symbol_conf.nr_events = nr_attrs;
2313
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02002314 if (f_header.event_types.size) {
2315 lseek(fd, f_header.event_types.offset, SEEK_SET);
Robert Richterdb146f02012-08-16 21:10:20 +02002316 trace_events = malloc(f_header.event_types.size);
2317 if (trace_events == NULL)
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002318 return -ENOMEM;
Robert Richterdb146f02012-08-16 21:10:20 +02002319 if (perf_header__getbuffer64(header, fd, trace_events,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002320 f_header.event_types.size))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002321 goto out_errno;
Robert Richterdb146f02012-08-16 21:10:20 +02002322 trace_event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02002323 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +02002324
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002325 perf_header__process_sections(header, fd, &session->pevent,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002326 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002327
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002328 lseek(fd, header->data_offset, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002329
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002330 if (perf_evlist__set_tracepoint_names(session->evlist, session->pevent))
Arnaldo Carvalho de Melocb9dd492012-06-11 19:03:32 -03002331 goto out_delete_evlist;
2332
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002333 header->frozen = 1;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002334 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002335out_errno:
2336 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002337
2338out_delete_evlist:
2339 perf_evlist__delete(session->evlist);
2340 session->evlist = NULL;
2341 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002342}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002343
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002344int perf_event__synthesize_attr(struct perf_tool *tool,
Robert Richterf4d83432012-08-16 21:10:17 +02002345 struct perf_event_attr *attr, u32 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002346 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002347{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002348 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002349 size_t size;
2350 int err;
2351
2352 size = sizeof(struct perf_event_attr);
2353 size = ALIGN(size, sizeof(u64));
2354 size += sizeof(struct perf_event_header);
2355 size += ids * sizeof(u64);
2356
2357 ev = malloc(size);
2358
Chris Samuelce47dc52010-11-13 13:35:06 +11002359 if (ev == NULL)
2360 return -ENOMEM;
2361
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002362 ev->attr.attr = *attr;
2363 memcpy(ev->attr.id, id, ids * sizeof(u64));
2364
2365 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
Robert Richterf4d83432012-08-16 21:10:17 +02002366 ev->attr.header.size = (u16)size;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002367
Robert Richterf4d83432012-08-16 21:10:17 +02002368 if (ev->attr.header.size == size)
2369 err = process(tool, ev, NULL, NULL);
2370 else
2371 err = -E2BIG;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002372
2373 free(ev);
2374
2375 return err;
2376}
2377
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002378int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002379 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002380 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002381{
Robert Richter6606f872012-08-16 21:10:19 +02002382 struct perf_evsel *evsel;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002383 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002384
Robert Richter6606f872012-08-16 21:10:19 +02002385 list_for_each_entry(evsel, &session->evlist->entries, node) {
2386 err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids,
2387 evsel->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002388 if (err) {
2389 pr_debug("failed to create perf header attribute\n");
2390 return err;
2391 }
2392 }
2393
2394 return err;
2395}
2396
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002397int perf_event__process_attr(union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002398 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002399{
Robert Richterf4d83432012-08-16 21:10:17 +02002400 u32 i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002401 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002402 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002403
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002404 if (evlist == NULL) {
2405 *pevlist = evlist = perf_evlist__new(NULL, NULL);
2406 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002407 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002408 }
2409
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002410 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002411 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002412 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002413
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002414 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002415
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002416 ids = event->header.size;
2417 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002418 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002419 /*
2420 * We don't have the cpu and thread maps on the header, so
2421 * for allocating the perf_sample_id table we fake 1 cpu and
2422 * hattr->ids threads.
2423 */
2424 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2425 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002426
2427 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002428 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002429 }
2430
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002431 return 0;
2432}
Tom Zanussicd19a032010-04-01 23:59:20 -05002433
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002434int perf_event__synthesize_event_type(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002435 u64 event_id, char *name,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002436 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002437 struct machine *machine)
Tom Zanussicd19a032010-04-01 23:59:20 -05002438{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002439 union perf_event ev;
Tom Zanussicd19a032010-04-01 23:59:20 -05002440 size_t size = 0;
2441 int err = 0;
2442
2443 memset(&ev, 0, sizeof(ev));
2444
2445 ev.event_type.event_type.event_id = event_id;
2446 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
2447 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
2448
2449 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
Stephane Eranianf8f4b282012-01-20 10:49:12 +01002450 size = strlen(ev.event_type.event_type.name);
Tom Zanussicd19a032010-04-01 23:59:20 -05002451 size = ALIGN(size, sizeof(u64));
2452 ev.event_type.header.size = sizeof(ev.event_type) -
2453 (sizeof(ev.event_type.event_type.name) - size);
2454
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002455 err = process(tool, &ev, NULL, machine);
Tom Zanussicd19a032010-04-01 23:59:20 -05002456
2457 return err;
2458}
2459
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002460int perf_event__synthesize_event_types(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002461 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002462 struct machine *machine)
Tom Zanussicd19a032010-04-01 23:59:20 -05002463{
2464 struct perf_trace_event_type *type;
2465 int i, err = 0;
2466
Robert Richterdb146f02012-08-16 21:10:20 +02002467 for (i = 0; i < trace_event_count; i++) {
2468 type = &trace_events[i];
Tom Zanussicd19a032010-04-01 23:59:20 -05002469
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002470 err = perf_event__synthesize_event_type(tool, type->event_id,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002471 type->name, process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002472 machine);
Tom Zanussicd19a032010-04-01 23:59:20 -05002473 if (err) {
2474 pr_debug("failed to create perf header event type\n");
2475 return err;
2476 }
2477 }
2478
2479 return err;
2480}
2481
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002482int perf_event__process_event_type(struct perf_tool *tool __unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002483 union perf_event *event)
Tom Zanussicd19a032010-04-01 23:59:20 -05002484{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002485 if (perf_header__push_event(event->event_type.event_type.event_id,
2486 event->event_type.event_type.name) < 0)
Tom Zanussicd19a032010-04-01 23:59:20 -05002487 return -ENOMEM;
2488
2489 return 0;
2490}
Tom Zanussi92155452010-04-01 23:59:21 -05002491
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002492int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002493 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002494 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05002495{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002496 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02002497 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05002498 ssize_t size = 0, aligned_size = 0, padding;
Kyle McMartinfb7d0b32011-01-24 11:13:04 -05002499 int err __used = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05002500
Jiri Olsa29208e52011-10-20 15:59:43 +02002501 /*
2502 * We are going to store the size of the data followed
2503 * by the data contents. Since the fd descriptor is a pipe,
2504 * we cannot seek back to store the size of the data once
2505 * we know it. Instead we:
2506 *
2507 * - write the tracing data to the temp file
2508 * - get/write the data size to pipe
2509 * - write the tracing data from the temp file
2510 * to the pipe
2511 */
2512 tdata = tracing_data_get(&evlist->entries, fd, true);
2513 if (!tdata)
2514 return -1;
2515
Tom Zanussi92155452010-04-01 23:59:21 -05002516 memset(&ev, 0, sizeof(ev));
2517
2518 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02002519 size = tdata->size;
Tom Zanussi92155452010-04-01 23:59:21 -05002520 aligned_size = ALIGN(size, sizeof(u64));
2521 padding = aligned_size - size;
2522 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2523 ev.tracing_data.size = aligned_size;
2524
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002525 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05002526
Jiri Olsa29208e52011-10-20 15:59:43 +02002527 /*
2528 * The put function will copy all the tracing data
2529 * stored in temp file to the pipe.
2530 */
2531 tracing_data_put(tdata);
2532
Tom Zanussi92155452010-04-01 23:59:21 -05002533 write_padded(fd, NULL, 0, padding);
2534
2535 return aligned_size;
2536}
2537
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002538int perf_event__process_tracing_data(union perf_event *event,
2539 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05002540{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002541 ssize_t size_read, padding, size = event->tracing_data.size;
Tom Zanussi92155452010-04-01 23:59:21 -05002542 off_t offset = lseek(session->fd, 0, SEEK_CUR);
2543 char buf[BUFSIZ];
2544
2545 /* setup for reading amidst mmap */
2546 lseek(session->fd, offset + sizeof(struct tracing_data_event),
2547 SEEK_SET);
2548
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -03002549 size_read = trace_report(session->fd, &session->pevent,
2550 session->repipe);
Tom Zanussi92155452010-04-01 23:59:21 -05002551 padding = ALIGN(size_read, sizeof(u64)) - size_read;
2552
2553 if (read(session->fd, buf, padding) < 0)
2554 die("reading input file");
Tom Zanussi454c4072010-05-01 01:41:20 -05002555 if (session->repipe) {
2556 int retw = write(STDOUT_FILENO, buf, padding);
2557 if (retw <= 0 || retw != padding)
2558 die("repiping tracing data padding");
2559 }
Tom Zanussi92155452010-04-01 23:59:21 -05002560
2561 if (size_read + padding != size)
2562 die("tracing data size mismatch");
2563
Arnaldo Carvalho de Melo8b6ee4c2012-08-07 23:36:16 -03002564 perf_evlist__set_tracepoint_names(session->evlist, session->pevent);
2565
Tom Zanussi92155452010-04-01 23:59:21 -05002566 return size_read + padding;
2567}
Tom Zanussic7929e42010-04-01 23:59:22 -05002568
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002569int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002570 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002571 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002572 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05002573{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002574 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05002575 size_t len;
2576 int err = 0;
2577
2578 if (!pos->hit)
2579 return err;
2580
2581 memset(&ev, 0, sizeof(ev));
2582
2583 len = pos->long_name_len + 1;
2584 len = ALIGN(len, NAME_ALIGN);
2585 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2586 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2587 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002588 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05002589 ev.build_id.header.size = sizeof(ev.build_id) + len;
2590 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2591
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002592 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05002593
2594 return err;
2595}
2596
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002597int perf_event__process_build_id(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002598 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002599 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05002600{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002601 __event_process_build_id(&event->build_id,
2602 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002603 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05002604 return 0;
2605}
Stephane Eraniana1ac1d32010-06-17 11:39:01 +02002606
2607void disable_buildid_cache(void)
2608{
2609 no_buildid_cache = true;
2610}