blob: 1aa39658539c01687ea5af97ef014a6ea64c0652 [file] [log] [blame]
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -03001#include "../browser.h"
2#include "../helpline.h"
3#include "../libslang.h"
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02004#include "../../annotate.h"
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -03005#include "../../hist.h"
6#include "../../sort.h"
7#include "../../symbol.h"
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02008#include "../../annotate.h"
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -03009
10static void ui__error_window(const char *fmt, ...)
11{
12 va_list ap;
13
14 va_start(ap, fmt);
15 newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
16 va_end(ap);
17}
18
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030019struct annotate_browser {
20 struct ui_browser b;
21 struct rb_root entries;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -030022 struct rb_node *curr_hot;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030023};
24
25struct objdump_line_rb_node {
26 struct rb_node rb_node;
27 double percent;
28 u32 idx;
29};
30
31static inline
32struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
33{
34 return (struct objdump_line_rb_node *)(self + 1);
35}
36
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030037static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
38{
39 struct objdump_line *ol = rb_entry(entry, struct objdump_line, node);
40 bool current_entry = ui_browser__is_current_entry(self, row);
41 int width = self->width;
42
43 if (ol->offset != -1) {
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030044 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030045 ui_browser__set_percent_color(self, olrb->percent, current_entry);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030046 slsmg_printf(" %7.2f ", olrb->percent);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030047 if (!current_entry)
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030048 ui_browser__set_color(self, HE_COLORSET_CODE);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030049 } else {
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030050 ui_browser__set_percent_color(self, 0, current_entry);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030051 slsmg_write_nstring(" ", 9);
52 }
53
54 SLsmg_write_char(':');
55 slsmg_write_nstring(" ", 8);
56 if (!*ol->line)
57 slsmg_write_nstring(" ", width - 18);
58 else
59 slsmg_write_nstring(ol->line, width - 18);
60}
61
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030062static double objdump_line__calc_percent(struct objdump_line *self,
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020063 struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030064{
65 double percent = 0.0;
66
67 if (self->offset != -1) {
68 int len = sym->end - sym->start;
69 unsigned int hits = 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020070 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -020071 struct source_line *src_line = notes->src->lines;
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020072 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030073 s64 offset = self->offset;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -020074 struct objdump_line *next;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030075
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -020076 next = objdump__get_next_ip_line(&notes->src->source, self);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030077 while (offset < (s64)len &&
78 (next == NULL || offset < next->offset)) {
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020079 if (src_line) {
80 percent += src_line[offset].percent;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030081 } else
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020082 hits += h->addr[offset];
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030083
84 ++offset;
85 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020086 /*
87 * If the percentage wasn't already calculated in
88 * symbol__get_source_line, do it now:
89 */
90 if (src_line == NULL && h->sum)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030091 percent = 100.0 * hits / h->sum;
92 }
93
94 return percent;
95}
96
97static void objdump__insert_line(struct rb_root *self,
98 struct objdump_line_rb_node *line)
99{
100 struct rb_node **p = &self->rb_node;
101 struct rb_node *parent = NULL;
102 struct objdump_line_rb_node *l;
103
104 while (*p != NULL) {
105 parent = *p;
106 l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
107 if (line->percent < l->percent)
108 p = &(*p)->rb_left;
109 else
110 p = &(*p)->rb_right;
111 }
112 rb_link_node(&line->rb_node, parent, p);
113 rb_insert_color(&line->rb_node, self);
114}
115
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300116static void annotate_browser__set_top(struct annotate_browser *self,
117 struct rb_node *nd)
118{
119 struct objdump_line_rb_node *rbpos;
120 struct objdump_line *pos;
121 unsigned back;
122
123 ui_browser__refresh_dimensions(&self->b);
124 back = self->b.height / 2;
125 rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
126 pos = ((struct objdump_line *)rbpos) - 1;
127 self->b.top_idx = self->b.index = rbpos->idx;
128
129 while (self->b.top_idx != 0 && back != 0) {
130 pos = list_entry(pos->node.prev, struct objdump_line, node);
131
132 --self->b.top_idx;
133 --back;
134 }
135
136 self->b.top = pos;
137 self->curr_hot = nd;
138}
139
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300140static int annotate_browser__run(struct annotate_browser *self)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300141{
142 struct rb_node *nd;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200143 struct symbol *sym = self->b.priv;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300144 int key;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300145
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200146 if (ui_browser__show(&self->b, sym->name,
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300147 "<-, -> or ESC: exit, TAB/shift+TAB: cycle thru samples") < 0)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300148 return -1;
Arnaldo Carvalho de Melo4c1c9522010-08-12 12:37:51 -0300149 /*
150 * To allow builtin-annotate to cycle thru multiple symbols by
151 * examining the exit key for this function.
152 */
153 ui_browser__add_exit_key(&self->b, NEWT_KEY_RIGHT);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300154
155 nd = self->curr_hot;
156 if (nd) {
Arnaldo Carvalho de Melo4c1c9522010-08-12 12:37:51 -0300157 int tabs[] = { NEWT_KEY_TAB, NEWT_KEY_UNTAB, 0 };
158 ui_browser__add_exit_keys(&self->b, tabs);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300159 }
160
161 while (1) {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300162 key = ui_browser__run(&self->b);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300163
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300164 switch (key) {
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300165 case NEWT_KEY_TAB:
166 nd = rb_prev(nd);
167 if (nd == NULL)
168 nd = rb_last(&self->entries);
169 annotate_browser__set_top(self, nd);
170 break;
171 case NEWT_KEY_UNTAB:
172 nd = rb_next(nd);
173 if (nd == NULL)
174 nd = rb_first(&self->entries);
175 annotate_browser__set_top(self, nd);
176 break;
177 default:
178 goto out;
179 }
180 }
181out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300182 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300183 return key;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300184}
185
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200186int hist_entry__tui_annotate(struct hist_entry *he, int evidx)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200187{
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200188 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200189}
190
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200191int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300192{
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300193 struct objdump_line *pos, *n;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300194 struct objdump_line_rb_node *rbpos;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200195 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300196 struct annotate_browser browser = {
197 .b = {
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200198 .entries = &notes->src->source,
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300199 .refresh = ui_browser__list_head_refresh,
200 .seek = ui_browser__list_head_seek,
201 .write = annotate_browser__write,
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200202 .priv = sym,
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300203 },
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300204 };
205 int ret;
206
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200207 if (sym == NULL)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300208 return -1;
209
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200210 if (map->dso->annotate_warned)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300211 return -1;
212
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200213 if (symbol__annotate(sym, map, sizeof(*rbpos)) < 0) {
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300214 ui__error_window(ui_helpline__last_msg);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300215 return -1;
216 }
217
218 ui_helpline__push("Press <- or ESC to exit");
219
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200220 list_for_each_entry(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300221 size_t line_len = strlen(pos->line);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300222 if (browser.b.width < line_len)
223 browser.b.width = line_len;
224 rbpos = objdump_line__rb(pos);
225 rbpos->idx = browser.b.nr_entries++;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200226 rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300227 if (rbpos->percent < 0.01)
228 continue;
229 objdump__insert_line(&browser.entries, rbpos);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300230 }
231
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300232 /*
233 * Position the browser at the hottest line.
234 */
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300235 browser.curr_hot = rb_last(&browser.entries);
236 if (browser.curr_hot)
237 annotate_browser__set_top(&browser, browser.curr_hot);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300238
239 browser.b.width += 18; /* Percentage */
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300240 ret = annotate_browser__run(&browser);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200241 list_for_each_entry_safe(pos, n, &notes->src->source, node) {
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300242 list_del(&pos->node);
243 objdump_line__free(pos);
244 }
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300245 return ret;
246}