annotate.c 22.2 KB
Newer Older
1
#include "../../util/util.h"
2 3 4
#include "../browser.h"
#include "../helpline.h"
#include "../libslang.h"
5 6
#include "../ui.h"
#include "../util.h"
7 8 9 10
#include "../../util/annotate.h"
#include "../../util/hist.h"
#include "../../util/sort.h"
#include "../../util/symbol.h"
11
#include <pthread.h>
12
#include <newt.h>
13

14 15 16 17 18
struct browser_disasm_line {
	struct rb_node	rb_node;
	double		percent;
	u32		idx;
	int		idx_asm;
19
	int		jump_sources;
20 21
};

22 23 24
struct annotate_browser {
	struct ui_browser b;
	struct rb_root	  entries;
25
	struct rb_node	  *curr_hot;
26
	struct disasm_line	  *selection;
27
	struct disasm_line  **offsets;
28
	u64		    start;
29 30
	int		    nr_asm_entries;
	int		    nr_entries;
31 32
	int		    max_jump_sources;
	int		    nr_jumps;
33
	bool		    hide_src_code;
34
	bool		    use_offset;
35
	bool		    jump_arrows;
36
	bool		    show_nr_jumps;
37
	bool		    searching_backwards;
38
	u8		    addr_width;
39 40
	u8		    jumps_width;
	u8		    target_width;
41 42
	u8		    min_addr_width;
	u8		    max_addr_width;
43
	char		    search_bf[128];
44 45
};

46
static inline struct browser_disasm_line *disasm_line__browser(struct disasm_line *dl)
47
{
48
	return (struct browser_disasm_line *)(dl + 1);
49 50
}

51
static bool disasm_line__filter(struct ui_browser *browser, void *entry)
52 53 54 55
{
	struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);

	if (ab->hide_src_code) {
56 57
		struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
		return dl->offset == -1;
58 59 60 61 62
	}

	return false;
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
static int annotate_browser__jumps_percent_color(struct annotate_browser *browser,
						 int nr, bool current)
{
	if (current && (!browser->b.use_navkeypressed || browser->b.navkeypressed))
		return HE_COLORSET_SELECTED;
	if (nr == browser->max_jump_sources)
		return HE_COLORSET_TOP;
	if (nr > 1)
		return HE_COLORSET_MEDIUM;
	return HE_COLORSET_NORMAL;
}

static int annotate_browser__set_jumps_percent_color(struct annotate_browser *browser,
						     int nr, bool current)
{
	 int color = annotate_browser__jumps_percent_color(browser, nr, current);
	 return ui_browser__set_color(&browser->b, color);
}

82 83
static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
{
84
	struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
85
	struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
86
	struct browser_disasm_line *bdl = disasm_line__browser(dl);
87
	bool current_entry = ui_browser__is_current_entry(self, row);
88 89 90
	bool change_color = (!ab->hide_src_code &&
			     (!current_entry || (self->use_navkeypressed &&
					         !self->navkeypressed)));
91 92
	int width = self->width, printed;
	char bf[256];
93

94
	if (dl->offset != -1 && bdl->percent != 0.0) {
95
		ui_browser__set_percent_color(self, bdl->percent, current_entry);
96
		slsmg_printf("%6.2f ", bdl->percent);
97
	} else {
98
		ui_browser__set_percent_color(self, 0, current_entry);
99
		slsmg_write_nstring(" ", 7);
100 101
	}

102
	SLsmg_write_char(' ');
103 104 105 106 107

	/* The scroll bar isn't being used */
	if (!self->navkeypressed)
		width += 1;

108
	if (!*dl->line)
109
		slsmg_write_nstring(" ", width - 7);
110 111 112 113 114 115
	else if (dl->offset == -1) {
		printed = scnprintf(bf, sizeof(bf), "%*s  ",
				    ab->addr_width, " ");
		slsmg_write_nstring(bf, printed);
		slsmg_write_nstring(dl->line, width - printed - 6);
	} else {
116
		u64 addr = dl->offset;
117
		int color = -1;
118

119 120 121
		if (!ab->use_offset)
			addr += ab->start;

122
		if (!ab->use_offset) {
123
			printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr);
124
		} else {
125
			if (bdl->jump_sources) {
126 127 128 129 130 131 132 133 134 135 136
				if (ab->show_nr_jumps) {
					int prev;
					printed = scnprintf(bf, sizeof(bf), "%*d ",
							    ab->jumps_width,
							    bdl->jump_sources);
					prev = annotate_browser__set_jumps_percent_color(ab, bdl->jump_sources,
											 current_entry);
					slsmg_write_nstring(bf, printed);
					ui_browser__set_color(self, prev);
				}

137
				printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ",
138
						    ab->target_width, addr);
139
			} else {
140 141
				printed = scnprintf(bf, sizeof(bf), "%*s  ",
						    ab->addr_width, " ");
142 143
			}
		}
