annotate.c 27.4 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 "../../util/evsel.h"
12
#include <pthread.h>
13

14 15 16 17 18
struct disasm_line_samples {
	double		percent;
	u64		nr;
};

19
struct browser_disasm_line {
20 21 22 23
	struct rb_node			rb_node;
	u32				idx;
	int				idx_asm;
	int				jump_sources;
24 25 26 27
	/*
	 * actual length of this array is saved on the nr_events field
	 * of the struct annotate_browser
	 */
28
	struct disasm_line_samples	samples[1];
29 30
};

31 32 33 34
static struct annotate_browser_opt {
	bool hide_src_code,
	     use_offset,
	     jump_arrows,
35
	     show_linenr,
36 37
	     show_nr_jumps,
	     show_total_period;
38 39 40 41 42
} annotate_browser__opts = {
	.use_offset	= true,
	.jump_arrows	= true,
};

43 44 45
struct annotate_browser {
	struct ui_browser b;
	struct rb_root	  entries;
46
	struct rb_node	  *curr_hot;
47
	struct disasm_line  *selection;
48
	struct disasm_line  **offsets;
49
	int		    nr_events;
50
	u64		    start;
51 52
	int		    nr_asm_entries;
	int		    nr_entries;
53 54
	int		    max_jump_sources;
	int		    nr_jumps;
55
	bool		    searching_backwards;
56
	u8		    addr_width;
57 58
	u8		    jumps_width;
	u8		    target_width;
59 60
	u8		    min_addr_width;
	u8		    max_addr_width;
61
	char		    search_bf[128];
62 63
};

64
static inline struct browser_disasm_line *disasm_line__browser(struct disasm_line *dl)
65
{
66
	return (struct browser_disasm_line *)(dl + 1);
67 68
}

69 70
static bool disasm_line__filter(struct ui_browser *browser __maybe_unused,
				void *entry)
71
{
72
	if (annotate_browser__opts.hide_src_code) {
73 74
		struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
		return dl->offset == -1;
75 76 77 78 79
	}

	return false;
}

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
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);
}

99
static void annotate_browser__write(struct ui_browser *browser, void *entry, int row)
100
{
101
	struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
102
	struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
103
	struct browser_disasm_line *bdl = disasm_line__browser(dl);
104
	bool current_entry = ui_browser__is_current_entry(browser, row);
105
	bool change_color = (!annotate_browser__opts.hide_src_code &&
106 107 108
			     (!current_entry || (browser->use_navkeypressed &&
					         !browser->navkeypressed)));
	int width = browser->width, printed;
109 110
	int i, pcnt_width = 7 * ab->nr_events;
	double percent_max = 0.0;
111
	char bf[256];
112

113
	for (i = 0; i < ab->nr_events; i++) {
114 115
		if (bdl->samples[i].percent > percent_max)
			percent_max = bdl->samples[i].percent;
116 117 118 119
	}

	if (dl->offset != -1 && percent_max != 0.0) {
		for (i = 0; i < ab->nr_events; i++) {
120 121
			ui_browser__set_percent_color(browser,
						      bdl->samples[i].percent,
122
						      current_entry);
123 124 125 126 127
			if (annotate_browser__opts.show_total_period)
				slsmg_printf("%6" PRIu64 " ",
					     bdl->samples[i].nr);
			else
				slsmg_printf("%6.2f ", bdl->samples[i].percent);
128
		}
129
	} else {
130
		ui_browser__set_percent_color(browser, 0, current_entry);
131
		slsmg_write_nstring(" ", pcnt_width);
132 133
	}

134
	SLsmg_write_char(' ');
135 136

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

140
	if (!*dl->line)
141
		slsmg_write_nstring(" ", width - pcnt_width);
142
	else if (dl->offset == -1) {
143 144 145 146 147
		if (dl->line_nr && annotate_browser__opts.show_linenr)
			printed = scnprintf(bf, sizeof(bf), "%-*d ",
					ab->addr_width + 1, dl->line_nr);
		else
			printed = scnprintf(bf, sizeof(bf), "%*s  ",
148 149
				    ab->addr_width, " ");
		slsmg_write_nstring(bf, printed);
150
		slsmg_write_nstring(dl->line, width - printed - pcnt_width + 1);
151
	} else {
152
		u64 addr = dl->offset;
153
		int color = -1;
154

155
		if (!annotate_browser__opts.use_offset)
156 157
			addr += ab->start;

158
		if (!annotate_browser__opts.use_offset) {
159
			printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr);
160
		} else {
161
			if (bdl->jump_sources) {
162
				if (annotate_browser__opts.show_nr_jumps) {
163 164 165 166 167 168 169
					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);
170
					ui_browser__set_color(browser, prev);
171 172
				}

173
				printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ",
174
						    ab->target_width, addr);
175
			} else {
176 177
				printed = scnprintf(bf, sizeof(bf), "%*s  ",
						    ab->addr_width, " ");
178 179
			}
		}
