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