144

145 146 147 148 149
		if (change_color)
			color = ui_browser__set_color(self, HE_COLORSET_ADDR);
		slsmg_write_nstring(bf, printed);
		if (change_color)
			ui_browser__set_color(self, color);
150
		if (dl->ins && dl->ins->ops->scnprintf) {
151
			if (ins__is_jump(dl->ins)) {
152
				bool fwd = dl->ops.target.offset > (u64)dl->offset;
153

154 155
				ui_browser__write_graph(self, fwd ? SLSMG_DARROW_CHAR :
								    SLSMG_UARROW_CHAR);
156
				SLsmg_write_char(' ');
157 158 159
			} else if (ins__is_call(dl->ins)) {
				ui_browser__write_graph(self, SLSMG_RARROW_CHAR);
				SLsmg_write_char(' ');
160 161 162
			} else {
				slsmg_write_nstring(" ", 2);
			}
163 164 165 166
		} else {
			if (strcmp(dl->name, "retq")) {
				slsmg_write_nstring(" ", 2);
			} else {
167
				ui_browser__write_graph(self, SLSMG_LARROW_CHAR);
168 169 170
				SLsmg_write_char(' ');
			}
		}
171

172
		disasm_line__scnprintf(dl, bf, sizeof(bf), !ab->use_offset);
173
		slsmg_write_nstring(bf, width - 10 - printed);
174
	}
175

176
	if (current_entry)
177
		ab->selection = dl;
178 179
}

180
static void annotate_browser__draw_current_jump(struct ui_browser *browser)
181 182
{
	struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
183 184
	struct disasm_line *cursor = ab->selection, *target;
	struct browser_disasm_line *btarget, *bcursor;
185
	unsigned int from, to;
186

187 188 189
	if (!cursor->ins || !ins__is_jump(cursor->ins) ||
	    !disasm_line__has_offset(cursor))
		return;
190

191 192 193
	target = ab->offsets[cursor->ops.target.offset];
	if (!target)
		return;
194

195 196
	bcursor = disasm_line__browser(cursor);
	btarget = disasm_line__browser(target);
197 198

	if (ab->hide_src_code) {
199
		from = bcursor->idx_asm;
200 201
		to = btarget->idx_asm;
	} else {
202
		from = (u64)bcursor->idx;
203 204 205 206
		to = (u64)btarget->idx;
	}

	ui_browser__set_color(browser, HE_COLORSET_CODE);
207
	__ui_browser__line_arrow(browser, 9 + ab->addr_width, from, to);
208 209 210 211
}

static unsigned int annotate_browser__refresh(struct ui_browser *browser)
{
212
	struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
213 214
	int ret = ui_browser__list_head_refresh(browser);

215 216
	if (ab->jump_arrows)
		annotate_browser__draw_current_jump(browser);
217

218 219
	ui_browser__set_color(browser, HE_COLORSET_NORMAL);
	__ui_browser__vline(browser, 7, 0, browser->height - 1);
220 221 222
	return ret;
}

