xref: /linux/tools/perf/ui/browsers/annotate.c (revision 44d1a3edfbd65f9da6725921e2425b10477772d8)
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;
19b793a401SArnaldo Carvalho de Melo 	bool		jump_target;
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;
31aca7a94dSNamhyung Kim 	bool		    hide_src_code;
32aca7a94dSNamhyung Kim 	bool		    use_offset;
33aca7a94dSNamhyung Kim 	bool		    searching_backwards;
3461e04b33SArnaldo Carvalho de Melo 	u8		    offset_width;
35aca7a94dSNamhyung Kim 	char		    search_bf[128];
36aca7a94dSNamhyung Kim };
37aca7a94dSNamhyung Kim 
38887c0066SArnaldo Carvalho de Melo static inline struct browser_disasm_line *disasm_line__browser(struct disasm_line *dl)
39aca7a94dSNamhyung Kim {
40887c0066SArnaldo Carvalho de Melo 	return (struct browser_disasm_line *)(dl + 1);
41aca7a94dSNamhyung Kim }
42aca7a94dSNamhyung Kim 
4329ed6e76SArnaldo Carvalho de Melo static bool disasm_line__filter(struct ui_browser *browser, void *entry)
44aca7a94dSNamhyung Kim {
45aca7a94dSNamhyung Kim 	struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
46aca7a94dSNamhyung Kim 
47aca7a94dSNamhyung Kim 	if (ab->hide_src_code) {
4829ed6e76SArnaldo Carvalho de Melo 		struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
4929ed6e76SArnaldo Carvalho de Melo 		return dl->offset == -1;
50aca7a94dSNamhyung Kim 	}
51aca7a94dSNamhyung Kim 
52aca7a94dSNamhyung Kim 	return false;
53aca7a94dSNamhyung Kim }
54aca7a94dSNamhyung Kim 
55aca7a94dSNamhyung Kim static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
56aca7a94dSNamhyung Kim {
57aca7a94dSNamhyung Kim 	struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
5829ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
59b793a401SArnaldo Carvalho de Melo 	struct browser_disasm_line *bdl = disasm_line__browser(dl);
60aca7a94dSNamhyung Kim 	bool current_entry = ui_browser__is_current_entry(self, row);
61aca7a94dSNamhyung Kim 	bool change_color = (!ab->hide_src_code &&
62aca7a94dSNamhyung Kim 			     (!current_entry || (self->use_navkeypressed &&
63aca7a94dSNamhyung Kim 					         !self->navkeypressed)));
64aca7a94dSNamhyung Kim 	int width = self->width;
65aca7a94dSNamhyung Kim 
6629ed6e76SArnaldo Carvalho de Melo 	if (dl->offset != -1) {
67887c0066SArnaldo Carvalho de Melo 		ui_browser__set_percent_color(self, bdl->percent, current_entry);
68887c0066SArnaldo Carvalho de Melo 		slsmg_printf(" %7.2f ", bdl->percent);
69aca7a94dSNamhyung Kim 	} else {
70aca7a94dSNamhyung Kim 		ui_browser__set_percent_color(self, 0, current_entry);
71aca7a94dSNamhyung Kim 		slsmg_write_nstring(" ", 9);
72aca7a94dSNamhyung Kim 	}
73aca7a94dSNamhyung Kim 
7459d038d5SArnaldo Carvalho de Melo 	ui_browser__write_graph(self, SLSMG_VLINE_CHAR);
75cf2dacc5SArnaldo Carvalho de Melo 	SLsmg_write_char(' ');
76aca7a94dSNamhyung Kim 
77aca7a94dSNamhyung Kim 	/* The scroll bar isn't being used */
78aca7a94dSNamhyung Kim 	if (!self->navkeypressed)
79aca7a94dSNamhyung Kim 		width += 1;
80aca7a94dSNamhyung Kim 
8129ed6e76SArnaldo Carvalho de Melo 	if (dl->offset != -1 && change_color)
82aca7a94dSNamhyung Kim 		ui_browser__set_color(self, HE_COLORSET_CODE);
83aca7a94dSNamhyung Kim 
8429ed6e76SArnaldo Carvalho de Melo 	if (!*dl->line)
858bf39cb8SArnaldo Carvalho de Melo 		slsmg_write_nstring(" ", width - 10);
8629ed6e76SArnaldo Carvalho de Melo 	else if (dl->offset == -1)
878bf39cb8SArnaldo Carvalho de Melo 		slsmg_write_nstring(dl->line, width - 10);
88aca7a94dSNamhyung Kim 	else {
898bf39cb8SArnaldo Carvalho de Melo 		char bf[256];
9029ed6e76SArnaldo Carvalho de Melo 		u64 addr = dl->offset;
91aca7a94dSNamhyung Kim 		int printed, color = -1;
92aca7a94dSNamhyung Kim 
93aca7a94dSNamhyung Kim 		if (!ab->use_offset)
94aca7a94dSNamhyung Kim 			addr += ab->start;
95aca7a94dSNamhyung Kim 
9661e04b33SArnaldo Carvalho de Melo 		if (!ab->use_offset) {
97aca7a94dSNamhyung Kim 			printed = scnprintf(bf, sizeof(bf), "  %" PRIx64 ":", addr);
9861e04b33SArnaldo Carvalho de Melo 		} else {
9961e04b33SArnaldo Carvalho de Melo 			if (bdl->jump_target) {
10061e04b33SArnaldo Carvalho de Melo 				printed = scnprintf(bf, sizeof(bf), "  %*" PRIx64 ":",
10161e04b33SArnaldo Carvalho de Melo 						    ab->offset_width, addr);
10261e04b33SArnaldo Carvalho de Melo 			} else {
10361e04b33SArnaldo Carvalho de Melo 				printed = scnprintf(bf, sizeof(bf), "  %*s ",
10461e04b33SArnaldo Carvalho de Melo 						    ab->offset_width, " ");
10561e04b33SArnaldo Carvalho de Melo 			}
10661e04b33SArnaldo Carvalho de Melo 		}
107b793a401SArnaldo Carvalho de Melo 
108aca7a94dSNamhyung Kim 		if (change_color)
109aca7a94dSNamhyung Kim 			color = ui_browser__set_color(self, HE_COLORSET_ADDR);
110aca7a94dSNamhyung Kim 		slsmg_write_nstring(bf, printed);
111aca7a94dSNamhyung Kim 		if (change_color)
112aca7a94dSNamhyung Kim 			ui_browser__set_color(self, color);
11328548d78SArnaldo Carvalho de Melo 		if (dl->ins && dl->ins->ops->scnprintf) {
11451a0d455SArnaldo Carvalho de Melo 			if (ins__is_jump(dl->ins)) {
115*44d1a3edSArnaldo Carvalho de Melo 				bool fwd = dl->ops.target.offset > (u64)dl->offset;
11651a0d455SArnaldo Carvalho de Melo 
11759d038d5SArnaldo Carvalho de Melo 				ui_browser__write_graph(self, fwd ? SLSMG_DARROW_CHAR :
11851a0d455SArnaldo Carvalho de Melo 								    SLSMG_UARROW_CHAR);
11951a0d455SArnaldo Carvalho de Melo 				SLsmg_write_char(' ');
12051a0d455SArnaldo Carvalho de Melo 			} else {
12151a0d455SArnaldo Carvalho de Melo 				slsmg_write_nstring(" ", 2);
12251a0d455SArnaldo Carvalho de Melo 			}
12351a0d455SArnaldo Carvalho de Melo 
124c7e6ead7SArnaldo Carvalho de Melo 			dl->ins->ops->scnprintf(dl->ins, bf, sizeof(bf), &dl->ops,
125c7e6ead7SArnaldo Carvalho de Melo 						!ab->use_offset);
1264ea08b52SArnaldo Carvalho de Melo 		} else {
1274ea08b52SArnaldo Carvalho de Melo 			if (strcmp(dl->name, "retq")) {
1284ea08b52SArnaldo Carvalho de Melo 				slsmg_write_nstring(" ", 2);
1294ea08b52SArnaldo Carvalho de Melo 			} else {
13059d038d5SArnaldo Carvalho de Melo 				ui_browser__write_graph(self, SLSMG_LARROW_CHAR);
1314ea08b52SArnaldo Carvalho de Melo 				SLsmg_write_char(' ');
1324ea08b52SArnaldo Carvalho de Melo 			}
13328548d78SArnaldo Carvalho de Melo 
1344ea08b52SArnaldo Carvalho de Melo 			scnprintf(bf, sizeof(bf), "%-6.6s %s", dl->name, dl->ops.raw);
1354ea08b52SArnaldo Carvalho de Melo 		}
1364ea08b52SArnaldo Carvalho de Melo 
1374ea08b52SArnaldo Carvalho de Melo 		slsmg_write_nstring(bf, width - 12 - printed);
138aca7a94dSNamhyung Kim 	}
139aca7a94dSNamhyung Kim 
140aca7a94dSNamhyung Kim 	if (current_entry)
14129ed6e76SArnaldo Carvalho de Melo 		ab->selection = dl;
142aca7a94dSNamhyung Kim }
143aca7a94dSNamhyung Kim 
144a3f895beSArnaldo Carvalho de Melo static void annotate_browser__draw_current_loop(struct ui_browser *browser)
145a3f895beSArnaldo Carvalho de Melo {
146a3f895beSArnaldo Carvalho de Melo 	struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
147a3f895beSArnaldo Carvalho de Melo 	struct map_symbol *ms = browser->priv;
148a3f895beSArnaldo Carvalho de Melo 	struct symbol *sym = ms->sym;
149a3f895beSArnaldo Carvalho de Melo 	struct annotation *notes = symbol__annotation(sym);
150a3f895beSArnaldo Carvalho de Melo 	struct disasm_line *cursor = ab->selection, *pos = cursor, *target;
151a3f895beSArnaldo Carvalho de Melo 	struct browser_disasm_line *bcursor = disasm_line__browser(cursor),
152a3f895beSArnaldo Carvalho de Melo 				   *btarget, *bpos;
153a3f895beSArnaldo Carvalho de Melo 	unsigned int from, to, start_width = 2;
154a3f895beSArnaldo Carvalho de Melo 
155a3f895beSArnaldo Carvalho de Melo 	list_for_each_entry_from(pos, &notes->src->source, node) {
156a3f895beSArnaldo Carvalho de Melo 		if (!pos->ins || !ins__is_jump(pos->ins))
157a3f895beSArnaldo Carvalho de Melo 			continue;
158a3f895beSArnaldo Carvalho de Melo 
159*44d1a3edSArnaldo Carvalho de Melo 		target = ab->offsets[pos->ops.target.offset];
160a3f895beSArnaldo Carvalho de Melo 		if (!target)
161a3f895beSArnaldo Carvalho de Melo 			continue;
162a3f895beSArnaldo Carvalho de Melo 
163a3f895beSArnaldo Carvalho de Melo 		btarget = disasm_line__browser(target);
164a3f895beSArnaldo Carvalho de Melo 		if (btarget->idx <= bcursor->idx)
165a3f895beSArnaldo Carvalho de Melo 			goto found;
166a3f895beSArnaldo Carvalho de Melo 	}
167a3f895beSArnaldo Carvalho de Melo 
168a3f895beSArnaldo Carvalho de Melo 	return;
169a3f895beSArnaldo Carvalho de Melo 
170a3f895beSArnaldo Carvalho de Melo found:
171a3f895beSArnaldo Carvalho de Melo 	bpos = disasm_line__browser(pos);
172a3f895beSArnaldo Carvalho de Melo 	if (ab->hide_src_code) {
173a3f895beSArnaldo Carvalho de Melo 		from = bpos->idx_asm;
174a3f895beSArnaldo Carvalho de Melo 		to = btarget->idx_asm;
175a3f895beSArnaldo Carvalho de Melo 	} else {
176a3f895beSArnaldo Carvalho de Melo 		from = (u64)bpos->idx;
177a3f895beSArnaldo Carvalho de Melo 		to = (u64)btarget->idx;
178a3f895beSArnaldo Carvalho de Melo 	}
179a3f895beSArnaldo Carvalho de Melo 
180a3f895beSArnaldo Carvalho de Melo 	ui_browser__set_color(browser, HE_COLORSET_CODE);
181a3f895beSArnaldo Carvalho de Melo 
182a3f895beSArnaldo Carvalho de Melo 	if (!bpos->jump_target)
183a3f895beSArnaldo Carvalho de Melo 		start_width += ab->offset_width + 1;
184a3f895beSArnaldo Carvalho de Melo 
185a3f895beSArnaldo Carvalho de Melo 	__ui_browser__line_arrow_up(browser, 10, from, to, start_width);
186a3f895beSArnaldo Carvalho de Melo }
187a3f895beSArnaldo Carvalho de Melo 
188a3f895beSArnaldo Carvalho de Melo static unsigned int annotate_browser__refresh(struct ui_browser *browser)
189a3f895beSArnaldo Carvalho de Melo {
190a3f895beSArnaldo Carvalho de Melo 	int ret = ui_browser__list_head_refresh(browser);
191a3f895beSArnaldo Carvalho de Melo 
192a3f895beSArnaldo Carvalho de Melo 	annotate_browser__draw_current_loop(browser);
193a3f895beSArnaldo Carvalho de Melo 
194a3f895beSArnaldo Carvalho de Melo 	return ret;
195a3f895beSArnaldo Carvalho de Melo }
196a3f895beSArnaldo Carvalho de Melo 
19729ed6e76SArnaldo Carvalho de Melo static double disasm_line__calc_percent(struct disasm_line *dl, struct symbol *sym, int evidx)
198aca7a94dSNamhyung Kim {
199aca7a94dSNamhyung Kim 	double percent = 0.0;
200aca7a94dSNamhyung Kim 
20129ed6e76SArnaldo Carvalho de Melo 	if (dl->offset != -1) {
202aca7a94dSNamhyung Kim 		int len = sym->end - sym->start;
203aca7a94dSNamhyung Kim 		unsigned int hits = 0;
204aca7a94dSNamhyung Kim 		struct annotation *notes = symbol__annotation(sym);
205aca7a94dSNamhyung Kim 		struct source_line *src_line = notes->src->lines;
206aca7a94dSNamhyung Kim 		struct sym_hist *h = annotation__histogram(notes, evidx);
20729ed6e76SArnaldo Carvalho de Melo 		s64 offset = dl->offset;
20829ed6e76SArnaldo Carvalho de Melo 		struct disasm_line *next;
209aca7a94dSNamhyung Kim 
21029ed6e76SArnaldo Carvalho de Melo 		next = disasm__get_next_ip_line(&notes->src->source, dl);
211aca7a94dSNamhyung Kim 		while (offset < (s64)len &&
212aca7a94dSNamhyung Kim 		       (next == NULL || offset < next->offset)) {
213aca7a94dSNamhyung Kim 			if (src_line) {
214aca7a94dSNamhyung Kim 				percent += src_line[offset].percent;
215aca7a94dSNamhyung Kim 			} else
216aca7a94dSNamhyung Kim 				hits += h->addr[offset];
217aca7a94dSNamhyung Kim 
218aca7a94dSNamhyung Kim 			++offset;
219aca7a94dSNamhyung Kim 		}
220aca7a94dSNamhyung Kim 		/*
221aca7a94dSNamhyung Kim  		 * If the percentage wasn't already calculated in
222aca7a94dSNamhyung Kim  		 * symbol__get_source_line, do it now:
223aca7a94dSNamhyung Kim  		 */
224aca7a94dSNamhyung Kim 		if (src_line == NULL && h->sum)
225aca7a94dSNamhyung Kim 			percent = 100.0 * hits / h->sum;
226aca7a94dSNamhyung Kim 	}
227aca7a94dSNamhyung Kim 
228aca7a94dSNamhyung Kim 	return percent;
229aca7a94dSNamhyung Kim }
230aca7a94dSNamhyung Kim 
231887c0066SArnaldo Carvalho de Melo static void disasm_rb_tree__insert(struct rb_root *root, struct browser_disasm_line *bdl)
232aca7a94dSNamhyung Kim {
23329ed6e76SArnaldo Carvalho de Melo 	struct rb_node **p = &root->rb_node;
234aca7a94dSNamhyung Kim 	struct rb_node *parent = NULL;
235887c0066SArnaldo Carvalho de Melo 	struct browser_disasm_line *l;
236aca7a94dSNamhyung Kim 
237aca7a94dSNamhyung Kim 	while (*p != NULL) {
238aca7a94dSNamhyung Kim 		parent = *p;
239887c0066SArnaldo Carvalho de Melo 		l = rb_entry(parent, struct browser_disasm_line, rb_node);
240887c0066SArnaldo Carvalho de Melo 		if (bdl->percent < l->percent)
241aca7a94dSNamhyung Kim 			p = &(*p)->rb_left;
242aca7a94dSNamhyung Kim 		else
243aca7a94dSNamhyung Kim 			p = &(*p)->rb_right;
244aca7a94dSNamhyung Kim 	}
245887c0066SArnaldo Carvalho de Melo 	rb_link_node(&bdl->rb_node, parent, p);
246887c0066SArnaldo Carvalho de Melo 	rb_insert_color(&bdl->rb_node, root);
247aca7a94dSNamhyung Kim }
248aca7a94dSNamhyung Kim 
249aca7a94dSNamhyung Kim static void annotate_browser__set_top(struct annotate_browser *self,
25029ed6e76SArnaldo Carvalho de Melo 				      struct disasm_line *pos, u32 idx)
251aca7a94dSNamhyung Kim {
252aca7a94dSNamhyung Kim 	unsigned back;
253aca7a94dSNamhyung Kim 
254aca7a94dSNamhyung Kim 	ui_browser__refresh_dimensions(&self->b);
255aca7a94dSNamhyung Kim 	back = self->b.height / 2;
256aca7a94dSNamhyung Kim 	self->b.top_idx = self->b.index = idx;
257aca7a94dSNamhyung Kim 
258aca7a94dSNamhyung Kim 	while (self->b.top_idx != 0 && back != 0) {
25929ed6e76SArnaldo Carvalho de Melo 		pos = list_entry(pos->node.prev, struct disasm_line, node);
260aca7a94dSNamhyung Kim 
26129ed6e76SArnaldo Carvalho de Melo 		if (disasm_line__filter(&self->b, &pos->node))
262aca7a94dSNamhyung Kim 			continue;
263aca7a94dSNamhyung Kim 
264aca7a94dSNamhyung Kim 		--self->b.top_idx;
265aca7a94dSNamhyung Kim 		--back;
266aca7a94dSNamhyung Kim 	}
267aca7a94dSNamhyung Kim 
268aca7a94dSNamhyung Kim 	self->b.top = pos;
269aca7a94dSNamhyung Kim 	self->b.navkeypressed = true;
270aca7a94dSNamhyung Kim }
271aca7a94dSNamhyung Kim 
272aca7a94dSNamhyung Kim static void annotate_browser__set_rb_top(struct annotate_browser *browser,
273aca7a94dSNamhyung Kim 					 struct rb_node *nd)
274aca7a94dSNamhyung Kim {
275887c0066SArnaldo Carvalho de Melo 	struct browser_disasm_line *bpos;
27629ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos;
277aca7a94dSNamhyung Kim 
278887c0066SArnaldo Carvalho de Melo 	bpos = rb_entry(nd, struct browser_disasm_line, rb_node);
279887c0066SArnaldo Carvalho de Melo 	pos = ((struct disasm_line *)bpos) - 1;
280887c0066SArnaldo Carvalho de Melo 	annotate_browser__set_top(browser, pos, bpos->idx);
281aca7a94dSNamhyung Kim 	browser->curr_hot = nd;
282aca7a94dSNamhyung Kim }
283aca7a94dSNamhyung Kim 
284aca7a94dSNamhyung Kim static void annotate_browser__calc_percent(struct annotate_browser *browser,
285aca7a94dSNamhyung Kim 					   int evidx)
286aca7a94dSNamhyung Kim {
287aca7a94dSNamhyung Kim 	struct map_symbol *ms = browser->b.priv;
288aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
289aca7a94dSNamhyung Kim 	struct annotation *notes = symbol__annotation(sym);
29029ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos;
291aca7a94dSNamhyung Kim 
292aca7a94dSNamhyung Kim 	browser->entries = RB_ROOT;
293aca7a94dSNamhyung Kim 
294aca7a94dSNamhyung Kim 	pthread_mutex_lock(&notes->lock);
295aca7a94dSNamhyung Kim 
296aca7a94dSNamhyung Kim 	list_for_each_entry(pos, &notes->src->source, node) {
297887c0066SArnaldo Carvalho de Melo 		struct browser_disasm_line *bpos = disasm_line__browser(pos);
298887c0066SArnaldo Carvalho de Melo 		bpos->percent = disasm_line__calc_percent(pos, sym, evidx);
299887c0066SArnaldo Carvalho de Melo 		if (bpos->percent < 0.01) {
300887c0066SArnaldo Carvalho de Melo 			RB_CLEAR_NODE(&bpos->rb_node);
301aca7a94dSNamhyung Kim 			continue;
302aca7a94dSNamhyung Kim 		}
303887c0066SArnaldo Carvalho de Melo 		disasm_rb_tree__insert(&browser->entries, bpos);
304aca7a94dSNamhyung Kim 	}
305aca7a94dSNamhyung Kim 	pthread_mutex_unlock(&notes->lock);
306aca7a94dSNamhyung Kim 
307aca7a94dSNamhyung Kim 	browser->curr_hot = rb_last(&browser->entries);
308aca7a94dSNamhyung Kim }
309aca7a94dSNamhyung Kim 
310aca7a94dSNamhyung Kim static bool annotate_browser__toggle_source(struct annotate_browser *browser)
311aca7a94dSNamhyung Kim {
31229ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *dl;
313887c0066SArnaldo Carvalho de Melo 	struct browser_disasm_line *bdl;
314aca7a94dSNamhyung Kim 	off_t offset = browser->b.index - browser->b.top_idx;
315aca7a94dSNamhyung Kim 
316aca7a94dSNamhyung Kim 	browser->b.seek(&browser->b, offset, SEEK_CUR);
31729ed6e76SArnaldo Carvalho de Melo 	dl = list_entry(browser->b.top, struct disasm_line, node);
318887c0066SArnaldo Carvalho de Melo 	bdl = disasm_line__browser(dl);
319aca7a94dSNamhyung Kim 
320aca7a94dSNamhyung Kim 	if (browser->hide_src_code) {
321887c0066SArnaldo Carvalho de Melo 		if (bdl->idx_asm < offset)
322887c0066SArnaldo Carvalho de Melo 			offset = bdl->idx;
323aca7a94dSNamhyung Kim 
324aca7a94dSNamhyung Kim 		browser->b.nr_entries = browser->nr_entries;
325aca7a94dSNamhyung Kim 		browser->hide_src_code = false;
326aca7a94dSNamhyung Kim 		browser->b.seek(&browser->b, -offset, SEEK_CUR);
327887c0066SArnaldo Carvalho de Melo 		browser->b.top_idx = bdl->idx - offset;
328887c0066SArnaldo Carvalho de Melo 		browser->b.index = bdl->idx;
329aca7a94dSNamhyung Kim 	} else {
330887c0066SArnaldo Carvalho de Melo 		if (bdl->idx_asm < 0) {
331aca7a94dSNamhyung Kim 			ui_helpline__puts("Only available for assembly lines.");
332aca7a94dSNamhyung Kim 			browser->b.seek(&browser->b, -offset, SEEK_CUR);
333aca7a94dSNamhyung Kim 			return false;
334aca7a94dSNamhyung Kim 		}
335aca7a94dSNamhyung Kim 
336887c0066SArnaldo Carvalho de Melo 		if (bdl->idx_asm < offset)
337887c0066SArnaldo Carvalho de Melo 			offset = bdl->idx_asm;
338aca7a94dSNamhyung Kim 
339aca7a94dSNamhyung Kim 		browser->b.nr_entries = browser->nr_asm_entries;
340aca7a94dSNamhyung Kim 		browser->hide_src_code = true;
341aca7a94dSNamhyung Kim 		browser->b.seek(&browser->b, -offset, SEEK_CUR);
342887c0066SArnaldo Carvalho de Melo 		browser->b.top_idx = bdl->idx_asm - offset;
343887c0066SArnaldo Carvalho de Melo 		browser->b.index = bdl->idx_asm;
344aca7a94dSNamhyung Kim 	}
345aca7a94dSNamhyung Kim 
346aca7a94dSNamhyung Kim 	return true;
347aca7a94dSNamhyung Kim }
348aca7a94dSNamhyung Kim 
349aca7a94dSNamhyung Kim static bool annotate_browser__callq(struct annotate_browser *browser,
350aca7a94dSNamhyung Kim 				    int evidx, void (*timer)(void *arg),
351aca7a94dSNamhyung Kim 				    void *arg, int delay_secs)
352aca7a94dSNamhyung Kim {
353aca7a94dSNamhyung Kim 	struct map_symbol *ms = browser->b.priv;
354657bcaf5SArnaldo Carvalho de Melo 	struct disasm_line *dl = browser->selection;
355aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
356aca7a94dSNamhyung Kim 	struct annotation *notes;
357aca7a94dSNamhyung Kim 	struct symbol *target;
358aca7a94dSNamhyung Kim 	u64 ip;
359aca7a94dSNamhyung Kim 
360d86b0597SArnaldo Carvalho de Melo 	if (!ins__is_call(dl->ins))
361aca7a94dSNamhyung Kim 		return false;
362aca7a94dSNamhyung Kim 
363*44d1a3edSArnaldo Carvalho de Melo 	ip = ms->map->map_ip(ms->map, dl->ops.target.addr);
364aca7a94dSNamhyung Kim 	target = map__find_symbol(ms->map, ip, NULL);
365aca7a94dSNamhyung Kim 	if (target == NULL) {
366aca7a94dSNamhyung Kim 		ui_helpline__puts("The called function was not found.");
367aca7a94dSNamhyung Kim 		return true;
368aca7a94dSNamhyung Kim 	}
369aca7a94dSNamhyung Kim 
370aca7a94dSNamhyung Kim 	notes = symbol__annotation(target);
371aca7a94dSNamhyung Kim 	pthread_mutex_lock(&notes->lock);
372aca7a94dSNamhyung Kim 
373aca7a94dSNamhyung Kim 	if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
374aca7a94dSNamhyung Kim 		pthread_mutex_unlock(&notes->lock);
375aca7a94dSNamhyung Kim 		ui__warning("Not enough memory for annotating '%s' symbol!\n",
376aca7a94dSNamhyung Kim 			    target->name);
377aca7a94dSNamhyung Kim 		return true;
378aca7a94dSNamhyung Kim 	}
379aca7a94dSNamhyung Kim 
380aca7a94dSNamhyung Kim 	pthread_mutex_unlock(&notes->lock);
381aca7a94dSNamhyung Kim 	symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
382aca7a94dSNamhyung Kim 	ui_browser__show_title(&browser->b, sym->name);
383aca7a94dSNamhyung Kim 	return true;
384aca7a94dSNamhyung Kim }
385aca7a94dSNamhyung Kim 
38629ed6e76SArnaldo Carvalho de Melo static
38729ed6e76SArnaldo Carvalho de Melo struct disasm_line *annotate_browser__find_offset(struct annotate_browser *browser,
388aca7a94dSNamhyung Kim 					  s64 offset, s64 *idx)
389aca7a94dSNamhyung Kim {
390aca7a94dSNamhyung Kim 	struct map_symbol *ms = browser->b.priv;
391aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
392aca7a94dSNamhyung Kim 	struct annotation *notes = symbol__annotation(sym);
39329ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos;
394aca7a94dSNamhyung Kim 
395aca7a94dSNamhyung Kim 	*idx = 0;
396aca7a94dSNamhyung Kim 	list_for_each_entry(pos, &notes->src->source, node) {
397aca7a94dSNamhyung Kim 		if (pos->offset == offset)
398aca7a94dSNamhyung Kim 			return pos;
39929ed6e76SArnaldo Carvalho de Melo 		if (!disasm_line__filter(&browser->b, &pos->node))
400aca7a94dSNamhyung Kim 			++*idx;
401aca7a94dSNamhyung Kim 	}
402aca7a94dSNamhyung Kim 
403aca7a94dSNamhyung Kim 	return NULL;
404aca7a94dSNamhyung Kim }
405aca7a94dSNamhyung Kim 
406aca7a94dSNamhyung Kim static bool annotate_browser__jump(struct annotate_browser *browser)
407aca7a94dSNamhyung Kim {
408657bcaf5SArnaldo Carvalho de Melo 	struct disasm_line *dl = browser->selection;
4094f9d0325SArnaldo Carvalho de Melo 	s64 idx;
410aca7a94dSNamhyung Kim 
411d86b0597SArnaldo Carvalho de Melo 	if (!ins__is_jump(dl->ins))
412aca7a94dSNamhyung Kim 		return false;
413aca7a94dSNamhyung Kim 
414*44d1a3edSArnaldo Carvalho de Melo 	dl = annotate_browser__find_offset(browser, dl->ops.target.offset, &idx);
41529ed6e76SArnaldo Carvalho de Melo 	if (dl == NULL) {
416aca7a94dSNamhyung Kim 		ui_helpline__puts("Invallid jump offset");
417aca7a94dSNamhyung Kim 		return true;
418aca7a94dSNamhyung Kim 	}
419aca7a94dSNamhyung Kim 
42029ed6e76SArnaldo Carvalho de Melo 	annotate_browser__set_top(browser, dl, idx);
421aca7a94dSNamhyung Kim 
422aca7a94dSNamhyung Kim 	return true;
423aca7a94dSNamhyung Kim }
424aca7a94dSNamhyung Kim 
42529ed6e76SArnaldo Carvalho de Melo static
42629ed6e76SArnaldo Carvalho de Melo struct disasm_line *annotate_browser__find_string(struct annotate_browser *browser,
427aca7a94dSNamhyung Kim 					  char *s, s64 *idx)
428aca7a94dSNamhyung Kim {
429aca7a94dSNamhyung Kim 	struct map_symbol *ms = browser->b.priv;
430aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
431aca7a94dSNamhyung Kim 	struct annotation *notes = symbol__annotation(sym);
43229ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos = browser->selection;
433aca7a94dSNamhyung Kim 
434aca7a94dSNamhyung Kim 	*idx = browser->b.index;
435aca7a94dSNamhyung Kim 	list_for_each_entry_continue(pos, &notes->src->source, node) {
43629ed6e76SArnaldo Carvalho de Melo 		if (disasm_line__filter(&browser->b, &pos->node))
437aca7a94dSNamhyung Kim 			continue;
438aca7a94dSNamhyung Kim 
439aca7a94dSNamhyung Kim 		++*idx;
440aca7a94dSNamhyung Kim 
441aca7a94dSNamhyung Kim 		if (pos->line && strstr(pos->line, s) != NULL)
442aca7a94dSNamhyung Kim 			return pos;
443aca7a94dSNamhyung Kim 	}
444aca7a94dSNamhyung Kim 
445aca7a94dSNamhyung Kim 	return NULL;
446aca7a94dSNamhyung Kim }
447aca7a94dSNamhyung Kim 
448aca7a94dSNamhyung Kim static bool __annotate_browser__search(struct annotate_browser *browser)
449aca7a94dSNamhyung Kim {
45029ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *dl;
451aca7a94dSNamhyung Kim 	s64 idx;
452aca7a94dSNamhyung Kim 
45329ed6e76SArnaldo Carvalho de Melo 	dl = annotate_browser__find_string(browser, browser->search_bf, &idx);
45429ed6e76SArnaldo Carvalho de Melo 	if (dl == NULL) {
455aca7a94dSNamhyung Kim 		ui_helpline__puts("String not found!");
456aca7a94dSNamhyung Kim 		return false;
457aca7a94dSNamhyung Kim 	}
458aca7a94dSNamhyung Kim 
45929ed6e76SArnaldo Carvalho de Melo 	annotate_browser__set_top(browser, dl, idx);
460aca7a94dSNamhyung Kim 	browser->searching_backwards = false;
461aca7a94dSNamhyung Kim 	return true;
462aca7a94dSNamhyung Kim }
463aca7a94dSNamhyung Kim 
46429ed6e76SArnaldo Carvalho de Melo static
46529ed6e76SArnaldo Carvalho de Melo struct disasm_line *annotate_browser__find_string_reverse(struct annotate_browser *browser,
466aca7a94dSNamhyung Kim 						  char *s, s64 *idx)
467aca7a94dSNamhyung Kim {
468aca7a94dSNamhyung Kim 	struct map_symbol *ms = browser->b.priv;
469aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
470aca7a94dSNamhyung Kim 	struct annotation *notes = symbol__annotation(sym);
47129ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos = browser->selection;
472aca7a94dSNamhyung Kim 
473aca7a94dSNamhyung Kim 	*idx = browser->b.index;
474aca7a94dSNamhyung Kim 	list_for_each_entry_continue_reverse(pos, &notes->src->source, node) {
47529ed6e76SArnaldo Carvalho de Melo 		if (disasm_line__filter(&browser->b, &pos->node))
476aca7a94dSNamhyung Kim 			continue;
477aca7a94dSNamhyung Kim 
478aca7a94dSNamhyung Kim 		--*idx;
479aca7a94dSNamhyung Kim 
480aca7a94dSNamhyung Kim 		if (pos->line && strstr(pos->line, s) != NULL)
481aca7a94dSNamhyung Kim 			return pos;
482aca7a94dSNamhyung Kim 	}
483aca7a94dSNamhyung Kim 
484aca7a94dSNamhyung Kim 	return NULL;
485aca7a94dSNamhyung Kim }
486aca7a94dSNamhyung Kim 
487aca7a94dSNamhyung Kim static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
488aca7a94dSNamhyung Kim {
48929ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *dl;
490aca7a94dSNamhyung Kim 	s64 idx;
491aca7a94dSNamhyung Kim 
49229ed6e76SArnaldo Carvalho de Melo 	dl = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
49329ed6e76SArnaldo Carvalho de Melo 	if (dl == NULL) {
494aca7a94dSNamhyung Kim 		ui_helpline__puts("String not found!");
495aca7a94dSNamhyung Kim 		return false;
496aca7a94dSNamhyung Kim 	}
497aca7a94dSNamhyung Kim 
49829ed6e76SArnaldo Carvalho de Melo 	annotate_browser__set_top(browser, dl, idx);
499aca7a94dSNamhyung Kim 	browser->searching_backwards = true;
500aca7a94dSNamhyung Kim 	return true;
501aca7a94dSNamhyung Kim }
502aca7a94dSNamhyung Kim 
503aca7a94dSNamhyung Kim static bool annotate_browser__search_window(struct annotate_browser *browser,
504aca7a94dSNamhyung Kim 					    int delay_secs)
505aca7a94dSNamhyung Kim {
506aca7a94dSNamhyung Kim 	if (ui_browser__input_window("Search", "String: ", browser->search_bf,
507aca7a94dSNamhyung Kim 				     "ENTER: OK, ESC: Cancel",
508aca7a94dSNamhyung Kim 				     delay_secs * 2) != K_ENTER ||
509aca7a94dSNamhyung Kim 	    !*browser->search_bf)
510aca7a94dSNamhyung Kim 		return false;
511aca7a94dSNamhyung Kim 
512aca7a94dSNamhyung Kim 	return true;
513aca7a94dSNamhyung Kim }
514aca7a94dSNamhyung Kim 
515aca7a94dSNamhyung Kim static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs)
516aca7a94dSNamhyung Kim {
517aca7a94dSNamhyung Kim 	if (annotate_browser__search_window(browser, delay_secs))
518aca7a94dSNamhyung Kim 		return __annotate_browser__search(browser);
519aca7a94dSNamhyung Kim 
520aca7a94dSNamhyung Kim 	return false;
521aca7a94dSNamhyung Kim }
522aca7a94dSNamhyung Kim 
523aca7a94dSNamhyung Kim static bool annotate_browser__continue_search(struct annotate_browser *browser,
524aca7a94dSNamhyung Kim 					      int delay_secs)
525aca7a94dSNamhyung Kim {
526aca7a94dSNamhyung Kim 	if (!*browser->search_bf)
527aca7a94dSNamhyung Kim 		return annotate_browser__search(browser, delay_secs);
528aca7a94dSNamhyung Kim 
529aca7a94dSNamhyung Kim 	return __annotate_browser__search(browser);
530aca7a94dSNamhyung Kim }
531aca7a94dSNamhyung Kim 
532aca7a94dSNamhyung Kim static bool annotate_browser__search_reverse(struct annotate_browser *browser,
533aca7a94dSNamhyung Kim 					   int delay_secs)
534aca7a94dSNamhyung Kim {
535aca7a94dSNamhyung Kim 	if (annotate_browser__search_window(browser, delay_secs))
536aca7a94dSNamhyung Kim 		return __annotate_browser__search_reverse(browser);
537aca7a94dSNamhyung Kim 
538aca7a94dSNamhyung Kim 	return false;
539aca7a94dSNamhyung Kim }
540aca7a94dSNamhyung Kim 
541aca7a94dSNamhyung Kim static
542aca7a94dSNamhyung Kim bool annotate_browser__continue_search_reverse(struct annotate_browser *browser,
543aca7a94dSNamhyung Kim 					       int delay_secs)
544aca7a94dSNamhyung Kim {
545aca7a94dSNamhyung Kim 	if (!*browser->search_bf)
546aca7a94dSNamhyung Kim 		return annotate_browser__search_reverse(browser, delay_secs);
547aca7a94dSNamhyung Kim 
548aca7a94dSNamhyung Kim 	return __annotate_browser__search_reverse(browser);
549aca7a94dSNamhyung Kim }
550aca7a94dSNamhyung Kim 
551aca7a94dSNamhyung Kim static int annotate_browser__run(struct annotate_browser *self, int evidx,
552aca7a94dSNamhyung Kim 				 void(*timer)(void *arg),
553aca7a94dSNamhyung Kim 				 void *arg, int delay_secs)
554aca7a94dSNamhyung Kim {
555aca7a94dSNamhyung Kim 	struct rb_node *nd = NULL;
556aca7a94dSNamhyung Kim 	struct map_symbol *ms = self->b.priv;
557aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
558aca7a94dSNamhyung Kim 	const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
559aca7a94dSNamhyung Kim 			   "H: Go to hottest line, ->/ENTER: Line action, "
560aca7a94dSNamhyung Kim 			   "O: Toggle offset view, "
561aca7a94dSNamhyung Kim 			   "S: Toggle source code view";
562aca7a94dSNamhyung Kim 	int key;
563aca7a94dSNamhyung Kim 
564aca7a94dSNamhyung Kim 	if (ui_browser__show(&self->b, sym->name, help) < 0)
565aca7a94dSNamhyung Kim 		return -1;
566aca7a94dSNamhyung Kim 
567aca7a94dSNamhyung Kim 	annotate_browser__calc_percent(self, evidx);
568aca7a94dSNamhyung Kim 
569aca7a94dSNamhyung Kim 	if (self->curr_hot) {
570aca7a94dSNamhyung Kim 		annotate_browser__set_rb_top(self, self->curr_hot);
571aca7a94dSNamhyung Kim 		self->b.navkeypressed = false;
572aca7a94dSNamhyung Kim 	}
573aca7a94dSNamhyung Kim 
574aca7a94dSNamhyung Kim 	nd = self->curr_hot;
575aca7a94dSNamhyung Kim 
576aca7a94dSNamhyung Kim 	while (1) {
577aca7a94dSNamhyung Kim 		key = ui_browser__run(&self->b, delay_secs);
578aca7a94dSNamhyung Kim 
579aca7a94dSNamhyung Kim 		if (delay_secs != 0) {
580aca7a94dSNamhyung Kim 			annotate_browser__calc_percent(self, evidx);
581aca7a94dSNamhyung Kim 			/*
582aca7a94dSNamhyung Kim 			 * Current line focus got out of the list of most active
583aca7a94dSNamhyung Kim 			 * lines, NULL it so that if TAB|UNTAB is pressed, we
584aca7a94dSNamhyung Kim 			 * move to curr_hot (current hottest line).
585aca7a94dSNamhyung Kim 			 */
586aca7a94dSNamhyung Kim 			if (nd != NULL && RB_EMPTY_NODE(nd))
587aca7a94dSNamhyung Kim 				nd = NULL;
588aca7a94dSNamhyung Kim 		}
589aca7a94dSNamhyung Kim 
590aca7a94dSNamhyung Kim 		switch (key) {
591aca7a94dSNamhyung Kim 		case K_TIMER:
592aca7a94dSNamhyung Kim 			if (timer != NULL)
593aca7a94dSNamhyung Kim 				timer(arg);
594aca7a94dSNamhyung Kim 
595aca7a94dSNamhyung Kim 			if (delay_secs != 0)
596aca7a94dSNamhyung Kim 				symbol__annotate_decay_histogram(sym, evidx);
597aca7a94dSNamhyung Kim 			continue;
598aca7a94dSNamhyung Kim 		case K_TAB:
599aca7a94dSNamhyung Kim 			if (nd != NULL) {
600aca7a94dSNamhyung Kim 				nd = rb_prev(nd);
601aca7a94dSNamhyung Kim 				if (nd == NULL)
602aca7a94dSNamhyung Kim 					nd = rb_last(&self->entries);
603aca7a94dSNamhyung Kim 			} else
604aca7a94dSNamhyung Kim 				nd = self->curr_hot;
605aca7a94dSNamhyung Kim 			break;
606aca7a94dSNamhyung Kim 		case K_UNTAB:
607aca7a94dSNamhyung Kim 			if (nd != NULL)
608aca7a94dSNamhyung Kim 				nd = rb_next(nd);
609aca7a94dSNamhyung Kim 				if (nd == NULL)
610aca7a94dSNamhyung Kim 					nd = rb_first(&self->entries);
611aca7a94dSNamhyung Kim 			else
612aca7a94dSNamhyung Kim 				nd = self->curr_hot;
613aca7a94dSNamhyung Kim 			break;
614aca7a94dSNamhyung Kim 		case 'H':
615aca7a94dSNamhyung Kim 		case 'h':
616aca7a94dSNamhyung Kim 			nd = self->curr_hot;
617aca7a94dSNamhyung Kim 			break;
618aca7a94dSNamhyung Kim 		case 'S':
619aca7a94dSNamhyung Kim 		case 's':
620aca7a94dSNamhyung Kim 			if (annotate_browser__toggle_source(self))
621aca7a94dSNamhyung Kim 				ui_helpline__puts(help);
622aca7a94dSNamhyung Kim 			continue;
623aca7a94dSNamhyung Kim 		case 'O':
624aca7a94dSNamhyung Kim 		case 'o':
625aca7a94dSNamhyung Kim 			self->use_offset = !self->use_offset;
626aca7a94dSNamhyung Kim 			continue;
627aca7a94dSNamhyung Kim 		case '/':
628aca7a94dSNamhyung Kim 			if (annotate_browser__search(self, delay_secs)) {
629aca7a94dSNamhyung Kim show_help:
630aca7a94dSNamhyung Kim 				ui_helpline__puts(help);
631aca7a94dSNamhyung Kim 			}
632aca7a94dSNamhyung Kim 			continue;
633aca7a94dSNamhyung Kim 		case 'n':
634aca7a94dSNamhyung Kim 			if (self->searching_backwards ?
635aca7a94dSNamhyung Kim 			    annotate_browser__continue_search_reverse(self, delay_secs) :
636aca7a94dSNamhyung Kim 			    annotate_browser__continue_search(self, delay_secs))
637aca7a94dSNamhyung Kim 				goto show_help;
638aca7a94dSNamhyung Kim 			continue;
639aca7a94dSNamhyung Kim 		case '?':
640aca7a94dSNamhyung Kim 			if (annotate_browser__search_reverse(self, delay_secs))
641aca7a94dSNamhyung Kim 				goto show_help;
642aca7a94dSNamhyung Kim 			continue;
643aca7a94dSNamhyung Kim 		case K_ENTER:
644aca7a94dSNamhyung Kim 		case K_RIGHT:
645aca7a94dSNamhyung Kim 			if (self->selection == NULL)
646aca7a94dSNamhyung Kim 				ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
647aca7a94dSNamhyung Kim 			else if (self->selection->offset == -1)
648aca7a94dSNamhyung Kim 				ui_helpline__puts("Actions are only available for assembly lines.");
649c4cceae3SArnaldo Carvalho de Melo 			else if (!self->selection->ins) {
650c4cceae3SArnaldo Carvalho de Melo 				if (strcmp(self->selection->name, "retq"))
651c4cceae3SArnaldo Carvalho de Melo 					goto show_sup_ins;
652c4cceae3SArnaldo Carvalho de Melo 				goto out;
653c4cceae3SArnaldo Carvalho de Melo 			} else if (!(annotate_browser__jump(self) ||
654c4cceae3SArnaldo Carvalho de Melo 				     annotate_browser__callq(self, evidx, timer, arg, delay_secs))) {
655c4cceae3SArnaldo Carvalho de Melo show_sup_ins:
656c4cceae3SArnaldo Carvalho de Melo 				ui_helpline__puts("Actions are only available for 'callq', 'retq' & jump instructions.");
657c4cceae3SArnaldo Carvalho de Melo 			}
658aca7a94dSNamhyung Kim 			continue;
659aca7a94dSNamhyung Kim 		case K_LEFT:
660aca7a94dSNamhyung Kim 		case K_ESC:
661aca7a94dSNamhyung Kim 		case 'q':
662aca7a94dSNamhyung Kim 		case CTRL('c'):
663aca7a94dSNamhyung Kim 			goto out;
664aca7a94dSNamhyung Kim 		default:
665aca7a94dSNamhyung Kim 			continue;
666aca7a94dSNamhyung Kim 		}
667aca7a94dSNamhyung Kim 
668aca7a94dSNamhyung Kim 		if (nd != NULL)
669aca7a94dSNamhyung Kim 			annotate_browser__set_rb_top(self, nd);
670aca7a94dSNamhyung Kim 	}
671aca7a94dSNamhyung Kim out:
672aca7a94dSNamhyung Kim 	ui_browser__hide(&self->b);
673aca7a94dSNamhyung Kim 	return key;
674aca7a94dSNamhyung Kim }
675aca7a94dSNamhyung Kim 
676aca7a94dSNamhyung Kim int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
677aca7a94dSNamhyung Kim 			     void(*timer)(void *arg), void *arg, int delay_secs)
678aca7a94dSNamhyung Kim {
679aca7a94dSNamhyung Kim 	return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
680aca7a94dSNamhyung Kim 				    timer, arg, delay_secs);
681aca7a94dSNamhyung Kim }
682aca7a94dSNamhyung Kim 
683b793a401SArnaldo Carvalho de Melo static void annotate_browser__mark_jump_targets(struct annotate_browser *browser,
684b793a401SArnaldo Carvalho de Melo 						size_t size)
685b793a401SArnaldo Carvalho de Melo {
686b793a401SArnaldo Carvalho de Melo 	u64 offset;
687b793a401SArnaldo Carvalho de Melo 
688b793a401SArnaldo Carvalho de Melo 	for (offset = 0; offset < size; ++offset) {
689b793a401SArnaldo Carvalho de Melo 		struct disasm_line *dl = browser->offsets[offset], *dlt;
690b793a401SArnaldo Carvalho de Melo 		struct browser_disasm_line *bdlt;
691b793a401SArnaldo Carvalho de Melo 
692b793a401SArnaldo Carvalho de Melo 		if (!dl || !dl->ins || !ins__is_jump(dl->ins))
693b793a401SArnaldo Carvalho de Melo 			continue;
694b793a401SArnaldo Carvalho de Melo 
695*44d1a3edSArnaldo Carvalho de Melo 		if (dl->ops.target.offset >= size) {
696b793a401SArnaldo Carvalho de Melo 			ui__error("jump to after symbol!\n"
697b793a401SArnaldo Carvalho de Melo 				  "size: %zx, jump target: %" PRIx64,
698*44d1a3edSArnaldo Carvalho de Melo 				  size, dl->ops.target.offset);
699b793a401SArnaldo Carvalho de Melo 			continue;
700b793a401SArnaldo Carvalho de Melo 		}
701b793a401SArnaldo Carvalho de Melo 
702*44d1a3edSArnaldo Carvalho de Melo 		dlt = browser->offsets[dl->ops.target.offset];
7039481ede9SArnaldo Carvalho de Melo 		/*
7049481ede9SArnaldo Carvalho de Melo  		 * FIXME: Oops, no jump target? Buggy disassembler? Or do we
7059481ede9SArnaldo Carvalho de Melo  		 * have to adjust to the previous offset?
7069481ede9SArnaldo Carvalho de Melo  		 */
7079481ede9SArnaldo Carvalho de Melo 		if (dlt == NULL)
7089481ede9SArnaldo Carvalho de Melo 			continue;
7099481ede9SArnaldo Carvalho de Melo 
710b793a401SArnaldo Carvalho de Melo 		bdlt = disasm_line__browser(dlt);
711b793a401SArnaldo Carvalho de Melo 		bdlt->jump_target = true;
712b793a401SArnaldo Carvalho de Melo 	}
713b793a401SArnaldo Carvalho de Melo 
714b793a401SArnaldo Carvalho de Melo }
715b793a401SArnaldo Carvalho de Melo 
716aca7a94dSNamhyung Kim int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
717aca7a94dSNamhyung Kim 			 void(*timer)(void *arg), void *arg,
718aca7a94dSNamhyung Kim 			 int delay_secs)
719aca7a94dSNamhyung Kim {
72029ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos, *n;
721aca7a94dSNamhyung Kim 	struct annotation *notes;
722b793a401SArnaldo Carvalho de Melo 	const size_t size = symbol__size(sym);
723aca7a94dSNamhyung Kim 	struct map_symbol ms = {
724aca7a94dSNamhyung Kim 		.map = map,
725aca7a94dSNamhyung Kim 		.sym = sym,
726aca7a94dSNamhyung Kim 	};
727aca7a94dSNamhyung Kim 	struct annotate_browser browser = {
728aca7a94dSNamhyung Kim 		.b = {
729a3f895beSArnaldo Carvalho de Melo 			.refresh = annotate_browser__refresh,
730aca7a94dSNamhyung Kim 			.seek	 = ui_browser__list_head_seek,
731aca7a94dSNamhyung Kim 			.write	 = annotate_browser__write,
73229ed6e76SArnaldo Carvalho de Melo 			.filter  = disasm_line__filter,
733aca7a94dSNamhyung Kim 			.priv	 = &ms,
734aca7a94dSNamhyung Kim 			.use_navkeypressed = true,
735aca7a94dSNamhyung Kim 		},
7368bf39cb8SArnaldo Carvalho de Melo 		.use_offset = true,
737aca7a94dSNamhyung Kim 	};
738b793a401SArnaldo Carvalho de Melo 	int ret = -1;
739aca7a94dSNamhyung Kim 
740aca7a94dSNamhyung Kim 	if (sym == NULL)
741aca7a94dSNamhyung Kim 		return -1;
742aca7a94dSNamhyung Kim 
743aca7a94dSNamhyung Kim 	if (map->dso->annotate_warned)
744aca7a94dSNamhyung Kim 		return -1;
745aca7a94dSNamhyung Kim 
746b793a401SArnaldo Carvalho de Melo 	browser.offsets = zalloc(size * sizeof(struct disasm_line *));
747b793a401SArnaldo Carvalho de Melo 	if (browser.offsets == NULL) {
748b793a401SArnaldo Carvalho de Melo 		ui__error("Not enough memory!");
749b793a401SArnaldo Carvalho de Melo 		return -1;
750b793a401SArnaldo Carvalho de Melo 	}
751b793a401SArnaldo Carvalho de Melo 
752887c0066SArnaldo Carvalho de Melo 	if (symbol__annotate(sym, map, sizeof(struct browser_disasm_line)) < 0) {
753aca7a94dSNamhyung Kim 		ui__error("%s", ui_helpline__last_msg);
754b793a401SArnaldo Carvalho de Melo 		goto out_free_offsets;
755aca7a94dSNamhyung Kim 	}
756aca7a94dSNamhyung Kim 
757aca7a94dSNamhyung Kim 	ui_helpline__push("Press <- or ESC to exit");
758aca7a94dSNamhyung Kim 
759aca7a94dSNamhyung Kim 	notes = symbol__annotation(sym);
760aca7a94dSNamhyung Kim 	browser.start = map__rip_2objdump(map, sym->start);
761aca7a94dSNamhyung Kim 
762aca7a94dSNamhyung Kim 	list_for_each_entry(pos, &notes->src->source, node) {
763887c0066SArnaldo Carvalho de Melo 		struct browser_disasm_line *bpos;
764aca7a94dSNamhyung Kim 		size_t line_len = strlen(pos->line);
765aca7a94dSNamhyung Kim 
766aca7a94dSNamhyung Kim 		if (browser.b.width < line_len)
767aca7a94dSNamhyung Kim 			browser.b.width = line_len;
768887c0066SArnaldo Carvalho de Melo 		bpos = disasm_line__browser(pos);
769887c0066SArnaldo Carvalho de Melo 		bpos->idx = browser.nr_entries++;
770b793a401SArnaldo Carvalho de Melo 		if (pos->offset != -1) {
771887c0066SArnaldo Carvalho de Melo 			bpos->idx_asm = browser.nr_asm_entries++;
77297148a97SArnaldo Carvalho de Melo 			/*
77397148a97SArnaldo Carvalho de Melo 			 * FIXME: short term bandaid to cope with assembly
77497148a97SArnaldo Carvalho de Melo 			 * routines that comes with labels in the same column
77597148a97SArnaldo Carvalho de Melo 			 * as the address in objdump, sigh.
77697148a97SArnaldo Carvalho de Melo 			 *
77797148a97SArnaldo Carvalho de Melo 			 * E.g. copy_user_generic_unrolled
77897148a97SArnaldo Carvalho de Melo  			 */
77997148a97SArnaldo Carvalho de Melo 			if (pos->offset < (s64)size)
780b793a401SArnaldo Carvalho de Melo 				browser.offsets[pos->offset] = pos;
781b793a401SArnaldo Carvalho de Melo 		} else
782887c0066SArnaldo Carvalho de Melo 			bpos->idx_asm = -1;
783aca7a94dSNamhyung Kim 	}
784aca7a94dSNamhyung Kim 
785b793a401SArnaldo Carvalho de Melo 	annotate_browser__mark_jump_targets(&browser, size);
786b793a401SArnaldo Carvalho de Melo 
78761e04b33SArnaldo Carvalho de Melo 	browser.offset_width = hex_width(size);
788aca7a94dSNamhyung Kim 	browser.b.nr_entries = browser.nr_entries;
789aca7a94dSNamhyung Kim 	browser.b.entries = &notes->src->source,
790aca7a94dSNamhyung Kim 	browser.b.width += 18; /* Percentage */
791aca7a94dSNamhyung Kim 	ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
792aca7a94dSNamhyung Kim 	list_for_each_entry_safe(pos, n, &notes->src->source, node) {
793aca7a94dSNamhyung Kim 		list_del(&pos->node);
79429ed6e76SArnaldo Carvalho de Melo 		disasm_line__free(pos);
795aca7a94dSNamhyung Kim 	}
796b793a401SArnaldo Carvalho de Melo 
797b793a401SArnaldo Carvalho de Melo out_free_offsets:
798b793a401SArnaldo Carvalho de Melo 	free(browser.offsets);
799aca7a94dSNamhyung Kim 	return ret;
800aca7a94dSNamhyung Kim }
801