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