223
static double disasm_line__calc_percent(struct disasm_line *dl, struct symbol *sym, int evidx)
224 225 226
{
	double percent = 0.0;

227
	if (dl->offset != -1) {
228
		int len = sym->end - sym->start;
229
		unsigned int hits = 0;
230
		struct annotation *notes = symbol__annotation(sym);
231
		struct source_line *src_line = notes->src->lines;
232
		struct sym_hist *h = annotation__histogram(notes, evidx);
233 234
		s64 offset = dl->offset;
		struct disasm_line *next;
235

236
		next = disasm__get_next_ip_line(&notes->src->source, dl);
237 238
		while (offset < (s64)len &&
		       (next == NULL || offset < next->offset)) {
239 240
			if (src_line) {
				percent += src_line[offset].percent;
241
			} else
242
				hits += h->addr[offset];
243 244 245

			++offset;
		}
246 247 248 249 250
		/*
 		 * If the percentage wasn't already calculated in
 		 * symbol__get_source_line, do it now:
 		 */
		if (src_line == NULL && h->sum)
251 252 253
			percent = 100.0 * hits / h->sum;
	}

254 255 256
	return percent;
}

257
static void disasm_rb_tree__insert(struct rb_root *root, struct browser_disasm_line *bdl)
258
{
259
	struct rb_node **p = &root->rb_node;
260
	struct rb_node *parent = NULL;
261
	struct browser_disasm_line *l;
262 263 264

	while (*p != NULL) {
		parent = *p;
265 266
		l = rb_entry(parent, struct browser_disasm_line, rb_node);
		if (bdl->percent < l->percent)
267 268 269 270
			p = &(*p)->rb_left;
		else
			p = &(*p)->rb_right;
	}
271 272
	rb_link_node(&bdl->rb_node, parent, p);
	rb_insert_color(&bdl->rb_node, root);
273 274
}

275
static void annotate_browser__set_top(struct annotate_browser *self,
276
				      struct disasm_line *pos, u32 idx)
277 278 279 280 281
{
	unsigned back;

	ui_browser__refresh_dimensions(&self->b);
	back = self->b.height / 2;
282
	self->b.top_idx = self->b.index = idx;
283 284

	while (self->b.top_idx != 0 && back != 0) {
285
		pos = list_entry(pos->node.prev, struct disasm_line, node);
286

287
		if (disasm_line__filter(&self->b, &pos->node))
288 289
			continue;

290 291 292 293 294
		--self->b.top_idx;
		--back;
	}

	self->b.top = pos;
295
	self->b.navkeypressed = true;
296 297 298 299 300
}

static void annotate_browser__set_rb_top(struct annotate_browser *browser,
					 struct rb_node *nd)
{
301
	struct browser_disasm_line *bpos;
302
	struct disasm_line *pos;
303

304 305 306
	bpos = rb_entry(nd, struct browser_disasm_line, rb_node);
	pos = ((struct disasm_line *)bpos) - 1;
	annotate_browser__set_top(browser, pos, bpos->idx);
307
	browser->curr_hot = nd;
308 309
}

310 311
static void annotate_browser__calc_percent(struct annotate_browser *browser,
					   int evidx)
312
{
313 314
	struct map_symbol *ms = browser->b.priv;
	struct symbol *sym = ms->sym;
315
	struct annotation *notes = symbol__annotation(sym);
316
	struct disasm_line *pos;
317 318 319 320 321 322

	browser->entries = RB_ROOT;

	pthread_mutex_lock(&notes->lock);

	list_for_each_entry(pos, &notes->src->source, node) {
323 324 325 326
		struct browser_disasm_line *bpos = disasm_line__browser(pos);
		bpos->percent = disasm_line__calc_percent(pos, sym, evidx);
		if (bpos->percent < 0.01) {
			RB_CLEAR_NODE(&bpos->rb_node);
327 328
			continue;
		}
329
		disasm_rb_tree__insert(&browser->entries, bpos);
330 331 332 333 334 335
	}
	pthread_mutex_unlock(&notes->lock);

	browser->curr_hot = rb_last(&browser->entries);
}

