annotate.c 9.1 KB
Newer Older
1 2 3
#include "../browser.h"
#include "../helpline.h"
#include "../libslang.h"
4
#include "../../annotate.h"
5 6 7
#include "../../hist.h"
#include "../../sort.h"
#include "../../symbol.h"
8
#include <pthread.h>
9 10 11 12 13 14 15 16 17 18

static void ui__error_window(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
	va_end(ap);
}

19 20 21
struct annotate_browser {
	struct ui_browser b;
	struct rb_root	  entries;
22
	struct rb_node	  *curr_hot;
23
	struct objdump_line *selection;
24 25 26 27 28 29 30 31 32 33 34 35 36 37
};

struct objdump_line_rb_node {
	struct rb_node	rb_node;
	double		percent;
	u32		idx;
};

static inline
struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
{
	return (struct objdump_line_rb_node *)(self + 1);
}

38 39
static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
{
40
	struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
41 42 43 44 45
	struct objdump_line *ol = rb_entry(entry, struct objdump_line, node);
	bool current_entry = ui_browser__is_current_entry(self, row);
	int width = self->width;

	if (ol->offset != -1) {
46
		struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
47
		ui_browser__set_percent_color(self, olrb->percent, current_entry);
48 49
		slsmg_printf(" %7.2f ", olrb->percent);
	} else {
50
		ui_browser__set_percent_color(self, 0, current_entry);
51 52 53 54 55 56 57 58 59
		slsmg_write_nstring(" ", 9);
	}

	SLsmg_write_char(':');
	slsmg_write_nstring(" ", 8);
	if (!*ol->line)
		slsmg_write_nstring(" ", width - 18);
	else
		slsmg_write_nstring(ol->line, width - 18);
60 61 62

	if (!current_entry)
		ui_browser__set_color(self, HE_COLORSET_CODE);
63 64
	else
		ab->selection = ol;
65 66 67
}

static double objdump_line__calc_percent(struct objdump_line *self,
68
					 struct symbol *sym, int evidx)
69 70 71 72 73
{
	double percent = 0.0;

	if (self->offset != -1) {
		int len = sym->end - sym->start;
74
		unsigned int hits = 0;
75
		struct annotation *notes = symbol__annotation(sym);
76
		struct source_line *src_line = notes->src->lines;
77
		struct sym_hist *h = annotation__histogram(notes, evidx);
78
		s64 offset = self->offset;
79
		struct objdump_line *next;
80

81
		next = objdump__get_next_ip_line(&notes->src->source, self);
82 83
		while (offset < (s64)len &&
		       (next == NULL || offset < next->offset)) {
84 85
			if (src_line) {
				percent += src_line[offset].percent;
86
			} else
87
				hits += h->addr[offset];
88 89 90

			++offset;
		}
91 92 93 94 95
		/*
 		 * If the percentage wasn't already calculated in
 		 * symbol__get_source_line, do it now:
 		 */
		if (src_line == NULL && h->sum)
96 97 98
			percent = 100.0 * hits / h->sum;
	}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	return percent;
}

static void objdump__insert_line(struct rb_root *self,
				 struct objdump_line_rb_node *line)
{
	struct rb_node **p = &self->rb_node;
	struct rb_node *parent = NULL;
	struct objdump_line_rb_node *l;

	while (*p != NULL) {
		parent = *p;
		l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
		if (line->percent < l->percent)
			p = &(*p)->rb_left;
		else
			p = &(*p)->rb_right;
	}
	rb_link_node(&line->rb_node, parent, p);
	rb_insert_color(&line->rb_node, self);
119 120
}

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
static void annotate_browser__set_top(struct annotate_browser *self,
				      struct rb_node *nd)
{
	struct objdump_line_rb_node *rbpos;
	struct objdump_line *pos;
	unsigned back;

	ui_browser__refresh_dimensions(&self->b);
	back = self->b.height / 2;
	rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
	pos = ((struct objdump_line *)rbpos) - 1;
	self->b.top_idx = self->b.index = rbpos->idx;

