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