336 337
static bool annotate_browser__toggle_source(struct annotate_browser *browser)
{
338
	struct disasm_line *dl;
339
	struct browser_disasm_line *bdl;
340 341 342
	off_t offset = browser->b.index - browser->b.top_idx;

	browser->b.seek(&browser->b, offset, SEEK_CUR);
343
	dl = list_entry(browser->b.top, struct disasm_line, node);
344
	bdl = disasm_line__browser(dl);
345 346

	if (browser->hide_src_code) {
347 348
		if (bdl->idx_asm < offset)
			offset = bdl->idx;
349 350 351 352

		browser->b.nr_entries = browser->nr_entries;
		browser->hide_src_code = false;
		browser->b.seek(&browser->b, -offset, SEEK_CUR);
353 354
		browser->b.top_idx = bdl->idx - offset;
		browser->b.index = bdl->idx;
355
	} else {
356
		if (bdl->idx_asm < 0) {
357 358 359 360 361
			ui_helpline__puts("Only available for assembly lines.");
			browser->b.seek(&browser->b, -offset, SEEK_CUR);
			return false;
		}

362 363
		if (bdl->idx_asm < offset)
			offset = bdl->idx_asm;
364 365 366 367

		browser->b.nr_entries = browser->nr_asm_entries;
		browser->hide_src_code = true;
		browser->b.seek(&browser->b, -offset, SEEK_CUR);
368 369
		browser->b.top_idx = bdl->idx_asm - offset;
		browser->b.index = bdl->idx_asm;
370 371 372 373 374
	}

	return true;
}

375 376 377 378 379
static bool annotate_browser__callq(struct annotate_browser *browser,
				    int evidx, void (*timer)(void *arg),
				    void *arg, int delay_secs)
{
	struct map_symbol *ms = browser->b.priv;
380
	struct disasm_line *dl = browser->selection;
381 382 383 384 385
	struct symbol *sym = ms->sym;
	struct annotation *notes;
	struct symbol *target;
	u64 ip;

386
	if (!ins__is_call(dl->ins))
387 388
		return false;

389
	ip = ms->map->map_ip(ms->map, dl->ops.target.addr);
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
	target = map__find_symbol(ms->map, ip, NULL);
	if (target == NULL) {
		ui_helpline__puts("The called function was not found.");
		return true;
	}

	notes = symbol__annotation(target);
	pthread_mutex_lock(&notes->lock);

	if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
		pthread_mutex_unlock(&notes->lock);
		ui__warning("Not enough memory for annotating '%s' symbol!\n",
			    target->name);
		return true;
	}

	pthread_mutex_unlock(&notes->lock);
	symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
	ui_browser__show_title(&browser->b, sym->name);
	return true;
}

412 413 414
static
struct disasm_line *annotate_browser__find_offset(struct annotate_browser *browser,
					  s64 offset, s64 *idx)
415 416 417 418
{
	struct map_symbol *ms = browser->b.priv;
	struct symbol *sym = ms->sym;
	struct annotation *notes = symbol__annotation(sym);
419
	struct disasm_line *pos;
420 421 422 423 424

	*idx = 0;
	list_for_each_entry(pos, &notes->src->source, node) {
		if (pos->offset == offset)
			return pos;
425
		if (!disasm_line__filter(&browser->b, &pos->node))
426 427 428 429 430 431 432 433
			++*idx;
	}

	return NULL;
}

static bool annotate_browser__jump(struct annotate_browser *browser)
{
434
	struct disasm_line *dl = browser->selection;
435
	s64 idx;
436

437
	if (!ins__is_jump(dl->ins))
438 439
		return false;

440
	dl = annotate_browser__find_offset(browser, dl->ops.target.offset, &idx);
441
	if (dl == NULL) {
442 443 444 445
		ui_helpline__puts("Invallid jump offset");
		return true;
	}

446
	annotate_browser__set_top(browser, dl, idx);
447 448 449 450
	
	return true;
}

451 452 453
static
struct disasm_line *annotate_browser__find_string(struct annotate_browser *browser,
					  char *s, s64 *idx)
454 455 456 457
{
	struct map_symbol *ms = browser->b.priv;
	struct symbol *sym = ms->sym;
	struct annotation *notes = symbol__annotation(sym);
458
	struct disasm_line *pos = browser->selection;
459 460 461

	*idx = browser->b.index;
	list_for_each_entry_continue(pos, &notes->src->source, node) {
462
		if (disasm_line__filter(&browser->b, &pos->node))
463 464 465 466 467 468 469 470 471 472 473 474 475
			continue;

		++*idx;

		if (pos->line && strstr(pos->line, s) != NULL)
			return pos;
	}

	return NULL;
}

