blob: 0bc3e652b5417ca3ad18d55ba3729760972e1d77 [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 Melo29ed6e72012-04-15 15:24:39 -030018 struct disasm_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
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030028struct disasm_line_rb_node {
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030029 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
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030035static inline struct disasm_line_rb_node *disasm_line__rb(struct disasm_line *dl)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030036{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030037 return (struct disasm_line_rb_node *)(dl + 1);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030038}
39
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030040static bool disasm_line__filter(struct ui_browser *browser, void *entry)
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -030041{
42 struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
43
44 if (ab->hide_src_code) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030045 struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
46 return dl->offset == -1;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -030047 }
48
49 return false;
50}
51
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030052static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
53{
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -030054 struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030055 struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030056 bool current_entry = ui_browser__is_current_entry(self, row);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030057 bool change_color = (!ab->hide_src_code &&
58 (!current_entry || (self->use_navkeypressed &&
59 !self->navkeypressed)));
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030060 int width = self->width;
61
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030062 if (dl->offset != -1) {
63 struct disasm_line_rb_node *dlrb = disasm_line__rb(dl);
64 ui_browser__set_percent_color(self, dlrb->percent, current_entry);
65 slsmg_printf(" %7.2f ", dlrb->percent);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030066 } else {
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030067 ui_browser__set_percent_color(self, 0, current_entry);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030068 slsmg_write_nstring(" ", 9);
69 }
70
71 SLsmg_write_char(':');
72 slsmg_write_nstring(" ", 8);
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -020073
74 /* The scroll bar isn't being used */
75 if (!self->navkeypressed)
76 width += 1;
77
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030078 if (dl->offset != -1 && change_color)
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030079 ui_browser__set_color(self, HE_COLORSET_CODE);
Namhyung Kim58e817d2012-02-23 17:46:20 +090080
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030081 if (!*dl->line)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030082 slsmg_write_nstring(" ", width - 18);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030083 else if (dl->offset == -1)
84 slsmg_write_nstring(dl->line, width - 18);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030085 else {
86 char bf[64];
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030087 u64 addr = dl->offset;
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -030088 int printed, color = -1;
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030089
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -030090 if (!ab->use_offset)
91 addr += ab->start;
92
93 printed = scnprintf(bf, sizeof(bf), " %" PRIx64 ":", addr);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -030094 if (change_color)
95 color = ui_browser__set_color(self, HE_COLORSET_ADDR);
96 slsmg_write_nstring(bf, printed);
97 if (change_color)
98 ui_browser__set_color(self, color);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -030099 slsmg_write_nstring(dl->line, width - 18 - printed);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300100 }
Arnaldo Carvalho de Melob99976e2011-02-09 13:59:14 -0200101
Namhyung Kim58e817d2012-02-23 17:46:20 +0900102 if (current_entry)
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300103 ab->selection = dl;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300104}
105
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300106static double disasm_line__calc_percent(struct disasm_line *dl, struct symbol *sym, int evidx)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300107{
108 double percent = 0.0;
109
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300110 if (dl->offset != -1) {
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300111 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 Melo29ed6e72012-04-15 15:24:39 -0300116 s64 offset = dl->offset;
117 struct disasm_line *next;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300118
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300119 next = disasm__get_next_ip_line(&notes->src->source, dl);
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
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300140static void disasm_rb_tree__insert(struct rb_root *root, struct disasm_line_rb_node *dlrb)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300141{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300142 struct rb_node **p = &root->rb_node;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300143 struct rb_node *parent = NULL;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300144 struct disasm_line_rb_node *l;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300145
146 while (*p != NULL) {
147 parent = *p;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300148 l = rb_entry(parent, struct disasm_line_rb_node, rb_node);
149 if (dlrb->percent < l->percent)
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300150 p = &(*p)->rb_left;
151 else
152 p = &(*p)->rb_right;
153 }
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300154 rb_link_node(&dlrb->rb_node, parent, p);
155 rb_insert_color(&dlrb->rb_node, root);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300156}
157
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300158static void annotate_browser__set_top(struct annotate_browser *self,
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300159 struct disasm_line *pos, u32 idx)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300160{
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300161 unsigned back;
162
163 ui_browser__refresh_dimensions(&self->b);
164 back = self->b.height / 2;
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300165 self->b.top_idx = self->b.index = idx;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300166
167 while (self->b.top_idx != 0 && back != 0) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300168 pos = list_entry(pos->node.prev, struct disasm_line, node);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300169
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300170 if (disasm_line__filter(&self->b, &pos->node))
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300171 continue;
172
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300173 --self->b.top_idx;
174 --back;
175 }
176
177 self->b.top = pos;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300178 self->b.navkeypressed = true;
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{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300184 struct disasm_line_rb_node *rbpos;
185 struct disasm_line *pos;
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300186
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300187 rbpos = rb_entry(nd, struct disasm_line_rb_node, rb_node);
188 pos = ((struct disasm_line *)rbpos) - 1;
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300189 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);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300199 struct disasm_line *pos;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300200
201 browser->entries = RB_ROOT;
202
203 pthread_mutex_lock(&notes->lock);
204
205 list_for_each_entry(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300206 struct disasm_line_rb_node *rbpos = disasm_line__rb(pos);
207 rbpos->percent = disasm_line__calc_percent(pos, sym, evidx);
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300208 if (rbpos->percent < 0.01) {
209 RB_CLEAR_NODE(&rbpos->rb_node);
210 continue;
211 }
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300212 disasm_rb_tree__insert(&browser->entries, rbpos);
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300213 }
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{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300221 struct disasm_line *dl;
222 struct disasm_line_rb_node *dlrb;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300223 off_t offset = browser->b.index - browser->b.top_idx;
224
225 browser->b.seek(&browser->b, offset, SEEK_CUR);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300226 dl = list_entry(browser->b.top, struct disasm_line, node);
227 dlrb = disasm_line__rb(dl);
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300228
229 if (browser->hide_src_code) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300230 if (dlrb->idx_asm < offset)
231 offset = dlrb->idx;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300232
233 browser->b.nr_entries = browser->nr_entries;
234 browser->hide_src_code = false;
235 browser->b.seek(&browser->b, -offset, SEEK_CUR);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300236 browser->b.top_idx = dlrb->idx - offset;
237 browser->b.index = dlrb->idx;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300238 } else {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300239 if (dlrb->idx_asm < 0) {
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300240 ui_helpline__puts("Only available for assembly lines.");
241 browser->b.seek(&browser->b, -offset, SEEK_CUR);
242 return false;
243 }
244
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300245 if (dlrb->idx_asm < offset)
246 offset = dlrb->idx_asm;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300247
248 browser->b.nr_entries = browser->nr_asm_entries;
249 browser->hide_src_code = true;
250 browser->b.seek(&browser->b, -offset, SEEK_CUR);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300251 browser->b.top_idx = dlrb->idx_asm - offset;
252 browser->b.index = dlrb->idx_asm;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300253 }
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;
Arnaldo Carvalho de Melo657bcaf2012-04-15 20:12:07 -0300263 struct disasm_line *dl = browser->selection;
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300264 struct symbol *sym = ms->sym;
265 struct annotation *notes;
266 struct symbol *target;
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300267 u64 ip;
268
Arnaldo Carvalho de Melo657bcaf2012-04-15 20:12:07 -0300269 if (strcmp(dl->name, "callq"))
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300270 return false;
271
Arnaldo Carvalho de Melo657bcaf2012-04-15 20:12:07 -0300272 ip = strtoull(dl->operands, NULL, 16);
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300273 ip = ms->map->map_ip(ms->map, ip);
274 target = map__find_symbol(ms->map, ip, NULL);
275 if (target == NULL) {
276 ui_helpline__puts("The called function was not found.");
277 return true;
278 }
279
280 notes = symbol__annotation(target);
281 pthread_mutex_lock(&notes->lock);
282
283 if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
284 pthread_mutex_unlock(&notes->lock);
285 ui__warning("Not enough memory for annotating '%s' symbol!\n",
286 target->name);
287 return true;
288 }
289
290 pthread_mutex_unlock(&notes->lock);
291 symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
292 ui_browser__show_title(&browser->b, sym->name);
293 return true;
294}
295
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300296static
297struct disasm_line *annotate_browser__find_offset(struct annotate_browser *browser,
298 s64 offset, s64 *idx)
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300299{
300 struct map_symbol *ms = browser->b.priv;
301 struct symbol *sym = ms->sym;
302 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300303 struct disasm_line *pos;
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300304
305 *idx = 0;
306 list_for_each_entry(pos, &notes->src->source, node) {
307 if (pos->offset == offset)
308 return pos;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300309 if (!disasm_line__filter(&browser->b, &pos->node))
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300310 ++*idx;
311 }
312
313 return NULL;
314}
315
316static bool annotate_browser__jump(struct annotate_browser *browser)
317{
Arnaldo Carvalho de Melo657bcaf2012-04-15 20:12:07 -0300318 const char *jumps[] = { "je", "jne", "ja", "jmpq", "js", "jmp", NULL };
319 struct disasm_line *dl = browser->selection;
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300320 s64 idx, offset;
Arnaldo Carvalho de Melo657bcaf2012-04-15 20:12:07 -0300321 char *s;
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300322 int i = 0;
323
Arnaldo Carvalho de Melo657bcaf2012-04-15 20:12:07 -0300324 while (jumps[i] && strcmp(dl->name, jumps[i]))
325 ++i;
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300326
Arnaldo Carvalho de Melo657bcaf2012-04-15 20:12:07 -0300327 if (jumps[i] == NULL)
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300328 return false;
329
Arnaldo Carvalho de Melo657bcaf2012-04-15 20:12:07 -0300330 s = strchr(dl->operands, '+');
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300331 if (s++ == NULL) {
332 ui_helpline__puts("Invallid jump instruction.");
333 return true;
334 }
335
336 offset = strtoll(s, NULL, 16);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300337 dl = annotate_browser__find_offset(browser, offset, &idx);
338 if (dl == NULL) {
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300339 ui_helpline__puts("Invallid jump offset");
340 return true;
341 }
342
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300343 annotate_browser__set_top(browser, dl, idx);
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300344
345 return true;
346}
347
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300348static
349struct disasm_line *annotate_browser__find_string(struct annotate_browser *browser,
350 char *s, s64 *idx)
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300351{
352 struct map_symbol *ms = browser->b.priv;
353 struct symbol *sym = ms->sym;
354 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300355 struct disasm_line *pos = browser->selection;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300356
357 *idx = browser->b.index;
358 list_for_each_entry_continue(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300359 if (disasm_line__filter(&browser->b, &pos->node))
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300360 continue;
361
362 ++*idx;
363
364 if (pos->line && strstr(pos->line, s) != NULL)
365 return pos;
366 }
367
368 return NULL;
369}
370
371static bool __annotate_browser__search(struct annotate_browser *browser)
372{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300373 struct disasm_line *dl;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300374 s64 idx;
375
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300376 dl = annotate_browser__find_string(browser, browser->search_bf, &idx);
377 if (dl == NULL) {
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300378 ui_helpline__puts("String not found!");
379 return false;
380 }
381
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300382 annotate_browser__set_top(browser, dl, idx);
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300383 browser->searching_backwards = false;
384 return true;
385}
386
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300387static
388struct disasm_line *annotate_browser__find_string_reverse(struct annotate_browser *browser,
389 char *s, s64 *idx)
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300390{
391 struct map_symbol *ms = browser->b.priv;
392 struct symbol *sym = ms->sym;
393 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300394 struct disasm_line *pos = browser->selection;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300395
396 *idx = browser->b.index;
397 list_for_each_entry_continue_reverse(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300398 if (disasm_line__filter(&browser->b, &pos->node))
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300399 continue;
400
401 --*idx;
402
403 if (pos->line && strstr(pos->line, s) != NULL)
404 return pos;
405 }
406
407 return NULL;
408}
409
410static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
411{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300412 struct disasm_line *dl;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300413 s64 idx;
414
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300415 dl = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
416 if (dl == NULL) {
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300417 ui_helpline__puts("String not found!");
418 return false;
419 }
420
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300421 annotate_browser__set_top(browser, dl, idx);
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300422 browser->searching_backwards = true;
423 return true;
424}
425
426static bool annotate_browser__search_window(struct annotate_browser *browser,
427 int delay_secs)
428{
429 if (ui_browser__input_window("Search", "String: ", browser->search_bf,
430 "ENTER: OK, ESC: Cancel",
431 delay_secs * 2) != K_ENTER ||
432 !*browser->search_bf)
433 return false;
434
435 return true;
436}
437
438static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs)
439{
440 if (annotate_browser__search_window(browser, delay_secs))
441 return __annotate_browser__search(browser);
442
443 return false;
444}
445
446static bool annotate_browser__continue_search(struct annotate_browser *browser,
447 int delay_secs)
448{
449 if (!*browser->search_bf)
450 return annotate_browser__search(browser, delay_secs);
451
452 return __annotate_browser__search(browser);
453}
454
455static bool annotate_browser__search_reverse(struct annotate_browser *browser,
456 int delay_secs)
457{
458 if (annotate_browser__search_window(browser, delay_secs))
459 return __annotate_browser__search_reverse(browser);
460
461 return false;
462}
463
464static
465bool annotate_browser__continue_search_reverse(struct annotate_browser *browser,
466 int delay_secs)
467{
468 if (!*browser->search_bf)
469 return annotate_browser__search_reverse(browser, delay_secs);
470
471 return __annotate_browser__search_reverse(browser);
472}
473
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300474static int annotate_browser__run(struct annotate_browser *self, int evidx,
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200475 void(*timer)(void *arg),
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300476 void *arg, int delay_secs)
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300477{
478 struct rb_node *nd = NULL;
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300479 struct map_symbol *ms = self->b.priv;
480 struct symbol *sym = ms->sym;
Namhyung Kim824ac0e2012-02-23 17:46:23 +0900481 const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
482 "H: Go to hottest line, ->/ENTER: Line action, "
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -0300483 "O: Toggle offset view, "
Namhyung Kim824ac0e2012-02-23 17:46:23 +0900484 "S: Toggle source code view";
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300485 int key;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300486
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300487 if (ui_browser__show(&self->b, sym->name, help) < 0)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300488 return -1;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300489
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300490 annotate_browser__calc_percent(self, evidx);
491
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300492 if (self->curr_hot) {
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300493 annotate_browser__set_rb_top(self, self->curr_hot);
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300494 self->b.navkeypressed = false;
495 }
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300496
497 nd = self->curr_hot;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300498
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300499 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300500 key = ui_browser__run(&self->b, delay_secs);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300501
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300502 if (delay_secs != 0) {
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300503 annotate_browser__calc_percent(self, evidx);
504 /*
505 * Current line focus got out of the list of most active
506 * lines, NULL it so that if TAB|UNTAB is pressed, we
507 * move to curr_hot (current hottest line).
508 */
509 if (nd != NULL && RB_EMPTY_NODE(nd))
510 nd = NULL;
511 }
512
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300513 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200514 case K_TIMER:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300515 if (timer != NULL)
516 timer(arg);
517
518 if (delay_secs != 0)
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300519 symbol__annotate_decay_histogram(sym, evidx);
520 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200521 case K_TAB:
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300522 if (nd != NULL) {
523 nd = rb_prev(nd);
524 if (nd == NULL)
525 nd = rb_last(&self->entries);
526 } else
527 nd = self->curr_hot;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300528 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200529 case K_UNTAB:
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300530 if (nd != NULL)
531 nd = rb_next(nd);
532 if (nd == NULL)
533 nd = rb_first(&self->entries);
534 else
535 nd = self->curr_hot;
536 break;
537 case 'H':
Namhyung Kimef7c5372012-02-23 17:46:21 +0900538 case 'h':
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300539 nd = self->curr_hot;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300540 break;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300541 case 'S':
Namhyung Kimef7c5372012-02-23 17:46:21 +0900542 case 's':
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300543 if (annotate_browser__toggle_source(self))
544 ui_helpline__puts(help);
545 continue;
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -0300546 case 'O':
547 case 'o':
548 self->use_offset = !self->use_offset;
549 continue;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300550 case '/':
551 if (annotate_browser__search(self, delay_secs)) {
552show_help:
553 ui_helpline__puts(help);
554 }
555 continue;
556 case 'n':
557 if (self->searching_backwards ?
558 annotate_browser__continue_search_reverse(self, delay_secs) :
559 annotate_browser__continue_search(self, delay_secs))
560 goto show_help;
561 continue;
562 case '?':
563 if (annotate_browser__search_reverse(self, delay_secs))
564 goto show_help;
565 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200566 case K_ENTER:
567 case K_RIGHT:
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300568 if (self->selection == NULL)
Arnaldo Carvalho de Melo234a5372011-10-06 09:45:29 -0300569 ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300570 else if (self->selection->offset == -1)
Arnaldo Carvalho de Melo234a5372011-10-06 09:45:29 -0300571 ui_helpline__puts("Actions are only available for assembly lines.");
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300572 else if (!(annotate_browser__jump(self) ||
573 annotate_browser__callq(self, evidx, timer, arg, delay_secs)))
574 ui_helpline__puts("Actions are only available for the 'callq' and jump instructions.");
Arnaldo Carvalho de Melofe46e642011-10-19 13:18:13 -0200575 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200576 case K_LEFT:
577 case K_ESC:
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300578 case 'q':
579 case CTRL('c'):
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300580 goto out;
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300581 default:
582 continue;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300583 }
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300584
585 if (nd != NULL)
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300586 annotate_browser__set_rb_top(self, nd);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300587 }
588out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300589 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300590 return key;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300591}
592
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200593int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300594 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200595{
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200596 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300597 timer, arg, delay_secs);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200598}
599
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300600int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200601 void(*timer)(void *arg), void *arg,
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300602 int delay_secs)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300603{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300604 struct disasm_line *pos, *n;
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800605 struct annotation *notes;
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300606 struct map_symbol ms = {
607 .map = map,
608 .sym = sym,
609 };
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300610 struct annotate_browser browser = {
611 .b = {
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300612 .refresh = ui_browser__list_head_refresh,
613 .seek = ui_browser__list_head_seek,
614 .write = annotate_browser__write,
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300615 .filter = disasm_line__filter,
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300616 .priv = &ms,
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200617 .use_navkeypressed = true,
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300618 },
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300619 };
620 int ret;
621
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200622 if (sym == NULL)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300623 return -1;
624
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200625 if (map->dso->annotate_warned)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300626 return -1;
627
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300628 if (symbol__annotate(sym, map, sizeof(struct disasm_line_rb_node)) < 0) {
Arnaldo Carvalho de Meloae557952011-10-26 08:00:55 -0200629 ui__error("%s", ui_helpline__last_msg);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300630 return -1;
631 }
632
633 ui_helpline__push("Press <- or ESC to exit");
634
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800635 notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300636 browser.start = map__rip_2objdump(map, sym->start);
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800637
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200638 list_for_each_entry(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300639 struct disasm_line_rb_node *rbpos;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300640 size_t line_len = strlen(pos->line);
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300641
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300642 if (browser.b.width < line_len)
643 browser.b.width = line_len;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300644 rbpos = disasm_line__rb(pos);
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300645 rbpos->idx = browser.nr_entries++;
646 if (pos->offset != -1)
647 rbpos->idx_asm = browser.nr_asm_entries++;
648 else
649 rbpos->idx_asm = -1;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300650 }
651
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300652 browser.b.nr_entries = browser.nr_entries;
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800653 browser.b.entries = &notes->src->source,
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300654 browser.b.width += 18; /* Percentage */
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200655 ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200656 list_for_each_entry_safe(pos, n, &notes->src->source, node) {
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300657 list_del(&pos->node);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300658 disasm_line__free(pos);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300659 }
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300660 return ret;
661}