8 #include <sys/ttydefaults.h>
17 newtComponent form, scale;
20 struct ui_progress *ui_progress__new(const char *title, u64 total)
22 struct ui_progress *self = malloc(sizeof(*self));
26 newtGetScreenSize(&cols, NULL);
28 newtCenteredWindow(cols, 1, title);
29 self->form = newtForm(NULL, NULL, 0);
30 if (self->form == NULL)
32 self->scale = newtScale(0, 0, cols, total);
33 if (self->scale == NULL)
35 newtFormAddComponent(self->form, self->scale);
42 newtFormDestroy(self->form);
48 void ui_progress__update(struct ui_progress *self, u64 curr)
50 newtScaleSet(self->scale, curr);
54 void ui_progress__delete(struct ui_progress *self)
56 newtFormDestroy(self->form);
61 static void ui_helpline__pop(void)
66 static void ui_helpline__push(const char *msg)
68 newtPushHelpLine(msg);
71 static void ui_helpline__vpush(const char *fmt, va_list ap)
75 if (vasprintf(&s, fmt, ap) < 0)
76 vfprintf(stderr, fmt, ap);
83 static void ui_helpline__fpush(const char *fmt, ...)
88 ui_helpline__vpush(fmt, ap);
92 static void ui_helpline__puts(const char *msg)
95 ui_helpline__push(msg);
98 static char browser__last_msg[1024];
100 int browser__show_help(const char *format, va_list ap)
105 ret = vsnprintf(browser__last_msg + backlog,
106 sizeof(browser__last_msg) - backlog, format, ap);
109 if (browser__last_msg[backlog - 1] == '\n') {
110 ui_helpline__puts(browser__last_msg);
118 static void newt_form__set_exit_keys(newtComponent self)
120 newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
121 newtFormAddHotKey(self, 'Q');
122 newtFormAddHotKey(self, 'q');
123 newtFormAddHotKey(self, CTRL('c'));
126 static newtComponent newt_form__new(void)
128 newtComponent self = newtForm(NULL, NULL, 0);
130 newt_form__set_exit_keys(self);
134 static int popup_menu(int argc, char * const argv[])
136 struct newtExitStruct es;
137 int i, rc = -1, max_len = 5;
138 newtComponent listbox, form = newt_form__new();
143 listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
145 goto out_destroy_form;
147 newtFormAddComponent(form, listbox);
149 for (i = 0; i < argc; ++i) {
150 int len = strlen(argv[i]);
153 if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
154 goto out_destroy_form;
157 newtCenteredWindow(max_len, argc, NULL);
158 newtFormRun(form, &es);
159 rc = newtListboxGetCurrent(listbox) - NULL;
160 if (es.reason == NEWT_EXIT_HOTKEY)
164 newtFormDestroy(form);
168 static bool dialog_yesno(const char *msg)
170 /* newtWinChoice should really be accepting const char pointers... */
171 char yes[] = "Yes", no[] = "No";
172 return newtWinChoice(NULL, yes, no, (char *)msg) == 1;
175 #define HE_COLORSET_TOP 50
176 #define HE_COLORSET_MEDIUM 51
177 #define HE_COLORSET_NORMAL 52
178 #define HE_COLORSET_SELECTED 53
179 #define HE_COLORSET_CODE 54
181 static int ui_browser__percent_color(double percent, bool current)
184 return HE_COLORSET_SELECTED;
185 if (percent >= MIN_RED)
186 return HE_COLORSET_TOP;
187 if (percent >= MIN_GREEN)
188 return HE_COLORSET_MEDIUM;
189 return HE_COLORSET_NORMAL;
193 newtComponent form, sb;
194 u64 index, first_visible_entry_idx;
195 void *first_visible_entry, *entries;
196 u16 top, left, width, height;
201 static void ui_browser__refresh_dimensions(struct ui_browser *self)
204 newtGetScreenSize(&cols, &rows);
206 if (self->width > cols - 4)
207 self->width = cols - 4;
208 self->height = rows - 5;
209 if (self->height > self->nr_entries)
210 self->height = self->nr_entries;
211 self->top = (rows - self->height) / 2;
212 self->left = (cols - self->width) / 2;
215 static void ui_browser__reset_index(struct ui_browser *self)
217 self->index = self->first_visible_entry_idx = 0;
218 self->first_visible_entry = NULL;
221 static int objdump_line__show(struct objdump_line *self, struct list_head *head,
222 int width, struct hist_entry *he, int len,
225 if (self->offset != -1) {
226 struct symbol *sym = he->ms.sym;
227 unsigned int hits = 0;
228 double percent = 0.0;
230 struct sym_priv *priv = symbol__priv(sym);
231 struct sym_ext *sym_ext = priv->ext;
232 struct sym_hist *h = priv->hist;
233 s64 offset = self->offset;
234 struct objdump_line *next = objdump__get_next_ip_line(head, self);
236 while (offset < (s64)len &&
237 (next == NULL || offset < next->offset)) {
239 percent += sym_ext[offset].percent;
241 hits += h->ip[offset];
246 if (sym_ext == NULL && h->sum)
247 percent = 100.0 * hits / h->sum;
249 color = ui_browser__percent_color(percent, current_entry);
250 SLsmg_set_color(color);
251 SLsmg_printf(" %7.2f ", percent);
253 SLsmg_set_color(HE_COLORSET_CODE);
255 int color = ui_browser__percent_color(0, current_entry);
256 SLsmg_set_color(color);
257 SLsmg_write_nstring(" ", 9);
260 SLsmg_write_char(':');
261 SLsmg_write_nstring(" ", 8);
263 SLsmg_write_nstring(" ", width - 18);
265 SLsmg_write_nstring(self->line, width - 18);
270 static int ui_browser__refresh_entries(struct ui_browser *self)
272 struct objdump_line *pos;
273 struct list_head *head = self->entries;
274 struct hist_entry *he = self->priv;
276 int len = he->ms.sym->end - he->ms.sym->start;
278 if (self->first_visible_entry == NULL || self->first_visible_entry == self->entries)
279 self->first_visible_entry = head->next;
281 pos = list_entry(self->first_visible_entry, struct objdump_line, node);
283 list_for_each_entry_from(pos, head, node) {
284 bool current_entry = (self->first_visible_entry_idx + row) == self->index;
285 SLsmg_gotorc(self->top + row, self->left);
286 objdump_line__show(pos, head, self->width,
287 he, len, current_entry);
288 if (++row == self->height)
292 SLsmg_set_color(HE_COLORSET_NORMAL);
293 SLsmg_fill_region(self->top + row, self->left,
294 self->height - row, self->width, ' ');
299 static int ui_browser__run(struct ui_browser *self, const char *title,
300 struct newtExitStruct *es)
303 newtFormDestroy(self->form);
307 ui_browser__refresh_dimensions(self);
308 newtCenteredWindow(self->width + 2, self->height, title);
309 self->form = newt_form__new();
310 if (self->form == NULL)
313 self->sb = newtVerticalScrollbar(self->width + 1, 0, self->height,
315 HE_COLORSET_SELECTED);
316 if (self->sb == NULL)
319 newtFormAddHotKey(self->form, NEWT_KEY_UP);
320 newtFormAddHotKey(self->form, NEWT_KEY_DOWN);
321 newtFormAddHotKey(self->form, NEWT_KEY_PGUP);
322 newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
323 newtFormAddHotKey(self->form, NEWT_KEY_HOME);
324 newtFormAddHotKey(self->form, NEWT_KEY_END);
326 if (ui_browser__refresh_entries(self) < 0)
328 newtFormAddComponent(self->form, self->sb);
333 newtFormRun(self->form, es);
335 if (es->reason != NEWT_EXIT_HOTKEY)
339 if (self->index == self->nr_entries - 1)
342 if (self->index == self->first_visible_entry_idx + self->height) {
343 struct list_head *pos = self->first_visible_entry;
344 ++self->first_visible_entry_idx;
345 self->first_visible_entry = pos->next;
349 if (self->index == 0)
352 if (self->index < self->first_visible_entry_idx) {
353 struct list_head *pos = self->first_visible_entry;
354 --self->first_visible_entry_idx;
355 self->first_visible_entry = pos->prev;
359 if (self->first_visible_entry_idx + self->height > self->nr_entries - 1)
362 offset = self->height;
363 if (self->index + offset > self->nr_entries - 1)
364 offset = self->nr_entries - 1 - self->index;
365 self->index += offset;
366 self->first_visible_entry_idx += offset;
369 struct list_head *pos = self->first_visible_entry;
370 self->first_visible_entry = pos->next;
375 if (self->first_visible_entry_idx == 0)
378 if (self->first_visible_entry_idx < self->height)
379 offset = self->first_visible_entry_idx;
381 offset = self->height;
383 self->index -= offset;
384 self->first_visible_entry_idx -= offset;
387 struct list_head *pos = self->first_visible_entry;
388 self->first_visible_entry = pos->prev;
392 ui_browser__reset_index(self);
395 struct list_head *head = self->entries;
396 offset = self->height - 1;
398 if (offset > self->nr_entries)
399 offset = self->nr_entries;
401 self->index = self->first_visible_entry_idx = self->nr_entries - 1 - offset;
402 self->first_visible_entry = head->prev;
403 while (offset-- != 0) {
404 struct list_head *pos = self->first_visible_entry;
405 self->first_visible_entry = pos->prev;
409 case NEWT_KEY_ESCAPE:
417 if (ui_browser__refresh_entries(self) < 0)
424 * When debugging newt problems it was useful to be able to "unroll"
425 * the calls to newtCheckBoxTreeAdd{Array,Item}, so that we can generate
426 * a source file with the sequence of calls to these methods, to then
427 * tweak the arrays to get the intended results, so I'm keeping this code
428 * here, may be useful again in the future.
432 static void newt_checkbox_tree__add(newtComponent tree, const char *str,
433 void *priv, int *indexes)
436 /* Print the newtCheckboxTreeAddArray to tinker with its index arrays */
437 int i = 0, len = 40 - strlen(str);
440 "\tnewtCheckboxTreeAddItem(tree, %*.*s\"%s\", (void *)%p, 0, ",
441 len, len, " ", str, priv);
442 while (indexes[i] != NEWT_ARG_LAST) {
443 if (indexes[i] != NEWT_ARG_APPEND)
444 fprintf(stderr, " %d,", indexes[i]);
446 fprintf(stderr, " %s,", "NEWT_ARG_APPEND");
449 fprintf(stderr, " %s", " NEWT_ARG_LAST);\n");
452 newtCheckboxTreeAddArray(tree, str, priv, 0, indexes);
455 static char *callchain_list__sym_name(struct callchain_list *self,
456 char *bf, size_t bfsize)
459 return self->ms.sym->name;
461 snprintf(bf, bfsize, "%#Lx", self->ip);
465 static void __callchain__append_graph_browser(struct callchain_node *self,
466 newtComponent tree, u64 total,
467 int *indexes, int depth)
469 struct rb_node *node;
470 u64 new_total, remaining;
473 if (callchain_param.mode == CHAIN_GRAPH_REL)
474 new_total = self->children_hit;
478 remaining = new_total;
479 node = rb_first(&self->rb_root);
481 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
482 struct rb_node *next = rb_next(node);
483 u64 cumul = cumul_hits(child);
484 struct callchain_list *chain;
485 int first = true, printed = 0;
489 indexes[depth] = NEWT_ARG_APPEND;
490 indexes[depth + 1] = NEWT_ARG_LAST;
492 list_for_each_entry(chain, &child->val, list) {
493 char ipstr[BITS_PER_LONG / 4 + 1],
495 const char *str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
498 double percent = cumul * 100.0 / new_total;
501 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
502 str = "Not enough memory!";
506 indexes[depth] = idx;
507 indexes[depth + 1] = NEWT_ARG_APPEND;
508 indexes[depth + 2] = NEWT_ARG_LAST;
511 newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
516 indexes[depth] = idx;
518 indexes[depth + 1] = chain_idx;
521 __callchain__append_graph_browser(child, tree, new_total, indexes,
522 depth + (chain_idx != -1 ? 2 : 1));
527 static void callchain__append_graph_browser(struct callchain_node *self,
528 newtComponent tree, u64 total,
529 int *indexes, int parent_idx)
531 struct callchain_list *chain;
534 indexes[1] = NEWT_ARG_APPEND;
535 indexes[2] = NEWT_ARG_LAST;
537 list_for_each_entry(chain, &self->val, list) {
538 char ipstr[BITS_PER_LONG / 4 + 1], *str;
540 if (chain->ip >= PERF_CONTEXT_MAX)
543 if (!i++ && sort__first_dimension == SORT_SYM)
546 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
547 newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
550 indexes[1] = parent_idx;
551 indexes[2] = NEWT_ARG_APPEND;
552 indexes[3] = NEWT_ARG_LAST;
553 __callchain__append_graph_browser(self, tree, total, indexes, 2);
556 static void hist_entry__append_callchain_browser(struct hist_entry *self,
557 newtComponent tree, u64 total, int parent_idx)
559 struct rb_node *rb_node;
560 int indexes[1024] = { [0] = parent_idx, };
562 struct callchain_node *chain;
564 rb_node = rb_first(&self->sorted_chain);
566 chain = rb_entry(rb_node, struct callchain_node, rb_node);
567 switch (callchain_param.mode) {
570 case CHAIN_GRAPH_ABS: /* falldown */
571 case CHAIN_GRAPH_REL:
572 callchain__append_graph_browser(chain, tree, total, indexes, idx++);
578 rb_node = rb_next(rb_node);
582 static size_t hist_entry__append_browser(struct hist_entry *self,
583 newtComponent tree, u64 total)
588 if (symbol_conf.exclude_other && !self->parent)
591 ret = hist_entry__snprintf(self, s, sizeof(s), NULL,
592 false, 0, false, total);
593 if (symbol_conf.use_callchain) {
596 indexes[0] = NEWT_ARG_APPEND;
597 indexes[1] = NEWT_ARG_LAST;
598 newt_checkbox_tree__add(tree, s, &self->ms, indexes);
600 newtListboxAppendEntry(tree, s, &self->ms);
605 static void hist_entry__annotate_browser(struct hist_entry *self)
607 struct ui_browser browser;
608 struct newtExitStruct es;
609 struct objdump_line *pos, *n;
612 if (self->ms.sym == NULL)
615 if (hist_entry__annotate(self, &head) < 0)
618 ui_helpline__push("Press ESC to exit");
620 memset(&browser, 0, sizeof(browser));
621 browser.entries = &head;
623 list_for_each_entry(pos, &head, node) {
624 size_t line_len = strlen(pos->line);
625 if (browser.width < line_len)
626 browser.width = line_len;
627 ++browser.nr_entries;
630 browser.width += 18; /* Percentage */
631 ui_browser__run(&browser, self->ms.sym->name, &es);
632 newtFormDestroy(browser.form);
634 list_for_each_entry_safe(pos, n, &head, node) {
635 list_del(&pos->node);
636 objdump_line__free(pos);
641 static const void *newt__symbol_tree_get_current(newtComponent self)
643 if (symbol_conf.use_callchain)
644 return newtCheckboxTreeGetCurrent(self);
645 return newtListboxGetCurrent(self);
648 static void hist_browser__selection(newtComponent self, void *data)
650 const struct map_symbol **symbol_ptr = data;
651 *symbol_ptr = newt__symbol_tree_get_current(self);
654 struct hist_browser {
655 newtComponent form, tree;
656 const struct map_symbol *selection;
659 static struct hist_browser *hist_browser__new(void)
661 struct hist_browser *self = malloc(sizeof(*self));
669 static void hist_browser__delete(struct hist_browser *self)
671 newtFormDestroy(self->form);
676 static int hist_browser__populate(struct hist_browser *self, struct hists *hists,
679 int max_len = 0, idx, cols, rows;
680 struct ui_progress *progress;
683 char seq[] = ".", unit;
685 unsigned long nr_events = hists->stats.nr_events[PERF_RECORD_SAMPLE];
688 newtFormDestroy(self->form);
692 nr_events = convert_unit(nr_events, &unit);
693 snprintf(str, sizeof(str), "Events: %lu%c ",
695 newtDrawRootText(0, 0, str);
697 newtGetScreenSize(NULL, &rows);
699 if (symbol_conf.use_callchain)
700 self->tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq,
703 self->tree = newtListbox(0, 0, rows - 5,
705 NEWT_FLAG_RETURNEXIT));
707 newtComponentAddCallback(self->tree, hist_browser__selection,
710 progress = ui_progress__new("Adding entries to the browser...",
712 if (progress == NULL)
716 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
717 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
723 len = hist_entry__append_browser(h, self->tree, hists->stats.total_period);
726 if (symbol_conf.use_callchain)
727 hist_entry__append_callchain_browser(h, self->tree,
728 hists->stats.total_period, idx++);
731 ui_progress__update(progress, curr_hist);
734 ui_progress__delete(progress);
736 newtGetScreenSize(&cols, &rows);
741 if (!symbol_conf.use_callchain)
742 newtListboxSetWidth(self->tree, max_len);
744 newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0),
746 self->form = newt_form__new();
747 if (self->form == NULL)
750 newtFormAddHotKey(self->form, 'A');
751 newtFormAddHotKey(self->form, 'a');
752 newtFormAddHotKey(self->form, NEWT_KEY_RIGHT);
753 newtFormAddComponents(self->form, self->tree, NULL);
754 self->selection = newt__symbol_tree_get_current(self->tree);
759 static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
763 if (!symbol_conf.use_callchain)
766 indexes = newtCheckboxTreeFindItem(self->tree, (void *)self->selection);
768 bool is_hist_entry = indexes[1] == NEWT_ARG_LAST;
775 return container_of(self->selection, struct hist_entry, ms);
778 static struct thread *hist_browser__selected_thread(struct hist_browser *self)
780 struct hist_entry *he = hist_browser__selected_entry(self);
781 return he ? he->thread : NULL;
784 static int hist_browser__title(char *bf, size_t size, const char *input_name,
785 const struct dso *dso, const struct thread *thread)
790 printed += snprintf(bf + printed, size - printed,
792 (thread->comm_set ? thread->comm : ""),
795 printed += snprintf(bf + printed, size - printed,
796 "%sDSO: %s", thread ? " " : "",
798 return printed ?: snprintf(bf, size, "Report: %s", input_name);
801 int hists__browse(struct hists *self, const char *helpline, const char *input_name)
803 struct hist_browser *browser = hist_browser__new();
804 const struct thread *thread_filter = NULL;
805 const struct dso *dso_filter = NULL;
806 struct newtExitStruct es;
813 ui_helpline__push(helpline);
815 hist_browser__title(msg, sizeof(msg), input_name,
816 dso_filter, thread_filter);
817 if (hist_browser__populate(browser, self, msg) < 0)
821 const struct thread *thread;
822 const struct dso *dso;
824 int nr_options = 0, choice = 0, i,
825 annotate = -2, zoom_dso = -2, zoom_thread = -2;
827 newtFormRun(browser->form, &es);
828 if (es.reason == NEWT_EXIT_HOTKEY) {
829 if (toupper(es.u.key) == 'A')
831 if (es.u.key == NEWT_KEY_ESCAPE ||
832 toupper(es.u.key) == 'Q' ||
833 es.u.key == CTRL('c')) {
834 if (dialog_yesno("Do you really want to exit?"))
841 if (browser->selection->sym != NULL &&
842 asprintf(&options[nr_options], "Annotate %s",
843 browser->selection->sym->name) > 0)
844 annotate = nr_options++;
846 thread = hist_browser__selected_thread(browser);
847 if (thread != NULL &&
848 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
849 (thread_filter ? "out of" : "into"),
850 (thread->comm_set ? thread->comm : ""),
852 zoom_thread = nr_options++;
854 dso = browser->selection->map ? browser->selection->map->dso : NULL;
856 asprintf(&options[nr_options], "Zoom %s %s DSO",
857 (dso_filter ? "out of" : "into"),
858 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
859 zoom_dso = nr_options++;
861 options[nr_options++] = (char *)"Exit";
863 choice = popup_menu(nr_options, options);
865 for (i = 0; i < nr_options - 1; ++i)
868 if (choice == nr_options - 1)
874 if (choice == annotate) {
875 struct hist_entry *he;
877 if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
878 ui_helpline__puts("No vmlinux file found, can't "
879 "annotate with just a "
884 he = hist_browser__selected_entry(browser);
888 hist_entry__annotate_browser(he);
889 } else if (choice == zoom_dso) {
894 ui_helpline__fpush("To zoom out press -> + \"Zoom out of %s DSO\"",
895 dso->kernel ? "the Kernel" : dso->short_name);
898 hists__filter_by_dso(self, dso_filter);
899 hist_browser__title(msg, sizeof(msg), input_name,
900 dso_filter, thread_filter);
901 if (hist_browser__populate(browser, self, msg) < 0)
903 } else if (choice == zoom_thread) {
906 thread_filter = NULL;
908 ui_helpline__fpush("To zoom out press -> + \"Zoom out of %s(%d) thread\"",
909 thread->comm_set ? thread->comm : "",
911 thread_filter = thread;
913 hists__filter_by_thread(self, thread_filter);
914 hist_browser__title(msg, sizeof(msg), input_name,
915 dso_filter, thread_filter);
916 if (hist_browser__populate(browser, self, msg) < 0)
922 hist_browser__delete(browser);
926 static struct newtPercentTreeColors {
927 const char *topColorFg, *topColorBg;
928 const char *mediumColorFg, *mediumColorBg;
929 const char *normalColorFg, *normalColorBg;
930 const char *selColorFg, *selColorBg;
931 const char *codeColorFg, *codeColorBg;
932 } defaultPercentTreeColors = {
934 "green", "lightgray",
935 "black", "lightgray",
936 "lightgray", "magenta",
940 void setup_browser(void)
942 struct newtPercentTreeColors *c = &defaultPercentTreeColors;
949 ui_helpline__puts(" ");
950 SLtt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
951 SLtt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
952 SLtt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
953 SLtt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
954 SLtt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
957 void exit_browser(bool wait_for_ok)
961 char title[] = "Fatal Error", ok[] = "Ok";
962 newtWinMessage(title, ok, browser__last_msg);