static bool __annotate_browser__search(struct annotate_browser *browser)
{
476
	struct disasm_line *dl;
477 478
	s64 idx;

479 480
	dl = annotate_browser__find_string(browser, browser->search_bf, &idx);
	if (dl == NULL) {
481 482 483 484
		ui_helpline__puts("String not found!");
		return false;
	}

485
	annotate_browser__set_top(browser, dl, idx);
486 487 488 489
	browser->searching_backwards = false;
	return true;
}

490 491 492
static
struct disasm_line *annotate_browser__find_string_reverse(struct annotate_browser *browser,
						  char *s, s64 *idx)
493 494 495 496
{
	struct map_symbol *ms = browser->b.priv;
	struct symbol *sym = ms->sym;
	struct annotation *notes = symbol__annotation(sym);
497
	struct disasm_line *pos = browser->selection;
498 499 500

	*idx = browser->b.index;
	list_for_each_entry_continue_reverse(pos, &notes->src->source, node) {
501
		if (disasm_line__filter(&browser->b, &pos->node))
502 503 504 505 506 507 508 509 510 511 512 513 514
			continue;

		--*idx;

		if (pos->line && strstr(pos->line, s) != NULL)
			return pos;
	}

	return NULL;
}

static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
{
515
	struct disasm_line *dl;
516 517
	s64 idx;

518 519
	dl = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
	if (dl == NULL) {
520 521 522 523
		ui_helpline__puts("String not found!");
		return false;
	}

524
	annotate_browser__set_top(browser, dl, idx);
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
	browser->searching_backwards = true;
	return true;
}

static bool annotate_browser__search_window(struct annotate_browser *browser,
					    int delay_secs)
{
	if (ui_browser__input_window("Search", "String: ", browser->search_bf,
				     "ENTER: OK, ESC: Cancel",
				     delay_secs * 2) != K_ENTER ||
	    !*browser->search_bf)
		return false;

	return true;
}

static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs)
{
	if (annotate_browser__search_window(browser, delay_secs))
		return __annotate_browser__search(browser);

	return false;
}

static bool annotate_browser__continue_search(struct annotate_browser *browser,
					      int delay_secs)
{
	if (!*browser->search_bf)
		return annotate_browser__search(browser, delay_secs);

	return __annotate_browser__search(browser);
}

static bool annotate_browser__search_reverse(struct annotate_browser *browser,
					   int delay_secs)
{
	if (annotate_browser__search_window(browser, delay_secs))
		return __annotate_browser__search_reverse(browser);

	return false;
}

static
bool annotate_browser__continue_search_reverse(struct annotate_browser *browser,
					       int delay_secs)
{
	if (!*browser->search_bf)
		return annotate_browser__search_reverse(browser, delay_secs);

	return __annotate_browser__search_reverse(browser);
}

577
static int annotate_browser__run(struct annotate_browser *self, int evidx,
578
				 void(*timer)(void *arg),
579
				 void *arg, int delay_secs)