180

181
		if (change_color)
182
			color = ui_browser__set_color(browser, HE_COLORSET_ADDR);
183 184
		slsmg_write_nstring(bf, printed);
		if (change_color)
185
			ui_browser__set_color(browser, color);
186
		if (dl->ins && dl->ins->ops->scnprintf) {
187
			if (ins__is_jump(dl->ins)) {
188
				bool fwd = dl->ops.target.offset > (u64)dl->offset;
189

190
				ui_browser__write_graph(browser, fwd ? SLSMG_DARROW_CHAR :
191
								    SLSMG_UARROW_CHAR);
192
				SLsmg_write_char(' ');
193
			} else if (ins__is_call(dl->ins)) {
194
				ui_browser__write_graph(browser, SLSMG_RARROW_CHAR);
195
				SLsmg_write_char(' ');
196 197 198
			} else {
				slsmg_write_nstring(" ", 2);
			}
199 200 201 202
		} else {
			if (strcmp(dl->name, "retq")) {
				slsmg_write_nstring(" ", 2);
			} else {
203
				ui_browser__write_graph(browser, SLSMG_LARROW_CHAR);
204 205 206
				SLsmg_write_char(' ');
			}
		}
207

208
		disasm_line__scnprintf(dl, bf, sizeof(bf), !annotate_browser__opts.use_offset);
209
		slsmg_write_nstring(bf, width - pcnt_width - 3 - printed);
210
	}
211

212
	if (current_entry)
213
		ab->selection = dl;
214 215
}

216 217 218 219 220 221 222 223 224 225
static bool disasm_line__is_valid_jump(struct disasm_line *dl, struct symbol *sym)
{
	if (!dl || !dl->ins || !ins__is_jump(dl->ins)
	    || !disasm_line__has_offset(dl)
	    || dl->ops.target.offset >= symbol__size(sym))
		return false;

	return true;
}

226
static void annotate_browser__draw_current_jump(struct ui_browser *browser)
227 228
{
	struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
229 230
	struct disasm_line *cursor = ab->selection, *target;
	struct browser_disasm_line *btarget, *bcursor;
231
	unsigned int from, to;
232 233
	struct map_symbol *ms = ab->b.priv;
	struct symbol *sym = ms->sym;
234
	u8 pcnt_width = 7;
235 236 237 238

	/* PLT symbols contain external offsets */
	if (strstr(sym->name, "@plt"))
		return;
239

240
	if (!disasm_line__is_valid_jump(cursor, sym))
241
		return;
242

243 244 245
	target = ab->offsets[cursor->ops.target.offset];
	if (!target)
		return;
246

247 248
	bcursor = disasm_line__browser(cursor);
	btarget = disasm_line__browser(target);
249

250
	if (annotate_browser__opts.hide_src_code) {
251
		from = bcursor->idx_asm;
252 253
		to = btarget->idx_asm;
	} else {
254
		from = (u64)bcursor->idx;
255 256 257
		to = (u64)btarget->idx;
	}

258 259
	pcnt_width *= ab->nr_events;

260
	ui_browser__set_color(browser, HE_COLORSET_CODE);
261 262
	__ui_browser__line_arrow(browser, pcnt_width + 2 + ab->addr_width,
				 from, to);
263 264 265 266
}

