blob: 609d79b5fb5e37ae1742cff87eb2b4af53a36943 [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>
Stephane Eranianfbe96f22011-09-30 15:40:40 +020011#include <sys/utsname.h>
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020012
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020013#include "evlist.h"
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030014#include "evsel.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020015#include "header.h"
Frederic Weisbecker03456a12009-10-06 23:36:47 +020016#include "../perf.h"
17#include "trace-event.h"
Arnaldo Carvalho de Melo301a0b02009-12-13 19:50:25 -020018#include "session.h"
Frederic Weisbecker8671dab2009-11-11 04:51:03 +010019#include "symbol.h"
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +010020#include "debug.h"
Stephane Eranianfbe96f22011-09-30 15:40:40 +020021#include "cpumap.h"
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020022
Stephane Eraniana1ac1d32010-06-17 11:39:01 +020023static bool no_buildid_cache = false;
24
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020025static int event_count;
26static struct perf_trace_event_type *events;
27
Stephane Eranianfbe96f22011-09-30 15:40:40 +020028static u32 header_argc;
29static const char **header_argv;
30
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -020031int perf_header__push_event(u64 id, const char *name)
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020032{
Arjan van de Venec60a3f2009-09-19 13:36:30 +020033 if (strlen(name) > MAX_EVENT_NAME)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020034 pr_warning("Event %s will be truncated\n", name);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020035
36 if (!events) {
37 events = malloc(sizeof(struct perf_trace_event_type));
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -020038 if (events == NULL)
39 return -ENOMEM;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020040 } else {
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -020041 struct perf_trace_event_type *nevents;
42
43 nevents = realloc(events, (event_count + 1) * sizeof(*events));
44 if (nevents == NULL)
45 return -ENOMEM;
46 events = nevents;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020047 }
48 memset(&events[event_count], 0, sizeof(struct perf_trace_event_type));
49 events[event_count].event_id = id;
Arjan van de Venec60a3f2009-09-19 13:36:30 +020050 strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1);
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020051 event_count++;
Arnaldo Carvalho de Meloae99fb22009-12-28 22:48:33 -020052 return 0;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +020053}
54
55char *perf_header__find_event(u64 id)
56{
57 int i;
58 for (i = 0 ; i < event_count; i++) {
59 if (events[i].event_id == id)
60 return events[i].name;
61 }
62 return NULL;
63}
64
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020065static const char *__perf_magic = "PERFFILE";
66
67#define PERF_MAGIC (*(u64 *)__perf_magic)
68
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020069struct perf_file_attr {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020070 struct perf_event_attr attr;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020071 struct perf_file_section ids;
72};
73
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030074void perf_header__set_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020075{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030076 set_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020077}
78
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030079void perf_header__clear_feat(struct perf_header *header, int feat)
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020080{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030081 clear_bit(feat, header->adds_features);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -020082}
83
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030084bool perf_header__has_feat(const struct perf_header *header, int feat)
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020085{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -030086 return test_bit(feat, header->adds_features);
Arnaldo Carvalho de Melo8d063672009-11-04 18:50:43 -020087}
88
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -020089static int do_write(int fd, const void *buf, size_t size)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020090{
91 while (size) {
92 int ret = write(fd, buf, size);
93
94 if (ret < 0)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -020095 return -errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +020096
97 size -= ret;
98 buf += ret;
99 }
Arnaldo Carvalho de Melo3726cc72009-11-17 01:18:12 -0200100
101 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +0200102}
103
Arnaldo Carvalho de Melof92cb242010-01-04 16:19:28 -0200104#define NAME_ALIGN 64
105
106static int write_padded(int fd, const void *bf, size_t count,
107 size_t count_aligned)
108{
109 static const char zero_buf[NAME_ALIGN];
110 int err = do_write(fd, bf, count);
111
112 if (!err)
113 err = do_write(fd, zero_buf, count_aligned - count);
114
115 return err;
116}
117
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200118static int do_write_string(int fd, const char *str)
119{
120 u32 len, olen;
121 int ret;
122
123 olen = strlen(str) + 1;
124 len = ALIGN(olen, NAME_ALIGN);
125
126 /* write len, incl. \0 */
127 ret = do_write(fd, &len, sizeof(len));
128 if (ret < 0)
129 return ret;
130
131 return write_padded(fd, str, olen, len);
132}
133
134static char *do_read_string(int fd, struct perf_header *ph)
135{
136 ssize_t sz, ret;
137 u32 len;
138 char *buf;
139
140 sz = read(fd, &len, sizeof(len));
141 if (sz < (ssize_t)sizeof(len))
142 return NULL;
143
144 if (ph->needs_swap)
145 len = bswap_32(len);
146
147 buf = malloc(len);
148 if (!buf)
149 return NULL;
150
151 ret = read(fd, buf, len);
152 if (ret == (ssize_t)len) {
153 /*
154 * strings are padded by zeroes
155 * thus the actual strlen of buf
156 * may be less than len
157 */
158 return buf;
159 }
160
161 free(buf);
162 return NULL;
163}
164
165int
166perf_header__set_cmdline(int argc, const char **argv)
167{
168 int i;
169
170 header_argc = (u32)argc;
171
172 /* do not include NULL termination */
173 header_argv = calloc(argc, sizeof(char *));
174 if (!header_argv)
175 return -ENOMEM;
176
177 /*
178 * must copy argv contents because it gets moved
179 * around during option parsing
180 */
181 for (i = 0; i < argc ; i++)
182 header_argv[i] = argv[i];
183
184 return 0;
185}
186
Robert Richter1b549502011-12-07 10:02:53 +0100187#define dsos__for_each_with_build_id(pos, head) \
188 list_for_each_entry(pos, head, node) \
189 if (!pos->has_build_id) \
190 continue; \
191 else
192
193static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
194 u16 misc, int fd)
195{
196 struct dso *pos;
197
198 dsos__for_each_with_build_id(pos, head) {
199 int err;
200 struct build_id_event b;
201 size_t len;
202
203 if (!pos->hit)
204 continue;
205 len = pos->long_name_len + 1;
206 len = ALIGN(len, NAME_ALIGN);
207 memset(&b, 0, sizeof(b));
208 memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id));
209 b.pid = pid;
210 b.header.misc = misc;
211 b.header.size = sizeof(b) + len;
212 err = do_write(fd, &b, sizeof(b));
213 if (err < 0)
214 return err;
215 err = write_padded(fd, pos->long_name,
216 pos->long_name_len + 1, len);
217 if (err < 0)
218 return err;
219 }
220
221 return 0;
222}
223
224static int machine__write_buildid_table(struct machine *machine, int fd)
225{
226 int err;
227 u16 kmisc = PERF_RECORD_MISC_KERNEL,
228 umisc = PERF_RECORD_MISC_USER;
229
230 if (!machine__is_host(machine)) {
231 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
232 umisc = PERF_RECORD_MISC_GUEST_USER;
233 }
234
235 err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid,
236 kmisc, fd);
237 if (err == 0)
238 err = __dsos__write_buildid_table(&machine->user_dsos,
239 machine->pid, umisc, fd);
240 return err;
241}
242
243static int dsos__write_buildid_table(struct perf_header *header, int fd)
244{
245 struct perf_session *session = container_of(header,
246 struct perf_session, header);
247 struct rb_node *nd;
248 int err = machine__write_buildid_table(&session->host_machine, fd);
249
250 if (err)
251 return err;
252
253 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
254 struct machine *pos = rb_entry(nd, struct machine, rb_node);
255 err = machine__write_buildid_table(pos, fd);
256 if (err)
257 break;
258 }
259 return err;
260}
261
262int build_id_cache__add_s(const char *sbuild_id, const char *debugdir,
263 const char *name, bool is_kallsyms)
264{
265 const size_t size = PATH_MAX;
266 char *realname, *filename = zalloc(size),
267 *linkname = zalloc(size), *targetname;
268 int len, err = -1;
269
270 if (is_kallsyms) {
271 if (symbol_conf.kptr_restrict) {
272 pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n");
273 return 0;
274 }
275 realname = (char *)name;
276 } else
277 realname = realpath(name, NULL);
278
279 if (realname == NULL || filename == NULL || linkname == NULL)
280 goto out_free;
281
282 len = snprintf(filename, size, "%s%s%s",
283 debugdir, is_kallsyms ? "/" : "", realname);
284 if (mkdir_p(filename, 0755))
285 goto out_free;
286
287 snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id);
288
289 if (access(filename, F_OK)) {
290 if (is_kallsyms) {
291 if (copyfile("/proc/kallsyms", filename))
292 goto out_free;
293 } else if (link(realname, filename) && copyfile(name, filename))
294 goto out_free;
295 }
296
297 len = snprintf(linkname, size, "%s/.build-id/%.2s",
298 debugdir, sbuild_id);
299
300 if (access(linkname, X_OK) && mkdir_p(linkname, 0755))
301 goto out_free;
302
303 snprintf(linkname + len, size - len, "/%s", sbuild_id + 2);
304 targetname = filename + strlen(debugdir) - 5;
305 memcpy(targetname, "../..", 5);
306
307 if (symlink(targetname, linkname) == 0)
308 err = 0;
309out_free:
310 if (!is_kallsyms)
311 free(realname);
312 free(filename);
313 free(linkname);
314 return err;
315}
316
317static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size,
318 const char *name, const char *debugdir,
319 bool is_kallsyms)
320{
321 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
322
323 build_id__sprintf(build_id, build_id_size, sbuild_id);
324
325 return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms);
326}
327
328int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir)
329{
330 const size_t size = PATH_MAX;
331 char *filename = zalloc(size),
332 *linkname = zalloc(size);
333 int err = -1;
334
335 if (filename == NULL || linkname == NULL)
336 goto out_free;
337
338 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
339 debugdir, sbuild_id, sbuild_id + 2);
340
341 if (access(linkname, F_OK))
342 goto out_free;
343
344 if (readlink(linkname, filename, size - 1) < 0)
345 goto out_free;
346
347 if (unlink(linkname))
348 goto out_free;
349
350 /*
351 * Since the link is relative, we must make it absolute:
352 */
353 snprintf(linkname, size, "%s/.build-id/%.2s/%s",
354 debugdir, sbuild_id, filename);
355
356 if (unlink(linkname))
357 goto out_free;
358
359 err = 0;
360out_free:
361 free(filename);
362 free(linkname);
363 return err;
364}
365
366static int dso__cache_build_id(struct dso *dso, const char *debugdir)
367{
368 bool is_kallsyms = dso->kernel && dso->long_name[0] != '/';
369
370 return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id),
371 dso->long_name, debugdir, is_kallsyms);
372}
373
374static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
375{
376 struct dso *pos;
377 int err = 0;
378
379 dsos__for_each_with_build_id(pos, head)
380 if (dso__cache_build_id(pos, debugdir))
381 err = -1;
382
383 return err;
384}
385
386static int machine__cache_build_ids(struct machine *machine, const char *debugdir)
387{
388 int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir);
389 ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir);
390 return ret;
391}
392
393static int perf_session__cache_build_ids(struct perf_session *session)
394{
395 struct rb_node *nd;
396 int ret;
397 char debugdir[PATH_MAX];
398
399 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
400
401 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
402 return -1;
403
404 ret = machine__cache_build_ids(&session->host_machine, debugdir);
405
406 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
407 struct machine *pos = rb_entry(nd, struct machine, rb_node);
408 ret |= machine__cache_build_ids(pos, debugdir);
409 }
410 return ret ? -1 : 0;
411}
412
413static bool machine__read_build_ids(struct machine *machine, bool with_hits)
414{
415 bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits);
416 ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits);
417 return ret;
418}
419
420static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits)
421{
422 struct rb_node *nd;
423 bool ret = machine__read_build_ids(&session->host_machine, with_hits);
424
425 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
426 struct machine *pos = rb_entry(nd, struct machine, rb_node);
427 ret |= machine__read_build_ids(pos, with_hits);
428 }
429
430 return ret;
431}
432
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200433static int write_trace_info(int fd, struct perf_header *h __used,
434 struct perf_evlist *evlist)
435{
436 return read_tracing_data(fd, &evlist->entries);
437}
438
439
440static int write_build_id(int fd, struct perf_header *h,
441 struct perf_evlist *evlist __used)
442{
443 struct perf_session *session;
444 int err;
445
446 session = container_of(h, struct perf_session, header);
447
448 err = dsos__write_buildid_table(h, fd);
449 if (err < 0) {
450 pr_debug("failed to write buildid table\n");
451 return err;
452 }
453 if (!no_buildid_cache)
454 perf_session__cache_build_ids(session);
455
456 return 0;
457}
458
459static int write_hostname(int fd, struct perf_header *h __used,
460 struct perf_evlist *evlist __used)
461{
462 struct utsname uts;
463 int ret;
464
465 ret = uname(&uts);
466 if (ret < 0)
467 return -1;
468
469 return do_write_string(fd, uts.nodename);
470}
471
472static int write_osrelease(int fd, struct perf_header *h __used,
473 struct perf_evlist *evlist __used)
474{
475 struct utsname uts;
476 int ret;
477
478 ret = uname(&uts);
479 if (ret < 0)
480 return -1;
481
482 return do_write_string(fd, uts.release);
483}
484
485static int write_arch(int fd, struct perf_header *h __used,
486 struct perf_evlist *evlist __used)
487{
488 struct utsname uts;
489 int ret;
490
491 ret = uname(&uts);
492 if (ret < 0)
493 return -1;
494
495 return do_write_string(fd, uts.machine);
496}
497
498static int write_version(int fd, struct perf_header *h __used,
499 struct perf_evlist *evlist __used)
500{
501 return do_write_string(fd, perf_version_string);
502}
503
504static int write_cpudesc(int fd, struct perf_header *h __used,
505 struct perf_evlist *evlist __used)
506{
507#ifndef CPUINFO_PROC
508#define CPUINFO_PROC NULL
509#endif
510 FILE *file;
511 char *buf = NULL;
512 char *s, *p;
513 const char *search = CPUINFO_PROC;
514 size_t len = 0;
515 int ret = -1;
516
517 if (!search)
518 return -1;
519
520 file = fopen("/proc/cpuinfo", "r");
521 if (!file)
522 return -1;
523
524 while (getline(&buf, &len, file) > 0) {
525 ret = strncmp(buf, search, strlen(search));
526 if (!ret)
527 break;
528 }
529
530 if (ret)
531 goto done;
532
533 s = buf;
534
535 p = strchr(buf, ':');
536 if (p && *(p+1) == ' ' && *(p+2))
537 s = p + 2;
538 p = strchr(s, '\n');
539 if (p)
540 *p = '\0';
541
542 /* squash extra space characters (branding string) */
543 p = s;
544 while (*p) {
545 if (isspace(*p)) {
546 char *r = p + 1;
547 char *q = r;
548 *p = ' ';
549 while (*q && isspace(*q))
550 q++;
551 if (q != (p+1))
552 while ((*r++ = *q++));
553 }
554 p++;
555 }
556 ret = do_write_string(fd, s);
557done:
558 free(buf);
559 fclose(file);
560 return ret;
561}
562
563static int write_nrcpus(int fd, struct perf_header *h __used,
564 struct perf_evlist *evlist __used)
565{
566 long nr;
567 u32 nrc, nra;
568 int ret;
569
570 nr = sysconf(_SC_NPROCESSORS_CONF);
571 if (nr < 0)
572 return -1;
573
574 nrc = (u32)(nr & UINT_MAX);
575
576 nr = sysconf(_SC_NPROCESSORS_ONLN);
577 if (nr < 0)
578 return -1;
579
580 nra = (u32)(nr & UINT_MAX);
581
582 ret = do_write(fd, &nrc, sizeof(nrc));
583 if (ret < 0)
584 return ret;
585
586 return do_write(fd, &nra, sizeof(nra));
587}
588
589static int write_event_desc(int fd, struct perf_header *h __used,
590 struct perf_evlist *evlist)
591{
592 struct perf_evsel *attr;
593 u32 nre = 0, nri, sz;
594 int ret;
595
596 list_for_each_entry(attr, &evlist->entries, node)
597 nre++;
598
599 /*
600 * write number of events
601 */
602 ret = do_write(fd, &nre, sizeof(nre));
603 if (ret < 0)
604 return ret;
605
606 /*
607 * size of perf_event_attr struct
608 */
609 sz = (u32)sizeof(attr->attr);
610 ret = do_write(fd, &sz, sizeof(sz));
611 if (ret < 0)
612 return ret;
613
614 list_for_each_entry(attr, &evlist->entries, node) {
615
616 ret = do_write(fd, &attr->attr, sz);
617 if (ret < 0)
618 return ret;
619 /*
620 * write number of unique id per event
621 * there is one id per instance of an event
622 *
623 * copy into an nri to be independent of the
624 * type of ids,
625 */
626 nri = attr->ids;
627 ret = do_write(fd, &nri, sizeof(nri));
628 if (ret < 0)
629 return ret;
630
631 /*
632 * write event string as passed on cmdline
633 */
Andrew Vagin6340cfe2011-11-28 12:03:29 +0300634 ret = do_write_string(fd, event_name(attr));
Stephane Eranianfbe96f22011-09-30 15:40:40 +0200635 if (ret < 0)
636 return ret;
637 /*
638 * write unique ids for this event
639 */
640 ret = do_write(fd, attr->id, attr->ids * sizeof(u64));
641 if (ret < 0)
642 return ret;
643 }
644 return 0;
645}
646
647static int write_cmdline(int fd, struct perf_header *h __used,
648 struct perf_evlist *evlist __used)
649{
650 char buf[MAXPATHLEN];
651 char proc[32];
652 u32 i, n;
653 int ret;
654
655 /*
656 * actual atual path to perf binary
657 */
658 sprintf(proc, "/proc/%d/exe", getpid());
659 ret = readlink(proc, buf, sizeof(buf));
660 if (ret <= 0)
661 return -1;
662
663 /* readlink() does not add null termination */
664 buf[ret] = '\0';
665
666 /* account for binary path */
667 n = header_argc + 1;
668
669 ret = do_write(fd, &n, sizeof(n));
670 if (ret < 0)
671 return ret;
672
673 ret = do_write_string(fd, buf);
674 if (ret < 0)
675 return ret;
676
677 for (i = 0 ; i < header_argc; i++) {
678 ret = do_write_string(fd, header_argv[i]);
679 if (ret < 0)
680 return ret;
681 }
682 return 0;
683}
684
685#define CORE_SIB_FMT \
686 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list"
687#define THRD_SIB_FMT \
688 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list"
689
690struct cpu_topo {
691 u32 core_sib;
692 u32 thread_sib;
693 char **core_siblings;
694 char **thread_siblings;
695};
696
697static int build_cpu_topo(struct cpu_topo *tp, int cpu)
698{
699 FILE *fp;
700 char filename[MAXPATHLEN];
701 char *buf = NULL, *p;
702 size_t len = 0;
703 u32 i = 0;
704 int ret = -1;
705
706 sprintf(filename, CORE_SIB_FMT, cpu);
707 fp = fopen(filename, "r");
708 if (!fp)
709 return -1;
710
711 if (getline(&buf, &len, fp) <= 0)
712 goto done;
713
714 fclose(fp);
715
716 p = strchr(buf, '\n');
717 if (p)
718 *p = '\0';
719
720 for (i = 0; i < tp->core_sib; i++) {
721 if (!strcmp(buf, tp->core_siblings[i]))
722 break;
723 }
724 if (i == tp->core_sib) {
725 tp->core_siblings[i] = buf;
726 tp->core_sib++;
727 buf = NULL;
728 len = 0;
729 }
730
731 sprintf(filename, THRD_SIB_FMT, cpu);
732 fp = fopen(filename, "r");
733 if (!fp)
734 goto done;
735
736 if (getline(&buf, &len, fp) <= 0)
737 goto done;
738
739 p = strchr(buf, '\n');
740 if (p)
741 *p = '\0';
742
743 for (i = 0; i < tp->thread_sib; i++) {
744 if (!strcmp(buf, tp->thread_siblings[i]))
745 break;
746 }
747 if (i == tp->thread_sib) {
748 tp->thread_siblings[i] = buf;
749 tp->thread_sib++;
750 buf = NULL;
751 }
752 ret = 0;
753done:
754 if(fp)
755 fclose(fp);
756 free(buf);
757 return ret;
758}
759
760static void free_cpu_topo(struct cpu_topo *tp)
761{
762 u32 i;
763
764 if (!tp)
765 return;
766
767 for (i = 0 ; i < tp->core_sib; i++)
768 free(tp->core_siblings[i]);
769
770 for (i = 0 ; i < tp->thread_sib; i++)
771 free(tp->thread_siblings[i]);
772
773 free(tp);
774}
775
776static struct cpu_topo *build_cpu_topology(void)
777{
778 struct cpu_topo *tp;
779 void *addr;
780 u32 nr, i;
781 size_t sz;
782 long ncpus;
783 int ret = -1;
784
785 ncpus = sysconf(_SC_NPROCESSORS_CONF);
786 if (ncpus < 0)
787 return NULL;
788
789 nr = (u32)(ncpus & UINT_MAX);
790
791 sz = nr * sizeof(char *);
792
793 addr = calloc(1, sizeof(*tp) + 2 * sz);
794 if (!addr)
795 return NULL;
796
797 tp = addr;
798
799 addr += sizeof(*tp);
800 tp->core_siblings = addr;
801 addr += sz;
802 tp->thread_siblings = addr;
803
804 for (i = 0; i < nr; i++) {
805 ret = build_cpu_topo(tp, i);
806 if (ret < 0)
807 break;
808 }
809 if (ret) {
810 free_cpu_topo(tp);
811 tp = NULL;
812 }
813 return tp;
814}
815
816static int write_cpu_topology(int fd, struct perf_header *h __used,
817 struct perf_evlist *evlist __used)
818{
819 struct cpu_topo *tp;
820 u32 i;
821 int ret;
822
823 tp = build_cpu_topology();
824 if (!tp)
825 return -1;
826
827 ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib));
828 if (ret < 0)
829 goto done;
830
831 for (i = 0; i < tp->core_sib; i++) {
832 ret = do_write_string(fd, tp->core_siblings[i]);
833 if (ret < 0)
834 goto done;
835 }
836 ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib));
837 if (ret < 0)
838 goto done;
839
840 for (i = 0; i < tp->thread_sib; i++) {
841 ret = do_write_string(fd, tp->thread_siblings[i]);
842 if (ret < 0)
843 break;
844 }
845done:
846 free_cpu_topo(tp);
847 return ret;
848}
849
850
851
852static int write_total_mem(int fd, struct perf_header *h __used,
853 struct perf_evlist *evlist __used)
854{
855 char *buf = NULL;
856 FILE *fp;
857 size_t len = 0;
858 int ret = -1, n;
859 uint64_t mem;
860
861 fp = fopen("/proc/meminfo", "r");
862 if (!fp)
863 return -1;
864
865 while (getline(&buf, &len, fp) > 0) {
866 ret = strncmp(buf, "MemTotal:", 9);
867 if (!ret)
868 break;
869 }
870 if (!ret) {
871 n = sscanf(buf, "%*s %"PRIu64, &mem);
872 if (n == 1)
873 ret = do_write(fd, &mem, sizeof(mem));
874 }
875 free(buf);
876 fclose(fp);
877 return ret;
878}
879
880static int write_topo_node(int fd, int node)
881{
882 char str[MAXPATHLEN];
883 char field[32];
884 char *buf = NULL, *p;
885 size_t len = 0;
886 FILE *fp;
887 u64 mem_total, mem_free, mem;
888 int ret = -1;
889
890 sprintf(str, "/sys/devices/system/node/node%d/meminfo", node);
891 fp = fopen(str, "r");
892 if (!fp)
893 return -1;
894
895 while (getline(&buf, &len, fp) > 0) {
896 /* skip over invalid lines */
897 if (!strchr(buf, ':'))
898 continue;
899 if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2)
900 goto done;
901 if (!strcmp(field, "MemTotal:"))
902 mem_total = mem;
903 if (!strcmp(field, "MemFree:"))
904 mem_free = mem;
905 }
906
907 fclose(fp);
908
909 ret = do_write(fd, &mem_total, sizeof(u64));
910 if (ret)
911 goto done;
912
913 ret = do_write(fd, &mem_free, sizeof(u64));
914 if (ret)
915 goto done;
916
917 ret = -1;
918 sprintf(str, "/sys/devices/system/node/node%d/cpulist", node);
919
920 fp = fopen(str, "r");
921 if (!fp)
922 goto done;
923
924 if (getline(&buf, &len, fp) <= 0)
925 goto done;
926
927 p = strchr(buf, '\n');
928 if (p)
929 *p = '\0';
930
931 ret = do_write_string(fd, buf);
932done:
933 free(buf);
934 fclose(fp);
935 return ret;
936}
937
938static int write_numa_topology(int fd, struct perf_header *h __used,
939 struct perf_evlist *evlist __used)
940{
941 char *buf = NULL;
942 size_t len = 0;
943 FILE *fp;
944 struct cpu_map *node_map = NULL;
945 char *c;
946 u32 nr, i, j;
947 int ret = -1;
948
949 fp = fopen("/sys/devices/system/node/online", "r");
950 if (!fp)
951 return -1;
952
953 if (getline(&buf, &len, fp) <= 0)
954 goto done;
955
956 c = strchr(buf, '\n');
957 if (c)
958 *c = '\0';
959
960 node_map = cpu_map__new(buf);
961 if (!node_map)
962 goto done;
963
964 nr = (u32)node_map->nr;
965
966 ret = do_write(fd, &nr, sizeof(nr));
967 if (ret < 0)
968 goto done;
969
970 for (i = 0; i < nr; i++) {
971 j = (u32)node_map->map[i];
972 ret = do_write(fd, &j, sizeof(j));
973 if (ret < 0)
974 break;
975
976 ret = write_topo_node(fd, i);
977 if (ret < 0)
978 break;
979 }
980done:
981 free(buf);
982 fclose(fp);
983 free(node_map);
984 return ret;
985}
986
987/*
988 * default get_cpuid(): nothing gets recorded
989 * actual implementation must be in arch/$(ARCH)/util/header.c
990 */
991int __attribute__((weak)) get_cpuid(char *buffer __used, size_t sz __used)
992{
993 return -1;
994}
995
996static int write_cpuid(int fd, struct perf_header *h __used,
997 struct perf_evlist *evlist __used)
998{
999 char buffer[64];
1000 int ret;
1001
1002 ret = get_cpuid(buffer, sizeof(buffer));
1003 if (!ret)
1004 goto write_it;
1005
1006 return -1;
1007write_it:
1008 return do_write_string(fd, buffer);
1009}
1010
1011static void print_hostname(struct perf_header *ph, int fd, FILE *fp)
1012{
1013 char *str = do_read_string(fd, ph);
1014 fprintf(fp, "# hostname : %s\n", str);
1015 free(str);
1016}
1017
1018static void print_osrelease(struct perf_header *ph, int fd, FILE *fp)
1019{
1020 char *str = do_read_string(fd, ph);
1021 fprintf(fp, "# os release : %s\n", str);
1022 free(str);
1023}
1024
1025static void print_arch(struct perf_header *ph, int fd, FILE *fp)
1026{
1027 char *str = do_read_string(fd, ph);
1028 fprintf(fp, "# arch : %s\n", str);
1029 free(str);
1030}
1031
1032static void print_cpudesc(struct perf_header *ph, int fd, FILE *fp)
1033{
1034 char *str = do_read_string(fd, ph);
1035 fprintf(fp, "# cpudesc : %s\n", str);
1036 free(str);
1037}
1038
1039static void print_nrcpus(struct perf_header *ph, int fd, FILE *fp)
1040{
1041 ssize_t ret;
1042 u32 nr;
1043
1044 ret = read(fd, &nr, sizeof(nr));
1045 if (ret != (ssize_t)sizeof(nr))
1046 nr = -1; /* interpreted as error */
1047
1048 if (ph->needs_swap)
1049 nr = bswap_32(nr);
1050
1051 fprintf(fp, "# nrcpus online : %u\n", nr);
1052
1053 ret = read(fd, &nr, sizeof(nr));
1054 if (ret != (ssize_t)sizeof(nr))
1055 nr = -1; /* interpreted as error */
1056
1057 if (ph->needs_swap)
1058 nr = bswap_32(nr);
1059
1060 fprintf(fp, "# nrcpus avail : %u\n", nr);
1061}
1062
1063static void print_version(struct perf_header *ph, int fd, FILE *fp)
1064{
1065 char *str = do_read_string(fd, ph);
1066 fprintf(fp, "# perf version : %s\n", str);
1067 free(str);
1068}
1069
1070static void print_cmdline(struct perf_header *ph, int fd, FILE *fp)
1071{
1072 ssize_t ret;
1073 char *str;
1074 u32 nr, i;
1075
1076 ret = read(fd, &nr, sizeof(nr));
1077 if (ret != (ssize_t)sizeof(nr))
1078 return;
1079
1080 if (ph->needs_swap)
1081 nr = bswap_32(nr);
1082
1083 fprintf(fp, "# cmdline : ");
1084
1085 for (i = 0; i < nr; i++) {
1086 str = do_read_string(fd, ph);
1087 fprintf(fp, "%s ", str);
1088 free(str);
1089 }
1090 fputc('\n', fp);
1091}
1092
1093static void print_cpu_topology(struct perf_header *ph, int fd, FILE *fp)
1094{
1095 ssize_t ret;
1096 u32 nr, i;
1097 char *str;
1098
1099 ret = read(fd, &nr, sizeof(nr));
1100 if (ret != (ssize_t)sizeof(nr))
1101 return;
1102
1103 if (ph->needs_swap)
1104 nr = bswap_32(nr);
1105
1106 for (i = 0; i < nr; i++) {
1107 str = do_read_string(fd, ph);
1108 fprintf(fp, "# sibling cores : %s\n", str);
1109 free(str);
1110 }
1111
1112 ret = read(fd, &nr, sizeof(nr));
1113 if (ret != (ssize_t)sizeof(nr))
1114 return;
1115
1116 if (ph->needs_swap)
1117 nr = bswap_32(nr);
1118
1119 for (i = 0; i < nr; i++) {
1120 str = do_read_string(fd, ph);
1121 fprintf(fp, "# sibling threads : %s\n", str);
1122 free(str);
1123 }
1124}
1125
1126static void print_event_desc(struct perf_header *ph, int fd, FILE *fp)
1127{
1128 struct perf_event_attr attr;
1129 uint64_t id;
1130 void *buf = NULL;
1131 char *str;
1132 u32 nre, sz, nr, i, j, msz;
1133 int ret;
1134
1135 /* number of events */
1136 ret = read(fd, &nre, sizeof(nre));
1137 if (ret != (ssize_t)sizeof(nre))
1138 goto error;
1139
1140 if (ph->needs_swap)
1141 nre = bswap_32(nre);
1142
1143 ret = read(fd, &sz, sizeof(sz));
1144 if (ret != (ssize_t)sizeof(sz))
1145 goto error;
1146
1147 if (ph->needs_swap)
1148 sz = bswap_32(sz);
1149
1150 /*
1151 * ensure it is at least to our ABI rev
1152 */
1153 if (sz < (u32)sizeof(attr))
1154 goto error;
1155
1156 memset(&attr, 0, sizeof(attr));
1157
1158 /* read entire region to sync up to next field */
1159 buf = malloc(sz);
1160 if (!buf)
1161 goto error;
1162
1163 msz = sizeof(attr);
1164 if (sz < msz)
1165 msz = sz;
1166
1167 for (i = 0 ; i < nre; i++) {
1168
1169 ret = read(fd, buf, sz);
1170 if (ret != (ssize_t)sz)
1171 goto error;
1172
1173 if (ph->needs_swap)
1174 perf_event__attr_swap(buf);
1175
1176 memcpy(&attr, buf, msz);
1177
1178 ret = read(fd, &nr, sizeof(nr));
1179 if (ret != (ssize_t)sizeof(nr))
1180 goto error;
1181
1182 if (ph->needs_swap)
1183 nr = bswap_32(nr);
1184
1185 str = do_read_string(fd, ph);
1186 fprintf(fp, "# event : name = %s, ", str);
1187 free(str);
1188
1189 fprintf(fp, "type = %d, config = 0x%"PRIx64
1190 ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64,
1191 attr.type,
1192 (u64)attr.config,
1193 (u64)attr.config1,
1194 (u64)attr.config2);
1195
1196 fprintf(fp, ", excl_usr = %d, excl_kern = %d",
1197 attr.exclude_user,
1198 attr.exclude_kernel);
1199
1200 if (nr)
1201 fprintf(fp, ", id = {");
1202
1203 for (j = 0 ; j < nr; j++) {
1204 ret = read(fd, &id, sizeof(id));
1205 if (ret != (ssize_t)sizeof(id))
1206 goto error;
1207
1208 if (ph->needs_swap)
1209 id = bswap_64(id);
1210
1211 if (j)
1212 fputc(',', fp);
1213
1214 fprintf(fp, " %"PRIu64, id);
1215 }
1216 if (nr && j == nr)
1217 fprintf(fp, " }");
1218 fputc('\n', fp);
1219 }
1220 free(buf);
1221 return;
1222error:
1223 fprintf(fp, "# event desc: not available or unable to read\n");
1224}
1225
1226static void print_total_mem(struct perf_header *h __used, int fd, FILE *fp)
1227{
1228 uint64_t mem;
1229 ssize_t ret;
1230
1231 ret = read(fd, &mem, sizeof(mem));
1232 if (ret != sizeof(mem))
1233 goto error;
1234
1235 if (h->needs_swap)
1236 mem = bswap_64(mem);
1237
1238 fprintf(fp, "# total memory : %"PRIu64" kB\n", mem);
1239 return;
1240error:
1241 fprintf(fp, "# total memory : unknown\n");
1242}
1243
1244static void print_numa_topology(struct perf_header *h __used, int fd, FILE *fp)
1245{
1246 ssize_t ret;
1247 u32 nr, c, i;
1248 char *str;
1249 uint64_t mem_total, mem_free;
1250
1251 /* nr nodes */
1252 ret = read(fd, &nr, sizeof(nr));
1253 if (ret != (ssize_t)sizeof(nr))
1254 goto error;
1255
1256 if (h->needs_swap)
1257 nr = bswap_32(nr);
1258
1259 for (i = 0; i < nr; i++) {
1260
1261 /* node number */
1262 ret = read(fd, &c, sizeof(c));
1263 if (ret != (ssize_t)sizeof(c))
1264 goto error;
1265
1266 if (h->needs_swap)
1267 c = bswap_32(c);
1268
1269 ret = read(fd, &mem_total, sizeof(u64));
1270 if (ret != sizeof(u64))
1271 goto error;
1272
1273 ret = read(fd, &mem_free, sizeof(u64));
1274 if (ret != sizeof(u64))
1275 goto error;
1276
1277 if (h->needs_swap) {
1278 mem_total = bswap_64(mem_total);
1279 mem_free = bswap_64(mem_free);
1280 }
1281
1282 fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB,"
1283 " free = %"PRIu64" kB\n",
1284 c,
1285 mem_total,
1286 mem_free);
1287
1288 str = do_read_string(fd, h);
1289 fprintf(fp, "# node%u cpu list : %s\n", c, str);
1290 free(str);
1291 }
1292 return;
1293error:
1294 fprintf(fp, "# numa topology : not available\n");
1295}
1296
1297static void print_cpuid(struct perf_header *ph, int fd, FILE *fp)
1298{
1299 char *str = do_read_string(fd, ph);
1300 fprintf(fp, "# cpuid : %s\n", str);
1301 free(str);
1302}
1303
1304struct feature_ops {
1305 int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
1306 void (*print)(struct perf_header *h, int fd, FILE *fp);
1307 const char *name;
1308 bool full_only;
1309};
1310
Robert Richter8cdfa782011-12-07 10:02:56 +01001311#define FEAT_OPA(n, func) \
1312 [n] = { .name = #n, .write = write_##func, .print = print_##func }
1313#define FEAT_OPF(n, func) \
1314 [n] = { .name = #n, .write = write_##func, .print = print_##func, .full_only = true }
1315
1316/* feature_ops not implemented: */
1317#define print_trace_info NULL
1318#define print_build_id NULL
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001319
1320static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
Robert Richter8cdfa782011-12-07 10:02:56 +01001321 FEAT_OPA(HEADER_TRACE_INFO, trace_info),
1322 FEAT_OPA(HEADER_BUILD_ID, build_id),
1323 FEAT_OPA(HEADER_HOSTNAME, hostname),
1324 FEAT_OPA(HEADER_OSRELEASE, osrelease),
1325 FEAT_OPA(HEADER_VERSION, version),
1326 FEAT_OPA(HEADER_ARCH, arch),
1327 FEAT_OPA(HEADER_NRCPUS, nrcpus),
1328 FEAT_OPA(HEADER_CPUDESC, cpudesc),
1329 FEAT_OPA(HEADER_CPUID, cpuid),
1330 FEAT_OPA(HEADER_TOTAL_MEM, total_mem),
1331 FEAT_OPA(HEADER_EVENT_DESC, event_desc),
1332 FEAT_OPA(HEADER_CMDLINE, cmdline),
1333 FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology),
1334 FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology),
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001335};
1336
1337struct header_print_data {
1338 FILE *fp;
1339 bool full; /* extended list of headers */
1340};
1341
1342static int perf_file_section__fprintf_info(struct perf_file_section *section,
1343 struct perf_header *ph,
1344 int feat, int fd, void *data)
1345{
1346 struct header_print_data *hd = data;
1347
1348 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
1349 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
1350 "%d, continuing...\n", section->offset, feat);
1351 return 0;
1352 }
1353 if (feat < HEADER_TRACE_INFO || feat >= HEADER_LAST_FEATURE) {
1354 pr_warning("unknown feature %d\n", feat);
Robert Richterf7a8a132011-12-07 10:02:51 +01001355 return 0;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001356 }
1357 if (!feat_ops[feat].print)
1358 return 0;
1359
1360 if (!feat_ops[feat].full_only || hd->full)
1361 feat_ops[feat].print(ph, fd, hd->fp);
1362 else
1363 fprintf(hd->fp, "# %s info available, use -I to display\n",
1364 feat_ops[feat].name);
1365
1366 return 0;
1367}
1368
1369int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full)
1370{
1371 struct header_print_data hd;
1372 struct perf_header *header = &session->header;
1373 int fd = session->fd;
1374 hd.fp = fp;
1375 hd.full = full;
1376
1377 perf_header__process_sections(header, fd, &hd,
1378 perf_file_section__fprintf_info);
1379 return 0;
1380}
1381
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001382static int do_write_feat(int fd, struct perf_header *h, int type,
1383 struct perf_file_section **p,
1384 struct perf_evlist *evlist)
1385{
1386 int err;
1387 int ret = 0;
1388
1389 if (perf_header__has_feat(h, type)) {
1390
1391 (*p)->offset = lseek(fd, 0, SEEK_CUR);
1392
1393 err = feat_ops[type].write(fd, h, evlist);
1394 if (err < 0) {
1395 pr_debug("failed to write feature %d\n", type);
1396
1397 /* undo anything written */
1398 lseek(fd, (*p)->offset, SEEK_SET);
1399
1400 return -1;
1401 }
1402 (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset;
1403 (*p)++;
1404 }
1405 return ret;
1406}
1407
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001408static int perf_header__adds_write(struct perf_header *header,
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001409 struct perf_evlist *evlist, int fd)
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001410{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001411 int nr_sections;
Arnaldo Carvalho de Melof8690972010-05-19 13:41:23 -03001412 struct perf_session *session;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001413 struct perf_file_section *feat_sec, *p;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001414 int sec_size;
1415 u64 sec_start;
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001416 int err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001417
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001418 session = container_of(header, struct perf_session, header);
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -02001419
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001420 if (perf_header__has_feat(header, HEADER_BUILD_ID &&
Arnaldo Carvalho de Melobaa2f6c2010-11-26 19:39:15 -02001421 !perf_session__read_build_ids(session, true)))
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001422 perf_header__clear_feat(header, HEADER_BUILD_ID);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001423
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001424 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001425 if (!nr_sections)
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001426 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001427
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001428 feat_sec = p = calloc(sizeof(*feat_sec), nr_sections);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001429 if (feat_sec == NULL)
1430 return -ENOMEM;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001431
1432 sec_size = sizeof(*feat_sec) * nr_sections;
1433
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001434 sec_start = header->data_offset + header->data_size;
Xiao Guangrongf887f302010-02-04 16:46:42 +08001435 lseek(fd, sec_start + sec_size, SEEK_SET);
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001436
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001437 err = do_write_feat(fd, header, HEADER_TRACE_INFO, &p, evlist);
1438 if (err)
1439 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001440
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001441 err = do_write_feat(fd, header, HEADER_BUILD_ID, &p, evlist);
1442 if (err) {
1443 perf_header__clear_feat(header, HEADER_BUILD_ID);
1444 goto out_free;
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001445 }
Frederic Weisbecker8671dab2009-11-11 04:51:03 +01001446
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001447 err = do_write_feat(fd, header, HEADER_HOSTNAME, &p, evlist);
1448 if (err)
1449 perf_header__clear_feat(header, HEADER_HOSTNAME);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001450
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001451 err = do_write_feat(fd, header, HEADER_OSRELEASE, &p, evlist);
1452 if (err)
1453 perf_header__clear_feat(header, HEADER_OSRELEASE);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001454
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001455 err = do_write_feat(fd, header, HEADER_VERSION, &p, evlist);
1456 if (err)
1457 perf_header__clear_feat(header, HEADER_VERSION);
1458
1459 err = do_write_feat(fd, header, HEADER_ARCH, &p, evlist);
1460 if (err)
1461 perf_header__clear_feat(header, HEADER_ARCH);
1462
1463 err = do_write_feat(fd, header, HEADER_NRCPUS, &p, evlist);
1464 if (err)
1465 perf_header__clear_feat(header, HEADER_NRCPUS);
1466
1467 err = do_write_feat(fd, header, HEADER_CPUDESC, &p, evlist);
1468 if (err)
1469 perf_header__clear_feat(header, HEADER_CPUDESC);
1470
1471 err = do_write_feat(fd, header, HEADER_CPUID, &p, evlist);
1472 if (err)
1473 perf_header__clear_feat(header, HEADER_CPUID);
1474
1475 err = do_write_feat(fd, header, HEADER_TOTAL_MEM, &p, evlist);
1476 if (err)
1477 perf_header__clear_feat(header, HEADER_TOTAL_MEM);
1478
1479 err = do_write_feat(fd, header, HEADER_CMDLINE, &p, evlist);
1480 if (err)
1481 perf_header__clear_feat(header, HEADER_CMDLINE);
1482
1483 err = do_write_feat(fd, header, HEADER_EVENT_DESC, &p, evlist);
1484 if (err)
1485 perf_header__clear_feat(header, HEADER_EVENT_DESC);
1486
1487 err = do_write_feat(fd, header, HEADER_CPU_TOPOLOGY, &p, evlist);
1488 if (err)
1489 perf_header__clear_feat(header, HEADER_CPU_TOPOLOGY);
1490
1491 err = do_write_feat(fd, header, HEADER_NUMA_TOPOLOGY, &p, evlist);
1492 if (err)
1493 perf_header__clear_feat(header, HEADER_NUMA_TOPOLOGY);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001494
Xiao Guangrongf887f302010-02-04 16:46:42 +08001495 lseek(fd, sec_start, SEEK_SET);
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001496 /*
1497 * may write more than needed due to dropped feature, but
1498 * this is okay, reader will skip the mising entries
1499 */
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001500 err = do_write(fd, feat_sec, sec_size);
1501 if (err < 0)
1502 pr_debug("failed to write feature section\n");
1503out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001504 free(feat_sec);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001505 return err;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001506}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001507
Tom Zanussi8dc58102010-04-01 23:59:15 -05001508int perf_header__write_pipe(int fd)
1509{
1510 struct perf_pipe_file_header f_header;
1511 int err;
1512
1513 f_header = (struct perf_pipe_file_header){
1514 .magic = PERF_MAGIC,
1515 .size = sizeof(f_header),
1516 };
1517
1518 err = do_write(fd, &f_header, sizeof(f_header));
1519 if (err < 0) {
1520 pr_debug("failed to write perf pipe header\n");
1521 return err;
1522 }
1523
1524 return 0;
1525}
1526
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001527int perf_session__write_header(struct perf_session *session,
1528 struct perf_evlist *evlist,
1529 int fd, bool at_exit)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001530{
1531 struct perf_file_header f_header;
1532 struct perf_file_attr f_attr;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001533 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001534 struct perf_evsel *attr, *pair = NULL;
1535 int err;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001536
1537 lseek(fd, sizeof(f_header), SEEK_SET);
1538
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001539 if (session->evlist != evlist)
1540 pair = list_entry(session->evlist->entries.next, struct perf_evsel, node);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001541
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001542 list_for_each_entry(attr, &evlist->entries, node) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001543 attr->id_offset = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001544 err = do_write(fd, attr->id, attr->ids * sizeof(u64));
1545 if (err < 0) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001546out_err_write:
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001547 pr_debug("failed to write perf header\n");
1548 return err;
1549 }
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001550 if (session->evlist != evlist) {
1551 err = do_write(fd, pair->id, pair->ids * sizeof(u64));
1552 if (err < 0)
1553 goto out_err_write;
1554 attr->ids += pair->ids;
1555 pair = list_entry(pair->node.next, struct perf_evsel, node);
1556 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001557 }
1558
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001559 header->attr_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001560
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001561 list_for_each_entry(attr, &evlist->entries, node) {
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001562 f_attr = (struct perf_file_attr){
1563 .attr = attr->attr,
1564 .ids = {
1565 .offset = attr->id_offset,
1566 .size = attr->ids * sizeof(u64),
1567 }
1568 };
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001569 err = do_write(fd, &f_attr, sizeof(f_attr));
1570 if (err < 0) {
1571 pr_debug("failed to write perf header attribute\n");
1572 return err;
1573 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001574 }
1575
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001576 header->event_offset = lseek(fd, 0, SEEK_CUR);
1577 header->event_size = event_count * sizeof(struct perf_trace_event_type);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001578 if (events) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001579 err = do_write(fd, events, header->event_size);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001580 if (err < 0) {
1581 pr_debug("failed to write perf header events\n");
1582 return err;
1583 }
1584 }
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02001585
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001586 header->data_offset = lseek(fd, 0, SEEK_CUR);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001587
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001588 if (at_exit) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001589 err = perf_header__adds_write(header, evlist, fd);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001590 if (err < 0)
1591 return err;
1592 }
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001593
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001594 f_header = (struct perf_file_header){
1595 .magic = PERF_MAGIC,
1596 .size = sizeof(f_header),
1597 .attr_size = sizeof(f_attr),
1598 .attrs = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001599 .offset = header->attr_offset,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001600 .size = evlist->nr_entries * sizeof(f_attr),
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001601 },
1602 .data = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001603 .offset = header->data_offset,
1604 .size = header->data_size,
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001605 },
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02001606 .event_types = {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001607 .offset = header->event_offset,
1608 .size = header->event_size,
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02001609 },
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001610 };
1611
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001612 memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features));
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001613
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001614 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001615 err = do_write(fd, &f_header, sizeof(f_header));
1616 if (err < 0) {
1617 pr_debug("failed to write perf header\n");
1618 return err;
1619 }
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001620 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001621
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001622 header->frozen = 1;
Arnaldo Carvalho de Melod5eed902009-11-19 14:55:56 -02001623 return 0;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001624}
1625
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001626static int perf_header__getbuffer64(struct perf_header *header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001627 int fd, void *buf, size_t size)
1628{
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -02001629 if (readn(fd, buf, size) <= 0)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001630 return -1;
1631
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001632 if (header->needs_swap)
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001633 mem_bswap_64(buf, size);
1634
1635 return 0;
1636}
1637
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001638int perf_header__process_sections(struct perf_header *header, int fd,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001639 void *data,
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001640 int (*process)(struct perf_file_section *section,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001641 struct perf_header *ph,
1642 int feat, int fd, void *data))
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001643{
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001644 struct perf_file_section *feat_sec;
1645 int nr_sections;
1646 int sec_size;
1647 int idx = 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02001648 int err = -1, feat = 1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001649
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001650 nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001651 if (!nr_sections)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001652 return 0;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001653
1654 feat_sec = calloc(sizeof(*feat_sec), nr_sections);
1655 if (!feat_sec)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001656 return -1;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001657
1658 sec_size = sizeof(*feat_sec) * nr_sections;
1659
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001660 lseek(fd, header->data_offset + header->data_size, SEEK_SET);
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001661
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001662 if (perf_header__getbuffer64(header, fd, feat_sec, sec_size))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02001663 goto out_free;
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001664
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02001665 err = 0;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001666 while (idx < nr_sections && feat < HEADER_LAST_FEATURE) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001667 if (perf_header__has_feat(header, feat)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001668 struct perf_file_section *sec = &feat_sec[idx++];
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001669
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001670 err = process(sec, header, feat, fd, data);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001671 if (err < 0)
1672 break;
1673 }
1674 ++feat;
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01001675 }
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02001676out_free:
Frederic Weisbecker9e827dd2009-11-11 04:51:07 +01001677 free(feat_sec);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001678 return err;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02001679}
Frederic Weisbecker2ba08252009-10-17 17:12:34 +02001680
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001681int perf_file_header__read(struct perf_file_header *header,
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001682 struct perf_header *ph, int fd)
1683{
1684 lseek(fd, 0, SEEK_SET);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001685
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001686 if (readn(fd, header, sizeof(*header)) <= 0 ||
1687 memcmp(&header->magic, __perf_magic, sizeof(header->magic)))
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001688 return -1;
1689
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001690 if (header->attr_size != sizeof(struct perf_file_attr)) {
1691 u64 attr_size = bswap_64(header->attr_size);
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001692
1693 if (attr_size != sizeof(struct perf_file_attr))
1694 return -1;
1695
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001696 mem_bswap_64(header, offsetof(struct perf_file_header,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001697 adds_features));
1698 ph->needs_swap = true;
1699 }
1700
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001701 if (header->size != sizeof(*header)) {
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001702 /* Support the previous format */
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001703 if (header->size == offsetof(typeof(*header), adds_features))
1704 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001705 else
1706 return -1;
David Ahernd327fa42011-10-18 17:34:01 -06001707 } else if (ph->needs_swap) {
1708 unsigned int i;
1709 /*
1710 * feature bitmap is declared as an array of unsigned longs --
1711 * not good since its size can differ between the host that
1712 * generated the data file and the host analyzing the file.
1713 *
1714 * We need to handle endianness, but we don't know the size of
1715 * the unsigned long where the file was generated. Take a best
1716 * guess at determining it: try 64-bit swap first (ie., file
1717 * created on a 64-bit host), and check if the hostname feature
1718 * bit is set (this feature bit is forced on as of fbe96f2).
1719 * If the bit is not, undo the 64-bit swap and try a 32-bit
1720 * swap. If the hostname bit is still not set (e.g., older data
1721 * file), punt and fallback to the original behavior --
1722 * clearing all feature bits and setting buildid.
1723 */
1724 for (i = 0; i < BITS_TO_LONGS(HEADER_FEAT_BITS); ++i)
1725 header->adds_features[i] = bswap_64(header->adds_features[i]);
1726
1727 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
1728 for (i = 0; i < BITS_TO_LONGS(HEADER_FEAT_BITS); ++i) {
1729 header->adds_features[i] = bswap_64(header->adds_features[i]);
1730 header->adds_features[i] = bswap_32(header->adds_features[i]);
1731 }
1732 }
1733
1734 if (!test_bit(HEADER_HOSTNAME, header->adds_features)) {
1735 bitmap_zero(header->adds_features, HEADER_FEAT_BITS);
1736 set_bit(HEADER_BUILD_ID, header->adds_features);
1737 }
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001738 }
1739
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001740 memcpy(&ph->adds_features, &header->adds_features,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001741 sizeof(ph->adds_features));
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001742
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001743 ph->event_offset = header->event_types.offset;
1744 ph->event_size = header->event_types.size;
1745 ph->data_offset = header->data.offset;
1746 ph->data_size = header->data.size;
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001747 return 0;
1748}
1749
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001750static int __event_process_build_id(struct build_id_event *bev,
1751 char *filename,
1752 struct perf_session *session)
1753{
1754 int err = -1;
1755 struct list_head *head;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001756 struct machine *machine;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001757 u16 misc;
1758 struct dso *dso;
1759 enum dso_kernel_type dso_type;
1760
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001761 machine = perf_session__findnew_machine(session, bev->pid);
1762 if (!machine)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001763 goto out;
1764
1765 misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1766
1767 switch (misc) {
1768 case PERF_RECORD_MISC_KERNEL:
1769 dso_type = DSO_TYPE_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001770 head = &machine->kernel_dsos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001771 break;
1772 case PERF_RECORD_MISC_GUEST_KERNEL:
1773 dso_type = DSO_TYPE_GUEST_KERNEL;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001774 head = &machine->kernel_dsos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001775 break;
1776 case PERF_RECORD_MISC_USER:
1777 case PERF_RECORD_MISC_GUEST_USER:
1778 dso_type = DSO_TYPE_USER;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03001779 head = &machine->user_dsos;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001780 break;
1781 default:
1782 goto out;
1783 }
1784
1785 dso = __dsos__findnew(head, filename);
1786 if (dso != NULL) {
Tom Zanussi454c4072010-05-01 01:41:20 -05001787 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
1788
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001789 dso__set_build_id(dso, &bev->build_id);
Tom Zanussi454c4072010-05-01 01:41:20 -05001790
1791 if (filename[0] == '[')
1792 dso->kernel = dso_type;
1793
1794 build_id__sprintf(dso->build_id, sizeof(dso->build_id),
1795 sbuild_id);
1796 pr_debug("build id event received for %s: %s\n",
1797 dso->long_name, sbuild_id);
1798 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001799
1800 err = 0;
1801out:
1802 return err;
1803}
1804
Arnaldo Carvalho de Melob2511482011-03-22 13:36:45 -03001805static int perf_header__read_build_ids_abi_quirk(struct perf_header *header,
1806 int input, u64 offset, u64 size)
1807{
1808 struct perf_session *session = container_of(header, struct perf_session, header);
1809 struct {
1810 struct perf_event_header header;
1811 u8 build_id[ALIGN(BUILD_ID_SIZE, sizeof(u64))];
1812 char filename[0];
1813 } old_bev;
1814 struct build_id_event bev;
1815 char filename[PATH_MAX];
1816 u64 limit = offset + size;
1817
1818 while (offset < limit) {
1819 ssize_t len;
1820
1821 if (read(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev))
1822 return -1;
1823
1824 if (header->needs_swap)
1825 perf_event_header__bswap(&old_bev.header);
1826
1827 len = old_bev.header.size - sizeof(old_bev);
1828 if (read(input, filename, len) != len)
1829 return -1;
1830
1831 bev.header = old_bev.header;
Jiri Olsaf57b05e2011-06-01 21:43:46 +02001832
1833 /*
1834 * As the pid is the missing value, we need to fill
1835 * it properly. The header.misc value give us nice hint.
1836 */
1837 bev.pid = HOST_KERNEL_ID;
1838 if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER ||
1839 bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL)
1840 bev.pid = DEFAULT_GUEST_KERNEL_ID;
1841
Arnaldo Carvalho de Melob2511482011-03-22 13:36:45 -03001842 memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id));
1843 __event_process_build_id(&bev, filename, session);
1844
1845 offset += bev.header.size;
1846 }
1847
1848 return 0;
1849}
1850
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001851static int perf_header__read_build_ids(struct perf_header *header,
1852 int input, u64 offset, u64 size)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001853{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001854 struct perf_session *session = container_of(header, struct perf_session, header);
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001855 struct build_id_event bev;
1856 char filename[PATH_MAX];
Arnaldo Carvalho de Melob2511482011-03-22 13:36:45 -03001857 u64 limit = offset + size, orig_offset = offset;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001858 int err = -1;
1859
1860 while (offset < limit) {
1861 ssize_t len;
1862
1863 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
1864 goto out;
1865
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001866 if (header->needs_swap)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001867 perf_event_header__bswap(&bev.header);
1868
1869 len = bev.header.size - sizeof(bev);
1870 if (read(input, filename, len) != len)
1871 goto out;
Arnaldo Carvalho de Melob2511482011-03-22 13:36:45 -03001872 /*
1873 * The a1645ce1 changeset:
1874 *
1875 * "perf: 'perf kvm' tool for monitoring guest performance from host"
1876 *
1877 * Added a field to struct build_id_event that broke the file
1878 * format.
1879 *
1880 * Since the kernel build-id is the first entry, process the
1881 * table using the old format if the well known
1882 * '[kernel.kallsyms]' string for the kernel build-id has the
1883 * first 4 characters chopped off (where the pid_t sits).
1884 */
1885 if (memcmp(filename, "nel.kallsyms]", 13) == 0) {
1886 if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1)
1887 return -1;
1888 return perf_header__read_build_ids_abi_quirk(header, input, offset, size);
1889 }
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08001890
1891 __event_process_build_id(&bev, filename, session);
1892
1893 offset += bev.header.size;
1894 }
1895 err = 0;
1896out:
1897 return err;
1898}
1899
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001900static int perf_file_section__process(struct perf_file_section *section,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001901 struct perf_header *ph,
Stephane Eranianfbe96f22011-09-30 15:40:40 +02001902 int feat, int fd, void *data __used)
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001903{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001904 if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -02001905 pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001906 "%d, continuing...\n", section->offset, feat);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001907 return 0;
1908 }
1909
1910 switch (feat) {
1911 case HEADER_TRACE_INFO:
Tom Zanussi454c4072010-05-01 01:41:20 -05001912 trace_report(fd, false);
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001913 break;
1914
1915 case HEADER_BUILD_ID:
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001916 if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001917 pr_debug("Failed to read buildids, continuing...\n");
1918 break;
David Ahern51192de2011-10-18 18:34:24 -06001919
1920 case HEADER_HOSTNAME:
1921 case HEADER_OSRELEASE:
1922 case HEADER_VERSION:
1923 case HEADER_ARCH:
1924 case HEADER_NRCPUS:
1925 case HEADER_CPUDESC:
1926 case HEADER_CPUID:
1927 case HEADER_TOTAL_MEM:
1928 case HEADER_CMDLINE:
1929 case HEADER_EVENT_DESC:
1930 case HEADER_CPU_TOPOLOGY:
1931 case HEADER_NUMA_TOPOLOGY:
1932 break;
1933
Arnaldo Carvalho de Melo37562ea2009-11-16 16:32:43 -02001934 default:
1935 pr_debug("unknown feature %d, continuing...\n", feat);
1936 }
1937
1938 return 0;
1939}
1940
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001941static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
Tom Zanussi454c4072010-05-01 01:41:20 -05001942 struct perf_header *ph, int fd,
1943 bool repipe)
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001944{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001945 if (readn(fd, header, sizeof(*header)) <= 0 ||
1946 memcmp(&header->magic, __perf_magic, sizeof(header->magic)))
Tom Zanussi8dc58102010-04-01 23:59:15 -05001947 return -1;
1948
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001949 if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0)
Tom Zanussi454c4072010-05-01 01:41:20 -05001950 return -1;
1951
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001952 if (header->size != sizeof(*header)) {
1953 u64 size = bswap_64(header->size);
Tom Zanussi8dc58102010-04-01 23:59:15 -05001954
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001955 if (size != sizeof(*header))
Tom Zanussi8dc58102010-04-01 23:59:15 -05001956 return -1;
1957
1958 ph->needs_swap = true;
1959 }
1960
1961 return 0;
1962}
1963
1964static int perf_header__read_pipe(struct perf_session *session, int fd)
1965{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001966 struct perf_header *header = &session->header;
Tom Zanussi8dc58102010-04-01 23:59:15 -05001967 struct perf_pipe_file_header f_header;
1968
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001969 if (perf_file_header__read_pipe(&f_header, header, fd,
Tom Zanussi454c4072010-05-01 01:41:20 -05001970 session->repipe) < 0) {
Tom Zanussi8dc58102010-04-01 23:59:15 -05001971 pr_debug("incompatible file format\n");
1972 return -EINVAL;
1973 }
1974
1975 session->fd = fd;
1976
1977 return 0;
1978}
1979
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001980int perf_session__read_header(struct perf_session *session, int fd)
Tom Zanussi8dc58102010-04-01 23:59:15 -05001981{
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001982 struct perf_header *header = &session->header;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02001983 struct perf_file_header f_header;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001984 struct perf_file_attr f_attr;
1985 u64 f_id;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001986 int nr_attrs, nr_ids, i, j;
1987
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001988 session->evlist = perf_evlist__new(NULL, NULL);
1989 if (session->evlist == NULL)
1990 return -ENOMEM;
1991
Tom Zanussi8dc58102010-04-01 23:59:15 -05001992 if (session->fd_pipe)
1993 return perf_header__read_pipe(session, fd);
1994
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03001995 if (perf_file_header__read(&f_header, header, fd) < 0) {
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02001996 pr_debug("incompatible file format\n");
1997 return -EINVAL;
1998 }
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02001999
2000 nr_attrs = f_header.attrs.size / sizeof(f_attr);
2001 lseek(fd, f_header.attrs.offset, SEEK_SET);
2002
2003 for (i = 0; i < nr_attrs; i++) {
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002004 struct perf_evsel *evsel;
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002005 off_t tmp;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002006
David Aherneda39132011-07-15 12:34:09 -06002007 if (readn(fd, &f_attr, sizeof(f_attr)) <= 0)
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002008 goto out_errno;
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002009
David Aherneda39132011-07-15 12:34:09 -06002010 if (header->needs_swap)
2011 perf_event__attr_swap(&f_attr.attr);
2012
Peter Zijlstra1c222bc2009-08-06 20:57:41 +02002013 tmp = lseek(fd, 0, SEEK_CUR);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002014 evsel = perf_evsel__new(&f_attr.attr, i);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002015
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002016 if (evsel == NULL)
2017 goto out_delete_evlist;
2018 /*
2019 * Do it before so that if perf_evsel__alloc_id fails, this
2020 * entry gets purged too at perf_evlist__delete().
2021 */
2022 perf_evlist__add(session->evlist, evsel);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002023
2024 nr_ids = f_attr.ids.size / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002025 /*
2026 * We don't have the cpu and thread maps on the header, so
2027 * for allocating the perf_sample_id table we fake 1 cpu and
2028 * hattr->ids threads.
2029 */
2030 if (perf_evsel__alloc_id(evsel, 1, nr_ids))
2031 goto out_delete_evlist;
2032
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002033 lseek(fd, f_attr.ids.offset, SEEK_SET);
2034
2035 for (j = 0; j < nr_ids; j++) {
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002036 if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id)))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002037 goto out_errno;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002038
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002039 perf_evlist__id_add(session->evlist, evsel, 0, j, f_id);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002040 }
Arnaldo Carvalho de Melo11deb1f2009-11-17 01:18:09 -02002041
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002042 lseek(fd, tmp, SEEK_SET);
2043 }
2044
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02002045 symbol_conf.nr_events = nr_attrs;
2046
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02002047 if (f_header.event_types.size) {
2048 lseek(fd, f_header.event_types.offset, SEEK_SET);
2049 events = malloc(f_header.event_types.size);
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002050 if (events == NULL)
2051 return -ENOMEM;
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002052 if (perf_header__getbuffer64(header, fd, events,
Arnaldo Carvalho de Meloba215942010-01-14 12:23:10 -02002053 f_header.event_types.size))
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002054 goto out_errno;
Arjan van de Ven8755a8f2009-09-12 07:52:51 +02002055 event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type);
2056 }
Frederic Weisbecker03456a12009-10-06 23:36:47 +02002057
Stephane Eranianfbe96f22011-09-30 15:40:40 +02002058 perf_header__process_sections(header, fd, NULL,
2059 perf_file_section__process);
Frederic Weisbecker4778d2e2009-11-11 04:51:05 +01002060
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002061 lseek(fd, header->data_offset, SEEK_SET);
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002062
Arnaldo Carvalho de Melo1c0b04d2011-03-09 08:13:19 -03002063 header->frozen = 1;
Arnaldo Carvalho de Melo4dc0a042009-11-19 14:55:55 -02002064 return 0;
Arnaldo Carvalho de Melo769885f2009-12-28 22:48:32 -02002065out_errno:
2066 return -errno;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002067
2068out_delete_evlist:
2069 perf_evlist__delete(session->evlist);
2070 session->evlist = NULL;
2071 return -ENOMEM;
Peter Zijlstra7c6a1c62009-06-25 17:05:54 +02002072}
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002073
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002074int perf_event__synthesize_attr(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002075 struct perf_event_attr *attr, u16 ids, u64 *id,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002076 perf_event__handler_t process)
Frederic Weisbecker0d3a5c82009-08-16 20:56:37 +02002077{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002078 union perf_event *ev;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002079 size_t size;
2080 int err;
2081
2082 size = sizeof(struct perf_event_attr);
2083 size = ALIGN(size, sizeof(u64));
2084 size += sizeof(struct perf_event_header);
2085 size += ids * sizeof(u64);
2086
2087 ev = malloc(size);
2088
Chris Samuelce47dc52010-11-13 13:35:06 +11002089 if (ev == NULL)
2090 return -ENOMEM;
2091
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002092 ev->attr.attr = *attr;
2093 memcpy(ev->attr.id, id, ids * sizeof(u64));
2094
2095 ev->attr.header.type = PERF_RECORD_HEADER_ATTR;
2096 ev->attr.header.size = size;
2097
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002098 err = process(tool, ev, NULL, NULL);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002099
2100 free(ev);
2101
2102 return err;
2103}
2104
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002105int perf_event__synthesize_attrs(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002106 struct perf_session *session,
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002107 perf_event__handler_t process)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002108{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002109 struct perf_evsel *attr;
2110 int err = 0;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002111
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002112 list_for_each_entry(attr, &session->evlist->entries, node) {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002113 err = perf_event__synthesize_attr(tool, &attr->attr, attr->ids,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002114 attr->id, process);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002115 if (err) {
2116 pr_debug("failed to create perf header attribute\n");
2117 return err;
2118 }
2119 }
2120
2121 return err;
2122}
2123
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002124int perf_event__process_attr(union perf_event *event,
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002125 struct perf_evlist **pevlist)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002126{
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002127 unsigned int i, ids, n_ids;
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002128 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002129 struct perf_evlist *evlist = *pevlist;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002130
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002131 if (evlist == NULL) {
2132 *pevlist = evlist = perf_evlist__new(NULL, NULL);
2133 if (evlist == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002134 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002135 }
2136
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002137 evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002138 if (evsel == NULL)
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002139 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002140
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002141 perf_evlist__add(evlist, evsel);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002142
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002143 ids = event->header.size;
2144 ids -= (void *)&event->attr.id - (void *)event;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002145 n_ids = ids / sizeof(u64);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03002146 /*
2147 * We don't have the cpu and thread maps on the header, so
2148 * for allocating the perf_sample_id table we fake 1 cpu and
2149 * hattr->ids threads.
2150 */
2151 if (perf_evsel__alloc_id(evsel, 1, n_ids))
2152 return -ENOMEM;
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002153
2154 for (i = 0; i < n_ids; i++) {
Arnaldo Carvalho de Melo10d0f082011-11-11 22:45:41 -02002155 perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]);
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002156 }
2157
Tom Zanussi2c46dbb2010-04-01 23:59:19 -05002158 return 0;
2159}
Tom Zanussicd19a032010-04-01 23:59:20 -05002160
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002161int perf_event__synthesize_event_type(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002162 u64 event_id, char *name,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002163 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002164 struct machine *machine)
Tom Zanussicd19a032010-04-01 23:59:20 -05002165{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002166 union perf_event ev;
Tom Zanussicd19a032010-04-01 23:59:20 -05002167 size_t size = 0;
2168 int err = 0;
2169
2170 memset(&ev, 0, sizeof(ev));
2171
2172 ev.event_type.event_type.event_id = event_id;
2173 memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME);
2174 strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1);
2175
2176 ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE;
2177 size = strlen(name);
2178 size = ALIGN(size, sizeof(u64));
2179 ev.event_type.header.size = sizeof(ev.event_type) -
2180 (sizeof(ev.event_type.event_type.name) - size);
2181
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002182 err = process(tool, &ev, NULL, machine);
Tom Zanussicd19a032010-04-01 23:59:20 -05002183
2184 return err;
2185}
2186
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002187int perf_event__synthesize_event_types(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002188 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002189 struct machine *machine)
Tom Zanussicd19a032010-04-01 23:59:20 -05002190{
2191 struct perf_trace_event_type *type;
2192 int i, err = 0;
2193
2194 for (i = 0; i < event_count; i++) {
2195 type = &events[i];
2196
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002197 err = perf_event__synthesize_event_type(tool, type->event_id,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002198 type->name, process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002199 machine);
Tom Zanussicd19a032010-04-01 23:59:20 -05002200 if (err) {
2201 pr_debug("failed to create perf header event type\n");
2202 return err;
2203 }
2204 }
2205
2206 return err;
2207}
2208
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002209int perf_event__process_event_type(struct perf_tool *tool __unused,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002210 union perf_event *event)
Tom Zanussicd19a032010-04-01 23:59:20 -05002211{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002212 if (perf_header__push_event(event->event_type.event_type.event_id,
2213 event->event_type.event_type.name) < 0)
Tom Zanussicd19a032010-04-01 23:59:20 -05002214 return -ENOMEM;
2215
2216 return 0;
2217}
Tom Zanussi92155452010-04-01 23:59:21 -05002218
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002219int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002220 struct perf_evlist *evlist,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002221 perf_event__handler_t process)
Tom Zanussi92155452010-04-01 23:59:21 -05002222{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002223 union perf_event ev;
Jiri Olsa29208e52011-10-20 15:59:43 +02002224 struct tracing_data *tdata;
Tom Zanussi92155452010-04-01 23:59:21 -05002225 ssize_t size = 0, aligned_size = 0, padding;
Kyle McMartinfb7d0b32011-01-24 11:13:04 -05002226 int err __used = 0;
Tom Zanussi92155452010-04-01 23:59:21 -05002227
Jiri Olsa29208e52011-10-20 15:59:43 +02002228 /*
2229 * We are going to store the size of the data followed
2230 * by the data contents. Since the fd descriptor is a pipe,
2231 * we cannot seek back to store the size of the data once
2232 * we know it. Instead we:
2233 *
2234 * - write the tracing data to the temp file
2235 * - get/write the data size to pipe
2236 * - write the tracing data from the temp file
2237 * to the pipe
2238 */
2239 tdata = tracing_data_get(&evlist->entries, fd, true);
2240 if (!tdata)
2241 return -1;
2242
Tom Zanussi92155452010-04-01 23:59:21 -05002243 memset(&ev, 0, sizeof(ev));
2244
2245 ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
Jiri Olsa29208e52011-10-20 15:59:43 +02002246 size = tdata->size;
Tom Zanussi92155452010-04-01 23:59:21 -05002247 aligned_size = ALIGN(size, sizeof(u64));
2248 padding = aligned_size - size;
2249 ev.tracing_data.header.size = sizeof(ev.tracing_data);
2250 ev.tracing_data.size = aligned_size;
2251
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002252 process(tool, &ev, NULL, NULL);
Tom Zanussi92155452010-04-01 23:59:21 -05002253
Jiri Olsa29208e52011-10-20 15:59:43 +02002254 /*
2255 * The put function will copy all the tracing data
2256 * stored in temp file to the pipe.
2257 */
2258 tracing_data_put(tdata);
2259
Tom Zanussi92155452010-04-01 23:59:21 -05002260 write_padded(fd, NULL, 0, padding);
2261
2262 return aligned_size;
2263}
2264
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002265int perf_event__process_tracing_data(union perf_event *event,
2266 struct perf_session *session)
Tom Zanussi92155452010-04-01 23:59:21 -05002267{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002268 ssize_t size_read, padding, size = event->tracing_data.size;
Tom Zanussi92155452010-04-01 23:59:21 -05002269 off_t offset = lseek(session->fd, 0, SEEK_CUR);
2270 char buf[BUFSIZ];
2271
2272 /* setup for reading amidst mmap */
2273 lseek(session->fd, offset + sizeof(struct tracing_data_event),
2274 SEEK_SET);
2275
Tom Zanussi454c4072010-05-01 01:41:20 -05002276 size_read = trace_report(session->fd, session->repipe);
Tom Zanussi92155452010-04-01 23:59:21 -05002277
2278 padding = ALIGN(size_read, sizeof(u64)) - size_read;
2279
2280 if (read(session->fd, buf, padding) < 0)
2281 die("reading input file");
Tom Zanussi454c4072010-05-01 01:41:20 -05002282 if (session->repipe) {
2283 int retw = write(STDOUT_FILENO, buf, padding);
2284 if (retw <= 0 || retw != padding)
2285 die("repiping tracing data padding");
2286 }
Tom Zanussi92155452010-04-01 23:59:21 -05002287
2288 if (size_read + padding != size)
2289 die("tracing data size mismatch");
2290
2291 return size_read + padding;
2292}
Tom Zanussic7929e42010-04-01 23:59:22 -05002293
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002294int perf_event__synthesize_build_id(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002295 struct dso *pos, u16 misc,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002296 perf_event__handler_t process,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -02002297 struct machine *machine)
Tom Zanussic7929e42010-04-01 23:59:22 -05002298{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002299 union perf_event ev;
Tom Zanussic7929e42010-04-01 23:59:22 -05002300 size_t len;
2301 int err = 0;
2302
2303 if (!pos->hit)
2304 return err;
2305
2306 memset(&ev, 0, sizeof(ev));
2307
2308 len = pos->long_name_len + 1;
2309 len = ALIGN(len, NAME_ALIGN);
2310 memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id));
2311 ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID;
2312 ev.build_id.header.misc = misc;
Arnaldo Carvalho de Melo23346f22010-04-27 21:17:50 -03002313 ev.build_id.pid = machine->pid;
Tom Zanussic7929e42010-04-01 23:59:22 -05002314 ev.build_id.header.size = sizeof(ev.build_id) + len;
2315 memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len);
2316
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002317 err = process(tool, &ev, NULL, machine);
Tom Zanussic7929e42010-04-01 23:59:22 -05002318
2319 return err;
2320}
2321
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -02002322int perf_event__process_build_id(struct perf_tool *tool __used,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -02002323 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002324 struct perf_session *session)
Tom Zanussic7929e42010-04-01 23:59:22 -05002325{
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -02002326 __event_process_build_id(&event->build_id,
2327 event->build_id.filename,
Zhang, Yanmina1645ce2010-04-19 13:32:50 +08002328 session);
Tom Zanussic7929e42010-04-01 23:59:22 -05002329 return 0;
2330}
Stephane Eraniana1ac1d32010-06-17 11:39:01 +02002331
2332void disable_buildid_cache(void)
2333{
2334 no_buildid_cache = true;
2335}