annotate.c 6.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
#include "gtk.h"
3
#include "util/sort.h"
4 5
#include "util/debug.h"
#include "util/annotate.h"
6
#include "util/evsel.h"
7
#include "util/map.h"
8
#include "util/dso.h"
9
#include "util/symbol.h"
10
#include "ui/helpline.h"
11
#include <inttypes.h>
12
#include <signal.h>
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

enum {
	ANN_COL__PERCENT,
	ANN_COL__OFFSET,
	ANN_COL__LINE,

	MAX_ANN_COLS
};

static const char *const col_names[] = {
	"Overhead",
	"Offset",
	"Line"
};

static int perf_gtk__get_percent(char *buf, size_t size, struct symbol *sym,
				 struct disasm_line *dl, int evidx)
{
	struct sym_hist *symhist;
	double percent = 0.0;
	const char *markup;
	int ret = 0;

	strcpy(buf, "");

38
	if (dl->al.offset == (s64) -1)
39 40 41
		return 0;

	symhist = annotation__histogram(symbol__annotation(sym), evidx);
42
	if (!symbol_conf.event_group && !symhist->addr[dl->al.offset].nr_samples)
43 44
		return 0;

45
	percent = 100.0 * symhist->addr[dl->al.offset].nr_samples / symhist->nr_samples;
46 47 48 49 50 51 52 53 54 55 56

	markup = perf_gtk__get_percent_color(percent);
	if (markup)
		ret += scnprintf(buf, size, "%s", markup);
	ret += scnprintf(buf + ret, size - ret, "%6.2f%%", percent);
	if (markup)
		ret += scnprintf(buf + ret, size - ret, "</span>");

	return ret;
}

57 58
static int perf_gtk__get_offset(char *buf, size_t size, struct map_symbol *ms,
				struct disasm_line *dl)
59
{
60
	u64 start = map__rip_2objdump(ms->map, ms->sym->start);
61 62 63

	strcpy(buf, "");

64
	if (dl->al.offset == (s64) -1)
65 66
		return 0;

67
	return scnprintf(buf, size, "%"PRIx64, start + dl->al.offset);
68 69
}

70 71 72
static int perf_gtk__get_line(char *buf, size_t size, struct disasm_line *dl)
{
	int ret = 0;
73
	char *line = g_markup_escape_text(dl->al.line, -1);
74 75 76 77 78 79 80
	const char *markup = "<span fgcolor='gray'>";

	strcpy(buf, "");

	if (!line)
		return 0;

81
	if (dl->al.offset != (s64) -1)
82 83 84 85 86 87 88 89 90 91 92 93
		markup = NULL;

	if (markup)
		ret += scnprintf(buf, size, "%s", markup);
	ret += scnprintf(buf + ret, size - ret, "%s", line);
	if (markup)
		ret += scnprintf(buf + ret, size - ret, "</span>");

	g_free(line);
	return ret;
}

94 95
static int perf_gtk__annotate_symbol(GtkWidget *window, struct map_symbol *ms,
				struct evsel *evsel,
96 97
				struct hist_browser_timer *hbt __maybe_unused)
{
98
	struct symbol *sym = ms->sym;
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	struct disasm_line *pos, *n;
	struct annotation *notes;
	GType col_types[MAX_ANN_COLS];
	GtkCellRenderer *renderer;
	GtkListStore *store;
	GtkWidget *view;
	int i;
	char s[512];

	notes = symbol__annotation(sym);

	for (i = 0; i < MAX_ANN_COLS; i++) {
		col_types[i] = G_TYPE_STRING;
	}
	store = gtk_list_store_newv(MAX_ANN_COLS, col_types);

	view = gtk_tree_view_new();
	renderer = gtk_cell_renderer_text_new();

	for (i = 0; i < MAX_ANN_COLS; i++) {
		gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(view),
120
					-1, col_names[i], renderer, "markup",
121 122 123 124 125 126
					i, NULL);
	}

	gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
	g_object_unref(GTK_TREE_MODEL(store));

