blob: 982abce0e7c1502f22e9e7606c4f2ee70fbb7e4b [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";
Peter Zijlstra450aaa22009-05-27 20:20:23 +020022static char *vmlinux = NULL;
Peter Zijlstra37f440c2009-05-27 20:20:26 +020023static char *sort_order = "pid,symbol";
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030024static int input;
25static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
26
Ingo Molnar97b07b62009-05-26 18:48:58 +020027static int dump_trace = 0;
Ingo Molnar16f762a2009-05-27 09:10:38 +020028static int verbose;
Ingo Molnar97b07b62009-05-26 18:48:58 +020029
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030030static unsigned long page_size;
31static unsigned long mmap_window = 32;
32
Ingo Molnar53cb8bc2009-05-26 09:17:18 +020033const char *perf_event_names[] = {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030034 [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
35 [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
36 [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
37};
38
39struct ip_event {
40 struct perf_event_header header;
41 __u64 ip;
42 __u32 pid, tid;
43};
44struct mmap_event {
45 struct perf_event_header header;
46 __u32 pid, tid;
47 __u64 start;
48 __u64 len;
49 __u64 pgoff;
50 char filename[PATH_MAX];
51};
52struct comm_event {
53 struct perf_event_header header;
54 __u32 pid,tid;
55 char comm[16];
56};
57
58typedef union event_union {
59 struct perf_event_header header;
60 struct ip_event ip;
61 struct mmap_event mmap;
62 struct comm_event comm;
63} event_t;
64
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030065struct symbol {
Ingo Molnar16f762a2009-05-27 09:10:38 +020066 struct rb_node rb_node;
67 __u64 start;
68 __u64 end;
69 char name[0];
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030070};
71
72static struct symbol *symbol__new(uint64_t start, uint64_t len, const char *name)
73{
74 struct symbol *self = malloc(sizeof(*self) + strlen(name) + 1);
75
76 if (self != NULL) {
77 self->start = start;
78 self->end = start + len;
79 strcpy(self->name, name);
80 }
81
82 return self;
83}
84
85static void symbol__delete(struct symbol *self)
86{
87 free(self);
88}
89
90static size_t symbol__fprintf(struct symbol *self, FILE *fp)
91{
Ingo Molnar16f762a2009-05-27 09:10:38 +020092 return fprintf(fp, " %llx-%llx %s\n",
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030093 self->start, self->end, self->name);
94}
95
96struct dso {
97 struct list_head node;
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -030098 struct rb_root syms;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -030099 char name[0];
100};
101
102static struct dso *dso__new(const char *name)
103{
104 struct dso *self = malloc(sizeof(*self) + strlen(name) + 1);
105
106 if (self != NULL) {
107 strcpy(self->name, name);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300108 self->syms = RB_ROOT;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300109 }
110
111 return self;
112}
113
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300114static void dso__delete_symbols(struct dso *self)
115{
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300116 struct symbol *pos;
117 struct rb_node *next = rb_first(&self->syms);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300118
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300119 while (next) {
120 pos = rb_entry(next, struct symbol, rb_node);
121 next = rb_next(&pos->rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300122 symbol__delete(pos);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300123 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300124}
125
126static void dso__delete(struct dso *self)
127{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300128 dso__delete_symbols(self);
129 free(self);
130}
131
132static void dso__insert_symbol(struct dso *self, struct symbol *sym)
133{
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300134 struct rb_node **p = &self->syms.rb_node;
135 struct rb_node *parent = NULL;
136 const uint64_t ip = sym->start;
137 struct symbol *s;
138
139 while (*p != NULL) {
140 parent = *p;
141 s = rb_entry(parent, struct symbol, rb_node);
142 if (ip < s->start)
143 p = &(*p)->rb_left;
144 else
145 p = &(*p)->rb_right;
146 }
147 rb_link_node(&sym->rb_node, parent, p);
148 rb_insert_color(&sym->rb_node, &self->syms);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300149}
150
151static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip)
152{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200153 struct rb_node *n;
154
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300155 if (self == NULL)
156 return NULL;
157
Ingo Molnar16f762a2009-05-27 09:10:38 +0200158 n = self->syms.rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300159
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300160 while (n) {
161 struct symbol *s = rb_entry(n, struct symbol, rb_node);
162
163 if (ip < s->start)
164 n = n->rb_left;
165 else if (ip > s->end)
166 n = n->rb_right;
167 else
168 return s;
169 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300170
171 return NULL;
172}
173
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300174/**
175 * elf_symtab__for_each_symbol - iterate thru all the symbols
176 *
177 * @self: struct elf_symtab instance to iterate
178 * @index: uint32_t index
179 * @sym: GElf_Sym iterator
180 */
181#define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \
182 for (index = 0, gelf_getsym(syms, index, &sym);\
183 index < nr_syms; \
184 index++, gelf_getsym(syms, index, &sym))
185
186static inline uint8_t elf_sym__type(const GElf_Sym *sym)
187{
188 return GELF_ST_TYPE(sym->st_info);
189}
190
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200191static inline int elf_sym__is_function(const GElf_Sym *sym)
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300192{
193 return elf_sym__type(sym) == STT_FUNC &&
194 sym->st_name != 0 &&
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200195 sym->st_shndx != SHN_UNDEF &&
196 sym->st_size != 0;
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300197}
198
199static inline const char *elf_sym__name(const GElf_Sym *sym,
200 const Elf_Data *symstrs)
201{
202 return symstrs->d_buf + sym->st_name;
203}
204
205static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep,
206 GElf_Shdr *shp, const char *name,
207 size_t *index)
208{
209 Elf_Scn *sec = NULL;
210 size_t cnt = 1;
211
212 while ((sec = elf_nextscn(elf, sec)) != NULL) {
213 char *str;
214
215 gelf_getshdr(sec, shp);
216 str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name);
217 if (!strcmp(name, str)) {
218 if (index)
219 *index = cnt;
220 break;
221 }
222 ++cnt;
223 }
224
225 return sec;
226}
227
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200228static int dso__load_sym(struct dso *self, int fd, char *name)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300229{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200230 Elf_Data *symstrs;
231 uint32_t nr_syms;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200232 int err = -1;
Ingo Molnar16f762a2009-05-27 09:10:38 +0200233 uint32_t index;
234 GElf_Ehdr ehdr;
235 GElf_Shdr shdr;
236 Elf_Data *syms;
237 GElf_Sym sym;
238 Elf_Scn *sec;
239 Elf *elf;
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200240 int nr = 0;
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300241
Ingo Molnar16f762a2009-05-27 09:10:38 +0200242 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300243 if (elf == NULL) {
244 fprintf(stderr, "%s: cannot read %s ELF file.\n",
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200245 __func__, name);
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300246 goto out_close;
247 }
248
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300249 if (gelf_getehdr(elf, &ehdr) == NULL) {
250 fprintf(stderr, "%s: cannot get elf header.\n", __func__);
251 goto out_elf_end;
252 }
253
Ingo Molnar16f762a2009-05-27 09:10:38 +0200254 sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL);
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300255 if (sec == NULL)
256 sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL);
257
258 if (sec == NULL)
259 goto out_elf_end;
260
Ingo Molnar16f762a2009-05-27 09:10:38 +0200261 syms = elf_getdata(sec, NULL);
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300262 if (syms == NULL)
263 goto out_elf_end;
264
265 sec = elf_getscn(elf, shdr.sh_link);
266 if (sec == NULL)
267 goto out_elf_end;
268
Ingo Molnar16f762a2009-05-27 09:10:38 +0200269 symstrs = elf_getdata(sec, NULL);
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300270 if (symstrs == NULL)
271 goto out_elf_end;
272
Ingo Molnar16f762a2009-05-27 09:10:38 +0200273 nr_syms = shdr.sh_size / shdr.sh_entsize;
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300274
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300275 elf_symtab__for_each_symbol(syms, nr_syms, index, sym) {
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200276 struct symbol *f;
277
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300278 if (!elf_sym__is_function(&sym))
279 continue;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +0200280
281 sec = elf_getscn(elf, sym.st_shndx);
282 if (!sec)
283 goto out_elf_end;
284
285 gelf_getshdr(sec, &shdr);
286 sym.st_value -= shdr.sh_addr - shdr.sh_offset;
287
288 f = symbol__new(sym.st_value, sym.st_size,
289 elf_sym__name(&sym, symstrs));
290 if (!f)
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300291 goto out_elf_end;
292
293 dso__insert_symbol(self, f);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200294
295 nr++;
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300296 }
297
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200298 err = nr;
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300299out_elf_end:
300 elf_end(elf);
301out_close:
Arnaldo Carvalho de Melo62eb9392009-05-18 14:28:47 -0300302 return err;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300303}
304
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200305static int dso__load(struct dso *self)
306{
307 int size = strlen(self->name) + sizeof("/usr/lib/debug%s.debug");
308 char *name = malloc(size);
309 int variant = 0;
310 int ret = -1;
311 int fd;
312
313 if (!name)
314 return -1;
315
316more:
317 do {
318 switch (variant) {
319 case 0: /* Fedora */
320 snprintf(name, size, "/usr/lib/debug%s.debug", self->name);
321 break;
322 case 1: /* Ubuntu */
323 snprintf(name, size, "/usr/lib/debug%s", self->name);
324 break;
325 case 2: /* Sane people */
326 snprintf(name, size, "%s", self->name);
327 break;
328
329 default:
330 goto out;
331 }
332 variant++;
333
334 fd = open(name, O_RDONLY);
335 } while (fd < 0);
336
337 ret = dso__load_sym(self, fd, name);
338 close(fd);
339
340 /*
341 * Some people seem to have debuginfo files _WITHOUT_ debug info!?!?
342 */
343 if (!ret)
344 goto more;
345
346out:
347 free(name);
348 return ret;
349}
350
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300351static size_t dso__fprintf(struct dso *self, FILE *fp)
352{
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300353 size_t ret = fprintf(fp, "dso: %s\n", self->name);
354
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300355 struct rb_node *nd;
356 for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) {
357 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300358 ret += symbol__fprintf(pos, fp);
Arnaldo Carvalho de Melo35a50c82009-05-18 16:24:49 -0300359 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300360
361 return ret;
362}
363
364static LIST_HEAD(dsos);
365static struct dso *kernel_dso;
366
367static void dsos__add(struct dso *dso)
368{
369 list_add_tail(&dso->node, &dsos);
370}
371
372static struct dso *dsos__find(const char *name)
373{
374 struct dso *pos;
375
376 list_for_each_entry(pos, &dsos, node)
377 if (strcmp(pos->name, name) == 0)
378 return pos;
379 return NULL;
380}
381
382static struct dso *dsos__findnew(const char *name)
383{
384 struct dso *dso = dsos__find(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200385 int nr;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300386
387 if (dso == NULL) {
388 dso = dso__new(name);
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200389 if (!dso)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300390 goto out_delete_dso;
391
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +0200392 nr = dso__load(dso);
393 if (nr < 0) {
394 fprintf(stderr, "Failed to open: %s\n", name);
395 goto out_delete_dso;
396 }
397 if (!nr) {
398 fprintf(stderr,
399 "Failed to find debug symbols for: %s, maybe install a debug package?\n",
400 name);
401 }
402
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300403 dsos__add(dso);
404 }
405
406 return dso;
407
408out_delete_dso:
409 dso__delete(dso);
410 return NULL;
411}
412
Ingo Molnar16f762a2009-05-27 09:10:38 +0200413static void dsos__fprintf(FILE *fp)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300414{
415 struct dso *pos;
416
417 list_for_each_entry(pos, &dsos, node)
418 dso__fprintf(pos, fp);
419}
420
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300421static int hex(char ch)
422{
423 if ((ch >= '0') && (ch <= '9'))
424 return ch - '0';
425 if ((ch >= 'a') && (ch <= 'f'))
426 return ch - 'a' + 10;
427 if ((ch >= 'A') && (ch <= 'F'))
428 return ch - 'A' + 10;
429 return -1;
430}
431
432/*
433 * While we find nice hex chars, build a long_val.
434 * Return number of chars processed.
435 */
Ingo Molnar16f762a2009-05-27 09:10:38 +0200436static int hex2long(char *ptr, unsigned long *long_val)
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300437{
438 const char *p = ptr;
439 *long_val = 0;
440
441 while (*p) {
442 const int hex_val = hex(*p);
443
444 if (hex_val < 0)
445 break;
446
447 *long_val = (*long_val << 4) | hex_val;
448 p++;
449 }
450
451 return p - ptr;
452}
453
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300454static int load_kallsyms(void)
455{
Ingo Molnaraf836322009-05-27 08:38:48 +0200456 struct rb_node *nd, *prevnd;
457 char *line = NULL;
458 FILE *file;
459 size_t n;
460
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300461 kernel_dso = dso__new("[kernel]");
462 if (kernel_dso == NULL)
463 return -1;
464
Ingo Molnaraf836322009-05-27 08:38:48 +0200465 file = fopen("/proc/kallsyms", "r");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300466 if (file == NULL)
467 goto out_delete_dso;
468
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300469 while (!feof(file)) {
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300470 unsigned long start;
Ingo Molnaraf836322009-05-27 08:38:48 +0200471 struct symbol *sym;
472 int line_len, len;
473 char symbol_type;
474
475 line_len = getline(&line, &n, file);
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300476 if (line_len < 0)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300477 break;
478
479 if (!line)
480 goto out_delete_dso;
481
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300482 line[--line_len] = '\0'; /* \n */
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300483
Ingo Molnaraf836322009-05-27 08:38:48 +0200484 len = hex2long(line, &start);
485
Arnaldo Carvalho de Melo03f63162009-05-26 19:21:55 -0300486 len++;
487 if (len + 2 >= line_len)
488 continue;
489
Ingo Molnaraf836322009-05-27 08:38:48 +0200490 symbol_type = toupper(line[len]);
Arnaldo Carvalho de Melo03f63162009-05-26 19:21:55 -0300491 /*
492 * We're interested only in code ('T'ext)
493 */
Ingo Molnaraf836322009-05-27 08:38:48 +0200494 if (symbol_type != 'T' && symbol_type != 'W')
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300495 continue;
496 /*
497 * Well fix up the end later, when we have all sorted.
498 */
Ingo Molnaraf836322009-05-27 08:38:48 +0200499 sym = symbol__new(start, 0xdead, line + len + 2);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300500
Arnaldo Carvalho de Melod8d16562009-05-26 19:20:57 -0300501 if (sym == NULL)
502 goto out_delete_dso;
503
504 dso__insert_symbol(kernel_dso, sym);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300505 }
506
Arnaldo Carvalho de Meloabd54f62009-05-26 12:21:34 -0300507 /*
508 * Now that we have all sorted out, just set the ->end of all
509 * symbols
510 */
Ingo Molnaraf836322009-05-27 08:38:48 +0200511 prevnd = rb_first(&kernel_dso->syms);
Arnaldo Carvalho de Meloabd54f62009-05-26 12:21:34 -0300512
513 if (prevnd == NULL)
514 goto out_delete_line;
515
516 for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) {
517 struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node),
518 *curr = rb_entry(nd, struct symbol, rb_node);
519
520 prev->end = curr->start - 1;
521 prevnd = nd;
522 }
523
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300524 dsos__add(kernel_dso);
525 free(line);
526 fclose(file);
Ingo Molnaraf836322009-05-27 08:38:48 +0200527
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300528 return 0;
529
Arnaldo Carvalho de Melo59d81022009-05-26 11:14:27 -0300530out_delete_line:
531 free(line);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300532out_delete_dso:
533 dso__delete(kernel_dso);
534 return -1;
535}
536
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200537static int load_kernel(void)
538{
539 int fd, nr;
540
541 if (!vmlinux)
542 goto kallsyms;
543
544 fd = open(vmlinux, O_RDONLY);
545 if (fd < 0)
546 goto kallsyms;
547
548 kernel_dso = dso__new("[kernel]");
549 if (!kernel_dso)
550 goto fail_open;
551
552 nr = dso__load_sym(kernel_dso, fd, vmlinux);
553
554 if (nr <= 0)
555 goto fail_load;
556
557 dsos__add(kernel_dso);
558 close(fd);
559
560 return 0;
561
562fail_load:
563 dso__delete(kernel_dso);
564fail_open:
565 close(fd);
566kallsyms:
567 return load_kallsyms();
568}
569
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300570struct map {
571 struct list_head node;
572 uint64_t start;
573 uint64_t end;
574 uint64_t pgoff;
575 struct dso *dso;
576};
577
578static struct map *map__new(struct mmap_event *event)
579{
580 struct map *self = malloc(sizeof(*self));
581
582 if (self != NULL) {
583 self->start = event->start;
584 self->end = event->start + event->len;
585 self->pgoff = event->pgoff;
586
587 self->dso = dsos__findnew(event->filename);
588 if (self->dso == NULL)
589 goto out_delete;
590 }
591 return self;
592out_delete:
593 free(self);
594 return NULL;
595}
596
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300597struct thread;
598
599static const char *thread__name(struct thread *self, char *bf, size_t size);
600
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300601struct thread {
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300602 struct rb_node rb_node;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300603 struct list_head maps;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300604 pid_t pid;
605 char *comm;
606};
607
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300608static const char *thread__name(struct thread *self, char *bf, size_t size)
609{
610 if (self->comm)
611 return self->comm;
612
613 snprintf(bf, sizeof(bf), ":%u", self->pid);
614 return bf;
615}
616
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300617static struct thread *thread__new(pid_t pid)
618{
619 struct thread *self = malloc(sizeof(*self));
620
621 if (self != NULL) {
622 self->pid = pid;
623 self->comm = NULL;
624 INIT_LIST_HEAD(&self->maps);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300625 }
626
627 return self;
628}
629
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300630static int thread__set_comm(struct thread *self, const char *comm)
631{
632 self->comm = strdup(comm);
633 return self->comm ? 0 : -ENOMEM;
634}
635
Ingo Molnar16f762a2009-05-27 09:10:38 +0200636static struct rb_root threads;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300637
638static struct thread *threads__findnew(pid_t pid)
639{
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300640 struct rb_node **p = &threads.rb_node;
641 struct rb_node *parent = NULL;
642 struct thread *th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300643
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300644 while (*p != NULL) {
645 parent = *p;
646 th = rb_entry(parent, struct thread, rb_node);
647
648 if (th->pid == pid)
649 return th;
650
651 if (pid < th->pid)
652 p = &(*p)->rb_left;
653 else
654 p = &(*p)->rb_right;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300655 }
656
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -0300657 th = thread__new(pid);
658 if (th != NULL) {
659 rb_link_node(&th->rb_node, parent, p);
660 rb_insert_color(&th->rb_node, &threads);
661 }
662 return th;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300663}
664
665static void thread__insert_map(struct thread *self, struct map *map)
666{
667 list_add_tail(&map->node, &self->maps);
668}
669
670static struct map *thread__find_map(struct thread *self, uint64_t ip)
671{
Ingo Molnar16f762a2009-05-27 09:10:38 +0200672 struct map *pos;
673
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300674 if (self == NULL)
675 return NULL;
676
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300677 list_for_each_entry(pos, &self->maps, node)
678 if (ip >= pos->start && ip <= pos->end)
679 return pos;
680
681 return NULL;
682}
683
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200684/*
685 * histogram, sorted on item, collects counts
686 */
687
688static struct rb_root hist;
689
690struct hist_entry {
691 struct rb_node rb_node;
692
693 struct thread *thread;
694 struct map *map;
695 struct dso *dso;
696 struct symbol *sym;
697 uint64_t ip;
698 char level;
699
700 uint32_t count;
701};
702
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200703/*
704 * configurable sorting bits
705 */
706
707struct sort_entry {
708 struct list_head list;
709
710 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
711 size_t (*print)(FILE *fp, struct hist_entry *);
712};
713
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200714static int64_t
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200715sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
716{
717 return right->thread->pid - left->thread->pid;
718}
719
720static size_t
721sort__thread_print(FILE *fp, struct hist_entry *self)
722{
723 char bf[32];
724
725 return fprintf(fp, "%14s ",
726 thread__name(self->thread, bf, sizeof(bf)));
727}
728
729static struct sort_entry sort_thread = {
730 .cmp = sort__thread_cmp,
731 .print = sort__thread_print,
732};
733
734static int64_t
735sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300736{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200737 uint64_t ip_l, ip_r;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200738
739 if (left->sym == right->sym)
740 return 0;
741
742 ip_l = left->sym ? left->sym->start : left->ip;
743 ip_r = right->sym ? right->sym->start : right->ip;
744
745 return (int64_t)(ip_r - ip_l);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300746}
747
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200748static size_t
749sort__sym_print(FILE *fp, struct hist_entry *self)
750{
751 size_t ret = 0;
752
753 ret += fprintf(fp, "[%c] ", self->level);
754
755 if (verbose)
756 ret += fprintf(fp, "%#018llx ", (unsigned long long)self->ip);
757
758 if (self->level != '.')
759 ret += fprintf(fp, "%s ",
760 self->sym ? self->sym->name : "<unknown>");
761 else
762 ret += fprintf(fp, "%s: %s ",
763 self->dso ? self->dso->name : "<unknown>",
764 self->sym ? self->sym->name : "<unknown>");
765
766 return ret;
767}
768
769static struct sort_entry sort_sym = {
770 .cmp = sort__sym_cmp,
771 .print = sort__sym_print,
772};
773
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200774struct sort_dimension {
775 char *name;
776 struct sort_entry *entry;
777 int taken;
778};
779
780static struct sort_dimension sort_dimensions[] = {
781 { .name = "pid", .entry = &sort_thread, },
782 { .name = "symbol", .entry = &sort_sym, },
783};
784
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200785static LIST_HEAD(hist_entry__sort_list);
786
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200787static int sort_dimension__add(char *tok)
788{
789 int i;
790
791 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
792 struct sort_dimension *sd = &sort_dimensions[i];
793
794 if (sd->taken)
795 continue;
796
797 if (strcmp(tok, sd->name))
798 continue;
799
800 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
801 sd->taken = 1;
802 return 0;
803 }
804
805 return -ESRCH;
806}
807
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200808static void setup_sorting(void)
809{
Peter Zijlstra37f440c2009-05-27 20:20:26 +0200810 char *tmp, *tok, *str = strdup(sort_order);
811
812 for (tok = strtok_r(str, ", ", &tmp);
813 tok; tok = strtok_r(NULL, ", ", &tmp))
814 sort_dimension__add(tok);
815
816 free(str);
Peter Zijlstra1aa16732009-05-27 20:20:25 +0200817}
818
819static int64_t
820hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
821{
822 struct sort_entry *se;
823 int64_t cmp = 0;
824
825 list_for_each_entry(se, &hist_entry__sort_list, list) {
826 cmp = se->cmp(left, right);
827 if (cmp)
828 break;
829 }
830
831 return cmp;
832}
833
834static size_t
835hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
836{
837 struct sort_entry *se;
838 size_t ret;
839
840 if (total_samples) {
841 ret = fprintf(fp, "%5.2f%% ",
842 (self->count * 100.0) / total_samples);
843 } else
844 ret = fprintf(fp, "%12d ", self->count);
845
846 list_for_each_entry(se, &hist_entry__sort_list, list)
847 ret += se->print(fp, self);
848
849 ret += fprintf(fp, "\n");
850
851 return ret;
852}
853
854/*
855 * collect histogram counts
856 */
857
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200858static int
859hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
860 struct symbol *sym, uint64_t ip, char level)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300861{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200862 struct rb_node **p = &hist.rb_node;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300863 struct rb_node *parent = NULL;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200864 struct hist_entry *he;
865 struct hist_entry entry = {
866 .thread = thread,
867 .map = map,
868 .dso = dso,
869 .sym = sym,
870 .ip = ip,
871 .level = level,
872 .count = 1,
873 };
874 int cmp;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300875
876 while (*p != NULL) {
877 parent = *p;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200878 he = rb_entry(parent, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300879
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200880 cmp = hist_entry__cmp(&entry, he);
881
882 if (!cmp) {
883 he->count++;
884 return 0;
885 }
886
887 if (cmp < 0)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300888 p = &(*p)->rb_left;
889 else
890 p = &(*p)->rb_right;
891 }
892
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200893 he = malloc(sizeof(*he));
894 if (!he)
895 return -ENOMEM;
896 *he = entry;
897 rb_link_node(&he->rb_node, parent, p);
898 rb_insert_color(&he->rb_node, &hist);
899
900 return 0;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300901}
902
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200903/*
904 * reverse the map, sort on count.
905 */
906
907static struct rb_root output_hists;
908
909static void output__insert_entry(struct hist_entry *he)
910{
911 struct rb_node **p = &output_hists.rb_node;
912 struct rb_node *parent = NULL;
913 struct hist_entry *iter;
914
915 while (*p != NULL) {
916 parent = *p;
917 iter = rb_entry(parent, struct hist_entry, rb_node);
918
919 if (he->count > iter->count)
920 p = &(*p)->rb_left;
921 else
922 p = &(*p)->rb_right;
923 }
924
925 rb_link_node(&he->rb_node, parent, p);
926 rb_insert_color(&he->rb_node, &output_hists);
927}
928
929static void output__resort(void)
930{
931 struct rb_node *next = rb_first(&hist);
932 struct hist_entry *n;
933
934 while (next) {
935 n = rb_entry(next, struct hist_entry, rb_node);
936 next = rb_next(&n->rb_node);
937
938 rb_erase(&n->rb_node, &hist);
939 output__insert_entry(n);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300940 }
941}
942
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200943static size_t output__fprintf(FILE *fp, uint64_t total_samples)
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300944{
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200945 struct hist_entry *pos;
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300946 struct rb_node *nd;
947 size_t ret = 0;
948
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200949 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
950 pos = rb_entry(nd, struct hist_entry, rb_node);
951 ret += hist_entry__fprintf(fp, pos, total_samples);
Arnaldo Carvalho de Melo3a4b8cc2009-05-26 16:19:04 -0300952 }
953
954 return ret;
955}
956
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +0200957
Ingo Molnar53cb8bc2009-05-26 09:17:18 +0200958static int __cmd_report(void)
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300959{
960 unsigned long offset = 0;
961 unsigned long head = 0;
962 struct stat stat;
963 char *buf;
964 event_t *event;
965 int ret, rc = EXIT_FAILURE;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +0200966 uint32_t size;
Ingo Molnarf49515b2009-05-26 19:03:36 +0200967 unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300968
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300969 input = open(input_name, O_RDONLY);
970 if (input < 0) {
971 perror("failed to open file");
972 exit(-1);
973 }
974
975 ret = fstat(input, &stat);
976 if (ret < 0) {
977 perror("failed to stat file");
978 exit(-1);
979 }
980
981 if (!stat.st_size) {
982 fprintf(stderr, "zero-sized file, nothing to do!\n");
983 exit(0);
984 }
985
Peter Zijlstra450aaa22009-05-27 20:20:23 +0200986 if (load_kernel() < 0) {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -0300987 perror("failed to open kallsyms");
988 return EXIT_FAILURE;
989 }
990
991remap:
992 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
993 MAP_SHARED, input, offset);
994 if (buf == MAP_FAILED) {
995 perror("failed to mmap file");
996 exit(-1);
997 }
998
999more:
1000 event = (event_t *)(buf + head);
1001
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001002 size = event->header.size;
1003 if (!size)
1004 size = 8;
1005
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001006 if (head + event->header.size >= page_size * mmap_window) {
1007 unsigned long shift = page_size * (head / page_size);
1008 int ret;
1009
1010 ret = munmap(buf, page_size * mmap_window);
1011 assert(ret == 0);
1012
1013 offset += shift;
1014 head -= shift;
1015 goto remap;
1016 }
1017
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001018 size = event->header.size;
1019 if (!size)
1020 goto broken_event;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001021
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001022 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
1023 char level;
1024 int show = 0;
1025 struct dso *dso = NULL;
1026 struct thread *thread = threads__findnew(event->ip.pid);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +02001027 uint64_t ip = event->ip.ip;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001028 struct map *map = NULL;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001029
Ingo Molnar97b07b62009-05-26 18:48:58 +02001030 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +02001031 fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
1032 (void *)(offset + head),
1033 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001034 event->header.misc,
1035 event->ip.pid,
Ingo Molnar16f762a2009-05-27 09:10:38 +02001036 (void *)(long)ip);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001037 }
1038
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -03001039 if (thread == NULL) {
1040 fprintf(stderr, "problem processing %d event, bailing out\n",
1041 event->header.type);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001042 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -03001043 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001044
1045 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
1046 show = SHOW_KERNEL;
1047 level = 'k';
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001048
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001049 dso = kernel_dso;
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001050
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001051 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
Ingo Molnar16f762a2009-05-27 09:10:38 +02001052
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001053 show = SHOW_USER;
1054 level = '.';
Ingo Molnar16f762a2009-05-27 09:10:38 +02001055
1056 map = thread__find_map(thread, ip);
Peter Zijlstraf17e04a2009-05-26 15:30:22 +02001057 if (map != NULL) {
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001058 dso = map->dso;
Peter Zijlstraf17e04a2009-05-26 15:30:22 +02001059 ip -= map->start + map->pgoff;
1060 }
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001061
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001062 } else {
1063 show = SHOW_HV;
1064 level = 'H';
1065 }
1066
1067 if (show & show_mask) {
Peter Zijlstraf17e04a2009-05-26 15:30:22 +02001068 struct symbol *sym = dso__find_symbol(dso, ip);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001069
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001070 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
1071 fprintf(stderr,
1072 "problem incrementing symbol count, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001073 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -03001074 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001075 }
1076 total++;
1077 } else switch (event->header.type) {
1078 case PERF_EVENT_MMAP: {
1079 struct thread *thread = threads__findnew(event->mmap.pid);
1080 struct map *map = map__new(&event->mmap);
1081
Ingo Molnar97b07b62009-05-26 18:48:58 +02001082 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +02001083 fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
1084 (void *)(offset + head),
1085 (void *)(long)(event->header.size),
Ingo Molnar16f762a2009-05-27 09:10:38 +02001086 (void *)(long)event->mmap.start,
1087 (void *)(long)event->mmap.len,
1088 (void *)(long)event->mmap.pgoff,
Ingo Molnar97b07b62009-05-26 18:48:58 +02001089 event->mmap.filename);
1090 }
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -03001091 if (thread == NULL || map == NULL) {
1092 fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001093 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -03001094 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001095 thread__insert_map(thread, map);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001096 total_mmap++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001097 break;
1098 }
1099 case PERF_EVENT_COMM: {
1100 struct thread *thread = threads__findnew(event->comm.pid);
1101
Ingo Molnar97b07b62009-05-26 18:48:58 +02001102 if (dump_trace) {
Ingo Molnarf49515b2009-05-26 19:03:36 +02001103 fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
1104 (void *)(offset + head),
1105 (void *)(long)(event->header.size),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001106 event->comm.comm, event->comm.pid);
1107 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001108 if (thread == NULL ||
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -03001109 thread__set_comm(thread, event->comm.comm)) {
1110 fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n");
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001111 goto done;
Arnaldo Carvalho de Meloce7e4362009-05-19 09:30:23 -03001112 }
Ingo Molnar97b07b62009-05-26 18:48:58 +02001113 total_comm++;
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001114 break;
1115 }
Ingo Molnar97b07b62009-05-26 18:48:58 +02001116 default: {
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001117broken_event:
Peter Zijlstrab7a16ea2009-05-27 13:35:35 +02001118 if (dump_trace)
1119 fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
1120 (void *)(offset + head),
1121 (void *)(long)(event->header.size),
1122 event->header.type);
1123
Ingo Molnar3e706112009-05-26 18:53:17 +02001124 total_unknown++;
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001125
1126 /*
1127 * assume we lost track of the stream, check alignment, and
1128 * increment a single u64 in the hope to catch on again 'soon'.
1129 */
1130
1131 if (unlikely(head & 7))
1132 head &= ~7ULL;
1133
1134 size = 8;
Ingo Molnar97b07b62009-05-26 18:48:58 +02001135 }
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001136 }
1137
Peter Zijlstra6142f9e2009-05-26 20:51:47 +02001138 head += size;
Ingo Molnarf49515b2009-05-26 19:03:36 +02001139
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001140 if (offset + head < stat.st_size)
1141 goto more;
1142
1143 rc = EXIT_SUCCESS;
1144done:
1145 close(input);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001146
1147 if (dump_trace) {
Ingo Molnar3e706112009-05-26 18:53:17 +02001148 fprintf(stderr, " IP events: %10ld\n", total);
1149 fprintf(stderr, " mmap events: %10ld\n", total_mmap);
1150 fprintf(stderr, " comm events: %10ld\n", total_comm);
1151 fprintf(stderr, " unknown events: %10ld\n", total_unknown);
Ingo Molnar97b07b62009-05-26 18:48:58 +02001152
1153 return 0;
1154 }
1155
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001156 if (verbose >= 2)
Ingo Molnar16f762a2009-05-27 09:10:38 +02001157 dsos__fprintf(stdout);
Ingo Molnar16f762a2009-05-27 09:10:38 +02001158
Peter Zijlstrae7fb08b2009-05-27 20:20:24 +02001159 output__resort();
1160 output__fprintf(stdout, total);
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001161
Arnaldo Carvalho de Melo8fa66bd2009-05-18 12:45:42 -03001162 return rc;
1163}
1164
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001165static const char * const report_usage[] = {
1166 "perf report [<options>] <command>",
1167 NULL
1168};
1169
1170static const struct option options[] = {
1171 OPT_STRING('i', "input", &input_name, "file",
1172 "input file name"),
Arnaldo Carvalho de Melo815e7772009-05-26 19:46:14 -03001173 OPT_BOOLEAN('v', "verbose", &verbose,
1174 "be more verbose (show symbol address, etc)"),
Ingo Molnar97b07b62009-05-26 18:48:58 +02001175 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1176 "dump raw trace in ASCII"),
Peter Zijlstra450aaa22009-05-27 20:20:23 +02001177 OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
Peter Zijlstra37f440c2009-05-27 20:20:26 +02001178 OPT_STRING('s', "sort", &sort_order, "foo", "bar"),
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001179 OPT_END()
1180};
1181
1182int cmd_report(int argc, const char **argv, const char *prefix)
1183{
1184 elf_version(EV_CURRENT);
1185
1186 page_size = getpagesize();
1187
1188 parse_options(argc, argv, options, report_usage, 0);
1189
Peter Zijlstra1aa16732009-05-27 20:20:25 +02001190 setup_sorting();
1191
Ingo Molnara930d2c2009-05-27 09:50:13 +02001192 setup_pager();
1193
Ingo Molnar53cb8bc2009-05-26 09:17:18 +02001194 return __cmd_report();
1195}