1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
22b676bf0SNamhyung Kim #include "gtk.h"
38a249c73SArnaldo Carvalho de Melo #include "util/sort.h"
42b676bf0SNamhyung Kim #include "util/debug.h"
52b676bf0SNamhyung Kim #include "util/annotate.h"
6db8fd07aSNamhyung Kim #include "util/evsel.h"
77b644f9aSArnaldo Carvalho de Melo #include "util/map.h"
8b1d1b094SArnaldo Carvalho de Melo #include "util/dso.h"
97cadca8eSArnaldo Carvalho de Melo #include "util/symbol.h"
102b676bf0SNamhyung Kim #include "ui/helpline.h"
11fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
129607ad3aSArnaldo Carvalho de Melo #include <signal.h>
132b676bf0SNamhyung Kim
142b676bf0SNamhyung Kim enum {
152b676bf0SNamhyung Kim ANN_COL__PERCENT,
162b676bf0SNamhyung Kim ANN_COL__OFFSET,
172b676bf0SNamhyung Kim ANN_COL__LINE,
182b676bf0SNamhyung Kim
192b676bf0SNamhyung Kim MAX_ANN_COLS
202b676bf0SNamhyung Kim };
212b676bf0SNamhyung Kim
222b676bf0SNamhyung Kim static const char *const col_names[] = {
232b676bf0SNamhyung Kim "Overhead",
242b676bf0SNamhyung Kim "Offset",
252b676bf0SNamhyung Kim "Line"
262b676bf0SNamhyung Kim };
272b676bf0SNamhyung Kim
perf_gtk__get_percent(char * buf,size_t size,struct symbol * sym,struct disasm_line * dl,int evidx)282b676bf0SNamhyung Kim static int perf_gtk__get_percent(char *buf, size_t size, struct symbol *sym,
292b676bf0SNamhyung Kim struct disasm_line *dl, int evidx)
302b676bf0SNamhyung Kim {
312b676bf0SNamhyung Kim struct sym_hist *symhist;
322b676bf0SNamhyung Kim double percent = 0.0;
332b676bf0SNamhyung Kim const char *markup;
342b676bf0SNamhyung Kim int ret = 0;
352b676bf0SNamhyung Kim
362b676bf0SNamhyung Kim strcpy(buf, "");
372b676bf0SNamhyung Kim
38d5490b96SJiri Olsa if (dl->al.offset == (s64) -1)
392b676bf0SNamhyung Kim return 0;
402b676bf0SNamhyung Kim
412b676bf0SNamhyung Kim symhist = annotation__histogram(symbol__annotation(sym), evidx);
42d5490b96SJiri Olsa if (!symbol_conf.event_group && !symhist->addr[dl->al.offset].nr_samples)
432b676bf0SNamhyung Kim return 0;
442b676bf0SNamhyung Kim
45d5490b96SJiri Olsa percent = 100.0 * symhist->addr[dl->al.offset].nr_samples / symhist->nr_samples;
462b676bf0SNamhyung Kim
472b676bf0SNamhyung Kim markup = perf_gtk__get_percent_color(percent);
482b676bf0SNamhyung Kim if (markup)
492b676bf0SNamhyung Kim ret += scnprintf(buf, size, "%s", markup);
502b676bf0SNamhyung Kim ret += scnprintf(buf + ret, size - ret, "%6.2f%%", percent);
512b676bf0SNamhyung Kim if (markup)
522b676bf0SNamhyung Kim ret += scnprintf(buf + ret, size - ret, "</span>");
532b676bf0SNamhyung Kim
542b676bf0SNamhyung Kim return ret;
552b676bf0SNamhyung Kim }
562b676bf0SNamhyung Kim
perf_gtk__get_offset(char * buf,size_t size,struct map_symbol * ms,struct disasm_line * dl)5729754894SArnaldo Carvalho de Melo static int perf_gtk__get_offset(char *buf, size_t size, struct map_symbol *ms,
5829754894SArnaldo Carvalho de Melo struct disasm_line *dl)
592b676bf0SNamhyung Kim {
6029754894SArnaldo Carvalho de Melo u64 start = map__rip_2objdump(ms->map, ms->sym->start);
612b676bf0SNamhyung Kim
622b676bf0SNamhyung Kim strcpy(buf, "");
632b676bf0SNamhyung Kim
64d5490b96SJiri Olsa if (dl->al.offset == (s64) -1)
652b676bf0SNamhyung Kim return 0;
662b676bf0SNamhyung Kim
67d5490b96SJiri Olsa return scnprintf(buf, size, "%"PRIx64, start + dl->al.offset);
682b676bf0SNamhyung Kim }
692b676bf0SNamhyung Kim
perf_gtk__get_line(char * buf,size_t size,struct disasm_line * dl)7023752237SNamhyung Kim static int perf_gtk__get_line(char *buf, size_t size, struct disasm_line *dl)
7123752237SNamhyung Kim {
7223752237SNamhyung Kim int ret = 0;
73d5490b96SJiri Olsa char *line = g_markup_escape_text(dl->al.line, -1);
7423752237SNamhyung Kim const char *markup = "<span fgcolor='gray'>";
7523752237SNamhyung Kim
7623752237SNamhyung Kim strcpy(buf, "");
7723752237SNamhyung Kim
7823752237SNamhyung Kim if (!line)
7923752237SNamhyung Kim return 0;
8023752237SNamhyung Kim
81d5490b96SJiri Olsa if (dl->al.offset != (s64) -1)
8223752237SNamhyung Kim markup = NULL;
8323752237SNamhyung Kim
8423752237SNamhyung Kim if (markup)
8523752237SNamhyung Kim ret += scnprintf(buf, size, "%s", markup);
8623752237SNamhyung Kim ret += scnprintf(buf + ret, size - ret, "%s", line);
8723752237SNamhyung Kim if (markup)
8823752237SNamhyung Kim ret += scnprintf(buf + ret, size - ret, "</span>");
8923752237SNamhyung Kim
9023752237SNamhyung Kim g_free(line);
9123752237SNamhyung Kim return ret;
9223752237SNamhyung Kim }
9323752237SNamhyung Kim
perf_gtk__annotate_symbol(GtkWidget * window,struct map_symbol * ms,struct evsel * evsel,struct hist_browser_timer * hbt __maybe_unused)9429754894SArnaldo Carvalho de Melo static int perf_gtk__annotate_symbol(GtkWidget *window, struct map_symbol *ms,
9529754894SArnaldo Carvalho de Melo struct evsel *evsel,
962b676bf0SNamhyung Kim struct hist_browser_timer *hbt __maybe_unused)
972b676bf0SNamhyung Kim {
9829754894SArnaldo Carvalho de Melo struct symbol *sym = ms->sym;
992b676bf0SNamhyung Kim struct disasm_line *pos, *n;
1002b676bf0SNamhyung Kim struct annotation *notes;
1012b676bf0SNamhyung Kim GType col_types[MAX_ANN_COLS];
1022b676bf0SNamhyung Kim GtkCellRenderer *renderer;
1032b676bf0SNamhyung Kim GtkListStore *store;
1042b676bf0SNamhyung Kim GtkWidget *view;
1052b676bf0SNamhyung Kim int i;
1062b676bf0SNamhyung Kim char s[512];
1072b676bf0SNamhyung Kim
1082b676bf0SNamhyung Kim notes = symbol__annotation(sym);
1092b676bf0SNamhyung Kim
1102b676bf0SNamhyung Kim for (i = 0; i < MAX_ANN_COLS; i++) {
1112b676bf0SNamhyung Kim col_types[i] = G_TYPE_STRING;
1122b676bf0SNamhyung Kim }
1132b676bf0SNamhyung Kim store = gtk_list_store_newv(MAX_ANN_COLS, col_types);
1142b676bf0SNamhyung Kim
1152b676bf0SNamhyung Kim view = gtk_tree_view_new();
1162b676bf0SNamhyung Kim renderer = gtk_cell_renderer_text_new();
1172b676bf0SNamhyung Kim
1182b676bf0SNamhyung Kim for (i = 0; i < MAX_ANN_COLS; i++) {
1192b676bf0SNamhyung Kim gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
12023752237SNamhyung Kim -1, col_names[i], renderer, "markup",
1212b676bf0SNamhyung Kim i, NULL);
1222b676bf0SNamhyung Kim }
1232b676bf0SNamhyung Kim
1242b676bf0SNamhyung Kim gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
1252b676bf0SNamhyung Kim g_object_unref(GTK_TREE_MODEL(store));
1262b676bf0SNamhyung Kim
127a17c4ca0SJiri Olsa list_for_each_entry(pos, ¬es->src->source, al.node) {
1282b676bf0SNamhyung Kim GtkTreeIter iter;
129d8d7cd93SNamhyung Kim int ret = 0;
1302b676bf0SNamhyung Kim
1312b676bf0SNamhyung Kim gtk_list_store_append(store, &iter);
1322b676bf0SNamhyung Kim
133c754c382SArnaldo Carvalho de Melo if (evsel__is_group_event(evsel)) {
1345643b1a5SJiri Olsa for (i = 0; i < evsel->core.nr_members; i++) {
135d8d7cd93SNamhyung Kim ret += perf_gtk__get_percent(s + ret,
136d8d7cd93SNamhyung Kim sizeof(s) - ret,
137d8d7cd93SNamhyung Kim sym, pos,
13838fe0e01SJiri Olsa evsel->core.idx + i);
139d8d7cd93SNamhyung Kim ret += scnprintf(s + ret, sizeof(s) - ret, " ");
140d8d7cd93SNamhyung Kim }
141d8d7cd93SNamhyung Kim } else {
142d8d7cd93SNamhyung Kim ret = perf_gtk__get_percent(s, sizeof(s), sym, pos,
14338fe0e01SJiri Olsa evsel->core.idx);
144d8d7cd93SNamhyung Kim }
145d8d7cd93SNamhyung Kim
146d8d7cd93SNamhyung Kim if (ret)
1472b676bf0SNamhyung Kim gtk_list_store_set(store, &iter, ANN_COL__PERCENT, s, -1);
14829754894SArnaldo Carvalho de Melo if (perf_gtk__get_offset(s, sizeof(s), ms, pos))
1492b676bf0SNamhyung Kim gtk_list_store_set(store, &iter, ANN_COL__OFFSET, s, -1);
15023752237SNamhyung Kim if (perf_gtk__get_line(s, sizeof(s), pos))
15123752237SNamhyung Kim gtk_list_store_set(store, &iter, ANN_COL__LINE, s, -1);
1522b676bf0SNamhyung Kim }
1532b676bf0SNamhyung Kim
1542b676bf0SNamhyung Kim gtk_container_add(GTK_CONTAINER(window), view);
1552b676bf0SNamhyung Kim
156a17c4ca0SJiri Olsa list_for_each_entry_safe(pos, n, ¬es->src->source, al.node) {
157e56fbc9dSArnaldo Carvalho de Melo list_del_init(&pos->al.node);
1582b676bf0SNamhyung Kim disasm_line__free(pos);
1592b676bf0SNamhyung Kim }
1602b676bf0SNamhyung Kim
1612b676bf0SNamhyung Kim return 0;
1622b676bf0SNamhyung Kim }
1632b676bf0SNamhyung Kim
symbol__gtk_annotate(struct map_symbol * ms,struct evsel * evsel,struct hist_browser_timer * hbt)16429754894SArnaldo Carvalho de Melo static int symbol__gtk_annotate(struct map_symbol *ms, struct evsel *evsel,
1652b676bf0SNamhyung Kim struct hist_browser_timer *hbt)
1662b676bf0SNamhyung Kim {
16763df0e4bSIan Rogers struct dso *dso = map__dso(ms->map);
16829754894SArnaldo Carvalho de Melo struct symbol *sym = ms->sym;
1692b676bf0SNamhyung Kim GtkWidget *window;
1707a60ba94SNamhyung Kim GtkWidget *notebook;
1712b676bf0SNamhyung Kim GtkWidget *scrolled_window;
1722b676bf0SNamhyung Kim GtkWidget *tab_label;
173ee51d851SArnaldo Carvalho de Melo int err;
1742b676bf0SNamhyung Kim
17563df0e4bSIan Rogers if (dso->annotate_warned)
176c0e79be7SNamhyung Kim return -1;
177c0e79be7SNamhyung Kim
178*80e06e54SNamhyung Kim err = symbol__annotate(ms, evsel, NULL);
179ee51d851SArnaldo Carvalho de Melo if (err) {
180ee51d851SArnaldo Carvalho de Melo char msg[BUFSIZ];
18163df0e4bSIan Rogers dso->annotate_warned = true;
18229754894SArnaldo Carvalho de Melo symbol__strerror_disassemble(ms, err, msg, sizeof(msg));
183ee51d851SArnaldo Carvalho de Melo ui__error("Couldn't annotate %s: %s\n", sym->name, msg);
184c0e79be7SNamhyung Kim return -1;
185c0e79be7SNamhyung Kim }
186c0e79be7SNamhyung Kim
18705d3f1a1SJiri Olsa symbol__calc_percent(sym, evsel);
18805d3f1a1SJiri Olsa
1897a60ba94SNamhyung Kim if (perf_gtk__is_active_context(pgctx)) {
1907a60ba94SNamhyung Kim window = pgctx->main_window;
1917a60ba94SNamhyung Kim notebook = pgctx->notebook;
1927a60ba94SNamhyung Kim } else {
1937a60ba94SNamhyung Kim GtkWidget *vbox;
1947a60ba94SNamhyung Kim GtkWidget *infobar;
1957a60ba94SNamhyung Kim GtkWidget *statbar;
1967a60ba94SNamhyung Kim
1972b676bf0SNamhyung Kim signal(SIGSEGV, perf_gtk__signal);
1982b676bf0SNamhyung Kim signal(SIGFPE, perf_gtk__signal);
1992b676bf0SNamhyung Kim signal(SIGINT, perf_gtk__signal);
2002b676bf0SNamhyung Kim signal(SIGQUIT, perf_gtk__signal);
2012b676bf0SNamhyung Kim signal(SIGTERM, perf_gtk__signal);
2022b676bf0SNamhyung Kim
2032b676bf0SNamhyung Kim window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2042b676bf0SNamhyung Kim gtk_window_set_title(GTK_WINDOW(window), "perf annotate");
2052b676bf0SNamhyung Kim
2062b676bf0SNamhyung Kim g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
2072b676bf0SNamhyung Kim
2082b676bf0SNamhyung Kim pgctx = perf_gtk__activate_context(window);
2092b676bf0SNamhyung Kim if (!pgctx)
2102b676bf0SNamhyung Kim return -1;
2112b676bf0SNamhyung Kim
2122b676bf0SNamhyung Kim vbox = gtk_vbox_new(FALSE, 0);
2132b676bf0SNamhyung Kim notebook = gtk_notebook_new();
2147a60ba94SNamhyung Kim pgctx->notebook = notebook;
2157a60ba94SNamhyung Kim
2167a60ba94SNamhyung Kim gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);
2177a60ba94SNamhyung Kim
2187a60ba94SNamhyung Kim infobar = perf_gtk__setup_info_bar();
2197a60ba94SNamhyung Kim if (infobar) {
2207a60ba94SNamhyung Kim gtk_box_pack_start(GTK_BOX(vbox), infobar,
2217a60ba94SNamhyung Kim FALSE, FALSE, 0);
2227a60ba94SNamhyung Kim }
2237a60ba94SNamhyung Kim
2247a60ba94SNamhyung Kim statbar = perf_gtk__setup_statusbar();
2257a60ba94SNamhyung Kim gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0);
2267a60ba94SNamhyung Kim
2277a60ba94SNamhyung Kim gtk_container_add(GTK_CONTAINER(window), vbox);
2287a60ba94SNamhyung Kim }
2297a60ba94SNamhyung Kim
2302b676bf0SNamhyung Kim scrolled_window = gtk_scrolled_window_new(NULL, NULL);
2312b676bf0SNamhyung Kim tab_label = gtk_label_new(sym->name);
2322b676bf0SNamhyung Kim
2332b676bf0SNamhyung Kim gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
2342b676bf0SNamhyung Kim GTK_POLICY_AUTOMATIC,
2352b676bf0SNamhyung Kim GTK_POLICY_AUTOMATIC);
2362b676bf0SNamhyung Kim
2372b676bf0SNamhyung Kim gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window,
2382b676bf0SNamhyung Kim tab_label);
2392b676bf0SNamhyung Kim
24029754894SArnaldo Carvalho de Melo perf_gtk__annotate_symbol(scrolled_window, ms, evsel, hbt);
2417a60ba94SNamhyung Kim return 0;
2427a60ba94SNamhyung Kim }
2432b676bf0SNamhyung Kim
hist_entry__gtk_annotate(struct hist_entry * he,struct evsel * evsel,struct hist_browser_timer * hbt)244fc67297bSNamhyung Kim int hist_entry__gtk_annotate(struct hist_entry *he,
24532dcd021SJiri Olsa struct evsel *evsel,
246fc67297bSNamhyung Kim struct hist_browser_timer *hbt)
247fc67297bSNamhyung Kim {
248*80e06e54SNamhyung Kim return symbol__gtk_annotate(&he->ms, evsel, hbt);
249fc67297bSNamhyung Kim }
250fc67297bSNamhyung Kim
perf_gtk__show_annotations(void)2517a60ba94SNamhyung Kim void perf_gtk__show_annotations(void)
2527a60ba94SNamhyung Kim {
2537a60ba94SNamhyung Kim GtkWidget *window;
2547a60ba94SNamhyung Kim
2557a60ba94SNamhyung Kim if (!perf_gtk__is_active_context(pgctx))
2567a60ba94SNamhyung Kim return;
2577a60ba94SNamhyung Kim
2587a60ba94SNamhyung Kim window = pgctx->main_window;
2592b676bf0SNamhyung Kim gtk_widget_show_all(window);
2602b676bf0SNamhyung Kim
2612b676bf0SNamhyung Kim perf_gtk__resize_window(window);
2622b676bf0SNamhyung Kim gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
2632b676bf0SNamhyung Kim
2642b676bf0SNamhyung Kim gtk_main();
2652b676bf0SNamhyung Kim
2662b676bf0SNamhyung Kim perf_gtk__deactivate_context(&pgctx);
2672b676bf0SNamhyung Kim }
268