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 && | |
193 | sym->st_shndx != SHN_UNDEF; | |
194 | } | |
195 | ||
196 | static inline const char *elf_sym__name(const GElf_Sym *sym, | |
197 | const Elf_Data *symstrs) | |
198 | { | |
199 | return symstrs->d_buf + sym->st_name; | |
200 | } | |
201 | ||
202 | static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, | |
203 | GElf_Shdr *shp, const char *name, | |
204 | size_t *index) | |
205 | { | |
206 | Elf_Scn *sec = NULL; | |
207 | size_t cnt = 1; | |
208 | ||
209 | while ((sec = elf_nextscn(elf, sec)) != NULL) { | |
210 | char *str; | |
211 | ||
212 | gelf_getshdr(sec, shp); | |
213 | str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); | |
214 | if (!strcmp(name, str)) { | |
215 | if (index) | |
216 | *index = cnt; | |
217 | break; | |
218 | } | |
219 | ++cnt; | |
220 | } | |
221 | ||
222 | return sec; | |
223 | } | |
224 | ||
8fa66bdc ACM |
225 | static int dso__load(struct dso *self) |
226 | { | |
16f762a2 IM |
227 | Elf_Data *symstrs; |
228 | uint32_t nr_syms; | |
229 | int fd, err = -1; | |
230 | uint32_t index; | |
231 | GElf_Ehdr ehdr; | |
232 | GElf_Shdr shdr; | |
233 | Elf_Data *syms; | |
234 | GElf_Sym sym; | |
235 | Elf_Scn *sec; | |
236 | Elf *elf; | |
62eb9390 | 237 | |
16f762a2 IM |
238 | |
239 | fd = open(self->name, O_RDONLY); | |
62eb9390 ACM |
240 | if (fd == -1) |
241 | return -1; | |
242 | ||
16f762a2 | 243 | elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); |
62eb9390 ACM |
244 | if (elf == NULL) { |
245 | fprintf(stderr, "%s: cannot read %s ELF file.\n", | |
246 | __func__, self->name); | |
247 | goto out_close; | |
248 | } | |
249 | ||
62eb9390 ACM |
250 | if (gelf_getehdr(elf, &ehdr) == NULL) { |
251 | fprintf(stderr, "%s: cannot get elf header.\n", __func__); | |
252 | goto out_elf_end; | |
253 | } | |
254 | ||
16f762a2 | 255 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL); |
62eb9390 ACM |
256 | if (sec == NULL) |
257 | sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL); | |
258 | ||
259 | if (sec == NULL) | |
260 | goto out_elf_end; | |
261 | ||
16f762a2 | 262 | syms = elf_getdata(sec, NULL); |
62eb9390 ACM |
263 | if (syms == NULL) |
264 | goto out_elf_end; | |
265 | ||
266 | sec = elf_getscn(elf, shdr.sh_link); | |
267 | if (sec == NULL) | |
268 | goto out_elf_end; | |
269 | ||
16f762a2 | 270 | symstrs = elf_getdata(sec, NULL); |
62eb9390 ACM |
271 | if (symstrs == NULL) |
272 | goto out_elf_end; | |
273 | ||
16f762a2 | 274 | nr_syms = shdr.sh_size / shdr.sh_entsize; |
62eb9390 | 275 | |
62eb9390 | 276 | elf_symtab__for_each_symbol(syms, nr_syms, index, sym) { |
f17e04af PZ |
277 | struct symbol *f; |
278 | ||
62eb9390 ACM |
279 | if (!elf_sym__is_function(&sym)) |
280 | continue; | |
f17e04af PZ |
281 | |
282 | sec = elf_getscn(elf, sym.st_shndx); | |
283 | if (!sec) | |
284 | goto out_elf_end; | |
285 | ||
286 | gelf_getshdr(sec, &shdr); | |
287 | sym.st_value -= shdr.sh_addr - shdr.sh_offset; | |
288 | ||
289 | f = symbol__new(sym.st_value, sym.st_size, | |
290 | elf_sym__name(&sym, symstrs)); | |
291 | if (!f) | |
62eb9390 ACM |
292 | goto out_elf_end; |
293 | ||
294 | dso__insert_symbol(self, f); | |
295 | } | |
296 | ||
297 | err = 0; | |
298 | out_elf_end: | |
299 | elf_end(elf); | |
300 | out_close: | |
301 | close(fd); | |
302 | return err; | |
8fa66bdc ACM |
303 | } |
304 | ||
305 | static size_t dso__fprintf(struct dso *self, FILE *fp) | |
306 | { | |
8fa66bdc ACM |
307 | size_t ret = fprintf(fp, "dso: %s\n", self->name); |
308 | ||
35a50c8a ACM |
309 | struct rb_node *nd; |
310 | for (nd = rb_first(&self->syms); nd; nd = rb_next(nd)) { | |
311 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); | |
8fa66bdc | 312 | ret += symbol__fprintf(pos, fp); |
35a50c8a | 313 | } |
8fa66bdc ACM |
314 | |
315 | return ret; | |
316 | } | |
317 | ||
318 | static LIST_HEAD(dsos); | |
319 | static struct dso *kernel_dso; | |
320 | ||
321 | static void dsos__add(struct dso *dso) | |
322 | { | |
323 | list_add_tail(&dso->node, &dsos); | |
324 | } | |
325 | ||
326 | static struct dso *dsos__find(const char *name) | |
327 | { | |
328 | struct dso *pos; | |
329 | ||
330 | list_for_each_entry(pos, &dsos, node) | |
331 | if (strcmp(pos->name, name) == 0) | |
332 | return pos; | |
333 | return NULL; | |
334 | } | |
335 | ||
336 | static struct dso *dsos__findnew(const char *name) | |
337 | { | |
338 | struct dso *dso = dsos__find(name); | |
339 | ||
340 | if (dso == NULL) { | |
341 | dso = dso__new(name); | |
342 | if (dso != NULL && dso__load(dso) < 0) | |
343 | goto out_delete_dso; | |
344 | ||
345 | dsos__add(dso); | |
346 | } | |
347 | ||
348 | return dso; | |
349 | ||
350 | out_delete_dso: | |
351 | dso__delete(dso); | |
352 | return NULL; | |
353 | } | |
354 | ||
16f762a2 | 355 | static void dsos__fprintf(FILE *fp) |
8fa66bdc ACM |
356 | { |
357 | struct dso *pos; | |
358 | ||
359 | list_for_each_entry(pos, &dsos, node) | |
360 | dso__fprintf(pos, fp); | |
361 | } | |
362 | ||
d8d1656e ACM |
363 | static int hex(char ch) |
364 | { | |
365 | if ((ch >= '0') && (ch <= '9')) | |
366 | return ch - '0'; | |
367 | if ((ch >= 'a') && (ch <= 'f')) | |
368 | return ch - 'a' + 10; | |
369 | if ((ch >= 'A') && (ch <= 'F')) | |
370 | return ch - 'A' + 10; | |
371 | return -1; | |
372 | } | |
373 | ||
374 | /* | |
375 | * While we find nice hex chars, build a long_val. | |
376 | * Return number of chars processed. | |
377 | */ | |
16f762a2 | 378 | static int hex2long(char *ptr, unsigned long *long_val) |
d8d1656e ACM |
379 | { |
380 | const char *p = ptr; | |
381 | *long_val = 0; | |
382 | ||
383 | while (*p) { | |
384 | const int hex_val = hex(*p); | |
385 | ||
386 | if (hex_val < 0) | |
387 | break; | |
388 | ||
389 | *long_val = (*long_val << 4) | hex_val; | |
390 | p++; | |
391 | } | |
392 | ||
393 | return p - ptr; | |
394 | } | |
395 | ||
8fa66bdc ACM |
396 | static int load_kallsyms(void) |
397 | { | |
af83632f IM |
398 | struct rb_node *nd, *prevnd; |
399 | char *line = NULL; | |
400 | FILE *file; | |
401 | size_t n; | |
402 | ||
8fa66bdc ACM |
403 | kernel_dso = dso__new("[kernel]"); |
404 | if (kernel_dso == NULL) | |
405 | return -1; | |
406 | ||
af83632f | 407 | file = fopen("/proc/kallsyms", "r"); |
8fa66bdc ACM |
408 | if (file == NULL) |
409 | goto out_delete_dso; | |
410 | ||
8fa66bdc | 411 | while (!feof(file)) { |
d8d1656e | 412 | unsigned long start; |
af83632f IM |
413 | struct symbol *sym; |
414 | int line_len, len; | |
415 | char symbol_type; | |
416 | ||
417 | line_len = getline(&line, &n, file); | |
d8d1656e | 418 | if (line_len < 0) |
8fa66bdc ACM |
419 | break; |
420 | ||
421 | if (!line) | |
422 | goto out_delete_dso; | |
423 | ||
d8d1656e | 424 | line[--line_len] = '\0'; /* \n */ |
abd54f68 | 425 | |
af83632f IM |
426 | len = hex2long(line, &start); |
427 | ||
03f6316d ACM |
428 | len++; |
429 | if (len + 2 >= line_len) | |
430 | continue; | |
431 | ||
af83632f | 432 | symbol_type = toupper(line[len]); |
03f6316d ACM |
433 | /* |
434 | * We're interested only in code ('T'ext) | |
435 | */ | |
af83632f | 436 | if (symbol_type != 'T' && symbol_type != 'W') |
d8d1656e ACM |
437 | continue; |
438 | /* | |
439 | * Well fix up the end later, when we have all sorted. | |
440 | */ | |
af83632f | 441 | sym = symbol__new(start, 0xdead, line + len + 2); |
abd54f68 | 442 | |
d8d1656e ACM |
443 | if (sym == NULL) |
444 | goto out_delete_dso; | |
445 | ||
446 | dso__insert_symbol(kernel_dso, sym); | |
8fa66bdc ACM |
447 | } |
448 | ||
abd54f68 ACM |
449 | /* |
450 | * Now that we have all sorted out, just set the ->end of all | |
451 | * symbols | |
452 | */ | |
af83632f | 453 | prevnd = rb_first(&kernel_dso->syms); |
abd54f68 ACM |
454 | |
455 | if (prevnd == NULL) | |
456 | goto out_delete_line; | |
457 | ||
458 | for (nd = rb_next(prevnd); nd; nd = rb_next(nd)) { | |
459 | struct symbol *prev = rb_entry(prevnd, struct symbol, rb_node), | |
460 | *curr = rb_entry(nd, struct symbol, rb_node); | |
461 | ||
462 | prev->end = curr->start - 1; | |
463 | prevnd = nd; | |
464 | } | |
465 | ||
8fa66bdc ACM |
466 | dsos__add(kernel_dso); |
467 | free(line); | |
468 | fclose(file); | |
af83632f | 469 | |
8fa66bdc ACM |
470 | return 0; |
471 | ||
59d81029 ACM |
472 | out_delete_line: |
473 | free(line); | |
8fa66bdc ACM |
474 | out_delete_dso: |
475 | dso__delete(kernel_dso); | |
476 | return -1; | |
477 | } | |
478 | ||
479 | struct map { | |
480 | struct list_head node; | |
481 | uint64_t start; | |
482 | uint64_t end; | |
483 | uint64_t pgoff; | |
484 | struct dso *dso; | |
485 | }; | |
486 | ||
487 | static struct map *map__new(struct mmap_event *event) | |
488 | { | |
489 | struct map *self = malloc(sizeof(*self)); | |
490 | ||
491 | if (self != NULL) { | |
492 | self->start = event->start; | |
493 | self->end = event->start + event->len; | |
494 | self->pgoff = event->pgoff; | |
495 | ||
496 | self->dso = dsos__findnew(event->filename); | |
497 | if (self->dso == NULL) | |
498 | goto out_delete; | |
499 | } | |
500 | return self; | |
501 | out_delete: | |
502 | free(self); | |
503 | return NULL; | |
504 | } | |
505 | ||
3a4b8cc7 ACM |
506 | struct thread; |
507 | ||
508 | static const char *thread__name(struct thread *self, char *bf, size_t size); | |
509 | ||
8fa66bdc | 510 | struct symhist { |
ce7e4365 | 511 | struct rb_node rb_node; |
8fa66bdc ACM |
512 | struct dso *dso; |
513 | struct symbol *sym; | |
3a4b8cc7 | 514 | struct thread *thread; |
ce7e4365 | 515 | uint64_t ip; |
8fa66bdc ACM |
516 | uint32_t count; |
517 | char level; | |
518 | }; | |
519 | ||
ce7e4365 | 520 | static struct symhist *symhist__new(struct symbol *sym, uint64_t ip, |
3a4b8cc7 ACM |
521 | struct thread *thread, struct dso *dso, |
522 | char level) | |
8fa66bdc ACM |
523 | { |
524 | struct symhist *self = malloc(sizeof(*self)); | |
525 | ||
526 | if (self != NULL) { | |
3a4b8cc7 ACM |
527 | self->sym = sym; |
528 | self->thread = thread; | |
529 | self->ip = ip; | |
530 | self->dso = dso; | |
531 | self->level = level; | |
532 | self->count = 1; | |
8fa66bdc ACM |
533 | } |
534 | ||
535 | return self; | |
536 | } | |
537 | ||
8fa66bdc ACM |
538 | static void symhist__inc(struct symhist *self) |
539 | { | |
540 | ++self->count; | |
541 | } | |
542 | ||
3a4b8cc7 ACM |
543 | static size_t |
544 | symhist__fprintf(struct symhist *self, uint64_t total_samples, FILE *fp) | |
8fa66bdc | 545 | { |
3a4b8cc7 ACM |
546 | char bf[32]; |
547 | size_t ret; | |
548 | ||
549 | if (total_samples) | |
550 | ret = fprintf(fp, "%5.2f", (self->count * 100.0) / total_samples); | |
551 | else | |
552 | ret = fprintf(fp, "%12d", self->count); | |
553 | ||
815e777f | 554 | ret += fprintf(fp, "%14s [%c] ", |
3a4b8cc7 | 555 | thread__name(self->thread, bf, sizeof(bf)), |
815e777f ACM |
556 | self->level); |
557 | ||
558 | if (verbose) | |
559 | ret += fprintf(fp, "%#018llx ", (unsigned long long)self->ip); | |
8fa66bdc ACM |
560 | |
561 | if (self->level != '.') | |
3a4b8cc7 ACM |
562 | ret += fprintf(fp, "%s\n", |
563 | self->sym ? self->sym->name : "<unknown>"); | |
8fa66bdc | 564 | else |
3a4b8cc7 | 565 | ret += fprintf(fp, "%s: %s\n", |
f17e04af | 566 | self->dso ? self->dso->name : "<unknown>", |
8fa66bdc | 567 | self->sym ? self->sym->name : "<unknown>"); |
3a4b8cc7 | 568 | return ret; |
8fa66bdc ACM |
569 | } |
570 | ||
571 | struct thread { | |
ce7e4365 | 572 | struct rb_node rb_node; |
8fa66bdc | 573 | struct list_head maps; |
ce7e4365 | 574 | struct rb_root symhists; |
8fa66bdc ACM |
575 | pid_t pid; |
576 | char *comm; | |
577 | }; | |
578 | ||
3a4b8cc7 ACM |
579 | static const char *thread__name(struct thread *self, char *bf, size_t size) |
580 | { | |
581 | if (self->comm) | |
582 | return self->comm; | |
583 | ||
584 | snprintf(bf, sizeof(bf), ":%u", self->pid); | |
585 | return bf; | |
586 | } | |
587 | ||
8fa66bdc ACM |
588 | static struct thread *thread__new(pid_t pid) |
589 | { | |
590 | struct thread *self = malloc(sizeof(*self)); | |
591 | ||
592 | if (self != NULL) { | |
593 | self->pid = pid; | |
594 | self->comm = NULL; | |
595 | INIT_LIST_HEAD(&self->maps); | |
ce7e4365 | 596 | self->symhists = RB_ROOT; |
8fa66bdc ACM |
597 | } |
598 | ||
599 | return self; | |
600 | } | |
601 | ||
ce7e4365 ACM |
602 | static int thread__symbol_incnew(struct thread *self, struct symbol *sym, |
603 | uint64_t ip, struct dso *dso, char level) | |
8fa66bdc | 604 | { |
ce7e4365 ACM |
605 | struct rb_node **p = &self->symhists.rb_node; |
606 | struct rb_node *parent = NULL; | |
607 | struct symhist *sh; | |
8fa66bdc | 608 | |
ce7e4365 | 609 | while (*p != NULL) { |
16f762a2 IM |
610 | uint64_t start; |
611 | ||
ce7e4365 ACM |
612 | parent = *p; |
613 | sh = rb_entry(parent, struct symhist, rb_node); | |
8fa66bdc | 614 | |
ce7e4365 ACM |
615 | if (sh->sym == sym || ip == sh->ip) { |
616 | symhist__inc(sh); | |
617 | return 0; | |
618 | } | |
8fa66bdc | 619 | |
ce7e4365 | 620 | /* Handle unresolved symbols too */ |
16f762a2 | 621 | start = !sh->sym ? sh->ip : sh->sym->start; |
8fa66bdc | 622 | |
ce7e4365 ACM |
623 | if (ip < start) |
624 | p = &(*p)->rb_left; | |
625 | else | |
626 | p = &(*p)->rb_right; | |
8fa66bdc ACM |
627 | } |
628 | ||
3a4b8cc7 | 629 | sh = symhist__new(sym, ip, self, dso, level); |
ce7e4365 ACM |
630 | if (sh == NULL) |
631 | return -ENOMEM; | |
632 | rb_link_node(&sh->rb_node, parent, p); | |
633 | rb_insert_color(&sh->rb_node, &self->symhists); | |
8fa66bdc | 634 | return 0; |
8fa66bdc ACM |
635 | } |
636 | ||
637 | static int thread__set_comm(struct thread *self, const char *comm) | |
638 | { | |
639 | self->comm = strdup(comm); | |
640 | return self->comm ? 0 : -ENOMEM; | |
641 | } | |
642 | ||
8fa66bdc ACM |
643 | static size_t thread__fprintf(struct thread *self, FILE *fp) |
644 | { | |
8fa66bdc | 645 | int ret = fprintf(fp, "thread: %d %s\n", self->pid, self->comm); |
ce7e4365 | 646 | struct rb_node *nd; |
8fa66bdc | 647 | |
ce7e4365 ACM |
648 | for (nd = rb_first(&self->symhists); nd; nd = rb_next(nd)) { |
649 | struct symhist *pos = rb_entry(nd, struct symhist, rb_node); | |
16f762a2 | 650 | |
3a4b8cc7 | 651 | ret += symhist__fprintf(pos, 0, fp); |
ce7e4365 | 652 | } |
8fa66bdc ACM |
653 | |
654 | return ret; | |
655 | } | |
656 | ||
16f762a2 | 657 | static struct rb_root threads; |
8fa66bdc | 658 | |
ce7e4365 | 659 | static struct thread *threads__findnew(pid_t pid) |
8fa66bdc | 660 | { |
ce7e4365 ACM |
661 | struct rb_node **p = &threads.rb_node; |
662 | struct rb_node *parent = NULL; | |
663 | struct thread *th; | |
8fa66bdc | 664 | |
ce7e4365 ACM |
665 | while (*p != NULL) { |
666 | parent = *p; | |
667 | th = rb_entry(parent, struct thread, rb_node); | |
8fa66bdc | 668 | |
ce7e4365 ACM |
669 | if (th->pid == pid) |
670 | return th; | |
8fa66bdc | 671 | |
ce7e4365 ACM |
672 | if (pid < th->pid) |
673 | p = &(*p)->rb_left; | |
674 | else | |
675 | p = &(*p)->rb_right; | |
8fa66bdc ACM |
676 | } |
677 | ||
ce7e4365 ACM |
678 | th = thread__new(pid); |
679 | if (th != NULL) { | |
680 | rb_link_node(&th->rb_node, parent, p); | |
681 | rb_insert_color(&th->rb_node, &threads); | |
682 | } | |
683 | return th; | |
8fa66bdc ACM |
684 | } |
685 | ||
686 | static void thread__insert_map(struct thread *self, struct map *map) | |
687 | { | |
688 | list_add_tail(&map->node, &self->maps); | |
689 | } | |
690 | ||
691 | static struct map *thread__find_map(struct thread *self, uint64_t ip) | |
692 | { | |
16f762a2 IM |
693 | struct map *pos; |
694 | ||
8fa66bdc ACM |
695 | if (self == NULL) |
696 | return NULL; | |
697 | ||
8fa66bdc ACM |
698 | list_for_each_entry(pos, &self->maps, node) |
699 | if (ip >= pos->start && ip <= pos->end) | |
700 | return pos; | |
701 | ||
702 | return NULL; | |
703 | } | |
704 | ||
16f762a2 | 705 | static void threads__fprintf(FILE *fp) |
8fa66bdc | 706 | { |
ce7e4365 ACM |
707 | struct rb_node *nd; |
708 | for (nd = rb_first(&threads); nd; nd = rb_next(nd)) { | |
709 | struct thread *pos = rb_entry(nd, struct thread, rb_node); | |
8fa66bdc | 710 | thread__fprintf(pos, fp); |
ce7e4365 | 711 | } |
8fa66bdc ACM |
712 | } |
713 | ||
16f762a2 | 714 | static struct rb_root global_symhists; |
3a4b8cc7 ACM |
715 | |
716 | static void threads__insert_symhist(struct symhist *sh) | |
717 | { | |
718 | struct rb_node **p = &global_symhists.rb_node; | |
719 | struct rb_node *parent = NULL; | |
720 | struct symhist *iter; | |
721 | ||
722 | while (*p != NULL) { | |
723 | parent = *p; | |
724 | iter = rb_entry(parent, struct symhist, rb_node); | |
725 | ||
726 | /* Reverse order */ | |
727 | if (sh->count > iter->count) | |
728 | p = &(*p)->rb_left; | |
729 | else | |
730 | p = &(*p)->rb_right; | |
731 | } | |
732 | ||
733 | rb_link_node(&sh->rb_node, parent, p); | |
734 | rb_insert_color(&sh->rb_node, &global_symhists); | |
735 | } | |
736 | ||
737 | static void threads__sort_symhists(void) | |
738 | { | |
739 | struct rb_node *nd; | |
740 | ||
741 | for (nd = rb_first(&threads); nd; nd = rb_next(nd)) { | |
742 | struct thread *thread = rb_entry(nd, struct thread, rb_node); | |
743 | struct rb_node *next = rb_first(&thread->symhists); | |
744 | ||
745 | while (next) { | |
746 | struct symhist *n = rb_entry(next, struct symhist, | |
747 | rb_node); | |
748 | next = rb_next(&n->rb_node); | |
749 | rb_erase(&n->rb_node, &thread->symhists); | |
750 | threads__insert_symhist(n); | |
751 | } | |
752 | ||
753 | } | |
754 | } | |
755 | ||
756 | static size_t threads__symhists_fprintf(uint64_t total_samples, FILE *fp) | |
757 | { | |
758 | struct rb_node *nd; | |
759 | size_t ret = 0; | |
760 | ||
761 | for (nd = rb_first(&global_symhists); nd; nd = rb_next(nd)) { | |
762 | struct symhist *pos = rb_entry(nd, struct symhist, rb_node); | |
763 | ret += symhist__fprintf(pos, total_samples, fp); | |
764 | } | |
765 | ||
766 | return ret; | |
767 | } | |
768 | ||
53cb8bc2 | 769 | static int __cmd_report(void) |
8fa66bdc ACM |
770 | { |
771 | unsigned long offset = 0; | |
772 | unsigned long head = 0; | |
773 | struct stat stat; | |
774 | char *buf; | |
775 | event_t *event; | |
776 | int ret, rc = EXIT_FAILURE; | |
6142f9ec | 777 | uint32_t size; |
f49515b1 | 778 | unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0; |
8fa66bdc | 779 | |
8fa66bdc ACM |
780 | input = open(input_name, O_RDONLY); |
781 | if (input < 0) { | |
782 | perror("failed to open file"); | |
783 | exit(-1); | |
784 | } | |
785 | ||
786 | ret = fstat(input, &stat); | |
787 | if (ret < 0) { | |
788 | perror("failed to stat file"); | |
789 | exit(-1); | |
790 | } | |
791 | ||
792 | if (!stat.st_size) { | |
793 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | |
794 | exit(0); | |
795 | } | |
796 | ||
797 | if (load_kallsyms() < 0) { | |
798 | perror("failed to open kallsyms"); | |
799 | return EXIT_FAILURE; | |
800 | } | |
801 | ||
802 | remap: | |
803 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, | |
804 | MAP_SHARED, input, offset); | |
805 | if (buf == MAP_FAILED) { | |
806 | perror("failed to mmap file"); | |
807 | exit(-1); | |
808 | } | |
809 | ||
810 | more: | |
811 | event = (event_t *)(buf + head); | |
812 | ||
6142f9ec PZ |
813 | size = event->header.size; |
814 | if (!size) | |
815 | size = 8; | |
816 | ||
8fa66bdc ACM |
817 | if (head + event->header.size >= page_size * mmap_window) { |
818 | unsigned long shift = page_size * (head / page_size); | |
819 | int ret; | |
820 | ||
821 | ret = munmap(buf, page_size * mmap_window); | |
822 | assert(ret == 0); | |
823 | ||
824 | offset += shift; | |
825 | head -= shift; | |
826 | goto remap; | |
827 | } | |
828 | ||
6142f9ec PZ |
829 | size = event->header.size; |
830 | if (!size) | |
831 | goto broken_event; | |
8fa66bdc | 832 | |
8fa66bdc ACM |
833 | if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) { |
834 | char level; | |
835 | int show = 0; | |
836 | struct dso *dso = NULL; | |
837 | struct thread *thread = threads__findnew(event->ip.pid); | |
f17e04af | 838 | uint64_t ip = event->ip.ip; |
8fa66bdc | 839 | |
97b07b69 | 840 | if (dump_trace) { |
f49515b1 IM |
841 | fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n", |
842 | (void *)(offset + head), | |
843 | (void *)(long)(event->header.size), | |
97b07b69 IM |
844 | event->header.misc, |
845 | event->ip.pid, | |
16f762a2 | 846 | (void *)(long)ip); |
97b07b69 IM |
847 | } |
848 | ||
ce7e4365 ACM |
849 | if (thread == NULL) { |
850 | fprintf(stderr, "problem processing %d event, bailing out\n", | |
851 | event->header.type); | |
8fa66bdc | 852 | goto done; |
ce7e4365 | 853 | } |
8fa66bdc ACM |
854 | |
855 | if (event->header.misc & PERF_EVENT_MISC_KERNEL) { | |
856 | show = SHOW_KERNEL; | |
857 | level = 'k'; | |
858 | dso = kernel_dso; | |
859 | } else if (event->header.misc & PERF_EVENT_MISC_USER) { | |
16f762a2 IM |
860 | struct map *map; |
861 | ||
8fa66bdc ACM |
862 | show = SHOW_USER; |
863 | level = '.'; | |
16f762a2 IM |
864 | |
865 | map = thread__find_map(thread, ip); | |
f17e04af | 866 | if (map != NULL) { |
8fa66bdc | 867 | dso = map->dso; |
f17e04af PZ |
868 | ip -= map->start + map->pgoff; |
869 | } | |
8fa66bdc ACM |
870 | } else { |
871 | show = SHOW_HV; | |
872 | level = 'H'; | |
873 | } | |
874 | ||
875 | if (show & show_mask) { | |
f17e04af | 876 | struct symbol *sym = dso__find_symbol(dso, ip); |
8fa66bdc | 877 | |
f17e04af | 878 | if (thread__symbol_incnew(thread, sym, ip, dso, level)) { |
ce7e4365 | 879 | fprintf(stderr, "problem incrementing symbol count, bailing out\n"); |
8fa66bdc | 880 | goto done; |
ce7e4365 | 881 | } |
8fa66bdc ACM |
882 | } |
883 | total++; | |
884 | } else switch (event->header.type) { | |
885 | case PERF_EVENT_MMAP: { | |
886 | struct thread *thread = threads__findnew(event->mmap.pid); | |
887 | struct map *map = map__new(&event->mmap); | |
888 | ||
97b07b69 | 889 | if (dump_trace) { |
f49515b1 IM |
890 | fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n", |
891 | (void *)(offset + head), | |
892 | (void *)(long)(event->header.size), | |
16f762a2 IM |
893 | (void *)(long)event->mmap.start, |
894 | (void *)(long)event->mmap.len, | |
895 | (void *)(long)event->mmap.pgoff, | |
97b07b69 IM |
896 | event->mmap.filename); |
897 | } | |
ce7e4365 ACM |
898 | if (thread == NULL || map == NULL) { |
899 | fprintf(stderr, "problem processing PERF_EVENT_MMAP, bailing out\n"); | |
8fa66bdc | 900 | goto done; |
ce7e4365 | 901 | } |
8fa66bdc | 902 | thread__insert_map(thread, map); |
97b07b69 | 903 | total_mmap++; |
8fa66bdc ACM |
904 | break; |
905 | } | |
906 | case PERF_EVENT_COMM: { | |
907 | struct thread *thread = threads__findnew(event->comm.pid); | |
908 | ||
97b07b69 | 909 | if (dump_trace) { |
f49515b1 IM |
910 | fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n", |
911 | (void *)(offset + head), | |
912 | (void *)(long)(event->header.size), | |
97b07b69 IM |
913 | event->comm.comm, event->comm.pid); |
914 | } | |
8fa66bdc | 915 | if (thread == NULL || |
ce7e4365 ACM |
916 | thread__set_comm(thread, event->comm.comm)) { |
917 | fprintf(stderr, "problem processing PERF_EVENT_COMM, bailing out\n"); | |
8fa66bdc | 918 | goto done; |
ce7e4365 | 919 | } |
97b07b69 | 920 | total_comm++; |
8fa66bdc ACM |
921 | break; |
922 | } | |
97b07b69 | 923 | default: { |
6142f9ec | 924 | broken_event: |
f49515b1 IM |
925 | fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n", |
926 | (void *)(offset + head), | |
927 | (void *)(long)(event->header.size), | |
97b07b69 | 928 | event->header.type); |
3e706114 | 929 | total_unknown++; |
6142f9ec PZ |
930 | |
931 | /* | |
932 | * assume we lost track of the stream, check alignment, and | |
933 | * increment a single u64 in the hope to catch on again 'soon'. | |
934 | */ | |
935 | ||
936 | if (unlikely(head & 7)) | |
937 | head &= ~7ULL; | |
938 | ||
939 | size = 8; | |
97b07b69 | 940 | } |
8fa66bdc ACM |
941 | } |
942 | ||
6142f9ec | 943 | head += size; |
f49515b1 | 944 | |
8fa66bdc ACM |
945 | if (offset + head < stat.st_size) |
946 | goto more; | |
947 | ||
948 | rc = EXIT_SUCCESS; | |
949 | done: | |
950 | close(input); | |
97b07b69 IM |
951 | |
952 | if (dump_trace) { | |
3e706114 IM |
953 | fprintf(stderr, " IP events: %10ld\n", total); |
954 | fprintf(stderr, " mmap events: %10ld\n", total_mmap); | |
955 | fprintf(stderr, " comm events: %10ld\n", total_comm); | |
956 | fprintf(stderr, " unknown events: %10ld\n", total_unknown); | |
97b07b69 IM |
957 | |
958 | return 0; | |
959 | } | |
960 | ||
16f762a2 IM |
961 | if (verbose >= 2) { |
962 | dsos__fprintf(stdout); | |
963 | threads__fprintf(stdout); | |
964 | } | |
965 | ||
3a4b8cc7 ACM |
966 | threads__sort_symhists(); |
967 | threads__symhists_fprintf(total, stdout); | |
8fa66bdc | 968 | |
8fa66bdc ACM |
969 | return rc; |
970 | } | |
971 | ||
53cb8bc2 IM |
972 | static const char * const report_usage[] = { |
973 | "perf report [<options>] <command>", | |
974 | NULL | |
975 | }; | |
976 | ||
977 | static const struct option options[] = { | |
978 | OPT_STRING('i', "input", &input_name, "file", | |
979 | "input file name"), | |
815e777f ACM |
980 | OPT_BOOLEAN('v', "verbose", &verbose, |
981 | "be more verbose (show symbol address, etc)"), | |
97b07b69 IM |
982 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
983 | "dump raw trace in ASCII"), | |
53cb8bc2 IM |
984 | OPT_END() |
985 | }; | |
986 | ||
987 | int cmd_report(int argc, const char **argv, const char *prefix) | |
988 | { | |
989 | elf_version(EV_CURRENT); | |
990 | ||
991 | page_size = getpagesize(); | |
992 | ||
993 | parse_options(argc, argv, options, report_usage, 0); | |
994 | ||
a930d2c0 IM |
995 | setup_pager(); |
996 | ||
53cb8bc2 IM |
997 | return __cmd_report(); |
998 | } |