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