580 581
{
	struct rb_node *nd = NULL;
582 583
	struct map_symbol *ms = self->b.priv;
	struct symbol *sym = ms->sym;
584
	const char *help = "Press 'h' for help on key bindings";
585
	int key;
586

587
	if (ui_browser__show(&self->b, sym->name, help) < 0)
588
		return -1;
589 590 591

	annotate_browser__calc_percent(self, evidx);

592
	if (self->curr_hot) {
593
		annotate_browser__set_rb_top(self, self->curr_hot);
594 595
		self->b.navkeypressed = false;
	}
596 597

	nd = self->curr_hot;
598

599
	while (1) {
600
		key = ui_browser__run(&self->b, delay_secs);
601

602
		if (delay_secs != 0) {
603 604 605 606 607 608 609 610 611 612
			annotate_browser__calc_percent(self, evidx);
			/*
			 * Current line focus got out of the list of most active
			 * lines, NULL it so that if TAB|UNTAB is pressed, we
			 * move to curr_hot (current hottest line).
			 */
			if (nd != NULL && RB_EMPTY_NODE(nd))
				nd = NULL;
		}

613
		switch (key) {
614
		case K_TIMER:
615 616 617 618
			if (timer != NULL)
				timer(arg);

			if (delay_secs != 0)
619 620
				symbol__annotate_decay_histogram(sym, evidx);
			continue;
621
		case K_TAB:
622 623 624 625 626 627
			if (nd != NULL) {
				nd = rb_prev(nd);
				if (nd == NULL)
					nd = rb_last(&self->entries);
			} else
				nd = self->curr_hot;
628
			break;
629
		case K_UNTAB:
630 631 632 633 634 635 636
			if (nd != NULL)
				nd = rb_next(nd);
				if (nd == NULL)
					nd = rb_first(&self->entries);
			else
				nd = self->curr_hot;
			break;
637
		case K_F1:
638
		case 'h':
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
			ui_browser__help_window(&self->b,
		"UP/DOWN/PGUP\n"
		"PGDN/SPACE    Navigate\n"
		"q/ESC/CTRL+C  Exit\n\n"
		"->            Go to target\n"
		"<-            Exit\n"
		"h             Cycle thru hottest instructions\n"
		"j             Toggle showing jump to target arrows\n"
		"J             Toggle showing number of jump sources on targets\n"
		"n             Search next string\n"
		"o             Toggle disassembler output/simplified view\n"
		"s             Toggle source code view\n"
		"/             Search string\n"
		"?             Search previous string\n");
			continue;
		case 'H':
655
			nd = self->curr_hot;
656
			break;
657
		case 's':
658 659 660
			if (annotate_browser__toggle_source(self))
				ui_helpline__puts(help);
			continue;
661 662
		case 'o':
			self->use_offset = !self->use_offset;
663
			if (self->use_offset)
664
				self->target_width = self->min_addr_width;
665
			else
666 667 668 669 670
				self->target_width = self->max_addr_width;
update_addr_width:
			self->addr_width = self->target_width;
			if (self->show_nr_jumps)
				self->addr_width += self->jumps_width + 1;
671
			continue;
672 673 674
		case 'j':
			self->jump_arrows = !self->jump_arrows;
			continue;
675 676 677
		case 'J':
			self->show_nr_jumps = !self->show_nr_jumps;
			goto update_addr_width;
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
		case '/':
			if (annotate_browser__search(self, delay_secs)) {
show_help:
				ui_helpline__puts(help);
			}
			continue;
		case 'n':
			if (self->searching_backwards ?
			    annotate_browser__continue_search_reverse(self, delay_secs) :
			    annotate_browser__continue_search(self, delay_secs))
				goto show_help;
			continue;
		case '?':
			if (annotate_browser__search_reverse(self, delay_secs))
				goto show_help;
			continue;
694 695
		case K_ENTER:
		case K_RIGHT:
696
			if (self->selection == NULL)
697
				ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
698
			else if (self->selection->offset == -1)
699
				ui_helpline__puts("Actions are only available for assembly lines.");
700 701 702 703 704 705 706 707 708
			else if (!self->selection->ins) {
				if (strcmp(self->selection->name, "retq"))
					goto show_sup_ins;
				goto out;
			} else if (!(annotate_browser__jump(self) ||
				     annotate_browser__callq(self, evidx, timer, arg, delay_secs))) {
show_sup_ins:
				ui_helpline__puts("Actions are only available for 'callq', 'retq' & jump instructions.");
			}
709
			continue;
710 711
		case K_LEFT:
		case K_ESC:
712 713
		case 'q':
		case CTRL('c'):
714
			goto out;
715 716
		default:
			continue;
717
		}
718 719

		if (nd != NULL)
720
			annotate_browser__set_rb_top(self, nd);
721 722
	}
out:
723
	ui_browser__hide(&self->b);
724
	return key;
725 726
}

727
int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
728
			     void(*timer)(void *arg), void *arg, int delay_secs)
729
{
730
	return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
731
				    timer, arg, delay_secs);
732 733
}

