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 annotate_browser {
16 struct rb_root entries;
17 struct rb_node *curr_hot;
18 struct disasm_line *selection;
24 bool searching_backwards;
28 struct disasm_line_rb_node {
29 struct rb_node rb_node;
35 static inline struct disasm_line_rb_node *disasm_line__rb(struct disasm_line *dl)
37 return (struct disasm_line_rb_node *)(dl + 1);
40 static bool disasm_line__filter(struct ui_browser *browser, void *entry)
42 struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
44 if (ab->hide_src_code) {
45 struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
46 return dl->offset == -1;
52 static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
54 struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
55 struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
56 bool current_entry = ui_browser__is_current_entry(self, row);
57 bool change_color = (!ab->hide_src_code &&
58 (!current_entry || (self->use_navkeypressed &&
59 !self->navkeypressed)));
60 int width = self->width;
62 if (dl->offset != -1) {
63 struct disasm_line_rb_node *dlrb = disasm_line__rb(dl);
64 ui_browser__set_percent_color(self, dlrb->percent, current_entry);
65 slsmg_printf(" %7.2f ", dlrb->percent);
67 ui_browser__set_percent_color(self, 0, current_entry);
68 slsmg_write_nstring(" ", 9);
71 SLsmg_write_char(':');
72 slsmg_write_nstring(" ", 8);
74 /* The scroll bar isn't being used */
75 if (!self->navkeypressed)
78 if (dl->offset != -1 && change_color)
79 ui_browser__set_color(self, HE_COLORSET_CODE);
82 slsmg_write_nstring(" ", width - 18);
83 else if (dl->offset == -1)
84 slsmg_write_nstring(dl->line, width - 18);
87 u64 addr = dl->offset;
88 int printed, color = -1;
93 printed = scnprintf(bf, sizeof(bf), " %" PRIx64 ":", addr);
95 color = ui_browser__set_color(self, HE_COLORSET_ADDR);
96 slsmg_write_nstring(bf, printed);
98 ui_browser__set_color(self, color);
99 slsmg_write_nstring(dl->line, width - 18 - printed);
106 static double disasm_line__calc_percent(struct disasm_line *dl, struct symbol *sym, int evidx)
108 double percent = 0.0;
110 if (dl->offset != -1) {
111 int len = sym->end - sym->start;
112 unsigned int hits = 0;
113 struct annotation *notes = symbol__annotation(sym);
114 struct source_line *src_line = notes->src->lines;
115 struct sym_hist *h = annotation__histogram(notes, evidx);
116 s64 offset = dl->offset;
117 struct disasm_line *next;
119 next = disasm__get_next_ip_line(¬es->src->source, dl);
120 while (offset < (s64)len &&
121 (next == NULL || offset < next->offset)) {
123 percent += src_line[offset].percent;
125 hits += h->addr[offset];
130 * If the percentage wasn't already calculated in
131 * symbol__get_source_line, do it now:
133 if (src_line == NULL && h->sum)
134 percent = 100.0 * hits / h->sum;
140 static void disasm_rb_tree__insert(struct rb_root *root, struct disasm_line_rb_node *dlrb)
142 struct rb_node **p = &root->rb_node;
143 struct rb_node *parent = NULL;
144 struct disasm_line_rb_node *l;
148 l = rb_entry(parent, struct disasm_line_rb_node, rb_node);
149 if (dlrb->percent < l->percent)
154 rb_link_node(&dlrb->rb_node, parent, p);
155 rb_insert_color(&dlrb->rb_node, root);
158 static void annotate_browser__set_top(struct annotate_browser *self,
159 struct disasm_line *pos, u32 idx)
163 ui_browser__refresh_dimensions(&self->b);
164 back = self->b.height / 2;
165 self->b.top_idx = self->b.index = idx;
167 while (self->b.top_idx != 0 && back != 0) {
168 pos = list_entry(pos->node.prev, struct disasm_line, node);
170 if (disasm_line__filter(&self->b, &pos->node))
178 self->b.navkeypressed = true;
181 static void annotate_browser__set_rb_top(struct annotate_browser *browser,
184 struct disasm_line_rb_node *rbpos;
185 struct disasm_line *pos;
187 rbpos = rb_entry(nd, struct disasm_line_rb_node, rb_node);
188 pos = ((struct disasm_line *)rbpos) - 1;
189 annotate_browser__set_top(browser, pos, rbpos->idx);
190 browser->curr_hot = nd;
193 static void annotate_browser__calc_percent(struct annotate_browser *browser,
196 struct map_symbol *ms = browser->b.priv;
197 struct symbol *sym = ms->sym;
198 struct annotation *notes = symbol__annotation(sym);
199 struct disasm_line *pos;
201 browser->entries = RB_ROOT;
203 pthread_mutex_lock(¬es->lock);
205 list_for_each_entry(pos, ¬es->src->source, node) {
206 struct disasm_line_rb_node *rbpos = disasm_line__rb(pos);
207 rbpos->percent = disasm_line__calc_percent(pos, sym, evidx);
208 if (rbpos->percent < 0.01) {
209 RB_CLEAR_NODE(&rbpos->rb_node);
212 disasm_rb_tree__insert(&browser->entries, rbpos);
214 pthread_mutex_unlock(¬es->lock);
216 browser->curr_hot = rb_last(&browser->entries);
219 static bool annotate_browser__toggle_source(struct annotate_browser *browser)
221 struct disasm_line *dl;
222 struct disasm_line_rb_node *dlrb;
223 off_t offset = browser->b.index - browser->b.top_idx;
225 browser->b.seek(&browser->b, offset, SEEK_CUR);
226 dl = list_entry(browser->b.top, struct disasm_line, node);
227 dlrb = disasm_line__rb(dl);
229 if (browser->hide_src_code) {
230 if (dlrb->idx_asm < offset)
233 browser->b.nr_entries = browser->nr_entries;
234 browser->hide_src_code = false;
235 browser->b.seek(&browser->b, -offset, SEEK_CUR);
236 browser->b.top_idx = dlrb->idx - offset;
237 browser->b.index = dlrb->idx;
239 if (dlrb->idx_asm < 0) {
240 ui_helpline__puts("Only available for assembly lines.");
241 browser->b.seek(&browser->b, -offset, SEEK_CUR);
245 if (dlrb->idx_asm < offset)
246 offset = dlrb->idx_asm;
248 browser->b.nr_entries = browser->nr_asm_entries;
249 browser->hide_src_code = true;
250 browser->b.seek(&browser->b, -offset, SEEK_CUR);
251 browser->b.top_idx = dlrb->idx_asm - offset;
252 browser->b.index = dlrb->idx_asm;
258 static bool annotate_browser__callq(struct annotate_browser *browser,
259 int evidx, void (*timer)(void *arg),
260 void *arg, int delay_secs)
262 struct map_symbol *ms = browser->b.priv;
263 struct symbol *sym = ms->sym;
264 struct annotation *notes;
265 struct symbol *target;
266 char *s = strstr(browser->selection->line, "callq ");
274 ui_helpline__puts("Invallid callq instruction.");
278 ip = strtoull(s, NULL, 16);
279 ip = ms->map->map_ip(ms->map, ip);
280 target = map__find_symbol(ms->map, ip, NULL);
281 if (target == NULL) {
282 ui_helpline__puts("The called function was not found.");
286 notes = symbol__annotation(target);
287 pthread_mutex_lock(¬es->lock);
289 if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
290 pthread_mutex_unlock(¬es->lock);
291 ui__warning("Not enough memory for annotating '%s' symbol!\n",
296 pthread_mutex_unlock(¬es->lock);
297 symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
298 ui_browser__show_title(&browser->b, sym->name);
303 struct disasm_line *annotate_browser__find_offset(struct annotate_browser *browser,
304 s64 offset, s64 *idx)
306 struct map_symbol *ms = browser->b.priv;
307 struct symbol *sym = ms->sym;
308 struct annotation *notes = symbol__annotation(sym);
309 struct disasm_line *pos;
312 list_for_each_entry(pos, ¬es->src->source, node) {
313 if (pos->offset == offset)
315 if (!disasm_line__filter(&browser->b, &pos->node))
322 static bool annotate_browser__jump(struct annotate_browser *browser)
324 const char *jumps[] = { "je ", "jne ", "ja ", "jmpq ", "js ", "jmp ", NULL };
325 struct disasm_line *dl;
331 s = strstr(browser->selection->line, jumps[i++]);
341 ui_helpline__puts("Invallid jump instruction.");
345 offset = strtoll(s, NULL, 16);
346 dl = annotate_browser__find_offset(browser, offset, &idx);
348 ui_helpline__puts("Invallid jump offset");
352 annotate_browser__set_top(browser, dl, idx);
358 struct disasm_line *annotate_browser__find_string(struct annotate_browser *browser,
361 struct map_symbol *ms = browser->b.priv;
362 struct symbol *sym = ms->sym;
363 struct annotation *notes = symbol__annotation(sym);
364 struct disasm_line *pos = browser->selection;
366 *idx = browser->b.index;
367 list_for_each_entry_continue(pos, ¬es->src->source, node) {
368 if (disasm_line__filter(&browser->b, &pos->node))
373 if (pos->line && strstr(pos->line, s) != NULL)
380 static bool __annotate_browser__search(struct annotate_browser *browser)
382 struct disasm_line *dl;
385 dl = annotate_browser__find_string(browser, browser->search_bf, &idx);
387 ui_helpline__puts("String not found!");
391 annotate_browser__set_top(browser, dl, idx);
392 browser->searching_backwards = false;
397 struct disasm_line *annotate_browser__find_string_reverse(struct annotate_browser *browser,
400 struct map_symbol *ms = browser->b.priv;
401 struct symbol *sym = ms->sym;
402 struct annotation *notes = symbol__annotation(sym);
403 struct disasm_line *pos = browser->selection;
405 *idx = browser->b.index;
406 list_for_each_entry_continue_reverse(pos, ¬es->src->source, node) {
407 if (disasm_line__filter(&browser->b, &pos->node))
412 if (pos->line && strstr(pos->line, s) != NULL)
419 static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
421 struct disasm_line *dl;
424 dl = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
426 ui_helpline__puts("String not found!");
430 annotate_browser__set_top(browser, dl, idx);
431 browser->searching_backwards = true;
435 static bool annotate_browser__search_window(struct annotate_browser *browser,
438 if (ui_browser__input_window("Search", "String: ", browser->search_bf,
439 "ENTER: OK, ESC: Cancel",
440 delay_secs * 2) != K_ENTER ||
441 !*browser->search_bf)
447 static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs)
449 if (annotate_browser__search_window(browser, delay_secs))
450 return __annotate_browser__search(browser);
455 static bool annotate_browser__continue_search(struct annotate_browser *browser,
458 if (!*browser->search_bf)
459 return annotate_browser__search(browser, delay_secs);
461 return __annotate_browser__search(browser);
464 static bool annotate_browser__search_reverse(struct annotate_browser *browser,
467 if (annotate_browser__search_window(browser, delay_secs))
468 return __annotate_browser__search_reverse(browser);
474 bool annotate_browser__continue_search_reverse(struct annotate_browser *browser,
477 if (!*browser->search_bf)
478 return annotate_browser__search_reverse(browser, delay_secs);
480 return __annotate_browser__search_reverse(browser);
483 static int annotate_browser__run(struct annotate_browser *self, int evidx,
484 void(*timer)(void *arg),
485 void *arg, int delay_secs)
487 struct rb_node *nd = NULL;
488 struct map_symbol *ms = self->b.priv;
489 struct symbol *sym = ms->sym;
490 const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
491 "H: Go to hottest line, ->/ENTER: Line action, "
492 "O: Toggle offset view, "
493 "S: Toggle source code view";
496 if (ui_browser__show(&self->b, sym->name, help) < 0)
499 annotate_browser__calc_percent(self, evidx);
501 if (self->curr_hot) {
502 annotate_browser__set_rb_top(self, self->curr_hot);
503 self->b.navkeypressed = false;
509 key = ui_browser__run(&self->b, delay_secs);
511 if (delay_secs != 0) {
512 annotate_browser__calc_percent(self, evidx);
514 * Current line focus got out of the list of most active
515 * lines, NULL it so that if TAB|UNTAB is pressed, we
516 * move to curr_hot (current hottest line).
518 if (nd != NULL && RB_EMPTY_NODE(nd))
528 symbol__annotate_decay_histogram(sym, evidx);
534 nd = rb_last(&self->entries);
542 nd = rb_first(&self->entries);
552 if (annotate_browser__toggle_source(self))
553 ui_helpline__puts(help);
557 self->use_offset = !self->use_offset;
560 if (annotate_browser__search(self, delay_secs)) {
562 ui_helpline__puts(help);
566 if (self->searching_backwards ?
567 annotate_browser__continue_search_reverse(self, delay_secs) :
568 annotate_browser__continue_search(self, delay_secs))
572 if (annotate_browser__search_reverse(self, delay_secs))
577 if (self->selection == NULL)
578 ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
579 else if (self->selection->offset == -1)
580 ui_helpline__puts("Actions are only available for assembly lines.");
581 else if (!(annotate_browser__jump(self) ||
582 annotate_browser__callq(self, evidx, timer, arg, delay_secs)))
583 ui_helpline__puts("Actions are only available for the 'callq' and jump instructions.");
595 annotate_browser__set_rb_top(self, nd);
598 ui_browser__hide(&self->b);
602 int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
603 void(*timer)(void *arg), void *arg, int delay_secs)
605 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
606 timer, arg, delay_secs);
609 int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
610 void(*timer)(void *arg), void *arg,
613 struct disasm_line *pos, *n;
614 struct annotation *notes;
615 struct map_symbol ms = {
619 struct annotate_browser browser = {
621 .refresh = ui_browser__list_head_refresh,
622 .seek = ui_browser__list_head_seek,
623 .write = annotate_browser__write,
624 .filter = disasm_line__filter,
626 .use_navkeypressed = true,
634 if (map->dso->annotate_warned)
637 if (symbol__annotate(sym, map, sizeof(struct disasm_line_rb_node)) < 0) {
638 ui__error("%s", ui_helpline__last_msg);
642 ui_helpline__push("Press <- or ESC to exit");
644 notes = symbol__annotation(sym);
645 browser.start = map__rip_2objdump(map, sym->start);
647 list_for_each_entry(pos, ¬es->src->source, node) {
648 struct disasm_line_rb_node *rbpos;
649 size_t line_len = strlen(pos->line);
651 if (browser.b.width < line_len)
652 browser.b.width = line_len;
653 rbpos = disasm_line__rb(pos);
654 rbpos->idx = browser.nr_entries++;
655 if (pos->offset != -1)
656 rbpos->idx_asm = browser.nr_asm_entries++;
661 browser.b.nr_entries = browser.nr_entries;
662 browser.b.entries = ¬es->src->source,
663 browser.b.width += 18; /* Percentage */
664 ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
665 list_for_each_entry_safe(pos, n, ¬es->src->source, node) {
666 list_del(&pos->node);
667 disasm_line__free(pos);