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