xref: /linux/tools/perf/ui/browsers/annotate.c (revision 4ea08b522071c6c34098b7a880aac3551abecd75)
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 
74cf2dacc5SArnaldo Carvalho de Melo 	SLsmg_set_char_set(1);
75cf2dacc5SArnaldo Carvalho de Melo 	SLsmg_write_char(SLSMG_VLINE_CHAR);
76cf2dacc5SArnaldo Carvalho de Melo 	SLsmg_set_char_set(0);
77cf2dacc5SArnaldo Carvalho de Melo 	SLsmg_write_char(' ');
78aca7a94dSNamhyung Kim 
79aca7a94dSNamhyung Kim 	/* The scroll bar isn't being used */
80aca7a94dSNamhyung Kim 	if (!self->navkeypressed)
81aca7a94dSNamhyung Kim 		width += 1;
82aca7a94dSNamhyung Kim 
8329ed6e76SArnaldo Carvalho de Melo 	if (dl->offset != -1 && change_color)
84aca7a94dSNamhyung Kim 		ui_browser__set_color(self, HE_COLORSET_CODE);
85aca7a94dSNamhyung Kim 
8629ed6e76SArnaldo Carvalho de Melo 	if (!*dl->line)
878bf39cb8SArnaldo Carvalho de Melo 		slsmg_write_nstring(" ", width - 10);
8829ed6e76SArnaldo Carvalho de Melo 	else if (dl->offset == -1)
898bf39cb8SArnaldo Carvalho de Melo 		slsmg_write_nstring(dl->line, width - 10);
90aca7a94dSNamhyung Kim 	else {
918bf39cb8SArnaldo Carvalho de Melo 		char bf[256];
9229ed6e76SArnaldo Carvalho de Melo 		u64 addr = dl->offset;
93aca7a94dSNamhyung Kim 		int printed, color = -1;
94aca7a94dSNamhyung Kim 
95aca7a94dSNamhyung Kim 		if (!ab->use_offset)
96aca7a94dSNamhyung Kim 			addr += ab->start;
97aca7a94dSNamhyung Kim 
9861e04b33SArnaldo Carvalho de Melo 		if (!ab->use_offset) {
99aca7a94dSNamhyung Kim 			printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ":", addr);
10061e04b33SArnaldo Carvalho de Melo 		} else {
10161e04b33SArnaldo Carvalho de Melo 			if (bdl->jump_target) {
10261e04b33SArnaldo Carvalho de Melo 				printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ":",
10361e04b33SArnaldo Carvalho de Melo 						    ab->offset_width, addr);
10461e04b33SArnaldo Carvalho de Melo 			} else {
10561e04b33SArnaldo Carvalho de Melo 				printed = scnprintf(bf, sizeof(bf), "%*s ",
10661e04b33SArnaldo Carvalho de Melo 						    ab->offset_width, " ");
10761e04b33SArnaldo Carvalho de Melo 			}
10861e04b33SArnaldo Carvalho de Melo 		}
109b793a401SArnaldo Carvalho de Melo 
110aca7a94dSNamhyung Kim 		if (change_color)
111aca7a94dSNamhyung Kim 			color = ui_browser__set_color(self, HE_COLORSET_ADDR);
112aca7a94dSNamhyung Kim 		slsmg_write_nstring(bf, printed);
113aca7a94dSNamhyung Kim 		if (change_color)
114aca7a94dSNamhyung Kim 			ui_browser__set_color(self, color);
11528548d78SArnaldo Carvalho de Melo 		if (dl->ins && dl->ins->ops->scnprintf) {
11651a0d455SArnaldo Carvalho de Melo 			if (ins__is_jump(dl->ins)) {
11751a0d455SArnaldo Carvalho de Melo 				bool fwd = dl->ops.target > (u64)dl->offset;
11851a0d455SArnaldo Carvalho de Melo 
11951a0d455SArnaldo Carvalho de Melo 				SLsmg_set_char_set(1);
12051a0d455SArnaldo Carvalho de Melo 				SLsmg_write_char(fwd ? SLSMG_DARROW_CHAR :
12151a0d455SArnaldo Carvalho de Melo 						       SLSMG_UARROW_CHAR);
12251a0d455SArnaldo Carvalho de Melo 				SLsmg_set_char_set(0);
12351a0d455SArnaldo Carvalho de Melo 				SLsmg_write_char(' ');
12451a0d455SArnaldo Carvalho de Melo 			} else {
12551a0d455SArnaldo Carvalho de Melo 				slsmg_write_nstring(" ", 2);
12651a0d455SArnaldo Carvalho de Melo 			}
12751a0d455SArnaldo Carvalho de Melo 
128c7e6ead7SArnaldo Carvalho de Melo 			dl->ins->ops->scnprintf(dl->ins, bf, sizeof(bf), &dl->ops,
129c7e6ead7SArnaldo Carvalho de Melo 						!ab->use_offset);
130*4ea08b52SArnaldo Carvalho de Melo 		} else {
131*4ea08b52SArnaldo Carvalho de Melo 			if (strcmp(dl->name, "retq")) {
132*4ea08b52SArnaldo Carvalho de Melo 				slsmg_write_nstring(" ", 2);
133*4ea08b52SArnaldo Carvalho de Melo 			} else {
134*4ea08b52SArnaldo Carvalho de Melo 				SLsmg_set_char_set(1);
135*4ea08b52SArnaldo Carvalho de Melo 				SLsmg_write_char(SLSMG_LARROW_CHAR);
136*4ea08b52SArnaldo Carvalho de Melo 				SLsmg_set_char_set(0);
137*4ea08b52SArnaldo Carvalho de Melo 				SLsmg_write_char(' ');
138*4ea08b52SArnaldo Carvalho de Melo 			}
13928548d78SArnaldo Carvalho de Melo 
140*4ea08b52SArnaldo Carvalho de Melo 			scnprintf(bf, sizeof(bf), "%-6.6s %s", dl->name, dl->ops.raw);
141*4ea08b52SArnaldo Carvalho de Melo 		}
142*4ea08b52SArnaldo Carvalho de Melo 
143*4ea08b52SArnaldo Carvalho de Melo 		slsmg_write_nstring(bf, width - 12 - printed);
144aca7a94dSNamhyung Kim 	}
145aca7a94dSNamhyung Kim 
146aca7a94dSNamhyung Kim 	if (current_entry)
14729ed6e76SArnaldo Carvalho de Melo 		ab->selection = dl;
148aca7a94dSNamhyung Kim }
149aca7a94dSNamhyung Kim 
15029ed6e76SArnaldo Carvalho de Melo static double disasm_line__calc_percent(struct disasm_line *dl, struct symbol *sym, int evidx)
151aca7a94dSNamhyung Kim {
152aca7a94dSNamhyung Kim 	double percent = 0.0;
153aca7a94dSNamhyung Kim 
15429ed6e76SArnaldo Carvalho de Melo 	if (dl->offset != -1) {
155aca7a94dSNamhyung Kim 		int len = sym->end - sym->start;
156aca7a94dSNamhyung Kim 		unsigned int hits = 0;
157aca7a94dSNamhyung Kim 		struct annotation *notes = symbol__annotation(sym);
158aca7a94dSNamhyung Kim 		struct source_line *src_line = notes->src->lines;
159aca7a94dSNamhyung Kim 		struct sym_hist *h = annotation__histogram(notes, evidx);
16029ed6e76SArnaldo Carvalho de Melo 		s64 offset = dl->offset;
16129ed6e76SArnaldo Carvalho de Melo 		struct disasm_line *next;
162aca7a94dSNamhyung Kim 
16329ed6e76SArnaldo Carvalho de Melo 		next = disasm__get_next_ip_line(&notes->src->source, dl);
164aca7a94dSNamhyung Kim 		while (offset < (s64)len &&
165aca7a94dSNamhyung Kim 		       (next == NULL || offset < next->offset)) {
166aca7a94dSNamhyung Kim 			if (src_line) {
167aca7a94dSNamhyung Kim 				percent += src_line[offset].percent;
168aca7a94dSNamhyung Kim 			} else
169aca7a94dSNamhyung Kim 				hits += h->addr[offset];
170aca7a94dSNamhyung Kim 
171aca7a94dSNamhyung Kim 			++offset;
172aca7a94dSNamhyung Kim 		}
173aca7a94dSNamhyung Kim 		/*
174aca7a94dSNamhyung Kim  		 * If the percentage wasn't already calculated in
175aca7a94dSNamhyung Kim  		 * symbol__get_source_line, do it now:
176aca7a94dSNamhyung Kim  		 */
177aca7a94dSNamhyung Kim 		if (src_line == NULL && h->sum)
178aca7a94dSNamhyung Kim 			percent = 100.0 * hits / h->sum;
179aca7a94dSNamhyung Kim 	}
180aca7a94dSNamhyung Kim 
181aca7a94dSNamhyung Kim 	return percent;
182aca7a94dSNamhyung Kim }
183aca7a94dSNamhyung Kim 
184887c0066SArnaldo Carvalho de Melo static void disasm_rb_tree__insert(struct rb_root *root, struct browser_disasm_line *bdl)
185aca7a94dSNamhyung Kim {
18629ed6e76SArnaldo Carvalho de Melo 	struct rb_node **p = &root->rb_node;
187aca7a94dSNamhyung Kim 	struct rb_node *parent = NULL;
188887c0066SArnaldo Carvalho de Melo 	struct browser_disasm_line *l;
189aca7a94dSNamhyung Kim 
190aca7a94dSNamhyung Kim 	while (*p != NULL) {
191aca7a94dSNamhyung Kim 		parent = *p;
192887c0066SArnaldo Carvalho de Melo 		l = rb_entry(parent, struct browser_disasm_line, rb_node);
193887c0066SArnaldo Carvalho de Melo 		if (bdl->percent < l->percent)
194aca7a94dSNamhyung Kim 			p = &(*p)->rb_left;
195aca7a94dSNamhyung Kim 		else
196aca7a94dSNamhyung Kim 			p = &(*p)->rb_right;
197aca7a94dSNamhyung Kim 	}
198887c0066SArnaldo Carvalho de Melo 	rb_link_node(&bdl->rb_node, parent, p);
199887c0066SArnaldo Carvalho de Melo 	rb_insert_color(&bdl->rb_node, root);
200aca7a94dSNamhyung Kim }
201aca7a94dSNamhyung Kim 
202aca7a94dSNamhyung Kim static void annotate_browser__set_top(struct annotate_browser *self,
20329ed6e76SArnaldo Carvalho de Melo 				      struct disasm_line *pos, u32 idx)
204aca7a94dSNamhyung Kim {
205aca7a94dSNamhyung Kim 	unsigned back;
206aca7a94dSNamhyung Kim 
207aca7a94dSNamhyung Kim 	ui_browser__refresh_dimensions(&self->b);
208aca7a94dSNamhyung Kim 	back = self->b.height / 2;
209aca7a94dSNamhyung Kim 	self->b.top_idx = self->b.index = idx;
210aca7a94dSNamhyung Kim 
211aca7a94dSNamhyung Kim 	while (self->b.top_idx != 0 && back != 0) {
21229ed6e76SArnaldo Carvalho de Melo 		pos = list_entry(pos->node.prev, struct disasm_line, node);
213aca7a94dSNamhyung Kim 
21429ed6e76SArnaldo Carvalho de Melo 		if (disasm_line__filter(&self->b, &pos->node))
215aca7a94dSNamhyung Kim 			continue;
216aca7a94dSNamhyung Kim 
217aca7a94dSNamhyung Kim 		--self->b.top_idx;
218aca7a94dSNamhyung Kim 		--back;
219aca7a94dSNamhyung Kim 	}
220aca7a94dSNamhyung Kim 
221aca7a94dSNamhyung Kim 	self->b.top = pos;
222aca7a94dSNamhyung Kim 	self->b.navkeypressed = true;
223aca7a94dSNamhyung Kim }
224aca7a94dSNamhyung Kim 
225aca7a94dSNamhyung Kim static void annotate_browser__set_rb_top(struct annotate_browser *browser,
226aca7a94dSNamhyung Kim 					 struct rb_node *nd)
227aca7a94dSNamhyung Kim {
228887c0066SArnaldo Carvalho de Melo 	struct browser_disasm_line *bpos;
22929ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos;
230aca7a94dSNamhyung Kim 
231887c0066SArnaldo Carvalho de Melo 	bpos = rb_entry(nd, struct browser_disasm_line, rb_node);
232887c0066SArnaldo Carvalho de Melo 	pos = ((struct disasm_line *)bpos) - 1;
233887c0066SArnaldo Carvalho de Melo 	annotate_browser__set_top(browser, pos, bpos->idx);
234aca7a94dSNamhyung Kim 	browser->curr_hot = nd;
235aca7a94dSNamhyung Kim }
236aca7a94dSNamhyung Kim 
237aca7a94dSNamhyung Kim static void annotate_browser__calc_percent(struct annotate_browser *browser,
238aca7a94dSNamhyung Kim 					   int evidx)
239aca7a94dSNamhyung Kim {
240aca7a94dSNamhyung Kim 	struct map_symbol *ms = browser->b.priv;
241aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
242aca7a94dSNamhyung Kim 	struct annotation *notes = symbol__annotation(sym);
24329ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos;
244aca7a94dSNamhyung Kim 
245aca7a94dSNamhyung Kim 	browser->entries = RB_ROOT;
246aca7a94dSNamhyung Kim 
247aca7a94dSNamhyung Kim 	pthread_mutex_lock(&notes->lock);
248aca7a94dSNamhyung Kim 
249aca7a94dSNamhyung Kim 	list_for_each_entry(pos, &notes->src->source, node) {
250887c0066SArnaldo Carvalho de Melo 		struct browser_disasm_line *bpos = disasm_line__browser(pos);
251887c0066SArnaldo Carvalho de Melo 		bpos->percent = disasm_line__calc_percent(pos, sym, evidx);
252887c0066SArnaldo Carvalho de Melo 		if (bpos->percent < 0.01) {
253887c0066SArnaldo Carvalho de Melo 			RB_CLEAR_NODE(&bpos->rb_node);
254aca7a94dSNamhyung Kim 			continue;
255aca7a94dSNamhyung Kim 		}
256887c0066SArnaldo Carvalho de Melo 		disasm_rb_tree__insert(&browser->entries, bpos);
257aca7a94dSNamhyung Kim 	}
258aca7a94dSNamhyung Kim 	pthread_mutex_unlock(&notes->lock);
259aca7a94dSNamhyung Kim 
260aca7a94dSNamhyung Kim 	browser->curr_hot = rb_last(&browser->entries);
261aca7a94dSNamhyung Kim }
262aca7a94dSNamhyung Kim 
263aca7a94dSNamhyung Kim static bool annotate_browser__toggle_source(struct annotate_browser *browser)
264aca7a94dSNamhyung Kim {
26529ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *dl;
266887c0066SArnaldo Carvalho de Melo 	struct browser_disasm_line *bdl;
267aca7a94dSNamhyung Kim 	off_t offset = browser->b.index - browser->b.top_idx;
268aca7a94dSNamhyung Kim 
269aca7a94dSNamhyung Kim 	browser->b.seek(&browser->b, offset, SEEK_CUR);
27029ed6e76SArnaldo Carvalho de Melo 	dl = list_entry(browser->b.top, struct disasm_line, node);
271887c0066SArnaldo Carvalho de Melo 	bdl = disasm_line__browser(dl);
272aca7a94dSNamhyung Kim 
273aca7a94dSNamhyung Kim 	if (browser->hide_src_code) {
274887c0066SArnaldo Carvalho de Melo 		if (bdl->idx_asm < offset)
275887c0066SArnaldo Carvalho de Melo 			offset = bdl->idx;
276aca7a94dSNamhyung Kim 
277aca7a94dSNamhyung Kim 		browser->b.nr_entries = browser->nr_entries;
278aca7a94dSNamhyung Kim 		browser->hide_src_code = false;
279aca7a94dSNamhyung Kim 		browser->b.seek(&browser->b, -offset, SEEK_CUR);
280887c0066SArnaldo Carvalho de Melo 		browser->b.top_idx = bdl->idx - offset;
281887c0066SArnaldo Carvalho de Melo 		browser->b.index = bdl->idx;
282aca7a94dSNamhyung Kim 	} else {
283887c0066SArnaldo Carvalho de Melo 		if (bdl->idx_asm < 0) {
284aca7a94dSNamhyung Kim 			ui_helpline__puts("Only available for assembly lines.");
285aca7a94dSNamhyung Kim 			browser->b.seek(&browser->b, -offset, SEEK_CUR);
286aca7a94dSNamhyung Kim 			return false;
287aca7a94dSNamhyung Kim 		}
288aca7a94dSNamhyung Kim 
289887c0066SArnaldo Carvalho de Melo 		if (bdl->idx_asm < offset)
290887c0066SArnaldo Carvalho de Melo 			offset = bdl->idx_asm;
291aca7a94dSNamhyung Kim 
292aca7a94dSNamhyung Kim 		browser->b.nr_entries = browser->nr_asm_entries;
293aca7a94dSNamhyung Kim 		browser->hide_src_code = true;
294aca7a94dSNamhyung Kim 		browser->b.seek(&browser->b, -offset, SEEK_CUR);
295887c0066SArnaldo Carvalho de Melo 		browser->b.top_idx = bdl->idx_asm - offset;
296887c0066SArnaldo Carvalho de Melo 		browser->b.index = bdl->idx_asm;
297aca7a94dSNamhyung Kim 	}
298aca7a94dSNamhyung Kim 
299aca7a94dSNamhyung Kim 	return true;
300aca7a94dSNamhyung Kim }
301aca7a94dSNamhyung Kim 
302aca7a94dSNamhyung Kim static bool annotate_browser__callq(struct annotate_browser *browser,
303aca7a94dSNamhyung Kim 				    int evidx, void (*timer)(void *arg),
304aca7a94dSNamhyung Kim 				    void *arg, int delay_secs)
305aca7a94dSNamhyung Kim {
306aca7a94dSNamhyung Kim 	struct map_symbol *ms = browser->b.priv;
307657bcaf5SArnaldo Carvalho de Melo 	struct disasm_line *dl = browser->selection;
308aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
309aca7a94dSNamhyung Kim 	struct annotation *notes;
310aca7a94dSNamhyung Kim 	struct symbol *target;
311aca7a94dSNamhyung Kim 	u64 ip;
312aca7a94dSNamhyung Kim 
313d86b0597SArnaldo Carvalho de Melo 	if (!ins__is_call(dl->ins))
314aca7a94dSNamhyung Kim 		return false;
315aca7a94dSNamhyung Kim 
316c7e6ead7SArnaldo Carvalho de Melo 	ip = ms->map->map_ip(ms->map, dl->ops.target);
317aca7a94dSNamhyung Kim 	target = map__find_symbol(ms->map, ip, NULL);
318aca7a94dSNamhyung Kim 	if (target == NULL) {
319aca7a94dSNamhyung Kim 		ui_helpline__puts("The called function was not found.");
320aca7a94dSNamhyung Kim 		return true;
321aca7a94dSNamhyung Kim 	}
322aca7a94dSNamhyung Kim 
323aca7a94dSNamhyung Kim 	notes = symbol__annotation(target);
324aca7a94dSNamhyung Kim 	pthread_mutex_lock(&notes->lock);
325aca7a94dSNamhyung Kim 
326aca7a94dSNamhyung Kim 	if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
327aca7a94dSNamhyung Kim 		pthread_mutex_unlock(&notes->lock);
328aca7a94dSNamhyung Kim 		ui__warning("Not enough memory for annotating '%s' symbol!\n",
329aca7a94dSNamhyung Kim 			    target->name);
330aca7a94dSNamhyung Kim 		return true;
331aca7a94dSNamhyung Kim 	}
332aca7a94dSNamhyung Kim 
333aca7a94dSNamhyung Kim 	pthread_mutex_unlock(&notes->lock);
334aca7a94dSNamhyung Kim 	symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
335aca7a94dSNamhyung Kim 	ui_browser__show_title(&browser->b, sym->name);
336aca7a94dSNamhyung Kim 	return true;
337aca7a94dSNamhyung Kim }
338aca7a94dSNamhyung Kim 
33929ed6e76SArnaldo Carvalho de Melo static
34029ed6e76SArnaldo Carvalho de Melo struct disasm_line *annotate_browser__find_offset(struct annotate_browser *browser,
341aca7a94dSNamhyung Kim 					  s64 offset, s64 *idx)
342aca7a94dSNamhyung Kim {
343aca7a94dSNamhyung Kim 	struct map_symbol *ms = browser->b.priv;
344aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
345aca7a94dSNamhyung Kim 	struct annotation *notes = symbol__annotation(sym);
34629ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos;
347aca7a94dSNamhyung Kim 
348aca7a94dSNamhyung Kim 	*idx = 0;
349aca7a94dSNamhyung Kim 	list_for_each_entry(pos, &notes->src->source, node) {
350aca7a94dSNamhyung Kim 		if (pos->offset == offset)
351aca7a94dSNamhyung Kim 			return pos;
35229ed6e76SArnaldo Carvalho de Melo 		if (!disasm_line__filter(&browser->b, &pos->node))
353aca7a94dSNamhyung Kim 			++*idx;
354aca7a94dSNamhyung Kim 	}
355aca7a94dSNamhyung Kim 
356aca7a94dSNamhyung Kim 	return NULL;
357aca7a94dSNamhyung Kim }
358aca7a94dSNamhyung Kim 
359aca7a94dSNamhyung Kim static bool annotate_browser__jump(struct annotate_browser *browser)
360aca7a94dSNamhyung Kim {
361657bcaf5SArnaldo Carvalho de Melo 	struct disasm_line *dl = browser->selection;
3624f9d0325SArnaldo Carvalho de Melo 	s64 idx;
363aca7a94dSNamhyung Kim 
364d86b0597SArnaldo Carvalho de Melo 	if (!ins__is_jump(dl->ins))
365aca7a94dSNamhyung Kim 		return false;
366aca7a94dSNamhyung Kim 
367c7e6ead7SArnaldo Carvalho de Melo 	dl = annotate_browser__find_offset(browser, dl->ops.target, &idx);
36829ed6e76SArnaldo Carvalho de Melo 	if (dl == NULL) {
369aca7a94dSNamhyung Kim 		ui_helpline__puts("Invallid jump offset");
370aca7a94dSNamhyung Kim 		return true;
371aca7a94dSNamhyung Kim 	}
372aca7a94dSNamhyung Kim 
37329ed6e76SArnaldo Carvalho de Melo 	annotate_browser__set_top(browser, dl, idx);
374aca7a94dSNamhyung Kim 
375aca7a94dSNamhyung Kim 	return true;
376aca7a94dSNamhyung Kim }
377aca7a94dSNamhyung Kim 
37829ed6e76SArnaldo Carvalho de Melo static
37929ed6e76SArnaldo Carvalho de Melo struct disasm_line *annotate_browser__find_string(struct annotate_browser *browser,
380aca7a94dSNamhyung Kim 					  char *s, s64 *idx)
381aca7a94dSNamhyung Kim {
382aca7a94dSNamhyung Kim 	struct map_symbol *ms = browser->b.priv;
383aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
384aca7a94dSNamhyung Kim 	struct annotation *notes = symbol__annotation(sym);
38529ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos = browser->selection;
386aca7a94dSNamhyung Kim 
387aca7a94dSNamhyung Kim 	*idx = browser->b.index;
388aca7a94dSNamhyung Kim 	list_for_each_entry_continue(pos, &notes->src->source, node) {
38929ed6e76SArnaldo Carvalho de Melo 		if (disasm_line__filter(&browser->b, &pos->node))
390aca7a94dSNamhyung Kim 			continue;
391aca7a94dSNamhyung Kim 
392aca7a94dSNamhyung Kim 		++*idx;
393aca7a94dSNamhyung Kim 
394aca7a94dSNamhyung Kim 		if (pos->line && strstr(pos->line, s) != NULL)
395aca7a94dSNamhyung Kim 			return pos;
396aca7a94dSNamhyung Kim 	}
397aca7a94dSNamhyung Kim 
398aca7a94dSNamhyung Kim 	return NULL;
399aca7a94dSNamhyung Kim }
400aca7a94dSNamhyung Kim 
401aca7a94dSNamhyung Kim static bool __annotate_browser__search(struct annotate_browser *browser)
402aca7a94dSNamhyung Kim {
40329ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *dl;
404aca7a94dSNamhyung Kim 	s64 idx;
405aca7a94dSNamhyung Kim 
40629ed6e76SArnaldo Carvalho de Melo 	dl = annotate_browser__find_string(browser, browser->search_bf, &idx);
40729ed6e76SArnaldo Carvalho de Melo 	if (dl == NULL) {
408aca7a94dSNamhyung Kim 		ui_helpline__puts("String not found!");
409aca7a94dSNamhyung Kim 		return false;
410aca7a94dSNamhyung Kim 	}
411aca7a94dSNamhyung Kim 
41229ed6e76SArnaldo Carvalho de Melo 	annotate_browser__set_top(browser, dl, idx);
413aca7a94dSNamhyung Kim 	browser->searching_backwards = false;
414aca7a94dSNamhyung Kim 	return true;
415aca7a94dSNamhyung Kim }
416aca7a94dSNamhyung Kim 
41729ed6e76SArnaldo Carvalho de Melo static
41829ed6e76SArnaldo Carvalho de Melo struct disasm_line *annotate_browser__find_string_reverse(struct annotate_browser *browser,
419aca7a94dSNamhyung Kim 						  char *s, s64 *idx)
420aca7a94dSNamhyung Kim {
421aca7a94dSNamhyung Kim 	struct map_symbol *ms = browser->b.priv;
422aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
423aca7a94dSNamhyung Kim 	struct annotation *notes = symbol__annotation(sym);
42429ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos = browser->selection;
425aca7a94dSNamhyung Kim 
426aca7a94dSNamhyung Kim 	*idx = browser->b.index;
427aca7a94dSNamhyung Kim 	list_for_each_entry_continue_reverse(pos, &notes->src->source, node) {
42829ed6e76SArnaldo Carvalho de Melo 		if (disasm_line__filter(&browser->b, &pos->node))
429aca7a94dSNamhyung Kim 			continue;
430aca7a94dSNamhyung Kim 
431aca7a94dSNamhyung Kim 		--*idx;
432aca7a94dSNamhyung Kim 
433aca7a94dSNamhyung Kim 		if (pos->line && strstr(pos->line, s) != NULL)
434aca7a94dSNamhyung Kim 			return pos;
435aca7a94dSNamhyung Kim 	}
436aca7a94dSNamhyung Kim 
437aca7a94dSNamhyung Kim 	return NULL;
438aca7a94dSNamhyung Kim }
439aca7a94dSNamhyung Kim 
440aca7a94dSNamhyung Kim static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
441aca7a94dSNamhyung Kim {
44229ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *dl;
443aca7a94dSNamhyung Kim 	s64 idx;
444aca7a94dSNamhyung Kim 
44529ed6e76SArnaldo Carvalho de Melo 	dl = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
44629ed6e76SArnaldo Carvalho de Melo 	if (dl == NULL) {
447aca7a94dSNamhyung Kim 		ui_helpline__puts("String not found!");
448aca7a94dSNamhyung Kim 		return false;
449aca7a94dSNamhyung Kim 	}
450aca7a94dSNamhyung Kim 
45129ed6e76SArnaldo Carvalho de Melo 	annotate_browser__set_top(browser, dl, idx);
452aca7a94dSNamhyung Kim 	browser->searching_backwards = true;
453aca7a94dSNamhyung Kim 	return true;
454aca7a94dSNamhyung Kim }
455aca7a94dSNamhyung Kim 
456aca7a94dSNamhyung Kim static bool annotate_browser__search_window(struct annotate_browser *browser,
457aca7a94dSNamhyung Kim 					    int delay_secs)
458aca7a94dSNamhyung Kim {
459aca7a94dSNamhyung Kim 	if (ui_browser__input_window("Search", "String: ", browser->search_bf,
460aca7a94dSNamhyung Kim 				     "ENTER: OK, ESC: Cancel",
461aca7a94dSNamhyung Kim 				     delay_secs * 2) != K_ENTER ||
462aca7a94dSNamhyung Kim 	    !*browser->search_bf)
463aca7a94dSNamhyung Kim 		return false;
464aca7a94dSNamhyung Kim 
465aca7a94dSNamhyung Kim 	return true;
466aca7a94dSNamhyung Kim }
467aca7a94dSNamhyung Kim 
468aca7a94dSNamhyung Kim static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs)
469aca7a94dSNamhyung Kim {
470aca7a94dSNamhyung Kim 	if (annotate_browser__search_window(browser, delay_secs))
471aca7a94dSNamhyung Kim 		return __annotate_browser__search(browser);
472aca7a94dSNamhyung Kim 
473aca7a94dSNamhyung Kim 	return false;
474aca7a94dSNamhyung Kim }
475aca7a94dSNamhyung Kim 
476aca7a94dSNamhyung Kim static bool annotate_browser__continue_search(struct annotate_browser *browser,
477aca7a94dSNamhyung Kim 					      int delay_secs)
478aca7a94dSNamhyung Kim {
479aca7a94dSNamhyung Kim 	if (!*browser->search_bf)
480aca7a94dSNamhyung Kim 		return annotate_browser__search(browser, delay_secs);
481aca7a94dSNamhyung Kim 
482aca7a94dSNamhyung Kim 	return __annotate_browser__search(browser);
483aca7a94dSNamhyung Kim }
484aca7a94dSNamhyung Kim 
485aca7a94dSNamhyung Kim static bool annotate_browser__search_reverse(struct annotate_browser *browser,
486aca7a94dSNamhyung Kim 					   int delay_secs)
487aca7a94dSNamhyung Kim {
488aca7a94dSNamhyung Kim 	if (annotate_browser__search_window(browser, delay_secs))
489aca7a94dSNamhyung Kim 		return __annotate_browser__search_reverse(browser);
490aca7a94dSNamhyung Kim 
491aca7a94dSNamhyung Kim 	return false;
492aca7a94dSNamhyung Kim }
493aca7a94dSNamhyung Kim 
494aca7a94dSNamhyung Kim static
495aca7a94dSNamhyung Kim bool annotate_browser__continue_search_reverse(struct annotate_browser *browser,
496aca7a94dSNamhyung Kim 					       int delay_secs)
497aca7a94dSNamhyung Kim {
498aca7a94dSNamhyung Kim 	if (!*browser->search_bf)
499aca7a94dSNamhyung Kim 		return annotate_browser__search_reverse(browser, delay_secs);
500aca7a94dSNamhyung Kim 
501aca7a94dSNamhyung Kim 	return __annotate_browser__search_reverse(browser);
502aca7a94dSNamhyung Kim }
503aca7a94dSNamhyung Kim 
504aca7a94dSNamhyung Kim static int annotate_browser__run(struct annotate_browser *self, int evidx,
505aca7a94dSNamhyung Kim 				 void(*timer)(void *arg),
506aca7a94dSNamhyung Kim 				 void *arg, int delay_secs)
507aca7a94dSNamhyung Kim {
508aca7a94dSNamhyung Kim 	struct rb_node *nd = NULL;
509aca7a94dSNamhyung Kim 	struct map_symbol *ms = self->b.priv;
510aca7a94dSNamhyung Kim 	struct symbol *sym = ms->sym;
511aca7a94dSNamhyung Kim 	const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
512aca7a94dSNamhyung Kim 			   "H: Go to hottest line, ->/ENTER: Line action, "
513aca7a94dSNamhyung Kim 			   "O: Toggle offset view, "
514aca7a94dSNamhyung Kim 			   "S: Toggle source code view";
515aca7a94dSNamhyung Kim 	int key;
516aca7a94dSNamhyung Kim 
517aca7a94dSNamhyung Kim 	if (ui_browser__show(&self->b, sym->name, help) < 0)
518aca7a94dSNamhyung Kim 		return -1;
519aca7a94dSNamhyung Kim 
520aca7a94dSNamhyung Kim 	annotate_browser__calc_percent(self, evidx);
521aca7a94dSNamhyung Kim 
522aca7a94dSNamhyung Kim 	if (self->curr_hot) {
523aca7a94dSNamhyung Kim 		annotate_browser__set_rb_top(self, self->curr_hot);
524aca7a94dSNamhyung Kim 		self->b.navkeypressed = false;
525aca7a94dSNamhyung Kim 	}
526aca7a94dSNamhyung Kim 
527aca7a94dSNamhyung Kim 	nd = self->curr_hot;
528aca7a94dSNamhyung Kim 
529aca7a94dSNamhyung Kim 	while (1) {
530aca7a94dSNamhyung Kim 		key = ui_browser__run(&self->b, delay_secs);
531aca7a94dSNamhyung Kim 
532aca7a94dSNamhyung Kim 		if (delay_secs != 0) {
533aca7a94dSNamhyung Kim 			annotate_browser__calc_percent(self, evidx);
534aca7a94dSNamhyung Kim 			/*
535aca7a94dSNamhyung Kim 			 * Current line focus got out of the list of most active
536aca7a94dSNamhyung Kim 			 * lines, NULL it so that if TAB|UNTAB is pressed, we
537aca7a94dSNamhyung Kim 			 * move to curr_hot (current hottest line).
538aca7a94dSNamhyung Kim 			 */
539aca7a94dSNamhyung Kim 			if (nd != NULL && RB_EMPTY_NODE(nd))
540aca7a94dSNamhyung Kim 				nd = NULL;
541aca7a94dSNamhyung Kim 		}
542aca7a94dSNamhyung Kim 
543aca7a94dSNamhyung Kim 		switch (key) {
544aca7a94dSNamhyung Kim 		case K_TIMER:
545aca7a94dSNamhyung Kim 			if (timer != NULL)
546aca7a94dSNamhyung Kim 				timer(arg);
547aca7a94dSNamhyung Kim 
548aca7a94dSNamhyung Kim 			if (delay_secs != 0)
549aca7a94dSNamhyung Kim 				symbol__annotate_decay_histogram(sym, evidx);
550aca7a94dSNamhyung Kim 			continue;
551aca7a94dSNamhyung Kim 		case K_TAB:
552aca7a94dSNamhyung Kim 			if (nd != NULL) {
553aca7a94dSNamhyung Kim 				nd = rb_prev(nd);
554aca7a94dSNamhyung Kim 				if (nd == NULL)
555aca7a94dSNamhyung Kim 					nd = rb_last(&self->entries);
556aca7a94dSNamhyung Kim 			} else
557aca7a94dSNamhyung Kim 				nd = self->curr_hot;
558aca7a94dSNamhyung Kim 			break;
559aca7a94dSNamhyung Kim 		case K_UNTAB:
560aca7a94dSNamhyung Kim 			if (nd != NULL)
561aca7a94dSNamhyung Kim 				nd = rb_next(nd);
562aca7a94dSNamhyung Kim 				if (nd == NULL)
563aca7a94dSNamhyung Kim 					nd = rb_first(&self->entries);
564aca7a94dSNamhyung Kim 			else
565aca7a94dSNamhyung Kim 				nd = self->curr_hot;
566aca7a94dSNamhyung Kim 			break;
567aca7a94dSNamhyung Kim 		case 'H':
568aca7a94dSNamhyung Kim 		case 'h':
569aca7a94dSNamhyung Kim 			nd = self->curr_hot;
570aca7a94dSNamhyung Kim 			break;
571aca7a94dSNamhyung Kim 		case 'S':
572aca7a94dSNamhyung Kim 		case 's':
573aca7a94dSNamhyung Kim 			if (annotate_browser__toggle_source(self))
574aca7a94dSNamhyung Kim 				ui_helpline__puts(help);
575aca7a94dSNamhyung Kim 			continue;
576aca7a94dSNamhyung Kim 		case 'O':
577aca7a94dSNamhyung Kim 		case 'o':
578aca7a94dSNamhyung Kim 			self->use_offset = !self->use_offset;
579aca7a94dSNamhyung Kim 			continue;
580aca7a94dSNamhyung Kim 		case '/':
581aca7a94dSNamhyung Kim 			if (annotate_browser__search(self, delay_secs)) {
582aca7a94dSNamhyung Kim show_help:
583aca7a94dSNamhyung Kim 				ui_helpline__puts(help);
584aca7a94dSNamhyung Kim 			}
585aca7a94dSNamhyung Kim 			continue;
586aca7a94dSNamhyung Kim 		case 'n':
587aca7a94dSNamhyung Kim 			if (self->searching_backwards ?
588aca7a94dSNamhyung Kim 			    annotate_browser__continue_search_reverse(self, delay_secs) :
589aca7a94dSNamhyung Kim 			    annotate_browser__continue_search(self, delay_secs))
590aca7a94dSNamhyung Kim 				goto show_help;
591aca7a94dSNamhyung Kim 			continue;
592aca7a94dSNamhyung Kim 		case '?':
593aca7a94dSNamhyung Kim 			if (annotate_browser__search_reverse(self, delay_secs))
594aca7a94dSNamhyung Kim 				goto show_help;
595aca7a94dSNamhyung Kim 			continue;
596aca7a94dSNamhyung Kim 		case K_ENTER:
597aca7a94dSNamhyung Kim 		case K_RIGHT:
598aca7a94dSNamhyung Kim 			if (self->selection == NULL)
599aca7a94dSNamhyung Kim 				ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
600aca7a94dSNamhyung Kim 			else if (self->selection->offset == -1)
601aca7a94dSNamhyung Kim 				ui_helpline__puts("Actions are only available for assembly lines.");
602d86b0597SArnaldo Carvalho de Melo 			else if (!self->selection->ins ||
603d86b0597SArnaldo Carvalho de Melo 				 !(annotate_browser__jump(self) ||
604aca7a94dSNamhyung Kim 				   annotate_browser__callq(self, evidx, timer, arg, delay_secs)))
605aca7a94dSNamhyung Kim 				ui_helpline__puts("Actions are only available for the 'callq' and jump instructions.");
606aca7a94dSNamhyung Kim 			continue;
607aca7a94dSNamhyung Kim 		case K_LEFT:
608aca7a94dSNamhyung Kim 		case K_ESC:
609aca7a94dSNamhyung Kim 		case 'q':
610aca7a94dSNamhyung Kim 		case CTRL('c'):
611aca7a94dSNamhyung Kim 			goto out;
612aca7a94dSNamhyung Kim 		default:
613aca7a94dSNamhyung Kim 			continue;
614aca7a94dSNamhyung Kim 		}
615aca7a94dSNamhyung Kim 
616aca7a94dSNamhyung Kim 		if (nd != NULL)
617aca7a94dSNamhyung Kim 			annotate_browser__set_rb_top(self, nd);
618aca7a94dSNamhyung Kim 	}
619aca7a94dSNamhyung Kim out:
620aca7a94dSNamhyung Kim 	ui_browser__hide(&self->b);
621aca7a94dSNamhyung Kim 	return key;
622aca7a94dSNamhyung Kim }
623aca7a94dSNamhyung Kim 
624aca7a94dSNamhyung Kim int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
625aca7a94dSNamhyung Kim 			     void(*timer)(void *arg), void *arg, int delay_secs)
626aca7a94dSNamhyung Kim {
627aca7a94dSNamhyung Kim 	return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
628aca7a94dSNamhyung Kim 				    timer, arg, delay_secs);
629aca7a94dSNamhyung Kim }
630aca7a94dSNamhyung Kim 
631b793a401SArnaldo Carvalho de Melo static void annotate_browser__mark_jump_targets(struct annotate_browser *browser,
632b793a401SArnaldo Carvalho de Melo 						size_t size)
633b793a401SArnaldo Carvalho de Melo {
634b793a401SArnaldo Carvalho de Melo 	u64 offset;
635b793a401SArnaldo Carvalho de Melo 
636b793a401SArnaldo Carvalho de Melo 	for (offset = 0; offset < size; ++offset) {
637b793a401SArnaldo Carvalho de Melo 		struct disasm_line *dl = browser->offsets[offset], *dlt;
638b793a401SArnaldo Carvalho de Melo 		struct browser_disasm_line *bdlt;
639b793a401SArnaldo Carvalho de Melo 
640b793a401SArnaldo Carvalho de Melo 		if (!dl || !dl->ins || !ins__is_jump(dl->ins))
641b793a401SArnaldo Carvalho de Melo 			continue;
642b793a401SArnaldo Carvalho de Melo 
643c7e6ead7SArnaldo Carvalho de Melo 		if (dl->ops.target >= size) {
644b793a401SArnaldo Carvalho de Melo 			ui__error("jump to after symbol!\n"
645b793a401SArnaldo Carvalho de Melo 				  "size: %zx, jump target: %" PRIx64,
646c7e6ead7SArnaldo Carvalho de Melo 				  size, dl->ops.target);
647b793a401SArnaldo Carvalho de Melo 			continue;
648b793a401SArnaldo Carvalho de Melo 		}
649b793a401SArnaldo Carvalho de Melo 
650c7e6ead7SArnaldo Carvalho de Melo 		dlt = browser->offsets[dl->ops.target];
651b793a401SArnaldo Carvalho de Melo 		bdlt = disasm_line__browser(dlt);
652b793a401SArnaldo Carvalho de Melo 		bdlt->jump_target = true;
653b793a401SArnaldo Carvalho de Melo 	}
654b793a401SArnaldo Carvalho de Melo 
655b793a401SArnaldo Carvalho de Melo }
656b793a401SArnaldo Carvalho de Melo 
657aca7a94dSNamhyung Kim int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
658aca7a94dSNamhyung Kim 			 void(*timer)(void *arg), void *arg,
659aca7a94dSNamhyung Kim 			 int delay_secs)
660aca7a94dSNamhyung Kim {
66129ed6e76SArnaldo Carvalho de Melo 	struct disasm_line *pos, *n;
662aca7a94dSNamhyung Kim 	struct annotation *notes;
663b793a401SArnaldo Carvalho de Melo 	const size_t size = symbol__size(sym);
664aca7a94dSNamhyung Kim 	struct map_symbol ms = {
665aca7a94dSNamhyung Kim 		.map = map,
666aca7a94dSNamhyung Kim 		.sym = sym,
667aca7a94dSNamhyung Kim 	};
668aca7a94dSNamhyung Kim 	struct annotate_browser browser = {
669aca7a94dSNamhyung Kim 		.b = {
670aca7a94dSNamhyung Kim 			.refresh = ui_browser__list_head_refresh,
671aca7a94dSNamhyung Kim 			.seek	 = ui_browser__list_head_seek,
672aca7a94dSNamhyung Kim 			.write	 = annotate_browser__write,
67329ed6e76SArnaldo Carvalho de Melo 			.filter  = disasm_line__filter,
674aca7a94dSNamhyung Kim 			.priv	 = &ms,
675aca7a94dSNamhyung Kim 			.use_navkeypressed = true,
676aca7a94dSNamhyung Kim 		},
6778bf39cb8SArnaldo Carvalho de Melo 		.use_offset = true,
678aca7a94dSNamhyung Kim 	};
679b793a401SArnaldo Carvalho de Melo 	int ret = -1;
680aca7a94dSNamhyung Kim 
681aca7a94dSNamhyung Kim 	if (sym == NULL)
682aca7a94dSNamhyung Kim 		return -1;
683aca7a94dSNamhyung Kim 
684aca7a94dSNamhyung Kim 	if (map->dso->annotate_warned)
685aca7a94dSNamhyung Kim 		return -1;
686aca7a94dSNamhyung Kim 
687b793a401SArnaldo Carvalho de Melo 	browser.offsets = zalloc(size * sizeof(struct disasm_line *));
688b793a401SArnaldo Carvalho de Melo 	if (browser.offsets == NULL) {
689b793a401SArnaldo Carvalho de Melo 		ui__error("Not enough memory!");
690b793a401SArnaldo Carvalho de Melo 		return -1;
691b793a401SArnaldo Carvalho de Melo 	}
692b793a401SArnaldo Carvalho de Melo 
693887c0066SArnaldo Carvalho de Melo 	if (symbol__annotate(sym, map, sizeof(struct browser_disasm_line)) < 0) {
694aca7a94dSNamhyung Kim 		ui__error("%s", ui_helpline__last_msg);
695b793a401SArnaldo Carvalho de Melo 		goto out_free_offsets;
696aca7a94dSNamhyung Kim 	}
697aca7a94dSNamhyung Kim 
698aca7a94dSNamhyung Kim 	ui_helpline__push("Press <- or ESC to exit");
699aca7a94dSNamhyung Kim 
700aca7a94dSNamhyung Kim 	notes = symbol__annotation(sym);
701aca7a94dSNamhyung Kim 	browser.start = map__rip_2objdump(map, sym->start);
702aca7a94dSNamhyung Kim 
703aca7a94dSNamhyung Kim 	list_for_each_entry(pos, &notes->src->source, node) {
704887c0066SArnaldo Carvalho de Melo 		struct browser_disasm_line *bpos;
705aca7a94dSNamhyung Kim 		size_t line_len = strlen(pos->line);
706aca7a94dSNamhyung Kim 
707aca7a94dSNamhyung Kim 		if (browser.b.width < line_len)
708aca7a94dSNamhyung Kim 			browser.b.width = line_len;
709887c0066SArnaldo Carvalho de Melo 		bpos = disasm_line__browser(pos);
710887c0066SArnaldo Carvalho de Melo 		bpos->idx = browser.nr_entries++;
711b793a401SArnaldo Carvalho de Melo 		if (pos->offset != -1) {
712887c0066SArnaldo Carvalho de Melo 			bpos->idx_asm = browser.nr_asm_entries++;
71397148a97SArnaldo Carvalho de Melo 			/*
71497148a97SArnaldo Carvalho de Melo 			 * FIXME: short term bandaid to cope with assembly
71597148a97SArnaldo Carvalho de Melo 			 * routines that comes with labels in the same column
71697148a97SArnaldo Carvalho de Melo 			 * as the address in objdump, sigh.
71797148a97SArnaldo Carvalho de Melo 			 *
71897148a97SArnaldo Carvalho de Melo 			 * E.g. copy_user_generic_unrolled
71997148a97SArnaldo Carvalho de Melo  			 */
72097148a97SArnaldo Carvalho de Melo 			if (pos->offset < (s64)size)
721b793a401SArnaldo Carvalho de Melo 				browser.offsets[pos->offset] = pos;
722b793a401SArnaldo Carvalho de Melo 		} else
723887c0066SArnaldo Carvalho de Melo 			bpos->idx_asm = -1;
724aca7a94dSNamhyung Kim 	}
725aca7a94dSNamhyung Kim 
726b793a401SArnaldo Carvalho de Melo 	annotate_browser__mark_jump_targets(&browser, size);
727b793a401SArnaldo Carvalho de Melo 
72861e04b33SArnaldo Carvalho de Melo 	browser.offset_width = hex_width(size);
729aca7a94dSNamhyung Kim 	browser.b.nr_entries = browser.nr_entries;
730aca7a94dSNamhyung Kim 	browser.b.entries = &notes->src->source,
731aca7a94dSNamhyung Kim 	browser.b.width += 18; /* Percentage */
732aca7a94dSNamhyung Kim 	ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
733aca7a94dSNamhyung Kim 	list_for_each_entry_safe(pos, n, &notes->src->source, node) {
734aca7a94dSNamhyung Kim 		list_del(&pos->node);
73529ed6e76SArnaldo Carvalho de Melo 		disasm_line__free(pos);
736aca7a94dSNamhyung Kim 	}
737b793a401SArnaldo Carvalho de Melo 
738b793a401SArnaldo Carvalho de Melo out_free_offsets:
739b793a401SArnaldo Carvalho de Melo 	free(browser.offsets);
740aca7a94dSNamhyung Kim 	return ret;
741aca7a94dSNamhyung Kim }
742