perf annotate: Print asm code as blue when source code is displayed
[deliverable/linux.git] / tools / perf / util / ui / browsers / annotate.c
CommitLineData
ae55795e 1#include "../../util.h"
211ef127
ACM
2#include "../browser.h"
3#include "../helpline.h"
4#include "../libslang.h"
ae55795e
ACM
5#include "../ui.h"
6#include "../util.h"
78f7defe 7#include "../../annotate.h"
211ef127
ACM
8#include "../../hist.h"
9#include "../../sort.h"
10#include "../../symbol.h"
c97cf422 11#include <pthread.h>
cf958003 12#include <newt.h>
211ef127 13
92221162
ACM
14struct annotate_browser {
15 struct ui_browser b;
16 struct rb_root entries;
f1e9214c 17 struct rb_node *curr_hot;
34958544 18 struct objdump_line *selection;
0361fc25
ACM
19 int nr_asm_entries;
20 int nr_entries;
21 bool hide_src_code;
92221162
ACM
22};
23
24struct objdump_line_rb_node {
25 struct rb_node rb_node;
26 double percent;
27 u32 idx;
0361fc25 28 int idx_asm;
92221162
ACM
29};
30
31static inline
32struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
33{
34 return (struct objdump_line_rb_node *)(self + 1);
35}
36
0361fc25
ACM
37static 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
211ef127
ACM
49static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
50{
34958544 51 struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
0361fc25 52 struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
211ef127
ACM
53 bool current_entry = ui_browser__is_current_entry(self, row);
54 int width = self->width;
55
56 if (ol->offset != -1) {
92221162 57 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
8f9bbc40 58 ui_browser__set_percent_color(self, olrb->percent, current_entry);
92221162 59 slsmg_printf(" %7.2f ", olrb->percent);
92221162 60 } else {
8f9bbc40 61 ui_browser__set_percent_color(self, 0, current_entry);
92221162
ACM
62 slsmg_write_nstring(" ", 9);
63 }
64
65 SLsmg_write_char(':');
66 slsmg_write_nstring(" ", 8);
c172f742
ACM
67
68 /* The scroll bar isn't being used */
69 if (!self->navkeypressed)
70 width += 1;
71
58e817d9
NK
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
92221162
ACM
77 if (!*ol->line)
78 slsmg_write_nstring(" ", width - 18);
79 else
80 slsmg_write_nstring(ol->line, width - 18);
b99976e2 81
58e817d9 82 if (current_entry)
34958544 83 ab->selection = ol;
92221162
ACM
84}
85
86static double objdump_line__calc_percent(struct objdump_line *self,
2f525d01 87 struct symbol *sym, int evidx)
92221162
ACM
88{
89 double percent = 0.0;
90
91 if (self->offset != -1) {
92 int len = sym->end - sym->start;
211ef127 93 unsigned int hits = 0;
78f7defe 94 struct annotation *notes = symbol__annotation(sym);
ce6f4fab 95 struct source_line *src_line = notes->src->lines;
2f525d01 96 struct sym_hist *h = annotation__histogram(notes, evidx);
92221162 97 s64 offset = self->offset;
ce6f4fab 98 struct objdump_line *next;
92221162 99
ce6f4fab 100 next = objdump__get_next_ip_line(&notes->src->source, self);
211ef127
ACM
101 while (offset < (s64)len &&
102 (next == NULL || offset < next->offset)) {
78f7defe
ACM
103 if (src_line) {
104 percent += src_line[offset].percent;
211ef127 105 } else
78f7defe 106 hits += h->addr[offset];
211ef127
ACM
107
108 ++offset;
109 }
78f7defe
ACM
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)
211ef127 115 percent = 100.0 * hits / h->sum;
211ef127
ACM
116 }
117
92221162
ACM
118 return percent;
119}
120
121static 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);
211ef127
ACM
138}
139
f1e9214c
ACM
140static 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
c97cf422
ACM
164static void annotate_browser__calc_percent(struct annotate_browser *browser,
165 int evidx)
f1e9214c 166{
34958544
ACM
167 struct map_symbol *ms = browser->b.priv;
168 struct symbol *sym = ms->sym;
c97cf422
ACM
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
0361fc25
ACM
190static 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
c97cf422 229static int annotate_browser__run(struct annotate_browser *self, int evidx,
d04b35f8 230 void(*timer)(void *arg),
34958544 231 void *arg, int delay_secs)
c97cf422
ACM
232{
233 struct rb_node *nd = NULL;
34958544
ACM
234 struct map_symbol *ms = self->b.priv;
235 struct symbol *sym = ms->sym;
0361fc25
ACM
236 const char *help = "<-, ESC: exit, TAB/shift+TAB: cycle hottest lines, "
237 "H: Hottest, -> Line action, S -> Toggle source "
238 "code view";
b50e003d 239 int key;
f1e9214c 240
0361fc25 241 if (ui_browser__show(&self->b, sym->name, help) < 0)
f1e9214c 242 return -1;
c97cf422 243
c97cf422
ACM
244 annotate_browser__calc_percent(self, evidx);
245
246 if (self->curr_hot)
247 annotate_browser__set_top(self, self->curr_hot);
f1e9214c
ACM
248
249 nd = self->curr_hot;
c97cf422 250
f1e9214c 251 while (1) {
3af6e338 252 key = ui_browser__run(&self->b, delay_secs);
f1e9214c 253
81cce8de 254 if (delay_secs != 0) {
c97cf422
ACM
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
b50e003d 265 switch (key) {
cf958003 266 case K_TIMER:
81cce8de
ACM
267 if (timer != NULL)
268 timer(arg);
269
270 if (delay_secs != 0)
c97cf422
ACM
271 symbol__annotate_decay_histogram(sym, evidx);
272 continue;
cf958003 273 case K_TAB:
c97cf422
ACM
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;
f1e9214c 280 break;
cf958003 281 case K_UNTAB:
c97cf422
ACM
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 nd = self->curr_hot;
f1e9214c 291 break;
0361fc25
ACM
292 case 'S':
293 if (annotate_browser__toggle_source(self))
294 ui_helpline__puts(help);
295 continue;
cf958003
ACM
296 case K_ENTER:
297 case K_RIGHT:
234a5375
ACM
298 if (self->selection == NULL) {
299 ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
300 continue;
301 }
302
303 if (self->selection->offset == -1) {
304 ui_helpline__puts("Actions are only available for assembly lines.");
305 continue;
306 } else {
34958544
ACM
307 char *s = strstr(self->selection->line, "callq ");
308 struct annotation *notes;
309 struct symbol *target;
310 u64 ip;
311
234a5375
ACM
312 if (s == NULL) {
313 ui_helpline__puts("Actions are only available for the 'callq' instruction.");
34958544 314 continue;
234a5375 315 }
34958544
ACM
316
317 s = strchr(s, ' ');
234a5375
ACM
318 if (s++ == NULL) {
319 ui_helpline__puts("Invallid callq instruction.");
34958544 320 continue;
234a5375 321 }
34958544
ACM
322
323 ip = strtoull(s, NULL, 16);
324 ip = ms->map->map_ip(ms->map, ip);
325 target = map__find_symbol(ms->map, ip, NULL);
234a5375
ACM
326 if (target == NULL) {
327 ui_helpline__puts("The called function was not found.");
34958544 328 continue;
234a5375 329 }
34958544
ACM
330
331 notes = symbol__annotation(target);
332 pthread_mutex_lock(&notes->lock);
333
d04b35f8 334 if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
34958544
ACM
335 pthread_mutex_unlock(&notes->lock);
336 ui__warning("Not enough memory for annotating '%s' symbol!\n",
337 target->name);
338 continue;
339 }
340
341 pthread_mutex_unlock(&notes->lock);
d04b35f8 342 symbol__tui_annotate(target, ms->map, evidx,
34958544
ACM
343 timer, arg, delay_secs);
344 }
fe46e64c 345 continue;
cf958003
ACM
346 case K_LEFT:
347 case K_ESC:
ed7e5662
ACM
348 case 'q':
349 case CTRL('c'):
f1e9214c 350 goto out;
ed7e5662
ACM
351 default:
352 continue;
f1e9214c 353 }
c97cf422
ACM
354
355 if (nd != NULL)
356 annotate_browser__set_top(self, nd);
f1e9214c
ACM
357 }
358out:
59e8fe32 359 ui_browser__hide(&self->b);
b50e003d 360 return key;
f1e9214c
ACM
361}
362
d04b35f8 363int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
81cce8de 364 void(*timer)(void *arg), void *arg, int delay_secs)
78f7defe 365{
d04b35f8 366 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
81cce8de 367 timer, arg, delay_secs);
78f7defe
ACM
368}
369
c97cf422 370int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
d04b35f8 371 void(*timer)(void *arg), void *arg,
34958544 372 int delay_secs)
211ef127 373{
211ef127 374 struct objdump_line *pos, *n;
db9a9cbc 375 struct annotation *notes;
34958544
ACM
376 struct map_symbol ms = {
377 .map = map,
378 .sym = sym,
379 };
92221162
ACM
380 struct annotate_browser browser = {
381 .b = {
92221162
ACM
382 .refresh = ui_browser__list_head_refresh,
383 .seek = ui_browser__list_head_seek,
384 .write = annotate_browser__write,
0361fc25 385 .filter = objdump_line__filter,
34958544 386 .priv = &ms,
c172f742 387 .use_navkeypressed = true,
92221162 388 },
211ef127
ACM
389 };
390 int ret;
391
78f7defe 392 if (sym == NULL)
211ef127
ACM
393 return -1;
394
78f7defe 395 if (map->dso->annotate_warned)
211ef127
ACM
396 return -1;
397
c97cf422 398 if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
ae55795e 399 ui__error("%s", ui_helpline__last_msg);
211ef127
ACM
400 return -1;
401 }
402
403 ui_helpline__push("Press <- or ESC to exit");
404
db9a9cbc
LM
405 notes = symbol__annotation(sym);
406
ce6f4fab 407 list_for_each_entry(pos, &notes->src->source, node) {
c97cf422 408 struct objdump_line_rb_node *rbpos;
211ef127 409 size_t line_len = strlen(pos->line);
c97cf422 410
92221162
ACM
411 if (browser.b.width < line_len)
412 browser.b.width = line_len;
413 rbpos = objdump_line__rb(pos);
0361fc25
ACM
414 rbpos->idx = browser.nr_entries++;
415 if (pos->offset != -1)
416 rbpos->idx_asm = browser.nr_asm_entries++;
417 else
418 rbpos->idx_asm = -1;
92221162
ACM
419 }
420
0361fc25 421 browser.b.nr_entries = browser.nr_entries;
db9a9cbc 422 browser.b.entries = &notes->src->source,
92221162 423 browser.b.width += 18; /* Percentage */
d04b35f8 424 ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
ce6f4fab 425 list_for_each_entry_safe(pos, n, &notes->src->source, node) {
211ef127
ACM
426 list_del(&pos->node);
427 objdump_line__free(pos);
428 }
211ef127
ACM
429 return ret;
430}
This page took 0.148157 seconds and 5 git commands to generate.