]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - tools/perf/util/ui/browsers/annotate.c
perf annotate: Fix a build error
[thirdparty/kernel/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;
058b4cc9 19 u64 start;
0361fc25
ACM
20 int nr_asm_entries;
21 int nr_entries;
22 bool hide_src_code;
e235f3f3 23 bool use_offset;
d3d1f61a
ACM
24 bool searching_backwards;
25 char search_bf[128];
92221162
ACM
26};
27
28struct objdump_line_rb_node {
29 struct rb_node rb_node;
30 double percent;
31 u32 idx;
0361fc25 32 int idx_asm;
92221162
ACM
33};
34
35static inline
36struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
37{
38 return (struct objdump_line_rb_node *)(self + 1);
39}
40
0361fc25
ACM
41static bool objdump_line__filter(struct ui_browser *browser, void *entry)
42{
43 struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
44
45 if (ab->hide_src_code) {
46 struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
47 return ol->offset == -1;
48 }
49
50 return false;
51}
52
211ef127
ACM
53static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
54{
34958544 55 struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
0361fc25 56 struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
211ef127 57 bool current_entry = ui_browser__is_current_entry(self, row);
058b4cc9
ACM
58 bool change_color = (!ab->hide_src_code &&
59 (!current_entry || (self->use_navkeypressed &&
60 !self->navkeypressed)));
211ef127
ACM
61 int width = self->width;
62
63 if (ol->offset != -1) {
92221162 64 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
8f9bbc40 65 ui_browser__set_percent_color(self, olrb->percent, current_entry);
92221162 66 slsmg_printf(" %7.2f ", olrb->percent);
92221162 67 } else {
8f9bbc40 68 ui_browser__set_percent_color(self, 0, current_entry);
92221162
ACM
69 slsmg_write_nstring(" ", 9);
70 }
71
72 SLsmg_write_char(':');
73 slsmg_write_nstring(" ", 8);
c172f742
ACM
74
75 /* The scroll bar isn't being used */
76 if (!self->navkeypressed)
77 width += 1;
78
058b4cc9
ACM
79 if (ol->offset != -1 && change_color)
80 ui_browser__set_color(self, HE_COLORSET_CODE);
58e817d9 81
92221162
ACM
82 if (!*ol->line)
83 slsmg_write_nstring(" ", width - 18);
058b4cc9 84 else if (ol->offset == -1)
92221162 85 slsmg_write_nstring(ol->line, width - 18);
058b4cc9
ACM
86 else {
87 char bf[64];
e235f3f3
ACM
88 u64 addr = ol->offset;
89 int printed, color = -1;
058b4cc9 90
e235f3f3
ACM
91 if (!ab->use_offset)
92 addr += ab->start;
93
94 printed = scnprintf(bf, sizeof(bf), " %" PRIx64 ":", addr);
058b4cc9
ACM
95 if (change_color)
96 color = ui_browser__set_color(self, HE_COLORSET_ADDR);
97 slsmg_write_nstring(bf, printed);
98 if (change_color)
99 ui_browser__set_color(self, color);
100 slsmg_write_nstring(ol->line, width - 18 - printed);
101 }
b99976e2 102
58e817d9 103 if (current_entry)
34958544 104 ab->selection = ol;
92221162
ACM
105}
106
107static double objdump_line__calc_percent(struct objdump_line *self,
2f525d01 108 struct symbol *sym, int evidx)
92221162
ACM
109{
110 double percent = 0.0;
111
112 if (self->offset != -1) {
113 int len = sym->end - sym->start;
211ef127 114 unsigned int hits = 0;
78f7defe 115 struct annotation *notes = symbol__annotation(sym);
ce6f4fab 116 struct source_line *src_line = notes->src->lines;
2f525d01 117 struct sym_hist *h = annotation__histogram(notes, evidx);
92221162 118 s64 offset = self->offset;
ce6f4fab 119 struct objdump_line *next;
92221162 120
ce6f4fab 121 next = objdump__get_next_ip_line(&notes->src->source, self);
211ef127
ACM
122 while (offset < (s64)len &&
123 (next == NULL || offset < next->offset)) {
78f7defe
ACM
124 if (src_line) {
125 percent += src_line[offset].percent;
211ef127 126 } else
78f7defe 127 hits += h->addr[offset];
211ef127
ACM
128
129 ++offset;
130 }
78f7defe
ACM
131 /*
132 * If the percentage wasn't already calculated in
133 * symbol__get_source_line, do it now:
134 */
135 if (src_line == NULL && h->sum)
211ef127 136 percent = 100.0 * hits / h->sum;
211ef127
ACM
137 }
138
92221162
ACM
139 return percent;
140}
141
142static void objdump__insert_line(struct rb_root *self,
143 struct objdump_line_rb_node *line)
144{
145 struct rb_node **p = &self->rb_node;
146 struct rb_node *parent = NULL;
147 struct objdump_line_rb_node *l;
148
149 while (*p != NULL) {
150 parent = *p;
151 l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
152 if (line->percent < l->percent)
153 p = &(*p)->rb_left;
154 else
155 p = &(*p)->rb_right;
156 }
157 rb_link_node(&line->rb_node, parent, p);
158 rb_insert_color(&line->rb_node, self);
211ef127
ACM
159}
160
f1e9214c 161static void annotate_browser__set_top(struct annotate_browser *self,
b0ffb2c4 162 struct objdump_line *pos, u32 idx)
f1e9214c 163{
f1e9214c
ACM
164 unsigned back;
165
166 ui_browser__refresh_dimensions(&self->b);
167 back = self->b.height / 2;
b0ffb2c4 168 self->b.top_idx = self->b.index = idx;
f1e9214c
ACM
169
170 while (self->b.top_idx != 0 && back != 0) {
171 pos = list_entry(pos->node.prev, struct objdump_line, node);
172
08be4eed
ACM
173 if (objdump_line__filter(&self->b, &pos->node))
174 continue;
175
f1e9214c
ACM
176 --self->b.top_idx;
177 --back;
178 }
179
180 self->b.top = pos;
d3d1f61a 181 self->b.navkeypressed = true;
b0ffb2c4
ACM
182}
183
184static void annotate_browser__set_rb_top(struct annotate_browser *browser,
185 struct rb_node *nd)
186{
187 struct objdump_line_rb_node *rbpos;
188 struct objdump_line *pos;
189
190 rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
191 pos = ((struct objdump_line *)rbpos) - 1;
192 annotate_browser__set_top(browser, pos, rbpos->idx);
193 browser->curr_hot = nd;
f1e9214c
ACM
194}
195
c97cf422
ACM
196static void annotate_browser__calc_percent(struct annotate_browser *browser,
197 int evidx)
f1e9214c 198{
34958544
ACM
199 struct map_symbol *ms = browser->b.priv;
200 struct symbol *sym = ms->sym;
c97cf422
ACM
201 struct annotation *notes = symbol__annotation(sym);
202 struct objdump_line *pos;
203
204 browser->entries = RB_ROOT;
205
206 pthread_mutex_lock(&notes->lock);
207
208 list_for_each_entry(pos, &notes->src->source, node) {
209 struct objdump_line_rb_node *rbpos = objdump_line__rb(pos);
210 rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
211 if (rbpos->percent < 0.01) {
212 RB_CLEAR_NODE(&rbpos->rb_node);
213 continue;
214 }
215 objdump__insert_line(&browser->entries, rbpos);
216 }
217 pthread_mutex_unlock(&notes->lock);
218
219 browser->curr_hot = rb_last(&browser->entries);
220}
221
0361fc25
ACM
222static bool annotate_browser__toggle_source(struct annotate_browser *browser)
223{
224 struct objdump_line *ol;
225 struct objdump_line_rb_node *olrb;
226 off_t offset = browser->b.index - browser->b.top_idx;
227
228 browser->b.seek(&browser->b, offset, SEEK_CUR);
229 ol = list_entry(browser->b.top, struct objdump_line, node);
230 olrb = objdump_line__rb(ol);
231
232 if (browser->hide_src_code) {
233 if (olrb->idx_asm < offset)
234 offset = olrb->idx;
235
236 browser->b.nr_entries = browser->nr_entries;
237 browser->hide_src_code = false;
238 browser->b.seek(&browser->b, -offset, SEEK_CUR);
239 browser->b.top_idx = olrb->idx - offset;
240 browser->b.index = olrb->idx;
241 } else {
242 if (olrb->idx_asm < 0) {
243 ui_helpline__puts("Only available for assembly lines.");
244 browser->b.seek(&browser->b, -offset, SEEK_CUR);
245 return false;
246 }
247
248 if (olrb->idx_asm < offset)
249 offset = olrb->idx_asm;
250
251 browser->b.nr_entries = browser->nr_asm_entries;
252 browser->hide_src_code = true;
253 browser->b.seek(&browser->b, -offset, SEEK_CUR);
254 browser->b.top_idx = olrb->idx_asm - offset;
255 browser->b.index = olrb->idx_asm;
256 }
257
258 return true;
259}
260
60521702
ACM
261static bool annotate_browser__callq(struct annotate_browser *browser,
262 int evidx, void (*timer)(void *arg),
263 void *arg, int delay_secs)
264{
265 struct map_symbol *ms = browser->b.priv;
266 struct symbol *sym = ms->sym;
267 struct annotation *notes;
268 struct symbol *target;
269 char *s = strstr(browser->selection->line, "callq ");
270 u64 ip;
271
272 if (s == NULL)
273 return false;
274
275 s = strchr(s, ' ');
276 if (s++ == NULL) {
277 ui_helpline__puts("Invallid callq instruction.");
278 return true;
279 }
280
281 ip = strtoull(s, NULL, 16);
282 ip = ms->map->map_ip(ms->map, ip);
283 target = map__find_symbol(ms->map, ip, NULL);
284 if (target == NULL) {
285 ui_helpline__puts("The called function was not found.");
286 return true;
287 }
288
289 notes = symbol__annotation(target);
290 pthread_mutex_lock(&notes->lock);
291
292 if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
293 pthread_mutex_unlock(&notes->lock);
294 ui__warning("Not enough memory for annotating '%s' symbol!\n",
295 target->name);
296 return true;
297 }
298
299 pthread_mutex_unlock(&notes->lock);
300 symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
301 ui_browser__show_title(&browser->b, sym->name);
302 return true;
303}
304
08be4eed
ACM
305static struct objdump_line *
306 annotate_browser__find_offset(struct annotate_browser *browser,
307 s64 offset, s64 *idx)
308{
309 struct map_symbol *ms = browser->b.priv;
310 struct symbol *sym = ms->sym;
311 struct annotation *notes = symbol__annotation(sym);
312 struct objdump_line *pos;
313
314 *idx = 0;
315 list_for_each_entry(pos, &notes->src->source, node) {
316 if (pos->offset == offset)
317 return pos;
318 if (!objdump_line__filter(&browser->b, &pos->node))
319 ++*idx;
320 }
321
322 return NULL;
323}
324
325static bool annotate_browser__jump(struct annotate_browser *browser)
326{
327 const char *jumps[] = { "je ", "jne ", "ja ", "jmpq ", "js ", "jmp ", NULL };
328 struct objdump_line *line;
329 s64 idx, offset;
330 char *s = NULL;
331 int i = 0;
332
333 while (jumps[i]) {
334 s = strstr(browser->selection->line, jumps[i++]);
335 if (s)
336 break;
337 }
338
339 if (s == NULL)
340 return false;
341
342 s = strchr(s, '+');
343 if (s++ == NULL) {
344 ui_helpline__puts("Invallid jump instruction.");
345 return true;
346 }
347
348 offset = strtoll(s, NULL, 16);
349 line = annotate_browser__find_offset(browser, offset, &idx);
350 if (line == NULL) {
351 ui_helpline__puts("Invallid jump offset");
352 return true;
353 }
354
355 annotate_browser__set_top(browser, line, idx);
356
357 return true;
358}
359
d3d1f61a
ACM
360static struct objdump_line *
361 annotate_browser__find_string(struct annotate_browser *browser,
362 char *s, s64 *idx)
363{
364 struct map_symbol *ms = browser->b.priv;
365 struct symbol *sym = ms->sym;
366 struct annotation *notes = symbol__annotation(sym);
367 struct objdump_line *pos = browser->selection;
368
369 *idx = browser->b.index;
370 list_for_each_entry_continue(pos, &notes->src->source, node) {
371 if (objdump_line__filter(&browser->b, &pos->node))
372 continue;
373
374 ++*idx;
375
376 if (pos->line && strstr(pos->line, s) != NULL)
377 return pos;
378 }
379
380 return NULL;
381}
382
383static bool __annotate_browser__search(struct annotate_browser *browser)
384{
385 struct objdump_line *line;
386 s64 idx;
387
388 line = annotate_browser__find_string(browser, browser->search_bf, &idx);
389 if (line == NULL) {
390 ui_helpline__puts("String not found!");
391 return false;
392 }
393
394 annotate_browser__set_top(browser, line, idx);
395 browser->searching_backwards = false;
396 return true;
397}
398
399static struct objdump_line *
400 annotate_browser__find_string_reverse(struct annotate_browser *browser,
401 char *s, s64 *idx)
402{
403 struct map_symbol *ms = browser->b.priv;
404 struct symbol *sym = ms->sym;
405 struct annotation *notes = symbol__annotation(sym);
406 struct objdump_line *pos = browser->selection;
407
408 *idx = browser->b.index;
409 list_for_each_entry_continue_reverse(pos, &notes->src->source, node) {
410 if (objdump_line__filter(&browser->b, &pos->node))
411 continue;
412
413 --*idx;
414
415 if (pos->line && strstr(pos->line, s) != NULL)
416 return pos;
417 }
418
419 return NULL;
420}
421
422static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
423{
424 struct objdump_line *line;
425 s64 idx;
426
427 line = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
428 if (line == NULL) {
429 ui_helpline__puts("String not found!");
430 return false;
431 }
432
433 annotate_browser__set_top(browser, line, idx);
434 browser->searching_backwards = true;
435 return true;
436}
437
438static bool annotate_browser__search_window(struct annotate_browser *browser,
439 int delay_secs)
440{
441 if (ui_browser__input_window("Search", "String: ", browser->search_bf,
442 "ENTER: OK, ESC: Cancel",
443 delay_secs * 2) != K_ENTER ||
444 !*browser->search_bf)
445 return false;
446
447 return true;
448}
449
450static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs)
451{
452 if (annotate_browser__search_window(browser, delay_secs))
453 return __annotate_browser__search(browser);
454
455 return false;
456}
457
458static bool annotate_browser__continue_search(struct annotate_browser *browser,
459 int delay_secs)
460{
461 if (!*browser->search_bf)
462 return annotate_browser__search(browser, delay_secs);
463
464 return __annotate_browser__search(browser);
465}
466
467static bool annotate_browser__search_reverse(struct annotate_browser *browser,
468 int delay_secs)
469{
470 if (annotate_browser__search_window(browser, delay_secs))
471 return __annotate_browser__search_reverse(browser);
472
473 return false;
474}
475
476static
477bool annotate_browser__continue_search_reverse(struct annotate_browser *browser,
478 int delay_secs)
479{
480 if (!*browser->search_bf)
481 return annotate_browser__search_reverse(browser, delay_secs);
482
483 return __annotate_browser__search_reverse(browser);
484}
485
c97cf422 486static int annotate_browser__run(struct annotate_browser *self, int evidx,
d04b35f8 487 void(*timer)(void *arg),
34958544 488 void *arg, int delay_secs)
c97cf422
ACM
489{
490 struct rb_node *nd = NULL;
34958544
ACM
491 struct map_symbol *ms = self->b.priv;
492 struct symbol *sym = ms->sym;
824ac0e9
NK
493 const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
494 "H: Go to hottest line, ->/ENTER: Line action, "
e235f3f3 495 "O: Toggle offset view, "
824ac0e9 496 "S: Toggle source code view";
b50e003d 497 int key;
f1e9214c 498
0361fc25 499 if (ui_browser__show(&self->b, sym->name, help) < 0)
f1e9214c 500 return -1;
c97cf422 501
c97cf422
ACM
502 annotate_browser__calc_percent(self, evidx);
503
d3d1f61a 504 if (self->curr_hot) {
b0ffb2c4 505 annotate_browser__set_rb_top(self, self->curr_hot);
d3d1f61a
ACM
506 self->b.navkeypressed = false;
507 }
f1e9214c
ACM
508
509 nd = self->curr_hot;
c97cf422 510
f1e9214c 511 while (1) {
3af6e338 512 key = ui_browser__run(&self->b, delay_secs);
f1e9214c 513
81cce8de 514 if (delay_secs != 0) {
c97cf422
ACM
515 annotate_browser__calc_percent(self, evidx);
516 /*
517 * Current line focus got out of the list of most active
518 * lines, NULL it so that if TAB|UNTAB is pressed, we
519 * move to curr_hot (current hottest line).
520 */
521 if (nd != NULL && RB_EMPTY_NODE(nd))
522 nd = NULL;
523 }
524
b50e003d 525 switch (key) {
cf958003 526 case K_TIMER:
81cce8de
ACM
527 if (timer != NULL)
528 timer(arg);
529
530 if (delay_secs != 0)
c97cf422
ACM
531 symbol__annotate_decay_histogram(sym, evidx);
532 continue;
cf958003 533 case K_TAB:
c97cf422
ACM
534 if (nd != NULL) {
535 nd = rb_prev(nd);
536 if (nd == NULL)
537 nd = rb_last(&self->entries);
538 } else
539 nd = self->curr_hot;
f1e9214c 540 break;
cf958003 541 case K_UNTAB:
c97cf422
ACM
542 if (nd != NULL)
543 nd = rb_next(nd);
544 if (nd == NULL)
545 nd = rb_first(&self->entries);
546 else
547 nd = self->curr_hot;
548 break;
549 case 'H':
ef7c5372 550 case 'h':
c97cf422 551 nd = self->curr_hot;
f1e9214c 552 break;
0361fc25 553 case 'S':
ef7c5372 554 case 's':
0361fc25
ACM
555 if (annotate_browser__toggle_source(self))
556 ui_helpline__puts(help);
557 continue;
e235f3f3
ACM
558 case 'O':
559 case 'o':
560 self->use_offset = !self->use_offset;
561 continue;
d3d1f61a
ACM
562 case '/':
563 if (annotate_browser__search(self, delay_secs)) {
564show_help:
565 ui_helpline__puts(help);
566 }
567 continue;
568 case 'n':
569 if (self->searching_backwards ?
570 annotate_browser__continue_search_reverse(self, delay_secs) :
571 annotate_browser__continue_search(self, delay_secs))
572 goto show_help;
573 continue;
574 case '?':
575 if (annotate_browser__search_reverse(self, delay_secs))
576 goto show_help;
577 continue;
cf958003
ACM
578 case K_ENTER:
579 case K_RIGHT:
60521702 580 if (self->selection == NULL)
234a5375 581 ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
60521702 582 else if (self->selection->offset == -1)
234a5375 583 ui_helpline__puts("Actions are only available for assembly lines.");
08be4eed
ACM
584 else if (!(annotate_browser__jump(self) ||
585 annotate_browser__callq(self, evidx, timer, arg, delay_secs)))
586 ui_helpline__puts("Actions are only available for the 'callq' and jump instructions.");
fe46e64c 587 continue;
cf958003
ACM
588 case K_LEFT:
589 case K_ESC:
ed7e5662
ACM
590 case 'q':
591 case CTRL('c'):
f1e9214c 592 goto out;
ed7e5662
ACM
593 default:
594 continue;
f1e9214c 595 }
c97cf422
ACM
596
597 if (nd != NULL)
b0ffb2c4 598 annotate_browser__set_rb_top(self, nd);
f1e9214c
ACM
599 }
600out:
59e8fe32 601 ui_browser__hide(&self->b);
b50e003d 602 return key;
f1e9214c
ACM
603}
604
d04b35f8 605int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
81cce8de 606 void(*timer)(void *arg), void *arg, int delay_secs)
78f7defe 607{
d04b35f8 608 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
81cce8de 609 timer, arg, delay_secs);
78f7defe
ACM
610}
611
c97cf422 612int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
d04b35f8 613 void(*timer)(void *arg), void *arg,
34958544 614 int delay_secs)
211ef127 615{
211ef127 616 struct objdump_line *pos, *n;
db9a9cbc 617 struct annotation *notes;
34958544
ACM
618 struct map_symbol ms = {
619 .map = map,
620 .sym = sym,
621 };
92221162
ACM
622 struct annotate_browser browser = {
623 .b = {
92221162
ACM
624 .refresh = ui_browser__list_head_refresh,
625 .seek = ui_browser__list_head_seek,
626 .write = annotate_browser__write,
0361fc25 627 .filter = objdump_line__filter,
34958544 628 .priv = &ms,
c172f742 629 .use_navkeypressed = true,
92221162 630 },
211ef127
ACM
631 };
632 int ret;
633
78f7defe 634 if (sym == NULL)
211ef127
ACM
635 return -1;
636
78f7defe 637 if (map->dso->annotate_warned)
211ef127
ACM
638 return -1;
639
c97cf422 640 if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
ae55795e 641 ui__error("%s", ui_helpline__last_msg);
211ef127
ACM
642 return -1;
643 }
644
645 ui_helpline__push("Press <- or ESC to exit");
646
db9a9cbc 647 notes = symbol__annotation(sym);
058b4cc9 648 browser.start = map__rip_2objdump(map, sym->start);
db9a9cbc 649
ce6f4fab 650 list_for_each_entry(pos, &notes->src->source, node) {
c97cf422 651 struct objdump_line_rb_node *rbpos;
211ef127 652 size_t line_len = strlen(pos->line);
c97cf422 653
92221162
ACM
654 if (browser.b.width < line_len)
655 browser.b.width = line_len;
656 rbpos = objdump_line__rb(pos);
0361fc25
ACM
657 rbpos->idx = browser.nr_entries++;
658 if (pos->offset != -1)
659 rbpos->idx_asm = browser.nr_asm_entries++;
660 else
661 rbpos->idx_asm = -1;
92221162
ACM
662 }
663
0361fc25 664 browser.b.nr_entries = browser.nr_entries;
db9a9cbc 665 browser.b.entries = &notes->src->source,
92221162 666 browser.b.width += 18; /* Percentage */
d04b35f8 667 ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
ce6f4fab 668 list_for_each_entry_safe(pos, n, &notes->src->source, node) {
211ef127
ACM
669 list_del(&pos->node);
670 objdump_line__free(pos);
671 }
211ef127
ACM
672 return ret;
673}