blob: 15633d6081332f0204acc978974ff6deec8d2375 [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 Meloc97cf422011-02-22 12:02:07 -03009#include <pthread.h>
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030010
11static void ui__error_window(const char *fmt, ...)
12{
13 va_list ap;
14
15 va_start(ap, fmt);
16 newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
17 va_end(ap);
18}
19
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030020struct annotate_browser {
21 struct ui_browser b;
22 struct rb_root entries;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -030023 struct rb_node *curr_hot;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030024};
25
26struct objdump_line_rb_node {
27 struct rb_node rb_node;
28 double percent;
29 u32 idx;
30};
31
32static inline
33struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
34{
35 return (struct objdump_line_rb_node *)(self + 1);
36}
37
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030038static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
39{
40 struct objdump_line *ol = rb_entry(entry, struct objdump_line, node);
41 bool current_entry = ui_browser__is_current_entry(self, row);
42 int width = self->width;
43
44 if (ol->offset != -1) {
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030045 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030046 ui_browser__set_percent_color(self, olrb->percent, current_entry);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030047 slsmg_printf(" %7.2f ", olrb->percent);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030048 } else {
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030049 ui_browser__set_percent_color(self, 0, current_entry);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030050 slsmg_write_nstring(" ", 9);
51 }
52
53 SLsmg_write_char(':');
54 slsmg_write_nstring(" ", 8);
55 if (!*ol->line)
56 slsmg_write_nstring(" ", width - 18);
57 else
58 slsmg_write_nstring(ol->line, width - 18);
Arnaldo Carvalho de Melob99976e2011-02-09 13:59:14 -020059
60 if (!current_entry)
61 ui_browser__set_color(self, HE_COLORSET_CODE);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030062}
63
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030064static double objdump_line__calc_percent(struct objdump_line *self,
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020065 struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030066{
67 double percent = 0.0;
68
69 if (self->offset != -1) {
70 int len = sym->end - sym->start;
71 unsigned int hits = 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020072 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -020073 struct source_line *src_line = notes->src->lines;
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -020074 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030075 s64 offset = self->offset;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -020076 struct objdump_line *next;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030077
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -020078 next = objdump__get_next_ip_line(&notes->src->source, self);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030079 while (offset < (s64)len &&
80 (next == NULL || offset < next->offset)) {
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020081 if (src_line) {
82 percent += src_line[offset].percent;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030083 } else
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020084 hits += h->addr[offset];
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030085
86 ++offset;
87 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020088 /*
89 * If the percentage wasn't already calculated in
90 * symbol__get_source_line, do it now:
91 */
92 if (src_line == NULL && h->sum)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030093 percent = 100.0 * hits / h->sum;
94 }
95
96 return percent;
97}
98
99static void objdump__insert_line(struct rb_root *self,
100 struct objdump_line_rb_node *line)
101{
102 struct rb_node **p = &self->rb_node;
103 struct rb_node *parent = NULL;
104 struct objdump_line_rb_node *l;
105
106 while (*p != NULL) {
107 parent = *p;
108 l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
109 if (line->percent < l->percent)
110 p = &(*p)->rb_left;
111 else
112 p = &(*p)->rb_right;
113 }
114 rb_link_node(&line->rb_node, parent, p);
115 rb_insert_color(&line->rb_node, self);
116}
117
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300118static void annotate_browser__set_top(struct annotate_browser *self,
119 struct rb_node *nd)
120{
121 struct objdump_line_rb_node *rbpos;
122 struct objdump_line *pos;
123 unsigned back;
124
125 ui_browser__refresh_dimensions(&self->b);
126 back = self->b.height / 2;
127 rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
128 pos = ((struct objdump_line *)rbpos) - 1;
129 self->b.top_idx = self->b.index = rbpos->idx;
130
131 while (self->b.top_idx != 0 && back != 0) {
132 pos = list_entry(pos->node.prev, struct objdump_line, node);
133
134 --self->b.top_idx;
135 --back;
136 }
137
138 self->b.top = pos;
139 self->curr_hot = nd;
140}
141
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300142static void annotate_browser__calc_percent(struct annotate_browser *browser,
143 int evidx)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300144{
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300145 struct symbol *sym = browser->b.priv;
146 struct annotation *notes = symbol__annotation(sym);
147 struct objdump_line *pos;
148
149 browser->entries = RB_ROOT;
150
151 pthread_mutex_lock(&notes->lock);
152
153 list_for_each_entry(pos, &notes->src->source, node) {
154 struct objdump_line_rb_node *rbpos = objdump_line__rb(pos);
155 rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
156 if (rbpos->percent < 0.01) {
157 RB_CLEAR_NODE(&rbpos->rb_node);
158 continue;
159 }
160 objdump__insert_line(&browser->entries, rbpos);
161 }
162 pthread_mutex_unlock(&notes->lock);
163
164 browser->curr_hot = rb_last(&browser->entries);
165}
166
167static int annotate_browser__run(struct annotate_browser *self, int evidx,
168 int refresh)
169{
170 struct rb_node *nd = NULL;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200171 struct symbol *sym = self->b.priv;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300172 /*
173 * RIGHT To allow builtin-annotate to cycle thru multiple symbols by
174 * examining the exit key for this function.
175 */
176 int exit_keys[] = { 'H', NEWT_KEY_TAB, NEWT_KEY_UNTAB,
177 NEWT_KEY_RIGHT, 0 };
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300178 int key;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300179
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200180 if (ui_browser__show(&self->b, sym->name,
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300181 "<-, -> or ESC: exit, TAB/shift+TAB: "
182 "cycle hottest lines, H: Hottest") < 0)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300183 return -1;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300184
185 ui_browser__add_exit_keys(&self->b, exit_keys);
186 annotate_browser__calc_percent(self, evidx);
187
188 if (self->curr_hot)
189 annotate_browser__set_top(self, self->curr_hot);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300190
191 nd = self->curr_hot;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300192
193 if (refresh != 0)
194 newtFormSetTimer(self->b.form, refresh);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300195
196 while (1) {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300197 key = ui_browser__run(&self->b);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300198
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300199 if (refresh != 0) {
200 annotate_browser__calc_percent(self, evidx);
201 /*
202 * Current line focus got out of the list of most active
203 * lines, NULL it so that if TAB|UNTAB is pressed, we
204 * move to curr_hot (current hottest line).
205 */
206 if (nd != NULL && RB_EMPTY_NODE(nd))
207 nd = NULL;
208 }
209
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300210 switch (key) {
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300211 case -1:
212 /*
213 * FIXME we need to check if it was
214 * es.reason == NEWT_EXIT_TIMER
215 */
216 if (refresh != 0)
217 symbol__annotate_decay_histogram(sym, evidx);
218 continue;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300219 case NEWT_KEY_TAB:
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300220 if (nd != NULL) {
221 nd = rb_prev(nd);
222 if (nd == NULL)
223 nd = rb_last(&self->entries);
224 } else
225 nd = self->curr_hot;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300226 break;
227 case NEWT_KEY_UNTAB:
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300228 if (nd != NULL)
229 nd = rb_next(nd);
230 if (nd == NULL)
231 nd = rb_first(&self->entries);
232 else
233 nd = self->curr_hot;
234 break;
235 case 'H':
236 nd = self->curr_hot;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300237 break;
238 default:
239 goto out;
240 }
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300241
242 if (nd != NULL)
243 annotate_browser__set_top(self, nd);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300244 }
245out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300246 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300247 return key;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300248}
249
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200250int hist_entry__tui_annotate(struct hist_entry *he, int evidx)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200251{
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300252 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx, 0);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200253}
254
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300255int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
256 int refresh)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300257{
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300258 struct objdump_line *pos, *n;
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800259 struct annotation *notes;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300260 struct annotate_browser browser = {
261 .b = {
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300262 .refresh = ui_browser__list_head_refresh,
263 .seek = ui_browser__list_head_seek,
264 .write = annotate_browser__write,
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200265 .priv = sym,
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300266 },
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300267 };
268 int ret;
269
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200270 if (sym == NULL)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300271 return -1;
272
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200273 if (map->dso->annotate_warned)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300274 return -1;
275
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300276 if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300277 ui__error_window(ui_helpline__last_msg);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300278 return -1;
279 }
280
281 ui_helpline__push("Press <- or ESC to exit");
282
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800283 notes = symbol__annotation(sym);
284
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200285 list_for_each_entry(pos, &notes->src->source, node) {
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300286 struct objdump_line_rb_node *rbpos;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300287 size_t line_len = strlen(pos->line);
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300288
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300289 if (browser.b.width < line_len)
290 browser.b.width = line_len;
291 rbpos = objdump_line__rb(pos);
292 rbpos->idx = browser.b.nr_entries++;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300293 }
294
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800295 browser.b.entries = &notes->src->source,
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300296 browser.b.width += 18; /* Percentage */
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300297 ret = annotate_browser__run(&browser, evidx, refresh);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200298 list_for_each_entry_safe(pos, n, &notes->src->source, node) {
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300299 list_del(&pos->node);
300 objdump_line__free(pos);
301 }
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300302 return ret;
303}