static unsigned int annotate_browser__refresh(struct ui_browser *browser)
{
267
	struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
268
	int ret = ui_browser__list_head_refresh(browser);
269 270 271
	int pcnt_width;

	pcnt_width = 7 * ab->nr_events;
272

273
	if (annotate_browser__opts.jump_arrows)
274
		annotate_browser__draw_current_jump(browser);
275

276
	ui_browser__set_color(browser, HE_COLORSET_NORMAL);
277
	__ui_browser__vline(browser, pcnt_width, 0, browser->height - 1);
278 279 280
	return ret;
}

281 282 283 284 285 286
static int disasm__cmp(struct browser_disasm_line *a,
		       struct browser_disasm_line *b, int nr_pcnt)
{
	int i;

	for (i = 0; i < nr_pcnt; i++) {
287
		if (a->samples[i].percent == b->samples[i].percent)
288
			continue;
289
		return a->samples[i].percent < b->samples[i].percent;
290 291 292 293 294 295
	}
	return 0;
}

static void disasm_rb_tree__insert(struct rb_root *root, struct browser_disasm_line *bdl,
				   int nr_events)
296
{
297
	struct rb_node **p = &root->rb_node;
298
	struct rb_node *parent = NULL;
299
	struct browser_disasm_line *l;
300 301 302

	while (*p != NULL) {
		parent = *p;
303
		l = rb_entry(parent, struct browser_disasm_line, rb_node);
304 305

		if (disasm__cmp(bdl, l, nr_events))
306 307 308 309
			p = &(*p)->rb_left;
		else
			p = &(*p)->rb_right;
	}
310 311
	rb_link_node(&bdl->rb_node, parent, p);
	rb_insert_color(&bdl->rb_node, root);
312 313
}

314
static void annotate_browser__set_top(struct annotate_browser *browser,
315
				      struct disasm_line *pos, u32 idx)
316 317 318
{
	unsigned back;

319 320 321
	ui_browser__refresh_dimensions(&browser->b);
	back = browser->b.height / 2;
	browser->b.top_idx = browser->b.index = idx;
322

323
	while (browser->b.top_idx != 0 && back != 0) {
324
		pos = list_entry(pos->node.prev, struct disasm_line, node);
325

326
		if (disasm_line__filter(&browser->b, &pos->node))
327 328
			continue;

329
		--browser->b.top_idx;
330 331 332
		--back;
	}

333 334
	browser->b.top = pos;
	browser->b.navkeypressed = true;
335 336 337 338 339
}

static void annotate_browser__set_rb_top(struct annotate_browser *browser,
					 struct rb_node *nd)
{
340
	struct browser_disasm_line *bpos;
341
	struct disasm_line *pos;
342
	u32 idx;
343

344 345
	bpos = rb_entry(nd, struct browser_disasm_line, rb_node);
	pos = ((struct disasm_line *)bpos) - 1;
346
	idx = bpos->idx;
347
	if (annotate_browser__opts.hide_src_code)
348 349
		idx = bpos->idx_asm;
	annotate_browser__set_top(browser, pos, idx);
350
	browser->curr_hot = nd;
351 352
}

353
static void annotate_browser__calc_percent(struct annotate_browser *browser,
354
					   struct perf_evsel *evsel)
