xref: /linux/tools/perf/ui/browsers/annotate.c (revision 2402e4a936a02a24772c9823e1fd2085f0e8ec93)
1aca7a94dSNamhyung Kim #include "../../util/util.h"
2aca7a94dSNamhyung Kim #include "../browser.h"
3aca7a94dSNamhyung Kim #include "../helpline.h"
4aca7a94dSNamhyung Kim #include "../libslang.h"
5aca7a94dSNamhyung Kim #include "../ui.h"
6aca7a94dSNamhyung Kim #include "../util.h"
7aca7a94dSNamhyung Kim #include "../../util/annotate.h"
8aca7a94dSNamhyung Kim #include "../../util/hist.h"
9aca7a94dSNamhyung Kim #include "../../util/sort.h"
10aca7a94dSNamhyung Kim #include "../../util/symbol.h"
11aca7a94dSNamhyung Kim #include <pthread.h>
12aca7a94dSNamhyung Kim #include <newt.h>
13aca7a94dSNamhyung Kim 
14b793a401SArnaldo Carvalho de Melo struct browser_disasm_line {
15b793a401SArnaldo Carvalho de Melo 	struct rb_node	rb_node;
16b793a401SArnaldo Carvalho de Melo 	double		percent;
17b793a401SArnaldo Carvalho de Melo 	u32		idx;
18b793a401SArnaldo Carvalho de Melo 	int		idx_asm;
197d5b12f5SArnaldo Carvalho de Melo 	int		jump_sources;
20b793a401SArnaldo Carvalho de Melo };
21b793a401SArnaldo Carvalho de Melo 
22aca7a94dSNamhyung Kim struct annotate_browser {
23aca7a94dSNamhyung Kim 	struct ui_browser b;
24aca7a94dSNamhyung Kim 	struct rb_root	  entries;
25aca7a94dSNamhyung Kim 	struct rb_node	  *curr_hot;
2629ed6e76SArnaldo Carvalho de Melo 	struct disasm_line	  *selection;
27b793a401SArnaldo Carvalho de Melo 	struct disasm_line  **offsets;
28aca7a94dSNamhyung Kim 	u64		    start;
29aca7a94dSNamhyung Kim 	int		    nr_asm_entries;
30aca7a94dSNamhyung Kim 	int		    nr_entries;
31*2402e4a9SArnaldo Carvalho de Melo 	int		    max_jump_sources;
32*2402e4a9SArnaldo Carvalho de Melo 	int		    nr_jumps;
33aca7a94dSNamhyung Kim 	bool		    hide_src_code;
34aca7a94dSNamhyung Kim 	bool		    use_offset;
359d1ef56dSArnaldo Carvalho de Melo 	bool		    jump_arrows;
36*2402e4a9SArnaldo Carvalho de Melo 	bool		    show_nr_jumps;
37aca7a94dSNamhyung Kim 	bool		    searching_backwards;
3883b1f2aaSArnaldo Carvalho de Melo 	u8		    addr_width;
39*2402e4a9SArnaldo Carvalho de Melo 	u8		    jumps_width;
40*2402e4a9SArnaldo Carvalho de Melo 	u8		    target_width;
4183b1f2aaSArnaldo Carvalho de Melo 	u8		    min_addr_width;
4283b1f2aaSArnaldo Carvalho de Melo 	u8		    max_addr_width;
43aca7a94dSNamhyung Kim 	char		    search_bf[128];
44aca7a94dSNamhyung Kim };
45aca7a94dSNamhyung Kim 
46887c0066SArnaldo Carvalho de Melo static inline struct browser_disasm_line *disasm_line__browser(struct disasm_line *dl)
47aca7a94dSNamhyung Kim {
48887c0066SArnaldo Carvalho de Melo 	return (struct browser_disasm_line *)(dl + 1);
49aca7a94dSNamhyung Kim }
50aca7a94dSNamhyung Kim 
5129ed6e76SArnaldo Carvalho de Melo static bool disasm_line__filter(struct ui_browser *browser, void *entry)
52aca7a94dSNamhyung Kim {
53aca7a94dSNamhyung Kim 	struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
54aca7a94dSNamhyung Kim 
55aca7a94dSNamhyung Kim 	if (ab->hide_src_code) {
5629ed6e76SArnaldo Carvalho de Melo 		struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
5729ed6e76SArnaldo Carvalho de Melo 		return dl->offset == -1;
58aca7a94dSNamhyung Kim 	}
59aca7a94dSNamhyung Kim 
60aca7a94dSNamhyung Kim 	return false;
61aca7a94dSNamhyung Kim }
62aca7a94dSNamhyung Kim 
63*2402e4a9SArnaldo Carvalho de Melo static int annotate_browser__jumps_percent_color(struct annotate_browser *browser,
64*2402e4a9SArnaldo Carvalho de Melo 						 int nr, bool current)
65*2402e4a9SArnaldo Carvalho de Melo {
66*2402e4a9SArnaldo Carvalho de Melo 	if (current && (!browser->b.use_navkeypressed || browser->b.navkeypressed))
67*2402e4a9SArnaldo Carvalho de Melo 		return HE_COLORSET_SELECTED;
68*2402e4a9SArnaldo Carvalho de Melo 	if (nr == browser->max_jump_sources)
69*2402e4a9SArnaldo Carvalho de Melo 		return HE_COLORSET_TOP;
70*2402e4a9SArnaldo Carvalho de Melo 	if (nr > 1)
71*2402e4a9SArnaldo Carvalho de Melo 		return HE_COLORSET_MEDIUM;
72*2402e4a9SArnaldo Carvalho de Melo 	return HE_COLORSET_NORMAL;
73*2402e4a9SArnaldo Carvalho de Melo }
74*2402e4a9SArnaldo Carvalho de Melo 
75*2402e4a9SArnaldo Carvalho de Melo static int annotate_browser__set_jumps_percent_color(struct annotate_browser *browser,
76*2402e4a9SArnaldo Carvalho de Melo 						     int nr, bool current)
77*2402e4a9SArnaldo Carvalho de Melo {
78*2402e4a9SArnaldo Carvalho de Melo 	 int color = annotate_browser__jumps_percent_color(browser, nr, current);
79*2402e4a9SArnaldo Carvalho de Melo 	 return ui_browser__set_color(&browser->b, color);
80*2402e4a9SArnaldo Carvalho de Melo }
81*2402e4a9SArnaldo Carvalho de Melo 
82aca7a94dSNamhyung Kim static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
83aca7a94dSNamhyung Kim {
84aca7a94dSNamhyung Kim 	struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
8529ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
86b793a401SArnaldo Carvalho de Melo 	struct browser_disasm_line *bdl = disasm_line__browser(dl);
87aca7a94dSNamhyung Kim 	bool current_entry = ui_browser__is_current_entry(self, row);
88aca7a94dSNamhyung Kim 	bool change_color = (!ab->hide_src_code &&
89aca7a94dSNamhyung Kim 			     (!current_entry || (self->use_navkeypressed &&
90aca7a94dSNamhyung Kim 					         !self->navkeypressed)));
9183b1f2aaSArnaldo Carvalho de Melo 	int width = self->width, printed;
9283b1f2aaSArnaldo Carvalho de Melo 	char bf[256];
93aca7a94dSNamhyung Kim 
940822cc80SArnaldo Carvalho de Melo 	if (dl->offset != -1 && bdl->percent != 0.0) {
95887c0066SArnaldo Carvalho de Melo 		ui_browser__set_percent_color(self, bdl->percent, current_entry);
960822cc80SArnaldo Carvalho de Melo 		slsmg_printf("%6.2f ", bdl->percent);
97aca7a94dSNamhyung Kim 	} else {
98aca7a94dSNamhyung Kim 		ui_browser__set_percent_color(self, 0, current_entry);
990822cc80SArnaldo Carvalho de Melo 		slsmg_write_nstring(" ", 7);
100aca7a94dSNamhyung Kim 	}
101aca7a94dSNamhyung Kim 
102cf2dacc5SArnaldo Carvalho de Melo 	SLsmg_write_char(' ');
103aca7a94dSNamhyung Kim 
104aca7a94dSNamhyung Kim 	/* The scroll bar isn't being used */
105aca7a94dSNamhyung Kim 	if (!self->navkeypressed)
106aca7a94dSNamhyung Kim 		width += 1;
107aca7a94dSNamhyung Kim 
10829ed6e76SArnaldo Carvalho de Melo 	if (!*dl->line)
1090822cc80SArnaldo Carvalho de Melo 		slsmg_write_nstring(" ", width - 7);
11083b1f2aaSArnaldo Carvalho de Melo 	else if (dl->offset == -1) {
11183b1f2aaSArnaldo Carvalho de Melo 		printed = scnprintf(bf, sizeof(bf), "%*s  ",
11283b1f2aaSArnaldo Carvalho de Melo 				    ab->addr_width, " ");
11383b1f2aaSArnaldo Carvalho de Melo 		slsmg_write_nstring(bf, printed);
11483b1f2aaSArnaldo Carvalho de Melo 		slsmg_write_nstring(dl->line, width - printed - 6);
11583b1f2aaSArnaldo Carvalho de Melo 	} else {
11629ed6e76SArnaldo Carvalho de Melo 		u64 addr = dl->offset;
11783b1f2aaSArnaldo Carvalho de Melo 		int color = -1;
118aca7a94dSNamhyung Kim 
119aca7a94dSNamhyung Kim 		if (!ab->use_offset)
120aca7a94dSNamhyung Kim 			addr += ab->start;
121aca7a94dSNamhyung Kim 
12261e04b33SArnaldo Carvalho de Melo 		if (!ab->use_offset) {
123aca7a94dSNamhyung Kim 			printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr);
12461e04b33SArnaldo Carvalho de Melo 		} else {
1257d5b12f5SArnaldo Carvalho de Melo 			if (bdl->jump_sources) {
126*2402e4a9SArnaldo Carvalho de Melo 				if (ab->show_nr_jumps) {
127*2402e4a9SArnaldo Carvalho de Melo 					int prev;
128*2402e4a9SArnaldo Carvalho de Melo 					printed = scnprintf(bf, sizeof(bf), "%*d ",
129*2402e4a9SArnaldo Carvalho de Melo 							    ab->jumps_width,
130*2402e4a9SArnaldo Carvalho de Melo 							    bdl->jump_sources);
131*2402e4a9SArnaldo Carvalho de Melo 					prev = annotate_browser__set_jumps_percent_color(ab, bdl->jump_sources,
132*2402e4a9SArnaldo Carvalho de Melo 											 current_entry);
133*2402e4a9SArnaldo Carvalho de Melo 					slsmg_write_nstring(bf, printed);
134*2402e4a9SArnaldo Carvalho de Melo 					ui_browser__set_color(self, prev);
135*2402e4a9SArnaldo Carvalho de Melo 				}
136*2402e4a9SArnaldo Carvalho de Melo 
13761e04b33SArnaldo Carvalho de Melo 				printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ",
138*2402e4a9SArnaldo Carvalho de Melo 						    ab->target_width, addr);
13961e04b33SArnaldo Carvalho de Melo 			} else {
14061e04b33SArnaldo Carvalho de Melo 				printed = scnprintf(bf, sizeof(bf), "%*s  ",
14183b1f2aaSArnaldo Carvalho de Melo 						    ab->addr_width, " ");
14261e04b33SArnaldo Carvalho de Melo 			}
14361e04b33SArnaldo Carvalho de Melo 		}
144b793a401SArnaldo Carvalho de Melo 
145aca7a94dSNamhyung Kim 		if (change_color)
146aca7a94dSNamhyung Kim 			color = ui_browser__set_color(self, HE_COLORSET_ADDR);
147aca7a94dSNamhyung Kim 		slsmg_write_nstring(bf, printed);
148aca7a94dSNamhyung Kim 		if (change_color)
149aca7a94dSNamhyung Kim 			ui_browser__set_color(self, color);
15028548d78SArnaldo Carvalho de Melo 		if (dl->ins && dl->ins->ops->scnprintf) {
15151a0d455SArnaldo Carvalho de Melo 			if (ins__is_jump(dl->ins)) {
15244d1a3edSArnaldo Carvalho de Melo 				bool fwd = dl->ops.target.offset > (u64)dl->offset;
15351a0d455SArnaldo Carvalho de Melo 
15459d038d5SArnaldo Carvalho de Melo 				ui_browser__write_graph(self, fwd ? SLSMG_DARROW_CHAR :
15551a0d455SArnaldo Carvalho de Melo 								    SLSMG_UARROW_CHAR);
15651a0d455SArnaldo Carvalho de Melo 				SLsmg_write_char(' ');
15788298f5aSArnaldo Carvalho de Melo 			} else if (ins__is_call(dl->ins)) {
15888298f5aSArnaldo Carvalho de Melo 				ui_browser__write_graph(self, SLSMG_RARROW_CHAR);
15988298f5aSArnaldo Carvalho de Melo 				SLsmg_write_char(' ');
16051a0d455SArnaldo Carvalho de Melo 			} else {
16151a0d455SArnaldo Carvalho de Melo 				slsmg_write_nstring(" ", 2);
16251a0d455SArnaldo Carvalho de Melo 			}
1634ea08b52SArnaldo Carvalho de Melo 		} else {
1644ea08b52SArnaldo Carvalho de Melo 			if (strcmp(dl->name, "retq")) {
1654ea08b52SArnaldo Carvalho de Melo 				slsmg_write_nstring(" ", 2);
1664ea08b52SArnaldo Carvalho de Melo 			} else {
16759d038d5SArnaldo Carvalho de Melo 				ui_browser__write_graph(self, SLSMG_LARROW_CHAR);
1684ea08b52SArnaldo Carvalho de Melo 				SLsmg_write_char(' ');
1694ea08b52SArnaldo Carvalho de Melo 			}
1704ea08b52SArnaldo Carvalho de Melo 		}
1714ea08b52SArnaldo Carvalho de Melo 
1725417072bSArnaldo Carvalho de Melo 		disasm_line__scnprintf(dl, bf, sizeof(bf), !ab->use_offset);
17383b1f2aaSArnaldo Carvalho de Melo 		slsmg_write_nstring(bf, width - 10 - printed);
174aca7a94dSNamhyung Kim 	}
175aca7a94dSNamhyung Kim 
176aca7a94dSNamhyung Kim 	if (current_entry)
17729ed6e76SArnaldo Carvalho de Melo 		ab->selection = dl;
178aca7a94dSNamhyung Kim }
179aca7a94dSNamhyung Kim 
1809d1ef56dSArnaldo Carvalho de Melo static void annotate_browser__draw_current_jump(struct ui_browser *browser)
181a3f895beSArnaldo Carvalho de Melo {
182a3f895beSArnaldo Carvalho de Melo 	struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
1839d1ef56dSArnaldo Carvalho de Melo 	struct disasm_line *cursor = ab->selection, *target;
1849d1ef56dSArnaldo Carvalho de Melo 	struct browser_disasm_line *btarget, *bcursor;
18583b1f2aaSArnaldo Carvalho de Melo 	unsigned int from, to;
186a3f895beSArnaldo Carvalho de Melo 
1879d1ef56dSArnaldo Carvalho de Melo 	if (!cursor->ins || !ins__is_jump(cursor->ins) ||
1889d1ef56dSArnaldo Carvalho de Melo 	    !disasm_line__has_offset(cursor))
189a3f895beSArnaldo Carvalho de Melo 		return;
190a3f895beSArnaldo Carvalho de Melo 
1919d1ef56dSArnaldo Carvalho de Melo 	target = ab->offsets[cursor->ops.target.offset];
1929d1ef56dSArnaldo Carvalho de Melo 	if (!target)
1939d1ef56dSArnaldo Carvalho de Melo 		return;
1949d1ef56dSArnaldo Carvalho de Melo 
1959d1ef56dSArnaldo Carvalho de Melo 	bcursor = disasm_line__browser(cursor);
1969d1ef56dSArnaldo Carvalho de Melo 	btarget = disasm_line__browser(target);
1979d1ef56dSArnaldo Carvalho de Melo 
198a3f895beSArnaldo Carvalho de Melo 	if (ab->hide_src_code) {
1999d1ef56dSArnaldo Carvalho de Melo 		from = bcursor->idx_asm;
200a3f895beSArnaldo Carvalho de Melo 		to = btarget->idx_asm;
201a3f895beSArnaldo Carvalho de Melo 	} else {
2029d1ef56dSArnaldo Carvalho de Melo 		from = (u64)bcursor->idx;
203a3f895beSArnaldo Carvalho de Melo 		to = (u64)btarget->idx;
204a3f895beSArnaldo Carvalho de Melo 	}
205a3f895beSArnaldo Carvalho de Melo 
206a3f895beSArnaldo Carvalho de Melo 	ui_browser__set_color(browser, HE_COLORSET_CODE);
20783b1f2aaSArnaldo Carvalho de Melo 	__ui_browser__line_arrow(browser, 9 + ab->addr_width, from, to);
208a3f895beSArnaldo Carvalho de Melo }
209a3f895beSArnaldo Carvalho de Melo 
210a3f895beSArnaldo Carvalho de Melo static unsigned int annotate_browser__refresh(struct ui_browser *browser)
211a3f895beSArnaldo Carvalho de Melo {
2129d1ef56dSArnaldo Carvalho de Melo 	struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
213a3f895beSArnaldo Carvalho de Melo 	int ret = ui_browser__list_head_refresh(browser);
214a3f895beSArnaldo Carvalho de Melo 
2159d1ef56dSArnaldo Carvalho de Melo 	if (ab->jump_arrows)
2169d1ef56dSArnaldo Carvalho de Melo 		annotate_browser__draw_current_jump(browser);
217a3f895beSArnaldo Carvalho de Melo 
21883b1f2aaSArnaldo Carvalho de Melo 	ui_browser__set_color(browser, HE_COLORSET_NORMAL);
21983b1f2aaSArnaldo Carvalho de Melo 	__ui_browser__vline(browser, 7, 0, browser->height - 1);
220a3f895beSArnaldo Carvalho de Melo 	return ret;
221a3f895beSArnaldo Carvalho de Melo }
222a3f895beSArnaldo Carvalho de Melo 
22329ed6e76SArnaldo Carvalho de Melo static double disasm_line__calc_percent(struct disasm_line *dl, struct symbol *sym, int evidx)
224aca7a94dSNamhyung Kim {
225aca7a94dSNamhyung Kim 	double percent = 0.0;
226aca7a94dSNamhyung Kim 
22729ed6e76SArnaldo Carvalho de Melo 	if (dl->offset != -1) {
228aca7a94dSNamhyung Kim 		int len = sym->end - sym->start;
229aca7a94dSNamhyung Kim 		unsigned int hits = 0;
230aca7a94dSNamhyung Kim 		struct annotation *notes = symbol__annotation(sym);
231aca7a94dSNamhyung Kim 		struct source_line *src_line = notes->src->lines;
232aca7a94dSNamhyung Kim 		struct sym_hist *h = annotation__histogram(notes, evidx);
23329ed6e76SArnaldo Carvalho de Melo 		s64 offset = dl->offset;
23429ed6e76SArnaldo Carvalho de Melo 		struct disasm_line *next;
235aca7a94dSNamhyung Kim 
23629ed6e76SArnaldo Carvalho de Melo 		next = disasm__get_next_ip_line(&notes->src->source, dl);
237aca7a94dSNamhyung Kim 		while (offset < (s64)len &&
238aca7a94dSNamhyung Kim 		       (next == NULL || offset < next->offset)) {
239aca7a94dSNamhyung Kim 			if (src_line) {
240aca7a94dSNamhyung Kim 				percent += src_line[offset].percent;
241aca7a94dSNamhyung Kim 			} else
242aca7a94dSNamhyung Kim 				hits += h->addr[offset];
243aca7a94dSNamhyung Kim 
244aca7a94dSNamhyung Kim 			++offset;
245aca7a94dSNamhyung Kim 		}
246aca7a94dSNamhyung Kim 		/*
247aca7a94dSNamhyung Kim  		 * If the percentage wasn't already calculated in
248aca7a94dSNamhyung Kim  		 * symbol__get_source_line, do it now:
249aca7a94dSNamhyung Kim  		 */
250aca7a94dSNamhyung Kim 		if (src_line == NULL && h->sum)
251aca7a94dSNamhyung Kim 			percent = 100.0 * hits / h->sum;
252aca7a94dSNamhyung Kim 	}
253aca7a94dSNamhyung Kim 
254aca7a94dSNamhyung Kim 	return percent;
255aca7a94dSNamhyung Kim }
256aca7a94dSNamhyung Kim 
257887c0066SArnaldo Carvalho de Melo static void disasm_rb_tree__insert(struct rb_root *root, struct browser_disasm_line *bdl)
258aca7a94dSNamhyung Kim {
25929ed6e76SArnaldo Carvalho de Melo 	struct rb_node **p = &root->rb_node;
260aca7a94dSNamhyung Kim 	struct rb_node *parent = NULL;
261887c0066SArnaldo Carvalho de Melo 	struct browser_disasm_line *l;
262aca7a94dSNamhyung Kim 
263aca7a94dSNamhyung Kim 	while (*p != NULL) {
264aca7a94dSNamhyung Kim 		parent = *p;
265887c0066SArnaldo Carvalho de Melo 		l = rb_entry(parent, struct browser_disasm_line, rb_node);
266887c0066SArnaldo Carvalho de Melo 		if (bdl->percent < l->percent)
267aca7a94dSNamhyung Kim 			p = &(*p)->rb_left;
268aca7a94dSNamhyung Kim 		else
269aca7a94dSNamhyung Kim 			p = &(*p)->rb_right;
270aca7a94dSNamhyung Kim 	}
271887c0066SArnaldo Carvalho de Melo 	rb_link_node(&bdl->rb_node, parent, p);
272887c0066SArnaldo Carvalho de Melo 	rb_insert_color(&bdl->rb_node, root);
273aca7a94dSNamhyung Kim }
274aca7a94dSNamhyung Kim 
275aca7a94dSNamhyung Kim static void annotate_browser__set_top(struct annotate_browser *self,
27629ed6e76SArnaldo Carvalho de Melo 				      struct disasm_line *pos, u32 idx)
277aca7a94dSNamhyung Kim {
278aca7a94dSNamhyung Kim 	unsigned back;
279aca7a94dSNamhyung Kim 
280aca7a94dSNamhyung Kim 	ui_browser__refresh_dimensions(&self->b);
281aca7a94dSNamhyung Kim 	back = self->b.height / 2;
282aca7a94dSNamhyung Kim 	self->b.top_idx = self->b.index = idx;
283aca7a94dSNamhyung Kim 
284aca7a94dSNamhyung Kim 	while (self->b.top_idx != 0 && back != 0) {
28529ed6e76SArnaldo Carvalho de Melo 		pos = list_entry(pos->node.prev, struct disasm_line, node);
286aca7a94dSNamhyung Kim 
28729ed6e76SArnaldo Carvalho de Melo 		if (disasm_line__filter(&self->b, &pos->node))
288aca7a94dSNamhyung Kim 			continue;
289aca7a94dSNamhyung Kim 
290aca7a94dSNamhyung Kim 		--self->b.top_idx;
291aca7a94dSNamhyung Kim 		--back;
292aca7a94dSNamhyung Kim 	}
293aca7a94dSNamhyung Kim 
294aca7a94dSNamhyung Kim 	self->b.top = pos;
295aca7a94dSNamhyung Kim 	self->b.navkeypressed = true;
296aca7a94dSNamhyung Kim }
297aca7a94dSNamhyung Kim 
298aca7a94dSNamhyung Kim static void annotate_browser__set_rb_top(struct annotate_browser *browser,
299aca7a94dSNamhyung Kim 					 struct rb_node *nd)
300aca7a94dSNamhyung Kim {
301887c0066SArnaldo Carvalho de Melo 	struct browser_disasm_line *bpos;
30229ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos;
303aca7a94dSNamhyung Kim 
304887c0066SArnaldo Carvalho de Melo 	bpos = rb_entry(nd, struct browser_disasm_line, rb_node);
305887c0066SArnaldo Carvalho de Melo 	pos = ((struct disasm_line *)bpos) - 1;
306887c0066SArnaldo Carvalho de Melo 	annotate_browser__set_top(browser, pos, bpos->idx);
307aca7a94dSNamhyung Kim 	browser->curr_hot = nd;
308aca7a94dSNamhyung Kim }
309aca7a94dSNamhyung Kim 
310aca7a94dSNamhyung Kim static void annotate_browser__calc_percent(struct annotate_browser *browser,
311aca7a94dSNamhyung Kim 					   int evidx)
312aca7a94dSNamhyung Kim {
313aca7a94dSNamhyung Kim 	struct map_symbol *ms = browser->b.priv;
314aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
315aca7a94dSNamhyung Kim 	struct annotation *notes = symbol__annotation(sym);
31629ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos;
317aca7a94dSNamhyung Kim 
318aca7a94dSNamhyung Kim 	browser->entries = RB_ROOT;
319aca7a94dSNamhyung Kim 
320aca7a94dSNamhyung Kim 	pthread_mutex_lock(&notes->lock);
321aca7a94dSNamhyung Kim 
322aca7a94dSNamhyung Kim 	list_for_each_entry(pos, &notes->src->source, node) {
323887c0066SArnaldo Carvalho de Melo 		struct browser_disasm_line *bpos = disasm_line__browser(pos);
324887c0066SArnaldo Carvalho de Melo 		bpos->percent = disasm_line__calc_percent(pos, sym, evidx);
325887c0066SArnaldo Carvalho de Melo 		if (bpos->percent < 0.01) {
326887c0066SArnaldo Carvalho de Melo 			RB_CLEAR_NODE(&bpos->rb_node);
327aca7a94dSNamhyung Kim 			continue;
328aca7a94dSNamhyung Kim 		}
329887c0066SArnaldo Carvalho de Melo 		disasm_rb_tree__insert(&browser->entries, bpos);
330aca7a94dSNamhyung Kim 	}
331aca7a94dSNamhyung Kim 	pthread_mutex_unlock(&notes->lock);
332aca7a94dSNamhyung Kim 
333aca7a94dSNamhyung Kim 	browser->curr_hot = rb_last(&browser->entries);
334aca7a94dSNamhyung Kim }
335aca7a94dSNamhyung Kim 
336aca7a94dSNamhyung Kim static bool annotate_browser__toggle_source(struct annotate_browser *browser)
337aca7a94dSNamhyung Kim {
33829ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *dl;
339887c0066SArnaldo Carvalho de Melo 	struct browser_disasm_line *bdl;
340aca7a94dSNamhyung Kim 	off_t offset = browser->b.index - browser->b.top_idx;
341aca7a94dSNamhyung Kim 
342aca7a94dSNamhyung Kim 	browser->b.seek(&browser->b, offset, SEEK_CUR);
34329ed6e76SArnaldo Carvalho de Melo 	dl = list_entry(browser->b.top, struct disasm_line, node);
344887c0066SArnaldo Carvalho de Melo 	bdl = disasm_line__browser(dl);
345aca7a94dSNamhyung Kim 
346aca7a94dSNamhyung Kim 	if (browser->hide_src_code) {
347887c0066SArnaldo Carvalho de Melo 		if (bdl->idx_asm < offset)
348887c0066SArnaldo Carvalho de Melo 			offset = bdl->idx;
349aca7a94dSNamhyung Kim 
350aca7a94dSNamhyung Kim 		browser->b.nr_entries = browser->nr_entries;
351aca7a94dSNamhyung Kim 		browser->hide_src_code = false;
352aca7a94dSNamhyung Kim 		browser->b.seek(&browser->b, -offset, SEEK_CUR);
353887c0066SArnaldo Carvalho de Melo 		browser->b.top_idx = bdl->idx - offset;
354887c0066SArnaldo Carvalho de Melo 		browser->b.index = bdl->idx;
355aca7a94dSNamhyung Kim 	} else {
356887c0066SArnaldo Carvalho de Melo 		if (bdl->idx_asm < 0) {
357aca7a94dSNamhyung Kim 			ui_helpline__puts("Only available for assembly lines.");
358aca7a94dSNamhyung Kim 			browser->b.seek(&browser->b, -offset, SEEK_CUR);
359aca7a94dSNamhyung Kim 			return false;
360aca7a94dSNamhyung Kim 		}
361aca7a94dSNamhyung Kim 
362887c0066SArnaldo Carvalho de Melo 		if (bdl->idx_asm < offset)
363887c0066SArnaldo Carvalho de Melo 			offset = bdl->idx_asm;
364aca7a94dSNamhyung Kim 
365aca7a94dSNamhyung Kim 		browser->b.nr_entries = browser->nr_asm_entries;
366aca7a94dSNamhyung Kim 		browser->hide_src_code = true;
367aca7a94dSNamhyung Kim 		browser->b.seek(&browser->b, -offset, SEEK_CUR);
368887c0066SArnaldo Carvalho de Melo 		browser->b.top_idx = bdl->idx_asm - offset;
369887c0066SArnaldo Carvalho de Melo 		browser->b.index = bdl->idx_asm;
370aca7a94dSNamhyung Kim 	}
371aca7a94dSNamhyung Kim 
372aca7a94dSNamhyung Kim 	return true;
373aca7a94dSNamhyung Kim }
374aca7a94dSNamhyung Kim 
375aca7a94dSNamhyung Kim static bool annotate_browser__callq(struct annotate_browser *browser,
376aca7a94dSNamhyung Kim 				    int evidx, void (*timer)(void *arg),
377aca7a94dSNamhyung Kim 				    void *arg, int delay_secs)
378aca7a94dSNamhyung Kim {
379aca7a94dSNamhyung Kim 	struct map_symbol *ms = browser->b.priv;
380657bcaf5SArnaldo Carvalho de Melo 	struct disasm_line *dl = browser->selection;
381aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
382aca7a94dSNamhyung Kim 	struct annotation *notes;
383aca7a94dSNamhyung Kim 	struct symbol *target;
384aca7a94dSNamhyung Kim 	u64 ip;
385aca7a94dSNamhyung Kim 
386d86b0597SArnaldo Carvalho de Melo 	if (!ins__is_call(dl->ins))
387aca7a94dSNamhyung Kim 		return false;
388aca7a94dSNamhyung Kim 
38944d1a3edSArnaldo Carvalho de Melo 	ip = ms->map->map_ip(ms->map, dl->ops.target.addr);
390aca7a94dSNamhyung Kim 	target = map__find_symbol(ms->map, ip, NULL);
391aca7a94dSNamhyung Kim 	if (target == NULL) {
392aca7a94dSNamhyung Kim 		ui_helpline__puts("The called function was not found.");
393aca7a94dSNamhyung Kim 		return true;
394aca7a94dSNamhyung Kim 	}
395aca7a94dSNamhyung Kim 
396aca7a94dSNamhyung Kim 	notes = symbol__annotation(target);
397aca7a94dSNamhyung Kim 	pthread_mutex_lock(&notes->lock);
398aca7a94dSNamhyung Kim 
399aca7a94dSNamhyung Kim 	if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
400aca7a94dSNamhyung Kim 		pthread_mutex_unlock(&notes->lock);
401aca7a94dSNamhyung Kim 		ui__warning("Not enough memory for annotating '%s' symbol!\n",
402aca7a94dSNamhyung Kim 			    target->name);
403aca7a94dSNamhyung Kim 		return true;
404aca7a94dSNamhyung Kim 	}
405aca7a94dSNamhyung Kim 
406aca7a94dSNamhyung Kim 	pthread_mutex_unlock(&notes->lock);
407aca7a94dSNamhyung Kim 	symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
408aca7a94dSNamhyung Kim 	ui_browser__show_title(&browser->b, sym->name);
409aca7a94dSNamhyung Kim 	return true;
410aca7a94dSNamhyung Kim }
411aca7a94dSNamhyung Kim 
41229ed6e76SArnaldo Carvalho de Melo static
41329ed6e76SArnaldo Carvalho de Melo struct disasm_line *annotate_browser__find_offset(struct annotate_browser *browser,
414aca7a94dSNamhyung Kim 					  s64 offset, s64 *idx)
415aca7a94dSNamhyung Kim {
416aca7a94dSNamhyung Kim 	struct map_symbol *ms = browser->b.priv;
417aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
418aca7a94dSNamhyung Kim 	struct annotation *notes = symbol__annotation(sym);
41929ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos;
420aca7a94dSNamhyung Kim 
421aca7a94dSNamhyung Kim 	*idx = 0;
422aca7a94dSNamhyung Kim 	list_for_each_entry(pos, &notes->src->source, node) {
423aca7a94dSNamhyung Kim 		if (pos->offset == offset)
424aca7a94dSNamhyung Kim 			return pos;
42529ed6e76SArnaldo Carvalho de Melo 		if (!disasm_line__filter(&browser->b, &pos->node))
426aca7a94dSNamhyung Kim 			++*idx;
427aca7a94dSNamhyung Kim 	}
428aca7a94dSNamhyung Kim 
429aca7a94dSNamhyung Kim 	return NULL;
430aca7a94dSNamhyung Kim }
431aca7a94dSNamhyung Kim 
432aca7a94dSNamhyung Kim static bool annotate_browser__jump(struct annotate_browser *browser)
433aca7a94dSNamhyung Kim {
434657bcaf5SArnaldo Carvalho de Melo 	struct disasm_line *dl = browser->selection;
4354f9d0325SArnaldo Carvalho de Melo 	s64 idx;
436aca7a94dSNamhyung Kim 
437d86b0597SArnaldo Carvalho de Melo 	if (!ins__is_jump(dl->ins))
438aca7a94dSNamhyung Kim 		return false;
439aca7a94dSNamhyung Kim 
44044d1a3edSArnaldo Carvalho de Melo 	dl = annotate_browser__find_offset(browser, dl->ops.target.offset, &idx);
44129ed6e76SArnaldo Carvalho de Melo 	if (dl == NULL) {
442aca7a94dSNamhyung Kim 		ui_helpline__puts("Invallid jump offset");
443aca7a94dSNamhyung Kim 		return true;
444aca7a94dSNamhyung Kim 	}
445aca7a94dSNamhyung Kim 
44629ed6e76SArnaldo Carvalho de Melo 	annotate_browser__set_top(browser, dl, idx);
447aca7a94dSNamhyung Kim 
448aca7a94dSNamhyung Kim 	return true;
449aca7a94dSNamhyung Kim }
450aca7a94dSNamhyung Kim 
45129ed6e76SArnaldo Carvalho de Melo static
45229ed6e76SArnaldo Carvalho de Melo struct disasm_line *annotate_browser__find_string(struct annotate_browser *browser,
453aca7a94dSNamhyung Kim 					  char *s, s64 *idx)
454aca7a94dSNamhyung Kim {
455aca7a94dSNamhyung Kim 	struct map_symbol *ms = browser->b.priv;
456aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
457aca7a94dSNamhyung Kim 	struct annotation *notes = symbol__annotation(sym);
45829ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos = browser->selection;
459aca7a94dSNamhyung Kim 
460aca7a94dSNamhyung Kim 	*idx = browser->b.index;
461aca7a94dSNamhyung Kim 	list_for_each_entry_continue(pos, &notes->src->source, node) {
46229ed6e76SArnaldo Carvalho de Melo 		if (disasm_line__filter(&browser->b, &pos->node))
463aca7a94dSNamhyung Kim 			continue;
464aca7a94dSNamhyung Kim 
465aca7a94dSNamhyung Kim 		++*idx;
466aca7a94dSNamhyung Kim 
467aca7a94dSNamhyung Kim 		if (pos->line && strstr(pos->line, s) != NULL)
468aca7a94dSNamhyung Kim 			return pos;
469aca7a94dSNamhyung Kim 	}
470aca7a94dSNamhyung Kim 
471aca7a94dSNamhyung Kim 	return NULL;
472aca7a94dSNamhyung Kim }
473aca7a94dSNamhyung Kim 
474aca7a94dSNamhyung Kim static bool __annotate_browser__search(struct annotate_browser *browser)
475aca7a94dSNamhyung Kim {
47629ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *dl;
477aca7a94dSNamhyung Kim 	s64 idx;
478aca7a94dSNamhyung Kim 
47929ed6e76SArnaldo Carvalho de Melo 	dl = annotate_browser__find_string(browser, browser->search_bf, &idx);
48029ed6e76SArnaldo Carvalho de Melo 	if (dl == NULL) {
481aca7a94dSNamhyung Kim 		ui_helpline__puts("String not found!");
482aca7a94dSNamhyung Kim 		return false;
483aca7a94dSNamhyung Kim 	}
484aca7a94dSNamhyung Kim 
48529ed6e76SArnaldo Carvalho de Melo 	annotate_browser__set_top(browser, dl, idx);
486aca7a94dSNamhyung Kim 	browser->searching_backwards = false;
487aca7a94dSNamhyung Kim 	return true;
488aca7a94dSNamhyung Kim }
489aca7a94dSNamhyung Kim 
49029ed6e76SArnaldo Carvalho de Melo static
49129ed6e76SArnaldo Carvalho de Melo struct disasm_line *annotate_browser__find_string_reverse(struct annotate_browser *browser,
492aca7a94dSNamhyung Kim 						  char *s, s64 *idx)
493aca7a94dSNamhyung Kim {
494aca7a94dSNamhyung Kim 	struct map_symbol *ms = browser->b.priv;
495aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
496aca7a94dSNamhyung Kim 	struct annotation *notes = symbol__annotation(sym);
49729ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos = browser->selection;
498aca7a94dSNamhyung Kim 
499aca7a94dSNamhyung Kim 	*idx = browser->b.index;
500aca7a94dSNamhyung Kim 	list_for_each_entry_continue_reverse(pos, &notes->src->source, node) {
50129ed6e76SArnaldo Carvalho de Melo 		if (disasm_line__filter(&browser->b, &pos->node))
502aca7a94dSNamhyung Kim 			continue;
503aca7a94dSNamhyung Kim 
504aca7a94dSNamhyung Kim 		--*idx;
505aca7a94dSNamhyung Kim 
506aca7a94dSNamhyung Kim 		if (pos->line && strstr(pos->line, s) != NULL)
507aca7a94dSNamhyung Kim 			return pos;
508aca7a94dSNamhyung Kim 	}
509aca7a94dSNamhyung Kim 
510aca7a94dSNamhyung Kim 	return NULL;
511aca7a94dSNamhyung Kim }
512aca7a94dSNamhyung Kim 
513aca7a94dSNamhyung Kim static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
514aca7a94dSNamhyung Kim {
51529ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *dl;
516aca7a94dSNamhyung Kim 	s64 idx;
517aca7a94dSNamhyung Kim 
51829ed6e76SArnaldo Carvalho de Melo 	dl = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
51929ed6e76SArnaldo Carvalho de Melo 	if (dl == NULL) {
520aca7a94dSNamhyung Kim 		ui_helpline__puts("String not found!");
521aca7a94dSNamhyung Kim 		return false;
522aca7a94dSNamhyung Kim 	}
523aca7a94dSNamhyung Kim 
52429ed6e76SArnaldo Carvalho de Melo 	annotate_browser__set_top(browser, dl, idx);
525aca7a94dSNamhyung Kim 	browser->searching_backwards = true;
526aca7a94dSNamhyung Kim 	return true;
527aca7a94dSNamhyung Kim }
528aca7a94dSNamhyung Kim 
529aca7a94dSNamhyung Kim static bool annotate_browser__search_window(struct annotate_browser *browser,
530aca7a94dSNamhyung Kim 					    int delay_secs)
531aca7a94dSNamhyung Kim {
532aca7a94dSNamhyung Kim 	if (ui_browser__input_window("Search", "String: ", browser->search_bf,
533aca7a94dSNamhyung Kim 				     "ENTER: OK, ESC: Cancel",
534aca7a94dSNamhyung Kim 				     delay_secs * 2) != K_ENTER ||
535aca7a94dSNamhyung Kim 	    !*browser->search_bf)
536aca7a94dSNamhyung Kim 		return false;
537aca7a94dSNamhyung Kim 
538aca7a94dSNamhyung Kim 	return true;
539aca7a94dSNamhyung Kim }
540aca7a94dSNamhyung Kim 
541aca7a94dSNamhyung Kim static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs)
542aca7a94dSNamhyung Kim {
543aca7a94dSNamhyung Kim 	if (annotate_browser__search_window(browser, delay_secs))
544aca7a94dSNamhyung Kim 		return __annotate_browser__search(browser);
545aca7a94dSNamhyung Kim 
546aca7a94dSNamhyung Kim 	return false;
547aca7a94dSNamhyung Kim }
548aca7a94dSNamhyung Kim 
549aca7a94dSNamhyung Kim static bool annotate_browser__continue_search(struct annotate_browser *browser,
550aca7a94dSNamhyung Kim 					      int delay_secs)
551aca7a94dSNamhyung Kim {
552aca7a94dSNamhyung Kim 	if (!*browser->search_bf)
553aca7a94dSNamhyung Kim 		return annotate_browser__search(browser, delay_secs);
554aca7a94dSNamhyung Kim 
555aca7a94dSNamhyung Kim 	return __annotate_browser__search(browser);
556aca7a94dSNamhyung Kim }
557aca7a94dSNamhyung Kim 
558aca7a94dSNamhyung Kim static bool annotate_browser__search_reverse(struct annotate_browser *browser,
559aca7a94dSNamhyung Kim 					   int delay_secs)
560aca7a94dSNamhyung Kim {
561aca7a94dSNamhyung Kim 	if (annotate_browser__search_window(browser, delay_secs))
562aca7a94dSNamhyung Kim 		return __annotate_browser__search_reverse(browser);
563aca7a94dSNamhyung Kim 
564aca7a94dSNamhyung Kim 	return false;
565aca7a94dSNamhyung Kim }
566aca7a94dSNamhyung Kim 
567aca7a94dSNamhyung Kim static
568aca7a94dSNamhyung Kim bool annotate_browser__continue_search_reverse(struct annotate_browser *browser,
569aca7a94dSNamhyung Kim 					       int delay_secs)
570aca7a94dSNamhyung Kim {
571aca7a94dSNamhyung Kim 	if (!*browser->search_bf)
572aca7a94dSNamhyung Kim 		return annotate_browser__search_reverse(browser, delay_secs);
573aca7a94dSNamhyung Kim 
574aca7a94dSNamhyung Kim 	return __annotate_browser__search_reverse(browser);
575aca7a94dSNamhyung Kim }
576aca7a94dSNamhyung Kim 
577aca7a94dSNamhyung Kim static int annotate_browser__run(struct annotate_browser *self, int evidx,
578aca7a94dSNamhyung Kim 				 void(*timer)(void *arg),
579aca7a94dSNamhyung Kim 				 void *arg, int delay_secs)
580aca7a94dSNamhyung Kim {
581aca7a94dSNamhyung Kim 	struct rb_node *nd = NULL;
582aca7a94dSNamhyung Kim 	struct map_symbol *ms = self->b.priv;
583aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
584aca7a94dSNamhyung Kim 	const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
585f6c1be27SDavid Ahern 			   "H: Hottest line, ->/ENTER: Line action, "
586f6c1be27SDavid Ahern 			   "O: Offset view, "
587f6c1be27SDavid Ahern 			   "S: Source view";
588aca7a94dSNamhyung Kim 	int key;
589aca7a94dSNamhyung Kim 
590aca7a94dSNamhyung Kim 	if (ui_browser__show(&self->b, sym->name, help) < 0)
591aca7a94dSNamhyung Kim 		return -1;
592aca7a94dSNamhyung Kim 
593aca7a94dSNamhyung Kim 	annotate_browser__calc_percent(self, evidx);
594aca7a94dSNamhyung Kim 
595aca7a94dSNamhyung Kim 	if (self->curr_hot) {
596aca7a94dSNamhyung Kim 		annotate_browser__set_rb_top(self, self->curr_hot);
597aca7a94dSNamhyung Kim 		self->b.navkeypressed = false;
598aca7a94dSNamhyung Kim 	}
599aca7a94dSNamhyung Kim 
600aca7a94dSNamhyung Kim 	nd = self->curr_hot;
601aca7a94dSNamhyung Kim 
602aca7a94dSNamhyung Kim 	while (1) {
603aca7a94dSNamhyung Kim 		key = ui_browser__run(&self->b, delay_secs);
604aca7a94dSNamhyung Kim 
605aca7a94dSNamhyung Kim 		if (delay_secs != 0) {
606aca7a94dSNamhyung Kim 			annotate_browser__calc_percent(self, evidx);
607aca7a94dSNamhyung Kim 			/*
608aca7a94dSNamhyung Kim 			 * Current line focus got out of the list of most active
609aca7a94dSNamhyung Kim 			 * lines, NULL it so that if TAB|UNTAB is pressed, we
610aca7a94dSNamhyung Kim 			 * move to curr_hot (current hottest line).
611aca7a94dSNamhyung Kim 			 */
612aca7a94dSNamhyung Kim 			if (nd != NULL && RB_EMPTY_NODE(nd))
613aca7a94dSNamhyung Kim 				nd = NULL;
614aca7a94dSNamhyung Kim 		}
615aca7a94dSNamhyung Kim 
616aca7a94dSNamhyung Kim 		switch (key) {
617aca7a94dSNamhyung Kim 		case K_TIMER:
618aca7a94dSNamhyung Kim 			if (timer != NULL)
619aca7a94dSNamhyung Kim 				timer(arg);
620aca7a94dSNamhyung Kim 
621aca7a94dSNamhyung Kim 			if (delay_secs != 0)
622aca7a94dSNamhyung Kim 				symbol__annotate_decay_histogram(sym, evidx);
623aca7a94dSNamhyung Kim 			continue;
624aca7a94dSNamhyung Kim 		case K_TAB:
625aca7a94dSNamhyung Kim 			if (nd != NULL) {
626aca7a94dSNamhyung Kim 				nd = rb_prev(nd);
627aca7a94dSNamhyung Kim 				if (nd == NULL)
628aca7a94dSNamhyung Kim 					nd = rb_last(&self->entries);
629aca7a94dSNamhyung Kim 			} else
630aca7a94dSNamhyung Kim 				nd = self->curr_hot;
631aca7a94dSNamhyung Kim 			break;
632aca7a94dSNamhyung Kim 		case K_UNTAB:
633aca7a94dSNamhyung Kim 			if (nd != NULL)
634aca7a94dSNamhyung Kim 				nd = rb_next(nd);
635aca7a94dSNamhyung Kim 				if (nd == NULL)
636aca7a94dSNamhyung Kim 					nd = rb_first(&self->entries);
637aca7a94dSNamhyung Kim 			else
638aca7a94dSNamhyung Kim 				nd = self->curr_hot;
639aca7a94dSNamhyung Kim 			break;
640aca7a94dSNamhyung Kim 		case 'H':
641aca7a94dSNamhyung Kim 		case 'h':
642aca7a94dSNamhyung Kim 			nd = self->curr_hot;
643aca7a94dSNamhyung Kim 			break;
644aca7a94dSNamhyung Kim 		case 'S':
645aca7a94dSNamhyung Kim 		case 's':
646aca7a94dSNamhyung Kim 			if (annotate_browser__toggle_source(self))
647aca7a94dSNamhyung Kim 				ui_helpline__puts(help);
648aca7a94dSNamhyung Kim 			continue;
649aca7a94dSNamhyung Kim 		case 'O':
650aca7a94dSNamhyung Kim 		case 'o':
651aca7a94dSNamhyung Kim 			self->use_offset = !self->use_offset;
65283b1f2aaSArnaldo Carvalho de Melo 			if (self->use_offset)
653*2402e4a9SArnaldo Carvalho de Melo 				self->target_width = self->min_addr_width;
65483b1f2aaSArnaldo Carvalho de Melo 			else
655*2402e4a9SArnaldo Carvalho de Melo 				self->target_width = self->max_addr_width;
656*2402e4a9SArnaldo Carvalho de Melo update_addr_width:
657*2402e4a9SArnaldo Carvalho de Melo 			self->addr_width = self->target_width;
658*2402e4a9SArnaldo Carvalho de Melo 			if (self->show_nr_jumps)
659*2402e4a9SArnaldo Carvalho de Melo 				self->addr_width += self->jumps_width + 1;
660aca7a94dSNamhyung Kim 			continue;
6619d1ef56dSArnaldo Carvalho de Melo 		case 'j':
6629d1ef56dSArnaldo Carvalho de Melo 			self->jump_arrows = !self->jump_arrows;
6639d1ef56dSArnaldo Carvalho de Melo 			continue;
664*2402e4a9SArnaldo Carvalho de Melo 		case 'J':
665*2402e4a9SArnaldo Carvalho de Melo 			self->show_nr_jumps = !self->show_nr_jumps;
666*2402e4a9SArnaldo Carvalho de Melo 			goto update_addr_width;
667aca7a94dSNamhyung Kim 		case '/':
668aca7a94dSNamhyung Kim 			if (annotate_browser__search(self, delay_secs)) {
669aca7a94dSNamhyung Kim show_help:
670aca7a94dSNamhyung Kim 				ui_helpline__puts(help);
671aca7a94dSNamhyung Kim 			}
672aca7a94dSNamhyung Kim 			continue;
673aca7a94dSNamhyung Kim 		case 'n':
674aca7a94dSNamhyung Kim 			if (self->searching_backwards ?
675aca7a94dSNamhyung Kim 			    annotate_browser__continue_search_reverse(self, delay_secs) :
676aca7a94dSNamhyung Kim 			    annotate_browser__continue_search(self, delay_secs))
677aca7a94dSNamhyung Kim 				goto show_help;
678aca7a94dSNamhyung Kim 			continue;
679aca7a94dSNamhyung Kim 		case '?':
680aca7a94dSNamhyung Kim 			if (annotate_browser__search_reverse(self, delay_secs))
681aca7a94dSNamhyung Kim 				goto show_help;
682aca7a94dSNamhyung Kim 			continue;
683aca7a94dSNamhyung Kim 		case K_ENTER:
684aca7a94dSNamhyung Kim 		case K_RIGHT:
685aca7a94dSNamhyung Kim 			if (self->selection == NULL)
686aca7a94dSNamhyung Kim 				ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
687aca7a94dSNamhyung Kim 			else if (self->selection->offset == -1)
688aca7a94dSNamhyung Kim 				ui_helpline__puts("Actions are only available for assembly lines.");
689c4cceae3SArnaldo Carvalho de Melo 			else if (!self->selection->ins) {
690c4cceae3SArnaldo Carvalho de Melo 				if (strcmp(self->selection->name, "retq"))
691c4cceae3SArnaldo Carvalho de Melo 					goto show_sup_ins;
692c4cceae3SArnaldo Carvalho de Melo 				goto out;
693c4cceae3SArnaldo Carvalho de Melo 			} else if (!(annotate_browser__jump(self) ||
694c4cceae3SArnaldo Carvalho de Melo 				     annotate_browser__callq(self, evidx, timer, arg, delay_secs))) {
695c4cceae3SArnaldo Carvalho de Melo show_sup_ins:
696c4cceae3SArnaldo Carvalho de Melo 				ui_helpline__puts("Actions are only available for 'callq', 'retq' & jump instructions.");
697c4cceae3SArnaldo Carvalho de Melo 			}
698aca7a94dSNamhyung Kim 			continue;
699aca7a94dSNamhyung Kim 		case K_LEFT:
700aca7a94dSNamhyung Kim 		case K_ESC:
701aca7a94dSNamhyung Kim 		case 'q':
702aca7a94dSNamhyung Kim 		case CTRL('c'):
703aca7a94dSNamhyung Kim 			goto out;
704aca7a94dSNamhyung Kim 		default:
705aca7a94dSNamhyung Kim 			continue;
706aca7a94dSNamhyung Kim 		}
707aca7a94dSNamhyung Kim 
708aca7a94dSNamhyung Kim 		if (nd != NULL)
709aca7a94dSNamhyung Kim 			annotate_browser__set_rb_top(self, nd);
710aca7a94dSNamhyung Kim 	}
711aca7a94dSNamhyung Kim out:
712aca7a94dSNamhyung Kim 	ui_browser__hide(&self->b);
713aca7a94dSNamhyung Kim 	return key;
714aca7a94dSNamhyung Kim }
715aca7a94dSNamhyung Kim 
716aca7a94dSNamhyung Kim int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
717aca7a94dSNamhyung Kim 			     void(*timer)(void *arg), void *arg, int delay_secs)
718aca7a94dSNamhyung Kim {
719aca7a94dSNamhyung Kim 	return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
720aca7a94dSNamhyung Kim 				    timer, arg, delay_secs);
721aca7a94dSNamhyung Kim }
722aca7a94dSNamhyung Kim 
723b793a401SArnaldo Carvalho de Melo static void annotate_browser__mark_jump_targets(struct annotate_browser *browser,
724b793a401SArnaldo Carvalho de Melo 						size_t size)
725b793a401SArnaldo Carvalho de Melo {
726b793a401SArnaldo Carvalho de Melo 	u64 offset;
727b793a401SArnaldo Carvalho de Melo 
728b793a401SArnaldo Carvalho de Melo 	for (offset = 0; offset < size; ++offset) {
729b793a401SArnaldo Carvalho de Melo 		struct disasm_line *dl = browser->offsets[offset], *dlt;
730b793a401SArnaldo Carvalho de Melo 		struct browser_disasm_line *bdlt;
731b793a401SArnaldo Carvalho de Melo 
73238b31bd0SArnaldo Carvalho de Melo 		if (!dl || !dl->ins || !ins__is_jump(dl->ins) ||
73338b31bd0SArnaldo Carvalho de Melo 		    !disasm_line__has_offset(dl))
734b793a401SArnaldo Carvalho de Melo 			continue;
735b793a401SArnaldo Carvalho de Melo 
73644d1a3edSArnaldo Carvalho de Melo 		if (dl->ops.target.offset >= size) {
737b793a401SArnaldo Carvalho de Melo 			ui__error("jump to after symbol!\n"
738b793a401SArnaldo Carvalho de Melo 				  "size: %zx, jump target: %" PRIx64,
73944d1a3edSArnaldo Carvalho de Melo 				  size, dl->ops.target.offset);
740b793a401SArnaldo Carvalho de Melo 			continue;
741b793a401SArnaldo Carvalho de Melo 		}
742b793a401SArnaldo Carvalho de Melo 
74344d1a3edSArnaldo Carvalho de Melo 		dlt = browser->offsets[dl->ops.target.offset];
7449481ede9SArnaldo Carvalho de Melo 		/*
7459481ede9SArnaldo Carvalho de Melo  		 * FIXME: Oops, no jump target? Buggy disassembler? Or do we
7469481ede9SArnaldo Carvalho de Melo  		 * have to adjust to the previous offset?
7479481ede9SArnaldo Carvalho de Melo  		 */
7489481ede9SArnaldo Carvalho de Melo 		if (dlt == NULL)
7499481ede9SArnaldo Carvalho de Melo 			continue;
7509481ede9SArnaldo Carvalho de Melo 
751b793a401SArnaldo Carvalho de Melo 		bdlt = disasm_line__browser(dlt);
752*2402e4a9SArnaldo Carvalho de Melo 		if (++bdlt->jump_sources > browser->max_jump_sources)
753*2402e4a9SArnaldo Carvalho de Melo 			browser->max_jump_sources = bdlt->jump_sources;
754*2402e4a9SArnaldo Carvalho de Melo 
755*2402e4a9SArnaldo Carvalho de Melo 		++browser->nr_jumps;
756b793a401SArnaldo Carvalho de Melo 	}
757b793a401SArnaldo Carvalho de Melo 
758b793a401SArnaldo Carvalho de Melo }
759b793a401SArnaldo Carvalho de Melo 
760*2402e4a9SArnaldo Carvalho de Melo static inline int width_jumps(int n)
761*2402e4a9SArnaldo Carvalho de Melo {
762*2402e4a9SArnaldo Carvalho de Melo 	if (n >= 100)
763*2402e4a9SArnaldo Carvalho de Melo 		return 5;
764*2402e4a9SArnaldo Carvalho de Melo 	if (n / 10)
765*2402e4a9SArnaldo Carvalho de Melo 		return 2;
766*2402e4a9SArnaldo Carvalho de Melo 	return 1;
767*2402e4a9SArnaldo Carvalho de Melo }
768*2402e4a9SArnaldo Carvalho de Melo 
769aca7a94dSNamhyung Kim int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
770aca7a94dSNamhyung Kim 			 void(*timer)(void *arg), void *arg,
771aca7a94dSNamhyung Kim 			 int delay_secs)
772aca7a94dSNamhyung Kim {
77329ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos, *n;
774aca7a94dSNamhyung Kim 	struct annotation *notes;
775b793a401SArnaldo Carvalho de Melo 	const size_t size = symbol__size(sym);
776aca7a94dSNamhyung Kim 	struct map_symbol ms = {
777aca7a94dSNamhyung Kim 		.map = map,
778aca7a94dSNamhyung Kim 		.sym = sym,
779aca7a94dSNamhyung Kim 	};
780aca7a94dSNamhyung Kim 	struct annotate_browser browser = {
781aca7a94dSNamhyung Kim 		.b = {
782a3f895beSArnaldo Carvalho de Melo 			.refresh = annotate_browser__refresh,
783aca7a94dSNamhyung Kim 			.seek	 = ui_browser__list_head_seek,
784aca7a94dSNamhyung Kim 			.write	 = annotate_browser__write,
78529ed6e76SArnaldo Carvalho de Melo 			.filter  = disasm_line__filter,
786aca7a94dSNamhyung Kim 			.priv	 = &ms,
787aca7a94dSNamhyung Kim 			.use_navkeypressed = true,
788aca7a94dSNamhyung Kim 		},
7898bf39cb8SArnaldo Carvalho de Melo 		.use_offset = true,
7909d1ef56dSArnaldo Carvalho de Melo 		.jump_arrows = true,
791aca7a94dSNamhyung Kim 	};
792b793a401SArnaldo Carvalho de Melo 	int ret = -1;
793aca7a94dSNamhyung Kim 
794aca7a94dSNamhyung Kim 	if (sym == NULL)
795aca7a94dSNamhyung Kim 		return -1;
796aca7a94dSNamhyung Kim 
797aca7a94dSNamhyung Kim 	if (map->dso->annotate_warned)
798aca7a94dSNamhyung Kim 		return -1;
799aca7a94dSNamhyung Kim 
800b793a401SArnaldo Carvalho de Melo 	browser.offsets = zalloc(size * sizeof(struct disasm_line *));
801b793a401SArnaldo Carvalho de Melo 	if (browser.offsets == NULL) {
802b793a401SArnaldo Carvalho de Melo 		ui__error("Not enough memory!");
803b793a401SArnaldo Carvalho de Melo 		return -1;
804b793a401SArnaldo Carvalho de Melo 	}
805b793a401SArnaldo Carvalho de Melo 
806887c0066SArnaldo Carvalho de Melo 	if (symbol__annotate(sym, map, sizeof(struct browser_disasm_line)) < 0) {
807aca7a94dSNamhyung Kim 		ui__error("%s", ui_helpline__last_msg);
808b793a401SArnaldo Carvalho de Melo 		goto out_free_offsets;
809aca7a94dSNamhyung Kim 	}
810aca7a94dSNamhyung Kim 
811aca7a94dSNamhyung Kim 	ui_helpline__push("Press <- or ESC to exit");
812aca7a94dSNamhyung Kim 
813aca7a94dSNamhyung Kim 	notes = symbol__annotation(sym);
814aca7a94dSNamhyung Kim 	browser.start = map__rip_2objdump(map, sym->start);
815aca7a94dSNamhyung Kim 
816aca7a94dSNamhyung Kim 	list_for_each_entry(pos, &notes->src->source, node) {
817887c0066SArnaldo Carvalho de Melo 		struct browser_disasm_line *bpos;
818aca7a94dSNamhyung Kim 		size_t line_len = strlen(pos->line);
819aca7a94dSNamhyung Kim 
820aca7a94dSNamhyung Kim 		if (browser.b.width < line_len)
821aca7a94dSNamhyung Kim 			browser.b.width = line_len;
822887c0066SArnaldo Carvalho de Melo 		bpos = disasm_line__browser(pos);
823887c0066SArnaldo Carvalho de Melo 		bpos->idx = browser.nr_entries++;
824b793a401SArnaldo Carvalho de Melo 		if (pos->offset != -1) {
825887c0066SArnaldo Carvalho de Melo 			bpos->idx_asm = browser.nr_asm_entries++;
82697148a97SArnaldo Carvalho de Melo 			/*
82797148a97SArnaldo Carvalho de Melo 			 * FIXME: short term bandaid to cope with assembly
82897148a97SArnaldo Carvalho de Melo 			 * routines that comes with labels in the same column
82997148a97SArnaldo Carvalho de Melo 			 * as the address in objdump, sigh.
83097148a97SArnaldo Carvalho de Melo 			 *
83197148a97SArnaldo Carvalho de Melo 			 * E.g. copy_user_generic_unrolled
83297148a97SArnaldo Carvalho de Melo  			 */
83397148a97SArnaldo Carvalho de Melo 			if (pos->offset < (s64)size)
834b793a401SArnaldo Carvalho de Melo 				browser.offsets[pos->offset] = pos;
835b793a401SArnaldo Carvalho de Melo 		} else
836887c0066SArnaldo Carvalho de Melo 			bpos->idx_asm = -1;
837aca7a94dSNamhyung Kim 	}
838aca7a94dSNamhyung Kim 
839b793a401SArnaldo Carvalho de Melo 	annotate_browser__mark_jump_targets(&browser, size);
840b793a401SArnaldo Carvalho de Melo 
841*2402e4a9SArnaldo Carvalho de Melo 	browser.addr_width = browser.target_width = browser.min_addr_width = hex_width(size);
84283b1f2aaSArnaldo Carvalho de Melo 	browser.max_addr_width = hex_width(sym->end);
843*2402e4a9SArnaldo Carvalho de Melo 	browser.jumps_width = width_jumps(browser.max_jump_sources);
844aca7a94dSNamhyung Kim 	browser.b.nr_entries = browser.nr_entries;
845aca7a94dSNamhyung Kim 	browser.b.entries = &notes->src->source,
846aca7a94dSNamhyung Kim 	browser.b.width += 18; /* Percentage */
847aca7a94dSNamhyung Kim 	ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
848aca7a94dSNamhyung Kim 	list_for_each_entry_safe(pos, n, &notes->src->source, node) {
849aca7a94dSNamhyung Kim 		list_del(&pos->node);
85029ed6e76SArnaldo Carvalho de Melo 		disasm_line__free(pos);
851aca7a94dSNamhyung Kim 	}
852b793a401SArnaldo Carvalho de Melo 
853b793a401SArnaldo Carvalho de Melo out_free_offsets:
854b793a401SArnaldo Carvalho de Melo 	free(browser.offsets);
855aca7a94dSNamhyung Kim 	return ret;
856aca7a94dSNamhyung Kim }
857