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