1 #include "../../util.h"
2 #include "../browser.h"
3 #include "../helpline.h"
4 #include "../libslang.h"
5 #include "../ui.h"
6 #include "../util.h"
7 #include "../../annotate.h"
8 #include "../../hist.h"
9 #include "../../sort.h"
10 #include "../../symbol.h"
11 #include <pthread.h>
12 #include <newt.h>
13 
14 struct annotate_browser {
15 	struct ui_browser b;
16 	struct rb_root	  entries;
17 	struct rb_node	  *curr_hot;
18 	struct objdump_line *selection;
19 	int		    nr_asm_entries;
20 	int		    nr_entries;
21 	bool		    hide_src_code;
22 };
23 
24 struct objdump_line_rb_node {
25 	struct rb_node	rb_node;
26 	double		percent;
27 	u32		idx;
28 	int		idx_asm;
29 };
30 
31 static inline
objdump_line__rb(struct objdump_line * self)32 struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
33 {
34 	return (struct objdump_line_rb_node *)(self + 1);
35 }
36 
objdump_line__filter(struct ui_browser * browser,void * entry)37 static bool objdump_line__filter(struct ui_browser *browser, void *entry)
38 {
39 	struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
40 
41 	if (ab->hide_src_code) {
42 		struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
43 		return ol->offset == -1;
44 	}
45 
46 	return false;
47 }
48 
annotate_browser__write(struct ui_browser * self,void * entry,int row)49 static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
50 {
51 	struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
52 	struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
53 	bool current_entry = ui_browser__is_current_entry(self, row);
54 	int width = self->width;
55 
56 	if (ol->offset != -1) {
57 		struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
58 		ui_browser__set_percent_color(self, olrb->percent, current_entry);
59 		slsmg_printf(" %7.2f ", olrb->percent);
60 	} else {
61 		ui_browser__set_percent_color(self, 0, current_entry);
62 		slsmg_write_nstring(" ", 9);
63 	}
64 
65 	SLsmg_write_char(':');
66 	slsmg_write_nstring(" ", 8);
67 
68 	/* The scroll bar isn't being used */
69 	if (!self->navkeypressed)
70 		width += 1;
71 
72 	if (!*ol->line)
73 		slsmg_write_nstring(" ", width - 18);
74 	else
75 		slsmg_write_nstring(ol->line, width - 18);
76 
77 	if (!current_entry)
78 		ui_browser__set_color(self, HE_COLORSET_CODE);
79 	else
80 		ab->selection = ol;
81 }
82 
objdump_line__calc_percent(struct objdump_line * self,struct symbol * sym,int evidx)83 static double objdump_line__calc_percent(struct objdump_line *self,
84 					 struct symbol *sym, int evidx)
85 {
86 	double percent = 0.0;
87 
88 	if (self->offset != -1) {
89 		int len = sym->end - sym->start;
90 		unsigned int hits = 0;
91 		struct annotation *notes = symbol__annotation(sym);
92 		struct source_line *src_line = notes->src->lines;
93 		struct sym_hist *h = annotation__histogram(notes, evidx);
94 		s64 offset = self->offset;
95 		struct objdump_line *next;
96 
97 		next = objdump__get_next_ip_line(&notes->src->source, self);
98 		while (offset < (s64)len &&
99 		       (next == NULL || offset < next->offset)) {
100 			if (src_line) {
101 				percent += src_line[offset].percent;
102 			} else
103 				hits += h->addr[offset];
104 
105 			++offset;
106 		}
107 		/*
108  		 * If the percentage wasn't already calculated in
109  		 * symbol__get_source_line, do it now:
110  		 */
111 		if (src_line == NULL && h->sum)
112 			percent = 100.0 * hits / h->sum;
113 	}
114 
115 	return percent;
116 }
117 
objdump__insert_line(struct rb_root * self,struct objdump_line_rb_node * line)118 static void objdump__insert_line(struct rb_root *self,
119 				 struct objdump_line_rb_node *line)
120 {
121 	struct rb_node **p = &self->rb_node;
122 	struct rb_node *parent = NULL;
123 	struct objdump_line_rb_node *l;
124 
125 	while (*p != NULL) {
126 		parent = *p;
127 		l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
128 		if (line->percent < l->percent)
129 			p = &(*p)->rb_left;
130 		else
131 			p = &(*p)->rb_right;
132 	}
133 	rb_link_node(&line->rb_node, parent, p);
134 	rb_insert_color(&line->rb_node, self);
135 }
136 
annotate_browser__set_top(struct annotate_browser * self,struct rb_node * nd)137 static void annotate_browser__set_top(struct annotate_browser *self,
138 				      struct rb_node *nd)
139 {
140 	struct objdump_line_rb_node *rbpos;
141 	struct objdump_line *pos;
142 	unsigned back;
143 
144 	ui_browser__refresh_dimensions(&self->b);
145 	back = self->b.height / 2;
146 	rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
147 	pos = ((struct objdump_line *)rbpos) - 1;
148 	self->b.top_idx = self->b.index = rbpos->idx;
149 
150 	while (self->b.top_idx != 0 && back != 0) {
151 		pos = list_entry(pos->node.prev, struct objdump_line, node);
152 
153 		--self->b.top_idx;
154 		--back;
155 	}
156 
157 	self->b.top = pos;
158 	self->curr_hot = nd;
159 }
160 
annotate_browser__calc_percent(struct annotate_browser * browser,int evidx)161 static void annotate_browser__calc_percent(struct annotate_browser *browser,
162 					   int evidx)
163 {
164 	struct map_symbol *ms = browser->b.priv;
165 	struct symbol *sym = ms->sym;
166 	struct annotation *notes = symbol__annotation(sym);
167 	struct objdump_line *pos;
168 
169 	browser->entries = RB_ROOT;
170 
171 	pthread_mutex_lock(&notes->lock);
172 
173 	list_for_each_entry(pos, &notes->src->source, node) {
174 		struct objdump_line_rb_node *rbpos = objdump_line__rb(pos);
175 		rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
176 		if (rbpos->percent < 0.01) {
177 			RB_CLEAR_NODE(&rbpos->rb_node);
178 			continue;
179 		}
180 		objdump__insert_line(&browser->entries, rbpos);
181 	}
182 	pthread_mutex_unlock(&notes->lock);
183 
184 	browser->curr_hot = rb_last(&browser->entries);
185 }
186 
annotate_browser__toggle_source(struct annotate_browser * browser)187 static bool annotate_browser__toggle_source(struct annotate_browser *browser)
188 {
189 	struct objdump_line *ol;
190 	struct objdump_line_rb_node *olrb;
191 	off_t offset = browser->b.index - browser->b.top_idx;
192 
193 	browser->b.seek(&browser->b, offset, SEEK_CUR);
194 	ol = list_entry(browser->b.top, struct objdump_line, node);
195 	olrb = objdump_line__rb(ol);
196 
197 	if (browser->hide_src_code) {
198 		if (olrb->idx_asm < offset)
199 			offset = olrb->idx;
200 
201 		browser->b.nr_entries = browser->nr_entries;
202 		browser->hide_src_code = false;
203 		browser->b.seek(&browser->b, -offset, SEEK_CUR);
204 		browser->b.top_idx = olrb->idx - offset;
205 		browser->b.index = olrb->idx;
206 	} else {
207 		if (olrb->idx_asm < 0) {
208 			ui_helpline__puts("Only available for assembly lines.");
209 			browser->b.seek(&browser->b, -offset, SEEK_CUR);
210 			return false;
211 		}
212 
213 		if (olrb->idx_asm < offset)
214 			offset = olrb->idx_asm;
215 
216 		browser->b.nr_entries = browser->nr_asm_entries;
217 		browser->hide_src_code = true;
218 		browser->b.seek(&browser->b, -offset, SEEK_CUR);
219 		browser->b.top_idx = olrb->idx_asm - offset;
220 		browser->b.index = olrb->idx_asm;
221 	}
222 
223 	return true;
224 }
225 
annotate_browser__run(struct annotate_browser * self,int evidx,void (* timer)(void * arg),void * arg,int delay_secs)226 static int annotate_browser__run(struct annotate_browser *self, int evidx,
227 				 void(*timer)(void *arg),
228 				 void *arg, int delay_secs)
229 {
230 	struct rb_node *nd = NULL;
231 	struct map_symbol *ms = self->b.priv;
232 	struct symbol *sym = ms->sym;
233 	const char *help = "<-, ESC: exit, TAB/shift+TAB: cycle hottest lines, "
234 			   "H: Hottest, -> Line action, S -> Toggle source "
235 			   "code view";
236 	int key;
237 
238 	if (ui_browser__show(&self->b, sym->name, help) < 0)
239 		return -1;
240 
241 	annotate_browser__calc_percent(self, evidx);
242 
243 	if (self->curr_hot)
244 		annotate_browser__set_top(self, self->curr_hot);
245 
246 	nd = self->curr_hot;
247 
248 	while (1) {
249 		key = ui_browser__run(&self->b, delay_secs);
250 
251 		if (delay_secs != 0) {
252 			annotate_browser__calc_percent(self, evidx);
253 			/*
254 			 * Current line focus got out of the list of most active
255 			 * lines, NULL it so that if TAB|UNTAB is pressed, we
256 			 * move to curr_hot (current hottest line).
257 			 */
258 			if (nd != NULL && RB_EMPTY_NODE(nd))
259 				nd = NULL;
260 		}
261 
262 		switch (key) {
263 		case K_TIMER:
264 			if (timer != NULL)
265 				timer(arg);
266 
267 			if (delay_secs != 0)
268 				symbol__annotate_decay_histogram(sym, evidx);
269 			continue;
270 		case K_TAB:
271 			if (nd != NULL) {
272 				nd = rb_prev(nd);
273 				if (nd == NULL)
274 					nd = rb_last(&self->entries);
275 			} else
276 				nd = self->curr_hot;
277 			break;
278 		case K_UNTAB:
279 			if (nd != NULL)
280 				nd = rb_next(nd);
281 				if (nd == NULL)
282 					nd = rb_first(&self->entries);
283 			else
284 				nd = self->curr_hot;
285 			break;
286 		case 'H':
287 			nd = self->curr_hot;
288 			break;
289 		case 'S':
290 			if (annotate_browser__toggle_source(self))
291 				ui_helpline__puts(help);
292 			continue;
293 		case K_ENTER:
294 		case K_RIGHT:
295 			if (self->selection == NULL) {
296 				ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
297 				continue;
298 			}
299 
300 			if (self->selection->offset == -1) {
301 				ui_helpline__puts("Actions are only available for assembly lines.");
302 				continue;
303 			} else {
304 				char *s = strstr(self->selection->line, "callq ");
305 				struct annotation *notes;
306 				struct symbol *target;
307 				u64 ip;
308 
309 				if (s == NULL) {
310 					ui_helpline__puts("Actions are only available for the 'callq' instruction.");
311 					continue;
312 				}
313 
314 				s = strchr(s, ' ');
315 				if (s++ == NULL) {
316 					ui_helpline__puts("Invallid callq instruction.");
317 					continue;
318 				}
319 
320 				ip = strtoull(s, NULL, 16);
321 				ip = ms->map->map_ip(ms->map, ip);
322 				target = map__find_symbol(ms->map, ip, NULL);
323 				if (target == NULL) {
324 					ui_helpline__puts("The called function was not found.");
325 					continue;
326 				}
327 
328 				notes = symbol__annotation(target);
329 				pthread_mutex_lock(&notes->lock);
330 
331 				if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
332 					pthread_mutex_unlock(&notes->lock);
333 					ui__warning("Not enough memory for annotating '%s' symbol!\n",
334 						    target->name);
335 					continue;
336 				}
337 
338 				pthread_mutex_unlock(&notes->lock);
339 				symbol__tui_annotate(target, ms->map, evidx,
340 						     timer, arg, delay_secs);
341 			}
342 			continue;
343 		case K_LEFT:
344 		case K_ESC:
345 		case 'q':
346 		case CTRL('c'):
347 			goto out;
348 		default:
349 			continue;
350 		}
351 
352 		if (nd != NULL)
353 			annotate_browser__set_top(self, nd);
354 	}
355 out:
356 	ui_browser__hide(&self->b);
357 	return key;
358 }
359 
hist_entry__tui_annotate(struct hist_entry * he,int evidx,void (* timer)(void * arg),void * arg,int delay_secs)360 int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
361 			     void(*timer)(void *arg), void *arg, int delay_secs)
362 {
363 	return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
364 				    timer, arg, delay_secs);
365 }
366 
symbol__tui_annotate(struct symbol * sym,struct map * map,int evidx,void (* timer)(void * arg),void * arg,int delay_secs)367 int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
368 			 void(*timer)(void *arg), void *arg,
369 			 int delay_secs)
370 {
371 	struct objdump_line *pos, *n;
372 	struct annotation *notes;
373 	struct map_symbol ms = {
374 		.map = map,
375 		.sym = sym,
376 	};
377 	struct annotate_browser browser = {
378 		.b = {
379 			.refresh = ui_browser__list_head_refresh,
380 			.seek	 = ui_browser__list_head_seek,
381 			.write	 = annotate_browser__write,
382 			.filter  = objdump_line__filter,
383 			.priv	 = &ms,
384 			.use_navkeypressed = true,
385 		},
386 	};
387 	int ret;
388 
389 	if (sym == NULL)
390 		return -1;
391 
392 	if (map->dso->annotate_warned)
393 		return -1;
394 
395 	if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
396 		ui__error("%s", ui_helpline__last_msg);
397 		return -1;
398 	}
399 
400 	ui_helpline__push("Press <- or ESC to exit");
401 
402 	notes = symbol__annotation(sym);
403 
404 	list_for_each_entry(pos, &notes->src->source, node) {
405 		struct objdump_line_rb_node *rbpos;
406 		size_t line_len = strlen(pos->line);
407 
408 		if (browser.b.width < line_len)
409 			browser.b.width = line_len;
410 		rbpos = objdump_line__rb(pos);
411 		rbpos->idx = browser.nr_entries++;
412 		if (pos->offset != -1)
413 			rbpos->idx_asm = browser.nr_asm_entries++;
414 		else
415 			rbpos->idx_asm = -1;
416 	}
417 
418 	browser.b.nr_entries = browser.nr_entries;
419 	browser.b.entries = &notes->src->source,
420 	browser.b.width += 18; /* Percentage */
421 	ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
422 	list_for_each_entry_safe(pos, n, &notes->src->source, node) {
423 		list_del(&pos->node);
424 		objdump_line__free(pos);
425 	}
426 	return ret;
427 }
428