blob: 44fb6a447d00070ebda70e4853c64fe3e8924d04 [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 Melob793a4012012-04-19 12:19:22 -030014struct browser_disasm_line {
15 struct rb_node rb_node;
16 double percent;
17 u32 idx;
18 int idx_asm;
19 bool jump_target;
20};
21
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030022struct annotate_browser {
23 struct ui_browser b;
24 struct rb_root entries;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -030025 struct rb_node *curr_hot;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030026 struct disasm_line *selection;
Arnaldo Carvalho de Melob793a4012012-04-19 12:19:22 -030027 struct disasm_line **offsets;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030028 u64 start;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -030029 int nr_asm_entries;
30 int nr_entries;
31 bool hide_src_code;
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -030032 bool use_offset;
Arnaldo Carvalho de Melo9d1ef562012-04-27 16:35:29 -030033 bool jump_arrows;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -030034 bool searching_backwards;
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -030035 u8 offset_width;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -030036 char search_bf[128];
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030037};
38
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -030039static inline struct browser_disasm_line *disasm_line__browser(struct disasm_line *dl)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030040{
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -030041 return (struct browser_disasm_line *)(dl + 1);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030042}
43
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030044static bool disasm_line__filter(struct ui_browser *browser, void *entry)
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -030045{
46 struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
47
48 if (ab->hide_src_code) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030049 struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
50 return dl->offset == -1;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -030051 }
52
53 return false;
54}
55
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030056static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
57{
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -030058 struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030059 struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
Arnaldo Carvalho de Melob793a4012012-04-19 12:19:22 -030060 struct browser_disasm_line *bdl = disasm_line__browser(dl);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030061 bool current_entry = ui_browser__is_current_entry(self, row);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030062 bool change_color = (!ab->hide_src_code &&
63 (!current_entry || (self->use_navkeypressed &&
64 !self->navkeypressed)));
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030065 int width = self->width;
66
Arnaldo Carvalho de Melo0822cc82012-04-27 17:13:53 -030067 if (dl->offset != -1 && bdl->percent != 0.0) {
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -030068 ui_browser__set_percent_color(self, bdl->percent, current_entry);
Arnaldo Carvalho de Melo0822cc82012-04-27 17:13:53 -030069 slsmg_printf("%6.2f ", bdl->percent);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030070 } else {
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030071 ui_browser__set_percent_color(self, 0, current_entry);
Arnaldo Carvalho de Melo0822cc82012-04-27 17:13:53 -030072 slsmg_write_nstring(" ", 7);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030073 }
74
Arnaldo Carvalho de Melocf2dacc2012-04-19 15:19:17 -030075 SLsmg_write_char(' ');
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -020076
77 /* The scroll bar isn't being used */
78 if (!self->navkeypressed)
79 width += 1;
80
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030081 if (dl->offset != -1 && change_color)
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030082 ui_browser__set_color(self, HE_COLORSET_CODE);
Namhyung Kim58e817d2012-02-23 17:46:20 +090083
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030084 if (!*dl->line)
Arnaldo Carvalho de Melo0822cc82012-04-27 17:13:53 -030085 slsmg_write_nstring(" ", width - 7);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030086 else if (dl->offset == -1)
Arnaldo Carvalho de Melo0822cc82012-04-27 17:13:53 -030087 slsmg_write_nstring(dl->line, width - 7);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030088 else {
Arnaldo Carvalho de Melo8bf39cb2012-04-19 15:07:46 -030089 char bf[256];
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030090 u64 addr = dl->offset;
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -030091 int printed, color = -1;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030092
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -030093 if (!ab->use_offset)
94 addr += ab->start;
95
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -030096 if (!ab->use_offset) {
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -030097 printed = scnprintf(bf, sizeof(bf), " %" PRIx64 ":", addr);
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -030098 } else {
99 if (bdl->jump_target) {
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300100 printed = scnprintf(bf, sizeof(bf), " %*" PRIx64 ":",
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -0300101 ab->offset_width, addr);
102 } else {
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300103 printed = scnprintf(bf, sizeof(bf), " %*s ",
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -0300104 ab->offset_width, " ");
105 }
106 }
Arnaldo Carvalho de Melob793a4012012-04-19 12:19:22 -0300107
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300108 if (change_color)
109 color = ui_browser__set_color(self, HE_COLORSET_ADDR);
110 slsmg_write_nstring(bf, printed);
111 if (change_color)
112 ui_browser__set_color(self, color);
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -0300113 if (dl->ins && dl->ins->ops->scnprintf) {
Arnaldo Carvalho de Melo51a0d452012-04-20 15:40:20 -0300114 if (ins__is_jump(dl->ins)) {
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300115 bool fwd = dl->ops.target.offset > (u64)dl->offset;
Arnaldo Carvalho de Melo51a0d452012-04-20 15:40:20 -0300116
Arnaldo Carvalho de Melo59d038d2012-04-20 16:26:14 -0300117 ui_browser__write_graph(self, fwd ? SLSMG_DARROW_CHAR :
118 SLSMG_UARROW_CHAR);
Arnaldo Carvalho de Melo51a0d452012-04-20 15:40:20 -0300119 SLsmg_write_char(' ');
Arnaldo Carvalho de Melo88298f52012-04-27 15:10:54 -0300120 } else if (ins__is_call(dl->ins)) {
121 ui_browser__write_graph(self, SLSMG_RARROW_CHAR);
122 SLsmg_write_char(' ');
Arnaldo Carvalho de Melo51a0d452012-04-20 15:40:20 -0300123 } else {
124 slsmg_write_nstring(" ", 2);
125 }
126
Arnaldo Carvalho de Meloc7e6ead2012-04-20 14:38:46 -0300127 dl->ins->ops->scnprintf(dl->ins, bf, sizeof(bf), &dl->ops,
128 !ab->use_offset);
Arnaldo Carvalho de Melo4ea08b52012-04-20 15:51:40 -0300129 } else {
130 if (strcmp(dl->name, "retq")) {
131 slsmg_write_nstring(" ", 2);
132 } else {
Arnaldo Carvalho de Melo59d038d2012-04-20 16:26:14 -0300133 ui_browser__write_graph(self, SLSMG_LARROW_CHAR);
Arnaldo Carvalho de Melo4ea08b52012-04-20 15:51:40 -0300134 SLsmg_write_char(' ');
135 }
Arnaldo Carvalho de Melo28548d72012-04-19 10:16:27 -0300136
Arnaldo Carvalho de Melo4ea08b52012-04-20 15:51:40 -0300137 scnprintf(bf, sizeof(bf), "%-6.6s %s", dl->name, dl->ops.raw);
138 }
139
Arnaldo Carvalho de Melo0822cc82012-04-27 17:13:53 -0300140 slsmg_write_nstring(bf, width - 9 - printed);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300141 }
Arnaldo Carvalho de Melob99976e2011-02-09 13:59:14 -0200142
Namhyung Kim58e817d2012-02-23 17:46:20 +0900143 if (current_entry)
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300144 ab->selection = dl;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300145}
146
Arnaldo Carvalho de Melo9d1ef562012-04-27 16:35:29 -0300147static void annotate_browser__draw_current_jump(struct ui_browser *browser)
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300148{
149 struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
Arnaldo Carvalho de Melo9d1ef562012-04-27 16:35:29 -0300150 struct disasm_line *cursor = ab->selection, *target;
151 struct browser_disasm_line *btarget, *bcursor;
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300152 unsigned int from, to, start_width = 2;
153
Arnaldo Carvalho de Melo9d1ef562012-04-27 16:35:29 -0300154 if (!cursor->ins || !ins__is_jump(cursor->ins) ||
155 !disasm_line__has_offset(cursor))
156 return;
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300157
Arnaldo Carvalho de Melo9d1ef562012-04-27 16:35:29 -0300158 target = ab->offsets[cursor->ops.target.offset];
159 if (!target)
160 return;
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300161
Arnaldo Carvalho de Melo9d1ef562012-04-27 16:35:29 -0300162 bcursor = disasm_line__browser(cursor);
163 btarget = disasm_line__browser(target);
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300164
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300165 if (ab->hide_src_code) {
Arnaldo Carvalho de Melo9d1ef562012-04-27 16:35:29 -0300166 from = bcursor->idx_asm;
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300167 to = btarget->idx_asm;
168 } else {
Arnaldo Carvalho de Melo9d1ef562012-04-27 16:35:29 -0300169 from = (u64)bcursor->idx;
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300170 to = (u64)btarget->idx;
171 }
172
173 ui_browser__set_color(browser, HE_COLORSET_CODE);
174
Arnaldo Carvalho de Melo9d1ef562012-04-27 16:35:29 -0300175 if (!bcursor->jump_target)
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300176 start_width += ab->offset_width + 1;
177
Arnaldo Carvalho de Melo0822cc82012-04-27 17:13:53 -0300178 __ui_browser__line_arrow(browser, 7, from, to, start_width);
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300179}
180
181static unsigned int annotate_browser__refresh(struct ui_browser *browser)
182{
Arnaldo Carvalho de Melo9d1ef562012-04-27 16:35:29 -0300183 struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300184 int ret = ui_browser__list_head_refresh(browser);
185
Arnaldo Carvalho de Melo9d1ef562012-04-27 16:35:29 -0300186 if (ab->jump_arrows)
187 annotate_browser__draw_current_jump(browser);
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300188
189 return ret;
190}
191
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300192static double disasm_line__calc_percent(struct disasm_line *dl, struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300193{
194 double percent = 0.0;
195
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300196 if (dl->offset != -1) {
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300197 int len = sym->end - sym->start;
198 unsigned int hits = 0;
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200199 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200200 struct source_line *src_line = notes->src->lines;
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200201 struct sym_hist *h = annotation__histogram(notes, evidx);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300202 s64 offset = dl->offset;
203 struct disasm_line *next;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300204
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300205 next = disasm__get_next_ip_line(&notes->src->source, dl);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300206 while (offset < (s64)len &&
207 (next == NULL || offset < next->offset)) {
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200208 if (src_line) {
209 percent += src_line[offset].percent;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300210 } else
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200211 hits += h->addr[offset];
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300212
213 ++offset;
214 }
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200215 /*
216 * If the percentage wasn't already calculated in
217 * symbol__get_source_line, do it now:
218 */
219 if (src_line == NULL && h->sum)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300220 percent = 100.0 * hits / h->sum;
221 }
222
223 return percent;
224}
225
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300226static void disasm_rb_tree__insert(struct rb_root *root, struct browser_disasm_line *bdl)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300227{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300228 struct rb_node **p = &root->rb_node;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300229 struct rb_node *parent = NULL;
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300230 struct browser_disasm_line *l;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300231
232 while (*p != NULL) {
233 parent = *p;
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300234 l = rb_entry(parent, struct browser_disasm_line, rb_node);
235 if (bdl->percent < l->percent)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300236 p = &(*p)->rb_left;
237 else
238 p = &(*p)->rb_right;
239 }
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300240 rb_link_node(&bdl->rb_node, parent, p);
241 rb_insert_color(&bdl->rb_node, root);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300242}
243
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300244static void annotate_browser__set_top(struct annotate_browser *self,
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300245 struct disasm_line *pos, u32 idx)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300246{
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300247 unsigned back;
248
249 ui_browser__refresh_dimensions(&self->b);
250 back = self->b.height / 2;
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300251 self->b.top_idx = self->b.index = idx;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300252
253 while (self->b.top_idx != 0 && back != 0) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300254 pos = list_entry(pos->node.prev, struct disasm_line, node);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300255
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300256 if (disasm_line__filter(&self->b, &pos->node))
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300257 continue;
258
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300259 --self->b.top_idx;
260 --back;
261 }
262
263 self->b.top = pos;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300264 self->b.navkeypressed = true;
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300265}
266
267static void annotate_browser__set_rb_top(struct annotate_browser *browser,
268 struct rb_node *nd)
269{
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300270 struct browser_disasm_line *bpos;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300271 struct disasm_line *pos;
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300272
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300273 bpos = rb_entry(nd, struct browser_disasm_line, rb_node);
274 pos = ((struct disasm_line *)bpos) - 1;
275 annotate_browser__set_top(browser, pos, bpos->idx);
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300276 browser->curr_hot = nd;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300277}
278
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300279static void annotate_browser__calc_percent(struct annotate_browser *browser,
280 int evidx)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300281{
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300282 struct map_symbol *ms = browser->b.priv;
283 struct symbol *sym = ms->sym;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300284 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300285 struct disasm_line *pos;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300286
287 browser->entries = RB_ROOT;
288
289 pthread_mutex_lock(&notes->lock);
290
291 list_for_each_entry(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300292 struct browser_disasm_line *bpos = disasm_line__browser(pos);
293 bpos->percent = disasm_line__calc_percent(pos, sym, evidx);
294 if (bpos->percent < 0.01) {
295 RB_CLEAR_NODE(&bpos->rb_node);
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300296 continue;
297 }
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300298 disasm_rb_tree__insert(&browser->entries, bpos);
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300299 }
300 pthread_mutex_unlock(&notes->lock);
301
302 browser->curr_hot = rb_last(&browser->entries);
303}
304
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300305static bool annotate_browser__toggle_source(struct annotate_browser *browser)
306{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300307 struct disasm_line *dl;
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300308 struct browser_disasm_line *bdl;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300309 off_t offset = browser->b.index - browser->b.top_idx;
310
311 browser->b.seek(&browser->b, offset, SEEK_CUR);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300312 dl = list_entry(browser->b.top, struct disasm_line, node);
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300313 bdl = disasm_line__browser(dl);
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300314
315 if (browser->hide_src_code) {
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300316 if (bdl->idx_asm < offset)
317 offset = bdl->idx;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300318
319 browser->b.nr_entries = browser->nr_entries;
320 browser->hide_src_code = false;
321 browser->b.seek(&browser->b, -offset, SEEK_CUR);
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300322 browser->b.top_idx = bdl->idx - offset;
323 browser->b.index = bdl->idx;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300324 } else {
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300325 if (bdl->idx_asm < 0) {
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300326 ui_helpline__puts("Only available for assembly lines.");
327 browser->b.seek(&browser->b, -offset, SEEK_CUR);
328 return false;
329 }
330
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300331 if (bdl->idx_asm < offset)
332 offset = bdl->idx_asm;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300333
334 browser->b.nr_entries = browser->nr_asm_entries;
335 browser->hide_src_code = true;
336 browser->b.seek(&browser->b, -offset, SEEK_CUR);
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300337 browser->b.top_idx = bdl->idx_asm - offset;
338 browser->b.index = bdl->idx_asm;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300339 }
340
341 return true;
342}
343
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300344static bool annotate_browser__callq(struct annotate_browser *browser,
345 int evidx, void (*timer)(void *arg),
346 void *arg, int delay_secs)
347{
348 struct map_symbol *ms = browser->b.priv;
Arnaldo Carvalho de Melo657bcaf2012-04-15 20:12:07 -0300349 struct disasm_line *dl = browser->selection;
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300350 struct symbol *sym = ms->sym;
351 struct annotation *notes;
352 struct symbol *target;
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300353 u64 ip;
354
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -0300355 if (!ins__is_call(dl->ins))
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300356 return false;
357
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300358 ip = ms->map->map_ip(ms->map, dl->ops.target.addr);
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300359 target = map__find_symbol(ms->map, ip, NULL);
360 if (target == NULL) {
361 ui_helpline__puts("The called function was not found.");
362 return true;
363 }
364
365 notes = symbol__annotation(target);
366 pthread_mutex_lock(&notes->lock);
367
368 if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
369 pthread_mutex_unlock(&notes->lock);
370 ui__warning("Not enough memory for annotating '%s' symbol!\n",
371 target->name);
372 return true;
373 }
374
375 pthread_mutex_unlock(&notes->lock);
376 symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
377 ui_browser__show_title(&browser->b, sym->name);
378 return true;
379}
380
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300381static
382struct disasm_line *annotate_browser__find_offset(struct annotate_browser *browser,
383 s64 offset, s64 *idx)
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300384{
385 struct map_symbol *ms = browser->b.priv;
386 struct symbol *sym = ms->sym;
387 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300388 struct disasm_line *pos;
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300389
390 *idx = 0;
391 list_for_each_entry(pos, &notes->src->source, node) {
392 if (pos->offset == offset)
393 return pos;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300394 if (!disasm_line__filter(&browser->b, &pos->node))
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300395 ++*idx;
396 }
397
398 return NULL;
399}
400
401static bool annotate_browser__jump(struct annotate_browser *browser)
402{
Arnaldo Carvalho de Melo657bcaf2012-04-15 20:12:07 -0300403 struct disasm_line *dl = browser->selection;
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300404 s64 idx;
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300405
Arnaldo Carvalho de Melod86b0592012-04-18 16:07:38 -0300406 if (!ins__is_jump(dl->ins))
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300407 return false;
408
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300409 dl = annotate_browser__find_offset(browser, dl->ops.target.offset, &idx);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300410 if (dl == NULL) {
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300411 ui_helpline__puts("Invallid jump offset");
412 return true;
413 }
414
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300415 annotate_browser__set_top(browser, dl, idx);
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300416
417 return true;
418}
419
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300420static
421struct disasm_line *annotate_browser__find_string(struct annotate_browser *browser,
422 char *s, s64 *idx)
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300423{
424 struct map_symbol *ms = browser->b.priv;
425 struct symbol *sym = ms->sym;
426 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300427 struct disasm_line *pos = browser->selection;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300428
429 *idx = browser->b.index;
430 list_for_each_entry_continue(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300431 if (disasm_line__filter(&browser->b, &pos->node))
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300432 continue;
433
434 ++*idx;
435
436 if (pos->line && strstr(pos->line, s) != NULL)
437 return pos;
438 }
439
440 return NULL;
441}
442
443static bool __annotate_browser__search(struct annotate_browser *browser)
444{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300445 struct disasm_line *dl;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300446 s64 idx;
447
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300448 dl = annotate_browser__find_string(browser, browser->search_bf, &idx);
449 if (dl == NULL) {
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300450 ui_helpline__puts("String not found!");
451 return false;
452 }
453
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300454 annotate_browser__set_top(browser, dl, idx);
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300455 browser->searching_backwards = false;
456 return true;
457}
458
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300459static
460struct disasm_line *annotate_browser__find_string_reverse(struct annotate_browser *browser,
461 char *s, s64 *idx)
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300462{
463 struct map_symbol *ms = browser->b.priv;
464 struct symbol *sym = ms->sym;
465 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300466 struct disasm_line *pos = browser->selection;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300467
468 *idx = browser->b.index;
469 list_for_each_entry_continue_reverse(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300470 if (disasm_line__filter(&browser->b, &pos->node))
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300471 continue;
472
473 --*idx;
474
475 if (pos->line && strstr(pos->line, s) != NULL)
476 return pos;
477 }
478
479 return NULL;
480}
481
482static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
483{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300484 struct disasm_line *dl;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300485 s64 idx;
486
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300487 dl = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
488 if (dl == NULL) {
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300489 ui_helpline__puts("String not found!");
490 return false;
491 }
492
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300493 annotate_browser__set_top(browser, dl, idx);
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300494 browser->searching_backwards = true;
495 return true;
496}
497
498static bool annotate_browser__search_window(struct annotate_browser *browser,
499 int delay_secs)
500{
501 if (ui_browser__input_window("Search", "String: ", browser->search_bf,
502 "ENTER: OK, ESC: Cancel",
503 delay_secs * 2) != K_ENTER ||
504 !*browser->search_bf)
505 return false;
506
507 return true;
508}
509
510static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs)
511{
512 if (annotate_browser__search_window(browser, delay_secs))
513 return __annotate_browser__search(browser);
514
515 return false;
516}
517
518static bool annotate_browser__continue_search(struct annotate_browser *browser,
519 int delay_secs)
520{
521 if (!*browser->search_bf)
522 return annotate_browser__search(browser, delay_secs);
523
524 return __annotate_browser__search(browser);
525}
526
527static bool annotate_browser__search_reverse(struct annotate_browser *browser,
528 int delay_secs)
529{
530 if (annotate_browser__search_window(browser, delay_secs))
531 return __annotate_browser__search_reverse(browser);
532
533 return false;
534}
535
536static
537bool annotate_browser__continue_search_reverse(struct annotate_browser *browser,
538 int delay_secs)
539{
540 if (!*browser->search_bf)
541 return annotate_browser__search_reverse(browser, delay_secs);
542
543 return __annotate_browser__search_reverse(browser);
544}
545
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300546static int annotate_browser__run(struct annotate_browser *self, int evidx,
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200547 void(*timer)(void *arg),
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300548 void *arg, int delay_secs)
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300549{
550 struct rb_node *nd = NULL;
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300551 struct map_symbol *ms = self->b.priv;
552 struct symbol *sym = ms->sym;
Namhyung Kim824ac0e2012-02-23 17:46:23 +0900553 const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
554 "H: Go to hottest line, ->/ENTER: Line action, "
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -0300555 "O: Toggle offset view, "
Namhyung Kim824ac0e2012-02-23 17:46:23 +0900556 "S: Toggle source code view";
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300557 int key;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300558
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300559 if (ui_browser__show(&self->b, sym->name, help) < 0)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300560 return -1;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300561
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300562 annotate_browser__calc_percent(self, evidx);
563
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300564 if (self->curr_hot) {
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300565 annotate_browser__set_rb_top(self, self->curr_hot);
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300566 self->b.navkeypressed = false;
567 }
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300568
569 nd = self->curr_hot;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300570
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300571 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300572 key = ui_browser__run(&self->b, delay_secs);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300573
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300574 if (delay_secs != 0) {
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300575 annotate_browser__calc_percent(self, evidx);
576 /*
577 * Current line focus got out of the list of most active
578 * lines, NULL it so that if TAB|UNTAB is pressed, we
579 * move to curr_hot (current hottest line).
580 */
581 if (nd != NULL && RB_EMPTY_NODE(nd))
582 nd = NULL;
583 }
584
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300585 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200586 case K_TIMER:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300587 if (timer != NULL)
588 timer(arg);
589
590 if (delay_secs != 0)
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300591 symbol__annotate_decay_histogram(sym, evidx);
592 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200593 case K_TAB:
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300594 if (nd != NULL) {
595 nd = rb_prev(nd);
596 if (nd == NULL)
597 nd = rb_last(&self->entries);
598 } else
599 nd = self->curr_hot;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300600 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200601 case K_UNTAB:
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300602 if (nd != NULL)
603 nd = rb_next(nd);
604 if (nd == NULL)
605 nd = rb_first(&self->entries);
606 else
607 nd = self->curr_hot;
608 break;
609 case 'H':
Namhyung Kimef7c5372012-02-23 17:46:21 +0900610 case 'h':
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300611 nd = self->curr_hot;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300612 break;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300613 case 'S':
Namhyung Kimef7c5372012-02-23 17:46:21 +0900614 case 's':
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300615 if (annotate_browser__toggle_source(self))
616 ui_helpline__puts(help);
617 continue;
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -0300618 case 'O':
619 case 'o':
620 self->use_offset = !self->use_offset;
621 continue;
Arnaldo Carvalho de Melo9d1ef562012-04-27 16:35:29 -0300622 case 'j':
623 self->jump_arrows = !self->jump_arrows;
624 continue;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300625 case '/':
626 if (annotate_browser__search(self, delay_secs)) {
627show_help:
628 ui_helpline__puts(help);
629 }
630 continue;
631 case 'n':
632 if (self->searching_backwards ?
633 annotate_browser__continue_search_reverse(self, delay_secs) :
634 annotate_browser__continue_search(self, delay_secs))
635 goto show_help;
636 continue;
637 case '?':
638 if (annotate_browser__search_reverse(self, delay_secs))
639 goto show_help;
640 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200641 case K_ENTER:
642 case K_RIGHT:
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300643 if (self->selection == NULL)
Arnaldo Carvalho de Melo234a5372011-10-06 09:45:29 -0300644 ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300645 else if (self->selection->offset == -1)
Arnaldo Carvalho de Melo234a5372011-10-06 09:45:29 -0300646 ui_helpline__puts("Actions are only available for assembly lines.");
Arnaldo Carvalho de Meloc4cceae2012-04-20 15:57:15 -0300647 else if (!self->selection->ins) {
648 if (strcmp(self->selection->name, "retq"))
649 goto show_sup_ins;
650 goto out;
651 } else if (!(annotate_browser__jump(self) ||
652 annotate_browser__callq(self, evidx, timer, arg, delay_secs))) {
653show_sup_ins:
654 ui_helpline__puts("Actions are only available for 'callq', 'retq' & jump instructions.");
655 }
Arnaldo Carvalho de Melofe46e642011-10-19 13:18:13 -0200656 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200657 case K_LEFT:
658 case K_ESC:
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300659 case 'q':
660 case CTRL('c'):
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300661 goto out;
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300662 default:
663 continue;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300664 }
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300665
666 if (nd != NULL)
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300667 annotate_browser__set_rb_top(self, nd);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300668 }
669out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300670 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300671 return key;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300672}
673
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200674int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300675 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200676{
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200677 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300678 timer, arg, delay_secs);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200679}
680
Arnaldo Carvalho de Melob793a4012012-04-19 12:19:22 -0300681static void annotate_browser__mark_jump_targets(struct annotate_browser *browser,
682 size_t size)
683{
684 u64 offset;
685
686 for (offset = 0; offset < size; ++offset) {
687 struct disasm_line *dl = browser->offsets[offset], *dlt;
688 struct browser_disasm_line *bdlt;
689
Arnaldo Carvalho de Melo38b31bd2012-04-25 14:18:42 -0300690 if (!dl || !dl->ins || !ins__is_jump(dl->ins) ||
691 !disasm_line__has_offset(dl))
Arnaldo Carvalho de Melob793a4012012-04-19 12:19:22 -0300692 continue;
693
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300694 if (dl->ops.target.offset >= size) {
Arnaldo Carvalho de Melob793a4012012-04-19 12:19:22 -0300695 ui__error("jump to after symbol!\n"
696 "size: %zx, jump target: %" PRIx64,
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300697 size, dl->ops.target.offset);
Arnaldo Carvalho de Melob793a4012012-04-19 12:19:22 -0300698 continue;
699 }
700
Arnaldo Carvalho de Melo44d1a3e2012-04-25 08:00:23 -0300701 dlt = browser->offsets[dl->ops.target.offset];
Arnaldo Carvalho de Melo9481ede2012-04-25 07:48:42 -0300702 /*
703 * FIXME: Oops, no jump target? Buggy disassembler? Or do we
704 * have to adjust to the previous offset?
705 */
706 if (dlt == NULL)
707 continue;
708
Arnaldo Carvalho de Melob793a4012012-04-19 12:19:22 -0300709 bdlt = disasm_line__browser(dlt);
710 bdlt->jump_target = true;
711 }
712
713}
714
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300715int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200716 void(*timer)(void *arg), void *arg,
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300717 int delay_secs)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300718{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300719 struct disasm_line *pos, *n;
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800720 struct annotation *notes;
Arnaldo Carvalho de Melob793a4012012-04-19 12:19:22 -0300721 const size_t size = symbol__size(sym);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300722 struct map_symbol ms = {
723 .map = map,
724 .sym = sym,
725 };
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300726 struct annotate_browser browser = {
727 .b = {
Arnaldo Carvalho de Meloa3f895b2012-04-24 14:24:28 -0300728 .refresh = annotate_browser__refresh,
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300729 .seek = ui_browser__list_head_seek,
730 .write = annotate_browser__write,
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300731 .filter = disasm_line__filter,
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300732 .priv = &ms,
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200733 .use_navkeypressed = true,
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300734 },
Arnaldo Carvalho de Melo8bf39cb2012-04-19 15:07:46 -0300735 .use_offset = true,
Arnaldo Carvalho de Melo9d1ef562012-04-27 16:35:29 -0300736 .jump_arrows = true,
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300737 };
Arnaldo Carvalho de Melob793a4012012-04-19 12:19:22 -0300738 int ret = -1;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300739
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200740 if (sym == NULL)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300741 return -1;
742
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200743 if (map->dso->annotate_warned)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300744 return -1;
745
Arnaldo Carvalho de Melob793a4012012-04-19 12:19:22 -0300746 browser.offsets = zalloc(size * sizeof(struct disasm_line *));
747 if (browser.offsets == NULL) {
748 ui__error("Not enough memory!");
749 return -1;
750 }
751
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300752 if (symbol__annotate(sym, map, sizeof(struct browser_disasm_line)) < 0) {
Arnaldo Carvalho de Meloae557952011-10-26 08:00:55 -0200753 ui__error("%s", ui_helpline__last_msg);
Arnaldo Carvalho de Melob793a4012012-04-19 12:19:22 -0300754 goto out_free_offsets;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300755 }
756
757 ui_helpline__push("Press <- or ESC to exit");
758
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800759 notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300760 browser.start = map__rip_2objdump(map, sym->start);
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800761
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200762 list_for_each_entry(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300763 struct browser_disasm_line *bpos;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300764 size_t line_len = strlen(pos->line);
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300765
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300766 if (browser.b.width < line_len)
767 browser.b.width = line_len;
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300768 bpos = disasm_line__browser(pos);
769 bpos->idx = browser.nr_entries++;
Arnaldo Carvalho de Melob793a4012012-04-19 12:19:22 -0300770 if (pos->offset != -1) {
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300771 bpos->idx_asm = browser.nr_asm_entries++;
Arnaldo Carvalho de Melo97148a92012-04-20 15:17:50 -0300772 /*
773 * FIXME: short term bandaid to cope with assembly
774 * routines that comes with labels in the same column
775 * as the address in objdump, sigh.
776 *
777 * E.g. copy_user_generic_unrolled
778 */
779 if (pos->offset < (s64)size)
780 browser.offsets[pos->offset] = pos;
Arnaldo Carvalho de Melob793a4012012-04-19 12:19:22 -0300781 } else
Arnaldo Carvalho de Melo887c0062012-04-19 10:29:53 -0300782 bpos->idx_asm = -1;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300783 }
784
Arnaldo Carvalho de Melob793a4012012-04-19 12:19:22 -0300785 annotate_browser__mark_jump_targets(&browser, size);
786
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -0300787 browser.offset_width = hex_width(size);
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300788 browser.b.nr_entries = browser.nr_entries;
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800789 browser.b.entries = &notes->src->source,
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300790 browser.b.width += 18; /* Percentage */
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200791 ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200792 list_for_each_entry_safe(pos, n, &notes->src->source, node) {
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300793 list_del(&pos->node);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300794 disasm_line__free(pos);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300795 }
Arnaldo Carvalho de Melob793a4012012-04-19 12:19:22 -0300796
797out_free_offsets:
798 free(browser.offsets);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300799 return ret;
800}