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