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"
10 static void ui__error_window(const char *fmt, ...)
15 newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
19 struct annotate_browser {
21 struct rb_root entries;
22 struct rb_node *curr_hot;
23 struct objdump_line *selection;
29 struct objdump_line_rb_node {
30 struct rb_node rb_node;
37 struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
39 return (struct objdump_line_rb_node *)(self + 1);
42 static bool objdump_line__filter(struct ui_browser *browser, void *entry)
44 struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
46 if (ab->hide_src_code) {
47 struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
48 return ol->offset == -1;
54 static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
56 struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
57 struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
58 bool current_entry = ui_browser__is_current_entry(self, row);
59 int width = self->width;
61 if (ol->offset != -1) {
62 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
63 ui_browser__set_percent_color(self, olrb->percent, current_entry);
64 slsmg_printf(" %7.2f ", olrb->percent);
66 ui_browser__set_percent_color(self, 0, current_entry);
67 slsmg_write_nstring(" ", 9);
70 SLsmg_write_char(':');
71 slsmg_write_nstring(" ", 8);
73 /* The scroll bar isn't being used */
74 if (!self->navkeypressed)
78 slsmg_write_nstring(" ", width - 18);
80 slsmg_write_nstring(ol->line, width - 18);
83 ui_browser__set_color(self, HE_COLORSET_CODE);
88 static double objdump_line__calc_percent(struct objdump_line *self,
89 struct symbol *sym, int evidx)
93 if (self->offset != -1) {
94 int len = sym->end - sym->start;
95 unsigned int hits = 0;
96 struct annotation *notes = symbol__annotation(sym);
97 struct source_line *src_line = notes->src->lines;
98 struct sym_hist *h = annotation__histogram(notes, evidx);
99 s64 offset = self->offset;
100 struct objdump_line *next;
102 next = objdump__get_next_ip_line(¬es->src->source, self);
103 while (offset < (s64)len &&
104 (next == NULL || offset < next->offset)) {
106 percent += src_line[offset].percent;
108 hits += h->addr[offset];
113 * If the percentage wasn't already calculated in
114 * symbol__get_source_line, do it now:
116 if (src_line == NULL && h->sum)
117 percent = 100.0 * hits / h->sum;
123 static void objdump__insert_line(struct rb_root *self,
124 struct objdump_line_rb_node *line)
126 struct rb_node **p = &self->rb_node;
127 struct rb_node *parent = NULL;
128 struct objdump_line_rb_node *l;
132 l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
133 if (line->percent < l->percent)
138 rb_link_node(&line->rb_node, parent, p);
139 rb_insert_color(&line->rb_node, self);
142 static void annotate_browser__set_top(struct annotate_browser *self,
145 struct objdump_line_rb_node *rbpos;
146 struct objdump_line *pos;
149 ui_browser__refresh_dimensions(&self->b);
150 back = self->b.height / 2;
151 rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
152 pos = ((struct objdump_line *)rbpos) - 1;
153 self->b.top_idx = self->b.index = rbpos->idx;
155 while (self->b.top_idx != 0 && back != 0) {
156 pos = list_entry(pos->node.prev, struct objdump_line, node);
166 static void annotate_browser__calc_percent(struct annotate_browser *browser,
169 struct map_symbol *ms = browser->b.priv;
170 struct symbol *sym = ms->sym;
171 struct annotation *notes = symbol__annotation(sym);
172 struct objdump_line *pos;
174 browser->entries = RB_ROOT;
176 pthread_mutex_lock(¬es->lock);
178 list_for_each_entry(pos, ¬es->src->source, node) {
179 struct objdump_line_rb_node *rbpos = objdump_line__rb(pos);
180 rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
181 if (rbpos->percent < 0.01) {
182 RB_CLEAR_NODE(&rbpos->rb_node);
185 objdump__insert_line(&browser->entries, rbpos);
187 pthread_mutex_unlock(¬es->lock);
189 browser->curr_hot = rb_last(&browser->entries);
192 static bool annotate_browser__toggle_source(struct annotate_browser *browser)
194 struct objdump_line *ol;
195 struct objdump_line_rb_node *olrb;
196 off_t offset = browser->b.index - browser->b.top_idx;
198 browser->b.seek(&browser->b, offset, SEEK_CUR);
199 ol = list_entry(browser->b.top, struct objdump_line, node);
200 olrb = objdump_line__rb(ol);
202 if (browser->hide_src_code) {
203 if (olrb->idx_asm < offset)
206 browser->b.nr_entries = browser->nr_entries;
207 browser->hide_src_code = false;
208 browser->b.seek(&browser->b, -offset, SEEK_CUR);
209 browser->b.top_idx = olrb->idx - offset;
210 browser->b.index = olrb->idx;
212 if (olrb->idx_asm < 0) {
213 ui_helpline__puts("Only available for assembly lines.");
214 browser->b.seek(&browser->b, -offset, SEEK_CUR);
218 if (olrb->idx_asm < offset)
219 offset = olrb->idx_asm;
221 browser->b.nr_entries = browser->nr_asm_entries;
222 browser->hide_src_code = true;
223 browser->b.seek(&browser->b, -offset, SEEK_CUR);
224 browser->b.top_idx = olrb->idx_asm - offset;
225 browser->b.index = olrb->idx_asm;
231 static int annotate_browser__run(struct annotate_browser *self, int evidx,
232 int nr_events, void(*timer)(void *arg),
233 void *arg, int delay_secs)
235 struct rb_node *nd = NULL;
236 struct map_symbol *ms = self->b.priv;
237 struct symbol *sym = ms->sym;
238 const char *help = "<-, ESC: exit, TAB/shift+TAB: cycle hottest lines, "
239 "H: Hottest, -> Line action, S -> Toggle source "
243 if (ui_browser__show(&self->b, sym->name, help) < 0)
246 annotate_browser__calc_percent(self, evidx);
249 annotate_browser__set_top(self, self->curr_hot);
254 key = ui_browser__run(&self->b, delay_secs);
256 if (delay_secs != 0) {
257 annotate_browser__calc_percent(self, evidx);
259 * Current line focus got out of the list of most active
260 * lines, NULL it so that if TAB|UNTAB is pressed, we
261 * move to curr_hot (current hottest line).
263 if (nd != NULL && RB_EMPTY_NODE(nd))
270 * FIXME we need to check if it was
271 * es.reason == NEWT_EXIT_TIMER
277 symbol__annotate_decay_histogram(sym, evidx);
283 nd = rb_last(&self->entries);
291 nd = rb_first(&self->entries);
299 if (annotate_browser__toggle_source(self))
300 ui_helpline__puts(help);
304 if (self->selection == NULL) {
305 ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
309 if (self->selection->offset == -1) {
310 ui_helpline__puts("Actions are only available for assembly lines.");
313 char *s = strstr(self->selection->line, "callq ");
314 struct annotation *notes;
315 struct symbol *target;
319 ui_helpline__puts("Actions are only available for the 'callq' instruction.");
325 ui_helpline__puts("Invallid callq instruction.");
329 ip = strtoull(s, NULL, 16);
330 ip = ms->map->map_ip(ms->map, ip);
331 target = map__find_symbol(ms->map, ip, NULL);
332 if (target == NULL) {
333 ui_helpline__puts("The called function was not found.");
337 notes = symbol__annotation(target);
338 pthread_mutex_lock(¬es->lock);
340 if (notes->src == NULL &&
341 symbol__alloc_hist(target, nr_events) < 0) {
342 pthread_mutex_unlock(¬es->lock);
343 ui__warning("Not enough memory for annotating '%s' symbol!\n",
348 pthread_mutex_unlock(¬es->lock);
349 symbol__tui_annotate(target, ms->map, evidx, nr_events,
350 timer, arg, delay_secs);
354 case NEWT_KEY_ESCAPE:
363 annotate_browser__set_top(self, nd);
366 ui_browser__hide(&self->b);
370 int hist_entry__tui_annotate(struct hist_entry *he, int evidx, int nr_events,
371 void(*timer)(void *arg), void *arg, int delay_secs)
373 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx, nr_events,
374 timer, arg, delay_secs);
377 int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
378 int nr_events, void(*timer)(void *arg), void *arg,
381 struct objdump_line *pos, *n;
382 struct annotation *notes;
383 struct map_symbol ms = {
387 struct annotate_browser browser = {
389 .refresh = ui_browser__list_head_refresh,
390 .seek = ui_browser__list_head_seek,
391 .write = annotate_browser__write,
392 .filter = objdump_line__filter,
394 .use_navkeypressed = true,
402 if (map->dso->annotate_warned)
405 if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
406 ui__error_window(ui_helpline__last_msg);
410 ui_helpline__push("Press <- or ESC to exit");
412 notes = symbol__annotation(sym);
414 list_for_each_entry(pos, ¬es->src->source, node) {
415 struct objdump_line_rb_node *rbpos;
416 size_t line_len = strlen(pos->line);
418 if (browser.b.width < line_len)
419 browser.b.width = line_len;
420 rbpos = objdump_line__rb(pos);
421 rbpos->idx = browser.nr_entries++;
422 if (pos->offset != -1)
423 rbpos->idx_asm = browser.nr_asm_entries++;
428 browser.b.nr_entries = browser.nr_entries;
429 browser.b.entries = ¬es->src->source,
430 browser.b.width += 18; /* Percentage */
431 ret = annotate_browser__run(&browser, evidx, nr_events,
432 timer, arg, delay_secs);
433 list_for_each_entry_safe(pos, n, ¬es->src->source, node) {
434 list_del(&pos->node);
435 objdump_line__free(pos);