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