perf annotate browser: Don't change selection line when returning from callq
[linux-2.6.git] / tools / perf / util / ui / browsers / annotate.c
1 #include "../browser.h"
2 #include "../helpline.h"
3 #include "../libslang.h"
4 #include "../../annotate.h"
5 #include "../../hist.h"
6 #include "../../sort.h"
7 #include "../../symbol.h"
8 #include <pthread.h>
9
10 static 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
19 struct annotate_browser {
20         struct ui_browser b;
21         struct rb_root    entries;
22         struct rb_node    *curr_hot;
23         struct objdump_line *selection;
24         int                 nr_asm_entries;
25         int                 nr_entries;
26         bool                hide_src_code;
27 };
28
29 struct objdump_line_rb_node {
30         struct rb_node  rb_node;
31         double          percent;
32         u32             idx;
33         int             idx_asm;
34 };
35
36 static inline
37 struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
38 {
39         return (struct objdump_line_rb_node *)(self + 1);
40 }
41
42 static 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
54 static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
55 {
56         struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
57         struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
58         bool current_entry = ui_browser__is_current_entry(self, row);
59         int width = self->width;
60
61         if (ol->offset != -1) {
62                 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
63                 ui_browser__set_percent_color(self, olrb->percent, current_entry);
64                 slsmg_printf(" %7.2f ", olrb->percent);
65         } else {
66                 ui_browser__set_percent_color(self, 0, current_entry);
67                 slsmg_write_nstring(" ", 9);
68         }
69
70         SLsmg_write_char(':');
71         slsmg_write_nstring(" ", 8);
72
73         /* The scroll bar isn't being used */
74         if (!self->navkeypressed)
75                 width += 1;
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                 ui_browser__set_color(self, HE_COLORSET_CODE);
84         else
85                 ab->selection = ol;
86 }
87
88 static double objdump_line__calc_percent(struct objdump_line *self,
89                                          struct symbol *sym, int evidx)
90 {
91         double percent = 0.0;
92
93         if (self->offset != -1) {
94                 int len = sym->end - sym->start;
95                 unsigned int hits = 0;
96                 struct annotation *notes = symbol__annotation(sym);
97                 struct source_line *src_line = notes->src->lines;
98                 struct sym_hist *h = annotation__histogram(notes, evidx);
99                 s64 offset = self->offset;
100                 struct objdump_line *next;
101
102                 next = objdump__get_next_ip_line(&notes->src->source, self);
103                 while (offset < (s64)len &&
104                        (next == NULL || offset < next->offset)) {
105                         if (src_line) {
106                                 percent += src_line[offset].percent;
107                         } else
108                                 hits += h->addr[offset];
109
110                         ++offset;
111                 }
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)
117                         percent = 100.0 * hits / h->sum;
118         }
119
120         return percent;
121 }
122
123 static 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);
140 }
141
142 static 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
166 static void annotate_browser__calc_percent(struct annotate_browser *browser,
167                                            int evidx)
168 {
169         struct map_symbol *ms = browser->b.priv;
170         struct symbol *sym = ms->sym;
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
192 static 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
231 static int annotate_browser__run(struct annotate_browser *self, int evidx,
232                                  int nr_events, void(*timer)(void *arg),
233                                  void *arg, int delay_secs)
234 {
235         struct rb_node *nd = NULL;
236         struct map_symbol *ms = self->b.priv;
237         struct symbol *sym = ms->sym;
238         const char *help = "<-, ESC: exit, TAB/shift+TAB: cycle hottest lines, "
239                            "H: Hottest, -> Line action, S -> Toggle source "
240                            "code view";
241         int key;
242
243         if (ui_browser__show(&self->b, sym->name, help) < 0)
244                 return -1;
245
246         annotate_browser__calc_percent(self, evidx);
247
248         if (self->curr_hot)
249                 annotate_browser__set_top(self, self->curr_hot);
250
251         nd = self->curr_hot;
252
253         while (1) {
254                 key = ui_browser__run(&self->b, delay_secs);
255
256                 if (delay_secs != 0) {
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
267                 switch (key) {
268                 case -1:
269                         /*
270                          * FIXME we need to check if it was
271                          * es.reason == NEWT_EXIT_TIMER
272                          */
273                         if (timer != NULL)
274                                 timer(arg);
275
276                         if (delay_secs != 0)
277                                 symbol__annotate_decay_histogram(sym, evidx);
278                         continue;
279                 case NEWT_KEY_TAB:
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;
286                         break;
287                 case NEWT_KEY_UNTAB:
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;
297                         break;
298                 case 'S':
299                         if (annotate_browser__toggle_source(self))
300                                 ui_helpline__puts(help);
301                         continue;
302                 case NEWT_KEY_ENTER:
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 {
313                                 char *s = strstr(self->selection->line, "callq ");
314                                 struct annotation *notes;
315                                 struct symbol *target;
316                                 u64 ip;
317
318                                 if (s == NULL) {
319                                         ui_helpline__puts("Actions are only available for the 'callq' instruction.");
320                                         continue;
321                                 }
322
323                                 s = strchr(s, ' ');
324                                 if (s++ == NULL) {
325                                         ui_helpline__puts("Invallid callq instruction.");
326                                         continue;
327                                 }
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);
332                                 if (target == NULL) {
333                                         ui_helpline__puts("The called function was not found.");
334                                         continue;
335                                 }
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                         continue;
353                 case NEWT_KEY_LEFT:
354                 case NEWT_KEY_ESCAPE:
355                 case 'q':
356                 case CTRL('c'):
357                         goto out;
358                 default:
359                         continue;
360                 }
361
362                 if (nd != NULL)
363                         annotate_browser__set_top(self, nd);
364         }
365 out:
366         ui_browser__hide(&self->b);
367         return key;
368 }
369
370 int hist_entry__tui_annotate(struct hist_entry *he, int evidx, int nr_events,
371                              void(*timer)(void *arg), void *arg, int delay_secs)
372 {
373         return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx, nr_events,
374                                     timer, arg, delay_secs);
375 }
376
377 int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
378                          int nr_events, void(*timer)(void *arg), void *arg,
379                          int delay_secs)
380 {
381         struct objdump_line *pos, *n;
382         struct annotation *notes;
383         struct map_symbol ms = {
384                 .map = map,
385                 .sym = sym,
386         };
387         struct annotate_browser browser = {
388                 .b = {
389                         .refresh = ui_browser__list_head_refresh,
390                         .seek    = ui_browser__list_head_seek,
391                         .write   = annotate_browser__write,
392                         .filter  = objdump_line__filter,
393                         .priv    = &ms,
394                         .use_navkeypressed = true,
395                 },
396         };
397         int ret;
398
399         if (sym == NULL)
400                 return -1;
401
402         if (map->dso->annotate_warned)
403                 return -1;
404
405         if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
406                 ui__error_window(ui_helpline__last_msg);
407                 return -1;
408         }
409
410         ui_helpline__push("Press <- or ESC to exit");
411
412         notes = symbol__annotation(sym);
413
414         list_for_each_entry(pos, &notes->src->source, node) {
415                 struct objdump_line_rb_node *rbpos;
416                 size_t line_len = strlen(pos->line);
417
418                 if (browser.b.width < line_len)
419                         browser.b.width = line_len;
420                 rbpos = objdump_line__rb(pos);
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;
426         }
427
428         browser.b.nr_entries = browser.nr_entries;
429         browser.b.entries = &notes->src->source,
430         browser.b.width += 18; /* Percentage */
431         ret = annotate_browser__run(&browser, evidx, nr_events,
432                                     timer, arg, delay_secs);
433         list_for_each_entry_safe(pos, n, &notes->src->source, node) {
434                 list_del(&pos->node);
435                 objdump_line__free(pos);
436         }
437         return ret;
438 }