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