	while (self->b.top_idx != 0 && back != 0) {
		pos = list_entry(pos->node.prev, struct objdump_line, node);

		--self->b.top_idx;
		--back;
	}

	self->b.top = pos;
	self->curr_hot = nd;
}

145 146
static void annotate_browser__calc_percent(struct annotate_browser *browser,
					   int evidx)
147
{
148 149
	struct map_symbol *ms = browser->b.priv;
	struct symbol *sym = ms->sym;
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	struct annotation *notes = symbol__annotation(sym);
	struct objdump_line *pos;

	browser->entries = RB_ROOT;

	pthread_mutex_lock(&notes->lock);

	list_for_each_entry(pos, &notes->src->source, node) {
		struct objdump_line_rb_node *rbpos = objdump_line__rb(pos);
		rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
		if (rbpos->percent < 0.01) {
			RB_CLEAR_NODE(&rbpos->rb_node);
			continue;
		}
		objdump__insert_line(&browser->entries, rbpos);
	}
	pthread_mutex_unlock(&notes->lock);

	browser->curr_hot = rb_last(&browser->entries);
}

static int annotate_browser__run(struct annotate_browser *self, int evidx,
172 173
				 int nr_events, void(*timer)(void *arg),
				 void *arg, int delay_secs)
174 175
{
	struct rb_node *nd = NULL;
176 177
	struct map_symbol *ms = self->b.priv;
	struct symbol *sym = ms->sym;
178 179 180 181 182
	/*
	 * RIGHT To allow builtin-annotate to cycle thru multiple symbols by
	 * examining the exit key for this function.
	 */
	int exit_keys[] = { 'H', NEWT_KEY_TAB, NEWT_KEY_UNTAB,
183
			    NEWT_KEY_RIGHT, NEWT_KEY_ENTER, 0 };
184
	int key;
185

186
	if (ui_browser__show(&self->b, sym->name,
187 188
			     "<- or ESC: exit, TAB/shift+TAB: "
			     "cycle hottest lines, H: Hottest, -> Line action") < 0)
189
		return -1;
190 191 192 193 194 195

	ui_browser__add_exit_keys(&self->b, exit_keys);
	annotate_browser__calc_percent(self, evidx);

	if (self->curr_hot)
		annotate_browser__set_top(self, self->curr_hot);
196 197

	nd = self->curr_hot;
198

199 200
	if (delay_secs != 0)
		newtFormSetTimer(self->b.form, delay_secs * 1000);
201 202

	while (1) {
203
		key = ui_browser__run(&self->b);
204

205
		if (delay_secs != 0) {
206 207 208 209 210 211 212 213 214 215
			annotate_browser__calc_percent(self, evidx);
			/*
			 * Current line focus got out of the list of most active
			 * lines, NULL it so that if TAB|UNTAB is pressed, we
			 * move to curr_hot (current hottest line).
			 */
			if (nd != NULL && RB_EMPTY_NODE(nd))
				nd = NULL;
		}

216
		switch (key) {
217 218 219 220 221
		case -1:
			/*
 			 * FIXME we need to check if it was
 			 * es.reason == NEWT_EXIT_TIMER
 			 */
222 223 224 225
			if (timer != NULL)
				timer(arg);

			if (delay_secs != 0)
226 227
				symbol__annotate_decay_histogram(sym, evidx);
			continue;
228
		case NEWT_KEY_TAB:
229 230 231 232 233 234
			if (nd != NULL) {
				nd = rb_prev(nd);
				if (nd == NULL)
					nd = rb_last(&self->entries);
			} else
				nd = self->curr_hot;
235 236
			break;
		case NEWT_KEY_UNTAB:
237 238 239 240 241 242 243 244 245
			if (nd != NULL)
				nd = rb_next(nd);
				if (nd == NULL)
					nd = rb_first(&self->entries);
			else
				nd = self->curr_hot;
			break;
		case 'H':
			nd = self->curr_hot;
246
			break;
247
		case NEWT_KEY_ENTER:
248 249 250 251 252 253 254 255 256 257
		case NEWT_KEY_RIGHT:
			if (self->selection == NULL) {
				ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
				continue;
			}

			if (self->selection->offset == -1) {
				ui_helpline__puts("Actions are only available for assembly lines.");
				continue;
			} else {
258 259 260 261 262
				char *s = strstr(self->selection->line, "callq ");
				struct annotation *notes;
				struct symbol *target;
				u64 ip;

263 264
				if (s == NULL) {
					ui_helpline__puts("Actions are only available for the 'callq' instruction.");
265
					continue;
266
				}
267 268

				s = strchr(s, ' ');
269 270
				if (s++ == NULL) {
					ui_helpline__puts("Invallid callq instruction.");
271
					continue;
272
				}
273 274 275 276

				ip = strtoull(s, NULL, 16);
				ip = ms->map->map_ip(ms->map, ip);
				target = map__find_symbol(ms->map, ip, NULL);
277 278
				if (target == NULL) {
					ui_helpline__puts("The called function was not found.");
279
					continue;
280
				}
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

				notes = symbol__annotation(target);
				pthread_mutex_lock(&notes->lock);

				if (notes->src == NULL &&
				    symbol__alloc_hist(target, nr_events) < 0) {
					pthread_mutex_unlock(&notes->lock);
					ui__warning("Not enough memory for annotating '%s' symbol!\n",
						    target->name);
					continue;
				}

				pthread_mutex_unlock(&notes->lock);
				symbol__tui_annotate(target, ms->map, evidx, nr_events,
						     timer, arg, delay_secs);
			}
			break;
298 299 300
		default:
			goto out;
		}
301 302 303

		if (nd != NULL)
			annotate_browser__set_top(self, nd);
304 305
	}
out:
306
	ui_browser__hide(&self->b);
307
	return key;
308 309
}

