1 #include "../browser.h"
2 #include "../helpline.h"
3 #include "../libslang.h"
4 #include "../../hist.h"
5 #include "../../sort.h"
6 #include "../../symbol.h"
8 static void ui__error_window(const char *fmt, ...)
13 newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
17 struct annotate_browser {
19 struct rb_root entries;
20 struct rb_node *curr_hot;
23 struct objdump_line_rb_node {
24 struct rb_node rb_node;
30 struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
32 return (struct objdump_line_rb_node *)(self + 1);
35 static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
37 struct objdump_line *ol = rb_entry(entry, struct objdump_line, node);
38 bool current_entry = ui_browser__is_current_entry(self, row);
39 int width = self->width;
41 if (ol->offset != -1) {
42 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
43 ui_browser__set_percent_color(self, olrb->percent, current_entry);
44 slsmg_printf(" %7.2f ", olrb->percent);
46 ui_browser__set_color(self, HE_COLORSET_CODE);
48 ui_browser__set_percent_color(self, 0, current_entry);
49 slsmg_write_nstring(" ", 9);
52 SLsmg_write_char(':');
53 slsmg_write_nstring(" ", 8);
55 slsmg_write_nstring(" ", width - 18);
57 slsmg_write_nstring(ol->line, width - 18);
60 static double objdump_line__calc_percent(struct objdump_line *self,
61 struct list_head *head,
66 if (self->offset != -1) {
67 int len = sym->end - sym->start;
68 unsigned int hits = 0;
69 struct sym_priv *priv = symbol__priv(sym);
70 struct sym_ext *sym_ext = priv->ext;
71 struct sym_hist *h = priv->hist;
72 s64 offset = self->offset;
73 struct objdump_line *next = objdump__get_next_ip_line(head, self);
76 while (offset < (s64)len &&
77 (next == NULL || offset < next->offset)) {
79 percent += sym_ext[offset].percent;
81 hits += h->ip[offset];
86 if (sym_ext == NULL && h->sum)
87 percent = 100.0 * hits / h->sum;
93 static void objdump__insert_line(struct rb_root *self,
94 struct objdump_line_rb_node *line)
96 struct rb_node **p = &self->rb_node;
97 struct rb_node *parent = NULL;
98 struct objdump_line_rb_node *l;
102 l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
103 if (line->percent < l->percent)
108 rb_link_node(&line->rb_node, parent, p);
109 rb_insert_color(&line->rb_node, self);
112 static void annotate_browser__set_top(struct annotate_browser *self,
115 struct objdump_line_rb_node *rbpos;
116 struct objdump_line *pos;
119 ui_browser__refresh_dimensions(&self->b);
120 back = self->b.height / 2;
121 rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
122 pos = ((struct objdump_line *)rbpos) - 1;
123 self->b.top_idx = self->b.index = rbpos->idx;
125 while (self->b.top_idx != 0 && back != 0) {
126 pos = list_entry(pos->node.prev, struct objdump_line, node);
136 static int annotate_browser__run(struct annotate_browser *self)
139 struct hist_entry *he = self->b.priv;
142 if (ui_browser__show(&self->b, he->ms.sym->name,
143 "<-, -> or ESC: exit, TAB/shift+TAB: cycle thru samples") < 0)
146 * To allow builtin-annotate to cycle thru multiple symbols by
147 * examining the exit key for this function.
149 ui_browser__add_exit_key(&self->b, NEWT_KEY_RIGHT);
153 int tabs[] = { NEWT_KEY_TAB, NEWT_KEY_UNTAB, 0 };
154 ui_browser__add_exit_keys(&self->b, tabs);
158 key = ui_browser__run(&self->b);
164 nd = rb_last(&self->entries);
165 annotate_browser__set_top(self, nd);
170 nd = rb_first(&self->entries);
171 annotate_browser__set_top(self, nd);
178 ui_browser__hide(&self->b);
182 int hist_entry__tui_annotate(struct hist_entry *self)
184 struct objdump_line *pos, *n;
185 struct objdump_line_rb_node *rbpos;
187 struct annotate_browser browser = {
190 .refresh = ui_browser__list_head_refresh,
191 .seek = ui_browser__list_head_seek,
192 .write = annotate_browser__write,
198 if (self->ms.sym == NULL)
201 if (self->ms.map->dso->annotate_warned)
204 if (hist_entry__annotate(self, &head, sizeof(*rbpos)) < 0) {
205 ui__error_window(ui_helpline__last_msg);
209 ui_helpline__push("Press <- or ESC to exit");
211 list_for_each_entry(pos, &head, node) {
212 size_t line_len = strlen(pos->line);
213 if (browser.b.width < line_len)
214 browser.b.width = line_len;
215 rbpos = objdump_line__rb(pos);
216 rbpos->idx = browser.b.nr_entries++;
217 rbpos->percent = objdump_line__calc_percent(pos, &head, self->ms.sym);
218 if (rbpos->percent < 0.01)
220 objdump__insert_line(&browser.entries, rbpos);
224 * Position the browser at the hottest line.
226 browser.curr_hot = rb_last(&browser.entries);
227 if (browser.curr_hot)
228 annotate_browser__set_top(&browser, browser.curr_hot);
230 browser.b.width += 18; /* Percentage */
231 ret = annotate_browser__run(&browser);
232 list_for_each_entry_safe(pos, n, &head, node) {
233 list_del(&pos->node);
234 objdump_line__free(pos);