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