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