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