355
{
356 357
	struct map_symbol *ms = browser->b.priv;
	struct symbol *sym = ms->sym;
358
	struct annotation *notes = symbol__annotation(sym);
359 360
	struct disasm_line *pos, *next;
	s64 len = symbol__size(sym);
361 362 363 364 365 366

	browser->entries = RB_ROOT;

	pthread_mutex_lock(&notes->lock);

	list_for_each_entry(pos, &notes->src->source, node) {
367
		struct browser_disasm_line *bpos = disasm_line__browser(pos);
368
		const char *path = NULL;
369 370
		double max_percent = 0.0;
		int i;
371 372 373 374 375 376 377 378

		if (pos->offset == -1) {
			RB_CLEAR_NODE(&bpos->rb_node);
			continue;
		}

		next = disasm__get_next_ip_line(&notes->src->source, pos);

379
		for (i = 0; i < browser->nr_events; i++) {
380 381 382
			u64 nr_samples;

			bpos->samples[i].percent = disasm__calc_percent(notes,
383 384 385
						evsel->idx + i,
						pos->offset,
						next ? next->offset : len,
386 387
						&path, &nr_samples);
			bpos->samples[i].nr = nr_samples;
388

389 390
			if (max_percent < bpos->samples[i].percent)
				max_percent = bpos->samples[i].percent;
391 392 393
		}

		if (max_percent < 0.01) {
394
			RB_CLEAR_NODE(&bpos->rb_node);
395 396
			continue;
		}
397 398
		disasm_rb_tree__insert(&browser->entries, bpos,
				       browser->nr_events);
399 400 401 402 403 404
	}
	pthread_mutex_unlock(&notes->lock);

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

405 406
static bool annotate_browser__toggle_source(struct annotate_browser *browser)
{
407
	struct disasm_line *dl;
408
	struct browser_disasm_line *bdl;
409 410 411
	off_t offset = browser->b.index - browser->b.top_idx;

	browser->b.seek(&browser->b, offset, SEEK_CUR);
412
	dl = list_entry(browser->b.top, struct disasm_line, node);
413
	bdl = disasm_line__browser(dl);
414

415
	if (annotate_browser__opts.hide_src_code) {
416 417
		if (bdl->idx_asm < offset)
			offset = bdl->idx;
418 419

		browser->b.nr_entries = browser->nr_entries;
420
		annotate_browser__opts.hide_src_code = false;
421
		browser->b.seek(&browser->b, -offset, SEEK_CUR);
422 423
		browser->b.top_idx = bdl->idx - offset;
		browser->b.index = bdl->idx;
424
	} else {
425
		if (bdl->idx_asm < 0) {
426 427 428 429 430
			ui_helpline__puts("Only available for assembly lines.");
			browser->b.seek(&browser->b, -offset, SEEK_CUR);
			return false;
		}

431 432
		if (bdl->idx_asm < offset)
			offset = bdl->idx_asm;
433 434

		browser->b.nr_entries = browser->nr_asm_entries;
435
		annotate_browser__opts.hide_src_code = true;
436
		browser->b.seek(&browser->b, -offset, SEEK_CUR);
437 438
		browser->b.top_idx = bdl->idx_asm - offset;
		browser->b.index = bdl->idx_asm;
439 440 441 442 443
	}

	return true;
}

444 445 446 447 448 449
static void annotate_browser__init_asm_mode(struct annotate_browser *browser)
{
	ui_browser__reset_index(&browser->b);
	browser->b.nr_entries = browser->nr_asm_entries;
}

450 451 452 453 454 455 456 457
#define SYM_TITLE_MAX_SIZE (PATH_MAX + 64)

static int sym_title(struct symbol *sym, struct map *map, char *title,
		     size_t sz)
{
	return snprintf(title, sz, "%s  %s", sym->name, map->dso->long_name);
}

458 459
static bool annotate_browser__callq(struct annotate_browser *browser,
				    struct perf_evsel *evsel,
460
				    struct hist_browser_timer *hbt)
461 462
{
	struct map_symbol *ms = browser->b.priv;
463
	struct disasm_line *dl = browser->selection;
464
	struct annotation *notes;
465 466
	struct addr_map_symbol target = {
		.map = ms->map,
467
		.addr = map__objdump_2mem(ms->map, dl->ops.target.addr),
468
	};
469
	char title[SYM_TITLE_MAX_SIZE];
470

471
	if (!ins__is_call(dl->ins))
472 473
		return false;

474 475 476 477
	if (map_groups__find_ams(&target, NULL) ||
	    map__rip_2objdump(target.map, target.map->map_ip(target.map,
							     target.addr)) !=
	    dl->ops.target.addr) {
478 479 480 481
		ui_helpline__puts("The called function was not found.");
		return true;
	}

482
	notes = symbol__annotation(target.sym);
483 484
	pthread_mutex_lock(&notes->lock);

485
	if (notes->src == NULL && symbol__alloc_hist(target.sym) < 0) {
486 487
		pthread_mutex_unlock(&notes->lock);
		ui__warning("Not enough memory for annotating '%s' symbol!\n",
488
			    target.sym->name);
489 490 491 492
		return true;
	}

	pthread_mutex_unlock(&notes->lock);
493 494
	symbol__tui_annotate(target.sym, target.map, evsel, hbt);
	sym_title(ms->sym, ms->map, title, sizeof(title));
495
	ui_browser__show_title(&browser->b, title);
496 497 498
	return true;
}

499 500 501
static
struct disasm_line *annotate_browser__find_offset(struct annotate_browser *browser,
					  s64 offset, s64 *idx)
502 503 504 505
{
	struct map_symbol *ms = browser->b.priv;
	struct symbol *sym = ms->sym;
	struct annotation *notes = symbol__annotation(sym);
506
	struct disasm_line *pos;
507 508 509 510 511

	*idx = 0;
	list_for_each_entry(pos, &notes->src->source, node) {
		if (pos->offset == offset)
			return pos;
512
		if (!disasm_line__filter(&browser->b, &pos->node))
513 514 515 516 517 518 519 520
			++*idx;
	}

	return NULL;
}

static bool annotate_browser__jump(struct annotate_browser *browser)
{
521
	struct disasm_line *dl = browser->selection;
522
	s64 idx;
523

524
	if (!ins__is_jump(dl->ins))
525 526
		return false;

527
	dl = annotate_browser__find_offset(browser, dl->ops.target.offset, &idx);
528
	if (dl == NULL) {
I
Ingo Molnar 已提交
529
		ui_helpline__puts("Invalid jump offset");
530 531 532
		return true;
	}

533
	annotate_browser__set_top(browser, dl, idx);
534

535 536 537
	return true;
}

538 539 540
static
struct disasm_line *annotate_browser__find_string(struct annotate_browser *browser,
					  char *s, s64 *idx)
541 542 543 544
{
	struct map_symbol *ms = browser->b.priv;
	struct symbol *sym = ms->sym;
	struct annotation *notes = symbol__annotation(sym);
545
	struct disasm_line *pos = browser->selection;
546 547 548

	*idx = browser->b.index;
	list_for_each_entry_continue(pos, &notes->src->source, node) {
549
		if (disasm_line__filter(&browser->b, &pos->node))
550 551 552 553 554 555 556 557 558 559 560 561 562
			continue;

		++*idx;

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

	return NULL;
}

static bool __annotate_browser__search(struct annotate_browser *browser)
{
563
	struct disasm_line *dl;
564 565
	s64 idx;

566 567
	dl = annotate_browser__find_string(browser, browser->search_bf, &idx);
	if (dl == NULL) {
568 569 570 571
		ui_helpline__puts("String not found!");
		return false;
	}

572
	annotate_browser__set_top(browser, dl, idx);
573 574 575 576
	browser->searching_backwards = false;
	return true;
}

577 578 579
static
struct disasm_line *annotate_browser__find_string_reverse(struct annotate_browser *browser,
						  char *s, s64 *idx)
580 581 582 583
{
	struct map_symbol *ms = browser->b.priv;
	struct symbol *sym = ms->sym;
	struct annotation *notes = symbol__annotation(sym);
584
	struct disasm_line *pos = browser->selection;
585 586 587

	*idx = browser->b.index;
	list_for_each_entry_continue_reverse(pos, &notes->src->source, node) {
588
		if (disasm_line__filter(&browser->b, &pos->node))
589 590 591 592 593 594 595 596 597 598 599 600 601
			continue;

		--*idx;

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

	return NULL;
}

static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
{
602
	struct disasm_line *dl;
603 604
	s64 idx;

605 606
	dl = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
	if (dl == NULL) {
607 608 609 610
		ui_helpline__puts("String not found!");
		return false;
	}

611
	annotate_browser__set_top(browser, dl, idx);
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
	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);
}

664 665 666 667 668 669 670 671 672 673 674 675 676
static void annotate_browser__update_addr_width(struct annotate_browser *browser)
{
	if (annotate_browser__opts.use_offset)
		browser->target_width = browser->min_addr_width;
	else
		browser->target_width = browser->max_addr_width;

	browser->addr_width = browser->target_width;

	if (annotate_browser__opts.show_nr_jumps)
		browser->addr_width += browser->jumps_width + 1;
}

677 678
static int annotate_browser__run(struct annotate_browser *browser,
				 struct perf_evsel *evsel,
679
				 struct hist_browser_timer *hbt)
680 681
{
	struct rb_node *nd = NULL;
682
	struct map_symbol *ms = browser->b.priv;
683
	struct symbol *sym = ms->sym;
684
	const char *help = "Press 'h' for help on key bindings";
685
	int delay_secs = hbt ? hbt->refresh : 0;
686
	int key;
687
	char title[SYM_TITLE_MAX_SIZE];
688

689 690
	sym_title(sym, ms->map, title, sizeof(title));
	if (ui_browser__show(&browser->b, title, help) < 0)
691
		return -1;
692

693
	annotate_browser__calc_percent(browser, evsel);
694

695 696 697
	if (browser->curr_hot) {
		annotate_browser__set_rb_top(browser, browser->curr_hot);
		browser->b.navkeypressed = false;
698
	}
699

700
	nd = browser->curr_hot;
701

702
	while (1) {
703
		key = ui_browser__run(&browser->b, delay_secs);
704

705
		if (delay_secs != 0) {
706
			annotate_browser__calc_percent(browser, evsel);
707 708 709 710 711 712 713 714 715
			/*
			 * 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;
		}

716
		switch (key) {
717
		case K_TIMER:
718 719
			if (hbt)
				hbt->timer(hbt->arg);
720 721

			if (delay_secs != 0)
722
				symbol__annotate_decay_histogram(sym, evsel->idx);
723
			continue;
724
		case K_TAB:
725 726 727
			if (nd != NULL) {
				nd = rb_prev(nd);
				if (nd == NULL)
728
					nd = rb_last(&browser->entries);
729
			} else
730
				nd = browser->curr_hot;
731
			break;
732
		case K_UNTAB:
733 734 735
			if (nd != NULL)
				nd = rb_next(nd);
				if (nd == NULL)
736
					nd = rb_first(&browser->entries);
737
			else
738
				nd = browser->curr_hot;
739
			break;
740
		case K_F1:
741
		case 'h':
742
			ui_browser__help_window(&browser->b,
743 744 745 746 747
		"UP/DOWN/PGUP\n"
		"PGDN/SPACE    Navigate\n"
		"q/ESC/CTRL+C  Exit\n\n"
		"->            Go to target\n"
		"<-            Exit\n"
748
		"H             Cycle thru hottest instructions\n"
749 750 751 752 753
		"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"
754
		"t             Toggle total period view\n"
755
		"/             Search string\n"
756
		"k             Toggle line numbers\n"
757
		"r             Run available scripts\n"
758
		"?             Search string backwards\n");
759
			continue;
760 761 762 763 764
		case 'r':
			{
				script_browse(NULL);
				continue;
			}
765 766 767 768
		case 'k':
			annotate_browser__opts.show_linenr =
				!annotate_browser__opts.show_linenr;
			break;
769
		case 'H':
770
			nd = browser->curr_hot;
771
			break;
772
		case 's':
773
			if (annotate_browser__toggle_source(browser))
774 775
				ui_helpline__puts(help);
			continue;
776
		case 'o':
777
			annotate_browser__opts.use_offset = !annotate_browser__opts.use_offset;
778
			annotate_browser__update_addr_width(browser);
779
			continue;
780
		case 'j':
781
			annotate_browser__opts.jump_arrows = !annotate_browser__opts.jump_arrows;
782
			continue;
783
		case 'J':
784
			annotate_browser__opts.show_nr_jumps = !annotate_browser__opts.show_nr_jumps;
785
			annotate_browser__update_addr_width(browser);
786
			continue;
787
		case '/':
788
			if (annotate_browser__search(browser, delay_secs)) {
789 790 791 792 793
show_help:
				ui_helpline__puts(help);
			}
			continue;
		case 'n':
794 795 796
			if (browser->searching_backwards ?
			    annotate_browser__continue_search_reverse(browser, delay_secs) :
			    annotate_browser__continue_search(browser, delay_secs))
797 798 799
				goto show_help;
			continue;
		case '?':
800
			if (annotate_browser__search_reverse(browser, delay_secs))
801 802
				goto show_help;
			continue;
803 804 805 806
		case 'D': {
			static int seq;
			ui_helpline__pop();
			ui_helpline__fpush("%d: nr_ent=%d, height=%d, idx=%d, top_idx=%d, nr_asm_entries=%d",
807 808 809 810 811
					   seq++, browser->b.nr_entries,
					   browser->b.height,
					   browser->b.index,
					   browser->b.top_idx,
					   browser->nr_asm_entries);
812 813
		}
			continue;
814 815
		case K_ENTER:
		case K_RIGHT:
816
			if (browser->selection == NULL)
817
				ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
818
			else if (browser->selection->offset == -1)
819
				ui_helpline__puts("Actions are only available for assembly lines.");
820 821
			else if (!browser->selection->ins) {
				if (strcmp(browser->selection->name, "retq"))
822 823
					goto show_sup_ins;
				goto out;
824
			} else if (!(annotate_browser__jump(browser) ||
825
				     annotate_browser__callq(browser, evsel, hbt))) {
826 827 828
show_sup_ins:
				ui_helpline__puts("Actions are only available for 'callq', 'retq' & jump instructions.");
			}
829
			continue;
830 831 832 833 834
		case 't':
			annotate_browser__opts.show_total_period =
			  !annotate_browser__opts.show_total_period;
			annotate_browser__update_addr_width(browser);
			continue;
835 836
		case K_LEFT:
		case K_ESC:
837 838
		case 'q':
		case CTRL('c'):
839
			goto out;
840 841
		default:
			continue;
842
		}
843 844

		if (nd != NULL)
845
			annotate_browser__set_rb_top(browser, nd);
846 847
	}
out:
848
	ui_browser__hide(&browser->b);
849
	return key;
850 851
}

852 853 854
int map_symbol__tui_annotate(struct map_symbol *ms, struct perf_evsel *evsel,
			     struct hist_browser_timer *hbt)
{
855 856 857 858
	/* Set default value for show_total_period.  */
	annotate_browser__opts.show_total_period =
	  symbol_conf.show_total_period;

859 860 861
	return symbol__tui_annotate(ms->sym, ms->map, evsel, hbt);
}

862
int hist_entry__tui_annotate(struct hist_entry *he, struct perf_evsel *evsel,
863
			     struct hist_browser_timer *hbt)
864
{
865 866 867 868
	/* reset abort key so that it can get Ctrl-C as a key */
	SLang_reset_tty();
	SLang_init_tty(0, 0, 0);

869
	return map_symbol__tui_annotate(&he->ms, evsel, hbt);
870 871
}

872 873 874 875
static void annotate_browser__mark_jump_targets(struct annotate_browser *browser,
						size_t size)
{
	u64 offset;
876 877 878 879 880 881
	struct map_symbol *ms = browser->b.priv;
	struct symbol *sym = ms->sym;

	/* PLT symbols contain external offsets */
	if (strstr(sym->name, "@plt"))
		return;
882 883 884 885 886

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

887
		if (!disasm_line__is_valid_jump(dl, sym))
888 889
			continue;

890
		dlt = browser->offsets[dl->ops.target.offset];
891 892 893 894 895 896 897
		/*
 		 * FIXME: Oops, no jump target? Buggy disassembler? Or do we
 		 * have to adjust to the previous offset?
 		 */
		if (dlt == NULL)
			continue;

898
		bdlt = disasm_line__browser(dlt);
899 900 901 902
		if (++bdlt->jump_sources > browser->max_jump_sources)
			browser->max_jump_sources = bdlt->jump_sources;

		++browser->nr_jumps;
903 904 905
	}
}

906 907 908 909 910 911 912 913 914
static inline int width_jumps(int n)
{
	if (n >= 100)
		return 5;
	if (n / 10)
		return 2;
	return 1;
}

915 916
int symbol__tui_annotate(struct symbol *sym, struct map *map,
			 struct perf_evsel *evsel,
917
			 struct hist_browser_timer *hbt)
918
{
919
	struct disasm_line *pos, *n;
920
	struct annotation *notes;
921
	size_t size;
922 923 924 925
	struct map_symbol ms = {
		.map = map,
		.sym = sym,
	};
926 927
	struct annotate_browser browser = {
		.b = {
928
			.refresh = annotate_browser__refresh,
929 930
			.seek	 = ui_browser__list_head_seek,
			.write	 = annotate_browser__write,
931
			.filter  = disasm_line__filter,
932
			.priv	 = &ms,
933
			.use_navkeypressed = true,
934
		},
935
	};
936
	int ret = -1;
937 938
	int nr_pcnt = 1;
	size_t sizeof_bdl = sizeof(struct browser_disasm_line);
939

940
	if (sym == NULL)
941 942
		return -1;

943 944
	size = symbol__size(sym);

945
	if (map->dso->annotate_warned)
946 947
		return -1;

948 949 950 951 952 953
	browser.offsets = zalloc(size * sizeof(struct disasm_line *));
	if (browser.offsets == NULL) {
		ui__error("Not enough memory!");
		return -1;
	}

954 955
	if (perf_evsel__is_group_event(evsel)) {
		nr_pcnt = evsel->nr_members;
956 957
		sizeof_bdl += sizeof(struct disasm_line_samples) *
		  (nr_pcnt - 1);
958 959 960
	}

	if (symbol__annotate(sym, map, sizeof_bdl) < 0) {
961
		ui__error("%s", ui_helpline__last_msg);
962
		goto out_free_offsets;
963 964 965 966
	}

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

967
	notes = symbol__annotation(sym);
968
	browser.start = map__rip_2objdump(map, sym->start);
969

970
	list_for_each_entry(pos, &notes->src->source, node) {
971
		struct browser_disasm_line *bpos;
972
		size_t line_len = strlen(pos->line);
973

974 975
		if (browser.b.width < line_len)
			browser.b.width = line_len;
976 977
		bpos = disasm_line__browser(pos);
		bpos->idx = browser.nr_entries++;
978
		if (pos->offset != -1) {
979
			bpos->idx_asm = browser.nr_asm_entries++;
980 981 982 983 984 985 986 987 988
			/*
			 * 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;
989
		} else
990
			bpos->idx_asm = -1;
991 992
	}

993 994
	annotate_browser__mark_jump_targets(&browser, size);

995
	browser.addr_width = browser.target_width = browser.min_addr_width = hex_width(size);
996
	browser.max_addr_width = hex_width(sym->end);
997
	browser.jumps_width = width_jumps(browser.max_jump_sources);
998
	browser.nr_events = nr_pcnt;
999
	browser.b.nr_entries = browser.nr_entries;
1000
	browser.b.entries = &notes->src->source,
1001
	browser.b.width += 18; /* Percentage */
1002 1003 1004 1005 1006 1007

	if (annotate_browser__opts.hide_src_code)
		annotate_browser__init_asm_mode(&browser);

	annotate_browser__update_addr_width(&browser);

1008
	ret = annotate_browser__run(&browser, evsel, hbt);
1009
	list_for_each_entry_safe(pos, n, &notes->src->source, node) {
1010
		list_del(&pos->node);
1011
		disasm_line__free(pos);
1012
	}
1013 1014 1015

out_free_offsets:
	free(browser.offsets);
1016 1017
	return ret;
}
1018 1019 1020

#define ANNOTATE_CFG(n) \
	{ .name = #n, .value = &annotate_browser__opts.n, }
1021

1022 1023 1024
/*
 * Keep the entries sorted, they are bsearch'ed
 */
1025
static struct annotate_config {
1026 1027 1028 1029 1030
	const char *name;
	bool *value;
} annotate__configs[] = {
	ANNOTATE_CFG(hide_src_code),
	ANNOTATE_CFG(jump_arrows),
1031
	ANNOTATE_CFG(show_linenr),
1032 1033
	ANNOTATE_CFG(show_nr_jumps),
	ANNOTATE_CFG(use_offset),
1034
	ANNOTATE_CFG(show_total_period),
1035 1036 1037 1038 1039 1040
};

#undef ANNOTATE_CFG

static int annotate_config__cmp(const void *name, const void *cfgp)
{
1041
	const struct annotate_config *cfg = cfgp;
1042 1043 1044 1045

	return strcmp(name, cfg->name);
}

1046 1047
static int annotate__config(const char *var, const char *value,
			    void *data __maybe_unused)
1048
{
1049
	struct annotate_config *cfg;
1050 1051 1052 1053 1054 1055 1056
	const char *name;

	if (prefixcmp(var, "annotate.") != 0)
		return 0;

	name = var + 9;
	cfg = bsearch(name, annotate__configs, ARRAY_SIZE(annotate__configs),
1057
		      sizeof(struct annotate_config), annotate_config__cmp);
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069

	if (cfg == NULL)
		return -1;

	*cfg->value = perf_config_bool(name, value);
	return 0;
}

void annotate_browser__init(void)
{
	perf_config(annotate__config, NULL);
}