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