blob: 1ce89f2558faa4d1a4680750071ef0a52961cca0 [file] [log] [blame]
Namhyung Kim2b676bf2013-02-07 18:02:08 +09001#include "gtk.h"
2#include "util/debug.h"
3#include "util/annotate.h"
4#include "ui/helpline.h"
5
6
7enum {
8 ANN_COL__PERCENT,
9 ANN_COL__OFFSET,
10 ANN_COL__LINE,
11
12 MAX_ANN_COLS
13};
14
15static const char *const col_names[] = {
16 "Overhead",
17 "Offset",
18 "Line"
19};
20
21static int perf_gtk__get_percent(char *buf, size_t size, struct symbol *sym,
22 struct disasm_line *dl, int evidx)
23{
24 struct sym_hist *symhist;
25 double percent = 0.0;
26 const char *markup;
27 int ret = 0;
28
29 strcpy(buf, "");
30
31 if (dl->offset == (s64) -1)
32 return 0;
33
34 symhist = annotation__histogram(symbol__annotation(sym), evidx);
35 if (!symhist->addr[dl->offset])
36 return 0;
37
38 percent = 100.0 * symhist->addr[dl->offset] / symhist->sum;
39
40 markup = perf_gtk__get_percent_color(percent);
41 if (markup)
42 ret += scnprintf(buf, size, "%s", markup);
43 ret += scnprintf(buf + ret, size - ret, "%6.2f%%", percent);
44 if (markup)
45 ret += scnprintf(buf + ret, size - ret, "</span>");
46
47 return ret;
48}
49
50static int perf_gtk__get_offset(char *buf, size_t size, struct symbol *sym,
51 struct map *map, struct disasm_line *dl)
52{
53 u64 start = map__rip_2objdump(map, sym->start);
54
55 strcpy(buf, "");
56
57 if (dl->offset == (s64) -1)
58 return 0;
59
60 return scnprintf(buf, size, "%"PRIx64, start + dl->offset);
61}
62
63static int perf_gtk__annotate_symbol(GtkWidget *window, struct symbol *sym,
64 struct map *map, int evidx,
65 struct hist_browser_timer *hbt __maybe_unused)
66{
67 struct disasm_line *pos, *n;
68 struct annotation *notes;
69 GType col_types[MAX_ANN_COLS];
70 GtkCellRenderer *renderer;
71 GtkListStore *store;
72 GtkWidget *view;
73 int i;
74 char s[512];
75
76 if (map->dso->annotate_warned)
77 return -1;
78
79 if (symbol__annotate(sym, map, 0) < 0) {
80 ui__error("%s", ui_helpline__current);
81 return -1;
82 }
83
84 notes = symbol__annotation(sym);
85
86 for (i = 0; i < MAX_ANN_COLS; i++) {
87 col_types[i] = G_TYPE_STRING;
88 }
89 store = gtk_list_store_newv(MAX_ANN_COLS, col_types);
90
91 view = gtk_tree_view_new();
92 renderer = gtk_cell_renderer_text_new();
93
94 for (i = 0; i < MAX_ANN_COLS; i++) {
95 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
96 -1, col_names[i], renderer,
97 i == ANN_COL__PERCENT ? "markup" : "text",
98 i, NULL);
99 }
100
101 gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
102 g_object_unref(GTK_TREE_MODEL(store));
103
104 list_for_each_entry(pos, &notes->src->source, node) {
105 GtkTreeIter iter;
106
107 gtk_list_store_append(store, &iter);
108
109 if (perf_gtk__get_percent(s, sizeof(s), sym, pos, evidx))
110 gtk_list_store_set(store, &iter, ANN_COL__PERCENT, s, -1);
111 if (perf_gtk__get_offset(s, sizeof(s), sym, map, pos))
112 gtk_list_store_set(store, &iter, ANN_COL__OFFSET, s, -1);
113 gtk_list_store_set(store, &iter, ANN_COL__LINE, pos->line, -1);
114 }
115
116 gtk_container_add(GTK_CONTAINER(window), view);
117
118 list_for_each_entry_safe(pos, n, &notes->src->source, node) {
119 list_del(&pos->node);
120 disasm_line__free(pos);
121 }
122
123 return 0;
124}
125
126int symbol__gtk_annotate(struct symbol *sym, struct map *map, int evidx,
127 struct hist_browser_timer *hbt)
128{
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900129 GtkWidget *window;
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900130 GtkWidget *notebook;
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900131 GtkWidget *scrolled_window;
132 GtkWidget *tab_label;
133
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900134 if (perf_gtk__is_active_context(pgctx)) {
135 window = pgctx->main_window;
136 notebook = pgctx->notebook;
137 } else {
138 GtkWidget *vbox;
139 GtkWidget *infobar;
140 GtkWidget *statbar;
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900141
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900142 signal(SIGSEGV, perf_gtk__signal);
143 signal(SIGFPE, perf_gtk__signal);
144 signal(SIGINT, perf_gtk__signal);
145 signal(SIGQUIT, perf_gtk__signal);
146 signal(SIGTERM, perf_gtk__signal);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900147
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900148 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
149 gtk_window_set_title(GTK_WINDOW(window), "perf annotate");
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900150
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900151 g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900152
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900153 pgctx = perf_gtk__activate_context(window);
154 if (!pgctx)
155 return -1;
156
157 vbox = gtk_vbox_new(FALSE, 0);
158 notebook = gtk_notebook_new();
159 pgctx->notebook = notebook;
160
161 gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
162
163 infobar = perf_gtk__setup_info_bar();
164 if (infobar) {
165 gtk_box_pack_start(GTK_BOX(vbox), infobar,
166 FALSE, FALSE, 0);
167 }
168
169 statbar = perf_gtk__setup_statusbar();
170 gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0);
171
172 gtk_container_add(GTK_CONTAINER(window), vbox);
173 }
174
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900175 scrolled_window = gtk_scrolled_window_new(NULL, NULL);
176 tab_label = gtk_label_new(sym->name);
177
178 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
179 GTK_POLICY_AUTOMATIC,
180 GTK_POLICY_AUTOMATIC);
181
182 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window,
183 tab_label);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900184
185 perf_gtk__annotate_symbol(scrolled_window, sym, map, evidx, hbt);
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900186 return 0;
187}
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900188
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900189void perf_gtk__show_annotations(void)
190{
191 GtkWidget *window;
192
193 if (!perf_gtk__is_active_context(pgctx))
194 return;
195
196 window = pgctx->main_window;
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900197 gtk_widget_show_all(window);
198
199 perf_gtk__resize_window(window);
200 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
201
202 gtk_main();
203
204 perf_gtk__deactivate_context(&pgctx);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900205}