perf symbols: Use the buildids if present
[linux-3.10.git] / tools / perf / util / data_map.c
1 #include "data_map.h"
2 #include "symbol.h"
3 #include "util.h"
4 #include "debug.h"
5
6
7 static struct perf_file_handler *curr_handler;
8 static unsigned long    mmap_window = 32;
9 static char             __cwd[PATH_MAX];
10
11 static int
12 process_event_stub(event_t *event __used,
13                    unsigned long offset __used,
14                    unsigned long head __used)
15 {
16         return 0;
17 }
18
19 void register_perf_file_handler(struct perf_file_handler *handler)
20 {
21         if (!handler->process_sample_event)
22                 handler->process_sample_event = process_event_stub;
23         if (!handler->process_mmap_event)
24                 handler->process_mmap_event = process_event_stub;
25         if (!handler->process_comm_event)
26                 handler->process_comm_event = process_event_stub;
27         if (!handler->process_fork_event)
28                 handler->process_fork_event = process_event_stub;
29         if (!handler->process_exit_event)
30                 handler->process_exit_event = process_event_stub;
31         if (!handler->process_lost_event)
32                 handler->process_lost_event = process_event_stub;
33         if (!handler->process_read_event)
34                 handler->process_read_event = process_event_stub;
35         if (!handler->process_throttle_event)
36                 handler->process_throttle_event = process_event_stub;
37         if (!handler->process_unthrottle_event)
38                 handler->process_unthrottle_event = process_event_stub;
39
40         curr_handler = handler;
41 }
42
43 static int
44 process_event(event_t *event, unsigned long offset, unsigned long head)
45 {
46         trace_event(event);
47
48         switch (event->header.type) {
49         case PERF_RECORD_SAMPLE:
50                 return curr_handler->process_sample_event(event, offset, head);
51         case PERF_RECORD_MMAP:
52                 return curr_handler->process_mmap_event(event, offset, head);
53         case PERF_RECORD_COMM:
54                 return curr_handler->process_comm_event(event, offset, head);
55         case PERF_RECORD_FORK:
56                 return curr_handler->process_fork_event(event, offset, head);
57         case PERF_RECORD_EXIT:
58                 return curr_handler->process_exit_event(event, offset, head);
59         case PERF_RECORD_LOST:
60                 return curr_handler->process_lost_event(event, offset, head);
61         case PERF_RECORD_READ:
62                 return curr_handler->process_read_event(event, offset, head);
63         case PERF_RECORD_THROTTLE:
64                 return curr_handler->process_throttle_event(event, offset, head);
65         case PERF_RECORD_UNTHROTTLE:
66                 return curr_handler->process_unthrottle_event(event, offset, head);
67         default:
68                 curr_handler->total_unknown++;
69                 return -1;
70         }
71 }
72
73 static int perf_header__read_build_ids(const struct perf_header *self,
74                                        int input, off_t file_size)
75 {
76         off_t offset = self->data_offset + self->data_size;
77         struct build_id_event bev;
78         char filename[PATH_MAX];
79         int err = -1;
80
81         if (lseek(input, offset, SEEK_SET) < 0)
82                 return -1;
83
84         while (offset < file_size) {
85                 struct dso *dso;
86                 ssize_t len;
87
88                 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
89                         goto out;
90
91                 len = bev.header.size - sizeof(bev);
92                 if (read(input, filename, len) != len)
93                         goto out;
94
95                 dso = dsos__findnew(filename);
96                 if (dso != NULL)
97                         dso__set_build_id(dso, &bev.build_id);
98
99                 offset += bev.header.size;
100         }
101         err = 0;
102 out:
103         return err;
104 }
105
106 int mmap_dispatch_perf_file(struct perf_header **pheader,
107                             const char *input_name,
108                             int force,
109                             int full_paths,
110                             int *cwdlen,
111                             char **cwd)
112 {
113         int ret, rc = EXIT_FAILURE;
114         struct perf_header *header;
115         unsigned long head, shift;
116         unsigned long offset = 0;
117         struct stat input_stat;
118         size_t  page_size;
119         u64 sample_type;
120         event_t *event;
121         uint32_t size;
122         int input;
123         char *buf;
124
125         if (!curr_handler)
126                 die("Forgot to register perf file handler");
127
128         page_size = getpagesize();
129
130         input = open(input_name, O_RDONLY);
131         if (input < 0) {
132                 fprintf(stderr, " failed to open file: %s", input_name);
133                 if (!strcmp(input_name, "perf.data"))
134                         fprintf(stderr, "  (try 'perf record' first)");
135                 fprintf(stderr, "\n");
136                 exit(-1);
137         }
138
139         ret = fstat(input, &input_stat);
140         if (ret < 0) {
141                 perror("failed to stat file");
142                 exit(-1);
143         }
144
145         if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
146                 fprintf(stderr, "file: %s not owned by current user or root\n",
147                         input_name);
148                 exit(-1);
149         }
150
151         if (!input_stat.st_size) {
152                 fprintf(stderr, "zero-sized file, nothing to do!\n");
153                 exit(0);
154         }
155
156         *pheader = perf_header__read(input);
157         header = *pheader;
158         head = header->data_offset;
159
160         sample_type = perf_header__sample_type(header);
161
162         if (curr_handler->sample_type_check)
163                 if (curr_handler->sample_type_check(sample_type) < 0)
164                         exit(-1);
165
166         if (perf_header__has_feat(header, HEADER_BUILD_ID) &&
167             perf_header__read_build_ids(header, input, input_stat.st_size))
168                 pr_debug("failed to read buildids, continuing...\n");
169
170         if (load_kernel(NULL) < 0) {
171                 perror("failed to load kernel symbols");
172                 return EXIT_FAILURE;
173         }
174
175         if (!full_paths) {
176                 if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
177                         perror("failed to get the current directory");
178                         return EXIT_FAILURE;
179                 }
180                 *cwd = __cwd;
181                 *cwdlen = strlen(*cwd);
182         } else {
183                 *cwd = NULL;
184                 *cwdlen = 0;
185         }
186
187         shift = page_size * (head / page_size);
188         offset += shift;
189         head -= shift;
190
191 remap:
192         buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
193                            MAP_SHARED, input, offset);
194         if (buf == MAP_FAILED) {
195                 perror("failed to mmap file");
196                 exit(-1);
197         }
198
199 more:
200         event = (event_t *)(buf + head);
201
202         size = event->header.size;
203         if (!size)
204                 size = 8;
205
206         if (head + event->header.size >= page_size * mmap_window) {
207                 int munmap_ret;
208
209                 shift = page_size * (head / page_size);
210
211                 munmap_ret = munmap(buf, page_size * mmap_window);
212                 assert(munmap_ret == 0);
213
214                 offset += shift;
215                 head -= shift;
216                 goto remap;
217         }
218
219         size = event->header.size;
220
221         dump_printf("\n%p [%p]: event: %d\n",
222                         (void *)(offset + head),
223                         (void *)(long)event->header.size,
224                         event->header.type);
225
226         if (!size || process_event(event, offset, head) < 0) {
227
228                 dump_printf("%p [%p]: skipping unknown header type: %d\n",
229                         (void *)(offset + head),
230                         (void *)(long)(event->header.size),
231                         event->header.type);
232
233                 /*
234                  * assume we lost track of the stream, check alignment, and
235                  * increment a single u64 in the hope to catch on again 'soon'.
236                  */
237
238                 if (unlikely(head & 7))
239                         head &= ~7ULL;
240
241                 size = 8;
242         }
243
244         head += size;
245
246         if (offset + head >= header->data_offset + header->data_size)
247                 goto done;
248
249         if (offset + head < (unsigned long)input_stat.st_size)
250                 goto more;
251
252 done:
253         rc = EXIT_SUCCESS;
254         close(input);
255
256         return rc;
257 }
258
259