Xiao Guangrong | b8f46c5 | 2010-02-03 11:53:14 +0800 | [diff] [blame] | 1 | #define _FILE_OFFSET_BITS 64 |
| 2 | |
Arnaldo Carvalho de Melo | a9072bc | 2011-10-26 12:41:38 -0200 | [diff] [blame] | 3 | #include "util.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 4 | #include <sys/types.h> |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 5 | #include <byteswap.h> |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 6 | #include <unistd.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 9 | #include <linux/list.h> |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 12 | #include <sys/utsname.h> |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 13 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 14 | #include "evlist.h" |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 15 | #include "evsel.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 16 | #include "header.h" |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 17 | #include "../perf.h" |
| 18 | #include "trace-event.h" |
Arnaldo Carvalho de Melo | 301a0b0 | 2009-12-13 19:50:25 -0200 | [diff] [blame] | 19 | #include "session.h" |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 20 | #include "symbol.h" |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 21 | #include "debug.h" |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 22 | #include "cpumap.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 23 | |
Stephane Eranian | a1ac1d3 | 2010-06-17 11:39:01 +0200 | [diff] [blame] | 24 | static bool no_buildid_cache = false; |
| 25 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 26 | static int event_count; |
| 27 | static struct perf_trace_event_type *events; |
| 28 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 29 | static u32 header_argc; |
| 30 | static const char **header_argv; |
| 31 | |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 32 | int perf_header__push_event(u64 id, const char *name) |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 33 | { |
Ulrich Drepper | f3054c7 | 2012-04-23 09:04:29 -0400 | [diff] [blame] | 34 | struct perf_trace_event_type *nevents; |
| 35 | |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 36 | if (strlen(name) > MAX_EVENT_NAME) |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 37 | pr_warning("Event %s will be truncated\n", name); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 38 | |
Ulrich Drepper | f3054c7 | 2012-04-23 09:04:29 -0400 | [diff] [blame] | 39 | nevents = realloc(events, (event_count + 1) * sizeof(*events)); |
| 40 | if (nevents == NULL) |
| 41 | return -ENOMEM; |
| 42 | events = nevents; |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 43 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 44 | memset(&events[event_count], 0, sizeof(struct perf_trace_event_type)); |
| 45 | events[event_count].event_id = id; |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 46 | strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 47 | event_count++; |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 48 | return 0; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | char *perf_header__find_event(u64 id) |
| 52 | { |
| 53 | int i; |
| 54 | for (i = 0 ; i < event_count; i++) { |
| 55 | if (events[i].event_id == id) |
| 56 | return events[i].name; |
| 57 | } |
| 58 | return NULL; |
| 59 | } |
| 60 | |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 61 | /* |
| 62 | * magic2 = "PERFILE2" |
| 63 | * must be a numerical value to let the endianness |
| 64 | * determine the memory layout. That way we are able |
| 65 | * to detect endianness when reading the perf.data file |
| 66 | * back. |
| 67 | * |
| 68 | * we check for legacy (PERFFILE) format. |
| 69 | */ |
| 70 | static const char *__perf_magic1 = "PERFFILE"; |
| 71 | static const u64 __perf_magic2 = 0x32454c4946524550ULL; |
| 72 | static const u64 __perf_magic2_sw = 0x50455246494c4532ULL; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 73 | |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 74 | #define PERF_MAGIC __perf_magic2 |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 75 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 76 | struct perf_file_attr { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 77 | struct perf_event_attr attr; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 78 | struct perf_file_section ids; |
| 79 | }; |
| 80 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 81 | void perf_header__set_feat(struct perf_header *header, int feat) |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 82 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 83 | set_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 84 | } |
| 85 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 86 | void perf_header__clear_feat(struct perf_header *header, int feat) |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 87 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 88 | clear_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 89 | } |
| 90 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 91 | bool perf_header__has_feat(const struct perf_header *header, int feat) |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 92 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 93 | return test_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 94 | } |
| 95 | |
Arnaldo Carvalho de Melo | 3726cc7 | 2009-11-17 01:18:12 -0200 | [diff] [blame] | 96 | static int do_write(int fd, const void *buf, size_t size) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 97 | { |
| 98 | while (size) { |
| 99 | int ret = write(fd, buf, size); |
| 100 | |
| 101 | if (ret < 0) |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 102 | return -errno; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 103 | |
| 104 | size -= ret; |
| 105 | buf += ret; |
| 106 | } |
Arnaldo Carvalho de Melo | 3726cc7 | 2009-11-17 01:18:12 -0200 | [diff] [blame] | 107 | |
| 108 | return 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Arnaldo Carvalho de Melo | f92cb24 | 2010-01-04 16:19:28 -0200 | [diff] [blame] | 111 | #define NAME_ALIGN 64 |
| 112 | |
| 113 | static int write_padded(int fd, const void *bf, size_t count, |
| 114 | size_t count_aligned) |
| 115 | { |
| 116 | static const char zero_buf[NAME_ALIGN]; |
| 117 | int err = do_write(fd, bf, count); |
| 118 | |
| 119 | if (!err) |
| 120 | err = do_write(fd, zero_buf, count_aligned - count); |
| 121 | |
| 122 | return err; |
| 123 | } |
| 124 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 125 | static int do_write_string(int fd, const char *str) |
| 126 | { |
| 127 | u32 len, olen; |
| 128 | int ret; |
| 129 | |
| 130 | olen = strlen(str) + 1; |
| 131 | len = ALIGN(olen, NAME_ALIGN); |
| 132 | |
| 133 | /* write len, incl. \0 */ |
| 134 | ret = do_write(fd, &len, sizeof(len)); |
| 135 | if (ret < 0) |
| 136 | return ret; |
| 137 | |
| 138 | return write_padded(fd, str, olen, len); |
| 139 | } |
| 140 | |
| 141 | static char *do_read_string(int fd, struct perf_header *ph) |
| 142 | { |
| 143 | ssize_t sz, ret; |
| 144 | u32 len; |
| 145 | char *buf; |
| 146 | |
| 147 | sz = read(fd, &len, sizeof(len)); |
| 148 | if (sz < (ssize_t)sizeof(len)) |
| 149 | return NULL; |
| 150 | |
| 151 | if (ph->needs_swap) |
| 152 | len = bswap_32(len); |
| 153 | |
| 154 | buf = malloc(len); |
| 155 | if (!buf) |
| 156 | return NULL; |
| 157 | |
| 158 | ret = read(fd, buf, len); |
| 159 | if (ret == (ssize_t)len) { |
| 160 | /* |
| 161 | * strings are padded by zeroes |
| 162 | * thus the actual strlen of buf |
| 163 | * may be less than len |
| 164 | */ |
| 165 | return buf; |
| 166 | } |
| 167 | |
| 168 | free(buf); |
| 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | int |
| 173 | perf_header__set_cmdline(int argc, const char **argv) |
| 174 | { |
| 175 | int i; |
| 176 | |
| 177 | header_argc = (u32)argc; |
| 178 | |
| 179 | /* do not include NULL termination */ |
| 180 | header_argv = calloc(argc, sizeof(char *)); |
| 181 | if (!header_argv) |
| 182 | return -ENOMEM; |
| 183 | |
| 184 | /* |
| 185 | * must copy argv contents because it gets moved |
| 186 | * around during option parsing |
| 187 | */ |
| 188 | for (i = 0; i < argc ; i++) |
| 189 | header_argv[i] = argv[i]; |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 194 | #define dsos__for_each_with_build_id(pos, head) \ |
| 195 | list_for_each_entry(pos, head, node) \ |
| 196 | if (!pos->has_build_id) \ |
| 197 | continue; \ |
| 198 | else |
| 199 | |
| 200 | static int __dsos__write_buildid_table(struct list_head *head, pid_t pid, |
| 201 | u16 misc, int fd) |
| 202 | { |
| 203 | struct dso *pos; |
| 204 | |
| 205 | dsos__for_each_with_build_id(pos, head) { |
| 206 | int err; |
| 207 | struct build_id_event b; |
| 208 | size_t len; |
| 209 | |
| 210 | if (!pos->hit) |
| 211 | continue; |
| 212 | len = pos->long_name_len + 1; |
| 213 | len = ALIGN(len, NAME_ALIGN); |
| 214 | memset(&b, 0, sizeof(b)); |
| 215 | memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id)); |
| 216 | b.pid = pid; |
| 217 | b.header.misc = misc; |
| 218 | b.header.size = sizeof(b) + len; |
| 219 | err = do_write(fd, &b, sizeof(b)); |
| 220 | if (err < 0) |
| 221 | return err; |
| 222 | err = write_padded(fd, pos->long_name, |
| 223 | pos->long_name_len + 1, len); |
| 224 | if (err < 0) |
| 225 | return err; |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int machine__write_buildid_table(struct machine *machine, int fd) |
| 232 | { |
| 233 | int err; |
| 234 | u16 kmisc = PERF_RECORD_MISC_KERNEL, |
| 235 | umisc = PERF_RECORD_MISC_USER; |
| 236 | |
| 237 | if (!machine__is_host(machine)) { |
| 238 | kmisc = PERF_RECORD_MISC_GUEST_KERNEL; |
| 239 | umisc = PERF_RECORD_MISC_GUEST_USER; |
| 240 | } |
| 241 | |
| 242 | err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid, |
| 243 | kmisc, fd); |
| 244 | if (err == 0) |
| 245 | err = __dsos__write_buildid_table(&machine->user_dsos, |
| 246 | machine->pid, umisc, fd); |
| 247 | return err; |
| 248 | } |
| 249 | |
| 250 | static int dsos__write_buildid_table(struct perf_header *header, int fd) |
| 251 | { |
| 252 | struct perf_session *session = container_of(header, |
| 253 | struct perf_session, header); |
| 254 | struct rb_node *nd; |
| 255 | int err = machine__write_buildid_table(&session->host_machine, fd); |
| 256 | |
| 257 | if (err) |
| 258 | return err; |
| 259 | |
| 260 | for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { |
| 261 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 262 | err = machine__write_buildid_table(pos, fd); |
| 263 | if (err) |
| 264 | break; |
| 265 | } |
| 266 | return err; |
| 267 | } |
| 268 | |
| 269 | int build_id_cache__add_s(const char *sbuild_id, const char *debugdir, |
| 270 | const char *name, bool is_kallsyms) |
| 271 | { |
| 272 | const size_t size = PATH_MAX; |
| 273 | char *realname, *filename = zalloc(size), |
| 274 | *linkname = zalloc(size), *targetname; |
| 275 | int len, err = -1; |
| 276 | |
| 277 | if (is_kallsyms) { |
| 278 | if (symbol_conf.kptr_restrict) { |
| 279 | pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n"); |
| 280 | return 0; |
| 281 | } |
| 282 | realname = (char *)name; |
| 283 | } else |
| 284 | realname = realpath(name, NULL); |
| 285 | |
| 286 | if (realname == NULL || filename == NULL || linkname == NULL) |
| 287 | goto out_free; |
| 288 | |
Arnaldo Carvalho de Melo | e7f01d1 | 2012-03-14 12:29:29 -0300 | [diff] [blame] | 289 | len = scnprintf(filename, size, "%s%s%s", |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 290 | debugdir, is_kallsyms ? "/" : "", realname); |
| 291 | if (mkdir_p(filename, 0755)) |
| 292 | goto out_free; |
| 293 | |
Namhyung Kim | afda0f9 | 2012-05-01 23:19:36 +0900 | [diff] [blame] | 294 | snprintf(filename + len, size - len, "/%s", sbuild_id); |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 295 | |
| 296 | if (access(filename, F_OK)) { |
| 297 | if (is_kallsyms) { |
| 298 | if (copyfile("/proc/kallsyms", filename)) |
| 299 | goto out_free; |
| 300 | } else if (link(realname, filename) && copyfile(name, filename)) |
| 301 | goto out_free; |
| 302 | } |
| 303 | |
Arnaldo Carvalho de Melo | e7f01d1 | 2012-03-14 12:29:29 -0300 | [diff] [blame] | 304 | len = scnprintf(linkname, size, "%s/.build-id/%.2s", |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 305 | debugdir, sbuild_id); |
| 306 | |
| 307 | if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) |
| 308 | goto out_free; |
| 309 | |
| 310 | snprintf(linkname + len, size - len, "/%s", sbuild_id + 2); |
| 311 | targetname = filename + strlen(debugdir) - 5; |
| 312 | memcpy(targetname, "../..", 5); |
| 313 | |
| 314 | if (symlink(targetname, linkname) == 0) |
| 315 | err = 0; |
| 316 | out_free: |
| 317 | if (!is_kallsyms) |
| 318 | free(realname); |
| 319 | free(filename); |
| 320 | free(linkname); |
| 321 | return err; |
| 322 | } |
| 323 | |
| 324 | static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size, |
| 325 | const char *name, const char *debugdir, |
| 326 | bool is_kallsyms) |
| 327 | { |
| 328 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 329 | |
| 330 | build_id__sprintf(build_id, build_id_size, sbuild_id); |
| 331 | |
| 332 | return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms); |
| 333 | } |
| 334 | |
| 335 | int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir) |
| 336 | { |
| 337 | const size_t size = PATH_MAX; |
| 338 | char *filename = zalloc(size), |
| 339 | *linkname = zalloc(size); |
| 340 | int err = -1; |
| 341 | |
| 342 | if (filename == NULL || linkname == NULL) |
| 343 | goto out_free; |
| 344 | |
| 345 | snprintf(linkname, size, "%s/.build-id/%.2s/%s", |
| 346 | debugdir, sbuild_id, sbuild_id + 2); |
| 347 | |
| 348 | if (access(linkname, F_OK)) |
| 349 | goto out_free; |
| 350 | |
| 351 | if (readlink(linkname, filename, size - 1) < 0) |
| 352 | goto out_free; |
| 353 | |
| 354 | if (unlink(linkname)) |
| 355 | goto out_free; |
| 356 | |
| 357 | /* |
| 358 | * Since the link is relative, we must make it absolute: |
| 359 | */ |
| 360 | snprintf(linkname, size, "%s/.build-id/%.2s/%s", |
| 361 | debugdir, sbuild_id, filename); |
| 362 | |
| 363 | if (unlink(linkname)) |
| 364 | goto out_free; |
| 365 | |
| 366 | err = 0; |
| 367 | out_free: |
| 368 | free(filename); |
| 369 | free(linkname); |
| 370 | return err; |
| 371 | } |
| 372 | |
| 373 | static int dso__cache_build_id(struct dso *dso, const char *debugdir) |
| 374 | { |
| 375 | bool is_kallsyms = dso->kernel && dso->long_name[0] != '/'; |
| 376 | |
| 377 | return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), |
| 378 | dso->long_name, debugdir, is_kallsyms); |
| 379 | } |
| 380 | |
| 381 | static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir) |
| 382 | { |
| 383 | struct dso *pos; |
| 384 | int err = 0; |
| 385 | |
| 386 | dsos__for_each_with_build_id(pos, head) |
| 387 | if (dso__cache_build_id(pos, debugdir)) |
| 388 | err = -1; |
| 389 | |
| 390 | return err; |
| 391 | } |
| 392 | |
| 393 | static int machine__cache_build_ids(struct machine *machine, const char *debugdir) |
| 394 | { |
| 395 | int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir); |
| 396 | ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir); |
| 397 | return ret; |
| 398 | } |
| 399 | |
| 400 | static int perf_session__cache_build_ids(struct perf_session *session) |
| 401 | { |
| 402 | struct rb_node *nd; |
| 403 | int ret; |
| 404 | char debugdir[PATH_MAX]; |
| 405 | |
| 406 | snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir); |
| 407 | |
| 408 | if (mkdir(debugdir, 0755) != 0 && errno != EEXIST) |
| 409 | return -1; |
| 410 | |
| 411 | ret = machine__cache_build_ids(&session->host_machine, debugdir); |
| 412 | |
| 413 | for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { |
| 414 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 415 | ret |= machine__cache_build_ids(pos, debugdir); |
| 416 | } |
| 417 | return ret ? -1 : 0; |
| 418 | } |
| 419 | |
| 420 | static bool machine__read_build_ids(struct machine *machine, bool with_hits) |
| 421 | { |
| 422 | bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits); |
| 423 | ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits); |
| 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits) |
| 428 | { |
| 429 | struct rb_node *nd; |
| 430 | bool ret = machine__read_build_ids(&session->host_machine, with_hits); |
| 431 | |
| 432 | for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { |
| 433 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 434 | ret |= machine__read_build_ids(pos, with_hits); |
| 435 | } |
| 436 | |
| 437 | return ret; |
| 438 | } |
| 439 | |
Stephane Eranian | 2eeaaa0 | 2012-05-15 13:28:13 +0200 | [diff] [blame] | 440 | static int write_tracing_data(int fd, struct perf_header *h __used, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 441 | struct perf_evlist *evlist) |
| 442 | { |
| 443 | return read_tracing_data(fd, &evlist->entries); |
| 444 | } |
| 445 | |
| 446 | |
| 447 | static int write_build_id(int fd, struct perf_header *h, |
| 448 | struct perf_evlist *evlist __used) |
| 449 | { |
| 450 | struct perf_session *session; |
| 451 | int err; |
| 452 | |
| 453 | session = container_of(h, struct perf_session, header); |
| 454 | |
Robert Richter | e20960c | 2011-12-07 10:02:55 +0100 | [diff] [blame] | 455 | if (!perf_session__read_build_ids(session, true)) |
| 456 | return -1; |
| 457 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 458 | err = dsos__write_buildid_table(h, fd); |
| 459 | if (err < 0) { |
| 460 | pr_debug("failed to write buildid table\n"); |
| 461 | return err; |
| 462 | } |
| 463 | if (!no_buildid_cache) |
| 464 | perf_session__cache_build_ids(session); |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int write_hostname(int fd, struct perf_header *h __used, |
| 470 | struct perf_evlist *evlist __used) |
| 471 | { |
| 472 | struct utsname uts; |
| 473 | int ret; |
| 474 | |
| 475 | ret = uname(&uts); |
| 476 | if (ret < 0) |
| 477 | return -1; |
| 478 | |
| 479 | return do_write_string(fd, uts.nodename); |
| 480 | } |
| 481 | |
| 482 | static int write_osrelease(int fd, struct perf_header *h __used, |
| 483 | struct perf_evlist *evlist __used) |
| 484 | { |
| 485 | struct utsname uts; |
| 486 | int ret; |
| 487 | |
| 488 | ret = uname(&uts); |
| 489 | if (ret < 0) |
| 490 | return -1; |
| 491 | |
| 492 | return do_write_string(fd, uts.release); |
| 493 | } |
| 494 | |
| 495 | static int write_arch(int fd, struct perf_header *h __used, |
| 496 | struct perf_evlist *evlist __used) |
| 497 | { |
| 498 | struct utsname uts; |
| 499 | int ret; |
| 500 | |
| 501 | ret = uname(&uts); |
| 502 | if (ret < 0) |
| 503 | return -1; |
| 504 | |
| 505 | return do_write_string(fd, uts.machine); |
| 506 | } |
| 507 | |
| 508 | static int write_version(int fd, struct perf_header *h __used, |
| 509 | struct perf_evlist *evlist __used) |
| 510 | { |
| 511 | return do_write_string(fd, perf_version_string); |
| 512 | } |
| 513 | |
| 514 | static int write_cpudesc(int fd, struct perf_header *h __used, |
| 515 | struct perf_evlist *evlist __used) |
| 516 | { |
| 517 | #ifndef CPUINFO_PROC |
| 518 | #define CPUINFO_PROC NULL |
| 519 | #endif |
| 520 | FILE *file; |
| 521 | char *buf = NULL; |
| 522 | char *s, *p; |
| 523 | const char *search = CPUINFO_PROC; |
| 524 | size_t len = 0; |
| 525 | int ret = -1; |
| 526 | |
| 527 | if (!search) |
| 528 | return -1; |
| 529 | |
| 530 | file = fopen("/proc/cpuinfo", "r"); |
| 531 | if (!file) |
| 532 | return -1; |
| 533 | |
| 534 | while (getline(&buf, &len, file) > 0) { |
| 535 | ret = strncmp(buf, search, strlen(search)); |
| 536 | if (!ret) |
| 537 | break; |
| 538 | } |
| 539 | |
| 540 | if (ret) |
| 541 | goto done; |
| 542 | |
| 543 | s = buf; |
| 544 | |
| 545 | p = strchr(buf, ':'); |
| 546 | if (p && *(p+1) == ' ' && *(p+2)) |
| 547 | s = p + 2; |
| 548 | p = strchr(s, '\n'); |
| 549 | if (p) |
| 550 | *p = '\0'; |
| 551 | |
| 552 | /* squash extra space characters (branding string) */ |
| 553 | p = s; |
| 554 | while (*p) { |
| 555 | if (isspace(*p)) { |
| 556 | char *r = p + 1; |
| 557 | char *q = r; |
| 558 | *p = ' '; |
| 559 | while (*q && isspace(*q)) |
| 560 | q++; |
| 561 | if (q != (p+1)) |
| 562 | while ((*r++ = *q++)); |
| 563 | } |
| 564 | p++; |
| 565 | } |
| 566 | ret = do_write_string(fd, s); |
| 567 | done: |
| 568 | free(buf); |
| 569 | fclose(file); |
| 570 | return ret; |
| 571 | } |
| 572 | |
| 573 | static int write_nrcpus(int fd, struct perf_header *h __used, |
| 574 | struct perf_evlist *evlist __used) |
| 575 | { |
| 576 | long nr; |
| 577 | u32 nrc, nra; |
| 578 | int ret; |
| 579 | |
| 580 | nr = sysconf(_SC_NPROCESSORS_CONF); |
| 581 | if (nr < 0) |
| 582 | return -1; |
| 583 | |
| 584 | nrc = (u32)(nr & UINT_MAX); |
| 585 | |
| 586 | nr = sysconf(_SC_NPROCESSORS_ONLN); |
| 587 | if (nr < 0) |
| 588 | return -1; |
| 589 | |
| 590 | nra = (u32)(nr & UINT_MAX); |
| 591 | |
| 592 | ret = do_write(fd, &nrc, sizeof(nrc)); |
| 593 | if (ret < 0) |
| 594 | return ret; |
| 595 | |
| 596 | return do_write(fd, &nra, sizeof(nra)); |
| 597 | } |
| 598 | |
| 599 | static int write_event_desc(int fd, struct perf_header *h __used, |
| 600 | struct perf_evlist *evlist) |
| 601 | { |
| 602 | struct perf_evsel *attr; |
| 603 | u32 nre = 0, nri, sz; |
| 604 | int ret; |
| 605 | |
| 606 | list_for_each_entry(attr, &evlist->entries, node) |
| 607 | nre++; |
| 608 | |
| 609 | /* |
| 610 | * write number of events |
| 611 | */ |
| 612 | ret = do_write(fd, &nre, sizeof(nre)); |
| 613 | if (ret < 0) |
| 614 | return ret; |
| 615 | |
| 616 | /* |
| 617 | * size of perf_event_attr struct |
| 618 | */ |
| 619 | sz = (u32)sizeof(attr->attr); |
| 620 | ret = do_write(fd, &sz, sizeof(sz)); |
| 621 | if (ret < 0) |
| 622 | return ret; |
| 623 | |
| 624 | list_for_each_entry(attr, &evlist->entries, node) { |
| 625 | |
| 626 | ret = do_write(fd, &attr->attr, sz); |
| 627 | if (ret < 0) |
| 628 | return ret; |
| 629 | /* |
| 630 | * write number of unique id per event |
| 631 | * there is one id per instance of an event |
| 632 | * |
| 633 | * copy into an nri to be independent of the |
| 634 | * type of ids, |
| 635 | */ |
| 636 | nri = attr->ids; |
| 637 | ret = do_write(fd, &nri, sizeof(nri)); |
| 638 | if (ret < 0) |
| 639 | return ret; |
| 640 | |
| 641 | /* |
| 642 | * write event string as passed on cmdline |
| 643 | */ |
Andrew Vagin | 6340cfe | 2011-11-28 12:03:29 +0300 | [diff] [blame] | 644 | ret = do_write_string(fd, event_name(attr)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 645 | if (ret < 0) |
| 646 | return ret; |
| 647 | /* |
| 648 | * write unique ids for this event |
| 649 | */ |
| 650 | ret = do_write(fd, attr->id, attr->ids * sizeof(u64)); |
| 651 | if (ret < 0) |
| 652 | return ret; |
| 653 | } |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | static int write_cmdline(int fd, struct perf_header *h __used, |
| 658 | struct perf_evlist *evlist __used) |
| 659 | { |
| 660 | char buf[MAXPATHLEN]; |
| 661 | char proc[32]; |
| 662 | u32 i, n; |
| 663 | int ret; |
| 664 | |
| 665 | /* |
| 666 | * actual atual path to perf binary |
| 667 | */ |
| 668 | sprintf(proc, "/proc/%d/exe", getpid()); |
| 669 | ret = readlink(proc, buf, sizeof(buf)); |
| 670 | if (ret <= 0) |
| 671 | return -1; |
| 672 | |
| 673 | /* readlink() does not add null termination */ |
| 674 | buf[ret] = '\0'; |
| 675 | |
| 676 | /* account for binary path */ |
| 677 | n = header_argc + 1; |
| 678 | |
| 679 | ret = do_write(fd, &n, sizeof(n)); |
| 680 | if (ret < 0) |
| 681 | return ret; |
| 682 | |
| 683 | ret = do_write_string(fd, buf); |
| 684 | if (ret < 0) |
| 685 | return ret; |
| 686 | |
| 687 | for (i = 0 ; i < header_argc; i++) { |
| 688 | ret = do_write_string(fd, header_argv[i]); |
| 689 | if (ret < 0) |
| 690 | return ret; |
| 691 | } |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | #define CORE_SIB_FMT \ |
| 696 | "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list" |
| 697 | #define THRD_SIB_FMT \ |
| 698 | "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list" |
| 699 | |
| 700 | struct cpu_topo { |
| 701 | u32 core_sib; |
| 702 | u32 thread_sib; |
| 703 | char **core_siblings; |
| 704 | char **thread_siblings; |
| 705 | }; |
| 706 | |
| 707 | static int build_cpu_topo(struct cpu_topo *tp, int cpu) |
| 708 | { |
| 709 | FILE *fp; |
| 710 | char filename[MAXPATHLEN]; |
| 711 | char *buf = NULL, *p; |
| 712 | size_t len = 0; |
| 713 | u32 i = 0; |
| 714 | int ret = -1; |
| 715 | |
| 716 | sprintf(filename, CORE_SIB_FMT, cpu); |
| 717 | fp = fopen(filename, "r"); |
| 718 | if (!fp) |
| 719 | return -1; |
| 720 | |
| 721 | if (getline(&buf, &len, fp) <= 0) |
| 722 | goto done; |
| 723 | |
| 724 | fclose(fp); |
| 725 | |
| 726 | p = strchr(buf, '\n'); |
| 727 | if (p) |
| 728 | *p = '\0'; |
| 729 | |
| 730 | for (i = 0; i < tp->core_sib; i++) { |
| 731 | if (!strcmp(buf, tp->core_siblings[i])) |
| 732 | break; |
| 733 | } |
| 734 | if (i == tp->core_sib) { |
| 735 | tp->core_siblings[i] = buf; |
| 736 | tp->core_sib++; |
| 737 | buf = NULL; |
| 738 | len = 0; |
| 739 | } |
| 740 | |
| 741 | sprintf(filename, THRD_SIB_FMT, cpu); |
| 742 | fp = fopen(filename, "r"); |
| 743 | if (!fp) |
| 744 | goto done; |
| 745 | |
| 746 | if (getline(&buf, &len, fp) <= 0) |
| 747 | goto done; |
| 748 | |
| 749 | p = strchr(buf, '\n'); |
| 750 | if (p) |
| 751 | *p = '\0'; |
| 752 | |
| 753 | for (i = 0; i < tp->thread_sib; i++) { |
| 754 | if (!strcmp(buf, tp->thread_siblings[i])) |
| 755 | break; |
| 756 | } |
| 757 | if (i == tp->thread_sib) { |
| 758 | tp->thread_siblings[i] = buf; |
| 759 | tp->thread_sib++; |
| 760 | buf = NULL; |
| 761 | } |
| 762 | ret = 0; |
| 763 | done: |
| 764 | if(fp) |
| 765 | fclose(fp); |
| 766 | free(buf); |
| 767 | return ret; |
| 768 | } |
| 769 | |
| 770 | static void free_cpu_topo(struct cpu_topo *tp) |
| 771 | { |
| 772 | u32 i; |
| 773 | |
| 774 | if (!tp) |
| 775 | return; |
| 776 | |
| 777 | for (i = 0 ; i < tp->core_sib; i++) |
| 778 | free(tp->core_siblings[i]); |
| 779 | |
| 780 | for (i = 0 ; i < tp->thread_sib; i++) |
| 781 | free(tp->thread_siblings[i]); |
| 782 | |
| 783 | free(tp); |
| 784 | } |
| 785 | |
| 786 | static struct cpu_topo *build_cpu_topology(void) |
| 787 | { |
| 788 | struct cpu_topo *tp; |
| 789 | void *addr; |
| 790 | u32 nr, i; |
| 791 | size_t sz; |
| 792 | long ncpus; |
| 793 | int ret = -1; |
| 794 | |
| 795 | ncpus = sysconf(_SC_NPROCESSORS_CONF); |
| 796 | if (ncpus < 0) |
| 797 | return NULL; |
| 798 | |
| 799 | nr = (u32)(ncpus & UINT_MAX); |
| 800 | |
| 801 | sz = nr * sizeof(char *); |
| 802 | |
| 803 | addr = calloc(1, sizeof(*tp) + 2 * sz); |
| 804 | if (!addr) |
| 805 | return NULL; |
| 806 | |
| 807 | tp = addr; |
| 808 | |
| 809 | addr += sizeof(*tp); |
| 810 | tp->core_siblings = addr; |
| 811 | addr += sz; |
| 812 | tp->thread_siblings = addr; |
| 813 | |
| 814 | for (i = 0; i < nr; i++) { |
| 815 | ret = build_cpu_topo(tp, i); |
| 816 | if (ret < 0) |
| 817 | break; |
| 818 | } |
| 819 | if (ret) { |
| 820 | free_cpu_topo(tp); |
| 821 | tp = NULL; |
| 822 | } |
| 823 | return tp; |
| 824 | } |
| 825 | |
| 826 | static int write_cpu_topology(int fd, struct perf_header *h __used, |
| 827 | struct perf_evlist *evlist __used) |
| 828 | { |
| 829 | struct cpu_topo *tp; |
| 830 | u32 i; |
| 831 | int ret; |
| 832 | |
| 833 | tp = build_cpu_topology(); |
| 834 | if (!tp) |
| 835 | return -1; |
| 836 | |
| 837 | ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib)); |
| 838 | if (ret < 0) |
| 839 | goto done; |
| 840 | |
| 841 | for (i = 0; i < tp->core_sib; i++) { |
| 842 | ret = do_write_string(fd, tp->core_siblings[i]); |
| 843 | if (ret < 0) |
| 844 | goto done; |
| 845 | } |
| 846 | ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib)); |
| 847 | if (ret < 0) |
| 848 | goto done; |
| 849 | |
| 850 | for (i = 0; i < tp->thread_sib; i++) { |
| 851 | ret = do_write_string(fd, tp->thread_siblings[i]); |
| 852 | if (ret < 0) |
| 853 | break; |
| 854 | } |
| 855 | done: |
| 856 | free_cpu_topo(tp); |
| 857 | return ret; |
| 858 | } |
| 859 | |
| 860 | |
| 861 | |
| 862 | static int write_total_mem(int fd, struct perf_header *h __used, |
| 863 | struct perf_evlist *evlist __used) |
| 864 | { |
| 865 | char *buf = NULL; |
| 866 | FILE *fp; |
| 867 | size_t len = 0; |
| 868 | int ret = -1, n; |
| 869 | uint64_t mem; |
| 870 | |
| 871 | fp = fopen("/proc/meminfo", "r"); |
| 872 | if (!fp) |
| 873 | return -1; |
| 874 | |
| 875 | while (getline(&buf, &len, fp) > 0) { |
| 876 | ret = strncmp(buf, "MemTotal:", 9); |
| 877 | if (!ret) |
| 878 | break; |
| 879 | } |
| 880 | if (!ret) { |
| 881 | n = sscanf(buf, "%*s %"PRIu64, &mem); |
| 882 | if (n == 1) |
| 883 | ret = do_write(fd, &mem, sizeof(mem)); |
| 884 | } |
| 885 | free(buf); |
| 886 | fclose(fp); |
| 887 | return ret; |
| 888 | } |
| 889 | |
| 890 | static int write_topo_node(int fd, int node) |
| 891 | { |
| 892 | char str[MAXPATHLEN]; |
| 893 | char field[32]; |
| 894 | char *buf = NULL, *p; |
| 895 | size_t len = 0; |
| 896 | FILE *fp; |
| 897 | u64 mem_total, mem_free, mem; |
| 898 | int ret = -1; |
| 899 | |
| 900 | sprintf(str, "/sys/devices/system/node/node%d/meminfo", node); |
| 901 | fp = fopen(str, "r"); |
| 902 | if (!fp) |
| 903 | return -1; |
| 904 | |
| 905 | while (getline(&buf, &len, fp) > 0) { |
| 906 | /* skip over invalid lines */ |
| 907 | if (!strchr(buf, ':')) |
| 908 | continue; |
| 909 | if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2) |
| 910 | goto done; |
| 911 | if (!strcmp(field, "MemTotal:")) |
| 912 | mem_total = mem; |
| 913 | if (!strcmp(field, "MemFree:")) |
| 914 | mem_free = mem; |
| 915 | } |
| 916 | |
| 917 | fclose(fp); |
| 918 | |
| 919 | ret = do_write(fd, &mem_total, sizeof(u64)); |
| 920 | if (ret) |
| 921 | goto done; |
| 922 | |
| 923 | ret = do_write(fd, &mem_free, sizeof(u64)); |
| 924 | if (ret) |
| 925 | goto done; |
| 926 | |
| 927 | ret = -1; |
| 928 | sprintf(str, "/sys/devices/system/node/node%d/cpulist", node); |
| 929 | |
| 930 | fp = fopen(str, "r"); |
| 931 | if (!fp) |
| 932 | goto done; |
| 933 | |
| 934 | if (getline(&buf, &len, fp) <= 0) |
| 935 | goto done; |
| 936 | |
| 937 | p = strchr(buf, '\n'); |
| 938 | if (p) |
| 939 | *p = '\0'; |
| 940 | |
| 941 | ret = do_write_string(fd, buf); |
| 942 | done: |
| 943 | free(buf); |
| 944 | fclose(fp); |
| 945 | return ret; |
| 946 | } |
| 947 | |
| 948 | static int write_numa_topology(int fd, struct perf_header *h __used, |
| 949 | struct perf_evlist *evlist __used) |
| 950 | { |
| 951 | char *buf = NULL; |
| 952 | size_t len = 0; |
| 953 | FILE *fp; |
| 954 | struct cpu_map *node_map = NULL; |
| 955 | char *c; |
| 956 | u32 nr, i, j; |
| 957 | int ret = -1; |
| 958 | |
| 959 | fp = fopen("/sys/devices/system/node/online", "r"); |
| 960 | if (!fp) |
| 961 | return -1; |
| 962 | |
| 963 | if (getline(&buf, &len, fp) <= 0) |
| 964 | goto done; |
| 965 | |
| 966 | c = strchr(buf, '\n'); |
| 967 | if (c) |
| 968 | *c = '\0'; |
| 969 | |
| 970 | node_map = cpu_map__new(buf); |
| 971 | if (!node_map) |
| 972 | goto done; |
| 973 | |
| 974 | nr = (u32)node_map->nr; |
| 975 | |
| 976 | ret = do_write(fd, &nr, sizeof(nr)); |
| 977 | if (ret < 0) |
| 978 | goto done; |
| 979 | |
| 980 | for (i = 0; i < nr; i++) { |
| 981 | j = (u32)node_map->map[i]; |
| 982 | ret = do_write(fd, &j, sizeof(j)); |
| 983 | if (ret < 0) |
| 984 | break; |
| 985 | |
| 986 | ret = write_topo_node(fd, i); |
| 987 | if (ret < 0) |
| 988 | break; |
| 989 | } |
| 990 | done: |
| 991 | free(buf); |
| 992 | fclose(fp); |
| 993 | free(node_map); |
| 994 | return ret; |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * default get_cpuid(): nothing gets recorded |
| 999 | * actual implementation must be in arch/$(ARCH)/util/header.c |
| 1000 | */ |
| 1001 | int __attribute__((weak)) get_cpuid(char *buffer __used, size_t sz __used) |
| 1002 | { |
| 1003 | return -1; |
| 1004 | } |
| 1005 | |
| 1006 | static int write_cpuid(int fd, struct perf_header *h __used, |
| 1007 | struct perf_evlist *evlist __used) |
| 1008 | { |
| 1009 | char buffer[64]; |
| 1010 | int ret; |
| 1011 | |
| 1012 | ret = get_cpuid(buffer, sizeof(buffer)); |
| 1013 | if (!ret) |
| 1014 | goto write_it; |
| 1015 | |
| 1016 | return -1; |
| 1017 | write_it: |
| 1018 | return do_write_string(fd, buffer); |
| 1019 | } |
| 1020 | |
Stephane Eranian | 330aa67 | 2012-03-08 23:47:46 +0100 | [diff] [blame] | 1021 | static int write_branch_stack(int fd __used, struct perf_header *h __used, |
| 1022 | struct perf_evlist *evlist __used) |
| 1023 | { |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1027 | static void print_hostname(struct perf_header *ph, int fd, FILE *fp) |
| 1028 | { |
| 1029 | char *str = do_read_string(fd, ph); |
| 1030 | fprintf(fp, "# hostname : %s\n", str); |
| 1031 | free(str); |
| 1032 | } |
| 1033 | |
| 1034 | static void print_osrelease(struct perf_header *ph, int fd, FILE *fp) |
| 1035 | { |
| 1036 | char *str = do_read_string(fd, ph); |
| 1037 | fprintf(fp, "# os release : %s\n", str); |
| 1038 | free(str); |
| 1039 | } |
| 1040 | |
| 1041 | static void print_arch(struct perf_header *ph, int fd, FILE *fp) |
| 1042 | { |
| 1043 | char *str = do_read_string(fd, ph); |
| 1044 | fprintf(fp, "# arch : %s\n", str); |
| 1045 | free(str); |
| 1046 | } |
| 1047 | |
| 1048 | static void print_cpudesc(struct perf_header *ph, int fd, FILE *fp) |
| 1049 | { |
| 1050 | char *str = do_read_string(fd, ph); |
| 1051 | fprintf(fp, "# cpudesc : %s\n", str); |
| 1052 | free(str); |
| 1053 | } |
| 1054 | |
| 1055 | static void print_nrcpus(struct perf_header *ph, int fd, FILE *fp) |
| 1056 | { |
| 1057 | ssize_t ret; |
| 1058 | u32 nr; |
| 1059 | |
| 1060 | ret = read(fd, &nr, sizeof(nr)); |
| 1061 | if (ret != (ssize_t)sizeof(nr)) |
| 1062 | nr = -1; /* interpreted as error */ |
| 1063 | |
| 1064 | if (ph->needs_swap) |
| 1065 | nr = bswap_32(nr); |
| 1066 | |
| 1067 | fprintf(fp, "# nrcpus online : %u\n", nr); |
| 1068 | |
| 1069 | ret = read(fd, &nr, sizeof(nr)); |
| 1070 | if (ret != (ssize_t)sizeof(nr)) |
| 1071 | nr = -1; /* interpreted as error */ |
| 1072 | |
| 1073 | if (ph->needs_swap) |
| 1074 | nr = bswap_32(nr); |
| 1075 | |
| 1076 | fprintf(fp, "# nrcpus avail : %u\n", nr); |
| 1077 | } |
| 1078 | |
| 1079 | static void print_version(struct perf_header *ph, int fd, FILE *fp) |
| 1080 | { |
| 1081 | char *str = do_read_string(fd, ph); |
| 1082 | fprintf(fp, "# perf version : %s\n", str); |
| 1083 | free(str); |
| 1084 | } |
| 1085 | |
| 1086 | static void print_cmdline(struct perf_header *ph, int fd, FILE *fp) |
| 1087 | { |
| 1088 | ssize_t ret; |
| 1089 | char *str; |
| 1090 | u32 nr, i; |
| 1091 | |
| 1092 | ret = read(fd, &nr, sizeof(nr)); |
| 1093 | if (ret != (ssize_t)sizeof(nr)) |
| 1094 | return; |
| 1095 | |
| 1096 | if (ph->needs_swap) |
| 1097 | nr = bswap_32(nr); |
| 1098 | |
| 1099 | fprintf(fp, "# cmdline : "); |
| 1100 | |
| 1101 | for (i = 0; i < nr; i++) { |
| 1102 | str = do_read_string(fd, ph); |
| 1103 | fprintf(fp, "%s ", str); |
| 1104 | free(str); |
| 1105 | } |
| 1106 | fputc('\n', fp); |
| 1107 | } |
| 1108 | |
| 1109 | static void print_cpu_topology(struct perf_header *ph, int fd, FILE *fp) |
| 1110 | { |
| 1111 | ssize_t ret; |
| 1112 | u32 nr, i; |
| 1113 | char *str; |
| 1114 | |
| 1115 | ret = read(fd, &nr, sizeof(nr)); |
| 1116 | if (ret != (ssize_t)sizeof(nr)) |
| 1117 | return; |
| 1118 | |
| 1119 | if (ph->needs_swap) |
| 1120 | nr = bswap_32(nr); |
| 1121 | |
| 1122 | for (i = 0; i < nr; i++) { |
| 1123 | str = do_read_string(fd, ph); |
| 1124 | fprintf(fp, "# sibling cores : %s\n", str); |
| 1125 | free(str); |
| 1126 | } |
| 1127 | |
| 1128 | ret = read(fd, &nr, sizeof(nr)); |
| 1129 | if (ret != (ssize_t)sizeof(nr)) |
| 1130 | return; |
| 1131 | |
| 1132 | if (ph->needs_swap) |
| 1133 | nr = bswap_32(nr); |
| 1134 | |
| 1135 | for (i = 0; i < nr; i++) { |
| 1136 | str = do_read_string(fd, ph); |
| 1137 | fprintf(fp, "# sibling threads : %s\n", str); |
| 1138 | free(str); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | static void print_event_desc(struct perf_header *ph, int fd, FILE *fp) |
| 1143 | { |
| 1144 | struct perf_event_attr attr; |
| 1145 | uint64_t id; |
| 1146 | void *buf = NULL; |
| 1147 | char *str; |
Stephane Eranian | 62db906 | 2012-02-09 23:21:07 +0100 | [diff] [blame] | 1148 | u32 nre, sz, nr, i, j; |
| 1149 | ssize_t ret; |
| 1150 | size_t msz; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1151 | |
| 1152 | /* number of events */ |
| 1153 | ret = read(fd, &nre, sizeof(nre)); |
| 1154 | if (ret != (ssize_t)sizeof(nre)) |
| 1155 | goto error; |
| 1156 | |
| 1157 | if (ph->needs_swap) |
| 1158 | nre = bswap_32(nre); |
| 1159 | |
| 1160 | ret = read(fd, &sz, sizeof(sz)); |
| 1161 | if (ret != (ssize_t)sizeof(sz)) |
| 1162 | goto error; |
| 1163 | |
| 1164 | if (ph->needs_swap) |
| 1165 | sz = bswap_32(sz); |
| 1166 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1167 | memset(&attr, 0, sizeof(attr)); |
| 1168 | |
Stephane Eranian | 62db906 | 2012-02-09 23:21:07 +0100 | [diff] [blame] | 1169 | /* buffer to hold on file attr struct */ |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1170 | buf = malloc(sz); |
| 1171 | if (!buf) |
| 1172 | goto error; |
| 1173 | |
| 1174 | msz = sizeof(attr); |
Jiri Olsa | 9fafd98 | 2012-03-20 19:15:39 +0100 | [diff] [blame] | 1175 | if (sz < msz) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1176 | msz = sz; |
| 1177 | |
| 1178 | for (i = 0 ; i < nre; i++) { |
| 1179 | |
Stephane Eranian | 62db906 | 2012-02-09 23:21:07 +0100 | [diff] [blame] | 1180 | /* |
| 1181 | * must read entire on-file attr struct to |
| 1182 | * sync up with layout. |
| 1183 | */ |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1184 | ret = read(fd, buf, sz); |
| 1185 | if (ret != (ssize_t)sz) |
| 1186 | goto error; |
| 1187 | |
| 1188 | if (ph->needs_swap) |
| 1189 | perf_event__attr_swap(buf); |
| 1190 | |
| 1191 | memcpy(&attr, buf, msz); |
| 1192 | |
| 1193 | ret = read(fd, &nr, sizeof(nr)); |
| 1194 | if (ret != (ssize_t)sizeof(nr)) |
| 1195 | goto error; |
| 1196 | |
| 1197 | if (ph->needs_swap) |
| 1198 | nr = bswap_32(nr); |
| 1199 | |
| 1200 | str = do_read_string(fd, ph); |
| 1201 | fprintf(fp, "# event : name = %s, ", str); |
| 1202 | free(str); |
| 1203 | |
| 1204 | fprintf(fp, "type = %d, config = 0x%"PRIx64 |
| 1205 | ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64, |
| 1206 | attr.type, |
| 1207 | (u64)attr.config, |
| 1208 | (u64)attr.config1, |
| 1209 | (u64)attr.config2); |
| 1210 | |
| 1211 | fprintf(fp, ", excl_usr = %d, excl_kern = %d", |
| 1212 | attr.exclude_user, |
| 1213 | attr.exclude_kernel); |
| 1214 | |
| 1215 | if (nr) |
| 1216 | fprintf(fp, ", id = {"); |
| 1217 | |
| 1218 | for (j = 0 ; j < nr; j++) { |
| 1219 | ret = read(fd, &id, sizeof(id)); |
| 1220 | if (ret != (ssize_t)sizeof(id)) |
| 1221 | goto error; |
| 1222 | |
| 1223 | if (ph->needs_swap) |
| 1224 | id = bswap_64(id); |
| 1225 | |
| 1226 | if (j) |
| 1227 | fputc(',', fp); |
| 1228 | |
| 1229 | fprintf(fp, " %"PRIu64, id); |
| 1230 | } |
| 1231 | if (nr && j == nr) |
| 1232 | fprintf(fp, " }"); |
| 1233 | fputc('\n', fp); |
| 1234 | } |
| 1235 | free(buf); |
| 1236 | return; |
| 1237 | error: |
| 1238 | fprintf(fp, "# event desc: not available or unable to read\n"); |
| 1239 | } |
| 1240 | |
| 1241 | static void print_total_mem(struct perf_header *h __used, int fd, FILE *fp) |
| 1242 | { |
| 1243 | uint64_t mem; |
| 1244 | ssize_t ret; |
| 1245 | |
| 1246 | ret = read(fd, &mem, sizeof(mem)); |
| 1247 | if (ret != sizeof(mem)) |
| 1248 | goto error; |
| 1249 | |
| 1250 | if (h->needs_swap) |
| 1251 | mem = bswap_64(mem); |
| 1252 | |
| 1253 | fprintf(fp, "# total memory : %"PRIu64" kB\n", mem); |
| 1254 | return; |
| 1255 | error: |
| 1256 | fprintf(fp, "# total memory : unknown\n"); |
| 1257 | } |
| 1258 | |
| 1259 | static void print_numa_topology(struct perf_header *h __used, int fd, FILE *fp) |
| 1260 | { |
| 1261 | ssize_t ret; |
| 1262 | u32 nr, c, i; |
| 1263 | char *str; |
| 1264 | uint64_t mem_total, mem_free; |
| 1265 | |
| 1266 | /* nr nodes */ |
| 1267 | ret = read(fd, &nr, sizeof(nr)); |
| 1268 | if (ret != (ssize_t)sizeof(nr)) |
| 1269 | goto error; |
| 1270 | |
| 1271 | if (h->needs_swap) |
| 1272 | nr = bswap_32(nr); |
| 1273 | |
| 1274 | for (i = 0; i < nr; i++) { |
| 1275 | |
| 1276 | /* node number */ |
| 1277 | ret = read(fd, &c, sizeof(c)); |
| 1278 | if (ret != (ssize_t)sizeof(c)) |
| 1279 | goto error; |
| 1280 | |
| 1281 | if (h->needs_swap) |
| 1282 | c = bswap_32(c); |
| 1283 | |
| 1284 | ret = read(fd, &mem_total, sizeof(u64)); |
| 1285 | if (ret != sizeof(u64)) |
| 1286 | goto error; |
| 1287 | |
| 1288 | ret = read(fd, &mem_free, sizeof(u64)); |
| 1289 | if (ret != sizeof(u64)) |
| 1290 | goto error; |
| 1291 | |
| 1292 | if (h->needs_swap) { |
| 1293 | mem_total = bswap_64(mem_total); |
| 1294 | mem_free = bswap_64(mem_free); |
| 1295 | } |
| 1296 | |
| 1297 | fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB," |
| 1298 | " free = %"PRIu64" kB\n", |
| 1299 | c, |
| 1300 | mem_total, |
| 1301 | mem_free); |
| 1302 | |
| 1303 | str = do_read_string(fd, h); |
| 1304 | fprintf(fp, "# node%u cpu list : %s\n", c, str); |
| 1305 | free(str); |
| 1306 | } |
| 1307 | return; |
| 1308 | error: |
| 1309 | fprintf(fp, "# numa topology : not available\n"); |
| 1310 | } |
| 1311 | |
| 1312 | static void print_cpuid(struct perf_header *ph, int fd, FILE *fp) |
| 1313 | { |
| 1314 | char *str = do_read_string(fd, ph); |
| 1315 | fprintf(fp, "# cpuid : %s\n", str); |
| 1316 | free(str); |
| 1317 | } |
| 1318 | |
Stephane Eranian | 330aa67 | 2012-03-08 23:47:46 +0100 | [diff] [blame] | 1319 | static void print_branch_stack(struct perf_header *ph __used, int fd __used, |
| 1320 | FILE *fp) |
| 1321 | { |
| 1322 | fprintf(fp, "# contains samples with branch stack\n"); |
| 1323 | } |
| 1324 | |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1325 | static int __event_process_build_id(struct build_id_event *bev, |
| 1326 | char *filename, |
| 1327 | struct perf_session *session) |
| 1328 | { |
| 1329 | int err = -1; |
| 1330 | struct list_head *head; |
| 1331 | struct machine *machine; |
| 1332 | u16 misc; |
| 1333 | struct dso *dso; |
| 1334 | enum dso_kernel_type dso_type; |
| 1335 | |
| 1336 | machine = perf_session__findnew_machine(session, bev->pid); |
| 1337 | if (!machine) |
| 1338 | goto out; |
| 1339 | |
| 1340 | misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 1341 | |
| 1342 | switch (misc) { |
| 1343 | case PERF_RECORD_MISC_KERNEL: |
| 1344 | dso_type = DSO_TYPE_KERNEL; |
| 1345 | head = &machine->kernel_dsos; |
| 1346 | break; |
| 1347 | case PERF_RECORD_MISC_GUEST_KERNEL: |
| 1348 | dso_type = DSO_TYPE_GUEST_KERNEL; |
| 1349 | head = &machine->kernel_dsos; |
| 1350 | break; |
| 1351 | case PERF_RECORD_MISC_USER: |
| 1352 | case PERF_RECORD_MISC_GUEST_USER: |
| 1353 | dso_type = DSO_TYPE_USER; |
| 1354 | head = &machine->user_dsos; |
| 1355 | break; |
| 1356 | default: |
| 1357 | goto out; |
| 1358 | } |
| 1359 | |
| 1360 | dso = __dsos__findnew(head, filename); |
| 1361 | if (dso != NULL) { |
| 1362 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 1363 | |
| 1364 | dso__set_build_id(dso, &bev->build_id); |
| 1365 | |
| 1366 | if (filename[0] == '[') |
| 1367 | dso->kernel = dso_type; |
| 1368 | |
| 1369 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), |
| 1370 | sbuild_id); |
| 1371 | pr_debug("build id event received for %s: %s\n", |
| 1372 | dso->long_name, sbuild_id); |
| 1373 | } |
| 1374 | |
| 1375 | err = 0; |
| 1376 | out: |
| 1377 | return err; |
| 1378 | } |
| 1379 | |
| 1380 | static int perf_header__read_build_ids_abi_quirk(struct perf_header *header, |
| 1381 | int input, u64 offset, u64 size) |
| 1382 | { |
| 1383 | struct perf_session *session = container_of(header, struct perf_session, header); |
| 1384 | struct { |
| 1385 | struct perf_event_header header; |
| 1386 | u8 build_id[ALIGN(BUILD_ID_SIZE, sizeof(u64))]; |
| 1387 | char filename[0]; |
| 1388 | } old_bev; |
| 1389 | struct build_id_event bev; |
| 1390 | char filename[PATH_MAX]; |
| 1391 | u64 limit = offset + size; |
| 1392 | |
| 1393 | while (offset < limit) { |
| 1394 | ssize_t len; |
| 1395 | |
| 1396 | if (read(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev)) |
| 1397 | return -1; |
| 1398 | |
| 1399 | if (header->needs_swap) |
| 1400 | perf_event_header__bswap(&old_bev.header); |
| 1401 | |
| 1402 | len = old_bev.header.size - sizeof(old_bev); |
| 1403 | if (read(input, filename, len) != len) |
| 1404 | return -1; |
| 1405 | |
| 1406 | bev.header = old_bev.header; |
| 1407 | |
| 1408 | /* |
| 1409 | * As the pid is the missing value, we need to fill |
| 1410 | * it properly. The header.misc value give us nice hint. |
| 1411 | */ |
| 1412 | bev.pid = HOST_KERNEL_ID; |
| 1413 | if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER || |
| 1414 | bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL) |
| 1415 | bev.pid = DEFAULT_GUEST_KERNEL_ID; |
| 1416 | |
| 1417 | memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id)); |
| 1418 | __event_process_build_id(&bev, filename, session); |
| 1419 | |
| 1420 | offset += bev.header.size; |
| 1421 | } |
| 1422 | |
| 1423 | return 0; |
| 1424 | } |
| 1425 | |
| 1426 | static int perf_header__read_build_ids(struct perf_header *header, |
| 1427 | int input, u64 offset, u64 size) |
| 1428 | { |
| 1429 | struct perf_session *session = container_of(header, struct perf_session, header); |
| 1430 | struct build_id_event bev; |
| 1431 | char filename[PATH_MAX]; |
| 1432 | u64 limit = offset + size, orig_offset = offset; |
| 1433 | int err = -1; |
| 1434 | |
| 1435 | while (offset < limit) { |
| 1436 | ssize_t len; |
| 1437 | |
| 1438 | if (read(input, &bev, sizeof(bev)) != sizeof(bev)) |
| 1439 | goto out; |
| 1440 | |
| 1441 | if (header->needs_swap) |
| 1442 | perf_event_header__bswap(&bev.header); |
| 1443 | |
| 1444 | len = bev.header.size - sizeof(bev); |
| 1445 | if (read(input, filename, len) != len) |
| 1446 | goto out; |
| 1447 | /* |
| 1448 | * The a1645ce1 changeset: |
| 1449 | * |
| 1450 | * "perf: 'perf kvm' tool for monitoring guest performance from host" |
| 1451 | * |
| 1452 | * Added a field to struct build_id_event that broke the file |
| 1453 | * format. |
| 1454 | * |
| 1455 | * Since the kernel build-id is the first entry, process the |
| 1456 | * table using the old format if the well known |
| 1457 | * '[kernel.kallsyms]' string for the kernel build-id has the |
| 1458 | * first 4 characters chopped off (where the pid_t sits). |
| 1459 | */ |
| 1460 | if (memcmp(filename, "nel.kallsyms]", 13) == 0) { |
| 1461 | if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1) |
| 1462 | return -1; |
| 1463 | return perf_header__read_build_ids_abi_quirk(header, input, offset, size); |
| 1464 | } |
| 1465 | |
| 1466 | __event_process_build_id(&bev, filename, session); |
| 1467 | |
| 1468 | offset += bev.header.size; |
| 1469 | } |
| 1470 | err = 0; |
| 1471 | out: |
| 1472 | return err; |
| 1473 | } |
| 1474 | |
Stephane Eranian | 2eeaaa0 | 2012-05-15 13:28:13 +0200 | [diff] [blame] | 1475 | static int process_tracing_data(struct perf_file_section *section __unused, |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1476 | struct perf_header *ph __unused, |
| 1477 | int feat __unused, int fd) |
| 1478 | { |
| 1479 | trace_report(fd, false); |
| 1480 | return 0; |
| 1481 | } |
| 1482 | |
| 1483 | static int process_build_id(struct perf_file_section *section, |
| 1484 | struct perf_header *ph, |
| 1485 | int feat __unused, int fd) |
| 1486 | { |
| 1487 | if (perf_header__read_build_ids(ph, fd, section->offset, section->size)) |
| 1488 | pr_debug("Failed to read buildids, continuing...\n"); |
| 1489 | return 0; |
| 1490 | } |
| 1491 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1492 | struct feature_ops { |
| 1493 | int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist); |
| 1494 | void (*print)(struct perf_header *h, int fd, FILE *fp); |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1495 | int (*process)(struct perf_file_section *section, |
| 1496 | struct perf_header *h, int feat, int fd); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1497 | const char *name; |
| 1498 | bool full_only; |
| 1499 | }; |
| 1500 | |
Robert Richter | 8cdfa78 | 2011-12-07 10:02:56 +0100 | [diff] [blame] | 1501 | #define FEAT_OPA(n, func) \ |
| 1502 | [n] = { .name = #n, .write = write_##func, .print = print_##func } |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1503 | #define FEAT_OPP(n, func) \ |
| 1504 | [n] = { .name = #n, .write = write_##func, .print = print_##func, \ |
| 1505 | .process = process_##func } |
Robert Richter | 8cdfa78 | 2011-12-07 10:02:56 +0100 | [diff] [blame] | 1506 | #define FEAT_OPF(n, func) \ |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1507 | [n] = { .name = #n, .write = write_##func, .print = print_##func, \ |
| 1508 | .full_only = true } |
Robert Richter | 8cdfa78 | 2011-12-07 10:02:56 +0100 | [diff] [blame] | 1509 | |
| 1510 | /* feature_ops not implemented: */ |
Stephane Eranian | 2eeaaa0 | 2012-05-15 13:28:13 +0200 | [diff] [blame] | 1511 | #define print_tracing_data NULL |
| 1512 | #define print_build_id NULL |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1513 | |
| 1514 | static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = { |
Stephane Eranian | 2eeaaa0 | 2012-05-15 13:28:13 +0200 | [diff] [blame] | 1515 | FEAT_OPP(HEADER_TRACING_DATA, tracing_data), |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1516 | FEAT_OPP(HEADER_BUILD_ID, build_id), |
Robert Richter | 8cdfa78 | 2011-12-07 10:02:56 +0100 | [diff] [blame] | 1517 | FEAT_OPA(HEADER_HOSTNAME, hostname), |
| 1518 | FEAT_OPA(HEADER_OSRELEASE, osrelease), |
| 1519 | FEAT_OPA(HEADER_VERSION, version), |
| 1520 | FEAT_OPA(HEADER_ARCH, arch), |
| 1521 | FEAT_OPA(HEADER_NRCPUS, nrcpus), |
| 1522 | FEAT_OPA(HEADER_CPUDESC, cpudesc), |
| 1523 | FEAT_OPA(HEADER_CPUID, cpuid), |
| 1524 | FEAT_OPA(HEADER_TOTAL_MEM, total_mem), |
| 1525 | FEAT_OPA(HEADER_EVENT_DESC, event_desc), |
| 1526 | FEAT_OPA(HEADER_CMDLINE, cmdline), |
| 1527 | FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology), |
| 1528 | FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology), |
Stephane Eranian | 330aa67 | 2012-03-08 23:47:46 +0100 | [diff] [blame] | 1529 | FEAT_OPA(HEADER_BRANCH_STACK, branch_stack), |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1530 | }; |
| 1531 | |
| 1532 | struct header_print_data { |
| 1533 | FILE *fp; |
| 1534 | bool full; /* extended list of headers */ |
| 1535 | }; |
| 1536 | |
| 1537 | static int perf_file_section__fprintf_info(struct perf_file_section *section, |
| 1538 | struct perf_header *ph, |
| 1539 | int feat, int fd, void *data) |
| 1540 | { |
| 1541 | struct header_print_data *hd = data; |
| 1542 | |
| 1543 | if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { |
| 1544 | pr_debug("Failed to lseek to %" PRIu64 " offset for feature " |
| 1545 | "%d, continuing...\n", section->offset, feat); |
| 1546 | return 0; |
| 1547 | } |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 1548 | if (feat >= HEADER_LAST_FEATURE) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1549 | pr_warning("unknown feature %d\n", feat); |
Robert Richter | f7a8a13 | 2011-12-07 10:02:51 +0100 | [diff] [blame] | 1550 | return 0; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1551 | } |
| 1552 | if (!feat_ops[feat].print) |
| 1553 | return 0; |
| 1554 | |
| 1555 | if (!feat_ops[feat].full_only || hd->full) |
| 1556 | feat_ops[feat].print(ph, fd, hd->fp); |
| 1557 | else |
| 1558 | fprintf(hd->fp, "# %s info available, use -I to display\n", |
| 1559 | feat_ops[feat].name); |
| 1560 | |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
| 1564 | int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full) |
| 1565 | { |
| 1566 | struct header_print_data hd; |
| 1567 | struct perf_header *header = &session->header; |
| 1568 | int fd = session->fd; |
| 1569 | hd.fp = fp; |
| 1570 | hd.full = full; |
| 1571 | |
| 1572 | perf_header__process_sections(header, fd, &hd, |
| 1573 | perf_file_section__fprintf_info); |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1577 | static int do_write_feat(int fd, struct perf_header *h, int type, |
| 1578 | struct perf_file_section **p, |
| 1579 | struct perf_evlist *evlist) |
| 1580 | { |
| 1581 | int err; |
| 1582 | int ret = 0; |
| 1583 | |
| 1584 | if (perf_header__has_feat(h, type)) { |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 1585 | if (!feat_ops[type].write) |
| 1586 | return -1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1587 | |
| 1588 | (*p)->offset = lseek(fd, 0, SEEK_CUR); |
| 1589 | |
| 1590 | err = feat_ops[type].write(fd, h, evlist); |
| 1591 | if (err < 0) { |
| 1592 | pr_debug("failed to write feature %d\n", type); |
| 1593 | |
| 1594 | /* undo anything written */ |
| 1595 | lseek(fd, (*p)->offset, SEEK_SET); |
| 1596 | |
| 1597 | return -1; |
| 1598 | } |
| 1599 | (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset; |
| 1600 | (*p)++; |
| 1601 | } |
| 1602 | return ret; |
| 1603 | } |
| 1604 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1605 | static int perf_header__adds_write(struct perf_header *header, |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 1606 | struct perf_evlist *evlist, int fd) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 1607 | { |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1608 | int nr_sections; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1609 | struct perf_file_section *feat_sec, *p; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1610 | int sec_size; |
| 1611 | u64 sec_start; |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 1612 | int feat; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1613 | int err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1614 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1615 | nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1616 | if (!nr_sections) |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1617 | return 0; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1618 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1619 | feat_sec = p = calloc(sizeof(*feat_sec), nr_sections); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1620 | if (feat_sec == NULL) |
| 1621 | return -ENOMEM; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1622 | |
| 1623 | sec_size = sizeof(*feat_sec) * nr_sections; |
| 1624 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1625 | sec_start = header->data_offset + header->data_size; |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 1626 | lseek(fd, sec_start + sec_size, SEEK_SET); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 1627 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 1628 | for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { |
| 1629 | if (do_write_feat(fd, header, feat, &p, evlist)) |
| 1630 | perf_header__clear_feat(header, feat); |
| 1631 | } |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1632 | |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 1633 | lseek(fd, sec_start, SEEK_SET); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1634 | /* |
| 1635 | * may write more than needed due to dropped feature, but |
| 1636 | * this is okay, reader will skip the mising entries |
| 1637 | */ |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1638 | err = do_write(fd, feat_sec, sec_size); |
| 1639 | if (err < 0) |
| 1640 | pr_debug("failed to write feature section\n"); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1641 | free(feat_sec); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1642 | return err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1643 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 1644 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1645 | int perf_header__write_pipe(int fd) |
| 1646 | { |
| 1647 | struct perf_pipe_file_header f_header; |
| 1648 | int err; |
| 1649 | |
| 1650 | f_header = (struct perf_pipe_file_header){ |
| 1651 | .magic = PERF_MAGIC, |
| 1652 | .size = sizeof(f_header), |
| 1653 | }; |
| 1654 | |
| 1655 | err = do_write(fd, &f_header, sizeof(f_header)); |
| 1656 | if (err < 0) { |
| 1657 | pr_debug("failed to write perf pipe header\n"); |
| 1658 | return err; |
| 1659 | } |
| 1660 | |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1664 | int perf_session__write_header(struct perf_session *session, |
| 1665 | struct perf_evlist *evlist, |
| 1666 | int fd, bool at_exit) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1667 | { |
| 1668 | struct perf_file_header f_header; |
| 1669 | struct perf_file_attr f_attr; |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1670 | struct perf_header *header = &session->header; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1671 | struct perf_evsel *attr, *pair = NULL; |
| 1672 | int err; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1673 | |
| 1674 | lseek(fd, sizeof(f_header), SEEK_SET); |
| 1675 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1676 | if (session->evlist != evlist) |
| 1677 | pair = list_entry(session->evlist->entries.next, struct perf_evsel, node); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1678 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1679 | list_for_each_entry(attr, &evlist->entries, node) { |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1680 | attr->id_offset = lseek(fd, 0, SEEK_CUR); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1681 | err = do_write(fd, attr->id, attr->ids * sizeof(u64)); |
| 1682 | if (err < 0) { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1683 | out_err_write: |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1684 | pr_debug("failed to write perf header\n"); |
| 1685 | return err; |
| 1686 | } |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1687 | if (session->evlist != evlist) { |
| 1688 | err = do_write(fd, pair->id, pair->ids * sizeof(u64)); |
| 1689 | if (err < 0) |
| 1690 | goto out_err_write; |
| 1691 | attr->ids += pair->ids; |
| 1692 | pair = list_entry(pair->node.next, struct perf_evsel, node); |
| 1693 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1694 | } |
| 1695 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1696 | header->attr_offset = lseek(fd, 0, SEEK_CUR); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1697 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1698 | list_for_each_entry(attr, &evlist->entries, node) { |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1699 | f_attr = (struct perf_file_attr){ |
| 1700 | .attr = attr->attr, |
| 1701 | .ids = { |
| 1702 | .offset = attr->id_offset, |
| 1703 | .size = attr->ids * sizeof(u64), |
| 1704 | } |
| 1705 | }; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1706 | err = do_write(fd, &f_attr, sizeof(f_attr)); |
| 1707 | if (err < 0) { |
| 1708 | pr_debug("failed to write perf header attribute\n"); |
| 1709 | return err; |
| 1710 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1711 | } |
| 1712 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1713 | header->event_offset = lseek(fd, 0, SEEK_CUR); |
| 1714 | header->event_size = event_count * sizeof(struct perf_trace_event_type); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1715 | if (events) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1716 | err = do_write(fd, events, header->event_size); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1717 | if (err < 0) { |
| 1718 | pr_debug("failed to write perf header events\n"); |
| 1719 | return err; |
| 1720 | } |
| 1721 | } |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 1722 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1723 | header->data_offset = lseek(fd, 0, SEEK_CUR); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1724 | |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1725 | if (at_exit) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1726 | err = perf_header__adds_write(header, evlist, fd); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1727 | if (err < 0) |
| 1728 | return err; |
| 1729 | } |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1730 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1731 | f_header = (struct perf_file_header){ |
| 1732 | .magic = PERF_MAGIC, |
| 1733 | .size = sizeof(f_header), |
| 1734 | .attr_size = sizeof(f_attr), |
| 1735 | .attrs = { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1736 | .offset = header->attr_offset, |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1737 | .size = evlist->nr_entries * sizeof(f_attr), |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1738 | }, |
| 1739 | .data = { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1740 | .offset = header->data_offset, |
| 1741 | .size = header->data_size, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1742 | }, |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 1743 | .event_types = { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1744 | .offset = header->event_offset, |
| 1745 | .size = header->event_size, |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 1746 | }, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1747 | }; |
| 1748 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1749 | memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features)); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 1750 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1751 | lseek(fd, 0, SEEK_SET); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1752 | err = do_write(fd, &f_header, sizeof(f_header)); |
| 1753 | if (err < 0) { |
| 1754 | pr_debug("failed to write perf header\n"); |
| 1755 | return err; |
| 1756 | } |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1757 | lseek(fd, header->data_offset + header->data_size, SEEK_SET); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1758 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1759 | header->frozen = 1; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1760 | return 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1761 | } |
| 1762 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1763 | static int perf_header__getbuffer64(struct perf_header *header, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1764 | int fd, void *buf, size_t size) |
| 1765 | { |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 1766 | if (readn(fd, buf, size) <= 0) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1767 | return -1; |
| 1768 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1769 | if (header->needs_swap) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1770 | mem_bswap_64(buf, size); |
| 1771 | |
| 1772 | return 0; |
| 1773 | } |
| 1774 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1775 | int perf_header__process_sections(struct perf_header *header, int fd, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1776 | void *data, |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1777 | int (*process)(struct perf_file_section *section, |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 1778 | struct perf_header *ph, |
| 1779 | int feat, int fd, void *data)) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 1780 | { |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 1781 | struct perf_file_section *feat_sec, *sec; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1782 | int nr_sections; |
| 1783 | int sec_size; |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 1784 | int feat; |
| 1785 | int err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1786 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1787 | nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1788 | if (!nr_sections) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1789 | return 0; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1790 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 1791 | feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1792 | if (!feat_sec) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1793 | return -1; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1794 | |
| 1795 | sec_size = sizeof(*feat_sec) * nr_sections; |
| 1796 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1797 | lseek(fd, header->data_offset + header->data_size, SEEK_SET); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1798 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 1799 | err = perf_header__getbuffer64(header, fd, feat_sec, sec_size); |
| 1800 | if (err < 0) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 1801 | goto out_free; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1802 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 1803 | for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) { |
| 1804 | err = process(sec++, header, feat, fd, data); |
| 1805 | if (err < 0) |
| 1806 | goto out_free; |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 1807 | } |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 1808 | err = 0; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 1809 | out_free: |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1810 | free(feat_sec); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1811 | return err; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 1812 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 1813 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 1814 | static const int attr_file_abi_sizes[] = { |
| 1815 | [0] = PERF_ATTR_SIZE_VER0, |
| 1816 | [1] = PERF_ATTR_SIZE_VER1, |
| 1817 | 0, |
| 1818 | }; |
| 1819 | |
| 1820 | /* |
| 1821 | * In the legacy file format, the magic number is not used to encode endianness. |
| 1822 | * hdr_sz was used to encode endianness. But given that hdr_sz can vary based |
| 1823 | * on ABI revisions, we need to try all combinations for all endianness to |
| 1824 | * detect the endianness. |
| 1825 | */ |
| 1826 | static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph) |
| 1827 | { |
| 1828 | uint64_t ref_size, attr_size; |
| 1829 | int i; |
| 1830 | |
| 1831 | for (i = 0 ; attr_file_abi_sizes[i]; i++) { |
| 1832 | ref_size = attr_file_abi_sizes[i] |
| 1833 | + sizeof(struct perf_file_section); |
| 1834 | if (hdr_sz != ref_size) { |
| 1835 | attr_size = bswap_64(hdr_sz); |
| 1836 | if (attr_size != ref_size) |
| 1837 | continue; |
| 1838 | |
| 1839 | ph->needs_swap = true; |
| 1840 | } |
| 1841 | pr_debug("ABI%d perf.data file detected, need_swap=%d\n", |
| 1842 | i, |
| 1843 | ph->needs_swap); |
| 1844 | return 0; |
| 1845 | } |
| 1846 | /* could not determine endianness */ |
| 1847 | return -1; |
| 1848 | } |
| 1849 | |
| 1850 | #define PERF_PIPE_HDR_VER0 16 |
| 1851 | |
| 1852 | static const size_t attr_pipe_abi_sizes[] = { |
| 1853 | [0] = PERF_PIPE_HDR_VER0, |
| 1854 | 0, |
| 1855 | }; |
| 1856 | |
| 1857 | /* |
| 1858 | * In the legacy pipe format, there is an implicit assumption that endiannesss |
| 1859 | * between host recording the samples, and host parsing the samples is the |
| 1860 | * same. This is not always the case given that the pipe output may always be |
| 1861 | * redirected into a file and analyzed on a different machine with possibly a |
| 1862 | * different endianness and perf_event ABI revsions in the perf tool itself. |
| 1863 | */ |
| 1864 | static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph) |
| 1865 | { |
| 1866 | u64 attr_size; |
| 1867 | int i; |
| 1868 | |
| 1869 | for (i = 0 ; attr_pipe_abi_sizes[i]; i++) { |
| 1870 | if (hdr_sz != attr_pipe_abi_sizes[i]) { |
| 1871 | attr_size = bswap_64(hdr_sz); |
| 1872 | if (attr_size != hdr_sz) |
| 1873 | continue; |
| 1874 | |
| 1875 | ph->needs_swap = true; |
| 1876 | } |
| 1877 | pr_debug("Pipe ABI%d perf.data file detected\n", i); |
| 1878 | return 0; |
| 1879 | } |
| 1880 | return -1; |
| 1881 | } |
| 1882 | |
| 1883 | static int check_magic_endian(u64 magic, uint64_t hdr_sz, |
| 1884 | bool is_pipe, struct perf_header *ph) |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 1885 | { |
| 1886 | int ret; |
| 1887 | |
| 1888 | /* check for legacy format */ |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 1889 | ret = memcmp(&magic, __perf_magic1, sizeof(magic)); |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 1890 | if (ret == 0) { |
| 1891 | pr_debug("legacy perf.data format\n"); |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 1892 | if (is_pipe) |
| 1893 | return try_all_pipe_abis(hdr_sz, ph); |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 1894 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 1895 | return try_all_file_abis(hdr_sz, ph); |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 1896 | } |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 1897 | /* |
| 1898 | * the new magic number serves two purposes: |
| 1899 | * - unique number to identify actual perf.data files |
| 1900 | * - encode endianness of file |
| 1901 | */ |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 1902 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 1903 | /* check magic number with one endianness */ |
| 1904 | if (magic == __perf_magic2) |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 1905 | return 0; |
| 1906 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 1907 | /* check magic number with opposite endianness */ |
| 1908 | if (magic != __perf_magic2_sw) |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 1909 | return -1; |
| 1910 | |
| 1911 | ph->needs_swap = true; |
| 1912 | |
| 1913 | return 0; |
| 1914 | } |
| 1915 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1916 | int perf_file_header__read(struct perf_file_header *header, |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1917 | struct perf_header *ph, int fd) |
| 1918 | { |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 1919 | int ret; |
| 1920 | |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1921 | lseek(fd, 0, SEEK_SET); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1922 | |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 1923 | ret = readn(fd, header, sizeof(*header)); |
| 1924 | if (ret <= 0) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1925 | return -1; |
| 1926 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 1927 | if (check_magic_endian(header->magic, |
| 1928 | header->attr_size, false, ph) < 0) { |
| 1929 | pr_debug("magic/endian check failed\n"); |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 1930 | return -1; |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 1931 | } |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1932 | |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 1933 | if (ph->needs_swap) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1934 | mem_bswap_64(header, offsetof(struct perf_file_header, |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 1935 | adds_features)); |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1936 | } |
| 1937 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1938 | if (header->size != sizeof(*header)) { |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1939 | /* Support the previous format */ |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1940 | if (header->size == offsetof(typeof(*header), adds_features)) |
| 1941 | bitmap_zero(header->adds_features, HEADER_FEAT_BITS); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1942 | else |
| 1943 | return -1; |
David Ahern | d327fa4 | 2011-10-18 17:34:01 -0600 | [diff] [blame] | 1944 | } else if (ph->needs_swap) { |
| 1945 | unsigned int i; |
| 1946 | /* |
| 1947 | * feature bitmap is declared as an array of unsigned longs -- |
| 1948 | * not good since its size can differ between the host that |
| 1949 | * generated the data file and the host analyzing the file. |
| 1950 | * |
| 1951 | * We need to handle endianness, but we don't know the size of |
| 1952 | * the unsigned long where the file was generated. Take a best |
| 1953 | * guess at determining it: try 64-bit swap first (ie., file |
| 1954 | * created on a 64-bit host), and check if the hostname feature |
| 1955 | * bit is set (this feature bit is forced on as of fbe96f2). |
| 1956 | * If the bit is not, undo the 64-bit swap and try a 32-bit |
| 1957 | * swap. If the hostname bit is still not set (e.g., older data |
| 1958 | * file), punt and fallback to the original behavior -- |
| 1959 | * clearing all feature bits and setting buildid. |
| 1960 | */ |
| 1961 | for (i = 0; i < BITS_TO_LONGS(HEADER_FEAT_BITS); ++i) |
| 1962 | header->adds_features[i] = bswap_64(header->adds_features[i]); |
| 1963 | |
| 1964 | if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { |
| 1965 | for (i = 0; i < BITS_TO_LONGS(HEADER_FEAT_BITS); ++i) { |
| 1966 | header->adds_features[i] = bswap_64(header->adds_features[i]); |
| 1967 | header->adds_features[i] = bswap_32(header->adds_features[i]); |
| 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { |
| 1972 | bitmap_zero(header->adds_features, HEADER_FEAT_BITS); |
| 1973 | set_bit(HEADER_BUILD_ID, header->adds_features); |
| 1974 | } |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1975 | } |
| 1976 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1977 | memcpy(&ph->adds_features, &header->adds_features, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1978 | sizeof(ph->adds_features)); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1979 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1980 | ph->event_offset = header->event_types.offset; |
| 1981 | ph->event_size = header->event_types.size; |
| 1982 | ph->data_offset = header->data.offset; |
| 1983 | ph->data_size = header->data.size; |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1984 | return 0; |
| 1985 | } |
| 1986 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1987 | static int perf_file_section__process(struct perf_file_section *section, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1988 | struct perf_header *ph, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1989 | int feat, int fd, void *data __used) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1990 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1991 | if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 1992 | pr_debug("Failed to lseek to %" PRIu64 " offset for feature " |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1993 | "%d, continuing...\n", section->offset, feat); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1994 | return 0; |
| 1995 | } |
| 1996 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 1997 | if (feat >= HEADER_LAST_FEATURE) { |
| 1998 | pr_debug("unknown feature %d, continuing...\n", feat); |
| 1999 | return 0; |
| 2000 | } |
| 2001 | |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 2002 | if (!feat_ops[feat].process) |
| 2003 | return 0; |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2004 | |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 2005 | return feat_ops[feat].process(section, ph, feat, fd); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2006 | } |
| 2007 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2008 | static int perf_file_header__read_pipe(struct perf_pipe_file_header *header, |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 2009 | struct perf_header *ph, int fd, |
| 2010 | bool repipe) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2011 | { |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2012 | int ret; |
| 2013 | |
| 2014 | ret = readn(fd, header, sizeof(*header)); |
| 2015 | if (ret <= 0) |
| 2016 | return -1; |
| 2017 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2018 | if (check_magic_endian(header->magic, header->size, true, ph) < 0) { |
| 2019 | pr_debug("endian/magic failed\n"); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2020 | return -1; |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | if (ph->needs_swap) |
| 2024 | header->size = bswap_64(header->size); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2025 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2026 | if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 2027 | return -1; |
| 2028 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2029 | return 0; |
| 2030 | } |
| 2031 | |
| 2032 | static int perf_header__read_pipe(struct perf_session *session, int fd) |
| 2033 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2034 | struct perf_header *header = &session->header; |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2035 | struct perf_pipe_file_header f_header; |
| 2036 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2037 | if (perf_file_header__read_pipe(&f_header, header, fd, |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 2038 | session->repipe) < 0) { |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2039 | pr_debug("incompatible file format\n"); |
| 2040 | return -EINVAL; |
| 2041 | } |
| 2042 | |
| 2043 | session->fd = fd; |
| 2044 | |
| 2045 | return 0; |
| 2046 | } |
| 2047 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 2048 | static int read_attr(int fd, struct perf_header *ph, |
| 2049 | struct perf_file_attr *f_attr) |
| 2050 | { |
| 2051 | struct perf_event_attr *attr = &f_attr->attr; |
| 2052 | size_t sz, left; |
| 2053 | size_t our_sz = sizeof(f_attr->attr); |
| 2054 | int ret; |
| 2055 | |
| 2056 | memset(f_attr, 0, sizeof(*f_attr)); |
| 2057 | |
| 2058 | /* read minimal guaranteed structure */ |
| 2059 | ret = readn(fd, attr, PERF_ATTR_SIZE_VER0); |
| 2060 | if (ret <= 0) { |
| 2061 | pr_debug("cannot read %d bytes of header attr\n", |
| 2062 | PERF_ATTR_SIZE_VER0); |
| 2063 | return -1; |
| 2064 | } |
| 2065 | |
| 2066 | /* on file perf_event_attr size */ |
| 2067 | sz = attr->size; |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2068 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 2069 | if (ph->needs_swap) |
| 2070 | sz = bswap_32(sz); |
| 2071 | |
| 2072 | if (sz == 0) { |
| 2073 | /* assume ABI0 */ |
| 2074 | sz = PERF_ATTR_SIZE_VER0; |
| 2075 | } else if (sz > our_sz) { |
| 2076 | pr_debug("file uses a more recent and unsupported ABI" |
| 2077 | " (%zu bytes extra)\n", sz - our_sz); |
| 2078 | return -1; |
| 2079 | } |
| 2080 | /* what we have not yet read and that we know about */ |
| 2081 | left = sz - PERF_ATTR_SIZE_VER0; |
| 2082 | if (left) { |
| 2083 | void *ptr = attr; |
| 2084 | ptr += PERF_ATTR_SIZE_VER0; |
| 2085 | |
| 2086 | ret = readn(fd, ptr, left); |
| 2087 | } |
| 2088 | /* read perf_file_section, ids are read in caller */ |
| 2089 | ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids)); |
| 2090 | |
| 2091 | return ret <= 0 ? -1 : 0; |
| 2092 | } |
| 2093 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2094 | int perf_session__read_header(struct perf_session *session, int fd) |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2095 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2096 | struct perf_header *header = &session->header; |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2097 | struct perf_file_header f_header; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2098 | struct perf_file_attr f_attr; |
| 2099 | u64 f_id; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2100 | int nr_attrs, nr_ids, i, j; |
| 2101 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2102 | session->evlist = perf_evlist__new(NULL, NULL); |
| 2103 | if (session->evlist == NULL) |
| 2104 | return -ENOMEM; |
| 2105 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2106 | if (session->fd_pipe) |
| 2107 | return perf_header__read_pipe(session, fd); |
| 2108 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 2109 | if (perf_file_header__read(&f_header, header, fd) < 0) |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 2110 | return -EINVAL; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2111 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 2112 | nr_attrs = f_header.attrs.size / f_header.attr_size; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2113 | lseek(fd, f_header.attrs.offset, SEEK_SET); |
| 2114 | |
| 2115 | for (i = 0; i < nr_attrs; i++) { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2116 | struct perf_evsel *evsel; |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 2117 | off_t tmp; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2118 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 2119 | if (read_attr(fd, header, &f_attr) < 0) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2120 | goto out_errno; |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2121 | |
David Ahern | eda3913 | 2011-07-15 12:34:09 -0600 | [diff] [blame] | 2122 | if (header->needs_swap) |
| 2123 | perf_event__attr_swap(&f_attr.attr); |
| 2124 | |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 2125 | tmp = lseek(fd, 0, SEEK_CUR); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2126 | evsel = perf_evsel__new(&f_attr.attr, i); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2127 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2128 | if (evsel == NULL) |
| 2129 | goto out_delete_evlist; |
| 2130 | /* |
| 2131 | * Do it before so that if perf_evsel__alloc_id fails, this |
| 2132 | * entry gets purged too at perf_evlist__delete(). |
| 2133 | */ |
| 2134 | perf_evlist__add(session->evlist, evsel); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2135 | |
| 2136 | nr_ids = f_attr.ids.size / sizeof(u64); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2137 | /* |
| 2138 | * We don't have the cpu and thread maps on the header, so |
| 2139 | * for allocating the perf_sample_id table we fake 1 cpu and |
| 2140 | * hattr->ids threads. |
| 2141 | */ |
| 2142 | if (perf_evsel__alloc_id(evsel, 1, nr_ids)) |
| 2143 | goto out_delete_evlist; |
| 2144 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2145 | lseek(fd, f_attr.ids.offset, SEEK_SET); |
| 2146 | |
| 2147 | for (j = 0; j < nr_ids; j++) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2148 | if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id))) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2149 | goto out_errno; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2150 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2151 | perf_evlist__id_add(session->evlist, evsel, 0, j, f_id); |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 2152 | } |
Arnaldo Carvalho de Melo | 11deb1f | 2009-11-17 01:18:09 -0200 | [diff] [blame] | 2153 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2154 | lseek(fd, tmp, SEEK_SET); |
| 2155 | } |
| 2156 | |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 2157 | symbol_conf.nr_events = nr_attrs; |
| 2158 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 2159 | if (f_header.event_types.size) { |
| 2160 | lseek(fd, f_header.event_types.offset, SEEK_SET); |
| 2161 | events = malloc(f_header.event_types.size); |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 2162 | if (events == NULL) |
| 2163 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2164 | if (perf_header__getbuffer64(header, fd, events, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2165 | f_header.event_types.size)) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2166 | goto out_errno; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 2167 | event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type); |
| 2168 | } |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 2169 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2170 | perf_header__process_sections(header, fd, NULL, |
| 2171 | perf_file_section__process); |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 2172 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2173 | lseek(fd, header->data_offset, SEEK_SET); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2174 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2175 | header->frozen = 1; |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 2176 | return 0; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2177 | out_errno: |
| 2178 | return -errno; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2179 | |
| 2180 | out_delete_evlist: |
| 2181 | perf_evlist__delete(session->evlist); |
| 2182 | session->evlist = NULL; |
| 2183 | return -ENOMEM; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2184 | } |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 2185 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2186 | int perf_event__synthesize_attr(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2187 | struct perf_event_attr *attr, u16 ids, u64 *id, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2188 | perf_event__handler_t process) |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 2189 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2190 | union perf_event *ev; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2191 | size_t size; |
| 2192 | int err; |
| 2193 | |
| 2194 | size = sizeof(struct perf_event_attr); |
| 2195 | size = ALIGN(size, sizeof(u64)); |
| 2196 | size += sizeof(struct perf_event_header); |
| 2197 | size += ids * sizeof(u64); |
| 2198 | |
| 2199 | ev = malloc(size); |
| 2200 | |
Chris Samuel | ce47dc5 | 2010-11-13 13:35:06 +1100 | [diff] [blame] | 2201 | if (ev == NULL) |
| 2202 | return -ENOMEM; |
| 2203 | |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2204 | ev->attr.attr = *attr; |
| 2205 | memcpy(ev->attr.id, id, ids * sizeof(u64)); |
| 2206 | |
| 2207 | ev->attr.header.type = PERF_RECORD_HEADER_ATTR; |
| 2208 | ev->attr.header.size = size; |
| 2209 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2210 | err = process(tool, ev, NULL, NULL); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2211 | |
| 2212 | free(ev); |
| 2213 | |
| 2214 | return err; |
| 2215 | } |
| 2216 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2217 | int perf_event__synthesize_attrs(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2218 | struct perf_session *session, |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2219 | perf_event__handler_t process) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2220 | { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2221 | struct perf_evsel *attr; |
| 2222 | int err = 0; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2223 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2224 | list_for_each_entry(attr, &session->evlist->entries, node) { |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2225 | err = perf_event__synthesize_attr(tool, &attr->attr, attr->ids, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2226 | attr->id, process); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2227 | if (err) { |
| 2228 | pr_debug("failed to create perf header attribute\n"); |
| 2229 | return err; |
| 2230 | } |
| 2231 | } |
| 2232 | |
| 2233 | return err; |
| 2234 | } |
| 2235 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2236 | int perf_event__process_attr(union perf_event *event, |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2237 | struct perf_evlist **pevlist) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2238 | { |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2239 | unsigned int i, ids, n_ids; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2240 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2241 | struct perf_evlist *evlist = *pevlist; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2242 | |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2243 | if (evlist == NULL) { |
| 2244 | *pevlist = evlist = perf_evlist__new(NULL, NULL); |
| 2245 | if (evlist == NULL) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2246 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2247 | } |
| 2248 | |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2249 | evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2250 | if (evsel == NULL) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2251 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2252 | |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2253 | perf_evlist__add(evlist, evsel); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2254 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2255 | ids = event->header.size; |
| 2256 | ids -= (void *)&event->attr.id - (void *)event; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2257 | n_ids = ids / sizeof(u64); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2258 | /* |
| 2259 | * We don't have the cpu and thread maps on the header, so |
| 2260 | * for allocating the perf_sample_id table we fake 1 cpu and |
| 2261 | * hattr->ids threads. |
| 2262 | */ |
| 2263 | if (perf_evsel__alloc_id(evsel, 1, n_ids)) |
| 2264 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2265 | |
| 2266 | for (i = 0; i < n_ids; i++) { |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2267 | perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2268 | } |
| 2269 | |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2270 | return 0; |
| 2271 | } |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2272 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2273 | int perf_event__synthesize_event_type(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2274 | u64 event_id, char *name, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2275 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2276 | struct machine *machine) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2277 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2278 | union perf_event ev; |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2279 | size_t size = 0; |
| 2280 | int err = 0; |
| 2281 | |
| 2282 | memset(&ev, 0, sizeof(ev)); |
| 2283 | |
| 2284 | ev.event_type.event_type.event_id = event_id; |
| 2285 | memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME); |
| 2286 | strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1); |
| 2287 | |
| 2288 | ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE; |
Stephane Eranian | f8f4b28 | 2012-01-20 10:49:12 +0100 | [diff] [blame] | 2289 | size = strlen(ev.event_type.event_type.name); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2290 | size = ALIGN(size, sizeof(u64)); |
| 2291 | ev.event_type.header.size = sizeof(ev.event_type) - |
| 2292 | (sizeof(ev.event_type.event_type.name) - size); |
| 2293 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2294 | err = process(tool, &ev, NULL, machine); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2295 | |
| 2296 | return err; |
| 2297 | } |
| 2298 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2299 | int perf_event__synthesize_event_types(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2300 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2301 | struct machine *machine) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2302 | { |
| 2303 | struct perf_trace_event_type *type; |
| 2304 | int i, err = 0; |
| 2305 | |
| 2306 | for (i = 0; i < event_count; i++) { |
| 2307 | type = &events[i]; |
| 2308 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2309 | err = perf_event__synthesize_event_type(tool, type->event_id, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2310 | type->name, process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2311 | machine); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2312 | if (err) { |
| 2313 | pr_debug("failed to create perf header event type\n"); |
| 2314 | return err; |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | return err; |
| 2319 | } |
| 2320 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2321 | int perf_event__process_event_type(struct perf_tool *tool __unused, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2322 | union perf_event *event) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2323 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2324 | if (perf_header__push_event(event->event_type.event_type.event_id, |
| 2325 | event->event_type.event_type.name) < 0) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2326 | return -ENOMEM; |
| 2327 | |
| 2328 | return 0; |
| 2329 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2330 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2331 | int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2332 | struct perf_evlist *evlist, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2333 | perf_event__handler_t process) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2334 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2335 | union perf_event ev; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 2336 | struct tracing_data *tdata; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2337 | ssize_t size = 0, aligned_size = 0, padding; |
Kyle McMartin | fb7d0b3 | 2011-01-24 11:13:04 -0500 | [diff] [blame] | 2338 | int err __used = 0; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2339 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 2340 | /* |
| 2341 | * We are going to store the size of the data followed |
| 2342 | * by the data contents. Since the fd descriptor is a pipe, |
| 2343 | * we cannot seek back to store the size of the data once |
| 2344 | * we know it. Instead we: |
| 2345 | * |
| 2346 | * - write the tracing data to the temp file |
| 2347 | * - get/write the data size to pipe |
| 2348 | * - write the tracing data from the temp file |
| 2349 | * to the pipe |
| 2350 | */ |
| 2351 | tdata = tracing_data_get(&evlist->entries, fd, true); |
| 2352 | if (!tdata) |
| 2353 | return -1; |
| 2354 | |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2355 | memset(&ev, 0, sizeof(ev)); |
| 2356 | |
| 2357 | ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 2358 | size = tdata->size; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2359 | aligned_size = ALIGN(size, sizeof(u64)); |
| 2360 | padding = aligned_size - size; |
| 2361 | ev.tracing_data.header.size = sizeof(ev.tracing_data); |
| 2362 | ev.tracing_data.size = aligned_size; |
| 2363 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2364 | process(tool, &ev, NULL, NULL); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2365 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 2366 | /* |
| 2367 | * The put function will copy all the tracing data |
| 2368 | * stored in temp file to the pipe. |
| 2369 | */ |
| 2370 | tracing_data_put(tdata); |
| 2371 | |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2372 | write_padded(fd, NULL, 0, padding); |
| 2373 | |
| 2374 | return aligned_size; |
| 2375 | } |
| 2376 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2377 | int perf_event__process_tracing_data(union perf_event *event, |
| 2378 | struct perf_session *session) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2379 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2380 | ssize_t size_read, padding, size = event->tracing_data.size; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2381 | off_t offset = lseek(session->fd, 0, SEEK_CUR); |
| 2382 | char buf[BUFSIZ]; |
| 2383 | |
| 2384 | /* setup for reading amidst mmap */ |
| 2385 | lseek(session->fd, offset + sizeof(struct tracing_data_event), |
| 2386 | SEEK_SET); |
| 2387 | |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 2388 | size_read = trace_report(session->fd, session->repipe); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2389 | |
| 2390 | padding = ALIGN(size_read, sizeof(u64)) - size_read; |
| 2391 | |
| 2392 | if (read(session->fd, buf, padding) < 0) |
| 2393 | die("reading input file"); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 2394 | if (session->repipe) { |
| 2395 | int retw = write(STDOUT_FILENO, buf, padding); |
| 2396 | if (retw <= 0 || retw != padding) |
| 2397 | die("repiping tracing data padding"); |
| 2398 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2399 | |
| 2400 | if (size_read + padding != size) |
| 2401 | die("tracing data size mismatch"); |
| 2402 | |
| 2403 | return size_read + padding; |
| 2404 | } |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2405 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2406 | int perf_event__synthesize_build_id(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2407 | struct dso *pos, u16 misc, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2408 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2409 | struct machine *machine) |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2410 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2411 | union perf_event ev; |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2412 | size_t len; |
| 2413 | int err = 0; |
| 2414 | |
| 2415 | if (!pos->hit) |
| 2416 | return err; |
| 2417 | |
| 2418 | memset(&ev, 0, sizeof(ev)); |
| 2419 | |
| 2420 | len = pos->long_name_len + 1; |
| 2421 | len = ALIGN(len, NAME_ALIGN); |
| 2422 | memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id)); |
| 2423 | ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID; |
| 2424 | ev.build_id.header.misc = misc; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 2425 | ev.build_id.pid = machine->pid; |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2426 | ev.build_id.header.size = sizeof(ev.build_id) + len; |
| 2427 | memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len); |
| 2428 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2429 | err = process(tool, &ev, NULL, machine); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2430 | |
| 2431 | return err; |
| 2432 | } |
| 2433 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2434 | int perf_event__process_build_id(struct perf_tool *tool __used, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2435 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2436 | struct perf_session *session) |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2437 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2438 | __event_process_build_id(&event->build_id, |
| 2439 | event->build_id.filename, |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2440 | session); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2441 | return 0; |
| 2442 | } |
Stephane Eranian | a1ac1d3 | 2010-06-17 11:39:01 +0200 | [diff] [blame] | 2443 | |
| 2444 | void disable_buildid_cache(void) |
| 2445 | { |
| 2446 | no_buildid_cache = true; |
| 2447 | } |