blob: a9ff49a4edea37ca9d2fa8a2e45a0671a4a87453 [file] [log] [blame]
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001#include "util/util.h"
Ingo Molnar16f762a2009-05-27 09:10:38 +02002#include "builtin.h"
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02003
4#include <libelf.h>
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -03005#include <gelf.h>
6#include <elf.h>
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03007
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -03008#include "util/list.h"
Ingo Molnara930d2c2009-05-27 09:50:13 +02009#include "util/cache.h"
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030010#include "util/rbtree.h"
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030011
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020012#include "perf.h"
13
14#include "util/parse-options.h"
15#include "util/parse-events.h"
16
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030017#define SHOW_KERNEL 1
18#define SHOW_USER 2
19#define SHOW_HV 4
20
Ingo Molnar23ac9cb2009-05-27 09:33:18 +020021static char const *input_name = "perf.data";
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030022static int input;
23static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
24
Ingo Molnar97b07b62009-05-26 18:48:58 +020025static int dump_trace = 0;
Ingo Molnar16f762a2009-05-27 09:10:38 +020026static int verbose;
Ingo Molnar97b07b62009-05-26 18:48:58 +020027
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030028static unsigned long page_size;
29static unsigned long mmap_window = 32;
30
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020031const char *perf_event_names[] = {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030032 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
33 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
34 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
35};
36
37struct ip_event {
38 struct perf_event_header header;
39 __u64 ip;
40 __u32 pid, tid;
41};
42struct mmap_event {
43 struct perf_event_header header;
44 __u32 pid, tid;
45 __u64 start;
46 __u64 len;
47 __u64 pgoff;
48 char filename[PATH_MAX];
49};
50struct comm_event {
51 struct perf_event_header header;
52 __u32 pid,tid;
53 char comm[16];
54};
55
56typedef union event_union {
57 struct perf_event_header header;
58 struct ip_event ip;
59 struct mmap_event mmap;
60 struct comm_event comm;
61} event_t;
62
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030063struct symbol {
Ingo Molnar16f762a2009-05-27 09:10:38 +020064 struct rb_node rb_node;
65 __u64 start;
66 __u64 end;
67 char name[0];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030068};
69
70static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
71{
72 struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1);
73
74 if (self != NULL) {
75 self->start = start;
76 self->end = start + len;
77 strcpy(self->name, name);
78 }
79
80 return self;
81}
82
83static void symbol__delete(struct symbol *self)
84{
85 free(self);
86}
87
88static size_t symbol__fprintf(struct symbol *self, FILE *fp)
89{
Ingo Molnar16f762a2009-05-27 09:10:38 +020090 return fprintf(fp, " %llx-%llx %s\n",
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030091 self->start, self->end, self->name);
92}
93
94struct dso {
95 struct list_head node;
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030096 struct rb_root syms;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030097 char name[0];
98};
99
100static struct dso *dso__new(const char *name)
101{
102 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
103
104 if (self != NULL) {
105 strcpy(self->name, name);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300106 self->syms = RB_ROOT;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300107 }
108
109 return self;
110}
111
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300112static void dso__delete_symbols(struct dso *self)
113{
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300114 struct symbol *pos;
115 struct rb_node *next = rb_first(&self->syms);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300116
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300117 while (next) {
118 pos = rb_entry(next, struct symbol, rb_node);
119 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300120 symbol__delete(pos);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300121 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300122}
123
124static void dso__delete(struct dso *self)
125{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300126 dso__delete_symbols(self);
127 free(self);
128}
129
130static void dso__insert_symbol(struct dso *self, struct symbol *sym)
131{
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300132 struct rb_node **p = &self->syms.rb_node;
133 struct rb_node *parent = NULL;
134 const uint64_t ip = sym->start;
135 struct symbol *s;
136
137 while (*p != NULL) {
138 parent = *p;
139 s = rb_entry(parent, struct symbol, rb_node);
140 if (ip < s->start)
141 p = &(*p)->rb_left;
142 else
143 p = &(*p)->rb_right;
144 }
145 rb_link_node(&sym->rb_node, parent, p);
146 rb_insert_color(&sym->rb_node, &self->syms);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300147}
148
149static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
150{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200151 struct rb_node *n;
152
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300153 if (self == NULL)
154 return NULL;
155
Ingo Molnar16f762a2009-05-27 09:10:38 +0200156 n = self->syms.rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300157
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300158 while (n) {
159 struct symbol *s = rb_entry(n, struct symbol, rb_node);
160
161 if (ip < s->start)
162 n = n->rb_left;
163 else if (ip > s->end)
164 n = n->rb_right;
165 else
166 return s;
167 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300168
169 return NULL;
170}
171
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300172/**
173 * elf_symtab__for_each_symbol - iterate thru all the symbols
174 *
175 * @self: struct elf_symtab instance to iterate
176 * @index: uint32_t index
177 * @sym: GElf_Sym iterator
178 */
179#define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
180 for (index = 0, gelf_getsym(syms, index, &sym);\
181 index < nr_syms; \
182 index++, gelf_getsym(syms, index, &sym))
183
184static inline uint8_t elf_sym__type(const GElf_Sym *sym)
185{
186 return GELF_ST_TYPE(sym->st_info);
187}
188
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200189static inline int elf_sym__is_function(const GElf_Sym *sym)
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300190{
191 return elf_sym__type(sym) == STT_FUNC &&
192 sym->st_name != 0 &&
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200193 sym->st_shndx != SHN_UNDEF &&
194 sym->st_size != 0;
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300195}
196
197static inline const char *elf_sym__name(const GElf_Sym *sym,
198 const Elf_Data *symstrs)
199{
200 return symstrs->d_buf + sym->st_name;
201}
202
203static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
204 GElf_Shdr *shp, const char *name,
205 size_t *index)
206{
207 Elf_Scn *sec = NULL;
208 size_t cnt = 1;
209
210 while ((sec = elf_nextscn(elf, sec)) != NULL) {
211 char *str;
212
213 gelf_getshdr(sec, shp);
214 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
215 if (!strcmp(name, str)) {
216 if (index)
217 *index = cnt;
218 break;
219 }
220 ++cnt;
221 }
222
223 return sec;
224}
225
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200226static int dso__load_sym(struct dso *self, int fd, char *name)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300227{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200228 Elf_Data *symstrs;
229 uint32_t nr_syms;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200230 int err = -1;
Ingo Molnar16f762a2009-05-27 09:10:38 +0200231 uint32_t index;
232 GElf_Ehdr ehdr;
233 GElf_Shdr shdr;
234 Elf_Data *syms;
235 GElf_Sym sym;
236 Elf_Scn *sec;
237 Elf *elf;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200238 int nr = 0;
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300239
Ingo Molnar16f762a2009-05-27 09:10:38 +0200240 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300241 if (elf == NULL) {
242 fprintf(stderr, "%s: cannot read %s ELF file.\n",
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200243 __func__, name);
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300244 goto out_close;
245 }
246
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300247 if (gelf_getehdr(elf, &ehdr) == NULL) {
248 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
249 goto out_elf_end;
250 }
251
Ingo Molnar16f762a2009-05-27 09:10:38 +0200252 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300253 if (sec == NULL)
254 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
255
256 if (sec == NULL)
257 goto out_elf_end;
258
Ingo Molnar16f762a2009-05-27 09:10:38 +0200259 syms = elf_getdata(sec, NULL);
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300260 if (syms == NULL)
261 goto out_elf_end;
262
263 sec = elf_getscn(elf, shdr.sh_link);
264 if (sec == NULL)
265 goto out_elf_end;
266
Ingo Molnar16f762a2009-05-27 09:10:38 +0200267 symstrs = elf_getdata(sec, NULL);
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300268 if (symstrs == NULL)
269 goto out_elf_end;
270
Ingo Molnar16f762a2009-05-27 09:10:38 +0200271 nr_syms = shdr.sh_size / shdr.sh_entsize;
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300272
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300273 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200274 struct symbol *f;
275
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300276 if (!elf_sym__is_function(&sym))
277 continue;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200278
279 sec = elf_getscn(elf, sym.st_shndx);
280 if (!sec)
281 goto out_elf_end;
282
283 gelf_getshdr(sec, &shdr);
284 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
285
286 f = symbol__new(sym.st_value, sym.st_size,
287 elf_sym__name(&sym, symstrs));
288 if (!f)
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300289 goto out_elf_end;
290
291 dso__insert_symbol(self, f);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200292
293 nr++;
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300294 }
295
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200296 err = nr;
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300297out_elf_end:
298 elf_end(elf);
299out_close:
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300300 return err;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300301}
302
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200303static int dso__load(struct dso *self)
304{
305 int size = strlen(self->name) + sizeof("/usr/lib/debug%s.debug");
306 char *name = malloc(size);
307 int variant = 0;
308 int ret = -1;
309 int fd;
310
311 if (!name)
312 return -1;
313
314more:
315 do {
316 switch (variant) {
317 case 0: /* Fedora */
318 snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
319 break;
320 case 1: /* Ubuntu */
321 snprintf(name, size, "/usr/lib/debug%s", self->name);
322 break;
323 case 2: /* Sane people */
324 snprintf(name, size, "%s", self->name);
325 break;
326
327 default:
328 goto out;
329 }
330 variant++;
331
332 fd = open(name, O_RDONLY);
333 } while (fd < 0);
334
335 ret = dso__load_sym(self, fd, name);
336 close(fd);
337
338 /*
339 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
340 */
341 if (!ret)
342 goto more;
343
344out:
345 free(name);
346 return ret;
347}
348
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300349static size_t dso__fprintf(struct dso *self, FILE *fp)
350{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300351 size_t ret = fprintf(fp, "dso: %s\n", self->name);
352
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300353 struct rb_node *nd;
354 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
355 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300356 ret += symbol__fprintf(pos, fp);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300357 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300358
359 return ret;
360}
361
362static LIST_HEAD(dsos);
363static struct dso *kernel_dso;
364
365static void dsos__add(struct dso *dso)
366{
367 list_add_tail(&dso->node, &dsos);
368}
369
370static struct dso *dsos__find(const char *name)
371{
372 struct dso *pos;
373
374 list_for_each_entry(pos, &dsos, node)
375 if (strcmp(pos->name, name) == 0)
376 return pos;
377 return NULL;
378}
379
380static struct dso *dsos__findnew(const char *name)
381{
382 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200383 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300384
385 if (dso == NULL) {
386 dso = dso__new(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200387 if (!dso)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300388 goto out_delete_dso;
389
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200390 nr = dso__load(dso);
391 if (nr < 0) {
392 fprintf(stderr, "Failed to open: %s\n", name);
393 goto out_delete_dso;
394 }
395 if (!nr) {
396 fprintf(stderr,
397 "Failed to find debug symbols for: %s, maybe install a debug package?\n",
398 name);
399 }
400
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300401 dsos__add(dso);
402 }
403
404 return dso;
405
406out_delete_dso:
407 dso__delete(dso);
408 return NULL;
409}
410
Ingo Molnar16f762a2009-05-27 09:10:38 +0200411static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300412{
413 struct dso *pos;
414
415 list_for_each_entry(pos, &dsos, node)
416 dso__fprintf(pos, fp);
417}
418
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300419static int hex(char ch)
420{
421 if ((ch >= '0') && (ch <= '9'))
422 return ch - '0';
423 if ((ch >= 'a') && (ch <= 'f'))
424 return ch - 'a' + 10;
425 if ((ch >= 'A') && (ch <= 'F'))
426 return ch - 'A' + 10;
427 return -1;
428}
429
430/*
431 * While we find nice hex chars, build a long_val.
432 * Return number of chars processed.
433 */
Ingo Molnar16f762a2009-05-27 09:10:38 +0200434static int hex2long(char *ptr, unsigned long *long_val)
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300435{
436 const char *p = ptr;
437 *long_val = 0;
438
439 while (*p) {
440 const int hex_val = hex(*p);
441
442 if (hex_val < 0)
443 break;
444
445 *long_val = (*long_val << 4) | hex_val;
446 p++;
447 }
448
449 return p - ptr;
450}
451
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300452static int load_kallsyms(void)
453{
Ingo Molnaraf836322009-05-27 08:38:48 +0200454 struct rb_node *nd, *prevnd;
455 char *line = NULL;
456 FILE *file;
457 size_t n;
458
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300459 kernel_dso = dso__new("[kernel]");
460 if (kernel_dso == NULL)
461 return -1;
462
Ingo Molnaraf836322009-05-27 08:38:48 +0200463 file = fopen("/proc/kallsyms", "r");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300464 if (file == NULL)
465 goto out_delete_dso;
466
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300467 while (!feof(file)) {
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300468 unsigned long start;
Ingo Molnaraf836322009-05-27 08:38:48 +0200469 struct symbol *sym;
470 int line_len, len;
471 char symbol_type;
472
473 line_len = getline(&line, &n, file);
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300474 if (line_len < 0)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300475 break;
476
477 if (!line)
478 goto out_delete_dso;
479
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300480 line[--line_len] = '\0'; /* \n */
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300481
Ingo Molnaraf836322009-05-27 08:38:48 +0200482 len = hex2long(line, &start);
483
Arnaldo Carvalho de Melo03f63162009-05-26 19:21:55 -0300484 len++;
485 if (len + 2 >= line_len)
486 continue;
487
Ingo Molnaraf836322009-05-27 08:38:48 +0200488 symbol_type = toupper(line[len]);
Arnaldo Carvalho de Melo03f63162009-05-26 19:21:55 -0300489 /*
490 * We're interested only in code ('T'ext)
491 */
Ingo Molnaraf836322009-05-27 08:38:48 +0200492 if (symbol_type != 'T' && symbol_type != 'W')
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300493 continue;
494 /*
495 * Well fix up the end later, when we have all sorted.
496 */
Ingo Molnaraf836322009-05-27 08:38:48 +0200497 sym = symbol__new(start, 0xdead, line + len + 2);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300498
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300499 if (sym == NULL)
500 goto out_delete_dso;
501
502 dso__insert_symbol(kernel_dso, sym);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300503 }
504
Arnaldo Carvalho de Meloabd54f62009-05-26 12:21:34 -0300505 /*
506 * Now that we have all sorted out, just set the ->end of all
507 * symbols
508 */
Ingo Molnaraf836322009-05-27 08:38:48 +0200509 prevnd = rb_first(&kernel_dso->syms);
Arnaldo Carvalho de Meloabd54f62009-05-26 12:21:34 -0300510
511 if (prevnd == NULL)
512 goto out_delete_line;
513
514 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
515 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
516 *curr = rb_entry(nd, struct symbol, rb_node);
517
518 prev->end = curr->start - 1;
519 prevnd = nd;
520 }
521
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300522 dsos__add(kernel_dso);
523 free(line);
524 fclose(file);
Ingo Molnaraf836322009-05-27 08:38:48 +0200525
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300526 return 0;
527
Arnaldo Carvalho de Melo59d81022009-05-26 11:14:27 -0300528out_delete_line:
529 free(line);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300530out_delete_dso:
531 dso__delete(kernel_dso);
532 return -1;
533}
534
535struct map {
536 struct list_head node;
537 uint64_t start;
538 uint64_t end;
539 uint64_t pgoff;
540 struct dso *dso;
541};
542
543static struct map *map__new(struct mmap_event *event)
544{
545 struct map *self = malloc(sizeof(*self));
546
547 if (self != NULL) {
548 self->start = event->start;
549 self->end = event->start + event->len;
550 self->pgoff = event->pgoff;
551
552 self->dso = dsos__findnew(event->filename);
553 if (self->dso == NULL)
554 goto out_delete;
555 }
556 return self;
557out_delete:
558 free(self);
559 return NULL;
560}
561
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300562struct thread;
563
564static const char *thread__name(struct thread *self, char *bf, size_t size);
565
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300566struct symhist {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300567 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300568 struct dso *dso;
569 struct symbol *sym;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300570 struct thread *thread;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300571 uint64_t ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300572 uint32_t count;
573 char level;
574};
575
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300576static struct symhist *symhist__new(struct symbol *sym, uint64_t ip,
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300577 struct thread *thread, struct dso *dso,
578 char level)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300579{
580 struct symhist *self = malloc(sizeof(*self));
581
582 if (self != NULL) {
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300583 self->sym = sym;
584 self->thread = thread;
585 self->ip = ip;
586 self->dso = dso;
587 self->level = level;
588 self->count = 1;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300589 }
590
591 return self;
592}
593
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300594static void symhist__inc(struct symhist *self)
595{
596 ++self->count;
597}
598
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300599static size_t
600symhist__fprintf(struct symhist *self, uint64_t total_samples, FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300601{
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300602 char bf[32];
603 size_t ret;
604
605 if (total_samples)
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200606 ret = fprintf(fp, "%5.2f%% ", (self->count * 100.0) / total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300607 else
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200608 ret = fprintf(fp, "%12d ", self->count);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300609
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -0300610 ret += fprintf(fp, "%14s [%c] ",
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300611 thread__name(self->thread, bf, sizeof(bf)),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -0300612 self->level);
613
614 if (verbose)
615 ret += fprintf(fp, "%#018llx ", (unsigned long long)self->ip);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300616
617 if (self->level != '.')
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300618 ret += fprintf(fp, "%s\n",
619 self->sym ? self->sym->name : "<unknown>");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300620 else
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300621 ret += fprintf(fp, "%s: %s\n",
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200622 self->dso ? self->dso->name : "<unknown>",
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300623 self->sym ? self->sym->name : "<unknown>");
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300624 return ret;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300625}
626
627struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300628 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300629 struct list_head maps;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300630 struct rb_root symhists;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300631 pid_t pid;
632 char *comm;
633};
634
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300635static const char *thread__name(struct thread *self, char *bf, size_t size)
636{
637 if (self->comm)
638 return self->comm;
639
640 snprintf(bf, sizeof(bf), ":%u", self->pid);
641 return bf;
642}
643
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300644static struct thread *thread__new(pid_t pid)
645{
646 struct thread *self = malloc(sizeof(*self));
647
648 if (self != NULL) {
649 self->pid = pid;
650 self->comm = NULL;
651 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300652 self->symhists = RB_ROOT;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300653 }
654
655 return self;
656}
657
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300658static int thread__symbol_incnew(struct thread *self, struct symbol *sym,
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300659 uint64_t ip, struct dso *dso, char level)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300660{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300661 struct rb_node **p = &self->symhists.rb_node;
662 struct rb_node *parent = NULL;
663 struct symhist *sh;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300664
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300665 while (*p != NULL) {
Ingo Molnar16f762a2009-05-27 09:10:38 +0200666 uint64_t start;
667
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300668 parent = *p;
669 sh = rb_entry(parent, struct symhist, rb_node);
670
671 if (sh->sym == sym || ip == sh->ip) {
672 symhist__inc(sh);
673 return 0;
674 }
675
676 /* Handle unresolved symbols too */
Ingo Molnar16f762a2009-05-27 09:10:38 +0200677 start = !sh->sym ? sh->ip : sh->sym->start;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300678
679 if (ip < start)
680 p = &(*p)->rb_left;
681 else
682 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300683 }
684
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300685 sh = symhist__new(sym, ip, self, dso, level);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300686 if (sh == NULL)
687 return -ENOMEM;
688 rb_link_node(&sh->rb_node, parent, p);
689 rb_insert_color(&sh->rb_node, &self->symhists);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300690 return 0;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300691}
692
693static int thread__set_comm(struct thread *self, const char *comm)
694{
695 self->comm = strdup(comm);
696 return self->comm ? 0 : -ENOMEM;
697}
698
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300699static size_t thread__fprintf(struct thread *self, FILE *fp)
700{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300701 int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300702 struct rb_node *nd;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300703
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300704 for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) {
705 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
Ingo Molnar16f762a2009-05-27 09:10:38 +0200706
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300707 ret += symhist__fprintf(pos, 0, fp);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300708 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300709
710 return ret;
711}
712
Ingo Molnar16f762a2009-05-27 09:10:38 +0200713static struct rb_root threads;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300714
715static struct thread *threads__findnew(pid_t pid)
716{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300717 struct rb_node **p = &threads.rb_node;
718 struct rb_node *parent = NULL;
719 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300720
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300721 while (*p != NULL) {
722 parent = *p;
723 th = rb_entry(parent, struct thread, rb_node);
724
725 if (th->pid == pid)
726 return th;
727
728 if (pid < th->pid)
729 p = &(*p)->rb_left;
730 else
731 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300732 }
733
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300734 th = thread__new(pid);
735 if (th != NULL) {
736 rb_link_node(&th->rb_node, parent, p);
737 rb_insert_color(&th->rb_node, &threads);
738 }
739 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300740}
741
742static void thread__insert_map(struct thread *self, struct map *map)
743{
744 list_add_tail(&map->node, &self->maps);
745}
746
747static struct map *thread__find_map(struct thread *self, uint64_t ip)
748{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200749 struct map *pos;
750
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300751 if (self == NULL)
752 return NULL;
753
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300754 list_for_each_entry(pos, &self->maps, node)
755 if (ip >= pos->start && ip <= pos->end)
756 return pos;
757
758 return NULL;
759}
760
Ingo Molnar16f762a2009-05-27 09:10:38 +0200761static void threads__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300762{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300763 struct rb_node *nd;
764 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
765 struct thread *pos = rb_entry(nd, struct thread, rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300766 thread__fprintf(pos, fp);
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300767 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300768}
769
Ingo Molnar16f762a2009-05-27 09:10:38 +0200770static struct rb_root global_symhists;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300771
772static void threads__insert_symhist(struct symhist *sh)
773{
774 struct rb_node **p = &global_symhists.rb_node;
775 struct rb_node *parent = NULL;
776 struct symhist *iter;
777
778 while (*p != NULL) {
779 parent = *p;
780 iter = rb_entry(parent, struct symhist, rb_node);
781
782 /* Reverse order */
783 if (sh->count > iter->count)
784 p = &(*p)->rb_left;
785 else
786 p = &(*p)->rb_right;
787 }
788
789 rb_link_node(&sh->rb_node, parent, p);
790 rb_insert_color(&sh->rb_node, &global_symhists);
791}
792
793static void threads__sort_symhists(void)
794{
795 struct rb_node *nd;
796
797 for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
798 struct thread *thread = rb_entry(nd, struct thread, rb_node);
799 struct rb_node *next = rb_first(&thread->symhists);
800
801 while (next) {
802 struct symhist *n = rb_entry(next, struct symhist,
803 rb_node);
804 next = rb_next(&n->rb_node);
805 rb_erase(&n->rb_node, &thread->symhists);
806 threads__insert_symhist(n);
807 }
808
809 }
810}
811
812static size_t threads__symhists_fprintf(uint64_t total_samples, FILE *fp)
813{
814 struct rb_node *nd;
815 size_t ret = 0;
816
817 for (nd = rb_first(&global_symhists); nd; nd = rb_next(nd)) {
818 struct symhist *pos = rb_entry(nd, struct symhist, rb_node);
819 ret += symhist__fprintf(pos, total_samples, fp);
820 }
821
822 return ret;
823}
824
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200825static int __cmd_report(void)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300826{
827 unsigned long offset = 0;
828 unsigned long head = 0;
829 struct stat stat;
830 char *buf;
831 event_t *event;
832 int ret, rc = EXIT_FAILURE;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200833 uint32_t size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200834 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300835
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300836 input = open(input_name, O_RDONLY);
837 if (input < 0) {
838 perror("failed to open file");
839 exit(-1);
840 }
841
842 ret = fstat(input, &stat);
843 if (ret < 0) {
844 perror("failed to stat file");
845 exit(-1);
846 }
847
848 if (!stat.st_size) {
849 fprintf(stderr, "zero-sized file, nothing to do!\n");
850 exit(0);
851 }
852
853 if (load_kallsyms() < 0) {
854 perror("failed to open kallsyms");
855 return EXIT_FAILURE;
856 }
857
858remap:
859 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
860 MAP_SHARED, input, offset);
861 if (buf == MAP_FAILED) {
862 perror("failed to mmap file");
863 exit(-1);
864 }
865
866more:
867 event = (event_t *)(buf + head);
868
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200869 size = event->header.size;
870 if (!size)
871 size = 8;
872
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300873 if (head + event->header.size >= page_size * mmap_window) {
874 unsigned long shift = page_size * (head / page_size);
875 int ret;
876
877 ret = munmap(buf, page_size * mmap_window);
878 assert(ret == 0);
879
880 offset += shift;
881 head -= shift;
882 goto remap;
883 }
884
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200885 size = event->header.size;
886 if (!size)
887 goto broken_event;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300888
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300889 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
890 char level;
891 int show = 0;
892 struct dso *dso = NULL;
893 struct thread *thread = threads__findnew(event->ip.pid);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200894 uint64_t ip = event->ip.ip;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300895
Ingo Molnar97b07b62009-05-26 18:48:58 +0200896 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200897 fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
898 (void *)(offset + head),
899 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200900 event->header.misc,
901 event->ip.pid,
Ingo Molnar16f762a2009-05-27 09:10:38 +0200902 (void *)(long)ip);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200903 }
904
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300905 if (thread == NULL) {
906 fprintf(stderr, "problem processing %d event, bailing out\n",
907 event->header.type);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300908 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300909 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300910
911 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
912 show = SHOW_KERNEL;
913 level = 'k';
914 dso = kernel_dso;
915 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
Ingo Molnar16f762a2009-05-27 09:10:38 +0200916 struct map *map;
917
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300918 show = SHOW_USER;
919 level = '.';
Ingo Molnar16f762a2009-05-27 09:10:38 +0200920
921 map = thread__find_map(thread, ip);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200922 if (map != NULL) {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300923 dso = map->dso;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200924 ip -= map->start + map->pgoff;
925 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300926 } else {
927 show = SHOW_HV;
928 level = 'H';
929 }
930
931 if (show & show_mask) {
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200932 struct symbol *sym = dso__find_symbol(dso, ip);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300933
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200934 if (thread__symbol_incnew(thread, sym, ip, dso, level)) {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300935 fprintf(stderr, "problem incrementing symbol count, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300936 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300937 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300938 }
939 total++;
940 } else switch (event->header.type) {
941 case PERF_EVENT_MMAP: {
942 struct thread *thread = threads__findnew(event->mmap.pid);
943 struct map *map = map__new(&event->mmap);
944
Ingo Molnar97b07b62009-05-26 18:48:58 +0200945 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200946 fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
947 (void *)(offset + head),
948 (void *)(long)(event->header.size),
Ingo Molnar16f762a2009-05-27 09:10:38 +0200949 (void *)(long)event->mmap.start,
950 (void *)(long)event->mmap.len,
951 (void *)(long)event->mmap.pgoff,
Ingo Molnar97b07b62009-05-26 18:48:58 +0200952 event->mmap.filename);
953 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300954 if (thread == NULL || map == NULL) {
955 fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300956 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300957 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300958 thread__insert_map(thread, map);
Ingo Molnar97b07b62009-05-26 18:48:58 +0200959 total_mmap++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300960 break;
961 }
962 case PERF_EVENT_COMM: {
963 struct thread *thread = threads__findnew(event->comm.pid);
964
Ingo Molnar97b07b62009-05-26 18:48:58 +0200965 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +0200966 fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
967 (void *)(offset + head),
968 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +0200969 event->comm.comm, event->comm.pid);
970 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300971 if (thread == NULL ||
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300972 thread__set_comm(thread, event->comm.comm)) {
973 fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300974 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300975 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200976 total_comm++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300977 break;
978 }
Ingo Molnar97b07b62009-05-26 18:48:58 +0200979 default: {
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200980broken_event:
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200981 if (dump_trace)
982 fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
983 (void *)(offset + head),
984 (void *)(long)(event->header.size),
985 event->header.type);
986
Ingo Molnar3e706112009-05-26 18:53:17 +0200987 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200988
989 /*
990 * assume we lost track of the stream, check alignment, and
991 * increment a single u64 in the hope to catch on again 'soon'.
992 */
993
994 if (unlikely(head & 7))
995 head &= ~7ULL;
996
997 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +0200998 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300999 }
1000
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001001 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001002
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001003 if (offset + head < stat.st_size)
1004 goto more;
1005
1006 rc = EXIT_SUCCESS;
1007done:
1008 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001009
1010 if (dump_trace) {
Ingo Molnar3e706112009-05-26 18:53:17 +02001011 fprintf(stderr, " IP events: %10ld\n", total);
1012 fprintf(stderr, " mmap events: %10ld\n", total_mmap);
1013 fprintf(stderr, " comm events: %10ld\n", total_comm);
1014 fprintf(stderr, " unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001015
1016 return 0;
1017 }
1018
Ingo Molnar16f762a2009-05-27 09:10:38 +02001019 if (verbose >= 2) {
1020 dsos__fprintf(stdout);
1021 threads__fprintf(stdout);
1022 }
1023
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -03001024 threads__sort_symhists();
1025 threads__symhists_fprintf(total, stdout);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001026
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001027 return rc;
1028}
1029
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001030static const char * const report_usage[] = {
1031 "perf report [<options>] <command>",
1032 NULL
1033};
1034
1035static const struct option options[] = {
1036 OPT_STRING('i', "input", &input_name, "file",
1037 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001038 OPT_BOOLEAN('v', "verbose", &verbose,
1039 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001040 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1041 "dump raw trace in ASCII"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001042 OPT_END()
1043};
1044
1045int cmd_report(int argc, const char **argv, const char *prefix)
1046{
1047 elf_version(EV_CURRENT);
1048
1049 page_size = getpagesize();
1050
1051 parse_options(argc, argv, options, report_usage, 0);
1052
Ingo Molnara930d2c2009-05-27 09:50:13 +02001053 setup_pager();
1054
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001055 return __cmd_report();
1056}