blob: 3af87c18a914f738259d8474fb0105a956224d7d [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 Melo7cadca82019-01-27 14:10:31 +01008#include "util/symbol.h"
Namhyung Kim2b676bf2013-02-07 18:02:08 +09009#include "ui/helpline.h"
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030010#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030011#include <signal.h>
Namhyung Kim2b676bf2013-02-07 18:02:08 +090012
13enum {
14 ANN_COL__PERCENT,
15 ANN_COL__OFFSET,
16 ANN_COL__LINE,
17
18 MAX_ANN_COLS
19};
20
21static const char *const col_names[] = {
22 "Overhead",
23 "Offset",
24 "Line"
25};
26
27static 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
Jiri Olsad5490b92017-10-11 17:01:26 +020037 if (dl->al.offset == (s64) -1)
Namhyung Kim2b676bf2013-02-07 18:02:08 +090038 return 0;
39
40 symhist = annotation__histogram(symbol__annotation(sym), evidx);
Jiri Olsad5490b92017-10-11 17:01:26 +020041 if (!symbol_conf.event_group && !symhist->addr[dl->al.offset].nr_samples)
Namhyung Kim2b676bf2013-02-07 18:02:08 +090042 return 0;
43
Jiri Olsad5490b92017-10-11 17:01:26 +020044 percent = 100.0 * symhist->addr[dl->al.offset].nr_samples / symhist->nr_samples;
Namhyung Kim2b676bf2013-02-07 18:02:08 +090045
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
56static 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
Jiri Olsad5490b92017-10-11 17:01:26 +020063 if (dl->al.offset == (s64) -1)
Namhyung Kim2b676bf2013-02-07 18:02:08 +090064 return 0;
65
Jiri Olsad5490b92017-10-11 17:01:26 +020066 return scnprintf(buf, size, "%"PRIx64, start + dl->al.offset);
Namhyung Kim2b676bf2013-02-07 18:02:08 +090067}
68
Namhyung Kim23752232013-02-07 18:02:10 +090069static int perf_gtk__get_line(char *buf, size_t size, struct disasm_line *dl)
70{
71 int ret = 0;
Jiri Olsad5490b92017-10-11 17:01:26 +020072 char *line = g_markup_escape_text(dl->al.line, -1);
Namhyung Kim23752232013-02-07 18:02:10 +090073 const char *markup = "<span fgcolor='gray'>";
74
75 strcpy(buf, "");
76
77 if (!line)
78 return 0;
79
Jiri Olsad5490b92017-10-11 17:01:26 +020080 if (dl->al.offset != (s64) -1)
Namhyung Kim23752232013-02-07 18:02:10 +090081 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
Namhyung Kim2b676bf2013-02-07 18:02:08 +090093static int perf_gtk__annotate_symbol(GtkWidget *window, struct symbol *sym,
Namhyung Kimdb8fd072013-03-05 14:53:21 +090094 struct map *map, struct perf_evsel *evsel,
Namhyung Kim2b676bf2013-02-07 18:02:08 +090095 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
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900106 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),
Namhyung Kim23752232013-02-07 18:02:10 +0900118 -1, col_names[i], renderer, "markup",
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900119 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
Jiri Olsaa17c4ca2017-10-11 17:01:25 +0200125 list_for_each_entry(pos, &notes->src->source, al.node) {
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900126 GtkTreeIter iter;
Namhyung Kimd8d7cd92013-03-05 14:53:32 +0900127 int ret = 0;
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900128
129 gtk_list_store_append(store, &iter);
130
Namhyung Kimd8d7cd92013-03-05 14:53:32 +0900131 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)
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900145 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);
Namhyung Kim23752232013-02-07 18:02:10 +0900148 if (perf_gtk__get_line(s, sizeof(s), pos))
149 gtk_list_store_set(store, &iter, ANN_COL__LINE, s, -1);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900150 }
151
152 gtk_container_add(GTK_CONTAINER(window), view);
153
Jiri Olsaa17c4ca2017-10-11 17:01:25 +0200154 list_for_each_entry_safe(pos, n, &notes->src->source, al.node) {
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -0300155 list_del_init(&pos->al.node);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900156 disasm_line__free(pos);
157 }
158
159 return 0;
160}
161
Namhyung Kimfc672972013-09-13 15:27:43 +0900162static int symbol__gtk_annotate(struct symbol *sym, struct map *map,
163 struct perf_evsel *evsel,
164 struct hist_browser_timer *hbt)
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900165{
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900166 GtkWidget *window;
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900167 GtkWidget *notebook;
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900168 GtkWidget *scrolled_window;
169 GtkWidget *tab_label;
Arnaldo Carvalho de Meloee51d852016-07-29 16:27:18 -0300170 int err;
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900171
Namhyung Kimc0e79be2013-02-07 18:02:13 +0900172 if (map->dso->annotate_warned)
173 return -1;
174
Arnaldo Carvalho de Melo380195e2018-05-28 11:27:40 -0300175 err = symbol__annotate(sym, map, evsel, 0, &annotation__default_options, NULL);
Arnaldo Carvalho de Meloee51d852016-07-29 16:27:18 -0300176 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);
Namhyung Kimc0e79be2013-02-07 18:02:13 +0900180 return -1;
181 }
182
Jiri Olsa05d3f1a2017-11-15 12:20:08 +0100183 symbol__calc_percent(sym, evsel);
184
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900185 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;
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900192
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900193 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);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900198
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900199 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
200 gtk_window_set_title(GTK_WINDOW(window), "perf annotate");
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900201
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900202 g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900203
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900204 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
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900226 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);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900235
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900236 perf_gtk__annotate_symbol(scrolled_window, sym, map, evsel, hbt);
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900237 return 0;
238}
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900239
Namhyung Kimfc672972013-09-13 15:27:43 +0900240int 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
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900247void 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;
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900255 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);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900263}