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