blob: 35f9641bf670cb5bc9103efffa8f060e9d8d4627 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Namhyung Kim2b676bf2013-02-07 18:02:08 +09002#include "gtk.h"
Arnaldo Carvalho de Melo8a249c72019-01-22 10:47:38 -02003#include "util/sort.h"
Namhyung Kim2b676bf2013-02-07 18:02:08 +09004#include "util/debug.h"
5#include "util/annotate.h"
Namhyung Kimdb8fd072013-03-05 14:53:21 +09006#include "util/evsel.h"
Arnaldo Carvalho de Melo7b644f92019-01-27 11:55:39 +01007#include "util/map.h"
Arnaldo Carvalho de Melob1d1b092019-08-30 10:26:37 -03008#include "util/dso.h"
Arnaldo Carvalho de Melo7cadca82019-01-27 14:10:31 +01009#include "util/symbol.h"
Namhyung Kim2b676bf2013-02-07 18:02:08 +090010#include "ui/helpline.h"
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030011#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030012#include <signal.h>
Namhyung Kim2b676bf2013-02-07 18:02:08 +090013
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
Jiri Olsad5490b92017-10-11 17:01:26 +020038 if (dl->al.offset == (s64) -1)
Namhyung Kim2b676bf2013-02-07 18:02:08 +090039 return 0;
40
41 symhist = annotation__histogram(symbol__annotation(sym), evidx);
Jiri Olsad5490b92017-10-11 17:01:26 +020042 if (!symbol_conf.event_group && !symhist->addr[dl->al.offset].nr_samples)
Namhyung Kim2b676bf2013-02-07 18:02:08 +090043 return 0;
44
Jiri Olsad5490b92017-10-11 17:01:26 +020045 percent = 100.0 * symhist->addr[dl->al.offset].nr_samples / symhist->nr_samples;
Namhyung Kim2b676bf2013-02-07 18:02:08 +090046
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
Arnaldo Carvalho de Melo29754892019-11-04 11:10:00 -030057static int perf_gtk__get_offset(char *buf, size_t size, struct map_symbol *ms,
58 struct disasm_line *dl)
Namhyung Kim2b676bf2013-02-07 18:02:08 +090059{
Arnaldo Carvalho de Melo29754892019-11-04 11:10:00 -030060 u64 start = map__rip_2objdump(ms->map, ms->sym->start);
Namhyung Kim2b676bf2013-02-07 18:02:08 +090061
62 strcpy(buf, "");
63
Jiri Olsad5490b92017-10-11 17:01:26 +020064 if (dl->al.offset == (s64) -1)
Namhyung Kim2b676bf2013-02-07 18:02:08 +090065 return 0;
66
Jiri Olsad5490b92017-10-11 17:01:26 +020067 return scnprintf(buf, size, "%"PRIx64, start + dl->al.offset);
Namhyung Kim2b676bf2013-02-07 18:02:08 +090068}
69
Namhyung Kim23752232013-02-07 18:02:10 +090070static int perf_gtk__get_line(char *buf, size_t size, struct disasm_line *dl)
71{
72 int ret = 0;
Jiri Olsad5490b92017-10-11 17:01:26 +020073 char *line = g_markup_escape_text(dl->al.line, -1);
Namhyung Kim23752232013-02-07 18:02:10 +090074 const char *markup = "<span fgcolor='gray'>";
75
76 strcpy(buf, "");
77
78 if (!line)
79 return 0;
80
Jiri Olsad5490b92017-10-11 17:01:26 +020081 if (dl->al.offset != (s64) -1)
Namhyung Kim23752232013-02-07 18:02:10 +090082 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
Arnaldo Carvalho de Melo29754892019-11-04 11:10:00 -030094static int perf_gtk__annotate_symbol(GtkWidget *window, struct map_symbol *ms,
95 struct evsel *evsel,
Namhyung Kim2b676bf2013-02-07 18:02:08 +090096 struct hist_browser_timer *hbt __maybe_unused)
97{
Arnaldo Carvalho de Melo29754892019-11-04 11:10:00 -030098 struct symbol *sym = ms->sym;
Namhyung Kim2b676bf2013-02-07 18:02:08 +090099 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
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900108 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),
Namhyung Kim23752232013-02-07 18:02:10 +0900120 -1, col_names[i], renderer, "markup",
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900121 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
Jiri Olsaa17c4ca2017-10-11 17:01:25 +0200127 list_for_each_entry(pos, &notes->src->source, al.node) {
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900128 GtkTreeIter iter;
Namhyung Kimd8d7cd92013-03-05 14:53:32 +0900129 int ret = 0;
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900130
131 gtk_list_store_append(store, &iter);
132
Namhyung Kimd8d7cd92013-03-05 14:53:32 +0900133 if (perf_evsel__is_group_event(evsel)) {
Jiri Olsa5643b1a2019-07-21 13:24:46 +0200134 for (i = 0; i < evsel->core.nr_members; i++) {
Namhyung Kimd8d7cd92013-03-05 14:53:32 +0900135 ret += perf_gtk__get_percent(s + ret,
136 sizeof(s) - ret,
137 sym, pos,
138 evsel->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->idx);
144 }
145
146 if (ret)
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900147 gtk_list_store_set(store, &iter, ANN_COL__PERCENT, s, -1);
Arnaldo Carvalho de Melo29754892019-11-04 11:10:00 -0300148 if (perf_gtk__get_offset(s, sizeof(s), ms, pos))
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900149 gtk_list_store_set(store, &iter, ANN_COL__OFFSET, s, -1);
Namhyung Kim23752232013-02-07 18:02:10 +0900150 if (perf_gtk__get_line(s, sizeof(s), pos))
151 gtk_list_store_set(store, &iter, ANN_COL__LINE, s, -1);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900152 }
153
154 gtk_container_add(GTK_CONTAINER(window), view);
155
Jiri Olsaa17c4ca2017-10-11 17:01:25 +0200156 list_for_each_entry_safe(pos, n, &notes->src->source, al.node) {
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -0300157 list_del_init(&pos->al.node);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900158 disasm_line__free(pos);
159 }
160
161 return 0;
162}
163
Arnaldo Carvalho de Melo29754892019-11-04 11:10:00 -0300164static int symbol__gtk_annotate(struct map_symbol *ms, struct evsel *evsel,
Namhyung Kimfc672972013-09-13 15:27:43 +0900165 struct hist_browser_timer *hbt)
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900166{
Arnaldo Carvalho de Melo29754892019-11-04 11:10:00 -0300167 struct symbol *sym = ms->sym;
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900168 GtkWidget *window;
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900169 GtkWidget *notebook;
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900170 GtkWidget *scrolled_window;
171 GtkWidget *tab_label;
Arnaldo Carvalho de Meloee51d852016-07-29 16:27:18 -0300172 int err;
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900173
Arnaldo Carvalho de Melo29754892019-11-04 11:10:00 -0300174 if (ms->map->dso->annotate_warned)
Namhyung Kimc0e79be2013-02-07 18:02:13 +0900175 return -1;
176
Ravi Bangoriae0ad4d62020-02-04 10:22:28 +0530177 err = symbol__annotate(ms, evsel, &annotation__default_options, NULL);
Arnaldo Carvalho de Meloee51d852016-07-29 16:27:18 -0300178 if (err) {
179 char msg[BUFSIZ];
Arnaldo Carvalho de Melo29754892019-11-04 11:10:00 -0300180 symbol__strerror_disassemble(ms, err, msg, sizeof(msg));
Arnaldo Carvalho de Meloee51d852016-07-29 16:27:18 -0300181 ui__error("Couldn't annotate %s: %s\n", sym->name, msg);
Namhyung Kimc0e79be2013-02-07 18:02:13 +0900182 return -1;
183 }
184
Jiri Olsa05d3f1a2017-11-15 12:20:08 +0100185 symbol__calc_percent(sym, evsel);
186
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900187 if (perf_gtk__is_active_context(pgctx)) {
188 window = pgctx->main_window;
189 notebook = pgctx->notebook;
190 } else {
191 GtkWidget *vbox;
192 GtkWidget *infobar;
193 GtkWidget *statbar;
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900194
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900195 signal(SIGSEGV, perf_gtk__signal);
196 signal(SIGFPE, perf_gtk__signal);
197 signal(SIGINT, perf_gtk__signal);
198 signal(SIGQUIT, perf_gtk__signal);
199 signal(SIGTERM, perf_gtk__signal);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900200
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900201 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
202 gtk_window_set_title(GTK_WINDOW(window), "perf annotate");
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900203
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900204 g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900205
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900206 pgctx = perf_gtk__activate_context(window);
207 if (!pgctx)
208 return -1;
209
210 vbox = gtk_vbox_new(FALSE, 0);
211 notebook = gtk_notebook_new();
212 pgctx->notebook = notebook;
213
214 gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
215
216 infobar = perf_gtk__setup_info_bar();
217 if (infobar) {
218 gtk_box_pack_start(GTK_BOX(vbox), infobar,
219 FALSE, FALSE, 0);
220 }
221
222 statbar = perf_gtk__setup_statusbar();
223 gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0);
224
225 gtk_container_add(GTK_CONTAINER(window), vbox);
226 }
227
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900228 scrolled_window = gtk_scrolled_window_new(NULL, NULL);
229 tab_label = gtk_label_new(sym->name);
230
231 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
232 GTK_POLICY_AUTOMATIC,
233 GTK_POLICY_AUTOMATIC);
234
235 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window,
236 tab_label);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900237
Arnaldo Carvalho de Melo29754892019-11-04 11:10:00 -0300238 perf_gtk__annotate_symbol(scrolled_window, ms, evsel, hbt);
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900239 return 0;
240}
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900241
Namhyung Kimfc672972013-09-13 15:27:43 +0900242int hist_entry__gtk_annotate(struct hist_entry *he,
Jiri Olsa32dcd022019-07-21 13:23:51 +0200243 struct evsel *evsel,
Namhyung Kimfc672972013-09-13 15:27:43 +0900244 struct hist_browser_timer *hbt)
245{
Arnaldo Carvalho de Melo29754892019-11-04 11:10:00 -0300246 return symbol__gtk_annotate(&he->ms, evsel, hbt);
Namhyung Kimfc672972013-09-13 15:27:43 +0900247}
248
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900249void perf_gtk__show_annotations(void)
250{
251 GtkWidget *window;
252
253 if (!perf_gtk__is_active_context(pgctx))
254 return;
255
256 window = pgctx->main_window;
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900257 gtk_widget_show_all(window);
258
259 perf_gtk__resize_window(window);
260 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
261
262 gtk_main();
263
264 perf_gtk__deactivate_context(&pgctx);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900265}