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