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