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