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