blob: 4db5186472b5890c85e184e374549d36e921503c [file] [log] [blame]
Namhyung Kimaca7a942012-04-04 00:14:26 -07001#include "../../util/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"
Namhyung Kimaca7a942012-04-04 00:14:26 -07007#include "../../util/annotate.h"
8#include "../../util/hist.h"
9#include "../../util/sort.h"
10#include "../../util/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 Melod3d1f612012-04-07 17:10:30 -030024 bool searching_backwards;
25 char search_bf[128];
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030026};
27
28struct objdump_line_rb_node {
29 struct rb_node rb_node;
30 double percent;
31 u32 idx;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -030032 int idx_asm;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030033};
34
35static inline
36struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
37{
38 return (struct objdump_line_rb_node *)(self + 1);
39}
40
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -030041static bool objdump_line__filter(struct ui_browser *browser, void *entry)
42{
43 struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
44
45 if (ab->hide_src_code) {
46 struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
47 return ol->offset == -1;
48 }
49
50 return false;
51}
52
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030053static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
54{
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -030055 struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -030056 struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030057 bool current_entry = ui_browser__is_current_entry(self, row);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030058 bool change_color = (!ab->hide_src_code &&
59 (!current_entry || (self->use_navkeypressed &&
60 !self->navkeypressed)));
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030061 int width = self->width;
62
63 if (ol->offset != -1) {
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030064 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030065 ui_browser__set_percent_color(self, olrb->percent, current_entry);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030066 slsmg_printf(" %7.2f ", olrb->percent);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030067 } else {
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030068 ui_browser__set_percent_color(self, 0, current_entry);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030069 slsmg_write_nstring(" ", 9);
70 }
71
72 SLsmg_write_char(':');
73 slsmg_write_nstring(" ", 8);
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -020074
75 /* The scroll bar isn't being used */
76 if (!self->navkeypressed)
77 width += 1;
78
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030079 if (ol->offset != -1 && change_color)
80 ui_browser__set_color(self, HE_COLORSET_CODE);
Namhyung Kim58e817d2012-02-23 17:46:20 +090081
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030082 if (!*ol->line)
83 slsmg_write_nstring(" ", width - 18);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030084 else if (ol->offset == -1)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030085 slsmg_write_nstring(ol->line, width - 18);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030086 else {
87 char bf[64];
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -030088 u64 addr = ol->offset;
89 int printed, color = -1;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030090
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -030091 if (!ab->use_offset)
92 addr += ab->start;
93
94 printed = scnprintf(bf, sizeof(bf), " %" PRIx64 ":", addr);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030095 if (change_color)
96 color = ui_browser__set_color(self, HE_COLORSET_ADDR);
97 slsmg_write_nstring(bf, printed);
98 if (change_color)
99 ui_browser__set_color(self, color);
100 slsmg_write_nstring(ol->line, width - 18 - printed);
101 }
Arnaldo Carvalho de Melob99976e2011-02-09 13:59:14 -0200102
Namhyung Kim58e817d2012-02-23 17:46:20 +0900103 if (current_entry)
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300104 ab->selection = ol;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300105}
106
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300107static double objdump_line__calc_percent(struct objdump_line *self,
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200108 struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300109{
110 double percent = 0.0;
111
112 if (self->offset != -1) {
113 int len = sym->end - sym->start;
114 unsigned int hits = 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200115 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200116 struct source_line *src_line = notes->src->lines;
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200117 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300118 s64 offset = self->offset;
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200119 struct objdump_line *next;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300120
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200121 next = objdump__get_next_ip_line(&notes->src->source, self);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300122 while (offset < (s64)len &&
123 (next == NULL || offset < next->offset)) {
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200124 if (src_line) {
125 percent += src_line[offset].percent;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300126 } else
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200127 hits += h->addr[offset];
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300128
129 ++offset;
130 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200131 /*
132 * If the percentage wasn't already calculated in
133 * symbol__get_source_line, do it now:
134 */
135 if (src_line == NULL && h->sum)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300136 percent = 100.0 * hits / h->sum;
137 }
138
139 return percent;
140}
141
142static void objdump__insert_line(struct rb_root *self,
143 struct objdump_line_rb_node *line)
144{
145 struct rb_node **p = &self->rb_node;
146 struct rb_node *parent = NULL;
147 struct objdump_line_rb_node *l;
148
149 while (*p != NULL) {
150 parent = *p;
151 l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
152 if (line->percent < l->percent)
153 p = &(*p)->rb_left;
154 else
155 p = &(*p)->rb_right;
156 }
157 rb_link_node(&line->rb_node, parent, p);
158 rb_insert_color(&line->rb_node, self);
159}
160
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300161static void annotate_browser__set_top(struct annotate_browser *self,
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300162 struct objdump_line *pos, u32 idx)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300163{
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300164 unsigned back;
165
166 ui_browser__refresh_dimensions(&self->b);
167 back = self->b.height / 2;
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300168 self->b.top_idx = self->b.index = idx;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300169
170 while (self->b.top_idx != 0 && back != 0) {
171 pos = list_entry(pos->node.prev, struct objdump_line, node);
172
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300173 if (objdump_line__filter(&self->b, &pos->node))
174 continue;
175
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300176 --self->b.top_idx;
177 --back;
178 }
179
180 self->b.top = pos;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300181 self->b.navkeypressed = true;
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300182}
183
184static void annotate_browser__set_rb_top(struct annotate_browser *browser,
185 struct rb_node *nd)
186{
187 struct objdump_line_rb_node *rbpos;
188 struct objdump_line *pos;
189
190 rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
191 pos = ((struct objdump_line *)rbpos) - 1;
192 annotate_browser__set_top(browser, pos, rbpos->idx);
193 browser->curr_hot = nd;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300194}
195
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300196static void annotate_browser__calc_percent(struct annotate_browser *browser,
197 int evidx)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300198{
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300199 struct map_symbol *ms = browser->b.priv;
200 struct symbol *sym = ms->sym;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300201 struct annotation *notes = symbol__annotation(sym);
202 struct objdump_line *pos;
203
204 browser->entries = RB_ROOT;
205
206 pthread_mutex_lock(&notes->lock);
207
208 list_for_each_entry(pos, &notes->src->source, node) {
209 struct objdump_line_rb_node *rbpos = objdump_line__rb(pos);
210 rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
211 if (rbpos->percent < 0.01) {
212 RB_CLEAR_NODE(&rbpos->rb_node);
213 continue;
214 }
215 objdump__insert_line(&browser->entries, rbpos);
216 }
217 pthread_mutex_unlock(&notes->lock);
218
219 browser->curr_hot = rb_last(&browser->entries);
220}
221
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300222static bool annotate_browser__toggle_source(struct annotate_browser *browser)
223{
224 struct objdump_line *ol;
225 struct objdump_line_rb_node *olrb;
226 off_t offset = browser->b.index - browser->b.top_idx;
227
228 browser->b.seek(&browser->b, offset, SEEK_CUR);
229 ol = list_entry(browser->b.top, struct objdump_line, node);
230 olrb = objdump_line__rb(ol);
231
232 if (browser->hide_src_code) {
233 if (olrb->idx_asm < offset)
234 offset = olrb->idx;
235
236 browser->b.nr_entries = browser->nr_entries;
237 browser->hide_src_code = false;
238 browser->b.seek(&browser->b, -offset, SEEK_CUR);
239 browser->b.top_idx = olrb->idx - offset;
240 browser->b.index = olrb->idx;
241 } else {
242 if (olrb->idx_asm < 0) {
243 ui_helpline__puts("Only available for assembly lines.");
244 browser->b.seek(&browser->b, -offset, SEEK_CUR);
245 return false;
246 }
247
248 if (olrb->idx_asm < offset)
249 offset = olrb->idx_asm;
250
251 browser->b.nr_entries = browser->nr_asm_entries;
252 browser->hide_src_code = true;
253 browser->b.seek(&browser->b, -offset, SEEK_CUR);
254 browser->b.top_idx = olrb->idx_asm - offset;
255 browser->b.index = olrb->idx_asm;
256 }
257
258 return true;
259}
260
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300261static bool annotate_browser__callq(struct annotate_browser *browser,
262 int evidx, void (*timer)(void *arg),
263 void *arg, int delay_secs)
264{
265 struct map_symbol *ms = browser->b.priv;
266 struct symbol *sym = ms->sym;
267 struct annotation *notes;
268 struct symbol *target;
269 char *s = strstr(browser->selection->line, "callq ");
270 u64 ip;
271
272 if (s == NULL)
273 return false;
274
275 s = strchr(s, ' ');
276 if (s++ == NULL) {
277 ui_helpline__puts("Invallid callq instruction.");
278 return true;
279 }
280
281 ip = strtoull(s, NULL, 16);
282 ip = ms->map->map_ip(ms->map, ip);
283 target = map__find_symbol(ms->map, ip, NULL);
284 if (target == NULL) {
285 ui_helpline__puts("The called function was not found.");
286 return true;
287 }
288
289 notes = symbol__annotation(target);
290 pthread_mutex_lock(&notes->lock);
291
292 if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
293 pthread_mutex_unlock(&notes->lock);
294 ui__warning("Not enough memory for annotating '%s' symbol!\n",
295 target->name);
296 return true;
297 }
298
299 pthread_mutex_unlock(&notes->lock);
300 symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
301 ui_browser__show_title(&browser->b, sym->name);
302 return true;
303}
304
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300305static struct objdump_line *
306 annotate_browser__find_offset(struct annotate_browser *browser,
307 s64 offset, s64 *idx)
308{
309 struct map_symbol *ms = browser->b.priv;
310 struct symbol *sym = ms->sym;
311 struct annotation *notes = symbol__annotation(sym);
312 struct objdump_line *pos;
313
314 *idx = 0;
315 list_for_each_entry(pos, &notes->src->source, node) {
316 if (pos->offset == offset)
317 return pos;
318 if (!objdump_line__filter(&browser->b, &pos->node))
319 ++*idx;
320 }
321
322 return NULL;
323}
324
325static bool annotate_browser__jump(struct annotate_browser *browser)
326{
327 const char *jumps[] = { "je ", "jne ", "ja ", "jmpq ", "js ", "jmp ", NULL };
328 struct objdump_line *line;
329 s64 idx, offset;
330 char *s = NULL;
331 int i = 0;
332
333 while (jumps[i]) {
334 s = strstr(browser->selection->line, jumps[i++]);
335 if (s)
336 break;
337 }
338
339 if (s == NULL)
340 return false;
341
342 s = strchr(s, '+');
343 if (s++ == NULL) {
344 ui_helpline__puts("Invallid jump instruction.");
345 return true;
346 }
347
348 offset = strtoll(s, NULL, 16);
349 line = annotate_browser__find_offset(browser, offset, &idx);
350 if (line == NULL) {
351 ui_helpline__puts("Invallid jump offset");
352 return true;
353 }
354
355 annotate_browser__set_top(browser, line, idx);
356
357 return true;
358}
359
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300360static struct objdump_line *
361 annotate_browser__find_string(struct annotate_browser *browser,
362 char *s, s64 *idx)
363{
364 struct map_symbol *ms = browser->b.priv;
365 struct symbol *sym = ms->sym;
366 struct annotation *notes = symbol__annotation(sym);
367 struct objdump_line *pos = browser->selection;
368
369 *idx = browser->b.index;
370 list_for_each_entry_continue(pos, &notes->src->source, node) {
371 if (objdump_line__filter(&browser->b, &pos->node))
372 continue;
373
374 ++*idx;
375
376 if (pos->line && strstr(pos->line, s) != NULL)
377 return pos;
378 }
379
380 return NULL;
381}
382
383static bool __annotate_browser__search(struct annotate_browser *browser)
384{
385 struct objdump_line *line;
386 s64 idx;
387
388 line = annotate_browser__find_string(browser, browser->search_bf, &idx);
389 if (line == NULL) {
390 ui_helpline__puts("String not found!");
391 return false;
392 }
393
394 annotate_browser__set_top(browser, line, idx);
395 browser->searching_backwards = false;
396 return true;
397}
398
399static struct objdump_line *
400 annotate_browser__find_string_reverse(struct annotate_browser *browser,
401 char *s, s64 *idx)
402{
403 struct map_symbol *ms = browser->b.priv;
404 struct symbol *sym = ms->sym;
405 struct annotation *notes = symbol__annotation(sym);
406 struct objdump_line *pos = browser->selection;
407
408 *idx = browser->b.index;
409 list_for_each_entry_continue_reverse(pos, &notes->src->source, node) {
410 if (objdump_line__filter(&browser->b, &pos->node))
411 continue;
412
413 --*idx;
414
415 if (pos->line && strstr(pos->line, s) != NULL)
416 return pos;
417 }
418
419 return NULL;
420}
421
422static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
423{
424 struct objdump_line *line;
425 s64 idx;
426
427 line = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
428 if (line == NULL) {
429 ui_helpline__puts("String not found!");
430 return false;
431 }
432
433 annotate_browser__set_top(browser, line, idx);
434 browser->searching_backwards = true;
435 return true;
436}
437
438static bool annotate_browser__search_window(struct annotate_browser *browser,
439 int delay_secs)
440{
441 if (ui_browser__input_window("Search", "String: ", browser->search_bf,
442 "ENTER: OK, ESC: Cancel",
443 delay_secs * 2) != K_ENTER ||
444 !*browser->search_bf)
445 return false;
446
447 return true;
448}
449
450static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs)
451{
452 if (annotate_browser__search_window(browser, delay_secs))
453 return __annotate_browser__search(browser);
454
455 return false;
456}
457
458static bool annotate_browser__continue_search(struct annotate_browser *browser,
459 int delay_secs)
460{
461 if (!*browser->search_bf)
462 return annotate_browser__search(browser, delay_secs);
463
464 return __annotate_browser__search(browser);
465}
466
467static bool annotate_browser__search_reverse(struct annotate_browser *browser,
468 int delay_secs)
469{
470 if (annotate_browser__search_window(browser, delay_secs))
471 return __annotate_browser__search_reverse(browser);
472
473 return false;
474}
475
476static
477bool annotate_browser__continue_search_reverse(struct annotate_browser *browser,
478 int delay_secs)
479{
480 if (!*browser->search_bf)
481 return annotate_browser__search_reverse(browser, delay_secs);
482
483 return __annotate_browser__search_reverse(browser);
484}
485
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300486static int annotate_browser__run(struct annotate_browser *self, int evidx,
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200487 void(*timer)(void *arg),
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300488 void *arg, int delay_secs)
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300489{
490 struct rb_node *nd = NULL;
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300491 struct map_symbol *ms = self->b.priv;
492 struct symbol *sym = ms->sym;
Namhyung Kim824ac0e2012-02-23 17:46:23 +0900493 const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
494 "H: Go to hottest line, ->/ENTER: Line action, "
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -0300495 "O: Toggle offset view, "
Namhyung Kim824ac0e2012-02-23 17:46:23 +0900496 "S: Toggle source code view";
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300497 int key;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300498
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300499 if (ui_browser__show(&self->b, sym->name, help) < 0)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300500 return -1;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300501
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300502 annotate_browser__calc_percent(self, evidx);
503
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300504 if (self->curr_hot) {
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300505 annotate_browser__set_rb_top(self, self->curr_hot);
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300506 self->b.navkeypressed = false;
507 }
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300508
509 nd = self->curr_hot;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300510
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300511 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300512 key = ui_browser__run(&self->b, delay_secs);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300513
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300514 if (delay_secs != 0) {
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300515 annotate_browser__calc_percent(self, evidx);
516 /*
517 * Current line focus got out of the list of most active
518 * lines, NULL it so that if TAB|UNTAB is pressed, we
519 * move to curr_hot (current hottest line).
520 */
521 if (nd != NULL && RB_EMPTY_NODE(nd))
522 nd = NULL;
523 }
524
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300525 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200526 case K_TIMER:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300527 if (timer != NULL)
528 timer(arg);
529
530 if (delay_secs != 0)
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300531 symbol__annotate_decay_histogram(sym, evidx);
532 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200533 case K_TAB:
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300534 if (nd != NULL) {
535 nd = rb_prev(nd);
536 if (nd == NULL)
537 nd = rb_last(&self->entries);
538 } else
539 nd = self->curr_hot;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300540 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200541 case K_UNTAB:
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300542 if (nd != NULL)
543 nd = rb_next(nd);
544 if (nd == NULL)
545 nd = rb_first(&self->entries);
546 else
547 nd = self->curr_hot;
548 break;
549 case 'H':
Namhyung Kimef7c5372012-02-23 17:46:21 +0900550 case 'h':
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300551 nd = self->curr_hot;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300552 break;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300553 case 'S':
Namhyung Kimef7c5372012-02-23 17:46:21 +0900554 case 's':
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300555 if (annotate_browser__toggle_source(self))
556 ui_helpline__puts(help);
557 continue;
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -0300558 case 'O':
559 case 'o':
560 self->use_offset = !self->use_offset;
561 continue;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300562 case '/':
563 if (annotate_browser__search(self, delay_secs)) {
564show_help:
565 ui_helpline__puts(help);
566 }
567 continue;
568 case 'n':
569 if (self->searching_backwards ?
570 annotate_browser__continue_search_reverse(self, delay_secs) :
571 annotate_browser__continue_search(self, delay_secs))
572 goto show_help;
573 continue;
574 case '?':
575 if (annotate_browser__search_reverse(self, delay_secs))
576 goto show_help;
577 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200578 case K_ENTER:
579 case K_RIGHT:
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300580 if (self->selection == NULL)
Arnaldo Carvalho de Melo234a5372011-10-06 09:45:29 -0300581 ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300582 else if (self->selection->offset == -1)
Arnaldo Carvalho de Melo234a5372011-10-06 09:45:29 -0300583 ui_helpline__puts("Actions are only available for assembly lines.");
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300584 else if (!(annotate_browser__jump(self) ||
585 annotate_browser__callq(self, evidx, timer, arg, delay_secs)))
586 ui_helpline__puts("Actions are only available for the 'callq' and jump instructions.");
Arnaldo Carvalho de Melofe46e642011-10-19 13:18:13 -0200587 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200588 case K_LEFT:
589 case K_ESC:
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300590 case 'q':
591 case CTRL('c'):
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300592 goto out;
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300593 default:
594 continue;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300595 }
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300596
597 if (nd != NULL)
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300598 annotate_browser__set_rb_top(self, nd);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300599 }
600out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300601 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300602 return key;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300603}
604
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200605int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300606 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200607{
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200608 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300609 timer, arg, delay_secs);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200610}
611
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300612int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200613 void(*timer)(void *arg), void *arg,
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300614 int delay_secs)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300615{
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300616 struct objdump_line *pos, *n;
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800617 struct annotation *notes;
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300618 struct map_symbol ms = {
619 .map = map,
620 .sym = sym,
621 };
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300622 struct annotate_browser browser = {
623 .b = {
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300624 .refresh = ui_browser__list_head_refresh,
625 .seek = ui_browser__list_head_seek,
626 .write = annotate_browser__write,
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300627 .filter = objdump_line__filter,
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300628 .priv = &ms,
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200629 .use_navkeypressed = true,
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300630 },
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300631 };
632 int ret;
633
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200634 if (sym == NULL)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300635 return -1;
636
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200637 if (map->dso->annotate_warned)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300638 return -1;
639
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300640 if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
Arnaldo Carvalho de Meloae557952011-10-26 08:00:55 -0200641 ui__error("%s", ui_helpline__last_msg);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300642 return -1;
643 }
644
645 ui_helpline__push("Press <- or ESC to exit");
646
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800647 notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300648 browser.start = map__rip_2objdump(map, sym->start);
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800649
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200650 list_for_each_entry(pos, &notes->src->source, node) {
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300651 struct objdump_line_rb_node *rbpos;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300652 size_t line_len = strlen(pos->line);
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300653
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300654 if (browser.b.width < line_len)
655 browser.b.width = line_len;
656 rbpos = objdump_line__rb(pos);
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300657 rbpos->idx = browser.nr_entries++;
658 if (pos->offset != -1)
659 rbpos->idx_asm = browser.nr_asm_entries++;
660 else
661 rbpos->idx_asm = -1;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300662 }
663
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300664 browser.b.nr_entries = browser.nr_entries;
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800665 browser.b.entries = &notes->src->source,
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300666 browser.b.width += 18; /* Percentage */
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200667 ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200668 list_for_each_entry_safe(pos, n, &notes->src->source, node) {
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300669 list_del(&pos->node);
670 objdump_line__free(pos);
671 }
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300672 return ret;
673}