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