1 #include "../browser.h"
2 #include "../helpline.h"
3 #include "../libslang.h"
4 #include "../../annotate.h"
5 #include "../../hist.h"
6 #include "../../sort.h"
7 #include "../../symbol.h"
11 static void ui__error_window(const char *fmt, ...)
16 newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
20 struct annotate_browser {
22 struct rb_root entries;
23 struct rb_node *curr_hot;
24 struct objdump_line *selection;
30 struct objdump_line_rb_node {
31 struct rb_node rb_node;
38 struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
40 return (struct objdump_line_rb_node *)(self + 1);
43 static bool objdump_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 objdump_line *ol = list_entry(entry, struct objdump_line, node);
49 return ol->offset == -1;
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 objdump_line *ol = list_entry(entry, struct objdump_line, node);
59 bool current_entry = ui_browser__is_current_entry(self, row);
60 int width = self->width;
62 if (ol->offset != -1) {
63 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
64 ui_browser__set_percent_color(self, olrb->percent, current_entry);
65 slsmg_printf(" %7.2f ", olrb->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)
79 slsmg_write_nstring(" ", width - 18);
81 slsmg_write_nstring(ol->line, width - 18);
84 ui_browser__set_color(self, HE_COLORSET_CODE);
89 static double objdump_line__calc_percent(struct objdump_line *self,
90 struct symbol *sym, int evidx)
94 if (self->offset != -1) {
95 int len = sym->end - sym->start;
96 unsigned int hits = 0;
97 struct annotation *notes = symbol__annotation(sym);
98 struct source_line *src_line = notes->src->lines;
99 struct sym_hist *h = annotation__histogram(notes, evidx);
100 s64 offset = self->offset;
101 struct objdump_line *next;
103 next = objdump__get_next_ip_line(¬es->src->source, self);
104 while (offset < (s64)len &&
105 (next == NULL || offset < next->offset)) {
107 percent += src_line[offset].percent;
109 hits += h->addr[offset];
114 * If the percentage wasn't already calculated in
115 * symbol__get_source_line, do it now:
117 if (src_line == NULL && h->sum)
118 percent = 100.0 * hits / h->sum;
124 static void objdump__insert_line(struct rb_root *self,
125 struct objdump_line_rb_node *line)
127 struct rb_node **p = &self->rb_node;
128 struct rb_node *parent = NULL;
129 struct objdump_line_rb_node *l;
133 l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
134 if (line->percent < l->percent)
139 rb_link_node(&line->rb_node, parent, p);
140 rb_insert_color(&line->rb_node, self);
143 static void annotate_browser__set_top(struct annotate_browser *self,
146 struct objdump_line_rb_node *rbpos;
147 struct objdump_line *pos;
150 ui_browser__refresh_dimensions(&self->b);
151 back = self->b.height / 2;
152 rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
153 pos = ((struct objdump_line *)rbpos) - 1;
154 self->b.top_idx = self->b.index = rbpos->idx;
156 while (self->b.top_idx != 0 && back != 0) {
157 pos = list_entry(pos->node.prev, struct objdump_line, node);
167 static void annotate_browser__calc_percent(struct annotate_browser *browser,
170 struct map_symbol *ms = browser->b.priv;
171 struct symbol *sym = ms->sym;
172 struct annotation *notes = symbol__annotation(sym);
173 struct objdump_line *pos;
175 browser->entries = RB_ROOT;
177 pthread_mutex_lock(¬es->lock);
179 list_for_each_entry(pos, ¬es->src->source, node) {
180 struct objdump_line_rb_node *rbpos = objdump_line__rb(pos);
181 rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
182 if (rbpos->percent < 0.01) {
183 RB_CLEAR_NODE(&rbpos->rb_node);
186 objdump__insert_line(&browser->entries, rbpos);
188 pthread_mutex_unlock(¬es->lock);
190 browser->curr_hot = rb_last(&browser->entries);
193 static bool annotate_browser__toggle_source(struct annotate_browser *browser)
195 struct objdump_line *ol;
196 struct objdump_line_rb_node *olrb;
197 off_t offset = browser->b.index - browser->b.top_idx;
199 browser->b.seek(&browser->b, offset, SEEK_CUR);
200 ol = list_entry(browser->b.top, struct objdump_line, node);
201 olrb = objdump_line__rb(ol);
203 if (browser->hide_src_code) {
204 if (olrb->idx_asm < offset)
207 browser->b.nr_entries = browser->nr_entries;
208 browser->hide_src_code = false;
209 browser->b.seek(&browser->b, -offset, SEEK_CUR);
210 browser->b.top_idx = olrb->idx - offset;
211 browser->b.index = olrb->idx;
213 if (olrb->idx_asm < 0) {
214 ui_helpline__puts("Only available for assembly lines.");
215 browser->b.seek(&browser->b, -offset, SEEK_CUR);
219 if (olrb->idx_asm < offset)
220 offset = olrb->idx_asm;
222 browser->b.nr_entries = browser->nr_asm_entries;
223 browser->hide_src_code = true;
224 browser->b.seek(&browser->b, -offset, SEEK_CUR);
225 browser->b.top_idx = olrb->idx_asm - offset;
226 browser->b.index = olrb->idx_asm;
232 static int annotate_browser__run(struct annotate_browser *self, int evidx,
233 int nr_events, void(*timer)(void *arg),
234 void *arg, int delay_secs)
236 struct rb_node *nd = NULL;
237 struct map_symbol *ms = self->b.priv;
238 struct symbol *sym = ms->sym;
239 const char *help = "<-, ESC: exit, TAB/shift+TAB: cycle hottest lines, "
240 "H: Hottest, -> Line action, S -> Toggle source "
244 if (ui_browser__show(&self->b, sym->name, help) < 0)
247 annotate_browser__calc_percent(self, evidx);
250 annotate_browser__set_top(self, self->curr_hot);
255 key = ui_browser__run(&self->b, delay_secs);
257 if (delay_secs != 0) {
258 annotate_browser__calc_percent(self, evidx);
260 * Current line focus got out of the list of most active
261 * lines, NULL it so that if TAB|UNTAB is pressed, we
262 * move to curr_hot (current hottest line).
264 if (nd != NULL && RB_EMPTY_NODE(nd))
274 symbol__annotate_decay_histogram(sym, evidx);
280 nd = rb_last(&self->entries);
288 nd = rb_first(&self->entries);
296 if (annotate_browser__toggle_source(self))
297 ui_helpline__puts(help);
301 if (self->selection == NULL) {
302 ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
306 if (self->selection->offset == -1) {
307 ui_helpline__puts("Actions are only available for assembly lines.");
310 char *s = strstr(self->selection->line, "callq ");
311 struct annotation *notes;
312 struct symbol *target;
316 ui_helpline__puts("Actions are only available for the 'callq' instruction.");
322 ui_helpline__puts("Invallid callq instruction.");
326 ip = strtoull(s, NULL, 16);
327 ip = ms->map->map_ip(ms->map, ip);
328 target = map__find_symbol(ms->map, ip, NULL);
329 if (target == NULL) {
330 ui_helpline__puts("The called function was not found.");
334 notes = symbol__annotation(target);
335 pthread_mutex_lock(¬es->lock);
337 if (notes->src == NULL &&
338 symbol__alloc_hist(target, nr_events) < 0) {
339 pthread_mutex_unlock(¬es->lock);
340 ui__warning("Not enough memory for annotating '%s' symbol!\n",
345 pthread_mutex_unlock(¬es->lock);
346 symbol__tui_annotate(target, ms->map, evidx, nr_events,
347 timer, arg, delay_secs);
360 annotate_browser__set_top(self, nd);
363 ui_browser__hide(&self->b);
367 int hist_entry__tui_annotate(struct hist_entry *he, int evidx, int nr_events,
368 void(*timer)(void *arg), void *arg, int delay_secs)
370 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx, nr_events,
371 timer, arg, delay_secs);
374 int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
375 int nr_events, void(*timer)(void *arg), void *arg,
378 struct objdump_line *pos, *n;
379 struct annotation *notes;
380 struct map_symbol ms = {
384 struct annotate_browser browser = {
386 .refresh = ui_browser__list_head_refresh,
387 .seek = ui_browser__list_head_seek,
388 .write = annotate_browser__write,
389 .filter = objdump_line__filter,
391 .use_navkeypressed = true,
399 if (map->dso->annotate_warned)
402 if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
403 ui__error_window(ui_helpline__last_msg);
407 ui_helpline__push("Press <- or ESC to exit");
409 notes = symbol__annotation(sym);
411 list_for_each_entry(pos, ¬es->src->source, node) {
412 struct objdump_line_rb_node *rbpos;
413 size_t line_len = strlen(pos->line);
415 if (browser.b.width < line_len)
416 browser.b.width = line_len;
417 rbpos = objdump_line__rb(pos);
418 rbpos->idx = browser.nr_entries++;
419 if (pos->offset != -1)
420 rbpos->idx_asm = browser.nr_asm_entries++;
425 browser.b.nr_entries = browser.nr_entries;
426 browser.b.entries = ¬es->src->source,
427 browser.b.width += 18; /* Percentage */
428 ret = annotate_browser__run(&browser, evidx, nr_events,
429 timer, arg, delay_secs);
430 list_for_each_entry_safe(pos, n, ¬es->src->source, node) {
431 list_del(&pos->node);
432 objdump_line__free(pos);