blob: bdbb54fd05aee62becf98781076a25c173501310 [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 struct disasm_line *dl = browser->selection;
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300319 s64 idx;
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300320
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300321 if (!dl->ins || !ins__is_jump(dl->ins))
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300322 return false;
323
Arnaldo Carvalho de Melo4f9d0322012-04-18 13:58:34 -0300324 dl = annotate_browser__find_offset(browser, dl->target, &idx);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300325 if (dl == NULL) {
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300326 ui_helpline__puts("Invallid jump offset");
327 return true;
328 }
329
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300330 annotate_browser__set_top(browser, dl, idx);
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300331
332 return true;
333}
334
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300335static
336struct disasm_line *annotate_browser__find_string(struct annotate_browser *browser,
337 char *s, s64 *idx)
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300338{
339 struct map_symbol *ms = browser->b.priv;
340 struct symbol *sym = ms->sym;
341 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300342 struct disasm_line *pos = browser->selection;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300343
344 *idx = browser->b.index;
345 list_for_each_entry_continue(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300346 if (disasm_line__filter(&browser->b, &pos->node))
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300347 continue;
348
349 ++*idx;
350
351 if (pos->line && strstr(pos->line, s) != NULL)
352 return pos;
353 }
354
355 return NULL;
356}
357
358static bool __annotate_browser__search(struct annotate_browser *browser)
359{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300360 struct disasm_line *dl;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300361 s64 idx;
362
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300363 dl = annotate_browser__find_string(browser, browser->search_bf, &idx);
364 if (dl == NULL) {
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300365 ui_helpline__puts("String not found!");
366 return false;
367 }
368
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300369 annotate_browser__set_top(browser, dl, idx);
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300370 browser->searching_backwards = false;
371 return true;
372}
373
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300374static
375struct disasm_line *annotate_browser__find_string_reverse(struct annotate_browser *browser,
376 char *s, s64 *idx)
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300377{
378 struct map_symbol *ms = browser->b.priv;
379 struct symbol *sym = ms->sym;
380 struct annotation *notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300381 struct disasm_line *pos = browser->selection;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300382
383 *idx = browser->b.index;
384 list_for_each_entry_continue_reverse(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300385 if (disasm_line__filter(&browser->b, &pos->node))
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300386 continue;
387
388 --*idx;
389
390 if (pos->line && strstr(pos->line, s) != NULL)
391 return pos;
392 }
393
394 return NULL;
395}
396
397static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
398{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300399 struct disasm_line *dl;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300400 s64 idx;
401
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300402 dl = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
403 if (dl == NULL) {
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300404 ui_helpline__puts("String not found!");
405 return false;
406 }
407
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300408 annotate_browser__set_top(browser, dl, idx);
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300409 browser->searching_backwards = true;
410 return true;
411}
412
413static bool annotate_browser__search_window(struct annotate_browser *browser,
414 int delay_secs)
415{
416 if (ui_browser__input_window("Search", "String: ", browser->search_bf,
417 "ENTER: OK, ESC: Cancel",
418 delay_secs * 2) != K_ENTER ||
419 !*browser->search_bf)
420 return false;
421
422 return true;
423}
424
425static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs)
426{
427 if (annotate_browser__search_window(browser, delay_secs))
428 return __annotate_browser__search(browser);
429
430 return false;
431}
432
433static bool annotate_browser__continue_search(struct annotate_browser *browser,
434 int delay_secs)
435{
436 if (!*browser->search_bf)
437 return annotate_browser__search(browser, delay_secs);
438
439 return __annotate_browser__search(browser);
440}
441
442static bool annotate_browser__search_reverse(struct annotate_browser *browser,
443 int delay_secs)
444{
445 if (annotate_browser__search_window(browser, delay_secs))
446 return __annotate_browser__search_reverse(browser);
447
448 return false;
449}
450
451static
452bool annotate_browser__continue_search_reverse(struct annotate_browser *browser,
453 int delay_secs)
454{
455 if (!*browser->search_bf)
456 return annotate_browser__search_reverse(browser, delay_secs);
457
458 return __annotate_browser__search_reverse(browser);
459}
460
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300461static int annotate_browser__run(struct annotate_browser *self, int evidx,
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200462 void(*timer)(void *arg),
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300463 void *arg, int delay_secs)
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300464{
465 struct rb_node *nd = NULL;
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300466 struct map_symbol *ms = self->b.priv;
467 struct symbol *sym = ms->sym;
Namhyung Kim824ac0e2012-02-23 17:46:23 +0900468 const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
469 "H: Go to hottest line, ->/ENTER: Line action, "
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -0300470 "O: Toggle offset view, "
Namhyung Kim824ac0e2012-02-23 17:46:23 +0900471 "S: Toggle source code view";
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300472 int key;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300473
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300474 if (ui_browser__show(&self->b, sym->name, help) < 0)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300475 return -1;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300476
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300477 annotate_browser__calc_percent(self, evidx);
478
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300479 if (self->curr_hot) {
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300480 annotate_browser__set_rb_top(self, self->curr_hot);
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300481 self->b.navkeypressed = false;
482 }
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300483
484 nd = self->curr_hot;
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300485
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300486 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300487 key = ui_browser__run(&self->b, delay_secs);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300488
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300489 if (delay_secs != 0) {
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300490 annotate_browser__calc_percent(self, evidx);
491 /*
492 * Current line focus got out of the list of most active
493 * lines, NULL it so that if TAB|UNTAB is pressed, we
494 * move to curr_hot (current hottest line).
495 */
496 if (nd != NULL && RB_EMPTY_NODE(nd))
497 nd = NULL;
498 }
499
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300500 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200501 case K_TIMER:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300502 if (timer != NULL)
503 timer(arg);
504
505 if (delay_secs != 0)
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300506 symbol__annotate_decay_histogram(sym, evidx);
507 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200508 case K_TAB:
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300509 if (nd != NULL) {
510 nd = rb_prev(nd);
511 if (nd == NULL)
512 nd = rb_last(&self->entries);
513 } else
514 nd = self->curr_hot;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300515 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200516 case K_UNTAB:
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300517 if (nd != NULL)
518 nd = rb_next(nd);
519 if (nd == NULL)
520 nd = rb_first(&self->entries);
521 else
522 nd = self->curr_hot;
523 break;
524 case 'H':
Namhyung Kimef7c5372012-02-23 17:46:21 +0900525 case 'h':
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300526 nd = self->curr_hot;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300527 break;
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300528 case 'S':
Namhyung Kimef7c5372012-02-23 17:46:21 +0900529 case 's':
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300530 if (annotate_browser__toggle_source(self))
531 ui_helpline__puts(help);
532 continue;
Arnaldo Carvalho de Meloe235f3f2012-04-02 13:21:55 -0300533 case 'O':
534 case 'o':
535 self->use_offset = !self->use_offset;
536 continue;
Arnaldo Carvalho de Melod3d1f612012-04-07 17:10:30 -0300537 case '/':
538 if (annotate_browser__search(self, delay_secs)) {
539show_help:
540 ui_helpline__puts(help);
541 }
542 continue;
543 case 'n':
544 if (self->searching_backwards ?
545 annotate_browser__continue_search_reverse(self, delay_secs) :
546 annotate_browser__continue_search(self, delay_secs))
547 goto show_help;
548 continue;
549 case '?':
550 if (annotate_browser__search_reverse(self, delay_secs))
551 goto show_help;
552 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200553 case K_ENTER:
554 case K_RIGHT:
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300555 if (self->selection == NULL)
Arnaldo Carvalho de Melo234a5372011-10-06 09:45:29 -0300556 ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
Arnaldo Carvalho de Melo60521702012-04-02 13:58:33 -0300557 else if (self->selection->offset == -1)
Arnaldo Carvalho de Melo234a5372011-10-06 09:45:29 -0300558 ui_helpline__puts("Actions are only available for assembly lines.");
Arnaldo Carvalho de Melo08be4ee2012-04-03 21:35:35 -0300559 else if (!(annotate_browser__jump(self) ||
560 annotate_browser__callq(self, evidx, timer, arg, delay_secs)))
561 ui_helpline__puts("Actions are only available for the 'callq' and jump instructions.");
Arnaldo Carvalho de Melofe46e642011-10-19 13:18:13 -0200562 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200563 case K_LEFT:
564 case K_ESC:
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300565 case 'q':
566 case CTRL('c'):
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300567 goto out;
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300568 default:
569 continue;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300570 }
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300571
572 if (nd != NULL)
Arnaldo Carvalho de Melob0ffb2c2012-04-03 15:32:45 -0300573 annotate_browser__set_rb_top(self, nd);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300574 }
575out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300576 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300577 return key;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300578}
579
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200580int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300581 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200582{
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200583 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300584 timer, arg, delay_secs);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200585}
586
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300587int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200588 void(*timer)(void *arg), void *arg,
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300589 int delay_secs)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300590{
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300591 struct disasm_line *pos, *n;
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800592 struct annotation *notes;
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300593 struct map_symbol ms = {
594 .map = map,
595 .sym = sym,
596 };
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300597 struct annotate_browser browser = {
598 .b = {
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300599 .refresh = ui_browser__list_head_refresh,
600 .seek = ui_browser__list_head_seek,
601 .write = annotate_browser__write,
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300602 .filter = disasm_line__filter,
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300603 .priv = &ms,
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200604 .use_navkeypressed = true,
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300605 },
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300606 };
607 int ret;
608
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200609 if (sym == NULL)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300610 return -1;
611
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200612 if (map->dso->annotate_warned)
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300613 return -1;
614
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300615 if (symbol__annotate(sym, map, sizeof(struct disasm_line_rb_node)) < 0) {
Arnaldo Carvalho de Meloae557952011-10-26 08:00:55 -0200616 ui__error("%s", ui_helpline__last_msg);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300617 return -1;
618 }
619
620 ui_helpline__push("Press <- or ESC to exit");
621
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800622 notes = symbol__annotation(sym);
Arnaldo Carvalho de Melo058b4cc2012-04-02 12:59:01 -0300623 browser.start = map__rip_2objdump(map, sym->start);
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800624
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200625 list_for_each_entry(pos, &notes->src->source, node) {
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300626 struct disasm_line_rb_node *rbpos;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300627 size_t line_len = strlen(pos->line);
Arnaldo Carvalho de Meloc97cf422011-02-22 12:02:07 -0300628
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300629 if (browser.b.width < line_len)
630 browser.b.width = line_len;
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300631 rbpos = disasm_line__rb(pos);
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300632 rbpos->idx = browser.nr_entries++;
633 if (pos->offset != -1)
634 rbpos->idx_asm = browser.nr_asm_entries++;
635 else
636 rbpos->idx_asm = -1;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300637 }
638
Arnaldo Carvalho de Melo0361fc22011-10-14 12:31:21 -0300639 browser.b.nr_entries = browser.nr_entries;
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800640 browser.b.entries = &notes->src->source,
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300641 browser.b.width += 18; /* Percentage */
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200642 ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200643 list_for_each_entry_safe(pos, n, &notes->src->source, node) {
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300644 list_del(&pos->node);
Arnaldo Carvalho de Melo29ed6e72012-04-15 15:24:39 -0300645 disasm_line__free(pos);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300646 }
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300647 return ret;
648}