perf tools: Encode kernel module mappings in perf.data
[linux-2.6.git] / tools / perf / util / session.c
1 #include <linux/kernel.h>
2
3 #include <unistd.h>
4 #include <sys/types.h>
5
6 #include "session.h"
7 #include "sort.h"
8 #include "util.h"
9
10 static int perf_session__open(struct perf_session *self, bool force)
11 {
12         struct stat input_stat;
13
14         self->fd = open(self->filename, O_RDONLY);
15         if (self->fd < 0) {
16                 pr_err("failed to open file: %s", self->filename);
17                 if (!strcmp(self->filename, "perf.data"))
18                         pr_err("  (try 'perf record' first)");
19                 pr_err("\n");
20                 return -errno;
21         }
22
23         if (fstat(self->fd, &input_stat) < 0)
24                 goto out_close;
25
26         if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
27                 pr_err("file %s not owned by current user or root\n",
28                        self->filename);
29                 goto out_close;
30         }
31
32         if (!input_stat.st_size) {
33                 pr_info("zero-sized file (%s), nothing to do!\n",
34                         self->filename);
35                 goto out_close;
36         }
37
38         if (perf_header__read(&self->header, self->fd) < 0) {
39                 pr_err("incompatible file format");
40                 goto out_close;
41         }
42
43         self->size = input_stat.st_size;
44         return 0;
45
46 out_close:
47         close(self->fd);
48         self->fd = -1;
49         return -1;
50 }
51
52 struct perf_session *perf_session__new(const char *filename, int mode, bool force)
53 {
54         size_t len = filename ? strlen(filename) + 1 : 0;
55         struct perf_session *self = zalloc(sizeof(*self) + len);
56
57         if (self == NULL)
58                 goto out;
59
60         if (perf_header__init(&self->header) < 0)
61                 goto out_free;
62
63         memcpy(self->filename, filename, len);
64         self->threads = RB_ROOT;
65         self->last_match = NULL;
66         self->mmap_window = 32;
67         self->cwd = NULL;
68         self->cwdlen = 0;
69         self->unknown_events = 0;
70         map_groups__init(&self->kmaps);
71
72         if (mode == O_RDONLY && perf_session__open(self, force) < 0)
73                 goto out_delete;
74
75         self->sample_type = perf_header__sample_type(&self->header);
76 out:
77         return self;
78 out_free:
79         free(self);
80         return NULL;
81 out_delete:
82         perf_session__delete(self);
83         return NULL;
84 }
85
86 void perf_session__delete(struct perf_session *self)
87 {
88         perf_header__exit(&self->header);
89         close(self->fd);
90         free(self->cwd);
91         free(self);
92 }
93
94 static bool symbol__match_parent_regex(struct symbol *sym)
95 {
96         if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
97                 return 1;
98
99         return 0;
100 }
101
102 struct symbol **perf_session__resolve_callchain(struct perf_session *self,
103                                                 struct thread *thread,
104                                                 struct ip_callchain *chain,
105                                                 struct symbol **parent)
106 {
107         u8 cpumode = PERF_RECORD_MISC_USER;
108         struct symbol **syms = NULL;
109         unsigned int i;
110
111         if (symbol_conf.use_callchain) {
112                 syms = calloc(chain->nr, sizeof(*syms));
113                 if (!syms) {
114                         fprintf(stderr, "Can't allocate memory for symbols\n");
115                         exit(-1);
116                 }
117         }
118
119         for (i = 0; i < chain->nr; i++) {
120                 u64 ip = chain->ips[i];
121                 struct addr_location al;
122
123                 if (ip >= PERF_CONTEXT_MAX) {
124                         switch (ip) {
125                         case PERF_CONTEXT_HV:
126                                 cpumode = PERF_RECORD_MISC_HYPERVISOR;  break;
127                         case PERF_CONTEXT_KERNEL:
128                                 cpumode = PERF_RECORD_MISC_KERNEL;      break;
129                         case PERF_CONTEXT_USER:
130                                 cpumode = PERF_RECORD_MISC_USER;        break;
131                         default:
132                                 break;
133                         }
134                         continue;
135                 }
136
137                 thread__find_addr_location(thread, self, cpumode,
138                                            MAP__FUNCTION, ip, &al, NULL);
139                 if (al.sym != NULL) {
140                         if (sort__has_parent && !*parent &&
141                             symbol__match_parent_regex(al.sym))
142                                 *parent = al.sym;
143                         if (!symbol_conf.use_callchain)
144                                 break;
145                         syms[i] = al.sym;
146                 }
147         }
148
149         return syms;
150 }
151
152 static int process_event_stub(event_t *event __used,
153                               struct perf_session *session __used)
154 {
155         dump_printf(": unhandled!\n");
156         return 0;
157 }
158
159 static void perf_event_ops__fill_defaults(struct perf_event_ops *handler)
160 {
161         if (handler->sample == NULL)
162                 handler->sample = process_event_stub;
163         if (handler->mmap == NULL)
164                 handler->mmap = process_event_stub;
165         if (handler->comm == NULL)
166                 handler->comm = process_event_stub;
167         if (handler->fork == NULL)
168                 handler->fork = process_event_stub;
169         if (handler->exit == NULL)
170                 handler->exit = process_event_stub;
171         if (handler->lost == NULL)
172                 handler->lost = process_event_stub;
173         if (handler->read == NULL)
174                 handler->read = process_event_stub;
175         if (handler->throttle == NULL)
176                 handler->throttle = process_event_stub;
177         if (handler->unthrottle == NULL)
178                 handler->unthrottle = process_event_stub;
179 }
180
181 static const char *event__name[] = {
182         [0]                      = "TOTAL",
183         [PERF_RECORD_MMAP]       = "MMAP",
184         [PERF_RECORD_LOST]       = "LOST",
185         [PERF_RECORD_COMM]       = "COMM",
186         [PERF_RECORD_EXIT]       = "EXIT",
187         [PERF_RECORD_THROTTLE]   = "THROTTLE",
188         [PERF_RECORD_UNTHROTTLE] = "UNTHROTTLE",
189         [PERF_RECORD_FORK]       = "FORK",
190         [PERF_RECORD_READ]       = "READ",
191         [PERF_RECORD_SAMPLE]     = "SAMPLE",
192 };
193
194 unsigned long event__total[PERF_RECORD_MAX];
195
196 void event__print_totals(void)
197 {
198         int i;
199         for (i = 0; i < PERF_RECORD_MAX; ++i)
200                 pr_info("%10s events: %10ld\n",
201                         event__name[i], event__total[i]);
202 }
203
204 static int perf_session__process_event(struct perf_session *self,
205                                        event_t *event,
206                                        struct perf_event_ops *ops,
207                                        unsigned long offset, unsigned long head)
208 {
209         trace_event(event);
210
211         if (event->header.type < PERF_RECORD_MAX) {
212                 dump_printf("%p [%p]: PERF_RECORD_%s",
213                             (void *)(offset + head),
214                             (void *)(long)(event->header.size),
215                             event__name[event->header.type]);
216                 ++event__total[0];
217                 ++event__total[event->header.type];
218         }
219
220         switch (event->header.type) {
221         case PERF_RECORD_SAMPLE:
222                 return ops->sample(event, self);
223         case PERF_RECORD_MMAP:
224                 return ops->mmap(event, self);
225         case PERF_RECORD_COMM:
226                 return ops->comm(event, self);
227         case PERF_RECORD_FORK:
228                 return ops->fork(event, self);
229         case PERF_RECORD_EXIT:
230                 return ops->exit(event, self);
231         case PERF_RECORD_LOST:
232                 return ops->lost(event, self);
233         case PERF_RECORD_READ:
234                 return ops->read(event, self);
235         case PERF_RECORD_THROTTLE:
236                 return ops->throttle(event, self);
237         case PERF_RECORD_UNTHROTTLE:
238                 return ops->unthrottle(event, self);
239         default:
240                 self->unknown_events++;
241                 return -1;
242         }
243 }
244
245 int perf_header__read_build_ids(int input, u64 offset, u64 size)
246 {
247         struct build_id_event bev;
248         char filename[PATH_MAX];
249         u64 limit = offset + size;
250         int err = -1;
251
252         while (offset < limit) {
253                 struct dso *dso;
254                 ssize_t len;
255                 struct list_head *head = &dsos__user;
256
257                 if (read(input, &bev, sizeof(bev)) != sizeof(bev))
258                         goto out;
259
260                 len = bev.header.size - sizeof(bev);
261                 if (read(input, filename, len) != len)
262                         goto out;
263
264                 if (bev.header.misc & PERF_RECORD_MISC_KERNEL)
265                         head = &dsos__kernel;
266
267                 dso = __dsos__findnew(head, filename);
268                 if (dso != NULL) {
269                         dso__set_build_id(dso, &bev.build_id);
270                         if (head == &dsos__kernel && filename[0] == '[')
271                                 dso->kernel = 1;
272                 }
273
274                 offset += bev.header.size;
275         }
276         err = 0;
277 out:
278         return err;
279 }
280
281 static struct thread *perf_session__register_idle_thread(struct perf_session *self)
282 {
283         struct thread *thread = perf_session__findnew(self, 0);
284
285         if (thread == NULL || thread__set_comm(thread, "swapper")) {
286                 pr_err("problem inserting idle task.\n");
287                 thread = NULL;
288         }
289
290         return thread;
291 }
292
293 int perf_session__process_events(struct perf_session *self,
294                                  struct perf_event_ops *ops)
295 {
296         int err;
297         unsigned long head, shift;
298         unsigned long offset = 0;
299         size_t  page_size;
300         event_t *event;
301         uint32_t size;
302         char *buf;
303
304         if (perf_session__register_idle_thread(self) == NULL)
305                 return -ENOMEM;
306
307         perf_event_ops__fill_defaults(ops);
308
309         page_size = getpagesize();
310
311         head = self->header.data_offset;
312
313         if (!symbol_conf.full_paths) {
314                 char bf[PATH_MAX];
315
316                 if (getcwd(bf, sizeof(bf)) == NULL) {
317                         err = -errno;
318 out_getcwd_err:
319                         pr_err("failed to get the current directory\n");
320                         goto out_err;
321                 }
322                 self->cwd = strdup(bf);
323                 if (self->cwd == NULL) {
324                         err = -ENOMEM;
325                         goto out_getcwd_err;
326                 }
327                 self->cwdlen = strlen(self->cwd);
328         }
329
330         shift = page_size * (head / page_size);
331         offset += shift;
332         head -= shift;
333
334 remap:
335         buf = mmap(NULL, page_size * self->mmap_window, PROT_READ,
336                    MAP_SHARED, self->fd, offset);
337         if (buf == MAP_FAILED) {
338                 pr_err("failed to mmap file\n");
339                 err = -errno;
340                 goto out_err;
341         }
342
343 more:
344         event = (event_t *)(buf + head);
345
346         size = event->header.size;
347         if (size == 0)
348                 size = 8;
349
350         if (head + event->header.size >= page_size * self->mmap_window) {
351                 int munmap_ret;
352
353                 shift = page_size * (head / page_size);
354
355                 munmap_ret = munmap(buf, page_size * self->mmap_window);
356                 assert(munmap_ret == 0);
357
358                 offset += shift;
359                 head -= shift;
360                 goto remap;
361         }
362
363         size = event->header.size;
364
365         dump_printf("\n%p [%p]: event: %d\n",
366                         (void *)(offset + head),
367                         (void *)(long)event->header.size,
368                         event->header.type);
369
370         if (size == 0 ||
371             perf_session__process_event(self, event, ops, offset, head) < 0) {
372                 dump_printf("%p [%p]: skipping unknown header type: %d\n",
373                             (void *)(offset + head),
374                             (void *)(long)(event->header.size),
375                             event->header.type);
376                 /*
377                  * assume we lost track of the stream, check alignment, and
378                  * increment a single u64 in the hope to catch on again 'soon'.
379                  */
380                 if (unlikely(head & 7))
381                         head &= ~7ULL;
382
383                 size = 8;
384         }
385
386         head += size;
387
388         if (offset + head >= self->header.data_offset + self->header.data_size)
389                 goto done;
390
391         if (offset + head < self->size)
392                 goto more;
393 done:
394         err = 0;
395 out_err:
396         return err;
397 }
398
399 bool perf_session__has_traces(struct perf_session *self, const char *msg)
400 {
401         if (!(self->sample_type & PERF_SAMPLE_RAW)) {
402                 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
403                 return false;
404         }
405
406         return true;
407 }
408
409 int perf_session__set_kallsyms_ref_reloc_sym(struct perf_session *self,
410                                              const char *symbol_name,
411                                              u64 addr)
412 {
413         char *bracket;
414
415         self->ref_reloc_sym.name = strdup(symbol_name);
416         if (self->ref_reloc_sym.name == NULL)
417                 return -ENOMEM;
418
419         bracket = strchr(self->ref_reloc_sym.name, ']');
420         if (bracket)
421                 *bracket = '\0';
422
423         self->ref_reloc_sym.addr = addr;
424         return 0;
425 }
426
427 static u64 map__reloc_map_ip(struct map *map, u64 ip)
428 {
429         return ip + (s64)map->pgoff;
430 }
431
432 static u64 map__reloc_unmap_ip(struct map *map, u64 ip)
433 {
434         return ip - (s64)map->pgoff;
435 }
436
437 void perf_session__reloc_vmlinux_maps(struct perf_session *self,
438                                       u64 unrelocated_addr)
439 {
440         enum map_type type;
441         s64 reloc = unrelocated_addr - self->ref_reloc_sym.addr;
442
443         if (!reloc)
444                 return;
445
446         for (type = 0; type < MAP__NR_TYPES; ++type) {
447                 struct map *map = self->vmlinux_maps[type];
448
449                 map->map_ip = map__reloc_map_ip;
450                 map->unmap_ip = map__reloc_unmap_ip;
451                 map->pgoff = reloc;
452         }
453 }