xref: /openbmc/linux/tools/perf/ui/gtk/annotate.c (revision 237522378604a2e26e19a8b11a70171eaf98c6c5)
12b676bf0SNamhyung Kim #include "gtk.h"
22b676bf0SNamhyung Kim #include "util/debug.h"
32b676bf0SNamhyung Kim #include "util/annotate.h"
42b676bf0SNamhyung Kim #include "ui/helpline.h"
52b676bf0SNamhyung Kim 
62b676bf0SNamhyung Kim 
72b676bf0SNamhyung Kim enum {
82b676bf0SNamhyung Kim 	ANN_COL__PERCENT,
92b676bf0SNamhyung Kim 	ANN_COL__OFFSET,
102b676bf0SNamhyung Kim 	ANN_COL__LINE,
112b676bf0SNamhyung Kim 
122b676bf0SNamhyung Kim 	MAX_ANN_COLS
132b676bf0SNamhyung Kim };
142b676bf0SNamhyung Kim 
152b676bf0SNamhyung Kim static const char *const col_names[] = {
162b676bf0SNamhyung Kim 	"Overhead",
172b676bf0SNamhyung Kim 	"Offset",
182b676bf0SNamhyung Kim 	"Line"
192b676bf0SNamhyung Kim };
202b676bf0SNamhyung Kim 
212b676bf0SNamhyung Kim static int perf_gtk__get_percent(char *buf, size_t size, struct symbol *sym,
222b676bf0SNamhyung Kim 				 struct disasm_line *dl, int evidx)
232b676bf0SNamhyung Kim {
242b676bf0SNamhyung Kim 	struct sym_hist *symhist;
252b676bf0SNamhyung Kim 	double percent = 0.0;
262b676bf0SNamhyung Kim 	const char *markup;
272b676bf0SNamhyung Kim 	int ret = 0;
282b676bf0SNamhyung Kim 
292b676bf0SNamhyung Kim 	strcpy(buf, "");
302b676bf0SNamhyung Kim 
312b676bf0SNamhyung Kim 	if (dl->offset == (s64) -1)
322b676bf0SNamhyung Kim 		return 0;
332b676bf0SNamhyung Kim 
342b676bf0SNamhyung Kim 	symhist = annotation__histogram(symbol__annotation(sym), evidx);
352b676bf0SNamhyung Kim 	if (!symhist->addr[dl->offset])
362b676bf0SNamhyung Kim 		return 0;
372b676bf0SNamhyung Kim 
382b676bf0SNamhyung Kim 	percent = 100.0 * symhist->addr[dl->offset] / symhist->sum;
392b676bf0SNamhyung Kim 
402b676bf0SNamhyung Kim 	markup = perf_gtk__get_percent_color(percent);
412b676bf0SNamhyung Kim 	if (markup)
422b676bf0SNamhyung Kim 		ret += scnprintf(buf, size, "%s", markup);
432b676bf0SNamhyung Kim 	ret += scnprintf(buf + ret, size - ret, "%6.2f%%", percent);
442b676bf0SNamhyung Kim 	if (markup)
452b676bf0SNamhyung Kim 		ret += scnprintf(buf + ret, size - ret, "</span>");
462b676bf0SNamhyung Kim 
472b676bf0SNamhyung Kim 	return ret;
482b676bf0SNamhyung Kim }
492b676bf0SNamhyung Kim 
502b676bf0SNamhyung Kim static int perf_gtk__get_offset(char *buf, size_t size, struct symbol *sym,
512b676bf0SNamhyung Kim 				struct map *map, struct disasm_line *dl)
522b676bf0SNamhyung Kim {
532b676bf0SNamhyung Kim 	u64 start = map__rip_2objdump(map, sym->start);
542b676bf0SNamhyung Kim 
552b676bf0SNamhyung Kim 	strcpy(buf, "");
562b676bf0SNamhyung Kim 
572b676bf0SNamhyung Kim 	if (dl->offset == (s64) -1)
582b676bf0SNamhyung Kim 		return 0;
592b676bf0SNamhyung Kim 
602b676bf0SNamhyung Kim 	return scnprintf(buf, size, "%"PRIx64, start + dl->offset);
612b676bf0SNamhyung Kim }
622b676bf0SNamhyung Kim 
63*23752237SNamhyung Kim static int perf_gtk__get_line(char *buf, size_t size, struct disasm_line *dl)
64*23752237SNamhyung Kim {
65*23752237SNamhyung Kim 	int ret = 0;
66*23752237SNamhyung Kim 	char *line = g_markup_escape_text(dl->line, -1);
67*23752237SNamhyung Kim 	const char *markup = "<span fgcolor='gray'>";
68*23752237SNamhyung Kim 
69*23752237SNamhyung Kim 	strcpy(buf, "");
70*23752237SNamhyung Kim 
71*23752237SNamhyung Kim 	if (!line)
72*23752237SNamhyung Kim 		return 0;
73*23752237SNamhyung Kim 
74*23752237SNamhyung Kim 	if (dl->offset != (s64) -1)
75*23752237SNamhyung Kim 		markup = NULL;
76*23752237SNamhyung Kim 
77*23752237SNamhyung Kim 	if (markup)
78*23752237SNamhyung Kim 		ret += scnprintf(buf, size, "%s", markup);
79*23752237SNamhyung Kim 	ret += scnprintf(buf + ret, size - ret, "%s", line);
80*23752237SNamhyung Kim 	if (markup)
81*23752237SNamhyung Kim 		ret += scnprintf(buf + ret, size - ret, "</span>");
82*23752237SNamhyung Kim 
83*23752237SNamhyung Kim 	g_free(line);
84*23752237SNamhyung Kim 	return ret;
85*23752237SNamhyung Kim }
86*23752237SNamhyung Kim 
872b676bf0SNamhyung Kim static int perf_gtk__annotate_symbol(GtkWidget *window, struct symbol *sym,
882b676bf0SNamhyung Kim 				struct map *map, int evidx,
892b676bf0SNamhyung Kim 				struct hist_browser_timer *hbt __maybe_unused)
902b676bf0SNamhyung Kim {
912b676bf0SNamhyung Kim 	struct disasm_line *pos, *n;
922b676bf0SNamhyung Kim 	struct annotation *notes;
932b676bf0SNamhyung Kim 	GType col_types[MAX_ANN_COLS];
942b676bf0SNamhyung Kim 	GtkCellRenderer *renderer;
952b676bf0SNamhyung Kim 	GtkListStore *store;
962b676bf0SNamhyung Kim 	GtkWidget *view;
972b676bf0SNamhyung Kim 	int i;
982b676bf0SNamhyung Kim 	char s[512];
992b676bf0SNamhyung Kim 
1002b676bf0SNamhyung Kim 	if (map->dso->annotate_warned)
1012b676bf0SNamhyung Kim 		return -1;
1022b676bf0SNamhyung Kim 
1032b676bf0SNamhyung Kim 	if (symbol__annotate(sym, map, 0) < 0) {
1042b676bf0SNamhyung Kim 		ui__error("%s", ui_helpline__current);
1052b676bf0SNamhyung Kim 		return -1;
1062b676bf0SNamhyung Kim 	}
1072b676bf0SNamhyung Kim 
1082b676bf0SNamhyung Kim 	notes = symbol__annotation(sym);
1092b676bf0SNamhyung Kim 
1102b676bf0SNamhyung Kim 	for (i = 0; i < MAX_ANN_COLS; i++) {
1112b676bf0SNamhyung Kim 		col_types[i] = G_TYPE_STRING;
1122b676bf0SNamhyung Kim 	}
1132b676bf0SNamhyung Kim 	store = gtk_list_store_newv(MAX_ANN_COLS, col_types);
1142b676bf0SNamhyung Kim 
1152b676bf0SNamhyung Kim 	view = gtk_tree_view_new();
1162b676bf0SNamhyung Kim 	renderer = gtk_cell_renderer_text_new();
1172b676bf0SNamhyung Kim 
1182b676bf0SNamhyung Kim 	for (i = 0; i < MAX_ANN_COLS; i++) {
1192b676bf0SNamhyung Kim 		gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
120*23752237SNamhyung Kim 					-1, col_names[i], renderer, "markup",
1212b676bf0SNamhyung Kim 					i, NULL);
1222b676bf0SNamhyung Kim 	}
1232b676bf0SNamhyung Kim 
1242b676bf0SNamhyung Kim 	gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
1252b676bf0SNamhyung Kim 	g_object_unref(GTK_TREE_MODEL(store));
1262b676bf0SNamhyung Kim 
1272b676bf0SNamhyung Kim 	list_for_each_entry(pos, &notes->src->source, node) {
1282b676bf0SNamhyung Kim 		GtkTreeIter iter;
1292b676bf0SNamhyung Kim 
1302b676bf0SNamhyung Kim 		gtk_list_store_append(store, &iter);
1312b676bf0SNamhyung Kim 
1322b676bf0SNamhyung Kim 		if (perf_gtk__get_percent(s, sizeof(s), sym, pos, evidx))
1332b676bf0SNamhyung Kim 			gtk_list_store_set(store, &iter, ANN_COL__PERCENT, s, -1);
1342b676bf0SNamhyung Kim 		if (perf_gtk__get_offset(s, sizeof(s), sym, map, pos))
1352b676bf0SNamhyung Kim 			gtk_list_store_set(store, &iter, ANN_COL__OFFSET, s, -1);
136*23752237SNamhyung Kim 		if (perf_gtk__get_line(s, sizeof(s), pos))
137*23752237SNamhyung Kim 			gtk_list_store_set(store, &iter, ANN_COL__LINE, s, -1);
1382b676bf0SNamhyung Kim 	}
1392b676bf0SNamhyung Kim 
1402b676bf0SNamhyung Kim 	gtk_container_add(GTK_CONTAINER(window), view);
1412b676bf0SNamhyung Kim 
1422b676bf0SNamhyung Kim 	list_for_each_entry_safe(pos, n, &notes->src->source, node) {
1432b676bf0SNamhyung Kim 		list_del(&pos->node);
1442b676bf0SNamhyung Kim 		disasm_line__free(pos);
1452b676bf0SNamhyung Kim 	}
1462b676bf0SNamhyung Kim 
1472b676bf0SNamhyung Kim 	return 0;
1482b676bf0SNamhyung Kim }
1492b676bf0SNamhyung Kim 
1502b676bf0SNamhyung Kim int symbol__gtk_annotate(struct symbol *sym, struct map *map, int evidx,
1512b676bf0SNamhyung Kim 			 struct hist_browser_timer *hbt)
1522b676bf0SNamhyung Kim {
1532b676bf0SNamhyung Kim 	GtkWidget *window;
1547a60ba94SNamhyung Kim 	GtkWidget *notebook;
1552b676bf0SNamhyung Kim 	GtkWidget *scrolled_window;
1562b676bf0SNamhyung Kim 	GtkWidget *tab_label;
1572b676bf0SNamhyung Kim 
1587a60ba94SNamhyung Kim 	if (perf_gtk__is_active_context(pgctx)) {
1597a60ba94SNamhyung Kim 		window = pgctx->main_window;
1607a60ba94SNamhyung Kim 		notebook = pgctx->notebook;
1617a60ba94SNamhyung Kim 	} else {
1627a60ba94SNamhyung Kim 		GtkWidget *vbox;
1637a60ba94SNamhyung Kim 		GtkWidget *infobar;
1647a60ba94SNamhyung Kim 		GtkWidget *statbar;
1657a60ba94SNamhyung Kim 
1662b676bf0SNamhyung Kim 		signal(SIGSEGV, perf_gtk__signal);
1672b676bf0SNamhyung Kim 		signal(SIGFPE,  perf_gtk__signal);
1682b676bf0SNamhyung Kim 		signal(SIGINT,  perf_gtk__signal);
1692b676bf0SNamhyung Kim 		signal(SIGQUIT, perf_gtk__signal);
1702b676bf0SNamhyung Kim 		signal(SIGTERM, perf_gtk__signal);
1712b676bf0SNamhyung Kim 
1722b676bf0SNamhyung Kim 		window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1732b676bf0SNamhyung Kim 		gtk_window_set_title(GTK_WINDOW(window), "perf annotate");
1742b676bf0SNamhyung Kim 
1752b676bf0SNamhyung Kim 		g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
1762b676bf0SNamhyung Kim 
1772b676bf0SNamhyung Kim 		pgctx = perf_gtk__activate_context(window);
1782b676bf0SNamhyung Kim 		if (!pgctx)
1792b676bf0SNamhyung Kim 			return -1;
1802b676bf0SNamhyung Kim 
1812b676bf0SNamhyung Kim 		vbox = gtk_vbox_new(FALSE, 0);
1822b676bf0SNamhyung Kim 		notebook = gtk_notebook_new();
1837a60ba94SNamhyung Kim 		pgctx->notebook = notebook;
1847a60ba94SNamhyung Kim 
1857a60ba94SNamhyung Kim 		gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
1867a60ba94SNamhyung Kim 
1877a60ba94SNamhyung Kim 		infobar = perf_gtk__setup_info_bar();
1887a60ba94SNamhyung Kim 		if (infobar) {
1897a60ba94SNamhyung Kim 			gtk_box_pack_start(GTK_BOX(vbox), infobar,
1907a60ba94SNamhyung Kim 					   FALSE, FALSE, 0);
1917a60ba94SNamhyung Kim 		}
1927a60ba94SNamhyung Kim 
1937a60ba94SNamhyung Kim 		statbar = perf_gtk__setup_statusbar();
1947a60ba94SNamhyung Kim 		gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0);
1957a60ba94SNamhyung Kim 
1967a60ba94SNamhyung Kim 		gtk_container_add(GTK_CONTAINER(window), vbox);
1977a60ba94SNamhyung Kim 	}
1987a60ba94SNamhyung Kim 
1992b676bf0SNamhyung Kim 	scrolled_window = gtk_scrolled_window_new(NULL, NULL);
2002b676bf0SNamhyung Kim 	tab_label = gtk_label_new(sym->name);
2012b676bf0SNamhyung Kim 
2022b676bf0SNamhyung Kim 	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
2032b676bf0SNamhyung Kim 				       GTK_POLICY_AUTOMATIC,
2042b676bf0SNamhyung Kim 				       GTK_POLICY_AUTOMATIC);
2052b676bf0SNamhyung Kim 
2062b676bf0SNamhyung Kim 	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window,
2072b676bf0SNamhyung Kim 				 tab_label);
2082b676bf0SNamhyung Kim 
2092b676bf0SNamhyung Kim 	perf_gtk__annotate_symbol(scrolled_window, sym, map, evidx, hbt);
2107a60ba94SNamhyung Kim 	return 0;
2117a60ba94SNamhyung Kim }
2122b676bf0SNamhyung Kim 
2137a60ba94SNamhyung Kim void perf_gtk__show_annotations(void)
2147a60ba94SNamhyung Kim {
2157a60ba94SNamhyung Kim 	GtkWidget *window;
2167a60ba94SNamhyung Kim 
2177a60ba94SNamhyung Kim 	if (!perf_gtk__is_active_context(pgctx))
2187a60ba94SNamhyung Kim 		return;
2197a60ba94SNamhyung Kim 
2207a60ba94SNamhyung Kim 	window = pgctx->main_window;
2212b676bf0SNamhyung Kim 	gtk_widget_show_all(window);
2222b676bf0SNamhyung Kim 
2232b676bf0SNamhyung Kim 	perf_gtk__resize_window(window);
2242b676bf0SNamhyung Kim 	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
2252b676bf0SNamhyung Kim 
2262b676bf0SNamhyung Kim 	gtk_main();
2272b676bf0SNamhyung Kim 
2282b676bf0SNamhyung Kim 	perf_gtk__deactivate_context(&pgctx);
2292b676bf0SNamhyung Kim }
230