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" |
Arnaldo Carvalho de Melo | 8a249c7 | 2019-01-22 10:47:38 -0200 | [diff] [blame] | 3 | #include "util/sort.h" |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 4 | #include "util/debug.h" |
| 5 | #include "util/annotate.h" |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 6 | #include "util/evsel.h" |
Arnaldo Carvalho de Melo | 7b644f9 | 2019-01-27 11:55:39 +0100 | [diff] [blame] | 7 | #include "util/map.h" |
Arnaldo Carvalho de Melo | 7cadca8 | 2019-01-27 14:10:31 +0100 | [diff] [blame] | 8 | #include "util/symbol.h" |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 9 | #include "ui/helpline.h" |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 10 | #include <inttypes.h> |
Arnaldo Carvalho de Melo | 9607ad3 | 2017-04-19 15:49:18 -0300 | [diff] [blame] | 11 | #include <signal.h> |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 12 | |
| 13 | enum { |
| 14 | ANN_COL__PERCENT, |
| 15 | ANN_COL__OFFSET, |
| 16 | ANN_COL__LINE, |
| 17 | |
| 18 | MAX_ANN_COLS |
| 19 | }; |
| 20 | |
| 21 | static const char *const col_names[] = { |
| 22 | "Overhead", |
| 23 | "Offset", |
| 24 | "Line" |
| 25 | }; |
| 26 | |
| 27 | static 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 Olsa | d5490b9 | 2017-10-11 17:01:26 +0200 | [diff] [blame] | 37 | if (dl->al.offset == (s64) -1) |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 38 | return 0; |
| 39 | |
| 40 | symhist = annotation__histogram(symbol__annotation(sym), evidx); |
Jiri Olsa | d5490b9 | 2017-10-11 17:01:26 +0200 | [diff] [blame] | 41 | if (!symbol_conf.event_group && !symhist->addr[dl->al.offset].nr_samples) |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 42 | return 0; |
| 43 | |
Jiri Olsa | d5490b9 | 2017-10-11 17:01:26 +0200 | [diff] [blame] | 44 | percent = 100.0 * symhist->addr[dl->al.offset].nr_samples / symhist->nr_samples; |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 45 | |
| 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 | |
| 56 | static 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 Olsa | d5490b9 | 2017-10-11 17:01:26 +0200 | [diff] [blame] | 63 | if (dl->al.offset == (s64) -1) |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 64 | return 0; |
| 65 | |
Jiri Olsa | d5490b9 | 2017-10-11 17:01:26 +0200 | [diff] [blame] | 66 | return scnprintf(buf, size, "%"PRIx64, start + dl->al.offset); |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 67 | } |
| 68 | |
Namhyung Kim | 2375223 | 2013-02-07 18:02:10 +0900 | [diff] [blame] | 69 | static int perf_gtk__get_line(char *buf, size_t size, struct disasm_line *dl) |
| 70 | { |
| 71 | int ret = 0; |
Jiri Olsa | d5490b9 | 2017-10-11 17:01:26 +0200 | [diff] [blame] | 72 | char *line = g_markup_escape_text(dl->al.line, -1); |
Namhyung Kim | 2375223 | 2013-02-07 18:02:10 +0900 | [diff] [blame] | 73 | const char *markup = "<span fgcolor='gray'>"; |
| 74 | |
| 75 | strcpy(buf, ""); |
| 76 | |
| 77 | if (!line) |
| 78 | return 0; |
| 79 | |
Jiri Olsa | d5490b9 | 2017-10-11 17:01:26 +0200 | [diff] [blame] | 80 | if (dl->al.offset != (s64) -1) |
Namhyung Kim | 2375223 | 2013-02-07 18:02:10 +0900 | [diff] [blame] | 81 | 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 Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 93 | static int perf_gtk__annotate_symbol(GtkWidget *window, struct symbol *sym, |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 94 | struct map *map, struct perf_evsel *evsel, |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 95 | 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 Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 106 | 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 Kim | 2375223 | 2013-02-07 18:02:10 +0900 | [diff] [blame] | 118 | -1, col_names[i], renderer, "markup", |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 119 | 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 Olsa | a17c4ca | 2017-10-11 17:01:25 +0200 | [diff] [blame] | 125 | list_for_each_entry(pos, ¬es->src->source, al.node) { |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 126 | GtkTreeIter iter; |
Namhyung Kim | d8d7cd9 | 2013-03-05 14:53:32 +0900 | [diff] [blame] | 127 | int ret = 0; |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 128 | |
| 129 | gtk_list_store_append(store, &iter); |
| 130 | |
Namhyung Kim | d8d7cd9 | 2013-03-05 14:53:32 +0900 | [diff] [blame] | 131 | 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 Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 145 | 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 Kim | 2375223 | 2013-02-07 18:02:10 +0900 | [diff] [blame] | 148 | if (perf_gtk__get_line(s, sizeof(s), pos)) |
| 149 | gtk_list_store_set(store, &iter, ANN_COL__LINE, s, -1); |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | gtk_container_add(GTK_CONTAINER(window), view); |
| 153 | |
Jiri Olsa | a17c4ca | 2017-10-11 17:01:25 +0200 | [diff] [blame] | 154 | list_for_each_entry_safe(pos, n, ¬es->src->source, al.node) { |
Arnaldo Carvalho de Melo | e56fbc9 | 2019-07-04 12:13:46 -0300 | [diff] [blame] | 155 | list_del_init(&pos->al.node); |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 156 | disasm_line__free(pos); |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
Namhyung Kim | fc67297 | 2013-09-13 15:27:43 +0900 | [diff] [blame] | 162 | static int symbol__gtk_annotate(struct symbol *sym, struct map *map, |
| 163 | struct perf_evsel *evsel, |
| 164 | struct hist_browser_timer *hbt) |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 165 | { |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 166 | GtkWidget *window; |
Namhyung Kim | 7a60ba9 | 2013-02-07 18:02:09 +0900 | [diff] [blame] | 167 | GtkWidget *notebook; |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 168 | GtkWidget *scrolled_window; |
| 169 | GtkWidget *tab_label; |
Arnaldo Carvalho de Melo | ee51d85 | 2016-07-29 16:27:18 -0300 | [diff] [blame] | 170 | int err; |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 171 | |
Namhyung Kim | c0e79be | 2013-02-07 18:02:13 +0900 | [diff] [blame] | 172 | if (map->dso->annotate_warned) |
| 173 | return -1; |
| 174 | |
Arnaldo Carvalho de Melo | 380195e | 2018-05-28 11:27:40 -0300 | [diff] [blame] | 175 | err = symbol__annotate(sym, map, evsel, 0, &annotation__default_options, NULL); |
Arnaldo Carvalho de Melo | ee51d85 | 2016-07-29 16:27:18 -0300 | [diff] [blame] | 176 | 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 Kim | c0e79be | 2013-02-07 18:02:13 +0900 | [diff] [blame] | 180 | return -1; |
| 181 | } |
| 182 | |
Jiri Olsa | 05d3f1a | 2017-11-15 12:20:08 +0100 | [diff] [blame] | 183 | symbol__calc_percent(sym, evsel); |
| 184 | |
Namhyung Kim | 7a60ba9 | 2013-02-07 18:02:09 +0900 | [diff] [blame] | 185 | 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 Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 192 | |
Namhyung Kim | 7a60ba9 | 2013-02-07 18:02:09 +0900 | [diff] [blame] | 193 | 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 Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 198 | |
Namhyung Kim | 7a60ba9 | 2013-02-07 18:02:09 +0900 | [diff] [blame] | 199 | window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 200 | gtk_window_set_title(GTK_WINDOW(window), "perf annotate"); |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 201 | |
Namhyung Kim | 7a60ba9 | 2013-02-07 18:02:09 +0900 | [diff] [blame] | 202 | g_signal_connect(window, "delete_event", gtk_main_quit, NULL); |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 203 | |
Namhyung Kim | 7a60ba9 | 2013-02-07 18:02:09 +0900 | [diff] [blame] | 204 | 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 Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 226 | 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 Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 235 | |
Namhyung Kim | db8fd07 | 2013-03-05 14:53:21 +0900 | [diff] [blame] | 236 | perf_gtk__annotate_symbol(scrolled_window, sym, map, evsel, hbt); |
Namhyung Kim | 7a60ba9 | 2013-02-07 18:02:09 +0900 | [diff] [blame] | 237 | return 0; |
| 238 | } |
Namhyung Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 239 | |
Namhyung Kim | fc67297 | 2013-09-13 15:27:43 +0900 | [diff] [blame] | 240 | int 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 Kim | 7a60ba9 | 2013-02-07 18:02:09 +0900 | [diff] [blame] | 247 | void 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 Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 255 | 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 Kim | 2b676bf | 2013-02-07 18:02:08 +0900 | [diff] [blame] | 263 | } |