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 | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 102 | #define MAX_EVENT_NAME 64 |
| 103 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 104 | struct perf_trace_event_type { |
| 105 | u64 event_id; |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 106 | char name[MAX_EVENT_NAME]; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | static int event_count; |
| 110 | static struct perf_trace_event_type *events; |
| 111 | |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 112 | int perf_header__push_event(u64 id, const char *name) |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 113 | { |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 114 | if (strlen(name) > MAX_EVENT_NAME) |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 115 | pr_warning("Event %s will be truncated\n", name); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 116 | |
| 117 | if (!events) { |
| 118 | events = malloc(sizeof(struct perf_trace_event_type)); |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 119 | if (events == NULL) |
| 120 | return -ENOMEM; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 121 | } else { |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 122 | struct perf_trace_event_type *nevents; |
| 123 | |
| 124 | nevents = realloc(events, (event_count + 1) * sizeof(*events)); |
| 125 | if (nevents == NULL) |
| 126 | return -ENOMEM; |
| 127 | events = nevents; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 128 | } |
| 129 | memset(&events[event_count], 0, sizeof(struct perf_trace_event_type)); |
| 130 | events[event_count].event_id = id; |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 131 | strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 132 | event_count++; |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 133 | return 0; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | char *perf_header__find_event(u64 id) |
| 137 | { |
| 138 | int i; |
| 139 | for (i = 0 ; i < event_count; i++) { |
| 140 | if (events[i].event_id == id) |
| 141 | return events[i].name; |
| 142 | } |
| 143 | return NULL; |
| 144 | } |
| 145 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 146 | static const char *__perf_magic = "PERFFILE"; |
| 147 | |
| 148 | #define PERF_MAGIC (*(u64 *)__perf_magic) |
| 149 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 150 | struct perf_file_attr { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 151 | struct perf_event_attr attr; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 152 | struct perf_file_section ids; |
| 153 | }; |
| 154 | |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 155 | void perf_header__set_feat(struct perf_header *self, int feat) |
| 156 | { |
| 157 | set_bit(feat, self->adds_features); |
| 158 | } |
| 159 | |
| 160 | bool perf_header__has_feat(const struct perf_header *self, int feat) |
| 161 | { |
| 162 | return test_bit(feat, self->adds_features); |
| 163 | } |
| 164 | |
Arnaldo Carvalho de Melo | 3726cc7 | 2009-11-17 01:18:12 -0200 | [diff] [blame] | 165 | static int do_write(int fd, const void *buf, size_t size) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 166 | { |
| 167 | while (size) { |
| 168 | int ret = write(fd, buf, size); |
| 169 | |
| 170 | if (ret < 0) |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 171 | return -errno; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 172 | |
| 173 | size -= ret; |
| 174 | buf += ret; |
| 175 | } |
Arnaldo Carvalho de Melo | 3726cc7 | 2009-11-17 01:18:12 -0200 | [diff] [blame] | 176 | |
| 177 | return 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Arnaldo Carvalho de Melo | f92cb24 | 2010-01-04 16:19:28 -0200 | [diff] [blame] | 180 | #define NAME_ALIGN 64 |
| 181 | |
| 182 | static int write_padded(int fd, const void *bf, size_t count, |
| 183 | size_t count_aligned) |
| 184 | { |
| 185 | static const char zero_buf[NAME_ALIGN]; |
| 186 | int err = do_write(fd, bf, count); |
| 187 | |
| 188 | if (!err) |
| 189 | err = do_write(fd, zero_buf, count_aligned - count); |
| 190 | |
| 191 | return err; |
| 192 | } |
| 193 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 194 | #define dsos__for_each_with_build_id(pos, head) \ |
| 195 | list_for_each_entry(pos, head, node) \ |
| 196 | if (!pos->has_build_id) \ |
| 197 | continue; \ |
| 198 | else |
| 199 | |
Arnaldo Carvalho de Melo | a89e5abe | 2010-01-07 19:59:39 -0200 | [diff] [blame] | 200 | static int __dsos__write_buildid_table(struct list_head *head, u16 misc, int fd) |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 201 | { |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 202 | struct dso *pos; |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 203 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 204 | dsos__for_each_with_build_id(pos, head) { |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 205 | int err; |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 206 | struct build_id_event b; |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 207 | size_t len; |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 208 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 209 | if (!pos->hit) |
| 210 | continue; |
| 211 | len = pos->long_name_len + 1; |
OGAWA Hirofumi | 7691b1e | 2009-12-06 20:10:49 +0900 | [diff] [blame] | 212 | len = ALIGN(len, NAME_ALIGN); |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 213 | memset(&b, 0, sizeof(b)); |
| 214 | memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id)); |
Arnaldo Carvalho de Melo | a89e5abe | 2010-01-07 19:59:39 -0200 | [diff] [blame] | 215 | b.header.misc = misc; |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 216 | b.header.size = sizeof(b) + len; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 217 | err = do_write(fd, &b, sizeof(b)); |
| 218 | if (err < 0) |
| 219 | return err; |
Arnaldo Carvalho de Melo | f92cb24 | 2010-01-04 16:19:28 -0200 | [diff] [blame] | 220 | err = write_padded(fd, pos->long_name, |
| 221 | pos->long_name_len + 1, len); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 222 | if (err < 0) |
| 223 | return err; |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 224 | } |
Arnaldo Carvalho de Melo | 3726cc7 | 2009-11-17 01:18:12 -0200 | [diff] [blame] | 225 | |
| 226 | return 0; |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 229 | static int dsos__write_buildid_table(int fd) |
| 230 | { |
Arnaldo Carvalho de Melo | a89e5abe | 2010-01-07 19:59:39 -0200 | [diff] [blame] | 231 | int err = __dsos__write_buildid_table(&dsos__kernel, |
| 232 | PERF_RECORD_MISC_KERNEL, fd); |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 233 | if (err == 0) |
Arnaldo Carvalho de Melo | a89e5abe | 2010-01-07 19:59:39 -0200 | [diff] [blame] | 234 | err = __dsos__write_buildid_table(&dsos__user, |
| 235 | PERF_RECORD_MISC_USER, fd); |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 236 | return err; |
| 237 | } |
| 238 | |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 239 | int build_id_cache__add_s(const char *sbuild_id, const char *debugdir, |
| 240 | const char *name, bool is_kallsyms) |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 241 | { |
| 242 | const size_t size = PATH_MAX; |
| 243 | char *filename = malloc(size), |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 244 | *linkname = malloc(size), *targetname; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 245 | int len, err = -1; |
| 246 | |
| 247 | if (filename == NULL || linkname == NULL) |
| 248 | goto out_free; |
| 249 | |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 250 | len = snprintf(filename, size, "%s%s%s", |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 251 | debugdir, is_kallsyms ? "/" : "", name); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 252 | if (mkdir_p(filename, 0755)) |
| 253 | goto out_free; |
| 254 | |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 255 | snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 256 | |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 257 | if (access(filename, F_OK)) { |
| 258 | if (is_kallsyms) { |
| 259 | if (copyfile("/proc/kallsyms", filename)) |
| 260 | goto out_free; |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 261 | } else if (link(name, filename) && copyfile(name, filename)) |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 262 | goto out_free; |
| 263 | } |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 264 | |
| 265 | len = snprintf(linkname, size, "%s/.build-id/%.2s", |
| 266 | debugdir, sbuild_id); |
| 267 | |
| 268 | if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) |
| 269 | goto out_free; |
| 270 | |
| 271 | snprintf(linkname + len, size - len, "/%s", sbuild_id + 2); |
| 272 | targetname = filename + strlen(debugdir) - 5; |
| 273 | memcpy(targetname, "../..", 5); |
| 274 | |
| 275 | if (symlink(targetname, linkname) == 0) |
| 276 | err = 0; |
| 277 | out_free: |
| 278 | free(filename); |
| 279 | free(linkname); |
| 280 | return err; |
| 281 | } |
| 282 | |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 283 | static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size, |
| 284 | const char *name, const char *debugdir, |
| 285 | bool is_kallsyms) |
| 286 | { |
| 287 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 288 | |
| 289 | build_id__sprintf(build_id, build_id_size, sbuild_id); |
| 290 | |
| 291 | return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms); |
| 292 | } |
| 293 | |
| 294 | int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir) |
| 295 | { |
| 296 | const size_t size = PATH_MAX; |
| 297 | char *filename = malloc(size), |
| 298 | *linkname = malloc(size); |
| 299 | int err = -1; |
| 300 | |
| 301 | if (filename == NULL || linkname == NULL) |
| 302 | goto out_free; |
| 303 | |
| 304 | snprintf(linkname, size, "%s/.build-id/%.2s/%s", |
| 305 | debugdir, sbuild_id, sbuild_id + 2); |
| 306 | |
| 307 | if (access(linkname, F_OK)) |
| 308 | goto out_free; |
| 309 | |
| 310 | if (readlink(linkname, filename, size) < 0) |
| 311 | goto out_free; |
| 312 | |
| 313 | if (unlink(linkname)) |
| 314 | goto out_free; |
| 315 | |
| 316 | /* |
| 317 | * Since the link is relative, we must make it absolute: |
| 318 | */ |
| 319 | snprintf(linkname, size, "%s/.build-id/%.2s/%s", |
| 320 | debugdir, sbuild_id, filename); |
| 321 | |
| 322 | if (unlink(linkname)) |
| 323 | goto out_free; |
| 324 | |
| 325 | err = 0; |
| 326 | out_free: |
| 327 | free(filename); |
| 328 | free(linkname); |
| 329 | return err; |
| 330 | } |
| 331 | |
| 332 | static int dso__cache_build_id(struct dso *self, const char *debugdir) |
| 333 | { |
| 334 | bool is_kallsyms = self->kernel && self->long_name[0] != '/'; |
| 335 | |
| 336 | return build_id_cache__add_b(self->build_id, sizeof(self->build_id), |
| 337 | self->long_name, debugdir, is_kallsyms); |
| 338 | } |
| 339 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 340 | static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir) |
| 341 | { |
| 342 | struct dso *pos; |
| 343 | int err = 0; |
| 344 | |
| 345 | dsos__for_each_with_build_id(pos, head) |
| 346 | if (dso__cache_build_id(pos, debugdir)) |
| 347 | err = -1; |
| 348 | |
| 349 | return err; |
| 350 | } |
| 351 | |
| 352 | static int dsos__cache_build_ids(void) |
| 353 | { |
| 354 | int err_kernel, err_user; |
| 355 | char debugdir[PATH_MAX]; |
| 356 | |
| 357 | snprintf(debugdir, sizeof(debugdir), "%s/%s", getenv("HOME"), |
| 358 | DEBUG_CACHE_DIR); |
| 359 | |
| 360 | if (mkdir(debugdir, 0755) != 0 && errno != EEXIST) |
| 361 | return -1; |
| 362 | |
| 363 | err_kernel = __dsos__cache_build_ids(&dsos__kernel, debugdir); |
| 364 | err_user = __dsos__cache_build_ids(&dsos__user, debugdir); |
| 365 | return err_kernel || err_user ? -1 : 0; |
| 366 | } |
| 367 | |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 368 | static int perf_header__adds_write(struct perf_header *self, int fd) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 369 | { |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 370 | int nr_sections; |
| 371 | struct perf_file_section *feat_sec; |
| 372 | int sec_size; |
| 373 | u64 sec_start; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 374 | int idx = 0, err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 375 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 376 | if (dsos__read_build_ids(true)) |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 377 | perf_header__set_feat(self, HEADER_BUILD_ID); |
| 378 | |
| 379 | nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS); |
| 380 | if (!nr_sections) |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 381 | return 0; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 382 | |
| 383 | feat_sec = calloc(sizeof(*feat_sec), nr_sections); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 384 | if (feat_sec == NULL) |
| 385 | return -ENOMEM; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 386 | |
| 387 | sec_size = sizeof(*feat_sec) * nr_sections; |
| 388 | |
| 389 | sec_start = self->data_offset + self->data_size; |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 390 | lseek(fd, sec_start + sec_size, SEEK_SET); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 391 | |
Frederic Weisbecker | 3e13ab2 | 2009-11-11 04:51:06 +0100 | [diff] [blame] | 392 | if (perf_header__has_feat(self, HEADER_TRACE_INFO)) { |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 393 | struct perf_file_section *trace_sec; |
| 394 | |
| 395 | trace_sec = &feat_sec[idx++]; |
| 396 | |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 397 | /* Write trace info */ |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 398 | trace_sec->offset = lseek(fd, 0, SEEK_CUR); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 399 | read_tracing_data(fd, attrs, nr_counters); |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 400 | trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset; |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 401 | } |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 402 | |
Frederic Weisbecker | 57f395a | 2009-11-11 04:51:04 +0100 | [diff] [blame] | 403 | |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 404 | if (perf_header__has_feat(self, HEADER_BUILD_ID)) { |
| 405 | struct perf_file_section *buildid_sec; |
| 406 | |
| 407 | buildid_sec = &feat_sec[idx++]; |
| 408 | |
| 409 | /* Write build-ids */ |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 410 | buildid_sec->offset = lseek(fd, 0, SEEK_CUR); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 411 | err = dsos__write_buildid_table(fd); |
| 412 | if (err < 0) { |
| 413 | pr_debug("failed to write buildid table\n"); |
| 414 | goto out_free; |
| 415 | } |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 416 | buildid_sec->size = lseek(fd, 0, SEEK_CUR) - |
| 417 | buildid_sec->offset; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 418 | dsos__cache_build_ids(); |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 419 | } |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 420 | |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 421 | lseek(fd, sec_start, SEEK_SET); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 422 | err = do_write(fd, feat_sec, sec_size); |
| 423 | if (err < 0) |
| 424 | pr_debug("failed to write feature section\n"); |
| 425 | out_free: |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 426 | free(feat_sec); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 427 | return err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 428 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 429 | |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 430 | 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] | 431 | { |
| 432 | struct perf_file_header f_header; |
| 433 | struct perf_file_attr f_attr; |
| 434 | struct perf_header_attr *attr; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 435 | int i, err; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 436 | |
| 437 | lseek(fd, sizeof(f_header), SEEK_SET); |
| 438 | |
| 439 | |
| 440 | for (i = 0; i < self->attrs; i++) { |
| 441 | attr = self->attr[i]; |
| 442 | |
| 443 | attr->id_offset = lseek(fd, 0, SEEK_CUR); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 444 | err = do_write(fd, attr->id, attr->ids * sizeof(u64)); |
| 445 | if (err < 0) { |
| 446 | pr_debug("failed to write perf header\n"); |
| 447 | return err; |
| 448 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | |
| 452 | self->attr_offset = lseek(fd, 0, SEEK_CUR); |
| 453 | |
| 454 | for (i = 0; i < self->attrs; i++) { |
| 455 | attr = self->attr[i]; |
| 456 | |
| 457 | f_attr = (struct perf_file_attr){ |
| 458 | .attr = attr->attr, |
| 459 | .ids = { |
| 460 | .offset = attr->id_offset, |
| 461 | .size = attr->ids * sizeof(u64), |
| 462 | } |
| 463 | }; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 464 | err = do_write(fd, &f_attr, sizeof(f_attr)); |
| 465 | if (err < 0) { |
| 466 | pr_debug("failed to write perf header attribute\n"); |
| 467 | return err; |
| 468 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 471 | self->event_offset = lseek(fd, 0, SEEK_CUR); |
| 472 | 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] | 473 | if (events) { |
| 474 | err = do_write(fd, events, self->event_size); |
| 475 | if (err < 0) { |
| 476 | pr_debug("failed to write perf header events\n"); |
| 477 | return err; |
| 478 | } |
| 479 | } |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 480 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 481 | self->data_offset = lseek(fd, 0, SEEK_CUR); |
| 482 | |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 483 | if (at_exit) { |
| 484 | err = perf_header__adds_write(self, fd); |
| 485 | if (err < 0) |
| 486 | return err; |
| 487 | } |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 488 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 489 | f_header = (struct perf_file_header){ |
| 490 | .magic = PERF_MAGIC, |
| 491 | .size = sizeof(f_header), |
| 492 | .attr_size = sizeof(f_attr), |
| 493 | .attrs = { |
| 494 | .offset = self->attr_offset, |
| 495 | .size = self->attrs * sizeof(f_attr), |
| 496 | }, |
| 497 | .data = { |
| 498 | .offset = self->data_offset, |
| 499 | .size = self->data_size, |
| 500 | }, |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 501 | .event_types = { |
| 502 | .offset = self->event_offset, |
| 503 | .size = self->event_size, |
| 504 | }, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 505 | }; |
| 506 | |
Frederic Weisbecker | db9f11e | 2009-10-17 17:57:18 +0200 | [diff] [blame] | 507 | memcpy(&f_header.adds_features, &self->adds_features, sizeof(self->adds_features)); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 508 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 509 | lseek(fd, 0, SEEK_SET); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 510 | err = do_write(fd, &f_header, sizeof(f_header)); |
| 511 | if (err < 0) { |
| 512 | pr_debug("failed to write perf header\n"); |
| 513 | return err; |
| 514 | } |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 515 | lseek(fd, self->data_offset + self->data_size, SEEK_SET); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 516 | |
| 517 | self->frozen = 1; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 518 | return 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 519 | } |
| 520 | |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 521 | static int do_read(int fd, void *buf, size_t size) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 522 | { |
| 523 | while (size) { |
| 524 | int ret = read(fd, buf, size); |
| 525 | |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 526 | if (ret <= 0) |
| 527 | return -1; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 528 | |
| 529 | size -= ret; |
| 530 | buf += ret; |
| 531 | } |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 532 | |
| 533 | return 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 536 | static int perf_header__getbuffer64(struct perf_header *self, |
| 537 | int fd, void *buf, size_t size) |
| 538 | { |
| 539 | if (do_read(fd, buf, size)) |
| 540 | return -1; |
| 541 | |
| 542 | if (self->needs_swap) |
| 543 | mem_bswap_64(buf, size); |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 548 | int perf_header__process_sections(struct perf_header *self, int fd, |
| 549 | int (*process)(struct perf_file_section *self, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 550 | struct perf_header *ph, |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 551 | int feat, int fd)) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 552 | { |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 553 | struct perf_file_section *feat_sec; |
| 554 | int nr_sections; |
| 555 | int sec_size; |
| 556 | int idx = 0; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 557 | int err = -1, feat = 1; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 558 | |
| 559 | nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS); |
| 560 | if (!nr_sections) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 561 | return 0; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 562 | |
| 563 | feat_sec = calloc(sizeof(*feat_sec), nr_sections); |
| 564 | if (!feat_sec) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 565 | return -1; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 566 | |
| 567 | sec_size = sizeof(*feat_sec) * nr_sections; |
| 568 | |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 569 | lseek(fd, self->data_offset + self->data_size, SEEK_SET); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 570 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 571 | if (perf_header__getbuffer64(self, fd, feat_sec, sec_size)) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 572 | goto out_free; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 573 | |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 574 | err = 0; |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 575 | while (idx < nr_sections && feat < HEADER_LAST_FEATURE) { |
| 576 | if (perf_header__has_feat(self, feat)) { |
| 577 | struct perf_file_section *sec = &feat_sec[idx++]; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 578 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 579 | err = process(sec, self, feat, fd); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 580 | if (err < 0) |
| 581 | break; |
| 582 | } |
| 583 | ++feat; |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 584 | } |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 585 | out_free: |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 586 | free(feat_sec); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 587 | return err; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 588 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 589 | |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 590 | int perf_file_header__read(struct perf_file_header *self, |
| 591 | struct perf_header *ph, int fd) |
| 592 | { |
| 593 | lseek(fd, 0, SEEK_SET); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 594 | |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 595 | if (do_read(fd, self, sizeof(*self)) || |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 596 | memcmp(&self->magic, __perf_magic, sizeof(self->magic))) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 597 | return -1; |
| 598 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 599 | if (self->attr_size != sizeof(struct perf_file_attr)) { |
| 600 | u64 attr_size = bswap_64(self->attr_size); |
| 601 | |
| 602 | if (attr_size != sizeof(struct perf_file_attr)) |
| 603 | return -1; |
| 604 | |
| 605 | mem_bswap_64(self, offsetof(struct perf_file_header, |
| 606 | adds_features)); |
| 607 | ph->needs_swap = true; |
| 608 | } |
| 609 | |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 610 | if (self->size != sizeof(*self)) { |
| 611 | /* Support the previous format */ |
| 612 | if (self->size == offsetof(typeof(*self), adds_features)) |
| 613 | bitmap_zero(self->adds_features, HEADER_FEAT_BITS); |
| 614 | else |
| 615 | return -1; |
| 616 | } |
| 617 | |
| 618 | memcpy(&ph->adds_features, &self->adds_features, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 619 | sizeof(ph->adds_features)); |
| 620 | /* |
| 621 | * FIXME: hack that assumes that if we need swap the perf.data file |
| 622 | * may be coming from an arch with a different word-size, ergo different |
| 623 | * DEFINE_BITMAP format, investigate more later, but for now its mostly |
| 624 | * safe to assume that we have a build-id section. Trace files probably |
| 625 | * have several other issues in this realm anyway... |
| 626 | */ |
| 627 | if (ph->needs_swap) { |
| 628 | memset(&ph->adds_features, 0, sizeof(ph->adds_features)); |
| 629 | perf_header__set_feat(ph, HEADER_BUILD_ID); |
| 630 | } |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 631 | |
| 632 | ph->event_offset = self->event_types.offset; |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 633 | ph->event_size = self->event_types.size; |
| 634 | ph->data_offset = self->data.offset; |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 635 | ph->data_size = self->data.size; |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | 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] | 640 | struct perf_header *ph, |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 641 | int feat, int fd) |
| 642 | { |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 643 | if (lseek(fd, self->offset, SEEK_SET) == (off_t)-1) { |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 644 | pr_debug("Failed to lseek to %Ld offset for feature %d, " |
| 645 | "continuing...\n", self->offset, feat); |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | switch (feat) { |
| 650 | case HEADER_TRACE_INFO: |
| 651 | trace_report(fd); |
| 652 | break; |
| 653 | |
| 654 | case HEADER_BUILD_ID: |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 655 | 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] | 656 | pr_debug("Failed to read buildids, continuing...\n"); |
| 657 | break; |
| 658 | default: |
| 659 | pr_debug("unknown feature %d, continuing...\n", feat); |
| 660 | } |
| 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 665 | int perf_header__read(struct perf_header *self, int fd) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 666 | { |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 667 | struct perf_file_header f_header; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 668 | struct perf_file_attr f_attr; |
| 669 | u64 f_id; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 670 | int nr_attrs, nr_ids, i, j; |
| 671 | |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 672 | if (perf_file_header__read(&f_header, self, fd) < 0) { |
| 673 | pr_debug("incompatible file format\n"); |
| 674 | return -EINVAL; |
| 675 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 676 | |
| 677 | nr_attrs = f_header.attrs.size / sizeof(f_attr); |
| 678 | lseek(fd, f_header.attrs.offset, SEEK_SET); |
| 679 | |
| 680 | for (i = 0; i < nr_attrs; i++) { |
| 681 | struct perf_header_attr *attr; |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 682 | off_t tmp; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 683 | |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 684 | 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] | 685 | goto out_errno; |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 686 | |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 687 | tmp = lseek(fd, 0, SEEK_CUR); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 688 | |
| 689 | attr = perf_header_attr__new(&f_attr.attr); |
Arnaldo Carvalho de Melo | dc79c0f | 2009-11-16 19:30:26 -0200 | [diff] [blame] | 690 | if (attr == NULL) |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 691 | return -ENOMEM; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 692 | |
| 693 | nr_ids = f_attr.ids.size / sizeof(u64); |
| 694 | lseek(fd, f_attr.ids.offset, SEEK_SET); |
| 695 | |
| 696 | for (j = 0; j < nr_ids; j++) { |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 697 | 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] | 698 | goto out_errno; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 699 | |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 700 | if (perf_header_attr__add_id(attr, f_id) < 0) { |
| 701 | perf_header_attr__delete(attr); |
| 702 | return -ENOMEM; |
| 703 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 704 | } |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 705 | if (perf_header__add_attr(self, attr) < 0) { |
| 706 | perf_header_attr__delete(attr); |
| 707 | return -ENOMEM; |
| 708 | } |
Arnaldo Carvalho de Melo | 11deb1f | 2009-11-17 01:18:09 -0200 | [diff] [blame] | 709 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 710 | lseek(fd, tmp, SEEK_SET); |
| 711 | } |
| 712 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 713 | if (f_header.event_types.size) { |
| 714 | lseek(fd, f_header.event_types.offset, SEEK_SET); |
| 715 | events = malloc(f_header.event_types.size); |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 716 | if (events == NULL) |
| 717 | return -ENOMEM; |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 718 | if (perf_header__getbuffer64(self, fd, events, |
| 719 | f_header.event_types.size)) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 720 | goto out_errno; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 721 | event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type); |
| 722 | } |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 723 | |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 724 | perf_header__process_sections(self, fd, perf_file_section__process); |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 725 | |
Ingo Molnar | 2e01d17 | 2009-09-03 16:21:11 +0200 | [diff] [blame] | 726 | lseek(fd, self->data_offset, SEEK_SET); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 727 | |
| 728 | self->frozen = 1; |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 729 | return 0; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 730 | out_errno: |
| 731 | return -errno; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 732 | } |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 733 | |
| 734 | u64 perf_header__sample_type(struct perf_header *header) |
| 735 | { |
| 736 | u64 type = 0; |
| 737 | int i; |
| 738 | |
| 739 | for (i = 0; i < header->attrs; i++) { |
| 740 | struct perf_header_attr *attr = header->attr[i]; |
| 741 | |
| 742 | if (!type) |
| 743 | type = attr->attr.sample_type; |
| 744 | else if (type != attr->attr.sample_type) |
| 745 | die("non matching sample_type"); |
| 746 | } |
| 747 | |
| 748 | return type; |
| 749 | } |
| 750 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 751 | struct perf_event_attr * |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 752 | perf_header__find_attr(u64 id, struct perf_header *header) |
| 753 | { |
| 754 | int i; |
| 755 | |
| 756 | for (i = 0; i < header->attrs; i++) { |
| 757 | struct perf_header_attr *attr = header->attr[i]; |
| 758 | int j; |
| 759 | |
| 760 | for (j = 0; j < attr->ids; j++) { |
| 761 | if (attr->id[j] == id) |
| 762 | return &attr->attr; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | return NULL; |
| 767 | } |