blob: 752d8d0e480c17d19c7942f71cd5c7e41f7f1274 [file] [log] [blame]
Arnaldo Carvalho de Meloae557952011-10-26 08:00:55 -02001#include "../../util.h"
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -03002#include "../browser.h"
3#include "../helpline.h"
4#include "../libslang.h"
Arnaldo Carvalho de Meloae557952011-10-26 08:00:55 -02005#include "../ui.h"
6#include "../util.h"
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -02007#include "../../annotate.h"
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -03008#include "../../hist.h"
9#include "../../sort.h"
10#include "../../symbol.h"
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -030011#include <pthread.h>
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -020012#include <newt.h>
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030013
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030014struct annotate_browser {
15 struct ui_browser b;
16 struct rb_root entries;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -030017 struct rb_node *curr_hot;
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -030018 struct objdump_line *selection;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030019 u64 start;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -030020 int nr_asm_entries;
21 int nr_entries;
22 bool hide_src_code;
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -030023 bool use_offset;
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;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -030030 int idx_asm;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030031};
32
33static inline
34struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
35{
36 return (struct objdump_line_rb_node *)(self + 1);
37}
38
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -030039static bool objdump_line__filter(struct ui_browser *browser, void *entry)
40{
41 struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
42
43 if (ab->hide_src_code) {
44 struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
45 return ol->offset == -1;
46 }
47
48 return false;
49}
50
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030051static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
52{
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -030053 struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -030054 struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030055 bool current_entry = ui_browser__is_current_entry(self, row);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030056 bool change_color = (!ab->hide_src_code &&
57 (!current_entry || (self->use_navkeypressed &&
58 !self->navkeypressed)));
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030059 int width = self->width;
60
61 if (ol->offset != -1) {
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030062 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030063 ui_browser__set_percent_color(self, olrb->percent, current_entry);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030064 slsmg_printf(" %7.2f ", olrb->percent);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030065 } else {
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030066 ui_browser__set_percent_color(self, 0, current_entry);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030067 slsmg_write_nstring(" ", 9);
68 }
69
70 SLsmg_write_char(':');
71 slsmg_write_nstring(" ", 8);
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -020072
73 /* The scroll bar isn't being used */
74 if (!self->navkeypressed)
75 width += 1;
76
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030077 if (ol->offset != -1 && change_color)
78 ui_browser__set_color(self, HE_COLORSET_CODE);
Namhyung Kim58e817d2012-02-23 17:46:20 +090079
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030080 if (!*ol->line)
81 slsmg_write_nstring(" ", width - 18);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030082 else if (ol->offset == -1)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030083 slsmg_write_nstring(ol->line, width - 18);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030084 else {
85 char bf[64];
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -030086 u64 addr = ol->offset;
87 int printed, color = -1;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030088
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -030089 if (!ab->use_offset)
90 addr += ab->start;
91
92 printed = scnprintf(bf, sizeof(bf), " %" PRIx64 ":", addr);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030093 if (change_color)
94 color = ui_browser__set_color(self, HE_COLORSET_ADDR);
95 slsmg_write_nstring(bf, printed);
96 if (change_color)
97 ui_browser__set_color(self, color);
98 slsmg_write_nstring(ol->line, width - 18 - printed);
99 }
Arnaldo Carvalho de Melob99976e2011-02-09 13:59:14 -0200100
Namhyung Kim58e817d2012-02-23 17:46:20 +0900101 if (current_entry)
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300102 ab->selection = ol;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300103}
104
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300105static double objdump_line__calc_percent(struct objdump_line *self,
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200106 struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300107{
108 double percent = 0.0;
109
110 if (self->offset != -1) {
111 int len = sym->end - sym->start;
112 unsigned int hits = 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200113 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200114 struct source_line *src_line = notes->src->lines;
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200115 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300116 s64 offset = self->offset;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200117 struct objdump_line *next;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300118
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200119 next = objdump__get_next_ip_line(&notes->src->source, self);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300120 while (offset < (s64)len &&
121 (next == NULL || offset < next->offset)) {
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200122 if (src_line) {
123 percent += src_line[offset].percent;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300124 } else
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200125 hits += h->addr[offset];
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300126
127 ++offset;
128 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200129 /*
130 * If the percentage wasn't already calculated in
131 * symbol__get_source_line, do it now:
132 */
133 if (src_line == NULL && h->sum)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300134 percent = 100.0 * hits / h->sum;
135 }
136
137 return percent;
138}
139
140static void objdump__insert_line(struct rb_root *self,
141 struct objdump_line_rb_node *line)
142{
143 struct rb_node **p = &self->rb_node;
144 struct rb_node *parent = NULL;
145 struct objdump_line_rb_node *l;
146
147 while (*p != NULL) {
148 parent = *p;
149 l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
150 if (line->percent < l->percent)
151 p = &(*p)->rb_left;
152 else
153 p = &(*p)->rb_right;
154 }
155 rb_link_node(&line->rb_node, parent, p);
156 rb_insert_color(&line->rb_node, self);
157}
158
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300159static void annotate_browser__set_top(struct annotate_browser *self,
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300160 struct objdump_line *pos, u32 idx)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300161{
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300162 unsigned back;
163
164 ui_browser__refresh_dimensions(&self->b);
165 back = self->b.height / 2;
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300166 self->b.top_idx = self->b.index = idx;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300167
168 while (self->b.top_idx != 0 && back != 0) {
169 pos = list_entry(pos->node.prev, struct objdump_line, node);
170
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300171 if (objdump_line__filter(&self->b, &pos->node))
172 continue;
173
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300174 --self->b.top_idx;
175 --back;
176 }
177
178 self->b.top = pos;
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300179}
180
181static void annotate_browser__set_rb_top(struct annotate_browser *browser,
182 struct rb_node *nd)
183{
184 struct objdump_line_rb_node *rbpos;
185 struct objdump_line *pos;
186
187 rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
188 pos = ((struct objdump_line *)rbpos) - 1;
189 annotate_browser__set_top(browser, pos, rbpos->idx);
190 browser->curr_hot = nd;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300191}
192
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300193static void annotate_browser__calc_percent(struct annotate_browser *browser,
194 int evidx)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300195{
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300196 struct map_symbol *ms = browser->b.priv;
197 struct symbol *sym = ms->sym;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300198 struct annotation *notes = symbol__annotation(sym);
199 struct objdump_line *pos;
200
201 browser->entries = RB_ROOT;
202
203 pthread_mutex_lock(&notes->lock);
204
205 list_for_each_entry(pos, &notes->src->source, node) {
206 struct objdump_line_rb_node *rbpos = objdump_line__rb(pos);
207 rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
208 if (rbpos->percent < 0.01) {
209 RB_CLEAR_NODE(&rbpos->rb_node);
210 continue;
211 }
212 objdump__insert_line(&browser->entries, rbpos);
213 }
214 pthread_mutex_unlock(&notes->lock);
215
216 browser->curr_hot = rb_last(&browser->entries);
217}
218
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300219static bool annotate_browser__toggle_source(struct annotate_browser *browser)
220{
221 struct objdump_line *ol;
222 struct objdump_line_rb_node *olrb;
223 off_t offset = browser->b.index - browser->b.top_idx;
224
225 browser->b.seek(&browser->b, offset, SEEK_CUR);
226 ol = list_entry(browser->b.top, struct objdump_line, node);
227 olrb = objdump_line__rb(ol);
228
229 if (browser->hide_src_code) {
230 if (olrb->idx_asm < offset)
231 offset = olrb->idx;
232
233 browser->b.nr_entries = browser->nr_entries;
234 browser->hide_src_code = false;
235 browser->b.seek(&browser->b, -offset, SEEK_CUR);
236 browser->b.top_idx = olrb->idx - offset;
237 browser->b.index = olrb->idx;
238 } else {
239 if (olrb->idx_asm < 0) {
240 ui_helpline__puts("Only available for assembly lines.");
241 browser->b.seek(&browser->b, -offset, SEEK_CUR);
242 return false;
243 }
244
245 if (olrb->idx_asm < offset)
246 offset = olrb->idx_asm;
247
248 browser->b.nr_entries = browser->nr_asm_entries;
249 browser->hide_src_code = true;
250 browser->b.seek(&browser->b, -offset, SEEK_CUR);
251 browser->b.top_idx = olrb->idx_asm - offset;
252 browser->b.index = olrb->idx_asm;
253 }
254
255 return true;
256}
257
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300258static bool annotate_browser__callq(struct annotate_browser *browser,
259 int evidx, void (*timer)(void *arg),
260 void *arg, int delay_secs)
261{
262 struct map_symbol *ms = browser->b.priv;
263 struct symbol *sym = ms->sym;
264 struct annotation *notes;
265 struct symbol *target;
266 char *s = strstr(browser->selection->line, "callq ");
267 u64 ip;
268
269 if (s == NULL)
270 return false;
271
272 s = strchr(s, ' ');
273 if (s++ == NULL) {
274 ui_helpline__puts("Invallid callq instruction.");
275 return true;
276 }
277
278 ip = strtoull(s, NULL, 16);
279 ip = ms->map->map_ip(ms->map, ip);
280 target = map__find_symbol(ms->map, ip, NULL);
281 if (target == NULL) {
282 ui_helpline__puts("The called function was not found.");
283 return true;
284 }
285
286 notes = symbol__annotation(target);
287 pthread_mutex_lock(&notes->lock);
288
289 if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
290 pthread_mutex_unlock(&notes->lock);
291 ui__warning("Not enough memory for annotating '%s' symbol!\n",
292 target->name);
293 return true;
294 }
295
296 pthread_mutex_unlock(&notes->lock);
297 symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
298 ui_browser__show_title(&browser->b, sym->name);
299 return true;
300}
301
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300302static struct objdump_line *
303 annotate_browser__find_offset(struct annotate_browser *browser,
304 s64 offset, s64 *idx)
305{
306 struct map_symbol *ms = browser->b.priv;
307 struct symbol *sym = ms->sym;
308 struct annotation *notes = symbol__annotation(sym);
309 struct objdump_line *pos;
310
311 *idx = 0;
312 list_for_each_entry(pos, &notes->src->source, node) {
313 if (pos->offset == offset)
314 return pos;
315 if (!objdump_line__filter(&browser->b, &pos->node))
316 ++*idx;
317 }
318
319 return NULL;
320}
321
322static bool annotate_browser__jump(struct annotate_browser *browser)
323{
324 const char *jumps[] = { "je ", "jne ", "ja ", "jmpq ", "js ", "jmp ", NULL };
325 struct objdump_line *line;
326 s64 idx, offset;
327 char *s = NULL;
328 int i = 0;
329
330 while (jumps[i]) {
331 s = strstr(browser->selection->line, jumps[i++]);
332 if (s)
333 break;
334 }
335
336 if (s == NULL)
337 return false;
338
339 s = strchr(s, '+');
340 if (s++ == NULL) {
341 ui_helpline__puts("Invallid jump instruction.");
342 return true;
343 }
344
345 offset = strtoll(s, NULL, 16);
346 line = annotate_browser__find_offset(browser, offset, &idx);
347 if (line == NULL) {
348 ui_helpline__puts("Invallid jump offset");
349 return true;
350 }
351
352 annotate_browser__set_top(browser, line, idx);
353
354 return true;
355}
356
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300357static int annotate_browser__run(struct annotate_browser *self, int evidx,
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200358 void(*timer)(void *arg),
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300359 void *arg, int delay_secs)
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300360{
361 struct rb_node *nd = NULL;
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300362 struct map_symbol *ms = self->b.priv;
363 struct symbol *sym = ms->sym;
Namhyung Kim824ac0e2012-02-23 17:46:23 +0900364 const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
365 "H: Go to hottest line, ->/ENTER: Line action, "
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -0300366 "O: Toggle offset view, "
Namhyung Kim824ac0e2012-02-23 17:46:23 +0900367 "S: Toggle source code view";
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300368 int key;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300369
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300370 if (ui_browser__show(&self->b, sym->name, help) < 0)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300371 return -1;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300372
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300373 annotate_browser__calc_percent(self, evidx);
374
375 if (self->curr_hot)
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300376 annotate_browser__set_rb_top(self, self->curr_hot);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300377
378 nd = self->curr_hot;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300379
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300380 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300381 key = ui_browser__run(&self->b, delay_secs);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300382
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300383 if (delay_secs != 0) {
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300384 annotate_browser__calc_percent(self, evidx);
385 /*
386 * Current line focus got out of the list of most active
387 * lines, NULL it so that if TAB|UNTAB is pressed, we
388 * move to curr_hot (current hottest line).
389 */
390 if (nd != NULL && RB_EMPTY_NODE(nd))
391 nd = NULL;
392 }
393
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300394 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200395 case K_TIMER:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300396 if (timer != NULL)
397 timer(arg);
398
399 if (delay_secs != 0)
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300400 symbol__annotate_decay_histogram(sym, evidx);
401 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200402 case K_TAB:
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300403 if (nd != NULL) {
404 nd = rb_prev(nd);
405 if (nd == NULL)
406 nd = rb_last(&self->entries);
407 } else
408 nd = self->curr_hot;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300409 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200410 case K_UNTAB:
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300411 if (nd != NULL)
412 nd = rb_next(nd);
413 if (nd == NULL)
414 nd = rb_first(&self->entries);
415 else
416 nd = self->curr_hot;
417 break;
418 case 'H':
Namhyung Kimef7c5372012-02-23 17:46:21 +0900419 case 'h':
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300420 nd = self->curr_hot;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300421 break;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300422 case 'S':
Namhyung Kimef7c5372012-02-23 17:46:21 +0900423 case 's':
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300424 if (annotate_browser__toggle_source(self))
425 ui_helpline__puts(help);
426 continue;
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -0300427 case 'O':
428 case 'o':
429 self->use_offset = !self->use_offset;
430 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200431 case K_ENTER:
432 case K_RIGHT:
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300433 if (self->selection == NULL)
Arnaldo Carvalho de Melo234a5372011-10-06 09:45:29 -0300434 ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300435 else if (self->selection->offset == -1)
Arnaldo Carvalho de Melo234a5372011-10-06 09:45:29 -0300436 ui_helpline__puts("Actions are only available for assembly lines.");
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300437 else if (!(annotate_browser__jump(self) ||
438 annotate_browser__callq(self, evidx, timer, arg, delay_secs)))
439 ui_helpline__puts("Actions are only available for the 'callq' and jump instructions.");
Arnaldo Carvalho de Melofe46e642011-10-19 13:18:13 -0200440 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200441 case K_LEFT:
442 case K_ESC:
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300443 case 'q':
444 case CTRL('c'):
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300445 goto out;
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300446 default:
447 continue;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300448 }
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300449
450 if (nd != NULL)
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300451 annotate_browser__set_rb_top(self, nd);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300452 }
453out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300454 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300455 return key;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300456}
457
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200458int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300459 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200460{
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200461 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300462 timer, arg, delay_secs);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200463}
464
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300465int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200466 void(*timer)(void *arg), void *arg,
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300467 int delay_secs)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300468{
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300469 struct objdump_line *pos, *n;
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800470 struct annotation *notes;
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300471 struct map_symbol ms = {
472 .map = map,
473 .sym = sym,
474 };
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300475 struct annotate_browser browser = {
476 .b = {
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300477 .refresh = ui_browser__list_head_refresh,
478 .seek = ui_browser__list_head_seek,
479 .write = annotate_browser__write,
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300480 .filter = objdump_line__filter,
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300481 .priv = &ms,
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200482 .use_navkeypressed = true,
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300483 },
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300484 };
485 int ret;
486
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200487 if (sym == NULL)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300488 return -1;
489
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200490 if (map->dso->annotate_warned)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300491 return -1;
492
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300493 if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
Arnaldo Carvalho de Meloae557952011-10-26 08:00:55 -0200494 ui__error("%s", ui_helpline__last_msg);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300495 return -1;
496 }
497
498 ui_helpline__push("Press <- or ESC to exit");
499
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800500 notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300501 browser.start = map__rip_2objdump(map, sym->start);
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800502
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200503 list_for_each_entry(pos, &notes->src->source, node) {
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300504 struct objdump_line_rb_node *rbpos;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300505 size_t line_len = strlen(pos->line);
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300506
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300507 if (browser.b.width < line_len)
508 browser.b.width = line_len;
509 rbpos = objdump_line__rb(pos);
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300510 rbpos->idx = browser.nr_entries++;
511 if (pos->offset != -1)
512 rbpos->idx_asm = browser.nr_asm_entries++;
513 else
514 rbpos->idx_asm = -1;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300515 }
516
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300517 browser.b.nr_entries = browser.nr_entries;
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800518 browser.b.entries = &notes->src->source,
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300519 browser.b.width += 18; /* Percentage */
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200520 ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200521 list_for_each_entry_safe(pos, n, &notes->src->source, node) {
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300522 list_del(&pos->node);
523 objdump_line__free(pos);
524 }
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300525 return ret;
526}