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