blob: 820e7ccec62d72ba270044275fa1745915fa9dad [file] [log] [blame]
Ingo Molnar8035e422009-06-06 15:19:13 +02001/*
2 * builtin-annotate.c
3 *
4 * Builtin annotate command: Analyze the perf.data input file,
5 * look up and read DSOs and symbol information and display
6 * a histogram of results, along various sorting keys.
7 */
8#include "builtin.h"
9
10#include "util/util.h"
11
12#include "util/color.h"
Arnaldo Carvalho de Melo5da50252009-07-01 14:46:08 -030013#include <linux/list.h>
Ingo Molnar8035e422009-06-06 15:19:13 +020014#include "util/cache.h"
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030015#include <linux/rbtree.h>
Ingo Molnar8035e422009-06-06 15:19:13 +020016#include "util/symbol.h"
17#include "util/string.h"
18
19#include "perf.h"
20
21#include "util/parse-options.h"
22#include "util/parse-events.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020023#include "util/thread.h"
Ingo Molnar8035e422009-06-06 15:19:13 +020024
25#define SHOW_KERNEL 1
26#define SHOW_USER 2
27#define SHOW_HV 4
28
29static char const *input_name = "perf.data";
Ingo Molnar8035e422009-06-06 15:19:13 +020030
Ingo Molnar0b73da32009-06-06 15:48:52 +020031static char default_sort_order[] = "comm,symbol";
Ingo Molnar8035e422009-06-06 15:19:13 +020032static char *sort_order = default_sort_order;
33
34static int input;
35static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
36
Mike Galbraith42976482009-07-02 08:09:46 +020037static int full_paths;
38
Frederic Weisbecker301406b2009-06-13 00:11:21 +020039static int print_line;
40
Ingo Molnar8035e422009-06-06 15:19:13 +020041static unsigned long page_size;
42static unsigned long mmap_window = 32;
43
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020044static struct rb_root threads;
45static struct thread *last_match;
46
Frederic Weisbecker301406b2009-06-13 00:11:21 +020047
48struct sym_ext {
Frederic Weisbecker971738f2009-06-13 00:11:22 +020049 struct rb_node node;
Frederic Weisbecker301406b2009-06-13 00:11:21 +020050 double percent;
51 char *path;
52};
53
Ingo Molnar8035e422009-06-06 15:19:13 +020054/*
55 * histogram, sorted on item, collects counts
56 */
57
58static struct rb_root hist;
59
60struct hist_entry {
61 struct rb_node rb_node;
62
63 struct thread *thread;
64 struct map *map;
65 struct dso *dso;
66 struct symbol *sym;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +100067 u64 ip;
Ingo Molnar8035e422009-06-06 15:19:13 +020068 char level;
69
70 uint32_t count;
71};
72
73/*
74 * configurable sorting bits
75 */
76
77struct sort_entry {
78 struct list_head list;
79
Ingo Molnar83a09442009-08-15 12:26:57 +020080 const char *header;
Ingo Molnar8035e422009-06-06 15:19:13 +020081
82 int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
83 int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
84 size_t (*print)(FILE *fp, struct hist_entry *);
85};
86
87/* --sort pid */
88
89static int64_t
90sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
91{
92 return right->thread->pid - left->thread->pid;
93}
94
95static size_t
96sort__thread_print(FILE *fp, struct hist_entry *self)
97{
98 return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
99}
100
101static struct sort_entry sort_thread = {
102 .header = " Command: Pid",
103 .cmp = sort__thread_cmp,
104 .print = sort__thread_print,
105};
106
107/* --sort comm */
108
109static int64_t
110sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
111{
112 return right->thread->pid - left->thread->pid;
113}
114
115static int64_t
116sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
117{
118 char *comm_l = left->thread->comm;
119 char *comm_r = right->thread->comm;
120
121 if (!comm_l || !comm_r) {
122 if (!comm_l && !comm_r)
123 return 0;
124 else if (!comm_l)
125 return -1;
126 else
127 return 1;
128 }
129
130 return strcmp(comm_l, comm_r);
131}
132
133static size_t
134sort__comm_print(FILE *fp, struct hist_entry *self)
135{
136 return fprintf(fp, "%16s", self->thread->comm);
137}
138
139static struct sort_entry sort_comm = {
140 .header = " Command",
141 .cmp = sort__comm_cmp,
142 .collapse = sort__comm_collapse,
143 .print = sort__comm_print,
144};
145
146/* --sort dso */
147
148static int64_t
149sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
150{
151 struct dso *dso_l = left->dso;
152 struct dso *dso_r = right->dso;
153
154 if (!dso_l || !dso_r) {
155 if (!dso_l && !dso_r)
156 return 0;
157 else if (!dso_l)
158 return -1;
159 else
160 return 1;
161 }
162
163 return strcmp(dso_l->name, dso_r->name);
164}
165
166static size_t
167sort__dso_print(FILE *fp, struct hist_entry *self)
168{
169 if (self->dso)
170 return fprintf(fp, "%-25s", self->dso->name);
171
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000172 return fprintf(fp, "%016llx ", (u64)self->ip);
Ingo Molnar8035e422009-06-06 15:19:13 +0200173}
174
175static struct sort_entry sort_dso = {
176 .header = "Shared Object ",
177 .cmp = sort__dso_cmp,
178 .print = sort__dso_print,
179};
180
181/* --sort symbol */
182
183static int64_t
184sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
185{
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000186 u64 ip_l, ip_r;
Ingo Molnar8035e422009-06-06 15:19:13 +0200187
188 if (left->sym == right->sym)
189 return 0;
190
191 ip_l = left->sym ? left->sym->start : left->ip;
192 ip_r = right->sym ? right->sym->start : right->ip;
193
194 return (int64_t)(ip_r - ip_l);
195}
196
197static size_t
198sort__sym_print(FILE *fp, struct hist_entry *self)
199{
200 size_t ret = 0;
201
202 if (verbose)
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000203 ret += fprintf(fp, "%#018llx ", (u64)self->ip);
Ingo Molnar8035e422009-06-06 15:19:13 +0200204
205 if (self->sym) {
206 ret += fprintf(fp, "[%c] %s",
207 self->dso == kernel_dso ? 'k' : '.', self->sym->name);
208 } else {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000209 ret += fprintf(fp, "%#016llx", (u64)self->ip);
Ingo Molnar8035e422009-06-06 15:19:13 +0200210 }
211
212 return ret;
213}
214
215static struct sort_entry sort_sym = {
216 .header = "Symbol",
217 .cmp = sort__sym_cmp,
218 .print = sort__sym_print,
219};
220
221static int sort__need_collapse = 0;
222
223struct sort_dimension {
Ingo Molnar83a09442009-08-15 12:26:57 +0200224 const char *name;
Ingo Molnar8035e422009-06-06 15:19:13 +0200225 struct sort_entry *entry;
226 int taken;
227};
228
229static struct sort_dimension sort_dimensions[] = {
230 { .name = "pid", .entry = &sort_thread, },
231 { .name = "comm", .entry = &sort_comm, },
232 { .name = "dso", .entry = &sort_dso, },
233 { .name = "symbol", .entry = &sort_sym, },
234};
235
236static LIST_HEAD(hist_entry__sort_list);
237
238static int sort_dimension__add(char *tok)
239{
Ingo Molnarf37a2912009-07-01 12:37:06 +0200240 unsigned int i;
Ingo Molnar8035e422009-06-06 15:19:13 +0200241
242 for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
243 struct sort_dimension *sd = &sort_dimensions[i];
244
245 if (sd->taken)
246 continue;
247
248 if (strncasecmp(tok, sd->name, strlen(tok)))
249 continue;
250
251 if (sd->entry->collapse)
252 sort__need_collapse = 1;
253
254 list_add_tail(&sd->entry->list, &hist_entry__sort_list);
255 sd->taken = 1;
256
257 return 0;
258 }
259
260 return -ESRCH;
261}
262
263static int64_t
264hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
265{
266 struct sort_entry *se;
267 int64_t cmp = 0;
268
269 list_for_each_entry(se, &hist_entry__sort_list, list) {
270 cmp = se->cmp(left, right);
271 if (cmp)
272 break;
273 }
274
275 return cmp;
276}
277
278static int64_t
279hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
280{
281 struct sort_entry *se;
282 int64_t cmp = 0;
283
284 list_for_each_entry(se, &hist_entry__sort_list, list) {
285 int64_t (*f)(struct hist_entry *, struct hist_entry *);
286
287 f = se->collapse ?: se->cmp;
288
289 cmp = f(left, right);
290 if (cmp)
291 break;
292 }
293
294 return cmp;
295}
296
Ingo Molnar8035e422009-06-06 15:19:13 +0200297/*
298 * collect histogram counts
299 */
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000300static void hist_hit(struct hist_entry *he, u64 ip)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200301{
302 unsigned int sym_size, offset;
303 struct symbol *sym = he->sym;
304
305 he->count++;
306
307 if (!sym || !sym->hist)
308 return;
309
310 sym_size = sym->end - sym->start;
311 offset = ip - sym->start;
312
313 if (offset >= sym_size)
314 return;
315
316 sym->hist_sum++;
317 sym->hist[offset]++;
318
319 if (verbose >= 3)
320 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +0200321 (void *)(unsigned long)he->sym->start,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200322 he->sym->name,
Arjan van de Ven7d37a0c2009-06-06 20:36:38 +0200323 (void *)(unsigned long)ip, ip - he->sym->start,
Ingo Molnar0b73da32009-06-06 15:48:52 +0200324 sym->hist[offset]);
325}
Ingo Molnar8035e422009-06-06 15:19:13 +0200326
327static int
328hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000329 struct symbol *sym, u64 ip, char level)
Ingo Molnar8035e422009-06-06 15:19:13 +0200330{
331 struct rb_node **p = &hist.rb_node;
332 struct rb_node *parent = NULL;
333 struct hist_entry *he;
334 struct hist_entry entry = {
335 .thread = thread,
336 .map = map,
337 .dso = dso,
338 .sym = sym,
339 .ip = ip,
340 .level = level,
341 .count = 1,
342 };
343 int cmp;
344
345 while (*p != NULL) {
346 parent = *p;
347 he = rb_entry(parent, struct hist_entry, rb_node);
348
349 cmp = hist_entry__cmp(&entry, he);
350
351 if (!cmp) {
Ingo Molnar0b73da32009-06-06 15:48:52 +0200352 hist_hit(he, ip);
353
Ingo Molnar8035e422009-06-06 15:19:13 +0200354 return 0;
355 }
356
357 if (cmp < 0)
358 p = &(*p)->rb_left;
359 else
360 p = &(*p)->rb_right;
361 }
362
363 he = malloc(sizeof(*he));
364 if (!he)
365 return -ENOMEM;
366 *he = entry;
367 rb_link_node(&he->rb_node, parent, p);
368 rb_insert_color(&he->rb_node, &hist);
369
370 return 0;
371}
372
373static void hist_entry__free(struct hist_entry *he)
374{
375 free(he);
376}
377
378/*
379 * collapse the histogram
380 */
381
382static struct rb_root collapse_hists;
383
384static void collapse__insert_entry(struct hist_entry *he)
385{
386 struct rb_node **p = &collapse_hists.rb_node;
387 struct rb_node *parent = NULL;
388 struct hist_entry *iter;
389 int64_t cmp;
390
391 while (*p != NULL) {
392 parent = *p;
393 iter = rb_entry(parent, struct hist_entry, rb_node);
394
395 cmp = hist_entry__collapse(iter, he);
396
397 if (!cmp) {
398 iter->count += he->count;
399 hist_entry__free(he);
400 return;
401 }
402
403 if (cmp < 0)
404 p = &(*p)->rb_left;
405 else
406 p = &(*p)->rb_right;
407 }
408
409 rb_link_node(&he->rb_node, parent, p);
410 rb_insert_color(&he->rb_node, &collapse_hists);
411}
412
413static void collapse__resort(void)
414{
415 struct rb_node *next;
416 struct hist_entry *n;
417
418 if (!sort__need_collapse)
419 return;
420
421 next = rb_first(&hist);
422 while (next) {
423 n = rb_entry(next, struct hist_entry, rb_node);
424 next = rb_next(&n->rb_node);
425
426 rb_erase(&n->rb_node, &hist);
427 collapse__insert_entry(n);
428 }
429}
430
431/*
432 * reverse the map, sort on count.
433 */
434
435static struct rb_root output_hists;
436
437static void output__insert_entry(struct hist_entry *he)
438{
439 struct rb_node **p = &output_hists.rb_node;
440 struct rb_node *parent = NULL;
441 struct hist_entry *iter;
442
443 while (*p != NULL) {
444 parent = *p;
445 iter = rb_entry(parent, struct hist_entry, rb_node);
446
447 if (he->count > iter->count)
448 p = &(*p)->rb_left;
449 else
450 p = &(*p)->rb_right;
451 }
452
453 rb_link_node(&he->rb_node, parent, p);
454 rb_insert_color(&he->rb_node, &output_hists);
455}
456
457static void output__resort(void)
458{
459 struct rb_node *next;
460 struct hist_entry *n;
461 struct rb_root *tree = &hist;
462
463 if (sort__need_collapse)
464 tree = &collapse_hists;
465
466 next = rb_first(tree);
467
468 while (next) {
469 n = rb_entry(next, struct hist_entry, rb_node);
470 next = rb_next(&n->rb_node);
471
472 rb_erase(&n->rb_node, tree);
473 output__insert_entry(n);
474 }
475}
476
Ingo Molnar8035e422009-06-06 15:19:13 +0200477static void register_idle_thread(void)
478{
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200479 struct thread *thread = threads__findnew(0, &threads, &last_match);
Ingo Molnar8035e422009-06-06 15:19:13 +0200480
481 if (thread == NULL ||
482 thread__set_comm(thread, "[idle]")) {
483 fprintf(stderr, "problem inserting idle task.\n");
484 exit(-1);
485 }
486}
487
488static unsigned long total = 0,
489 total_mmap = 0,
490 total_comm = 0,
491 total_fork = 0,
492 total_unknown = 0;
493
494static int
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200495process_sample_event(event_t *event, unsigned long offset, unsigned long head)
Ingo Molnar8035e422009-06-06 15:19:13 +0200496{
497 char level;
498 int show = 0;
499 struct dso *dso = NULL;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200500 struct thread *thread;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000501 u64 ip = event->ip.ip;
Ingo Molnar8035e422009-06-06 15:19:13 +0200502 struct map *map = NULL;
503
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200504 thread = threads__findnew(event->ip.pid, &threads, &last_match);
505
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200506 dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200507 (void *)(offset + head),
508 (void *)(long)(event->header.size),
509 event->header.misc,
510 event->ip.pid,
511 (void *)(long)ip);
512
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200513 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
Ingo Molnar8035e422009-06-06 15:19:13 +0200514
515 if (thread == NULL) {
516 fprintf(stderr, "problem processing %d event, skipping it.\n",
517 event->header.type);
518 return -1;
519 }
520
521 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
522 show = SHOW_KERNEL;
523 level = 'k';
524
525 dso = kernel_dso;
526
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200527 dump_printf(" ...... dso: %s\n", dso->name);
Ingo Molnar8035e422009-06-06 15:19:13 +0200528
529 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
530
531 show = SHOW_USER;
532 level = '.';
533
534 map = thread__find_map(thread, ip);
535 if (map != NULL) {
536 ip = map->map_ip(map, ip);
537 dso = map->dso;
538 } else {
539 /*
540 * If this is outside of all known maps,
541 * and is a negative address, try to look it
542 * up in the kernel dso, as it might be a
543 * vsyscall (which executes in user-mode):
544 */
545 if ((long long)ip < 0)
546 dso = kernel_dso;
547 }
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200548 dump_printf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
Ingo Molnar8035e422009-06-06 15:19:13 +0200549
550 } else {
551 show = SHOW_HV;
552 level = 'H';
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200553 dump_printf(" ...... dso: [hypervisor]\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200554 }
555
556 if (show & show_mask) {
557 struct symbol *sym = NULL;
558
559 if (dso)
560 sym = dso->find_symbol(dso, ip);
561
562 if (hist_entry__add(thread, map, dso, sym, ip, level)) {
563 fprintf(stderr,
564 "problem incrementing symbol count, skipping event\n");
565 return -1;
566 }
567 }
568 total++;
569
570 return 0;
571}
572
573static int
574process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
575{
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200576 struct thread *thread;
Frederic Weisbecker66e274f2009-08-12 11:07:25 +0200577 struct map *map = map__new(&event->mmap, NULL, 0);
Ingo Molnar8035e422009-06-06 15:19:13 +0200578
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200579 thread = threads__findnew(event->mmap.pid, &threads, &last_match);
580
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200581 dump_printf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200582 (void *)(offset + head),
583 (void *)(long)(event->header.size),
584 event->mmap.pid,
585 (void *)(long)event->mmap.start,
586 (void *)(long)event->mmap.len,
587 (void *)(long)event->mmap.pgoff,
588 event->mmap.filename);
589
590 if (thread == NULL || map == NULL) {
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200591 dump_printf("problem processing PERF_EVENT_MMAP, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200592 return 0;
593 }
594
595 thread__insert_map(thread, map);
596 total_mmap++;
597
598 return 0;
599}
600
601static int
602process_comm_event(event_t *event, unsigned long offset, unsigned long head)
603{
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200604 struct thread *thread;
Ingo Molnar8035e422009-06-06 15:19:13 +0200605
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200606 thread = threads__findnew(event->comm.pid, &threads, &last_match);
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200607 dump_printf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200608 (void *)(offset + head),
609 (void *)(long)(event->header.size),
610 event->comm.comm, event->comm.pid);
611
612 if (thread == NULL ||
613 thread__set_comm(thread, event->comm.comm)) {
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200614 dump_printf("problem processing PERF_EVENT_COMM, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200615 return -1;
616 }
617 total_comm++;
618
619 return 0;
620}
621
622static int
623process_fork_event(event_t *event, unsigned long offset, unsigned long head)
624{
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200625 struct thread *thread;
626 struct thread *parent;
Ingo Molnar8035e422009-06-06 15:19:13 +0200627
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200628 thread = threads__findnew(event->fork.pid, &threads, &last_match);
629 parent = threads__findnew(event->fork.ppid, &threads, &last_match);
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200630 dump_printf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +0200631 (void *)(offset + head),
632 (void *)(long)(event->header.size),
633 event->fork.pid, event->fork.ppid);
634
635 if (!thread || !parent || thread__fork(thread, parent)) {
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +0200636 dump_printf("problem processing PERF_EVENT_FORK, skipping event.\n");
Ingo Molnar8035e422009-06-06 15:19:13 +0200637 return -1;
638 }
639 total_fork++;
640
641 return 0;
642}
643
644static int
Ingo Molnar8035e422009-06-06 15:19:13 +0200645process_event(event_t *event, unsigned long offset, unsigned long head)
646{
Ingo Molnar8035e422009-06-06 15:19:13 +0200647 switch (event->header.type) {
Peter Zijlstrae6e18ec2009-06-25 11:27:12 +0200648 case PERF_EVENT_SAMPLE:
649 return process_sample_event(event, offset, head);
650
Ingo Molnar8035e422009-06-06 15:19:13 +0200651 case PERF_EVENT_MMAP:
652 return process_mmap_event(event, offset, head);
653
654 case PERF_EVENT_COMM:
655 return process_comm_event(event, offset, head);
656
657 case PERF_EVENT_FORK:
658 return process_fork_event(event, offset, head);
Ingo Molnar8035e422009-06-06 15:19:13 +0200659 /*
660 * We dont process them right now but they are fine:
661 */
662
663 case PERF_EVENT_THROTTLE:
664 case PERF_EVENT_UNTHROTTLE:
665 return 0;
666
667 default:
668 return -1;
669 }
670
671 return 0;
672}
673
Ingo Molnar0b73da32009-06-06 15:48:52 +0200674static int
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000675parse_line(FILE *file, struct symbol *sym, u64 start, u64 len)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200676{
677 char *line = NULL, *tmp, *tmp2;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200678 static const char *prev_line;
679 static const char *prev_color;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200680 unsigned int offset;
681 size_t line_len;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200682 s64 line_ip;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200683 int ret;
684 char *c;
685
686 if (getline(&line, &line_len, file) < 0)
687 return -1;
688 if (!line)
689 return -1;
690
691 c = strchr(line, '\n');
692 if (c)
693 *c = 0;
694
695 line_ip = -1;
696 offset = 0;
697 ret = -2;
698
699 /*
700 * Strip leading spaces:
701 */
702 tmp = line;
703 while (*tmp) {
704 if (*tmp != ' ')
705 break;
706 tmp++;
707 }
708
709 if (*tmp) {
710 /*
711 * Parse hexa addresses followed by ':'
712 */
713 line_ip = strtoull(tmp, &tmp2, 16);
714 if (*tmp2 != ':')
715 line_ip = -1;
716 }
717
718 if (line_ip != -1) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200719 const char *path = NULL;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200720 unsigned int hits = 0;
721 double percent = 0.0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200722 const char *color;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200723 struct sym_ext *sym_ext = sym->priv;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200724
725 offset = line_ip - start;
726 if (offset < len)
727 hits = sym->hist[offset];
728
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200729 if (offset < len && sym_ext) {
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200730 path = sym_ext[offset].path;
731 percent = sym_ext[offset].percent;
732 } else if (sym->hist_sum)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200733 percent = 100.0 * hits / sym->hist_sum;
734
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200735 color = get_percent_color(percent);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200736
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200737 /*
738 * Also color the filename and line if needed, with
739 * the same color than the percentage. Don't print it
740 * twice for close colored ip with the same filename:line
741 */
742 if (path) {
743 if (!prev_line || strcmp(prev_line, path)
744 || color != prev_color) {
745 color_fprintf(stdout, color, " %s", path);
746 prev_line = path;
747 prev_color = color;
748 }
749 }
750
Ingo Molnar0b73da32009-06-06 15:48:52 +0200751 color_fprintf(stdout, color, " %7.2f", percent);
752 printf(" : ");
753 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
754 } else {
755 if (!*line)
756 printf(" :\n");
757 else
758 printf(" : %s\n", line);
759 }
760
761 return 0;
762}
763
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200764static struct rb_root root_sym_ext;
765
766static void insert_source_line(struct sym_ext *sym_ext)
767{
768 struct sym_ext *iter;
769 struct rb_node **p = &root_sym_ext.rb_node;
770 struct rb_node *parent = NULL;
771
772 while (*p != NULL) {
773 parent = *p;
774 iter = rb_entry(parent, struct sym_ext, node);
775
776 if (sym_ext->percent > iter->percent)
777 p = &(*p)->rb_left;
778 else
779 p = &(*p)->rb_right;
780 }
781
782 rb_link_node(&sym_ext->node, parent, p);
783 rb_insert_color(&sym_ext->node, &root_sym_ext);
784}
785
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200786static void free_source_line(struct symbol *sym, int len)
787{
788 struct sym_ext *sym_ext = sym->priv;
789 int i;
790
791 if (!sym_ext)
792 return;
793
794 for (i = 0; i < len; i++)
795 free(sym_ext[i].path);
796 free(sym_ext);
797
798 sym->priv = NULL;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200799 root_sym_ext = RB_ROOT;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200800}
801
802/* Get the filename:line for the colored entries */
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200803static void
Ingo Molnar83a09442009-08-15 12:26:57 +0200804get_source_line(struct symbol *sym, u64 start, int len, const char *filename)
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200805{
806 int i;
807 char cmd[PATH_MAX * 2];
808 struct sym_ext *sym_ext;
809
810 if (!sym->hist_sum)
811 return;
812
813 sym->priv = calloc(len, sizeof(struct sym_ext));
814 if (!sym->priv)
815 return;
816
817 sym_ext = sym->priv;
818
819 for (i = 0; i < len; i++) {
820 char *path = NULL;
821 size_t line_len;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000822 u64 offset;
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200823 FILE *fp;
824
825 sym_ext[i].percent = 100.0 * sym->hist[i] / sym->hist_sum;
826 if (sym_ext[i].percent <= 0.5)
827 continue;
828
829 offset = start + i;
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200830 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200831 fp = popen(cmd, "r");
832 if (!fp)
833 continue;
834
835 if (getline(&path, &line_len, fp) < 0 || !line_len)
836 goto next;
837
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200838 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200839 if (!sym_ext[i].path)
840 goto next;
841
842 strcpy(sym_ext[i].path, path);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200843 insert_source_line(&sym_ext[i]);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200844
845 next:
846 pclose(fp);
847 }
848}
849
Ingo Molnar83a09442009-08-15 12:26:57 +0200850static void print_summary(const char *filename)
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200851{
852 struct sym_ext *sym_ext;
853 struct rb_node *node;
854
855 printf("\nSorted summary for file %s\n", filename);
856 printf("----------------------------------------------\n\n");
857
858 if (RB_EMPTY_ROOT(&root_sym_ext)) {
859 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
860 return;
861 }
862
863 node = rb_first(&root_sym_ext);
864 while (node) {
865 double percent;
Ingo Molnar83a09442009-08-15 12:26:57 +0200866 const char *color;
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200867 char *path;
868
869 sym_ext = rb_entry(node, struct sym_ext, node);
870 percent = sym_ext->percent;
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200871 color = get_percent_color(percent);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200872 path = sym_ext->path;
873
874 color_fprintf(stdout, color, " %7.2f %s", percent, path);
875 node = rb_next(node);
876 }
877}
878
Ingo Molnar0b73da32009-06-06 15:48:52 +0200879static void annotate_sym(struct dso *dso, struct symbol *sym)
880{
Ingo Molnar83a09442009-08-15 12:26:57 +0200881 const char *filename = dso->name, *d_filename;
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000882 u64 start, end, len;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200883 char command[PATH_MAX*2];
884 FILE *file;
885
886 if (!filename)
887 return;
Mike Galbraith42976482009-07-02 08:09:46 +0200888 if (sym->module)
889 filename = sym->module->path;
890 else if (dso == kernel_dso)
Ingo Molnar83a09442009-08-15 12:26:57 +0200891 filename = vmlinux_name;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200892
Ingo Molnar0b73da32009-06-06 15:48:52 +0200893 start = sym->obj_start;
894 if (!start)
895 start = sym->start;
Mike Galbraith42976482009-07-02 08:09:46 +0200896 if (full_paths)
897 d_filename = filename;
898 else
899 d_filename = basename(filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200900
901 end = start + sym->end - sym->start + 1;
902 len = sym->end - sym->start;
903
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200904 if (print_line) {
Frederic Weisbeckerc17c2db2009-06-13 17:39:23 +0200905 get_source_line(sym, start, len, filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200906 print_summary(filename);
907 }
908
909 printf("\n\n------------------------------------------------\n");
Mike Galbraith42976482009-07-02 08:09:46 +0200910 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200911 printf("------------------------------------------------\n");
912
913 if (verbose >= 2)
914 printf("annotating [%p] %30s : [%p] %30s\n", dso, dso->name, sym, sym->name);
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200915
Mike Galbraith42976482009-07-02 08:09:46 +0200916 sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
917 (u64)start, (u64)end, filename, filename);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200918
919 if (verbose >= 3)
920 printf("doing: %s\n", command);
921
922 file = popen(command, "r");
923 if (!file)
924 return;
925
926 while (!feof(file)) {
927 if (parse_line(file, sym, start, len) < 0)
928 break;
929 }
930
931 pclose(file);
Frederic Weisbecker971738f2009-06-13 00:11:22 +0200932 if (print_line)
933 free_source_line(sym, len);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200934}
935
936static void find_annotations(void)
937{
938 struct rb_node *nd;
939 struct dso *dso;
940 int count = 0;
941
942 list_for_each_entry(dso, &dsos, node) {
943
944 for (nd = rb_first(&dso->syms); nd; nd = rb_next(nd)) {
945 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
946
947 if (sym->hist) {
948 annotate_sym(dso, sym);
949 count++;
950 }
951 }
952 }
953
954 if (!count)
955 printf(" Error: symbol '%s' not present amongst the samples.\n", sym_hist_filter);
956}
957
Ingo Molnar8035e422009-06-06 15:19:13 +0200958static int __cmd_annotate(void)
959{
960 int ret, rc = EXIT_FAILURE;
961 unsigned long offset = 0;
962 unsigned long head = 0;
Ingo Molnar83a09442009-08-15 12:26:57 +0200963 struct stat input_stat;
Ingo Molnar8035e422009-06-06 15:19:13 +0200964 event_t *event;
965 uint32_t size;
966 char *buf;
967
968 register_idle_thread();
969
970 input = open(input_name, O_RDONLY);
971 if (input < 0) {
972 perror("failed to open file");
973 exit(-1);
974 }
975
Ingo Molnar83a09442009-08-15 12:26:57 +0200976 ret = fstat(input, &input_stat);
Ingo Molnar8035e422009-06-06 15:19:13 +0200977 if (ret < 0) {
978 perror("failed to stat file");
979 exit(-1);
980 }
981
Ingo Molnar83a09442009-08-15 12:26:57 +0200982 if (!input_stat.st_size) {
Ingo Molnar8035e422009-06-06 15:19:13 +0200983 fprintf(stderr, "zero-sized file, nothing to do!\n");
984 exit(0);
985 }
986
987 if (load_kernel() < 0) {
988 perror("failed to load kernel symbols");
989 return EXIT_FAILURE;
990 }
991
Ingo Molnar8035e422009-06-06 15:19:13 +0200992remap:
993 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
994 MAP_SHARED, input, offset);
995 if (buf == MAP_FAILED) {
996 perror("failed to mmap file");
997 exit(-1);
998 }
999
1000more:
1001 event = (event_t *)(buf + head);
1002
1003 size = event->header.size;
1004 if (!size)
1005 size = 8;
1006
1007 if (head + event->header.size >= page_size * mmap_window) {
1008 unsigned long shift = page_size * (head / page_size);
Ingo Molnar83a09442009-08-15 12:26:57 +02001009 int munmap_ret;
Ingo Molnar8035e422009-06-06 15:19:13 +02001010
Ingo Molnar83a09442009-08-15 12:26:57 +02001011 munmap_ret = munmap(buf, page_size * mmap_window);
1012 assert(munmap_ret == 0);
Ingo Molnar8035e422009-06-06 15:19:13 +02001013
1014 offset += shift;
1015 head -= shift;
1016 goto remap;
1017 }
1018
1019 size = event->header.size;
1020
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001021 dump_printf("%p [%p]: event: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +02001022 (void *)(offset + head),
1023 (void *)(long)event->header.size,
1024 event->header.type);
1025
1026 if (!size || process_event(event, offset, head) < 0) {
1027
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001028 dump_printf("%p [%p]: skipping unknown header type: %d\n",
Ingo Molnar8035e422009-06-06 15:19:13 +02001029 (void *)(offset + head),
1030 (void *)(long)(event->header.size),
1031 event->header.type);
1032
1033 total_unknown++;
1034
1035 /*
1036 * assume we lost track of the stream, check alignment, and
1037 * increment a single u64 in the hope to catch on again 'soon'.
1038 */
1039
1040 if (unlikely(head & 7))
1041 head &= ~7ULL;
1042
1043 size = 8;
1044 }
1045
1046 head += size;
1047
Ingo Molnar83a09442009-08-15 12:26:57 +02001048 if (offset + head < (unsigned long)input_stat.st_size)
Ingo Molnar8035e422009-06-06 15:19:13 +02001049 goto more;
1050
1051 rc = EXIT_SUCCESS;
1052 close(input);
1053
Frederic Weisbecker2cec19d2009-08-16 19:24:21 +02001054 dump_printf(" IP events: %10ld\n", total);
1055 dump_printf(" mmap events: %10ld\n", total_mmap);
1056 dump_printf(" comm events: %10ld\n", total_comm);
1057 dump_printf(" fork events: %10ld\n", total_fork);
1058 dump_printf(" unknown events: %10ld\n", total_unknown);
Ingo Molnar8035e422009-06-06 15:19:13 +02001059
1060 if (dump_trace)
1061 return 0;
1062
1063 if (verbose >= 3)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02001064 threads__fprintf(stdout, &threads);
Ingo Molnar8035e422009-06-06 15:19:13 +02001065
1066 if (verbose >= 2)
1067 dsos__fprintf(stdout);
1068
1069 collapse__resort();
1070 output__resort();
Ingo Molnar0b73da32009-06-06 15:48:52 +02001071
1072 find_annotations();
Ingo Molnar8035e422009-06-06 15:19:13 +02001073
1074 return rc;
1075}
1076
1077static const char * const annotate_usage[] = {
1078 "perf annotate [<options>] <command>",
1079 NULL
1080};
1081
1082static const struct option options[] = {
1083 OPT_STRING('i', "input", &input_name, "file",
1084 "input file name"),
Ingo Molnar23b87112009-06-06 21:25:29 +02001085 OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
Ingo Molnar0b73da32009-06-06 15:48:52 +02001086 "symbol to annotate"),
Ingo Molnar8035e422009-06-06 15:19:13 +02001087 OPT_BOOLEAN('v', "verbose", &verbose,
1088 "be more verbose (show symbol address, etc)"),
1089 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1090 "dump raw trace in ASCII"),
Ingo Molnar83a09442009-08-15 12:26:57 +02001091 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Mike Galbraith42976482009-07-02 08:09:46 +02001092 OPT_BOOLEAN('m', "modules", &modules,
1093 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Frederic Weisbecker301406b2009-06-13 00:11:21 +02001094 OPT_BOOLEAN('l', "print-line", &print_line,
1095 "print matching source lines (may be slow)"),
Mike Galbraith42976482009-07-02 08:09:46 +02001096 OPT_BOOLEAN('P', "full-paths", &full_paths,
1097 "Don't shorten the displayed pathnames"),
Ingo Molnar8035e422009-06-06 15:19:13 +02001098 OPT_END()
1099};
1100
1101static void setup_sorting(void)
1102{
1103 char *tmp, *tok, *str = strdup(sort_order);
1104
1105 for (tok = strtok_r(str, ", ", &tmp);
1106 tok; tok = strtok_r(NULL, ", ", &tmp)) {
1107 if (sort_dimension__add(tok) < 0) {
1108 error("Unknown --sort key: `%s'", tok);
1109 usage_with_options(annotate_usage, options);
1110 }
1111 }
1112
1113 free(str);
1114}
1115
Ingo Molnarf37a2912009-07-01 12:37:06 +02001116int cmd_annotate(int argc, const char **argv, const char *prefix __used)
Ingo Molnar8035e422009-06-06 15:19:13 +02001117{
1118 symbol__init();
1119
1120 page_size = getpagesize();
1121
1122 argc = parse_options(argc, argv, options, annotate_usage, 0);
1123
1124 setup_sorting();
1125
Ingo Molnar0b73da32009-06-06 15:48:52 +02001126 if (argc) {
1127 /*
1128 * Special case: if there's an argument left then assume tha
1129 * it's a symbol filter:
1130 */
1131 if (argc > 1)
1132 usage_with_options(annotate_usage, options);
1133
1134 sym_hist_filter = argv[0];
1135 }
1136
1137 if (!sym_hist_filter)
Ingo Molnar8035e422009-06-06 15:19:13 +02001138 usage_with_options(annotate_usage, options);
1139
1140 setup_pager();
1141
1142 return __cmd_annotate();
1143}