xref: /linux/tools/perf/ui/gtk/annotate.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
1 #include "gtk.h"
2 #include "util/debug.h"
3 #include "util/annotate.h"
4 #include "util/evsel.h"
5 #include "ui/helpline.h"
6 #include <inttypes.h>
7 #include <signal.h>
8 
9 enum {
10 	ANN_COL__PERCENT,
11 	ANN_COL__OFFSET,
12 	ANN_COL__LINE,
13 
14 	MAX_ANN_COLS
15 };
16 
17 static const char *const col_names[] = {
18 	"Overhead",
19 	"Offset",
20 	"Line"
21 };
22 
23 static int perf_gtk__get_percent(char *buf, size_t size, struct symbol *sym,
24 				 struct disasm_line *dl, int evidx)
25 {
26 	struct sym_hist *symhist;
27 	double percent = 0.0;
28 	const char *markup;
29 	int ret = 0;
30 
31 	strcpy(buf, "");
32 
33 	if (dl->offset == (s64) -1)
34 		return 0;
35 
36 	symhist = annotation__histogram(symbol__annotation(sym), evidx);
37 	if (!symbol_conf.event_group && !symhist->addr[dl->offset])
38 		return 0;
39 
40 	percent = 100.0 * symhist->addr[dl->offset] / symhist->sum;
41 
42 	markup = perf_gtk__get_percent_color(percent);
43 	if (markup)
44 		ret += scnprintf(buf, size, "%s", markup);
45 	ret += scnprintf(buf + ret, size - ret, "%6.2f%%", percent);
46 	if (markup)
47 		ret += scnprintf(buf + ret, size - ret, "</span>");
48 
49 	return ret;
50 }
51 
52 static int perf_gtk__get_offset(char *buf, size_t size, struct symbol *sym,
53 				struct map *map, struct disasm_line *dl)
54 {
55 	u64 start = map__rip_2objdump(map, sym->start);
56 
57 	strcpy(buf, "");
58 
59 	if (dl->offset == (s64) -1)
60 		return 0;
61 
62 	return scnprintf(buf, size, "%"PRIx64, start + dl->offset);
63 }
64 
65 static int perf_gtk__get_line(char *buf, size_t size, struct disasm_line *dl)
66 {
67 	int ret = 0;
68 	char *line = g_markup_escape_text(dl->line, -1);
69 	const char *markup = "<span fgcolor='gray'>";
70 
71 	strcpy(buf, "");
72 
73 	if (!line)
74 		return 0;
75 
76 	if (dl->offset != (s64) -1)
77 		markup = NULL;
78 
79 	if (markup)
80 		ret += scnprintf(buf, size, "%s", markup);
81 	ret += scnprintf(buf + ret, size - ret, "%s", line);
82 	if (markup)
83 		ret += scnprintf(buf + ret, size - ret, "</span>");
84 
85 	g_free(line);
86 	return ret;
87 }
88 
89 static int perf_gtk__annotate_symbol(GtkWidget *window, struct symbol *sym,
90 				struct map *map, struct perf_evsel *evsel,
91 				struct hist_browser_timer *hbt __maybe_unused)
92 {
93 	struct disasm_line *pos, *n;
94 	struct annotation *notes;
95 	GType col_types[MAX_ANN_COLS];
96 	GtkCellRenderer *renderer;
97 	GtkListStore *store;
98 	GtkWidget *view;
99 	int i;
100 	char s[512];
101 
102 	notes = symbol__annotation(sym);
103 
104 	for (i = 0; i < MAX_ANN_COLS; i++) {
105 		col_types[i] = G_TYPE_STRING;
106 	}
107 	store = gtk_list_store_newv(MAX_ANN_COLS, col_types);
108 
109 	view = gtk_tree_view_new();
110 	renderer = gtk_cell_renderer_text_new();
111 
112 	for (i = 0; i < MAX_ANN_COLS; i++) {
113 		gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
114 					-1, col_names[i], renderer, "markup",
115 					i, NULL);
116 	}
117 
118 	gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
119 	g_object_unref(GTK_TREE_MODEL(store));
120 
121 	list_for_each_entry(pos, &notes->src->source, node) {
122 		GtkTreeIter iter;
123 		int ret = 0;
124 
125 		gtk_list_store_append(store, &iter);
126 
127 		if (perf_evsel__is_group_event(evsel)) {
128 			for (i = 0; i < evsel->nr_members; i++) {
129 				ret += perf_gtk__get_percent(s + ret,
130 							     sizeof(s) - ret,
131 							     sym, pos,
132 							     evsel->idx + i);
133 				ret += scnprintf(s + ret, sizeof(s) - ret, " ");
134 			}
135 		} else {
136 			ret = perf_gtk__get_percent(s, sizeof(s), sym, pos,
137 						    evsel->idx);
138 		}
139 
140 		if (ret)
141 			gtk_list_store_set(store, &iter, ANN_COL__PERCENT, s, -1);
142 		if (perf_gtk__get_offset(s, sizeof(s), sym, map, pos))
143 			gtk_list_store_set(store, &iter, ANN_COL__OFFSET, s, -1);
144 		if (perf_gtk__get_line(s, sizeof(s), pos))
145 			gtk_list_store_set(store, &iter, ANN_COL__LINE, s, -1);
146 	}
147 
148 	gtk_container_add(GTK_CONTAINER(window), view);
149 
150 	list_for_each_entry_safe(pos, n, &notes->src->source, node) {
151 		list_del(&pos->node);
152 		disasm_line__free(pos);
153 	}
154 
155 	return 0;
156 }
157 
158 static int symbol__gtk_annotate(struct symbol *sym, struct map *map,
159 				struct perf_evsel *evsel,
160 				struct hist_browser_timer *hbt)
161 {
162 	GtkWidget *window;
163 	GtkWidget *notebook;
164 	GtkWidget *scrolled_window;
165 	GtkWidget *tab_label;
166 	int err;
167 
168 	if (map->dso->annotate_warned)
169 		return -1;
170 
171 	err = symbol__disassemble(sym, map, perf_evsel__env_arch(evsel), 0);
172 	if (err) {
173 		char msg[BUFSIZ];
174 		symbol__strerror_disassemble(sym, map, err, msg, sizeof(msg));
175 		ui__error("Couldn't annotate %s: %s\n", sym->name, msg);
176 		return -1;
177 	}
178 
179 	if (perf_gtk__is_active_context(pgctx)) {
180 		window = pgctx->main_window;
181 		notebook = pgctx->notebook;
182 	} else {
183 		GtkWidget *vbox;
184 		GtkWidget *infobar;
185 		GtkWidget *statbar;
186 
187 		signal(SIGSEGV, perf_gtk__signal);
188 		signal(SIGFPE,  perf_gtk__signal);
189 		signal(SIGINT,  perf_gtk__signal);
190 		signal(SIGQUIT, perf_gtk__signal);
191 		signal(SIGTERM, perf_gtk__signal);
192 
193 		window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
194 		gtk_window_set_title(GTK_WINDOW(window), "perf annotate");
195 
196 		g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
197 
198 		pgctx = perf_gtk__activate_context(window);
199 		if (!pgctx)
200 			return -1;
201 
202 		vbox = gtk_vbox_new(FALSE, 0);
203 		notebook = gtk_notebook_new();
204 		pgctx->notebook = notebook;
205 
206 		gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
207 
208 		infobar = perf_gtk__setup_info_bar();
209 		if (infobar) {
210 			gtk_box_pack_start(GTK_BOX(vbox), infobar,
211 					   FALSE, FALSE, 0);
212 		}
213 
214 		statbar = perf_gtk__setup_statusbar();
215 		gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0);
216 
217 		gtk_container_add(GTK_CONTAINER(window), vbox);
218 	}
219 
220 	scrolled_window = gtk_scrolled_window_new(NULL, NULL);
221 	tab_label = gtk_label_new(sym->name);
222 
223 	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
224 				       GTK_POLICY_AUTOMATIC,
225 				       GTK_POLICY_AUTOMATIC);
226 
227 	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window,
228 				 tab_label);
229 
230 	perf_gtk__annotate_symbol(scrolled_window, sym, map, evsel, hbt);
231 	return 0;
232 }
233 
234 int hist_entry__gtk_annotate(struct hist_entry *he,
235 			     struct perf_evsel *evsel,
236 			     struct hist_browser_timer *hbt)
237 {
238 	return symbol__gtk_annotate(he->ms.sym, he->ms.map, evsel, hbt);
239 }
240 
241 void perf_gtk__show_annotations(void)
242 {
243 	GtkWidget *window;
244 
245 	if (!perf_gtk__is_active_context(pgctx))
246 		return;
247 
248 	window = pgctx->main_window;
249 	gtk_widget_show_all(window);
250 
251 	perf_gtk__resize_window(window);
252 	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
253 
254 	gtk_main();
255 
256 	perf_gtk__deactivate_context(&pgctx);
257 }
258