127
	list_for_each_entry(pos, &notes->src->source, al.node) {
128
		GtkTreeIter iter;
129
		int ret = 0;
130 131 132

		gtk_list_store_append(store, &iter);

133
		if (perf_evsel__is_group_event(evsel)) {
134
			for (i = 0; i < evsel->core.nr_members; i++) {
135 136 137 138 139 140 141 142 143 144 145 146
				ret += perf_gtk__get_percent(s + ret,
							     sizeof(s) - ret,
							     sym, pos,
							     evsel->idx + i);
				ret += scnprintf(s + ret, sizeof(s) - ret, " ");
			}
		} else {
			ret = perf_gtk__get_percent(s, sizeof(s), sym, pos,
						    evsel->idx);
		}

		if (ret)
147
			gtk_list_store_set(store, &iter, ANN_COL__PERCENT, s, -1);
148
		if (perf_gtk__get_offset(s, sizeof(s), ms, pos))
149
			gtk_list_store_set(store, &iter, ANN_COL__OFFSET, s, -1);
150 151
		if (perf_gtk__get_line(s, sizeof(s), pos))
			gtk_list_store_set(store, &iter, ANN_COL__LINE, s, -1);
152 153 154 155
	}

	gtk_container_add(GTK_CONTAINER(window), view);

156
	list_for_each_entry_safe(pos, n, &notes->src->source, al.node) {
157
		list_del_init(&pos->al.node);
158 159 160 161 162 163
		disasm_line__free(pos);
	}

	return 0;
}

164
static int symbol__gtk_annotate(struct map_symbol *ms, struct evsel *evsel,
165
				struct hist_browser_timer *hbt)
166
{
167
	struct symbol *sym = ms->sym;
168
	GtkWidget *window;
169
	GtkWidget *notebook;
170 171
	GtkWidget *scrolled_window;
	GtkWidget *tab_label;
172
	int err;
173

174
	if (ms->map->dso->annotate_warned)
175 176
		return -1;

177
	err = symbol__annotate(ms, evsel, &annotation__default_options, NULL);
178 179
	if (err) {
		char msg[BUFSIZ];
180
		symbol__strerror_disassemble(ms, err, msg, sizeof(msg));
181
		ui__error("Couldn't annotate %s: %s\n", sym->name, msg);
182 183 184
		return -1;
	}

185 186
	symbol__calc_percent(sym, evsel);

187 188 189 190 191 192 193
	if (perf_gtk__is_active_context(pgctx)) {
		window = pgctx->main_window;
		notebook = pgctx->notebook;
	} else {
		GtkWidget *vbox;
		GtkWidget *infobar;
		GtkWidget *statbar;
194

195 196 197 198 199
		signal(SIGSEGV, perf_gtk__signal);
		signal(SIGFPE,  perf_gtk__signal);
		signal(SIGINT,  perf_gtk__signal);
		signal(SIGQUIT, perf_gtk__signal);
		signal(SIGTERM, perf_gtk__signal);
200

201 202
		window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
		gtk_window_set_title(GTK_WINDOW(window), "perf annotate");
203

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
		g_signal_connect(window, "delete_event", gtk_main_quit, NULL);

		pgctx = perf_gtk__activate_context(window);
		if (!pgctx)
			return -1;

		vbox = gtk_vbox_new(FALSE, 0);
		notebook = gtk_notebook_new();
		pgctx->notebook = notebook;

		gtk_box_pack_start(GTK_BOX(vbox), notebook, TRUE, TRUE, 0);

		infobar = perf_gtk__setup_info_bar();
		if (infobar) {
			gtk_box_pack_start(GTK_BOX(vbox), infobar,
					   FALSE, FALSE, 0);
		}

		statbar = perf_gtk__setup_statusbar();
		gtk_box_pack_start(GTK_BOX(vbox), statbar, FALSE, FALSE, 0);

		gtk_container_add(GTK_CONTAINER(window), vbox);
	}
227 228 229 230 231 232 233 234 235 236 237

	scrolled_window = gtk_scrolled_window_new(NULL, NULL);
	tab_label = gtk_label_new(sym->name);

	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
				       GTK_POLICY_AUTOMATIC,
				       GTK_POLICY_AUTOMATIC);

	gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scrolled_window,
				 tab_label);

238
	perf_gtk__annotate_symbol(scrolled_window, ms, evsel, hbt);
239 240
	return 0;
}
241

242
int hist_entry__gtk_annotate(struct hist_entry *he,
243
			     struct evsel *evsel,
244 245
			     struct hist_browser_timer *hbt)
{
246
	return symbol__gtk_annotate(&he->ms, evsel, hbt);
247 248
}

249 250 251
void perf_gtk__show_annotations(void)
{
	GtkWidget *window;
252

253 254
	if (!perf_gtk__is_active_context(pgctx))
		return;
255

256
	window = pgctx->main_window;
257 258 259 260 261 262 263 264 265
	gtk_widget_show_all(window);

	perf_gtk__resize_window(window);
	gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

	gtk_main();

	perf_gtk__deactivate_context(&pgctx);
}