Commit | Line | Data |
---|---|---|
53cb8bc2 | 1 | #include "util/util.h" |
16f762a2 | 2 | #include "builtin.h" |
53cb8bc2 IM |
3 | |
4 | #include <libelf.h> | |
62eb9390 ACM |
5 | #include <gelf.h> |
6 | #include <elf.h> | |
53cb8bc2 | 7 | |
35a50c8a | 8 | #include "util/list.h" |
a930d2c0 | 9 | #include "util/cache.h" |
35a50c8a | 10 | #include "util/rbtree.h" |
8fa66bdc | 11 | |
53cb8bc2 IM |
12 | #include "perf.h" |
13 | ||
14 | #include "util/parse-options.h" | |
15 | #include "util/parse-events.h" | |
16 | ||
8fa66bdc ACM |
17 | #define SHOW_KERNEL 1 |
18 | #define SHOW_USER 2 | |
19 | #define SHOW_HV 4 | |
20 | ||
23ac9cbe | 21 | static char const *input_name = "perf.data"; |
8fa66bdc ACM |
22 | static int input; |
23 | static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; | |
24 | ||
97b07b69 | 25 | static int dump_trace = 0; |
16f762a2 | 26 | static int verbose; |
97b07b69 | 27 | |
8fa66bdc ACM |
28 | static unsigned long page_size; |
29 | static unsigned long mmap_window = 32; | |
30 | ||
53cb8bc2 | 31 | const char *perf_event_names[] = { |
8fa66bdc ACM |
32 | [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP", |
33 | [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP", | |
34 | [PERF_EVENT_COMM] = " PERF_EVENT_COMM", | |
35 | }; | |
36 | ||
37 | struct ip_event { | |
38 | struct perf_event_header header; | |
39 | __u64 ip; | |
40 | __u32 pid, tid; | |
41 | }; | |
42 | struct 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 | }; | |
50 | struct comm_event { | |
51 | struct perf_event_header header; | |
52 | __u32 pid,tid; | |
53 | char comm[16]; | |
54 | }; | |
55 | ||
56 | typedef 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 | ||
8fa66bdc | 63 | struct symbol { |
16f762a2 IM |
64 | struct rb_node rb_node; |
65 | __u64 start; | |
66 | __u64 end; | |
67 | char name[0]; | |
8fa66bdc ACM |
68 | }; |
69 | ||
70 | static 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 | ||
83 | static void symbol__delete(struct symbol *self) | |
84 | { | |
85 | free(self); | |
86 | } | |
87 | ||
88 | static size_t symbol__fprintf(struct symbol *self, FILE *fp) | |
89 | { | |
16f762a2 | 90 | return fprintf(fp, " %llx-%llx %s\n", |
8fa66bdc ACM |
91 | self->start, self->end, self->name); |
92 | } | |
93 | ||
94 | struct dso { | |
95 | struct list_head node; | |
35a50c8a | 96 | struct rb_root syms; |
8fa66bdc ACM |
97 | char name[0]; |
98 | }; | |
99 | ||
100 | static 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); | |
35a50c8a | 106 | self->syms = RB_ROOT; |
8fa66bdc ACM |
107 | } |
108 | ||
109 | return self; | |
110 | } | |
111 | ||
8fa66bdc ACM |
112 | static void dso__delete_symbols(struct dso *self) |
113 | { | |
35a50c8a ACM |
114 | struct symbol *pos; |
115 | struct rb_node *next = rb_first(&self->syms); | |
8fa66bdc | 116 | |
35a50c8a ACM |
117 | while (next) { |
118 | pos = rb_entry(next, struct symbol, rb_node); | |
119 | next = rb_next(&pos->rb_node); | |
8fa66bdc | 120 | symbol__delete(pos); |
35a50c8a | 121 | } |
8fa66bdc ACM |
122 | } |
123 | ||
124 | static void dso__delete(struct dso *self) | |
125 | { | |
8fa66bdc ACM |
126 | dso__delete_symbols(self); |
127 | free(self); | |
128 | } | |
129 | ||
130 | static void dso__insert_symbol(struct dso *self, struct symbol *sym) | |
131 | { | |
35a50c8a ACM |
132 | 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); | |
8fa66bdc ACM |
147 | } |
148 | ||
149 | static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip) | |
150 | { | |
16f762a2 IM |
151 | struct rb_node *n; |
152 | ||
8fa66bdc ACM |
153 | if (self == NULL) |
154 | return NULL; | |
155 | ||
16f762a2 | 156 | n = self->syms.rb_node; |
8fa66bdc | 157 | |
35a50c8a ACM |
158 | 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 | } | |
8fa66bdc ACM |
168 | |
169 | return NULL; | |
170 | } | |
171 | ||
62eb9390 ACM |
172 | /** |
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 | ||
184 | static inline uint8_t elf_sym__type(const GElf_Sym *sym) | |
185 | { | |
186 | return GELF_ST_TYPE(sym->st_info); | |
187 | } | |
188 | ||
53cb8bc2 | 189 | static inline int elf_sym__is_function(const GElf_Sym *sym) |
62eb9390 ACM |
190 | { |
191 | return elf_sym__type(sym) == STT_FUNC && | |
192 | sym->st_name != 0 && | |
b7a16eac PZ |
193 | sym->st_shndx != SHN_UNDEF && |
194 | sym->st_size != 0; | |
62eb9390 ACM |
195 | } |
196 | ||
197 | static 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 | ||
203 | static 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 | ||
b7a16eac | 226 | static int dso__load_sym(struct dso *self, int fd, char *name) |
8fa66bdc | 227 | { |
16f762a2 IM |
228 | Elf_Data *symstrs; |
229 | uint32_t nr_syms; | |
b7a16eac | 230 | int err = -1; |
16f762a2 IM |
231 | 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; | |
b7a16eac | 238 | int nr = 0; |
62eb9390 | 239 | |
16f762a2 | 240 | elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); |
62eb9390 ACM |
241 | if (elf == NULL) { |
242 | fprintf(stderr, "%s: cannot read %s ELF file.\n", | |
b7a16eac | 243 | __func__, name); |
62eb9390 ACM |
244 | goto out_close; |
245 | } | |
246 | ||
62eb9390 ACM |
247 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
248 | fprintf(stderr, "%s: cannot get elf header.\n", __func__); | |
249 | goto out_elf_end; | |
250 | } | |
251 | ||
16f762a2 | 252 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL); |
62eb9390 ACM |
253 | 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 | ||
16f762a2 | 259 | syms = elf_getdata(sec, NULL); |
62eb9390 ACM |
260 | 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 | ||
16f762a2 | 267 | symstrs = elf_getdata(sec, NULL); |
62eb9390 ACM |
268 | if (symstrs == NULL) |
269 | goto out_elf_end; | |
270 | ||
16f762a2 | 271 | nr_syms = shdr.sh_size / shdr.sh_entsize; |
62eb9390 | 272 | |
62eb9390 | 273 | elf_symtab__for_each_symbol(syms, nr_syms, index, sym) { |
f17e04af PZ |
274 | struct symbol *f; |
275 | ||
62eb9390 ACM |
276 | if (!elf_sym__is_function(&sym)) |
277 | continue; | |
f17e04af PZ |
278 | |
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) | |
62eb9390 ACM |
289 | goto out_elf_end; |
290 | ||
291 | dso__insert_symbol(self, f); | |
b7a16eac PZ |
292 | |
293 | nr++; | |
62eb9390 ACM |
294 | } |
295 | ||
b7a16eac | 296 | err = nr; |
62eb9390 ACM |
297 | out_elf_end: |
298 | elf_end(elf); | |
299 | out_close: | |
62eb9390 | 300 | return err; |
8fa66bdc ACM |
301 | } |
302 | ||
b7a16eac PZ |
303 | static 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 | ||
314 | more: | |
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 | ||
344 | out: | |
345 | free(name); | |
346 | return ret; | |
347 | } | |
348 | ||
8fa66bdc ACM |
349 | static size_t dso__fprintf(struct dso *self, FILE *fp) |
350 | { | |
8fa66bdc ACM |
351 | size_t ret = fprintf(fp, "dso: %s\n", self->name); |
352 | ||
35a50c8a ACM |
353 | 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); | |
8fa66bdc | 356 | ret += symbol__fprintf(pos, fp); |
35a50c8a | 357 | } |
8fa66bdc ACM |
358 | |
359 | return ret; | |
360 | } | |
361 | ||
362 | static LIST_HEAD(dsos); | |
363 | static struct dso *kernel_dso; | |
364 | ||
365 | static void dsos__add(struct dso *dso) | |
366 | { | |
367 | list_add_tail(&dso->node, &dsos); | |
368 | } | |
369 | ||
370 | static 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 | ||
380 | static struct dso *dsos__findnew(const char *name) | |
381 | { | |
382 | struct dso *dso = dsos__find(name); | |
b7a16eac | 383 | int nr; |
8fa66bdc ACM |
384 | |
385 | if (dso == NULL) { | |
386 | dso = dso__new(name); | |
b7a16eac PZ |
387 | if (!dso) |
388 | goto out_delete_dso; | |
389 | ||
390 | nr = dso__load(dso); | |
391 | if (nr < 0) { | |
392 | fprintf(stderr, "Failed to open: %s\n", name); | |
8fa66bdc | 393 | goto out_delete_dso; |
b7a16eac PZ |
394 | } |
395 | if (!nr) { | |
396 | fprintf(stderr, | |
397 | "Failed to find debug symbols for: %s, maybe install a debug package?\n", | |
398 | name); | |
399 | } | |
8fa66bdc ACM |
400 | |
401 | dsos__add(dso); | |
402 | } | |
403 | ||
404 | return dso; | |
405 | ||
406 | out_delete_dso: | |
407 | dso__delete(dso); | |
408 | return NULL; | |
409 | } | |
410 | ||
16f762a2 | 411 | static void dsos__fprintf(FILE *fp) |
8fa66bdc ACM |
412 | { |
413 | struct dso *pos; | |
414 | ||
415 | list_for_each_entry(pos, &dsos, node) | |
416 | dso__fprintf(pos, fp); | |
417 | } | |
418 | ||
d8d1656e ACM |
419 | static 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 | */ | |
16f762a2 | 434 | static int hex2long(char *ptr, unsigned long *long_val) |
d8d1656e ACM |
435 | { |
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 | ||
8fa66bdc ACM |
452 | static int load_kallsyms(void) |
453 | { | |
af83632f IM |
454 | struct rb_node *nd, *prevnd; |
455 | char *line = NULL; | |
456 | FILE *file; | |
457 | size_t n; | |
458 | ||
8fa66bdc ACM |
459 | kernel_dso = dso__new("[kernel]"); |
460 | if (kernel_dso == NULL) | |
461 | return -1; | |
462 | ||
af83632f | 463 | file = fopen("/proc/kallsyms", "r"); |
8fa66bdc ACM |
464 | if (file == NULL) |
465 | goto out_delete_dso; | |
466 | ||
8fa66bdc | 467 | while (!feof(file)) { |
d8d1656e | 468 | unsigned long start; |
af83632f IM |
469 | struct symbol *sym; |
470 | int line_len, len; | |
471 | char symbol_type; | |
472 | ||
473 | line_len = getline(&line, &n, file); | |
d8d1656e | 474 | if (line_len < 0) |
8fa66bdc ACM |
475 | break; |
476 | ||
477 | if (!line) | |
478 | goto out_delete_dso; | |
479 | ||
d8d1656e | 480 | line[--line_len] = '\0'; /* \n */ |
abd54f68 | 481 | |
af83632f IM |
482 | len = hex2long(line, &start); |
483 | ||
03f6316d ACM |
484 | len++; |
485 | if (len + 2 >= line_len) | |
486 | continue; | |
487 | ||
af83632f | 488 | symbol_type = toupper(line[len]); |
03f6316d ACM |
489 | /* |
490 | * We're interested only in code ('T'ext) | |
491 | */ | |
af83632f | 492 | if (symbol_type != 'T' && symbol_type != 'W') |
d8d1656e ACM |
493 | continue; |
494 | /* | |
495 | * Well fix up the end later, when we have all sorted. | |
496 | */ | |
af83632f | 497 | sym = symbol__new(start, 0xdead, line + len + 2); |
abd54f68 | 498 | |
d8d1656e ACM |
499 | if (sym == NULL) |
500 | goto out_delete_dso; | |
501 | ||
502 | dso__insert_symbol(kernel_dso, sym); | |
8fa66bdc ACM |
503 | } |
504 | ||
abd54f68 ACM |
505 | /* |
506 | * Now that we have all sorted out, just set the ->end of all | |
507 | * symbols | |
508 | */ | |
af83632f | 509 | prevnd = rb_first(&kernel_dso->syms); |
abd54f68 ACM |
510 | |
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 | ||
8fa66bdc ACM |
522 | dsos__add(kernel_dso); |
523 | free(line); | |
524 | fclose(file); | |
af83632f | 525 | |
8fa66bdc ACM |
526 | return 0; |
527 | ||
59d81029 ACM |
528 | out_delete_line: |
529 | free(line); | |
8fa66bdc ACM |
530 | out_delete_dso: |
531 | dso__delete(kernel_dso); | |
532 | return -1; | |
533 | } | |
534 | ||
535 | struct map { | |
536 | struct list_head node; | |
537 | uint64_t start; | |
538 | uint64_t end; | |
539 | uint64_t pgoff; | |
540 | struct dso *dso; | |
541 | }; | |
542 | ||
543 | static 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; | |
557 | out_delete: | |
558 | free(self); | |
559 | return NULL; | |
560 | } | |
561 | ||
3a4b8cc7 ACM |
562 | struct thread; |
563 | ||
564 | static const char *thread__name(struct thread *self, char *bf, size_t size); | |
565 | ||
8fa66bdc | 566 | struct symhist { |
ce7e4365 | 567 | struct rb_node rb_node; |
8fa66bdc ACM |
568 | struct dso *dso; |
569 | struct symbol *sym; | |
3a4b8cc7 | 570 | struct thread *thread; |
ce7e4365 | 571 | uint64_t ip; |
8fa66bdc ACM |
572 | uint32_t count; |
573 | char level; | |
574 | }; | |
575 | ||
ce7e4365 | 576 | static struct symhist *symhist__new(struct symbol *sym, uint64_t ip, |
3a4b8cc7 ACM |
577 | struct thread *thread, struct dso *dso, |
578 | char level) | |
8fa66bdc ACM |
579 | { |
580 | struct symhist *self = malloc(sizeof(*self)); | |
581 | ||
582 | if (self != NULL) { | |
3a4b8cc7 ACM |
583 | self->sym = sym; |
584 | self->thread = thread; | |
585 | self->ip = ip; | |
586 | self->dso = dso; | |
587 | self->level = level; | |
588 | self->count = 1; | |
8fa66bdc ACM |
589 | } |
590 | ||
591 | return self; | |
592 | } | |
593 | ||
8fa66bdc ACM |
594 | static void symhist__inc(struct symhist *self) |
595 | { | |
596 | ++self->count; | |
597 | } | |
598 | ||
3a4b8cc7 ACM |
599 | static size_t |
600 | symhist__fprintf(struct symhist *self, uint64_t total_samples, FILE *fp) | |
8fa66bdc | 601 | { |
3a4b8cc7 ACM |
602 | char bf[32]; |
603 | size_t ret; | |
604 | ||
605 | if (total_samples) | |
b7a16eac | 606 | ret = fprintf(fp, "%5.2f%% ", (self->count * 100.0) / total_samples); |
3a4b8cc7 | 607 | else |
b7a16eac | 608 | ret = fprintf(fp, "%12d ", self->count); |
3a4b8cc7 | 609 | |
815e777f | 610 | ret += fprintf(fp, "%14s [%c] ", |
3a4b8cc7 | 611 | thread__name(self->thread, bf, sizeof(bf)), |
815e777f ACM |
612 | self->level); |
613 | ||
614 | if (verbose) | |
615 | ret += fprintf(fp, "%#018llx ", (unsigned long long)self->ip); | |
8fa66bdc ACM |
616 | |
617 | if (self->level != '.') | |
3a4b8cc7 ACM |
618 | ret += fprintf(fp, "%s\n", |
619 | self->sym ? self->sym->name : "<unknown>"); | |
8fa66bdc | 620 | else |
3a4b8cc7 | 621 | ret += fprintf(fp, "%s: %s\n", |
f17e04af | 622 | self->dso ? self->dso->name : "<unknown>", |
8fa66bdc | 623 | self->sym ? self->sym->name : "<unknown>"); |
3a4b8cc7 | 624 | return ret; |
8fa66bdc ACM |
625 | } |
626 | ||
627 | struct thread { | |
ce7e4365 | 628 | struct rb_node rb_node; |
8fa66bdc | 629 | struct list_head maps; |
ce7e4365 | 630 | struct rb_root symhists; |
8fa66bdc ACM |
631 | pid_t pid; |
632 | char *comm; | |
633 | }; | |
634 | ||
3a4b8cc7 ACM |
635 | static 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 | ||
8fa66bdc ACM |
644 | static 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); | |
ce7e4365 | 652 | self->symhists = RB_ROOT; |
8fa66bdc ACM |
653 | } |
654 | ||
655 | return self; | |
656 | } | |
657 | ||
ce7e4365 ACM |
658 | static int thread__symbol_incnew(struct thread *self, struct symbol *sym, |
659 | uint64_t ip, struct dso *dso, char level) | |
8fa66bdc | 660 | { |
ce7e4365 ACM |
661 | struct rb_node **p = &self->symhists.rb_node; |
662 | struct rb_node *parent = NULL; | |
663 | struct symhist *sh; | |
8fa66bdc | 664 | |
ce7e4365 | 665 | while (*p != NULL) { |
16f762a2 IM |
666 | uint64_t start; |
667 | ||
ce7e4365 ACM |
668 | parent = *p; |
669 | sh = rb_entry(parent, struct symhist, rb_node); | |
8fa66bdc | 670 | |
ce7e4365 ACM |
671 | if (sh->sym == sym || ip == sh->ip) { |
672 | symhist__inc(sh); | |
673 | return 0; | |
674 | } | |
8fa66bdc | 675 | |
ce7e4365 | 676 | /* Handle unresolved symbols too */ |
16f762a2 | 677 | start = !sh->sym ? sh->ip : sh->sym->start; |
8fa66bdc | 678 | |
ce7e4365 ACM |
679 | if (ip < start) |
680 | p = &(*p)->rb_left; | |
681 | else | |
682 | p = &(*p)->rb_right; | |
8fa66bdc ACM |
683 | } |
684 | ||
3a4b8cc7 | 685 | sh = symhist__new(sym, ip, self, dso, level); |
ce7e4365 ACM |
686 | if (sh == NULL) |
687 | return -ENOMEM; | |
688 | rb_link_node(&sh->rb_node, parent, p); | |
689 | rb_insert_color(&sh->rb_node, &self->symhists); | |
8fa66bdc | 690 | return 0; |
8fa66bdc ACM |
691 | } |
692 | ||
693 | static int thread__set_comm(struct thread *self, const char *comm) | |
694 | { | |
695 | self->comm = strdup(comm); | |
696 | return self->comm ? 0 : -ENOMEM; | |
697 | } | |
698 | ||
8fa66bdc ACM |
699 | static size_t thread__fprintf(struct thread *self, FILE *fp) |
700 | { | |
8fa66bdc | 701 | int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm); |
ce7e4365 | 702 | struct rb_node *nd; |
8fa66bdc | 703 | |
ce7e4365 ACM |
704 | for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) { |
705 | struct symhist *pos = rb_entry(nd, struct symhist, rb_node); | |
16f762a2 | 706 | |
3a4b8cc7 | 707 | ret += symhist__fprintf(pos, 0, fp); |
ce7e4365 | 708 | } |
8fa66bdc ACM |
709 | |
710 | return ret; | |
711 | } | |
712 | ||
16f762a2 | 713 | static struct rb_root threads; |
8fa66bdc | 714 | |
ce7e4365 | 715 | static struct thread *threads__findnew(pid_t pid) |
8fa66bdc | 716 | { |
ce7e4365 ACM |
717 | struct rb_node **p = &threads.rb_node; |
718 | struct rb_node *parent = NULL; | |
719 | struct thread *th; | |
8fa66bdc | 720 | |
ce7e4365 ACM |
721 | while (*p != NULL) { |
722 | parent = *p; | |
723 | th = rb_entry(parent, struct thread, rb_node); | |
8fa66bdc | 724 | |
ce7e4365 ACM |
725 | if (th->pid == pid) |
726 | return th; | |
8fa66bdc | 727 | |
ce7e4365 ACM |
728 | if (pid < th->pid) |
729 | p = &(*p)->rb_left; | |
730 | else | |
731 | p = &(*p)->rb_right; | |
8fa66bdc ACM |
732 | } |
733 | ||
ce7e4365 ACM |
734 | 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; | |
8fa66bdc ACM |
740 | } |
741 | ||
742 | static void thread__insert_map(struct thread *self, struct map *map) | |
743 | { | |
744 | list_add_tail(&map->node, &self->maps); | |
745 | } | |
746 | ||
747 | static struct map *thread__find_map(struct thread *self, uint64_t ip) | |
748 | { | |
16f762a2 IM |
749 | struct map *pos; |
750 | ||
8fa66bdc ACM |
751 | if (self == NULL) |
752 | return NULL; | |
753 | ||
8fa66bdc ACM |
754 | 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 | ||
16f762a2 | 761 | static void threads__fprintf(FILE *fp) |
8fa66bdc | 762 | { |
ce7e4365 ACM |
763 | 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); | |
8fa66bdc | 766 | thread__fprintf(pos, fp); |
ce7e4365 | 767 | } |
8fa66bdc ACM |
768 | } |
769 | ||
16f762a2 | 770 | static struct rb_root global_symhists; |
3a4b8cc7 ACM |
771 | |
772 | static 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 | ||
793 | static 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 | ||
812 | static 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 | ||
53cb8bc2 | 825 | static int __cmd_report(void) |
8fa66bdc ACM |
826 | { |
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; | |
6142f9ec | 833 | uint32_t size; |
f49515b1 | 834 | unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0; |
8fa66bdc | 835 | |
8fa66bdc ACM |
836 | 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 | ||
858 | remap: | |
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 | ||
866 | more: | |
867 | event = (event_t *)(buf + head); | |
868 | ||
6142f9ec PZ |
869 | size = event->header.size; |
870 | if (!size) | |
871 | size = 8; | |
872 | ||
8fa66bdc ACM |
873 | 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 | ||
6142f9ec PZ |
885 | size = event->header.size; |
886 | if (!size) | |
887 | goto broken_event; | |
8fa66bdc | 888 | |
8fa66bdc ACM |
889 | 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); | |
f17e04af | 894 | uint64_t ip = event->ip.ip; |
8fa66bdc | 895 | |
97b07b69 | 896 | if (dump_trace) { |
f49515b1 IM |
897 | fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n", |
898 | (void *)(offset + head), | |
899 | (void *)(long)(event->header.size), | |
97b07b69 IM |
900 | event->header.misc, |
901 | event->ip.pid, | |
16f762a2 | 902 | (void *)(long)ip); |
97b07b69 IM |
903 | } |
904 | ||
ce7e4365 ACM |
905 | if (thread == NULL) { |
906 | fprintf(stderr, "problem processing %d event, bailing out\n", | |
907 | event->header.type); | |
8fa66bdc | 908 | goto done; |
ce7e4365 | 909 | } |
8fa66bdc ACM |
910 | |
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) { | |
16f762a2 IM |
916 | struct map *map; |
917 | ||
8fa66bdc ACM |
918 | show = SHOW_USER; |
919 | level = '.'; | |
16f762a2 IM |
920 | |
921 | map = thread__find_map(thread, ip); | |
f17e04af | 922 | if (map != NULL) { |
8fa66bdc | 923 | dso = map->dso; |
f17e04af PZ |
924 | ip -= map->start + map->pgoff; |
925 | } | |
8fa66bdc ACM |
926 | } else { |
927 | show = SHOW_HV; | |
928 | level = 'H'; | |
929 | } | |
930 | ||
931 | if (show & show_mask) { | |
f17e04af | 932 | struct symbol *sym = dso__find_symbol(dso, ip); |
8fa66bdc | 933 | |
f17e04af | 934 | if (thread__symbol_incnew(thread, sym, ip, dso, level)) { |
ce7e4365 | 935 | fprintf(stderr, "problem incrementing symbol count, bailing out\n"); |
8fa66bdc | 936 | goto done; |
ce7e4365 | 937 | } |
8fa66bdc ACM |
938 | } |
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 | ||
97b07b69 | 945 | if (dump_trace) { |
f49515b1 IM |
946 | fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n", |
947 | (void *)(offset + head), | |
948 | (void *)(long)(event->header.size), | |
16f762a2 IM |
949 | (void *)(long)event->mmap.start, |
950 | (void *)(long)event->mmap.len, | |
951 | (void *)(long)event->mmap.pgoff, | |
97b07b69 IM |
952 | event->mmap.filename); |
953 | } | |
ce7e4365 ACM |
954 | if (thread == NULL || map == NULL) { |
955 | fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n"); | |
8fa66bdc | 956 | goto done; |
ce7e4365 | 957 | } |
8fa66bdc | 958 | thread__insert_map(thread, map); |
97b07b69 | 959 | total_mmap++; |
8fa66bdc ACM |
960 | break; |
961 | } | |
962 | case PERF_EVENT_COMM: { | |
963 | struct thread *thread = threads__findnew(event->comm.pid); | |
964 | ||
97b07b69 | 965 | if (dump_trace) { |
f49515b1 IM |
966 | fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n", |
967 | (void *)(offset + head), | |
968 | (void *)(long)(event->header.size), | |
97b07b69 IM |
969 | event->comm.comm, event->comm.pid); |
970 | } | |
8fa66bdc | 971 | if (thread == NULL || |
ce7e4365 ACM |
972 | thread__set_comm(thread, event->comm.comm)) { |
973 | fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n"); | |
8fa66bdc | 974 | goto done; |
ce7e4365 | 975 | } |
97b07b69 | 976 | total_comm++; |
8fa66bdc ACM |
977 | break; |
978 | } | |
97b07b69 | 979 | default: { |
6142f9ec | 980 | broken_event: |
b7a16eac PZ |
981 | 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 | ||
3e706114 | 987 | total_unknown++; |
6142f9ec PZ |
988 | |
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; | |
97b07b69 | 998 | } |
8fa66bdc ACM |
999 | } |
1000 | ||
6142f9ec | 1001 | head += size; |
f49515b1 | 1002 | |
8fa66bdc ACM |
1003 | if (offset + head < stat.st_size) |
1004 | goto more; | |
1005 | ||
1006 | rc = EXIT_SUCCESS; | |
1007 | done: | |
1008 | close(input); | |
97b07b69 IM |
1009 | |
1010 | if (dump_trace) { | |
3e706114 IM |
1011 | 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); | |
97b07b69 IM |
1015 | |
1016 | return 0; | |
1017 | } | |
1018 | ||
16f762a2 IM |
1019 | if (verbose >= 2) { |
1020 | dsos__fprintf(stdout); | |
1021 | threads__fprintf(stdout); | |
1022 | } | |
1023 | ||
3a4b8cc7 ACM |
1024 | threads__sort_symhists(); |
1025 | threads__symhists_fprintf(total, stdout); | |
8fa66bdc | 1026 | |
8fa66bdc ACM |
1027 | return rc; |
1028 | } | |
1029 | ||
53cb8bc2 IM |
1030 | static const char * const report_usage[] = { |
1031 | "perf report [<options>] <command>", | |
1032 | NULL | |
1033 | }; | |
1034 | ||
1035 | static const struct option options[] = { | |
1036 | OPT_STRING('i', "input", &input_name, "file", | |
1037 | "input file name"), | |
815e777f ACM |
1038 | OPT_BOOLEAN('v', "verbose", &verbose, |
1039 | "be more verbose (show symbol address, etc)"), | |
97b07b69 IM |
1040 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
1041 | "dump raw trace in ASCII"), | |
53cb8bc2 IM |
1042 | OPT_END() |
1043 | }; | |
1044 | ||
1045 | int 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 | ||
a930d2c0 IM |
1053 | setup_pager(); |
1054 | ||
53cb8bc2 IM |
1055 | return __cmd_report(); |
1056 | } |