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