1 #include "../../util/util.h"
2 #include "../browser.h"
3 #include "../helpline.h"
4 #include "../libslang.h"
7 #include "../../util/annotate.h"
8 #include "../../util/hist.h"
9 #include "../../util/sort.h"
10 #include "../../util/symbol.h"
14 struct browser_disasm_line {
15 struct rb_node rb_node;
22 static struct annotate_browser_opt {
27 } annotate_browser__opts = {
32 struct annotate_browser {
34 struct rb_root entries;
35 struct rb_node *curr_hot;
36 struct disasm_line *selection;
37 struct disasm_line **offsets;
43 bool searching_backwards;
52 static inline struct browser_disasm_line *disasm_line__browser(struct disasm_line *dl)
54 return (struct browser_disasm_line *)(dl + 1);
57 static bool disasm_line__filter(struct ui_browser *browser __used, void *entry)
59 if (annotate_browser__opts.hide_src_code) {
60 struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
61 return dl->offset == -1;
67 static int annotate_browser__jumps_percent_color(struct annotate_browser *browser,
70 if (current && (!browser->b.use_navkeypressed || browser->b.navkeypressed))
71 return HE_COLORSET_SELECTED;
72 if (nr == browser->max_jump_sources)
73 return HE_COLORSET_TOP;
75 return HE_COLORSET_MEDIUM;
76 return HE_COLORSET_NORMAL;
79 static int annotate_browser__set_jumps_percent_color(struct annotate_browser *browser,
82 int color = annotate_browser__jumps_percent_color(browser, nr, current);
83 return ui_browser__set_color(&browser->b, color);
86 static void annotate_browser__write(struct ui_browser *browser, void *entry, int row)
88 struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
89 struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
90 struct browser_disasm_line *bdl = disasm_line__browser(dl);
91 bool current_entry = ui_browser__is_current_entry(browser, row);
92 bool change_color = (!annotate_browser__opts.hide_src_code &&
93 (!current_entry || (browser->use_navkeypressed &&
94 !browser->navkeypressed)));
95 int width = browser->width, printed;
98 if (dl->offset != -1 && bdl->percent != 0.0) {
99 ui_browser__set_percent_color(browser, bdl->percent, current_entry);
100 slsmg_printf("%6.2f ", bdl->percent);
102 ui_browser__set_percent_color(browser, 0, current_entry);
103 slsmg_write_nstring(" ", 7);
106 SLsmg_write_char(' ');
108 /* The scroll bar isn't being used */
109 if (!browser->navkeypressed)
113 slsmg_write_nstring(" ", width - 7);
114 else if (dl->offset == -1) {
115 printed = scnprintf(bf, sizeof(bf), "%*s ",
116 ab->addr_width, " ");
117 slsmg_write_nstring(bf, printed);
118 slsmg_write_nstring(dl->line, width - printed - 6);
120 u64 addr = dl->offset;
123 if (!annotate_browser__opts.use_offset)
126 if (!annotate_browser__opts.use_offset) {
127 printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr);
129 if (bdl->jump_sources) {
130 if (annotate_browser__opts.show_nr_jumps) {
132 printed = scnprintf(bf, sizeof(bf), "%*d ",
135 prev = annotate_browser__set_jumps_percent_color(ab, bdl->jump_sources,
137 slsmg_write_nstring(bf, printed);
138 ui_browser__set_color(browser, prev);
141 printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ",
142 ab->target_width, addr);
144 printed = scnprintf(bf, sizeof(bf), "%*s ",
145 ab->addr_width, " ");
150 color = ui_browser__set_color(browser, HE_COLORSET_ADDR);
151 slsmg_write_nstring(bf, printed);
153 ui_browser__set_color(browser, color);
154 if (dl->ins && dl->ins->ops->scnprintf) {
155 if (ins__is_jump(dl->ins)) {
156 bool fwd = dl->ops.target.offset > (u64)dl->offset;
158 ui_browser__write_graph(browser, fwd ? SLSMG_DARROW_CHAR :
160 SLsmg_write_char(' ');
161 } else if (ins__is_call(dl->ins)) {
162 ui_browser__write_graph(browser, SLSMG_RARROW_CHAR);
163 SLsmg_write_char(' ');
165 slsmg_write_nstring(" ", 2);
168 if (strcmp(dl->name, "retq")) {
169 slsmg_write_nstring(" ", 2);
171 ui_browser__write_graph(browser, SLSMG_LARROW_CHAR);
172 SLsmg_write_char(' ');
176 disasm_line__scnprintf(dl, bf, sizeof(bf), !annotate_browser__opts.use_offset);
177 slsmg_write_nstring(bf, width - 10 - printed);
184 static void annotate_browser__draw_current_jump(struct ui_browser *browser)
186 struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
187 struct disasm_line *cursor = ab->selection, *target;
188 struct browser_disasm_line *btarget, *bcursor;
189 unsigned int from, to;
191 if (!cursor || !cursor->ins || !ins__is_jump(cursor->ins) ||
192 !disasm_line__has_offset(cursor))
195 target = ab->offsets[cursor->ops.target.offset];
199 bcursor = disasm_line__browser(cursor);
200 btarget = disasm_line__browser(target);
202 if (annotate_browser__opts.hide_src_code) {
203 from = bcursor->idx_asm;
204 to = btarget->idx_asm;
206 from = (u64)bcursor->idx;
207 to = (u64)btarget->idx;
210 ui_browser__set_color(browser, HE_COLORSET_CODE);
211 __ui_browser__line_arrow(browser, 9 + ab->addr_width, from, to);
214 static unsigned int annotate_browser__refresh(struct ui_browser *browser)
216 int ret = ui_browser__list_head_refresh(browser);
218 if (annotate_browser__opts.jump_arrows)
219 annotate_browser__draw_current_jump(browser);
221 ui_browser__set_color(browser, HE_COLORSET_NORMAL);
222 __ui_browser__vline(browser, 7, 0, browser->height - 1);
226 static double disasm_line__calc_percent(struct disasm_line *dl, struct symbol *sym, int evidx)
228 double percent = 0.0;
230 if (dl->offset != -1) {
231 int len = sym->end - sym->start;
232 unsigned int hits = 0;
233 struct annotation *notes = symbol__annotation(sym);
234 struct source_line *src_line = notes->src->lines;
235 struct sym_hist *h = annotation__histogram(notes, evidx);
236 s64 offset = dl->offset;
237 struct disasm_line *next;
239 next = disasm__get_next_ip_line(¬es->src->source, dl);
240 while (offset < (s64)len &&
241 (next == NULL || offset < next->offset)) {
243 percent += src_line[offset].percent;
245 hits += h->addr[offset];
250 * If the percentage wasn't already calculated in
251 * symbol__get_source_line, do it now:
253 if (src_line == NULL && h->sum)
254 percent = 100.0 * hits / h->sum;
260 static void disasm_rb_tree__insert(struct rb_root *root, struct browser_disasm_line *bdl)
262 struct rb_node **p = &root->rb_node;
263 struct rb_node *parent = NULL;
264 struct browser_disasm_line *l;
268 l = rb_entry(parent, struct browser_disasm_line, rb_node);
269 if (bdl->percent < l->percent)
274 rb_link_node(&bdl->rb_node, parent, p);
275 rb_insert_color(&bdl->rb_node, root);
278 static void annotate_browser__set_top(struct annotate_browser *browser,
279 struct disasm_line *pos, u32 idx)
283 ui_browser__refresh_dimensions(&browser->b);
284 back = browser->b.height / 2;
285 browser->b.top_idx = browser->b.index = idx;
287 while (browser->b.top_idx != 0 && back != 0) {
288 pos = list_entry(pos->node.prev, struct disasm_line, node);
290 if (disasm_line__filter(&browser->b, &pos->node))
293 --browser->b.top_idx;
297 browser->b.top = pos;
298 browser->b.navkeypressed = true;
301 static void annotate_browser__set_rb_top(struct annotate_browser *browser,
304 struct browser_disasm_line *bpos;
305 struct disasm_line *pos;
308 bpos = rb_entry(nd, struct browser_disasm_line, rb_node);
309 pos = ((struct disasm_line *)bpos) - 1;
311 if (annotate_browser__opts.hide_src_code)
313 annotate_browser__set_top(browser, pos, idx);
314 browser->curr_hot = nd;
317 static void annotate_browser__calc_percent(struct annotate_browser *browser,
320 struct map_symbol *ms = browser->b.priv;
321 struct symbol *sym = ms->sym;
322 struct annotation *notes = symbol__annotation(sym);
323 struct disasm_line *pos;
325 browser->entries = RB_ROOT;
327 pthread_mutex_lock(¬es->lock);
329 list_for_each_entry(pos, ¬es->src->source, node) {
330 struct browser_disasm_line *bpos = disasm_line__browser(pos);
331 bpos->percent = disasm_line__calc_percent(pos, sym, evidx);
332 if (bpos->percent < 0.01) {
333 RB_CLEAR_NODE(&bpos->rb_node);
336 disasm_rb_tree__insert(&browser->entries, bpos);
338 pthread_mutex_unlock(¬es->lock);
340 browser->curr_hot = rb_last(&browser->entries);
343 static bool annotate_browser__toggle_source(struct annotate_browser *browser)
345 struct disasm_line *dl;
346 struct browser_disasm_line *bdl;
347 off_t offset = browser->b.index - browser->b.top_idx;
349 browser->b.seek(&browser->b, offset, SEEK_CUR);
350 dl = list_entry(browser->b.top, struct disasm_line, node);
351 bdl = disasm_line__browser(dl);
353 if (annotate_browser__opts.hide_src_code) {
354 if (bdl->idx_asm < offset)
357 browser->b.nr_entries = browser->nr_entries;
358 annotate_browser__opts.hide_src_code = false;
359 browser->b.seek(&browser->b, -offset, SEEK_CUR);
360 browser->b.top_idx = bdl->idx - offset;
361 browser->b.index = bdl->idx;
363 if (bdl->idx_asm < 0) {
364 ui_helpline__puts("Only available for assembly lines.");
365 browser->b.seek(&browser->b, -offset, SEEK_CUR);
369 if (bdl->idx_asm < offset)
370 offset = bdl->idx_asm;
372 browser->b.nr_entries = browser->nr_asm_entries;
373 annotate_browser__opts.hide_src_code = true;
374 browser->b.seek(&browser->b, -offset, SEEK_CUR);
375 browser->b.top_idx = bdl->idx_asm - offset;
376 browser->b.index = bdl->idx_asm;
382 static void annotate_browser__init_asm_mode(struct annotate_browser *browser)
384 ui_browser__reset_index(&browser->b);
385 browser->b.nr_entries = browser->nr_asm_entries;
388 static bool annotate_browser__callq(struct annotate_browser *browser,
389 int evidx, void (*timer)(void *arg),
390 void *arg, int delay_secs)
392 struct map_symbol *ms = browser->b.priv;
393 struct disasm_line *dl = browser->selection;
394 struct symbol *sym = ms->sym;
395 struct annotation *notes;
396 struct symbol *target;
399 if (!ins__is_call(dl->ins))
402 ip = ms->map->map_ip(ms->map, dl->ops.target.addr);
403 target = map__find_symbol(ms->map, ip, NULL);
404 if (target == NULL) {
405 ui_helpline__puts("The called function was not found.");
409 notes = symbol__annotation(target);
410 pthread_mutex_lock(¬es->lock);
412 if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
413 pthread_mutex_unlock(¬es->lock);
414 ui__warning("Not enough memory for annotating '%s' symbol!\n",
419 pthread_mutex_unlock(¬es->lock);
420 symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
421 ui_browser__show_title(&browser->b, sym->name);
426 struct disasm_line *annotate_browser__find_offset(struct annotate_browser *browser,
427 s64 offset, 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;
435 list_for_each_entry(pos, ¬es->src->source, node) {
436 if (pos->offset == offset)
438 if (!disasm_line__filter(&browser->b, &pos->node))
445 static bool annotate_browser__jump(struct annotate_browser *browser)
447 struct disasm_line *dl = browser->selection;
450 if (!ins__is_jump(dl->ins))
453 dl = annotate_browser__find_offset(browser, dl->ops.target.offset, &idx);
455 ui_helpline__puts("Invallid jump offset");
459 annotate_browser__set_top(browser, dl, idx);
465 struct disasm_line *annotate_browser__find_string(struct annotate_browser *browser,
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(pos, ¬es->src->source, node) {
475 if (disasm_line__filter(&browser->b, &pos->node))
480 if (pos->line && strstr(pos->line, s) != NULL)
487 static bool __annotate_browser__search(struct annotate_browser *browser)
489 struct disasm_line *dl;
492 dl = annotate_browser__find_string(browser, browser->search_bf, &idx);
494 ui_helpline__puts("String not found!");
498 annotate_browser__set_top(browser, dl, idx);
499 browser->searching_backwards = false;
504 struct disasm_line *annotate_browser__find_string_reverse(struct annotate_browser *browser,
507 struct map_symbol *ms = browser->b.priv;
508 struct symbol *sym = ms->sym;
509 struct annotation *notes = symbol__annotation(sym);
510 struct disasm_line *pos = browser->selection;
512 *idx = browser->b.index;
513 list_for_each_entry_continue_reverse(pos, ¬es->src->source, node) {
514 if (disasm_line__filter(&browser->b, &pos->node))
519 if (pos->line && strstr(pos->line, s) != NULL)
526 static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
528 struct disasm_line *dl;
531 dl = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
533 ui_helpline__puts("String not found!");
537 annotate_browser__set_top(browser, dl, idx);
538 browser->searching_backwards = true;
542 static bool annotate_browser__search_window(struct annotate_browser *browser,
545 if (ui_browser__input_window("Search", "String: ", browser->search_bf,
546 "ENTER: OK, ESC: Cancel",
547 delay_secs * 2) != K_ENTER ||
548 !*browser->search_bf)
554 static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs)
556 if (annotate_browser__search_window(browser, delay_secs))
557 return __annotate_browser__search(browser);
562 static bool annotate_browser__continue_search(struct annotate_browser *browser,
565 if (!*browser->search_bf)
566 return annotate_browser__search(browser, delay_secs);
568 return __annotate_browser__search(browser);
571 static bool annotate_browser__search_reverse(struct annotate_browser *browser,
574 if (annotate_browser__search_window(browser, delay_secs))
575 return __annotate_browser__search_reverse(browser);
581 bool annotate_browser__continue_search_reverse(struct annotate_browser *browser,
584 if (!*browser->search_bf)
585 return annotate_browser__search_reverse(browser, delay_secs);
587 return __annotate_browser__search_reverse(browser);
590 static void annotate_browser__update_addr_width(struct annotate_browser *browser)
592 if (annotate_browser__opts.use_offset)
593 browser->target_width = browser->min_addr_width;
595 browser->target_width = browser->max_addr_width;
597 browser->addr_width = browser->target_width;
599 if (annotate_browser__opts.show_nr_jumps)
600 browser->addr_width += browser->jumps_width + 1;
603 static int annotate_browser__run(struct annotate_browser *browser, int evidx,
604 void(*timer)(void *arg),
605 void *arg, int delay_secs)
607 struct rb_node *nd = NULL;
608 struct map_symbol *ms = browser->b.priv;
609 struct symbol *sym = ms->sym;
610 const char *help = "Press 'h' for help on key bindings";
613 if (ui_browser__show(&browser->b, sym->name, help) < 0)
616 annotate_browser__calc_percent(browser, evidx);
618 if (browser->curr_hot) {
619 annotate_browser__set_rb_top(browser, browser->curr_hot);
620 browser->b.navkeypressed = false;
623 nd = browser->curr_hot;
626 key = ui_browser__run(&browser->b, delay_secs);
628 if (delay_secs != 0) {
629 annotate_browser__calc_percent(browser, evidx);
631 * Current line focus got out of the list of most active
632 * lines, NULL it so that if TAB|UNTAB is pressed, we
633 * move to curr_hot (current hottest line).
635 if (nd != NULL && RB_EMPTY_NODE(nd))
645 symbol__annotate_decay_histogram(sym, evidx);
651 nd = rb_last(&browser->entries);
653 nd = browser->curr_hot;
659 nd = rb_first(&browser->entries);
661 nd = browser->curr_hot;
665 ui_browser__help_window(&browser->b,
667 "PGDN/SPACE Navigate\n"
668 "q/ESC/CTRL+C Exit\n\n"
671 "h Cycle thru hottest instructions\n"
672 "j Toggle showing jump to target arrows\n"
673 "J Toggle showing number of jump sources on targets\n"
674 "n Search next string\n"
675 "o Toggle disassembler output/simplified view\n"
676 "s Toggle source code view\n"
678 "? Search previous string\n");
681 nd = browser->curr_hot;
684 if (annotate_browser__toggle_source(browser))
685 ui_helpline__puts(help);
688 annotate_browser__opts.use_offset = !annotate_browser__opts.use_offset;
689 annotate_browser__update_addr_width(browser);
692 annotate_browser__opts.jump_arrows = !annotate_browser__opts.jump_arrows;
695 annotate_browser__opts.show_nr_jumps = !annotate_browser__opts.show_nr_jumps;
696 annotate_browser__update_addr_width(browser);
699 if (annotate_browser__search(browser, delay_secs)) {
701 ui_helpline__puts(help);
705 if (browser->searching_backwards ?
706 annotate_browser__continue_search_reverse(browser, delay_secs) :
707 annotate_browser__continue_search(browser, delay_secs))
711 if (annotate_browser__search_reverse(browser, delay_secs))
717 ui_helpline__fpush("%d: nr_ent=%d, height=%d, idx=%d, top_idx=%d, nr_asm_entries=%d",
718 seq++, browser->b.nr_entries,
722 browser->nr_asm_entries);
727 if (browser->selection == NULL)
728 ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
729 else if (browser->selection->offset == -1)
730 ui_helpline__puts("Actions are only available for assembly lines.");
731 else if (!browser->selection->ins) {
732 if (strcmp(browser->selection->name, "retq"))
735 } else if (!(annotate_browser__jump(browser) ||
736 annotate_browser__callq(browser, evidx, timer, arg, delay_secs))) {
738 ui_helpline__puts("Actions are only available for 'callq', 'retq' & jump instructions.");
751 annotate_browser__set_rb_top(browser, nd);
754 ui_browser__hide(&browser->b);
758 int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
759 void(*timer)(void *arg), void *arg, int delay_secs)
761 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
762 timer, arg, delay_secs);
765 static void annotate_browser__mark_jump_targets(struct annotate_browser *browser,
770 for (offset = 0; offset < size; ++offset) {
771 struct disasm_line *dl = browser->offsets[offset], *dlt;
772 struct browser_disasm_line *bdlt;
774 if (!dl || !dl->ins || !ins__is_jump(dl->ins) ||
775 !disasm_line__has_offset(dl))
778 if (dl->ops.target.offset >= size) {
779 ui__error("jump to after symbol!\n"
780 "size: %zx, jump target: %" PRIx64,
781 size, dl->ops.target.offset);
785 dlt = browser->offsets[dl->ops.target.offset];
787 * FIXME: Oops, no jump target? Buggy disassembler? Or do we
788 * have to adjust to the previous offset?
793 bdlt = disasm_line__browser(dlt);
794 if (++bdlt->jump_sources > browser->max_jump_sources)
795 browser->max_jump_sources = bdlt->jump_sources;
802 static inline int width_jumps(int n)
811 int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
812 void(*timer)(void *arg), void *arg,
815 struct disasm_line *pos, *n;
816 struct annotation *notes;
817 const size_t size = symbol__size(sym);
818 struct map_symbol ms = {
822 struct annotate_browser browser = {
824 .refresh = annotate_browser__refresh,
825 .seek = ui_browser__list_head_seek,
826 .write = annotate_browser__write,
827 .filter = disasm_line__filter,
829 .use_navkeypressed = true,
837 if (map->dso->annotate_warned)
840 browser.offsets = zalloc(size * sizeof(struct disasm_line *));
841 if (browser.offsets == NULL) {
842 ui__error("Not enough memory!");
846 if (symbol__annotate(sym, map, sizeof(struct browser_disasm_line)) < 0) {
847 ui__error("%s", ui_helpline__last_msg);
848 goto out_free_offsets;
851 ui_helpline__push("Press <- or ESC to exit");
853 notes = symbol__annotation(sym);
854 browser.start = map__rip_2objdump(map, sym->start);
856 list_for_each_entry(pos, ¬es->src->source, node) {
857 struct browser_disasm_line *bpos;
858 size_t line_len = strlen(pos->line);
860 if (browser.b.width < line_len)
861 browser.b.width = line_len;
862 bpos = disasm_line__browser(pos);
863 bpos->idx = browser.nr_entries++;
864 if (pos->offset != -1) {
865 bpos->idx_asm = browser.nr_asm_entries++;
867 * FIXME: short term bandaid to cope with assembly
868 * routines that comes with labels in the same column
869 * as the address in objdump, sigh.
871 * E.g. copy_user_generic_unrolled
873 if (pos->offset < (s64)size)
874 browser.offsets[pos->offset] = pos;
879 annotate_browser__mark_jump_targets(&browser, size);
881 browser.addr_width = browser.target_width = browser.min_addr_width = hex_width(size);
882 browser.max_addr_width = hex_width(sym->end);
883 browser.jumps_width = width_jumps(browser.max_jump_sources);
884 browser.b.nr_entries = browser.nr_entries;
885 browser.b.entries = ¬es->src->source,
886 browser.b.width += 18; /* Percentage */
888 if (annotate_browser__opts.hide_src_code)
889 annotate_browser__init_asm_mode(&browser);
891 annotate_browser__update_addr_width(&browser);
893 ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
894 list_for_each_entry_safe(pos, n, ¬es->src->source, node) {
895 list_del(&pos->node);
896 disasm_line__free(pos);
900 free(browser.offsets);
904 #define ANNOTATE_CFG(n) \
905 { .name = #n, .value = &annotate_browser__opts.n, }
908 * Keep the entries sorted, they are bsearch'ed
910 static struct annotate__config {
913 } annotate__configs[] = {
914 ANNOTATE_CFG(hide_src_code),
915 ANNOTATE_CFG(jump_arrows),
916 ANNOTATE_CFG(show_nr_jumps),
917 ANNOTATE_CFG(use_offset),
922 static int annotate_config__cmp(const void *name, const void *cfgp)
924 const struct annotate__config *cfg = cfgp;
926 return strcmp(name, cfg->name);
929 static int annotate__config(const char *var, const char *value, void *data __used)
931 struct annotate__config *cfg;
934 if (prefixcmp(var, "annotate.") != 0)
938 cfg = bsearch(name, annotate__configs, ARRAY_SIZE(annotate__configs),
939 sizeof(struct annotate__config), annotate_config__cmp);
944 *cfg->value = perf_config_bool(name, value);
948 void annotate_browser__init(void)
950 perf_config(annotate__config, NULL);