]> git.karo-electronics.de Git - mv-sheeva.git/blob - tools/perf/util/ui/browsers/hists.c
perf hists browser: Init the has_children fields just once
[mv-sheeva.git] / tools / perf / util / ui / browsers / hists.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #undef _GNU_SOURCE
4 #include "../libslang.h"
5 #include <stdlib.h>
6 #include <string.h>
7 #include <newt.h>
8 #include <linux/rbtree.h>
9
10 #include "../../hist.h"
11 #include "../../pstack.h"
12 #include "../../sort.h"
13 #include "../../util.h"
14
15 #include "../browser.h"
16 #include "../helpline.h"
17 #include "../util.h"
18 #include "map.h"
19
20 struct hist_browser {
21         struct ui_browser   b;
22         struct hists        *hists;
23         struct hist_entry   *he_selection;
24         struct map_symbol   *selection;
25 };
26
27 static void hist_browser__refresh_dimensions(struct hist_browser *self)
28 {
29         /* 3 == +/- toggle symbol before actual hist_entry rendering */
30         self->b.width = 3 + (hists__sort_list_width(self->hists) +
31                              sizeof("[k]"));
32 }
33
34 static void hist_browser__reset(struct hist_browser *self)
35 {
36         self->b.nr_entries = self->hists->nr_entries;
37         hist_browser__refresh_dimensions(self);
38         ui_browser__reset_index(&self->b);
39 }
40
41 static char tree__folded_sign(bool unfolded)
42 {
43         return unfolded ? '-' : '+';
44 }
45
46 static char map_symbol__folded(const struct map_symbol *self)
47 {
48         return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
49 }
50
51 static char hist_entry__folded(const struct hist_entry *self)
52 {
53         return map_symbol__folded(&self->ms);
54 }
55
56 static char callchain_list__folded(const struct callchain_list *self)
57 {
58         return map_symbol__folded(&self->ms);
59 }
60
61 static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
62 {
63         int n = 0;
64         struct rb_node *nd;
65
66         for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
67                 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
68                 struct callchain_list *chain;
69                 char folded_sign = ' '; /* No children */
70
71                 list_for_each_entry(chain, &child->val, list) {
72                         ++n;
73                         /* We need this because we may not have children */
74                         folded_sign = callchain_list__folded(chain);
75                         if (folded_sign == '+')
76                                 break;
77                 }
78
79                 if (folded_sign == '-') /* Have children and they're unfolded */
80                         n += callchain_node__count_rows_rb_tree(child);
81         }
82
83         return n;
84 }
85
86 static int callchain_node__count_rows(struct callchain_node *node)
87 {
88         struct callchain_list *chain;
89         bool unfolded = false;
90         int n = 0;
91
92         list_for_each_entry(chain, &node->val, list) {
93                 ++n;
94                 unfolded = chain->ms.unfolded;
95         }
96
97         if (unfolded)
98                 n += callchain_node__count_rows_rb_tree(node);
99
100         return n;
101 }
102
103 static int callchain__count_rows(struct rb_root *chain)
104 {
105         struct rb_node *nd;
106         int n = 0;
107
108         for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
109                 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
110                 n += callchain_node__count_rows(node);
111         }
112
113         return n;
114 }
115
116 static bool map_symbol__toggle_fold(struct map_symbol *self)
117 {
118         if (!self->has_children)
119                 return false;
120
121         self->unfolded = !self->unfolded;
122         return true;
123 }
124
125 static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
126 {
127         struct rb_node *nd = rb_first(&self->rb_root);
128
129         for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
130                 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
131                 struct callchain_list *chain;
132                 bool first = true;
133
134                 list_for_each_entry(chain, &child->val, list) {
135                         if (first) {
136                                 first = false;
137                                 chain->ms.has_children = chain->list.next != &child->val ||
138                                                          !RB_EMPTY_ROOT(&child->rb_root);
139                         } else
140                                 chain->ms.has_children = chain->list.next == &child->val &&
141                                                          !RB_EMPTY_ROOT(&child->rb_root);
142                 }
143
144                 callchain_node__init_have_children_rb_tree(child);
145         }
146 }
147
148 static void callchain_node__init_have_children(struct callchain_node *self)
149 {
150         struct callchain_list *chain;
151
152         list_for_each_entry(chain, &self->val, list)
153                 chain->ms.has_children = !RB_EMPTY_ROOT(&self->rb_root);
154
155         callchain_node__init_have_children_rb_tree(self);
156 }
157
158 static void callchain__init_have_children(struct rb_root *self)
159 {
160         struct rb_node *nd;
161
162         for (nd = rb_first(self); nd; nd = rb_next(nd)) {
163                 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
164                 callchain_node__init_have_children(node);
165         }
166 }
167
168 static void hist_entry__init_have_children(struct hist_entry *self)
169 {
170         if (!self->init_have_children) {
171                 self->ms.has_children = !RB_EMPTY_ROOT(&self->sorted_chain);
172                 callchain__init_have_children(&self->sorted_chain);
173                 self->init_have_children = true;
174         }
175 }
176
177 static bool hist_browser__toggle_fold(struct hist_browser *self)
178 {
179         if (map_symbol__toggle_fold(self->selection)) {
180                 struct hist_entry *he = self->he_selection;
181
182                 hist_entry__init_have_children(he);
183                 self->hists->nr_entries -= he->nr_rows;
184
185                 if (he->ms.unfolded)
186                         he->nr_rows = callchain__count_rows(&he->sorted_chain);
187                 else
188                         he->nr_rows = 0;
189                 self->hists->nr_entries += he->nr_rows;
190                 self->b.nr_entries = self->hists->nr_entries;
191
192                 return true;
193         }
194
195         /* If it doesn't have children, no toggling performed */
196         return false;
197 }
198
199 static int hist_browser__run(struct hist_browser *self, const char *title)
200 {
201         int key;
202         int exit_keys[] = { 'a', '?', 'h', 'd', 'D', 't', NEWT_KEY_ENTER,
203                             NEWT_KEY_RIGHT, NEWT_KEY_LEFT, 0, };
204         char str[256], unit;
205         unsigned long nr_events = self->hists->stats.nr_events[PERF_RECORD_SAMPLE];
206
207         self->b.entries = &self->hists->entries;
208         self->b.nr_entries = self->hists->nr_entries;
209
210         hist_browser__refresh_dimensions(self);
211
212         nr_events = convert_unit(nr_events, &unit);
213         snprintf(str, sizeof(str), "Events: %lu%c                            ",
214                  nr_events, unit);
215         newtDrawRootText(0, 0, str);
216
217         if (ui_browser__show(&self->b, title,
218                              "Press '?' for help on key bindings") < 0)
219                 return -1;
220
221         ui_browser__add_exit_keys(&self->b, exit_keys);
222
223         while (1) {
224                 key = ui_browser__run(&self->b);
225
226                 switch (key) {
227                 case 'D': { /* Debug */
228                         static int seq;
229                         struct hist_entry *h = rb_entry(self->b.top,
230                                                         struct hist_entry, rb_node);
231                         ui_helpline__pop();
232                         ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
233                                            seq++, self->b.nr_entries,
234                                            self->hists->nr_entries,
235                                            self->b.height,
236                                            self->b.index,
237                                            self->b.top_idx,
238                                            h->row_offset, h->nr_rows);
239                 }
240                         continue;
241                 case NEWT_KEY_ENTER:
242                         if (hist_browser__toggle_fold(self))
243                                 break;
244                         /* fall thru */
245                 default:
246                         goto out;
247                 }
248         }
249 out:
250         ui_browser__hide(&self->b);
251         return key;
252 }
253
254 static char *callchain_list__sym_name(struct callchain_list *self,
255                                       char *bf, size_t bfsize)
256 {
257         if (self->ms.sym)
258                 return self->ms.sym->name;
259
260         snprintf(bf, bfsize, "%#Lx", self->ip);
261         return bf;
262 }
263
264 #define LEVEL_OFFSET_STEP 3
265
266 static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
267                                                      struct callchain_node *chain_node,
268                                                      u64 total, int level,
269                                                      unsigned short row,
270                                                      off_t *row_offset,
271                                                      bool *is_current_entry)
272 {
273         struct rb_node *node;
274         int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
275         u64 new_total, remaining;
276
277         if (callchain_param.mode == CHAIN_GRAPH_REL)
278                 new_total = chain_node->children_hit;
279         else
280                 new_total = total;
281
282         remaining = new_total;
283         node = rb_first(&chain_node->rb_root);
284         while (node) {
285                 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
286                 struct rb_node *next = rb_next(node);
287                 u64 cumul = cumul_hits(child);
288                 struct callchain_list *chain;
289                 char folded_sign = ' ';
290                 int first = true;
291                 int extra_offset = 0;
292
293                 remaining -= cumul;
294
295                 list_for_each_entry(chain, &child->val, list) {
296                         char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
297                         const char *str;
298                         int color;
299                         bool was_first = first;
300
301                         if (first)
302                                 first = false;
303                         else
304                                 extra_offset = LEVEL_OFFSET_STEP;
305
306                         folded_sign = callchain_list__folded(chain);
307                         if (*row_offset != 0) {
308                                 --*row_offset;
309                                 goto do_next;
310                         }
311
312                         alloc_str = NULL;
313                         str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
314                         if (was_first) {
315                                 double percent = cumul * 100.0 / new_total;
316
317                                 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
318                                         str = "Not enough memory!";
319                                 else
320                                         str = alloc_str;
321                         }
322
323                         color = HE_COLORSET_NORMAL;
324                         width = self->b.width - (offset + extra_offset + 2);
325                         if (ui_browser__is_current_entry(&self->b, row)) {
326                                 self->selection = &chain->ms;
327                                 color = HE_COLORSET_SELECTED;
328                                 *is_current_entry = true;
329                         }
330
331                         ui_browser__set_color(&self->b, color);
332                         ui_browser__gotorc(&self->b, row, 0);
333                         slsmg_write_nstring(" ", offset + extra_offset);
334                         slsmg_printf("%c ", folded_sign);
335                         slsmg_write_nstring(str, width);
336                         free(alloc_str);
337
338                         if (++row == self->b.height)
339                                 goto out;
340 do_next:
341                         if (folded_sign == '+')
342                                 break;
343                 }
344
345                 if (folded_sign == '-') {
346                         const int new_level = level + (extra_offset ? 2 : 1);
347                         row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
348                                                                          new_level, row, row_offset,
349                                                                          is_current_entry);
350                 }
351                 if (row == self->b.height)
352                         goto out;
353                 node = next;
354         }
355 out:
356         return row - first_row;
357 }
358
359 static int hist_browser__show_callchain_node(struct hist_browser *self,
360                                              struct callchain_node *node,
361                                              int level, unsigned short row,
362                                              off_t *row_offset,
363                                              bool *is_current_entry)
364 {
365         struct callchain_list *chain;
366         int first_row = row,
367              offset = level * LEVEL_OFFSET_STEP,
368              width = self->b.width - offset;
369         char folded_sign = ' ';
370
371         list_for_each_entry(chain, &node->val, list) {
372                 char ipstr[BITS_PER_LONG / 4 + 1], *s;
373                 int color;
374
375                 folded_sign = callchain_list__folded(chain);
376
377                 if (*row_offset != 0) {
378                         --*row_offset;
379                         continue;
380                 }
381
382                 color = HE_COLORSET_NORMAL;
383                 if (ui_browser__is_current_entry(&self->b, row)) {
384                         self->selection = &chain->ms;
385                         color = HE_COLORSET_SELECTED;
386                         *is_current_entry = true;
387                 }
388
389                 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
390                 ui_browser__gotorc(&self->b, row, 0);
391                 ui_browser__set_color(&self->b, color);
392                 slsmg_write_nstring(" ", offset);
393                 slsmg_printf("%c ", folded_sign);
394                 slsmg_write_nstring(s, width - 2);
395
396                 if (++row == self->b.height)
397                         goto out;
398         }
399
400         if (folded_sign == '-')
401                 row += hist_browser__show_callchain_node_rb_tree(self, node,
402                                                                  self->hists->stats.total_period,
403                                                                  level + 1, row,
404                                                                  row_offset,
405                                                                  is_current_entry);
406 out:
407         return row - first_row;
408 }
409
410 static int hist_browser__show_callchain(struct hist_browser *self,
411                                         struct rb_root *chain,
412                                         int level, unsigned short row,
413                                         off_t *row_offset,
414                                         bool *is_current_entry)
415 {
416         struct rb_node *nd;
417         int first_row = row;
418
419         for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
420                 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
421
422                 row += hist_browser__show_callchain_node(self, node, level,
423                                                          row, row_offset,
424                                                          is_current_entry);
425                 if (row == self->b.height)
426                         break;
427         }
428
429         return row - first_row;
430 }
431
432 static int hist_browser__show_entry(struct hist_browser *self,
433                                     struct hist_entry *entry,
434                                     unsigned short row)
435 {
436         char s[256];
437         double percent;
438         int printed = 0;
439         int color, width = self->b.width;
440         char folded_sign = ' ';
441         bool current_entry = ui_browser__is_current_entry(&self->b, row);
442         off_t row_offset = entry->row_offset;
443
444         if (current_entry) {
445                 self->he_selection = entry;
446                 self->selection = &entry->ms;
447         }
448
449         if (symbol_conf.use_callchain) {
450                 hist_entry__init_have_children(entry);
451                 folded_sign = hist_entry__folded(entry);
452         }
453
454         if (row_offset == 0) {
455                 hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false,
456                                      0, false, self->hists->stats.total_period);
457                 percent = (entry->period * 100.0) / self->hists->stats.total_period;
458
459                 color = HE_COLORSET_SELECTED;
460                 if (!current_entry) {
461                         if (percent >= MIN_RED)
462                                 color = HE_COLORSET_TOP;
463                         else if (percent >= MIN_GREEN)
464                                 color = HE_COLORSET_MEDIUM;
465                         else
466                                 color = HE_COLORSET_NORMAL;
467                 }
468
469                 ui_browser__set_color(&self->b, color);
470                 ui_browser__gotorc(&self->b, row, 0);
471                 if (symbol_conf.use_callchain) {
472                         slsmg_printf("%c ", folded_sign);
473                         width -= 2;
474                 }
475                 slsmg_write_nstring(s, width);
476                 ++row;
477                 ++printed;
478         } else
479                 --row_offset;
480
481         if (folded_sign == '-' && row != self->b.height) {
482                 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
483                                                         1, row, &row_offset,
484                                                         &current_entry);
485                 if (current_entry)
486                         self->he_selection = entry;
487         }
488
489         return printed;
490 }
491
492 static unsigned int hist_browser__refresh(struct ui_browser *self)
493 {
494         unsigned row = 0;
495         struct rb_node *nd;
496         struct hist_browser *hb = container_of(self, struct hist_browser, b);
497
498         if (self->top == NULL)
499                 self->top = rb_first(&hb->hists->entries);
500
501         for (nd = self->top; nd; nd = rb_next(nd)) {
502                 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
503
504                 if (h->filtered)
505                         continue;
506
507                 row += hist_browser__show_entry(hb, h, row);
508                 if (row == self->height)
509                         break;
510         }
511
512         return row;
513 }
514
515 static struct rb_node *hists__filter_entries(struct rb_node *nd)
516 {
517         while (nd != NULL) {
518                 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
519                 if (!h->filtered)
520                         return nd;
521
522                 nd = rb_next(nd);
523         }
524
525         return NULL;
526 }
527
528 static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
529 {
530         while (nd != NULL) {
531                 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
532                 if (!h->filtered)
533                         return nd;
534
535                 nd = rb_prev(nd);
536         }
537
538         return NULL;
539 }
540
541 static void ui_browser__hists_seek(struct ui_browser *self,
542                                    off_t offset, int whence)
543 {
544         struct hist_entry *h;
545         struct rb_node *nd;
546         bool first = true;
547
548         switch (whence) {
549         case SEEK_SET:
550                 nd = hists__filter_entries(rb_first(self->entries));
551                 break;
552         case SEEK_CUR:
553                 nd = self->top;
554                 goto do_offset;
555         case SEEK_END:
556                 nd = hists__filter_prev_entries(rb_last(self->entries));
557                 first = false;
558                 break;
559         default:
560                 return;
561         }
562
563         /*
564          * Moves not relative to the first visible entry invalidates its
565          * row_offset:
566          */
567         h = rb_entry(self->top, struct hist_entry, rb_node);
568         h->row_offset = 0;
569
570         /*
571          * Here we have to check if nd is expanded (+), if it is we can't go
572          * the next top level hist_entry, instead we must compute an offset of
573          * what _not_ to show and not change the first visible entry.
574          *
575          * This offset increments when we are going from top to bottom and
576          * decreases when we're going from bottom to top.
577          *
578          * As we don't have backpointers to the top level in the callchains
579          * structure, we need to always print the whole hist_entry callchain,
580          * skipping the first ones that are before the first visible entry
581          * and stop when we printed enough lines to fill the screen.
582          */
583 do_offset:
584         if (offset > 0) {
585                 do {
586                         h = rb_entry(nd, struct hist_entry, rb_node);
587                         if (h->ms.unfolded) {
588                                 u16 remaining = h->nr_rows - h->row_offset;
589                                 if (offset > remaining) {
590                                         offset -= remaining;
591                                         h->row_offset = 0;
592                                 } else {
593                                         h->row_offset += offset;
594                                         offset = 0;
595                                         self->top = nd;
596                                         break;
597                                 }
598                         }
599                         nd = hists__filter_entries(rb_next(nd));
600                         if (nd == NULL)
601                                 break;
602                         --offset;
603                         self->top = nd;
604                 } while (offset != 0);
605         } else if (offset < 0) {
606                 while (1) {
607                         h = rb_entry(nd, struct hist_entry, rb_node);
608                         if (h->ms.unfolded) {
609                                 if (first) {
610                                         if (-offset > h->row_offset) {
611                                                 offset += h->row_offset;
612                                                 h->row_offset = 0;
613                                         } else {
614                                                 h->row_offset += offset;
615                                                 offset = 0;
616                                                 self->top = nd;
617                                                 break;
618                                         }
619                                 } else {
620                                         if (-offset > h->nr_rows) {
621                                                 offset += h->nr_rows;
622                                                 h->row_offset = 0;
623                                         } else {
624                                                 h->row_offset = h->nr_rows + offset;
625                                                 offset = 0;
626                                                 self->top = nd;
627                                                 break;
628                                         }
629                                 }
630                         }
631
632                         nd = hists__filter_prev_entries(rb_prev(nd));
633                         if (nd == NULL)
634                                 break;
635                         ++offset;
636                         self->top = nd;
637                         if (offset == 0) {
638                                 /*
639                                  * Last unfiltered hist_entry, check if it is
640                                  * unfolded, if it is then we should have
641                                  * row_offset at its last entry.
642                                  */
643                                 h = rb_entry(nd, struct hist_entry, rb_node);
644                                 if (h->ms.unfolded)
645                                         h->row_offset = h->nr_rows;
646                                 break;
647                         }
648                         first = false;
649                 }
650         } else {
651                 self->top = nd;
652                 h = rb_entry(nd, struct hist_entry, rb_node);
653                 h->row_offset = 0;
654         }
655 }
656
657 static struct hist_browser *hist_browser__new(struct hists *hists)
658 {
659         struct hist_browser *self = zalloc(sizeof(*self));
660
661         if (self) {
662                 self->hists = hists;
663                 self->b.refresh = hist_browser__refresh;
664                 self->b.seek = ui_browser__hists_seek;
665         }
666
667         return self;
668 }
669
670 static void hist_browser__delete(struct hist_browser *self)
671 {
672         free(self);
673 }
674
675 static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
676 {
677         return self->he_selection;
678 }
679
680 static struct thread *hist_browser__selected_thread(struct hist_browser *self)
681 {
682         return self->he_selection->thread;
683 }
684
685 static int hist_browser__title(char *bf, size_t size, const char *ev_name,
686                                const struct dso *dso, const struct thread *thread)
687 {
688         int printed = 0;
689
690         if (thread)
691                 printed += snprintf(bf + printed, size - printed,
692                                     "Thread: %s(%d)",
693                                     (thread->comm_set ?  thread->comm : ""),
694                                     thread->pid);
695         if (dso)
696                 printed += snprintf(bf + printed, size - printed,
697                                     "%sDSO: %s", thread ? " " : "",
698                                     dso->short_name);
699         return printed ?: snprintf(bf, size, "Event: %s", ev_name);
700 }
701
702 int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
703 {
704         struct hist_browser *browser = hist_browser__new(self);
705         struct pstack *fstack;
706         const struct thread *thread_filter = NULL;
707         const struct dso *dso_filter = NULL;
708         char msg[160];
709         int key = -1;
710
711         if (browser == NULL)
712                 return -1;
713
714         fstack = pstack__new(2);
715         if (fstack == NULL)
716                 goto out;
717
718         ui_helpline__push(helpline);
719
720         hist_browser__title(msg, sizeof(msg), ev_name,
721                             dso_filter, thread_filter);
722
723         while (1) {
724                 const struct thread *thread;
725                 const struct dso *dso;
726                 char *options[16];
727                 int nr_options = 0, choice = 0, i,
728                     annotate = -2, zoom_dso = -2, zoom_thread = -2,
729                     browse_map = -2;
730
731                 key = hist_browser__run(browser, msg);
732
733                 thread = hist_browser__selected_thread(browser);
734                 dso = browser->selection->map ? browser->selection->map->dso : NULL;
735
736                 switch (key) {
737                 case NEWT_KEY_F1:
738                         goto do_help;
739                 case NEWT_KEY_TAB:
740                 case NEWT_KEY_UNTAB:
741                         /*
742                          * Exit the browser, let hists__browser_tree
743                          * go to the next or previous
744                          */
745                         goto out_free_stack;
746                 case 'a':
747                         if (browser->selection->map == NULL &&
748                             browser->selection->map->dso->annotate_warned)
749                                 continue;
750                         goto do_annotate;
751                 case 'd':
752                         goto zoom_dso;
753                 case 't':
754                         goto zoom_thread;
755                 case 'h':
756                 case '?':
757 do_help:
758                         ui__help_window("->        Zoom into DSO/Threads & Annotate current symbol\n"
759                                         "<-        Zoom out\n"
760                                         "a         Annotate current symbol\n"
761                                         "h/?/F1    Show this window\n"
762                                         "d         Zoom into current DSO\n"
763                                         "t         Zoom into current Thread\n"
764                                         "q/CTRL+C  Exit browser");
765                         continue;
766                 case NEWT_KEY_ENTER:
767                 case NEWT_KEY_RIGHT:
768                         /* menu */
769                         break;
770                 case NEWT_KEY_LEFT: {
771                         const void *top;
772
773                         if (pstack__empty(fstack))
774                                 continue;
775                         top = pstack__pop(fstack);
776                         if (top == &dso_filter)
777                                 goto zoom_out_dso;
778                         if (top == &thread_filter)
779                                 goto zoom_out_thread;
780                         continue;
781                 }
782                 case NEWT_KEY_ESCAPE:
783                         if (!ui__dialog_yesno("Do you really want to exit?"))
784                                 continue;
785                         /* Fall thru */
786                 default:
787                         goto out_free_stack;
788                 }
789
790                 if (browser->selection->sym != NULL &&
791                     !browser->selection->map->dso->annotate_warned &&
792                     asprintf(&options[nr_options], "Annotate %s",
793                              browser->selection->sym->name) > 0)
794                         annotate = nr_options++;
795
796                 if (thread != NULL &&
797                     asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
798                              (thread_filter ? "out of" : "into"),
799                              (thread->comm_set ? thread->comm : ""),
800                              thread->pid) > 0)
801                         zoom_thread = nr_options++;
802
803                 if (dso != NULL &&
804                     asprintf(&options[nr_options], "Zoom %s %s DSO",
805                              (dso_filter ? "out of" : "into"),
806                              (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
807                         zoom_dso = nr_options++;
808
809                 if (browser->selection->map != NULL &&
810                     asprintf(&options[nr_options], "Browse map details") > 0)
811                         browse_map = nr_options++;
812
813                 options[nr_options++] = (char *)"Exit";
814
815                 choice = ui__popup_menu(nr_options, options);
816
817                 for (i = 0; i < nr_options - 1; ++i)
818                         free(options[i]);
819
820                 if (choice == nr_options - 1)
821                         break;
822
823                 if (choice == -1)
824                         continue;
825
826                 if (choice == annotate) {
827                         struct hist_entry *he;
828 do_annotate:
829                         if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
830                                 browser->selection->map->dso->annotate_warned = 1;
831                                 ui_helpline__puts("No vmlinux file found, can't "
832                                                  "annotate with just a "
833                                                  "kallsyms file");
834                                 continue;
835                         }
836
837                         he = hist_browser__selected_entry(browser);
838                         if (he == NULL)
839                                 continue;
840
841                         hist_entry__tui_annotate(he);
842                 } else if (choice == browse_map)
843                         map__browse(browser->selection->map);
844                 else if (choice == zoom_dso) {
845 zoom_dso:
846                         if (dso_filter) {
847                                 pstack__remove(fstack, &dso_filter);
848 zoom_out_dso:
849                                 ui_helpline__pop();
850                                 dso_filter = NULL;
851                         } else {
852                                 if (dso == NULL)
853                                         continue;
854                                 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
855                                                    dso->kernel ? "the Kernel" : dso->short_name);
856                                 dso_filter = dso;
857                                 pstack__push(fstack, &dso_filter);
858                         }
859                         hists__filter_by_dso(self, dso_filter);
860                         hist_browser__title(msg, sizeof(msg), ev_name,
861                                             dso_filter, thread_filter);
862                         hist_browser__reset(browser);
863                 } else if (choice == zoom_thread) {
864 zoom_thread:
865                         if (thread_filter) {
866                                 pstack__remove(fstack, &thread_filter);
867 zoom_out_thread:
868                                 ui_helpline__pop();
869                                 thread_filter = NULL;
870                         } else {
871                                 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
872                                                    thread->comm_set ? thread->comm : "",
873                                                    thread->pid);
874                                 thread_filter = thread;
875                                 pstack__push(fstack, &thread_filter);
876                         }
877                         hists__filter_by_thread(self, thread_filter);
878                         hist_browser__title(msg, sizeof(msg), ev_name,
879                                             dso_filter, thread_filter);
880                         hist_browser__reset(browser);
881                 }
882         }
883 out_free_stack:
884         pstack__delete(fstack);
885 out:
886         hist_browser__delete(browser);
887         return key;
888 }
889
890 int hists__tui_browse_tree(struct rb_root *self, const char *help)
891 {
892         struct rb_node *first = rb_first(self), *nd = first, *next;
893         int key = 0;
894
895         while (nd) {
896                 struct hists *hists = rb_entry(nd, struct hists, rb_node);
897                 const char *ev_name = __event_name(hists->type, hists->config);
898
899                 key = hists__browse(hists, help, ev_name);
900                 switch (key) {
901                 case NEWT_KEY_TAB:
902                         next = rb_next(nd);
903                         if (next)
904                                 nd = next;
905                         break;
906                 case NEWT_KEY_UNTAB:
907                         if (nd == first)
908                                 continue;
909                         nd = rb_prev(nd);
910                 default:
911                         return key;
912                 }
913         }
914
915         return key;
916 }