734 735 736 737 738 739 740 741 742
static void annotate_browser__mark_jump_targets(struct annotate_browser *browser,
						size_t size)
{
	u64 offset;

	for (offset = 0; offset < size; ++offset) {
		struct disasm_line *dl = browser->offsets[offset], *dlt;
		struct browser_disasm_line *bdlt;

743 744
		if (!dl || !dl->ins || !ins__is_jump(dl->ins) ||
		    !disasm_line__has_offset(dl))
745 746
			continue;

747
		if (dl->ops.target.offset >= size) {
748 749
			ui__error("jump to after symbol!\n"
				  "size: %zx, jump target: %" PRIx64,
750
				  size, dl->ops.target.offset);
751 752 753
			continue;
		}

754
		dlt = browser->offsets[dl->ops.target.offset];
755 756 757 758 759 760 761
		/*
 		 * FIXME: Oops, no jump target? Buggy disassembler? Or do we
 		 * have to adjust to the previous offset?
 		 */
		if (dlt == NULL)
			continue;

762
		bdlt = disasm_line__browser(dlt);
763 764 765 766
		if (++bdlt->jump_sources > browser->max_jump_sources)
			browser->max_jump_sources = bdlt->jump_sources;

		++browser->nr_jumps;
767 768 769 770
	}
		
}

771 772 773 774 775 776 777 778 779
static inline int width_jumps(int n)
{
	if (n >= 100)
		return 5;
	if (n / 10)
		return 2;
	return 1;
}

780
int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
781
			 void(*timer)(void *arg), void *arg,
782
			 int delay_secs)
783
{
784
	struct disasm_line *pos, *n;
785
	struct annotation *notes;
786
	const size_t size = symbol__size(sym);
787 788 789 790
	struct map_symbol ms = {
		.map = map,
		.sym = sym,
	};
791 792
	struct annotate_browser browser = {
		.b = {
793
			.refresh = annotate_browser__refresh,
794 795
			.seek	 = ui_browser__list_head_seek,
			.write	 = annotate_browser__write,
796
			.filter  = disasm_line__filter,
797
			.priv	 = &ms,
798
			.use_navkeypressed = true,
799
		},
800
		.use_offset = true,
801
		.jump_arrows = true,
802
	};
803
	int ret = -1;
804

805
	if (sym == NULL)
806 807
		return -1;

808
	if (map->dso->annotate_warned)
809 810
		return -1;

811 812 813 814 815 816
	browser.offsets = zalloc(size * sizeof(struct disasm_line *));
	if (browser.offsets == NULL) {
		ui__error("Not enough memory!");
		return -1;
	}

817
	if (symbol__annotate(sym, map, sizeof(struct browser_disasm_line)) < 0) {
818
		ui__error("%s", ui_helpline__last_msg);
819
		goto out_free_offsets;
820 821 822 823
	}

	ui_helpline__push("Press <- or ESC to exit");

824
	notes = symbol__annotation(sym);
825
	browser.start = map__rip_2objdump(map, sym->start);
826

827
	list_for_each_entry(pos, &notes->src->source, node) {
828
		struct browser_disasm_line *bpos;
829
		size_t line_len = strlen(pos->line);
830

831 832
		if (browser.b.width < line_len)
			browser.b.width = line_len;
833 834
		bpos = disasm_line__browser(pos);
		bpos->idx = browser.nr_entries++;
835
		if (pos->offset != -1) {
836
			bpos->idx_asm = browser.nr_asm_entries++;
837 838 839 840 841 842 843 844 845
			/*
			 * FIXME: short term bandaid to cope with assembly
			 * routines that comes with labels in the same column
			 * as the address in objdump, sigh.
			 *
			 * E.g. copy_user_generic_unrolled
 			 */
			if (pos->offset < (s64)size)
				browser.offsets[pos->offset] = pos;
846
		} else
847
			bpos->idx_asm = -1;
848 849
	}

850 851
	annotate_browser__mark_jump_targets(&browser, size);

852
	browser.addr_width = browser.target_width = browser.min_addr_width = hex_width(size);
853
	browser.max_addr_width = hex_width(sym->end);
854
	browser.jumps_width = width_jumps(browser.max_jump_sources);
855
	browser.b.nr_entries = browser.nr_entries;
856
	browser.b.entries = &notes->src->source,
857
	browser.b.width += 18; /* Percentage */
858
	ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
859
	list_for_each_entry_safe(pos, n, &notes->src->source, node) {
860
		list_del(&pos->node);
861
		disasm_line__free(pos);
862
	}
863 864 865

out_free_offsets:
	free(browser.offsets);
866 867
	return ret;
}