perf annotate browser: Handle NULL jump targets
[linux-2.6.git] / tools / perf / ui / browsers / annotate.c
blob4c83fe3d7dad7ab57b0f6acaa964e52564a8ed24
1 #include "../../util/util.h"
2 #include "../browser.h"
3 #include "../helpline.h"
4 #include "../libslang.h"
5 #include "../ui.h"
6 #include "../util.h"
7 #include "../../util/annotate.h"
8 #include "../../util/hist.h"
9 #include "../../util/sort.h"
10 #include "../../util/symbol.h"
11 #include <pthread.h>
12 #include <newt.h>
14 struct browser_disasm_line {
15 struct rb_node rb_node;
16 double percent;
17 u32 idx;
18 int idx_asm;
19 bool jump_target;
22 struct annotate_browser {
23 struct ui_browser b;
24 struct rb_root entries;
25 struct rb_node *curr_hot;
26 struct disasm_line *selection;
27 struct disasm_line **offsets;
28 u64 start;
29 int nr_asm_entries;
30 int nr_entries;
31 bool hide_src_code;
32 bool use_offset;
33 bool searching_backwards;
34 u8 offset_width;
35 char search_bf[128];
38 static inline struct browser_disasm_line *disasm_line__browser(struct disasm_line *dl)
40 return (struct browser_disasm_line *)(dl + 1);
43 static bool disasm_line__filter(struct ui_browser *browser, void *entry)
45 struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
47 if (ab->hide_src_code) {
48 struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
49 return dl->offset == -1;
52 return false;
55 static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
57 struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
58 struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
59 struct browser_disasm_line *bdl = disasm_line__browser(dl);
60 bool current_entry = ui_browser__is_current_entry(self, row);
61 bool change_color = (!ab->hide_src_code &&
62 (!current_entry || (self->use_navkeypressed &&
63 !self->navkeypressed)));
64 int width = self->width;
66 if (dl->offset != -1) {
67 ui_browser__set_percent_color(self, bdl->percent, current_entry);
68 slsmg_printf(" %7.2f ", bdl->percent);
69 } else {
70 ui_browser__set_percent_color(self, 0, current_entry);
71 slsmg_write_nstring(" ", 9);
74 ui_browser__write_graph(self, SLSMG_VLINE_CHAR);
75 SLsmg_write_char(' ');
77 /* The scroll bar isn't being used */
78 if (!self->navkeypressed)
79 width += 1;
81 if (dl->offset != -1 && change_color)
82 ui_browser__set_color(self, HE_COLORSET_CODE);
84 if (!*dl->line)
85 slsmg_write_nstring(" ", width - 10);
86 else if (dl->offset == -1)
87 slsmg_write_nstring(dl->line, width - 10);
88 else {
89 char bf[256];
90 u64 addr = dl->offset;
91 int printed, color = -1;
93 if (!ab->use_offset)
94 addr += ab->start;
96 if (!ab->use_offset) {
97 printed = scnprintf(bf, sizeof(bf), " %" PRIx64 ":", addr);
98 } else {
99 if (bdl->jump_target) {
100 printed = scnprintf(bf, sizeof(bf), " %*" PRIx64 ":",
101 ab->offset_width, addr);
102 } else {
103 printed = scnprintf(bf, sizeof(bf), " %*s ",
104 ab->offset_width, " ");
108 if (change_color)
109 color = ui_browser__set_color(self, HE_COLORSET_ADDR);
110 slsmg_write_nstring(bf, printed);
111 if (change_color)
112 ui_browser__set_color(self, color);
113 if (dl->ins && dl->ins->ops->scnprintf) {
114 if (ins__is_jump(dl->ins)) {
115 bool fwd = dl->ops.target > (u64)dl->offset;
117 ui_browser__write_graph(self, fwd ? SLSMG_DARROW_CHAR :
118 SLSMG_UARROW_CHAR);
119 SLsmg_write_char(' ');
120 } else {
121 slsmg_write_nstring(" ", 2);
124 dl->ins->ops->scnprintf(dl->ins, bf, sizeof(bf), &dl->ops,
125 !ab->use_offset);
126 } else {
127 if (strcmp(dl->name, "retq")) {
128 slsmg_write_nstring(" ", 2);
129 } else {
130 ui_browser__write_graph(self, SLSMG_LARROW_CHAR);
131 SLsmg_write_char(' ');
134 scnprintf(bf, sizeof(bf), "%-6.6s %s", dl->name, dl->ops.raw);
137 slsmg_write_nstring(bf, width - 12 - printed);
140 if (current_entry)
141 ab->selection = dl;
144 static void annotate_browser__draw_current_loop(struct ui_browser *browser)
146 struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
147 struct map_symbol *ms = browser->priv;
148 struct symbol *sym = ms->sym;
149 struct annotation *notes = symbol__annotation(sym);
150 struct disasm_line *cursor = ab->selection, *pos = cursor, *target;
151 struct browser_disasm_line *bcursor = disasm_line__browser(cursor),
152 *btarget, *bpos;
153 unsigned int from, to, start_width = 2;
155 list_for_each_entry_from(pos, &notes->src->source, node) {
156 if (!pos->ins || !ins__is_jump(pos->ins))
157 continue;
159 target = ab->offsets[pos->ops.target];
160 if (!target)
161 continue;
163 btarget = disasm_line__browser(target);
164 if (btarget->idx <= bcursor->idx)
165 goto found;
168 return;
170 found:
171 bpos = disasm_line__browser(pos);
172 if (ab->hide_src_code) {
173 from = bpos->idx_asm;
174 to = btarget->idx_asm;
175 } else {
176 from = (u64)bpos->idx;
177 to = (u64)btarget->idx;
180 ui_browser__set_color(browser, HE_COLORSET_CODE);
182 if (!bpos->jump_target)
183 start_width += ab->offset_width + 1;
185 __ui_browser__line_arrow_up(browser, 10, from, to, start_width);
188 static unsigned int annotate_browser__refresh(struct ui_browser *browser)
190 int ret = ui_browser__list_head_refresh(browser);
192 annotate_browser__draw_current_loop(browser);
194 return ret;
197 static double disasm_line__calc_percent(struct disasm_line *dl, struct symbol *sym, int evidx)
199 double percent = 0.0;
201 if (dl->offset != -1) {
202 int len = sym->end - sym->start;
203 unsigned int hits = 0;
204 struct annotation *notes = symbol__annotation(sym);
205 struct source_line *src_line = notes->src->lines;
206 struct sym_hist *h = annotation__histogram(notes, evidx);
207 s64 offset = dl->offset;
208 struct disasm_line *next;
210 next = disasm__get_next_ip_line(&notes->src->source, dl);
211 while (offset < (s64)len &&
212 (next == NULL || offset < next->offset)) {
213 if (src_line) {
214 percent += src_line[offset].percent;
215 } else
216 hits += h->addr[offset];
218 ++offset;
221 * If the percentage wasn't already calculated in
222 * symbol__get_source_line, do it now:
224 if (src_line == NULL && h->sum)
225 percent = 100.0 * hits / h->sum;
228 return percent;
231 static void disasm_rb_tree__insert(struct rb_root *root, struct browser_disasm_line *bdl)
233 struct rb_node **p = &root->rb_node;
234 struct rb_node *parent = NULL;
235 struct browser_disasm_line *l;
237 while (*p != NULL) {
238 parent = *p;
239 l = rb_entry(parent, struct browser_disasm_line, rb_node);
240 if (bdl->percent < l->percent)
241 p = &(*p)->rb_left;
242 else
243 p = &(*p)->rb_right;
245 rb_link_node(&bdl->rb_node, parent, p);
246 rb_insert_color(&bdl->rb_node, root);
249 static void annotate_browser__set_top(struct annotate_browser *self,
250 struct disasm_line *pos, u32 idx)
252 unsigned back;
254 ui_browser__refresh_dimensions(&self->b);
255 back = self->b.height / 2;
256 self->b.top_idx = self->b.index = idx;
258 while (self->b.top_idx != 0 && back != 0) {
259 pos = list_entry(pos->node.prev, struct disasm_line, node);
261 if (disasm_line__filter(&self->b, &pos->node))
262 continue;
264 --self->b.top_idx;
265 --back;
268 self->b.top = pos;
269 self->b.navkeypressed = true;
272 static void annotate_browser__set_rb_top(struct annotate_browser *browser,
273 struct rb_node *nd)
275 struct browser_disasm_line *bpos;
276 struct disasm_line *pos;
278 bpos = rb_entry(nd, struct browser_disasm_line, rb_node);
279 pos = ((struct disasm_line *)bpos) - 1;
280 annotate_browser__set_top(browser, pos, bpos->idx);
281 browser->curr_hot = nd;
284 static void annotate_browser__calc_percent(struct annotate_browser *browser,
285 int evidx)
287 struct map_symbol *ms = browser->b.priv;
288 struct symbol *sym = ms->sym;
289 struct annotation *notes = symbol__annotation(sym);
290 struct disasm_line *pos;
292 browser->entries = RB_ROOT;
294 pthread_mutex_lock(&notes->lock);
296 list_for_each_entry(pos, &notes->src->source, node) {
297 struct browser_disasm_line *bpos = disasm_line__browser(pos);
298 bpos->percent = disasm_line__calc_percent(pos, sym, evidx);
299 if (bpos->percent < 0.01) {
300 RB_CLEAR_NODE(&bpos->rb_node);
301 continue;
303 disasm_rb_tree__insert(&browser->entries, bpos);
305 pthread_mutex_unlock(&notes->lock);
307 browser->curr_hot = rb_last(&browser->entries);
310 static bool annotate_browser__toggle_source(struct annotate_browser *browser)
312 struct disasm_line *dl;
313 struct browser_disasm_line *bdl;
314 off_t offset = browser->b.index - browser->b.top_idx;
316 browser->b.seek(&browser->b, offset, SEEK_CUR);
317 dl = list_entry(browser->b.top, struct disasm_line, node);
318 bdl = disasm_line__browser(dl);
320 if (browser->hide_src_code) {
321 if (bdl->idx_asm < offset)
322 offset = bdl->idx;
324 browser->b.nr_entries = browser->nr_entries;
325 browser->hide_src_code = false;
326 browser->b.seek(&browser->b, -offset, SEEK_CUR);
327 browser->b.top_idx = bdl->idx - offset;
328 browser->b.index = bdl->idx;
329 } else {
330 if (bdl->idx_asm < 0) {
331 ui_helpline__puts("Only available for assembly lines.");
332 browser->b.seek(&browser->b, -offset, SEEK_CUR);
333 return false;
336 if (bdl->idx_asm < offset)
337 offset = bdl->idx_asm;
339 browser->b.nr_entries = browser->nr_asm_entries;
340 browser->hide_src_code = true;
341 browser->b.seek(&browser->b, -offset, SEEK_CUR);
342 browser->b.top_idx = bdl->idx_asm - offset;
343 browser->b.index = bdl->idx_asm;
346 return true;
349 static bool annotate_browser__callq(struct annotate_browser *browser,
350 int evidx, void (*timer)(void *arg),
351 void *arg, int delay_secs)
353 struct map_symbol *ms = browser->b.priv;
354 struct disasm_line *dl = browser->selection;
355 struct symbol *sym = ms->sym;
356 struct annotation *notes;
357 struct symbol *target;
358 u64 ip;
360 if (!ins__is_call(dl->ins))
361 return false;
363 ip = ms->map->map_ip(ms->map, dl->ops.target);
364 target = map__find_symbol(ms->map, ip, NULL);
365 if (target == NULL) {
366 ui_helpline__puts("The called function was not found.");
367 return true;
370 notes = symbol__annotation(target);
371 pthread_mutex_lock(&notes->lock);
373 if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
374 pthread_mutex_unlock(&notes->lock);
375 ui__warning("Not enough memory for annotating '%s' symbol!\n",
376 target->name);
377 return true;
380 pthread_mutex_unlock(&notes->lock);
381 symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
382 ui_browser__show_title(&browser->b, sym->name);
383 return true;
386 static
387 struct disasm_line *annotate_browser__find_offset(struct annotate_browser *browser,
388 s64 offset, s64 *idx)
390 struct map_symbol *ms = browser->b.priv;
391 struct symbol *sym = ms->sym;
392 struct annotation *notes = symbol__annotation(sym);
393 struct disasm_line *pos;
395 *idx = 0;
396 list_for_each_entry(pos, &notes->src->source, node) {
397 if (pos->offset == offset)
398 return pos;
399 if (!disasm_line__filter(&browser->b, &pos->node))
400 ++*idx;
403 return NULL;
406 static bool annotate_browser__jump(struct annotate_browser *browser)
408 struct disasm_line *dl = browser->selection;
409 s64 idx;
411 if (!ins__is_jump(dl->ins))
412 return false;
414 dl = annotate_browser__find_offset(browser, dl->ops.target, &idx);
415 if (dl == NULL) {
416 ui_helpline__puts("Invallid jump offset");
417 return true;
420 annotate_browser__set_top(browser, dl, idx);
422 return true;
425 static
426 struct disasm_line *annotate_browser__find_string(struct annotate_browser *browser,
427 char *s, s64 *idx)
429 struct map_symbol *ms = browser->b.priv;
430 struct symbol *sym = ms->sym;
431 struct annotation *notes = symbol__annotation(sym);
432 struct disasm_line *pos = browser->selection;
434 *idx = browser->b.index;
435 list_for_each_entry_continue(pos, &notes->src->source, node) {
436 if (disasm_line__filter(&browser->b, &pos->node))
437 continue;
439 ++*idx;
441 if (pos->line && strstr(pos->line, s) != NULL)
442 return pos;
445 return NULL;
448 static bool __annotate_browser__search(struct annotate_browser *browser)
450 struct disasm_line *dl;
451 s64 idx;
453 dl = annotate_browser__find_string(browser, browser->search_bf, &idx);
454 if (dl == NULL) {
455 ui_helpline__puts("String not found!");
456 return false;
459 annotate_browser__set_top(browser, dl, idx);
460 browser->searching_backwards = false;
461 return true;
464 static
465 struct disasm_line *annotate_browser__find_string_reverse(struct annotate_browser *browser,
466 char *s, s64 *idx)
468 struct map_symbol *ms = browser->b.priv;
469 struct symbol *sym = ms->sym;
470 struct annotation *notes = symbol__annotation(sym);
471 struct disasm_line *pos = browser->selection;
473 *idx = browser->b.index;
474 list_for_each_entry_continue_reverse(pos, &notes->src->source, node) {
475 if (disasm_line__filter(&browser->b, &pos->node))
476 continue;
478 --*idx;
480 if (pos->line && strstr(pos->line, s) != NULL)
481 return pos;
484 return NULL;
487 static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
489 struct disasm_line *dl;
490 s64 idx;
492 dl = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
493 if (dl == NULL) {
494 ui_helpline__puts("String not found!");
495 return false;
498 annotate_browser__set_top(browser, dl, idx);
499 browser->searching_backwards = true;
500 return true;
503 static bool annotate_browser__search_window(struct annotate_browser *browser,
504 int delay_secs)
506 if (ui_browser__input_window("Search", "String: ", browser->search_bf,
507 "ENTER: OK, ESC: Cancel",
508 delay_secs * 2) != K_ENTER ||
509 !*browser->search_bf)
510 return false;
512 return true;
515 static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs)
517 if (annotate_browser__search_window(browser, delay_secs))
518 return __annotate_browser__search(browser);
520 return false;
523 static bool annotate_browser__continue_search(struct annotate_browser *browser,
524 int delay_secs)
526 if (!*browser->search_bf)
527 return annotate_browser__search(browser, delay_secs);
529 return __annotate_browser__search(browser);
532 static bool annotate_browser__search_reverse(struct annotate_browser *browser,
533 int delay_secs)
535 if (annotate_browser__search_window(browser, delay_secs))
536 return __annotate_browser__search_reverse(browser);
538 return false;
541 static
542 bool annotate_browser__continue_search_reverse(struct annotate_browser *browser,
543 int delay_secs)
545 if (!*browser->search_bf)
546 return annotate_browser__search_reverse(browser, delay_secs);
548 return __annotate_browser__search_reverse(browser);
551 static int annotate_browser__run(struct annotate_browser *self, int evidx,
552 void(*timer)(void *arg),
553 void *arg, int delay_secs)
555 struct rb_node *nd = NULL;
556 struct map_symbol *ms = self->b.priv;
557 struct symbol *sym = ms->sym;
558 const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
559 "H: Go to hottest line, ->/ENTER: Line action, "
560 "O: Toggle offset view, "
561 "S: Toggle source code view";
562 int key;
564 if (ui_browser__show(&self->b, sym->name, help) < 0)
565 return -1;
567 annotate_browser__calc_percent(self, evidx);
569 if (self->curr_hot) {
570 annotate_browser__set_rb_top(self, self->curr_hot);
571 self->b.navkeypressed = false;
574 nd = self->curr_hot;
576 while (1) {
577 key = ui_browser__run(&self->b, delay_secs);
579 if (delay_secs != 0) {
580 annotate_browser__calc_percent(self, evidx);
582 * Current line focus got out of the list of most active
583 * lines, NULL it so that if TAB|UNTAB is pressed, we
584 * move to curr_hot (current hottest line).
586 if (nd != NULL && RB_EMPTY_NODE(nd))
587 nd = NULL;
590 switch (key) {
591 case K_TIMER:
592 if (timer != NULL)
593 timer(arg);
595 if (delay_secs != 0)
596 symbol__annotate_decay_histogram(sym, evidx);
597 continue;
598 case K_TAB:
599 if (nd != NULL) {
600 nd = rb_prev(nd);
601 if (nd == NULL)
602 nd = rb_last(&self->entries);
603 } else
604 nd = self->curr_hot;
605 break;
606 case K_UNTAB:
607 if (nd != NULL)
608 nd = rb_next(nd);
609 if (nd == NULL)
610 nd = rb_first(&self->entries);
611 else
612 nd = self->curr_hot;
613 break;
614 case 'H':
615 case 'h':
616 nd = self->curr_hot;
617 break;
618 case 'S':
619 case 's':
620 if (annotate_browser__toggle_source(self))
621 ui_helpline__puts(help);
622 continue;
623 case 'O':
624 case 'o':
625 self->use_offset = !self->use_offset;
626 continue;
627 case '/':
628 if (annotate_browser__search(self, delay_secs)) {
629 show_help:
630 ui_helpline__puts(help);
632 continue;
633 case 'n':
634 if (self->searching_backwards ?
635 annotate_browser__continue_search_reverse(self, delay_secs) :
636 annotate_browser__continue_search(self, delay_secs))
637 goto show_help;
638 continue;
639 case '?':
640 if (annotate_browser__search_reverse(self, delay_secs))
641 goto show_help;
642 continue;
643 case K_ENTER:
644 case K_RIGHT:
645 if (self->selection == NULL)
646 ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
647 else if (self->selection->offset == -1)
648 ui_helpline__puts("Actions are only available for assembly lines.");
649 else if (!self->selection->ins) {
650 if (strcmp(self->selection->name, "retq"))
651 goto show_sup_ins;
652 goto out;
653 } else if (!(annotate_browser__jump(self) ||
654 annotate_browser__callq(self, evidx, timer, arg, delay_secs))) {
655 show_sup_ins:
656 ui_helpline__puts("Actions are only available for 'callq', 'retq' & jump instructions.");
658 continue;
659 case K_LEFT:
660 case K_ESC:
661 case 'q':
662 case CTRL('c'):
663 goto out;
664 default:
665 continue;
668 if (nd != NULL)
669 annotate_browser__set_rb_top(self, nd);
671 out:
672 ui_browser__hide(&self->b);
673 return key;
676 int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
677 void(*timer)(void *arg), void *arg, int delay_secs)
679 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
680 timer, arg, delay_secs);
683 static void annotate_browser__mark_jump_targets(struct annotate_browser *browser,
684 size_t size)
686 u64 offset;
688 for (offset = 0; offset < size; ++offset) {
689 struct disasm_line *dl = browser->offsets[offset], *dlt;
690 struct browser_disasm_line *bdlt;
692 if (!dl || !dl->ins || !ins__is_jump(dl->ins))
693 continue;
695 if (dl->ops.target >= size) {
696 ui__error("jump to after symbol!\n"
697 "size: %zx, jump target: %" PRIx64,
698 size, dl->ops.target);
699 continue;
702 dlt = browser->offsets[dl->ops.target];
704 * FIXME: Oops, no jump target? Buggy disassembler? Or do we
705 * have to adjust to the previous offset?
707 if (dlt == NULL)
708 continue;
710 bdlt = disasm_line__browser(dlt);
711 bdlt->jump_target = true;
716 int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
717 void(*timer)(void *arg), void *arg,
718 int delay_secs)
720 struct disasm_line *pos, *n;
721 struct annotation *notes;
722 const size_t size = symbol__size(sym);
723 struct map_symbol ms = {
724 .map = map,
725 .sym = sym,
727 struct annotate_browser browser = {
728 .b = {
729 .refresh = annotate_browser__refresh,
730 .seek = ui_browser__list_head_seek,
731 .write = annotate_browser__write,
732 .filter = disasm_line__filter,
733 .priv = &ms,
734 .use_navkeypressed = true,
736 .use_offset = true,
738 int ret = -1;
740 if (sym == NULL)
741 return -1;
743 if (map->dso->annotate_warned)
744 return -1;
746 browser.offsets = zalloc(size * sizeof(struct disasm_line *));
747 if (browser.offsets == NULL) {
748 ui__error("Not enough memory!");
749 return -1;
752 if (symbol__annotate(sym, map, sizeof(struct browser_disasm_line)) < 0) {
753 ui__error("%s", ui_helpline__last_msg);
754 goto out_free_offsets;
757 ui_helpline__push("Press <- or ESC to exit");
759 notes = symbol__annotation(sym);
760 browser.start = map__rip_2objdump(map, sym->start);
762 list_for_each_entry(pos, &notes->src->source, node) {
763 struct browser_disasm_line *bpos;
764 size_t line_len = strlen(pos->line);
766 if (browser.b.width < line_len)
767 browser.b.width = line_len;
768 bpos = disasm_line__browser(pos);
769 bpos->idx = browser.nr_entries++;
770 if (pos->offset != -1) {
771 bpos->idx_asm = browser.nr_asm_entries++;
773 * FIXME: short term bandaid to cope with assembly
774 * routines that comes with labels in the same column
775 * as the address in objdump, sigh.
777 * E.g. copy_user_generic_unrolled
779 if (pos->offset < (s64)size)
780 browser.offsets[pos->offset] = pos;
781 } else
782 bpos->idx_asm = -1;
785 annotate_browser__mark_jump_targets(&browser, size);
787 browser.offset_width = hex_width(size);
788 browser.b.nr_entries = browser.nr_entries;
789 browser.b.entries = &notes->src->source,
790 browser.b.width += 18; /* Percentage */
791 ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
792 list_for_each_entry_safe(pos, n, &notes->src->source, node) {
793 list_del(&pos->node);
794 disasm_line__free(pos);
797 out_free_offsets:
798 free(browser.offsets);
799 return ret;