Xiao Guangrong | b8f46c5 | 2010-02-03 11:53:14 +0800 | [diff] [blame] | 1 | #define _FILE_OFFSET_BITS 64 |
| 2 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 3 | #include <sys/types.h> |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 4 | #include <byteswap.h> |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 5 | #include <unistd.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 8 | #include <linux/list.h> |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 9 | #include <linux/kernel.h> |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 10 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 11 | #include "evlist.h" |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 12 | #include "evsel.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 13 | #include "util.h" |
| 14 | #include "header.h" |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 15 | #include "../perf.h" |
| 16 | #include "trace-event.h" |
Arnaldo Carvalho de Melo | 301a0b0 | 2009-12-13 19:50:25 -0200 | [diff] [blame] | 17 | #include "session.h" |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 18 | #include "symbol.h" |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 19 | #include "debug.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 20 | |
Stephane Eranian | a1ac1d3 | 2010-06-17 11:39:01 +0200 | [diff] [blame] | 21 | static bool no_buildid_cache = false; |
| 22 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 23 | static int event_count; |
| 24 | static struct perf_trace_event_type *events; |
| 25 | |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 26 | int perf_header__push_event(u64 id, const char *name) |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 27 | { |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 28 | if (strlen(name) > MAX_EVENT_NAME) |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 29 | pr_warning("Event %s will be truncated\n", name); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 30 | |
| 31 | if (!events) { |
| 32 | events = malloc(sizeof(struct perf_trace_event_type)); |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 33 | if (events == NULL) |
| 34 | return -ENOMEM; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 35 | } else { |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 36 | struct perf_trace_event_type *nevents; |
| 37 | |
| 38 | nevents = realloc(events, (event_count + 1) * sizeof(*events)); |
| 39 | if (nevents == NULL) |
| 40 | return -ENOMEM; |
| 41 | events = nevents; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 42 | } |
| 43 | memset(&events[event_count], 0, sizeof(struct perf_trace_event_type)); |
| 44 | events[event_count].event_id = id; |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 45 | strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 46 | event_count++; |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 47 | return 0; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | char *perf_header__find_event(u64 id) |
| 51 | { |
| 52 | int i; |
| 53 | for (i = 0 ; i < event_count; i++) { |
| 54 | if (events[i].event_id == id) |
| 55 | return events[i].name; |
| 56 | } |
| 57 | return NULL; |
| 58 | } |
| 59 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 60 | static const char *__perf_magic = "PERFFILE"; |
| 61 | |
| 62 | #define PERF_MAGIC (*(u64 *)__perf_magic) |
| 63 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 64 | struct perf_file_attr { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 65 | struct perf_event_attr attr; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 66 | struct perf_file_section ids; |
| 67 | }; |
| 68 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 69 | 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] | 70 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 71 | set_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 72 | } |
| 73 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 74 | 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] | 75 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 76 | clear_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 77 | } |
| 78 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 79 | 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] | 80 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 81 | return test_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 82 | } |
| 83 | |
Arnaldo Carvalho de Melo | 3726cc7 | 2009-11-17 01:18:12 -0200 | [diff] [blame] | 84 | static int do_write(int fd, const void *buf, size_t size) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 85 | { |
| 86 | while (size) { |
| 87 | int ret = write(fd, buf, size); |
| 88 | |
| 89 | if (ret < 0) |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 90 | return -errno; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 91 | |
| 92 | size -= ret; |
| 93 | buf += ret; |
| 94 | } |
Arnaldo Carvalho de Melo | 3726cc7 | 2009-11-17 01:18:12 -0200 | [diff] [blame] | 95 | |
| 96 | return 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Arnaldo Carvalho de Melo | f92cb24 | 2010-01-04 16:19:28 -0200 | [diff] [blame] | 99 | #define NAME_ALIGN 64 |
| 100 | |
| 101 | static int write_padded(int fd, const void *bf, size_t count, |
| 102 | size_t count_aligned) |
| 103 | { |
| 104 | static const char zero_buf[NAME_ALIGN]; |
| 105 | int err = do_write(fd, bf, count); |
| 106 | |
| 107 | if (!err) |
| 108 | err = do_write(fd, zero_buf, count_aligned - count); |
| 109 | |
| 110 | return err; |
| 111 | } |
| 112 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 113 | #define dsos__for_each_with_build_id(pos, head) \ |
| 114 | list_for_each_entry(pos, head, node) \ |
| 115 | if (!pos->has_build_id) \ |
| 116 | continue; \ |
| 117 | else |
| 118 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 119 | static int __dsos__write_buildid_table(struct list_head *head, pid_t pid, |
| 120 | u16 misc, int fd) |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 121 | { |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 122 | struct dso *pos; |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 123 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 124 | dsos__for_each_with_build_id(pos, head) { |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 125 | int err; |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 126 | struct build_id_event b; |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 127 | size_t len; |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 128 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 129 | if (!pos->hit) |
| 130 | continue; |
| 131 | len = pos->long_name_len + 1; |
OGAWA Hirofumi | 7691b1e | 2009-12-06 20:10:49 +0900 | [diff] [blame] | 132 | len = ALIGN(len, NAME_ALIGN); |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 133 | memset(&b, 0, sizeof(b)); |
| 134 | memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id)); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 135 | b.pid = pid; |
Arnaldo Carvalho de Melo | a89e5ab | 2010-01-07 19:59:39 -0200 | [diff] [blame] | 136 | b.header.misc = misc; |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 137 | b.header.size = sizeof(b) + len; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 138 | err = do_write(fd, &b, sizeof(b)); |
| 139 | if (err < 0) |
| 140 | return err; |
Arnaldo Carvalho de Melo | f92cb24 | 2010-01-04 16:19:28 -0200 | [diff] [blame] | 141 | err = write_padded(fd, pos->long_name, |
| 142 | pos->long_name_len + 1, len); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 143 | if (err < 0) |
| 144 | return err; |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 145 | } |
Arnaldo Carvalho de Melo | 3726cc7 | 2009-11-17 01:18:12 -0200 | [diff] [blame] | 146 | |
| 147 | return 0; |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 150 | static int machine__write_buildid_table(struct machine *machine, int fd) |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 151 | { |
| 152 | int err; |
| 153 | u16 kmisc = PERF_RECORD_MISC_KERNEL, |
| 154 | umisc = PERF_RECORD_MISC_USER; |
| 155 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 156 | if (!machine__is_host(machine)) { |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 157 | kmisc = PERF_RECORD_MISC_GUEST_KERNEL; |
| 158 | umisc = PERF_RECORD_MISC_GUEST_USER; |
| 159 | } |
| 160 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 161 | err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid, |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 162 | kmisc, fd); |
| 163 | if (err == 0) |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 164 | err = __dsos__write_buildid_table(&machine->user_dsos, |
| 165 | machine->pid, umisc, fd); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 166 | return err; |
| 167 | } |
| 168 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 169 | static int dsos__write_buildid_table(struct perf_header *header, int fd) |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 170 | { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 171 | struct perf_session *session = container_of(header, |
| 172 | struct perf_session, header); |
| 173 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 174 | int err = machine__write_buildid_table(&session->host_machine, fd); |
| 175 | |
| 176 | if (err) |
| 177 | return err; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 178 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 179 | for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { |
| 180 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 181 | err = machine__write_buildid_table(pos, fd); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 182 | if (err) |
| 183 | break; |
| 184 | } |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 185 | return err; |
| 186 | } |
| 187 | |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 188 | int build_id_cache__add_s(const char *sbuild_id, const char *debugdir, |
| 189 | const char *name, bool is_kallsyms) |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 190 | { |
| 191 | const size_t size = PATH_MAX; |
Lin Ming | 7ee235e | 2011-03-03 23:23:57 +0800 | [diff] [blame] | 192 | char *realname, *filename = malloc(size), |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 193 | *linkname = malloc(size), *targetname; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 194 | int len, err = -1; |
| 195 | |
Lin Ming | 7ee235e | 2011-03-03 23:23:57 +0800 | [diff] [blame] | 196 | if (is_kallsyms) |
| 197 | realname = (char *)name; |
| 198 | else |
| 199 | realname = realpath(name, NULL); |
| 200 | |
Franck Bui-Huu | 68a7a77 | 2010-12-10 22:06:26 +0100 | [diff] [blame] | 201 | if (realname == NULL || filename == NULL || linkname == NULL) |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 202 | goto out_free; |
| 203 | |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 204 | len = snprintf(filename, size, "%s%s%s", |
Franck Bui-Huu | 68a7a77 | 2010-12-10 22:06:26 +0100 | [diff] [blame] | 205 | debugdir, is_kallsyms ? "/" : "", realname); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 206 | if (mkdir_p(filename, 0755)) |
| 207 | goto out_free; |
| 208 | |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 209 | snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 210 | |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 211 | if (access(filename, F_OK)) { |
| 212 | if (is_kallsyms) { |
| 213 | if (copyfile("/proc/kallsyms", filename)) |
| 214 | goto out_free; |
Franck Bui-Huu | 68a7a77 | 2010-12-10 22:06:26 +0100 | [diff] [blame] | 215 | } else if (link(realname, filename) && copyfile(name, filename)) |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 216 | goto out_free; |
| 217 | } |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 218 | |
| 219 | len = snprintf(linkname, size, "%s/.build-id/%.2s", |
| 220 | debugdir, sbuild_id); |
| 221 | |
| 222 | if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) |
| 223 | goto out_free; |
| 224 | |
| 225 | snprintf(linkname + len, size - len, "/%s", sbuild_id + 2); |
| 226 | targetname = filename + strlen(debugdir) - 5; |
| 227 | memcpy(targetname, "../..", 5); |
| 228 | |
| 229 | if (symlink(targetname, linkname) == 0) |
| 230 | err = 0; |
| 231 | out_free: |
Lin Ming | 7ee235e | 2011-03-03 23:23:57 +0800 | [diff] [blame] | 232 | if (!is_kallsyms) |
| 233 | free(realname); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 234 | free(filename); |
| 235 | free(linkname); |
| 236 | return err; |
| 237 | } |
| 238 | |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 239 | static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size, |
| 240 | const char *name, const char *debugdir, |
| 241 | bool is_kallsyms) |
| 242 | { |
| 243 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 244 | |
| 245 | build_id__sprintf(build_id, build_id_size, sbuild_id); |
| 246 | |
| 247 | return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms); |
| 248 | } |
| 249 | |
| 250 | int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir) |
| 251 | { |
| 252 | const size_t size = PATH_MAX; |
| 253 | char *filename = malloc(size), |
| 254 | *linkname = malloc(size); |
| 255 | int err = -1; |
| 256 | |
| 257 | if (filename == NULL || linkname == NULL) |
| 258 | goto out_free; |
| 259 | |
| 260 | snprintf(linkname, size, "%s/.build-id/%.2s/%s", |
| 261 | debugdir, sbuild_id, sbuild_id + 2); |
| 262 | |
| 263 | if (access(linkname, F_OK)) |
| 264 | goto out_free; |
| 265 | |
| 266 | if (readlink(linkname, filename, size) < 0) |
| 267 | goto out_free; |
| 268 | |
| 269 | if (unlink(linkname)) |
| 270 | goto out_free; |
| 271 | |
| 272 | /* |
| 273 | * Since the link is relative, we must make it absolute: |
| 274 | */ |
| 275 | snprintf(linkname, size, "%s/.build-id/%.2s/%s", |
| 276 | debugdir, sbuild_id, filename); |
| 277 | |
| 278 | if (unlink(linkname)) |
| 279 | goto out_free; |
| 280 | |
| 281 | err = 0; |
| 282 | out_free: |
| 283 | free(filename); |
| 284 | free(linkname); |
| 285 | return err; |
| 286 | } |
| 287 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 288 | static int dso__cache_build_id(struct dso *dso, const char *debugdir) |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 289 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 290 | bool is_kallsyms = dso->kernel && dso->long_name[0] != '/'; |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 291 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 292 | return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), |
| 293 | dso->long_name, debugdir, is_kallsyms); |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 294 | } |
| 295 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 296 | static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir) |
| 297 | { |
| 298 | struct dso *pos; |
| 299 | int err = 0; |
| 300 | |
| 301 | dsos__for_each_with_build_id(pos, head) |
| 302 | if (dso__cache_build_id(pos, debugdir)) |
| 303 | err = -1; |
| 304 | |
| 305 | return err; |
| 306 | } |
| 307 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 308 | static int machine__cache_build_ids(struct machine *machine, const char *debugdir) |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 309 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 310 | int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir); |
| 311 | ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 312 | return ret; |
| 313 | } |
| 314 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 315 | static int perf_session__cache_build_ids(struct perf_session *session) |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 316 | { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 317 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 318 | int ret; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 319 | char debugdir[PATH_MAX]; |
| 320 | |
Stephane Eranian | 45de34b | 2010-06-01 21:25:01 +0200 | [diff] [blame] | 321 | snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 322 | |
| 323 | if (mkdir(debugdir, 0755) != 0 && errno != EEXIST) |
| 324 | return -1; |
| 325 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 326 | ret = machine__cache_build_ids(&session->host_machine, debugdir); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 327 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 328 | for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 329 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 330 | ret |= machine__cache_build_ids(pos, debugdir); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 331 | } |
| 332 | return ret ? -1 : 0; |
| 333 | } |
| 334 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 335 | static bool machine__read_build_ids(struct machine *machine, bool with_hits) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 336 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 337 | bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits); |
| 338 | ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 339 | return ret; |
| 340 | } |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 341 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 342 | static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits) |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 343 | { |
| 344 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 345 | bool ret = machine__read_build_ids(&session->host_machine, with_hits); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 346 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 347 | for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 348 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 349 | ret |= machine__read_build_ids(pos, with_hits); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | return ret; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 353 | } |
| 354 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 355 | static int perf_header__adds_write(struct perf_header *header, |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 356 | struct perf_evlist *evlist, int fd) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 357 | { |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 358 | int nr_sections; |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 359 | struct perf_session *session; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 360 | struct perf_file_section *feat_sec; |
| 361 | int sec_size; |
| 362 | u64 sec_start; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 363 | int idx = 0, err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 364 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 365 | session = container_of(header, struct perf_session, header); |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 366 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 367 | if (perf_header__has_feat(header, HEADER_BUILD_ID && |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 368 | !perf_session__read_build_ids(session, true))) |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 369 | perf_header__clear_feat(header, HEADER_BUILD_ID); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 370 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 371 | nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 372 | if (!nr_sections) |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 373 | return 0; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 374 | |
| 375 | feat_sec = calloc(sizeof(*feat_sec), nr_sections); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 376 | if (feat_sec == NULL) |
| 377 | return -ENOMEM; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 378 | |
| 379 | sec_size = sizeof(*feat_sec) * nr_sections; |
| 380 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 381 | sec_start = header->data_offset + header->data_size; |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 382 | lseek(fd, sec_start + sec_size, SEEK_SET); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 383 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 384 | if (perf_header__has_feat(header, HEADER_TRACE_INFO)) { |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 385 | struct perf_file_section *trace_sec; |
| 386 | |
| 387 | trace_sec = &feat_sec[idx++]; |
| 388 | |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 389 | /* Write trace info */ |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 390 | trace_sec->offset = lseek(fd, 0, SEEK_CUR); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 391 | read_tracing_data(fd, &evlist->entries); |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 392 | trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset; |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 393 | } |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 394 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 395 | if (perf_header__has_feat(header, HEADER_BUILD_ID)) { |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 396 | struct perf_file_section *buildid_sec; |
| 397 | |
| 398 | buildid_sec = &feat_sec[idx++]; |
| 399 | |
| 400 | /* Write build-ids */ |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 401 | buildid_sec->offset = lseek(fd, 0, SEEK_CUR); |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 402 | err = dsos__write_buildid_table(header, fd); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 403 | if (err < 0) { |
| 404 | pr_debug("failed to write buildid table\n"); |
| 405 | goto out_free; |
| 406 | } |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 407 | buildid_sec->size = lseek(fd, 0, SEEK_CUR) - |
| 408 | buildid_sec->offset; |
Stephane Eranian | a1ac1d3 | 2010-06-17 11:39:01 +0200 | [diff] [blame] | 409 | if (!no_buildid_cache) |
| 410 | perf_session__cache_build_ids(session); |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 411 | } |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 412 | |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 413 | lseek(fd, sec_start, SEEK_SET); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 414 | err = do_write(fd, feat_sec, sec_size); |
| 415 | if (err < 0) |
| 416 | pr_debug("failed to write feature section\n"); |
| 417 | out_free: |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 418 | free(feat_sec); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 419 | return err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 420 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 421 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 422 | int perf_header__write_pipe(int fd) |
| 423 | { |
| 424 | struct perf_pipe_file_header f_header; |
| 425 | int err; |
| 426 | |
| 427 | f_header = (struct perf_pipe_file_header){ |
| 428 | .magic = PERF_MAGIC, |
| 429 | .size = sizeof(f_header), |
| 430 | }; |
| 431 | |
| 432 | err = do_write(fd, &f_header, sizeof(f_header)); |
| 433 | if (err < 0) { |
| 434 | pr_debug("failed to write perf pipe header\n"); |
| 435 | return err; |
| 436 | } |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 441 | int perf_session__write_header(struct perf_session *session, |
| 442 | struct perf_evlist *evlist, |
| 443 | int fd, bool at_exit) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 444 | { |
| 445 | struct perf_file_header f_header; |
| 446 | struct perf_file_attr f_attr; |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 447 | struct perf_header *header = &session->header; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 448 | struct perf_evsel *attr, *pair = NULL; |
| 449 | int err; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 450 | |
| 451 | lseek(fd, sizeof(f_header), SEEK_SET); |
| 452 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 453 | if (session->evlist != evlist) |
| 454 | pair = list_entry(session->evlist->entries.next, struct perf_evsel, node); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 455 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 456 | list_for_each_entry(attr, &evlist->entries, node) { |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 457 | attr->id_offset = lseek(fd, 0, SEEK_CUR); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 458 | err = do_write(fd, attr->id, attr->ids * sizeof(u64)); |
| 459 | if (err < 0) { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 460 | out_err_write: |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 461 | pr_debug("failed to write perf header\n"); |
| 462 | return err; |
| 463 | } |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 464 | if (session->evlist != evlist) { |
| 465 | err = do_write(fd, pair->id, pair->ids * sizeof(u64)); |
| 466 | if (err < 0) |
| 467 | goto out_err_write; |
| 468 | attr->ids += pair->ids; |
| 469 | pair = list_entry(pair->node.next, struct perf_evsel, node); |
| 470 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 471 | } |
| 472 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 473 | header->attr_offset = lseek(fd, 0, SEEK_CUR); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 474 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 475 | list_for_each_entry(attr, &evlist->entries, node) { |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 476 | f_attr = (struct perf_file_attr){ |
| 477 | .attr = attr->attr, |
| 478 | .ids = { |
| 479 | .offset = attr->id_offset, |
| 480 | .size = attr->ids * sizeof(u64), |
| 481 | } |
| 482 | }; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 483 | err = do_write(fd, &f_attr, sizeof(f_attr)); |
| 484 | if (err < 0) { |
| 485 | pr_debug("failed to write perf header attribute\n"); |
| 486 | return err; |
| 487 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 490 | header->event_offset = lseek(fd, 0, SEEK_CUR); |
| 491 | header->event_size = event_count * sizeof(struct perf_trace_event_type); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 492 | if (events) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 493 | err = do_write(fd, events, header->event_size); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 494 | if (err < 0) { |
| 495 | pr_debug("failed to write perf header events\n"); |
| 496 | return err; |
| 497 | } |
| 498 | } |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 499 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 500 | header->data_offset = lseek(fd, 0, SEEK_CUR); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 501 | |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 502 | if (at_exit) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 503 | err = perf_header__adds_write(header, evlist, fd); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 504 | if (err < 0) |
| 505 | return err; |
| 506 | } |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 507 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 508 | f_header = (struct perf_file_header){ |
| 509 | .magic = PERF_MAGIC, |
| 510 | .size = sizeof(f_header), |
| 511 | .attr_size = sizeof(f_attr), |
| 512 | .attrs = { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 513 | .offset = header->attr_offset, |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 514 | .size = evlist->nr_entries * sizeof(f_attr), |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 515 | }, |
| 516 | .data = { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 517 | .offset = header->data_offset, |
| 518 | .size = header->data_size, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 519 | }, |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 520 | .event_types = { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 521 | .offset = header->event_offset, |
| 522 | .size = header->event_size, |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 523 | }, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 524 | }; |
| 525 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 526 | memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features)); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 527 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 528 | lseek(fd, 0, SEEK_SET); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 529 | err = do_write(fd, &f_header, sizeof(f_header)); |
| 530 | if (err < 0) { |
| 531 | pr_debug("failed to write perf header\n"); |
| 532 | return err; |
| 533 | } |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 534 | lseek(fd, header->data_offset + header->data_size, SEEK_SET); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 535 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 536 | header->frozen = 1; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 537 | return 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 540 | static int perf_header__getbuffer64(struct perf_header *header, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 541 | int fd, void *buf, size_t size) |
| 542 | { |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 543 | if (readn(fd, buf, size) <= 0) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 544 | return -1; |
| 545 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 546 | if (header->needs_swap) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 547 | mem_bswap_64(buf, size); |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 552 | int perf_header__process_sections(struct perf_header *header, int fd, |
| 553 | int (*process)(struct perf_file_section *section, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 554 | struct perf_header *ph, |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 555 | int feat, int fd)) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 556 | { |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 557 | struct perf_file_section *feat_sec; |
| 558 | int nr_sections; |
| 559 | int sec_size; |
| 560 | int idx = 0; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 561 | int err = -1, feat = 1; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 562 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 563 | nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 564 | if (!nr_sections) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 565 | return 0; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 566 | |
| 567 | feat_sec = calloc(sizeof(*feat_sec), nr_sections); |
| 568 | if (!feat_sec) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 569 | return -1; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 570 | |
| 571 | sec_size = sizeof(*feat_sec) * nr_sections; |
| 572 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 573 | lseek(fd, header->data_offset + header->data_size, SEEK_SET); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 574 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 575 | if (perf_header__getbuffer64(header, fd, feat_sec, sec_size)) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 576 | goto out_free; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 577 | |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 578 | err = 0; |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 579 | while (idx < nr_sections && feat < HEADER_LAST_FEATURE) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 580 | if (perf_header__has_feat(header, feat)) { |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 581 | struct perf_file_section *sec = &feat_sec[idx++]; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 582 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 583 | err = process(sec, header, feat, fd); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 584 | if (err < 0) |
| 585 | break; |
| 586 | } |
| 587 | ++feat; |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 588 | } |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 589 | out_free: |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 590 | free(feat_sec); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 591 | return err; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 592 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 593 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 594 | int perf_file_header__read(struct perf_file_header *header, |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 595 | struct perf_header *ph, int fd) |
| 596 | { |
| 597 | lseek(fd, 0, SEEK_SET); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 598 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 599 | if (readn(fd, header, sizeof(*header)) <= 0 || |
| 600 | memcmp(&header->magic, __perf_magic, sizeof(header->magic))) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 601 | return -1; |
| 602 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 603 | if (header->attr_size != sizeof(struct perf_file_attr)) { |
| 604 | u64 attr_size = bswap_64(header->attr_size); |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 605 | |
| 606 | if (attr_size != sizeof(struct perf_file_attr)) |
| 607 | return -1; |
| 608 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 609 | mem_bswap_64(header, offsetof(struct perf_file_header, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 610 | adds_features)); |
| 611 | ph->needs_swap = true; |
| 612 | } |
| 613 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 614 | if (header->size != sizeof(*header)) { |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 615 | /* Support the previous format */ |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 616 | if (header->size == offsetof(typeof(*header), adds_features)) |
| 617 | bitmap_zero(header->adds_features, HEADER_FEAT_BITS); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 618 | else |
| 619 | return -1; |
| 620 | } |
| 621 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 622 | memcpy(&ph->adds_features, &header->adds_features, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 623 | sizeof(ph->adds_features)); |
| 624 | /* |
| 625 | * FIXME: hack that assumes that if we need swap the perf.data file |
| 626 | * may be coming from an arch with a different word-size, ergo different |
| 627 | * DEFINE_BITMAP format, investigate more later, but for now its mostly |
| 628 | * safe to assume that we have a build-id section. Trace files probably |
| 629 | * have several other issues in this realm anyway... |
| 630 | */ |
| 631 | if (ph->needs_swap) { |
| 632 | memset(&ph->adds_features, 0, sizeof(ph->adds_features)); |
| 633 | perf_header__set_feat(ph, HEADER_BUILD_ID); |
| 634 | } |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 635 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 636 | ph->event_offset = header->event_types.offset; |
| 637 | ph->event_size = header->event_types.size; |
| 638 | ph->data_offset = header->data.offset; |
| 639 | ph->data_size = header->data.size; |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 640 | return 0; |
| 641 | } |
| 642 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 643 | static int __event_process_build_id(struct build_id_event *bev, |
| 644 | char *filename, |
| 645 | struct perf_session *session) |
| 646 | { |
| 647 | int err = -1; |
| 648 | struct list_head *head; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 649 | struct machine *machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 650 | u16 misc; |
| 651 | struct dso *dso; |
| 652 | enum dso_kernel_type dso_type; |
| 653 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 654 | machine = perf_session__findnew_machine(session, bev->pid); |
| 655 | if (!machine) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 656 | goto out; |
| 657 | |
| 658 | misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 659 | |
| 660 | switch (misc) { |
| 661 | case PERF_RECORD_MISC_KERNEL: |
| 662 | dso_type = DSO_TYPE_KERNEL; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 663 | head = &machine->kernel_dsos; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 664 | break; |
| 665 | case PERF_RECORD_MISC_GUEST_KERNEL: |
| 666 | dso_type = DSO_TYPE_GUEST_KERNEL; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 667 | head = &machine->kernel_dsos; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 668 | break; |
| 669 | case PERF_RECORD_MISC_USER: |
| 670 | case PERF_RECORD_MISC_GUEST_USER: |
| 671 | dso_type = DSO_TYPE_USER; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 672 | head = &machine->user_dsos; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 673 | break; |
| 674 | default: |
| 675 | goto out; |
| 676 | } |
| 677 | |
| 678 | dso = __dsos__findnew(head, filename); |
| 679 | if (dso != NULL) { |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 680 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 681 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 682 | dso__set_build_id(dso, &bev->build_id); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 683 | |
| 684 | if (filename[0] == '[') |
| 685 | dso->kernel = dso_type; |
| 686 | |
| 687 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), |
| 688 | sbuild_id); |
| 689 | pr_debug("build id event received for %s: %s\n", |
| 690 | dso->long_name, sbuild_id); |
| 691 | } |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 692 | |
| 693 | err = 0; |
| 694 | out: |
| 695 | return err; |
| 696 | } |
| 697 | |
Arnaldo Carvalho de Melo | b251148 | 2011-03-22 13:36:45 -0300 | [diff] [blame] | 698 | static int perf_header__read_build_ids_abi_quirk(struct perf_header *header, |
| 699 | int input, u64 offset, u64 size) |
| 700 | { |
| 701 | struct perf_session *session = container_of(header, struct perf_session, header); |
| 702 | struct { |
| 703 | struct perf_event_header header; |
| 704 | u8 build_id[ALIGN(BUILD_ID_SIZE, sizeof(u64))]; |
| 705 | char filename[0]; |
| 706 | } old_bev; |
| 707 | struct build_id_event bev; |
| 708 | char filename[PATH_MAX]; |
| 709 | u64 limit = offset + size; |
| 710 | |
| 711 | while (offset < limit) { |
| 712 | ssize_t len; |
| 713 | |
| 714 | if (read(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev)) |
| 715 | return -1; |
| 716 | |
| 717 | if (header->needs_swap) |
| 718 | perf_event_header__bswap(&old_bev.header); |
| 719 | |
| 720 | len = old_bev.header.size - sizeof(old_bev); |
| 721 | if (read(input, filename, len) != len) |
| 722 | return -1; |
| 723 | |
| 724 | bev.header = old_bev.header; |
| 725 | bev.pid = 0; |
| 726 | memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id)); |
| 727 | __event_process_build_id(&bev, filename, session); |
| 728 | |
| 729 | offset += bev.header.size; |
| 730 | } |
| 731 | |
| 732 | return 0; |
| 733 | } |
| 734 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 735 | static int perf_header__read_build_ids(struct perf_header *header, |
| 736 | int input, u64 offset, u64 size) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 737 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 738 | struct perf_session *session = container_of(header, struct perf_session, header); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 739 | struct build_id_event bev; |
| 740 | char filename[PATH_MAX]; |
Arnaldo Carvalho de Melo | b251148 | 2011-03-22 13:36:45 -0300 | [diff] [blame] | 741 | u64 limit = offset + size, orig_offset = offset; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 742 | int err = -1; |
| 743 | |
| 744 | while (offset < limit) { |
| 745 | ssize_t len; |
| 746 | |
| 747 | if (read(input, &bev, sizeof(bev)) != sizeof(bev)) |
| 748 | goto out; |
| 749 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 750 | if (header->needs_swap) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 751 | perf_event_header__bswap(&bev.header); |
| 752 | |
| 753 | len = bev.header.size - sizeof(bev); |
| 754 | if (read(input, filename, len) != len) |
| 755 | goto out; |
Arnaldo Carvalho de Melo | b251148 | 2011-03-22 13:36:45 -0300 | [diff] [blame] | 756 | /* |
| 757 | * The a1645ce1 changeset: |
| 758 | * |
| 759 | * "perf: 'perf kvm' tool for monitoring guest performance from host" |
| 760 | * |
| 761 | * Added a field to struct build_id_event that broke the file |
| 762 | * format. |
| 763 | * |
| 764 | * Since the kernel build-id is the first entry, process the |
| 765 | * table using the old format if the well known |
| 766 | * '[kernel.kallsyms]' string for the kernel build-id has the |
| 767 | * first 4 characters chopped off (where the pid_t sits). |
| 768 | */ |
| 769 | if (memcmp(filename, "nel.kallsyms]", 13) == 0) { |
| 770 | if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1) |
| 771 | return -1; |
| 772 | return perf_header__read_build_ids_abi_quirk(header, input, offset, size); |
| 773 | } |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 774 | |
| 775 | __event_process_build_id(&bev, filename, session); |
| 776 | |
| 777 | offset += bev.header.size; |
| 778 | } |
| 779 | err = 0; |
| 780 | out: |
| 781 | return err; |
| 782 | } |
| 783 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 784 | 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] | 785 | struct perf_header *ph, |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 786 | int feat, int fd) |
| 787 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 788 | if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 789 | pr_debug("Failed to lseek to %" PRIu64 " offset for feature " |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 790 | "%d, continuing...\n", section->offset, feat); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | switch (feat) { |
| 795 | case HEADER_TRACE_INFO: |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 796 | trace_report(fd, false); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 797 | break; |
| 798 | |
| 799 | case HEADER_BUILD_ID: |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 800 | if (perf_header__read_build_ids(ph, fd, section->offset, section->size)) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 801 | pr_debug("Failed to read buildids, continuing...\n"); |
| 802 | break; |
| 803 | default: |
| 804 | pr_debug("unknown feature %d, continuing...\n", feat); |
| 805 | } |
| 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 810 | 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] | 811 | struct perf_header *ph, int fd, |
| 812 | bool repipe) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 813 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 814 | if (readn(fd, header, sizeof(*header)) <= 0 || |
| 815 | memcmp(&header->magic, __perf_magic, sizeof(header->magic))) |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 816 | return -1; |
| 817 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 818 | if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 819 | return -1; |
| 820 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 821 | if (header->size != sizeof(*header)) { |
| 822 | u64 size = bswap_64(header->size); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 823 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 824 | if (size != sizeof(*header)) |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 825 | return -1; |
| 826 | |
| 827 | ph->needs_swap = true; |
| 828 | } |
| 829 | |
| 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | static int perf_header__read_pipe(struct perf_session *session, int fd) |
| 834 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 835 | struct perf_header *header = &session->header; |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 836 | struct perf_pipe_file_header f_header; |
| 837 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 838 | if (perf_file_header__read_pipe(&f_header, header, fd, |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 839 | session->repipe) < 0) { |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 840 | pr_debug("incompatible file format\n"); |
| 841 | return -EINVAL; |
| 842 | } |
| 843 | |
| 844 | session->fd = fd; |
| 845 | |
| 846 | return 0; |
| 847 | } |
| 848 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 849 | int perf_session__read_header(struct perf_session *session, int fd) |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 850 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 851 | struct perf_header *header = &session->header; |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 852 | struct perf_file_header f_header; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 853 | struct perf_file_attr f_attr; |
| 854 | u64 f_id; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 855 | int nr_attrs, nr_ids, i, j; |
| 856 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 857 | session->evlist = perf_evlist__new(NULL, NULL); |
| 858 | if (session->evlist == NULL) |
| 859 | return -ENOMEM; |
| 860 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 861 | if (session->fd_pipe) |
| 862 | return perf_header__read_pipe(session, fd); |
| 863 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 864 | if (perf_file_header__read(&f_header, header, fd) < 0) { |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 865 | pr_debug("incompatible file format\n"); |
| 866 | return -EINVAL; |
| 867 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 868 | |
| 869 | nr_attrs = f_header.attrs.size / sizeof(f_attr); |
| 870 | lseek(fd, f_header.attrs.offset, SEEK_SET); |
| 871 | |
| 872 | for (i = 0; i < nr_attrs; i++) { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 873 | struct perf_evsel *evsel; |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 874 | off_t tmp; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 875 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 876 | if (perf_header__getbuffer64(header, fd, &f_attr, sizeof(f_attr))) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 877 | goto out_errno; |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 878 | |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 879 | tmp = lseek(fd, 0, SEEK_CUR); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 880 | evsel = perf_evsel__new(&f_attr.attr, i); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 881 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 882 | if (evsel == NULL) |
| 883 | goto out_delete_evlist; |
| 884 | /* |
| 885 | * Do it before so that if perf_evsel__alloc_id fails, this |
| 886 | * entry gets purged too at perf_evlist__delete(). |
| 887 | */ |
| 888 | perf_evlist__add(session->evlist, evsel); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 889 | |
| 890 | nr_ids = f_attr.ids.size / sizeof(u64); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 891 | /* |
| 892 | * We don't have the cpu and thread maps on the header, so |
| 893 | * for allocating the perf_sample_id table we fake 1 cpu and |
| 894 | * hattr->ids threads. |
| 895 | */ |
| 896 | if (perf_evsel__alloc_id(evsel, 1, nr_ids)) |
| 897 | goto out_delete_evlist; |
| 898 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 899 | lseek(fd, f_attr.ids.offset, SEEK_SET); |
| 900 | |
| 901 | for (j = 0; j < nr_ids; j++) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 902 | 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] | 903 | goto out_errno; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 904 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 905 | 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] | 906 | } |
Arnaldo Carvalho de Melo | 11deb1f | 2009-11-17 01:18:09 -0200 | [diff] [blame] | 907 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 908 | lseek(fd, tmp, SEEK_SET); |
| 909 | } |
| 910 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 911 | if (f_header.event_types.size) { |
| 912 | lseek(fd, f_header.event_types.offset, SEEK_SET); |
| 913 | events = malloc(f_header.event_types.size); |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 914 | if (events == NULL) |
| 915 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 916 | if (perf_header__getbuffer64(header, fd, events, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 917 | f_header.event_types.size)) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 918 | goto out_errno; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 919 | event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type); |
| 920 | } |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 921 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 922 | perf_header__process_sections(header, fd, perf_file_section__process); |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 923 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 924 | lseek(fd, header->data_offset, SEEK_SET); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 925 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 926 | header->frozen = 1; |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 927 | return 0; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 928 | out_errno: |
| 929 | return -errno; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 930 | |
| 931 | out_delete_evlist: |
| 932 | perf_evlist__delete(session->evlist); |
| 933 | session->evlist = NULL; |
| 934 | return -ENOMEM; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 935 | } |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 936 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 937 | int perf_event__synthesize_attr(struct perf_event_attr *attr, u16 ids, u64 *id, |
| 938 | perf_event__handler_t process, |
| 939 | struct perf_session *session) |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 940 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 941 | union perf_event *ev; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 942 | size_t size; |
| 943 | int err; |
| 944 | |
| 945 | size = sizeof(struct perf_event_attr); |
| 946 | size = ALIGN(size, sizeof(u64)); |
| 947 | size += sizeof(struct perf_event_header); |
| 948 | size += ids * sizeof(u64); |
| 949 | |
| 950 | ev = malloc(size); |
| 951 | |
Chris Samuel | ce47dc5 | 2010-11-13 13:35:06 +1100 | [diff] [blame] | 952 | if (ev == NULL) |
| 953 | return -ENOMEM; |
| 954 | |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 955 | ev->attr.attr = *attr; |
| 956 | memcpy(ev->attr.id, id, ids * sizeof(u64)); |
| 957 | |
| 958 | ev->attr.header.type = PERF_RECORD_HEADER_ATTR; |
| 959 | ev->attr.header.size = size; |
| 960 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 961 | err = process(ev, NULL, session); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 962 | |
| 963 | free(ev); |
| 964 | |
| 965 | return err; |
| 966 | } |
| 967 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 968 | int perf_session__synthesize_attrs(struct perf_session *session, |
| 969 | perf_event__handler_t process) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 970 | { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 971 | struct perf_evsel *attr; |
| 972 | int err = 0; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 973 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 974 | list_for_each_entry(attr, &session->evlist->entries, node) { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 975 | err = perf_event__synthesize_attr(&attr->attr, attr->ids, |
| 976 | attr->id, process, session); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 977 | if (err) { |
| 978 | pr_debug("failed to create perf header attribute\n"); |
| 979 | return err; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | return err; |
| 984 | } |
| 985 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 986 | int perf_event__process_attr(union perf_event *event, |
| 987 | struct perf_session *session) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 988 | { |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 989 | unsigned int i, ids, n_ids; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 990 | struct perf_evsel *evsel; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 991 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 992 | if (session->evlist == NULL) { |
| 993 | session->evlist = perf_evlist__new(NULL, NULL); |
| 994 | if (session->evlist == NULL) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 995 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 996 | } |
| 997 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 998 | evsel = perf_evsel__new(&event->attr.attr, |
| 999 | session->evlist->nr_entries); |
| 1000 | if (evsel == NULL) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 1001 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 1002 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1003 | perf_evlist__add(session->evlist, evsel); |
| 1004 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1005 | ids = event->header.size; |
| 1006 | ids -= (void *)&event->attr.id - (void *)event; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 1007 | n_ids = ids / sizeof(u64); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1008 | /* |
| 1009 | * We don't have the cpu and thread maps on the header, so |
| 1010 | * for allocating the perf_sample_id table we fake 1 cpu and |
| 1011 | * hattr->ids threads. |
| 1012 | */ |
| 1013 | if (perf_evsel__alloc_id(evsel, 1, n_ids)) |
| 1014 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 1015 | |
| 1016 | for (i = 0; i < n_ids; i++) { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1017 | perf_evlist__id_add(session->evlist, evsel, 0, i, |
| 1018 | event->attr.id[i]); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | perf_session__update_sample_type(session); |
| 1022 | |
| 1023 | return 0; |
| 1024 | } |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 1025 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1026 | int perf_event__synthesize_event_type(u64 event_id, char *name, |
| 1027 | perf_event__handler_t process, |
| 1028 | struct perf_session *session) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 1029 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1030 | union perf_event ev; |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 1031 | size_t size = 0; |
| 1032 | int err = 0; |
| 1033 | |
| 1034 | memset(&ev, 0, sizeof(ev)); |
| 1035 | |
| 1036 | ev.event_type.event_type.event_id = event_id; |
| 1037 | memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME); |
| 1038 | strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1); |
| 1039 | |
| 1040 | ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE; |
| 1041 | size = strlen(name); |
| 1042 | size = ALIGN(size, sizeof(u64)); |
| 1043 | ev.event_type.header.size = sizeof(ev.event_type) - |
| 1044 | (sizeof(ev.event_type.event_type.name) - size); |
| 1045 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 1046 | err = process(&ev, NULL, session); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 1047 | |
| 1048 | return err; |
| 1049 | } |
| 1050 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1051 | int perf_event__synthesize_event_types(perf_event__handler_t process, |
| 1052 | struct perf_session *session) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 1053 | { |
| 1054 | struct perf_trace_event_type *type; |
| 1055 | int i, err = 0; |
| 1056 | |
| 1057 | for (i = 0; i < event_count; i++) { |
| 1058 | type = &events[i]; |
| 1059 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1060 | err = perf_event__synthesize_event_type(type->event_id, |
| 1061 | type->name, process, |
| 1062 | session); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 1063 | if (err) { |
| 1064 | pr_debug("failed to create perf header event type\n"); |
| 1065 | return err; |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | return err; |
| 1070 | } |
| 1071 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1072 | int perf_event__process_event_type(union perf_event *event, |
| 1073 | struct perf_session *session __unused) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 1074 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1075 | if (perf_header__push_event(event->event_type.event_type.event_id, |
| 1076 | event->event_type.event_type.name) < 0) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 1077 | return -ENOMEM; |
| 1078 | |
| 1079 | return 0; |
| 1080 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 1081 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1082 | int perf_event__synthesize_tracing_data(int fd, struct perf_evlist *evlist, |
| 1083 | perf_event__handler_t process, |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 1084 | struct perf_session *session __unused) |
| 1085 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1086 | union perf_event ev; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 1087 | ssize_t size = 0, aligned_size = 0, padding; |
Kyle McMartin | fb7d0b3 | 2011-01-24 11:13:04 -0500 | [diff] [blame] | 1088 | int err __used = 0; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 1089 | |
| 1090 | memset(&ev, 0, sizeof(ev)); |
| 1091 | |
| 1092 | ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 1093 | size = read_tracing_data_size(fd, &evlist->entries); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 1094 | if (size <= 0) |
| 1095 | return size; |
| 1096 | aligned_size = ALIGN(size, sizeof(u64)); |
| 1097 | padding = aligned_size - size; |
| 1098 | ev.tracing_data.header.size = sizeof(ev.tracing_data); |
| 1099 | ev.tracing_data.size = aligned_size; |
| 1100 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 1101 | process(&ev, NULL, session); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 1102 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 1103 | err = read_tracing_data(fd, &evlist->entries); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 1104 | write_padded(fd, NULL, 0, padding); |
| 1105 | |
| 1106 | return aligned_size; |
| 1107 | } |
| 1108 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1109 | int perf_event__process_tracing_data(union perf_event *event, |
| 1110 | struct perf_session *session) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 1111 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1112 | ssize_t size_read, padding, size = event->tracing_data.size; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 1113 | off_t offset = lseek(session->fd, 0, SEEK_CUR); |
| 1114 | char buf[BUFSIZ]; |
| 1115 | |
| 1116 | /* setup for reading amidst mmap */ |
| 1117 | lseek(session->fd, offset + sizeof(struct tracing_data_event), |
| 1118 | SEEK_SET); |
| 1119 | |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 1120 | size_read = trace_report(session->fd, session->repipe); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 1121 | |
| 1122 | padding = ALIGN(size_read, sizeof(u64)) - size_read; |
| 1123 | |
| 1124 | if (read(session->fd, buf, padding) < 0) |
| 1125 | die("reading input file"); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 1126 | if (session->repipe) { |
| 1127 | int retw = write(STDOUT_FILENO, buf, padding); |
| 1128 | if (retw <= 0 || retw != padding) |
| 1129 | die("repiping tracing data padding"); |
| 1130 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 1131 | |
| 1132 | if (size_read + padding != size) |
| 1133 | die("tracing data size mismatch"); |
| 1134 | |
| 1135 | return size_read + padding; |
| 1136 | } |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 1137 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1138 | int perf_event__synthesize_build_id(struct dso *pos, u16 misc, |
| 1139 | perf_event__handler_t process, |
| 1140 | struct machine *machine, |
| 1141 | struct perf_session *session) |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 1142 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1143 | union perf_event ev; |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 1144 | size_t len; |
| 1145 | int err = 0; |
| 1146 | |
| 1147 | if (!pos->hit) |
| 1148 | return err; |
| 1149 | |
| 1150 | memset(&ev, 0, sizeof(ev)); |
| 1151 | |
| 1152 | len = pos->long_name_len + 1; |
| 1153 | len = ALIGN(len, NAME_ALIGN); |
| 1154 | memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id)); |
| 1155 | ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID; |
| 1156 | ev.build_id.header.misc = misc; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1157 | ev.build_id.pid = machine->pid; |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 1158 | ev.build_id.header.size = sizeof(ev.build_id) + len; |
| 1159 | memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len); |
| 1160 | |
Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 1161 | err = process(&ev, NULL, session); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 1162 | |
| 1163 | return err; |
| 1164 | } |
| 1165 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1166 | int perf_event__process_build_id(union perf_event *event, |
| 1167 | struct perf_session *session) |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 1168 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 1169 | __event_process_build_id(&event->build_id, |
| 1170 | event->build_id.filename, |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1171 | session); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 1172 | return 0; |
| 1173 | } |
Stephane Eranian | a1ac1d3 | 2010-06-17 11:39:01 +0200 | [diff] [blame] | 1174 | |
| 1175 | void disable_buildid_cache(void) |
| 1176 | { |
| 1177 | no_buildid_cache = true; |
| 1178 | } |