310
int hist_entry__tui_annotate(struct hist_entry *he, int evidx, int nr_events,
311
			     void(*timer)(void *arg), void *arg, int delay_secs)
312
{
313
	return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx, nr_events,
314
				    timer, arg, delay_secs);
315 316
}

317
int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
318 319
			 int nr_events, void(*timer)(void *arg), void *arg,
			 int delay_secs)
320 321
{
	struct objdump_line *pos, *n;
322
	struct annotation *notes;
323 324 325 326
	struct map_symbol ms = {
		.map = map,
		.sym = sym,
	};
327 328 329 330 331
	struct annotate_browser browser = {
		.b = {
			.refresh = ui_browser__list_head_refresh,
			.seek	 = ui_browser__list_head_seek,
			.write	 = annotate_browser__write,
332
			.priv	 = &ms,
333
		},
334 335 336
	};
	int ret;

337
	if (sym == NULL)
338 339
		return -1;

340
	if (map->dso->annotate_warned)
341 342
		return -1;

343
	if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
344
		ui__error_window(ui_helpline__last_msg);
345 346 347 348 349
		return -1;
	}

	ui_helpline__push("Press <- or ESC to exit");

350 351
	notes = symbol__annotation(sym);

352
	list_for_each_entry(pos, &notes->src->source, node) {
353
		struct objdump_line_rb_node *rbpos;
354
		size_t line_len = strlen(pos->line);
355

356 357 358 359 360 361
		if (browser.b.width < line_len)
			browser.b.width = line_len;
		rbpos = objdump_line__rb(pos);
		rbpos->idx = browser.b.nr_entries++;
	}

362
	browser.b.entries = &notes->src->source,
363
	browser.b.width += 18; /* Percentage */
364 365
	ret = annotate_browser__run(&browser, evidx, nr_events,
				    timer, arg, delay_secs);
366
	list_for_each_entry_safe(pos, n, &notes->src->source, node) {
367 368 369 370 371
		list_del(&pos->node);
		objdump_line__free(pos);
	}
	return ret;
}