blob: 37fe8eb811ca64af48e8fe15f4d6df5ad502fee1 [file] [log] [blame]
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001#define _GNU_SOURCE
2#include <stdio.h>
3#undef _GNU_SOURCE
Arnaldo Carvalho de Melo32ec6ac2010-05-18 00:23:14 -03004/*
5 * slang versions <= 2.0.6 have a "#if HAVE_LONG_LONG" that breaks
6 * the build if it isn't defined. Use the equivalent one that glibc
7 * has on features.h.
8 */
9#include <features.h>
10#ifndef HAVE_LONG_LONG
11#define HAVE_LONG_LONG __GLIBC_HAVE_LONG_LONG
12#endif
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -030013#include <slang.h>
Arnaldo Carvalho de Melo73ae8f82010-07-30 10:06:06 -030014#include <signal.h>
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -030015#include <stdlib.h>
Arnaldo Carvalho de Melo9a725992010-08-05 17:00:42 -030016#include <elf.h>
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -030017#include <newt.h>
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -030018#include <sys/ttydefaults.h>
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -030019
20#include "cache.h"
21#include "hist.h"
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030022#include "pstack.h"
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -030023#include "session.h"
24#include "sort.h"
25#include "symbol.h"
26
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -030027#if SLANG_VERSION < 20104
28#define slsmg_printf(msg, args...) SLsmg_printf((char *)msg, ##args)
29#define slsmg_write_nstring(msg, len) SLsmg_write_nstring((char *)msg, len)
30#define sltt_set_color(obj, name, fg, bg) SLtt_set_color(obj,(char *)name,\
31 (char *)fg, (char *)bg)
32#else
33#define slsmg_printf SLsmg_printf
34#define slsmg_write_nstring SLsmg_write_nstring
35#define sltt_set_color SLtt_set_color
36#endif
37
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030038struct ui_progress {
39 newtComponent form, scale;
40};
41
42struct ui_progress *ui_progress__new(const char *title, u64 total)
43{
44 struct ui_progress *self = malloc(sizeof(*self));
45
46 if (self != NULL) {
47 int cols;
Arnaldo Carvalho de Melo1d90f2e2010-06-09 07:13:16 -030048
49 if (use_browser <= 0)
50 return self;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030051 newtGetScreenSize(&cols, NULL);
52 cols -= 4;
53 newtCenteredWindow(cols, 1, title);
54 self->form = newtForm(NULL, NULL, 0);
55 if (self->form == NULL)
56 goto out_free_self;
57 self->scale = newtScale(0, 0, cols, total);
58 if (self->scale == NULL)
59 goto out_free_form;
Arnaldo Carvalho de Melo7f826452010-05-10 10:51:25 -030060 newtFormAddComponent(self->form, self->scale);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030061 newtRefresh();
62 }
63
64 return self;
65
66out_free_form:
67 newtFormDestroy(self->form);
68out_free_self:
69 free(self);
70 return NULL;
71}
72
73void ui_progress__update(struct ui_progress *self, u64 curr)
74{
Arnaldo Carvalho de Melo1d90f2e2010-06-09 07:13:16 -030075 /*
76 * FIXME: We should have a per UI backend way of showing progress,
77 * stdio will just show a percentage as NN%, etc.
78 */
79 if (use_browser <= 0)
80 return;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030081 newtScaleSet(self->scale, curr);
82 newtRefresh();
83}
84
85void ui_progress__delete(struct ui_progress *self)
86{
Arnaldo Carvalho de Melo1d90f2e2010-06-09 07:13:16 -030087 if (use_browser > 0) {
88 newtFormDestroy(self->form);
89 newtPopWindow();
90 }
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030091 free(self);
92}
93
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -030094static void ui_helpline__pop(void)
95{
96 newtPopHelpLine();
97}
98
99static void ui_helpline__push(const char *msg)
100{
101 newtPushHelpLine(msg);
102}
103
104static void ui_helpline__vpush(const char *fmt, va_list ap)
105{
106 char *s;
107
108 if (vasprintf(&s, fmt, ap) < 0)
109 vfprintf(stderr, fmt, ap);
110 else {
111 ui_helpline__push(s);
112 free(s);
113 }
114}
115
116static void ui_helpline__fpush(const char *fmt, ...)
117{
118 va_list ap;
119
120 va_start(ap, fmt);
121 ui_helpline__vpush(fmt, ap);
122 va_end(ap);
123}
124
125static void ui_helpline__puts(const char *msg)
126{
127 ui_helpline__pop();
128 ui_helpline__push(msg);
129}
130
Arnaldo Carvalho de Melo80d50ca2010-08-05 19:28:27 -0300131static int ui_entry__read(const char *title, char *bf, size_t size, int width)
132{
133 struct newtExitStruct es;
134 newtComponent form, entry;
135 const char *result;
136 int err = -1;
137
138 newtCenteredWindow(width, 1, title);
139 form = newtForm(NULL, NULL, 0);
140 if (form == NULL)
141 return -1;
142
143 entry = newtEntry(0, 0, "0x", width, &result, NEWT_FLAG_SCROLL);
144 if (entry == NULL)
145 goto out_free_form;
146
147 newtFormAddComponent(form, entry);
148 newtFormAddHotKey(form, NEWT_KEY_ENTER);
149 newtFormAddHotKey(form, NEWT_KEY_ESCAPE);
150 newtFormAddHotKey(form, NEWT_KEY_LEFT);
151 newtFormAddHotKey(form, CTRL('c'));
152 newtFormRun(form, &es);
153
154 if (result != NULL) {
155 strncpy(bf, result, size);
156 err = 0;
157 }
158out_free_form:
159 newtPopWindow();
160 newtFormDestroy(form);
161 return 0;
162}
163
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300164static char browser__last_msg[1024];
165
166int browser__show_help(const char *format, va_list ap)
167{
168 int ret;
169 static int backlog;
170
171 ret = vsnprintf(browser__last_msg + backlog,
172 sizeof(browser__last_msg) - backlog, format, ap);
173 backlog += ret;
174
175 if (browser__last_msg[backlog - 1] == '\n') {
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300176 ui_helpline__puts(browser__last_msg);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300177 newtRefresh();
178 backlog = 0;
179 }
180
181 return ret;
182}
183
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -0300184static void newt_form__set_exit_keys(newtComponent self)
185{
Arnaldo Carvalho de Meloa308f3a2010-05-16 20:29:38 -0300186 newtFormAddHotKey(self, NEWT_KEY_LEFT);
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -0300187 newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
188 newtFormAddHotKey(self, 'Q');
189 newtFormAddHotKey(self, 'q');
190 newtFormAddHotKey(self, CTRL('c'));
191}
192
193static newtComponent newt_form__new(void)
194{
195 newtComponent self = newtForm(NULL, NULL, 0);
196 if (self)
197 newt_form__set_exit_keys(self);
198 return self;
199}
200
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300201static int popup_menu(int argc, char * const argv[])
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300202{
203 struct newtExitStruct es;
204 int i, rc = -1, max_len = 5;
205 newtComponent listbox, form = newt_form__new();
206
207 if (form == NULL)
208 return -1;
209
210 listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
211 if (listbox == NULL)
212 goto out_destroy_form;
213
Arnaldo Carvalho de Melo7f826452010-05-10 10:51:25 -0300214 newtFormAddComponent(form, listbox);
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300215
216 for (i = 0; i < argc; ++i) {
217 int len = strlen(argv[i]);
218 if (len > max_len)
219 max_len = len;
220 if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
221 goto out_destroy_form;
222 }
223
224 newtCenteredWindow(max_len, argc, NULL);
225 newtFormRun(form, &es);
226 rc = newtListboxGetCurrent(listbox) - NULL;
227 if (es.reason == NEWT_EXIT_HOTKEY)
228 rc = -1;
229 newtPopWindow();
230out_destroy_form:
231 newtFormDestroy(form);
232 return rc;
233}
234
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300235static int ui__help_window(const char *text)
236{
237 struct newtExitStruct es;
238 newtComponent tb, form = newt_form__new();
239 int rc = -1;
240 int max_len = 0, nr_lines = 0;
241 const char *t;
242
243 if (form == NULL)
244 return -1;
245
246 t = text;
247 while (1) {
248 const char *sep = strchr(t, '\n');
249 int len;
250
251 if (sep == NULL)
252 sep = strchr(t, '\0');
253 len = sep - t;
254 if (max_len < len)
255 max_len = len;
256 ++nr_lines;
257 if (*sep == '\0')
258 break;
259 t = sep + 1;
260 }
261
262 tb = newtTextbox(0, 0, max_len, nr_lines, 0);
263 if (tb == NULL)
264 goto out_destroy_form;
265
266 newtTextboxSetText(tb, text);
267 newtFormAddComponent(form, tb);
268 newtCenteredWindow(max_len, nr_lines, NULL);
269 newtFormRun(form, &es);
270 newtPopWindow();
271 rc = 0;
272out_destroy_form:
273 newtFormDestroy(form);
274 return rc;
275}
276
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300277static bool dialog_yesno(const char *msg)
278{
279 /* newtWinChoice should really be accepting const char pointers... */
280 char yes[] = "Yes", no[] = "No";
Arnaldo Carvalho de Meloc0ed55d2010-04-05 12:04:23 -0300281 return newtWinChoice(NULL, yes, no, (char *)msg) == 1;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300282}
283
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300284static void ui__error_window(const char *fmt, ...)
285{
286 va_list ap;
287
288 va_start(ap, fmt);
289 newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
290 va_end(ap);
291}
292
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300293#define HE_COLORSET_TOP 50
294#define HE_COLORSET_MEDIUM 51
295#define HE_COLORSET_NORMAL 52
296#define HE_COLORSET_SELECTED 53
297#define HE_COLORSET_CODE 54
298
299static int ui_browser__percent_color(double percent, bool current)
300{
301 if (current)
302 return HE_COLORSET_SELECTED;
303 if (percent >= MIN_RED)
304 return HE_COLORSET_TOP;
305 if (percent >= MIN_GREEN)
306 return HE_COLORSET_MEDIUM;
307 return HE_COLORSET_NORMAL;
308}
309
310struct ui_browser {
311 newtComponent form, sb;
312 u64 index, first_visible_entry_idx;
313 void *first_visible_entry, *entries;
314 u16 top, left, width, height;
315 void *priv;
Arnaldo Carvalho de Melo76ce93d2010-08-05 17:02:54 -0300316 unsigned int (*refresh)(struct ui_browser *self);
Arnaldo Carvalho de Melo9a725992010-08-05 17:00:42 -0300317 void (*write)(struct ui_browser *self, void *entry, int row);
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300318 void (*seek)(struct ui_browser *self,
319 off_t offset, int whence);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300320 u32 nr_entries;
321};
322
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300323static void ui_browser__list_head_seek(struct ui_browser *self,
324 off_t offset, int whence)
325{
326 struct list_head *head = self->entries;
327 struct list_head *pos;
328
329 switch (whence) {
330 case SEEK_SET:
331 pos = head->next;
332 break;
333 case SEEK_CUR:
334 pos = self->first_visible_entry;
335 break;
336 case SEEK_END:
337 pos = head->prev;
338 break;
339 default:
340 return;
341 }
342
343 if (offset > 0) {
344 while (offset-- != 0)
345 pos = pos->next;
346 } else {
347 while (offset++ != 0)
348 pos = pos->prev;
349 }
350
351 self->first_visible_entry = pos;
352}
353
Arnaldo Carvalho de Melo9a725992010-08-05 17:00:42 -0300354static void ui_browser__rb_tree_seek(struct ui_browser *self,
355 off_t offset, int whence)
356{
357 struct rb_root *root = self->entries;
358 struct rb_node *nd;
359
360 switch (whence) {
361 case SEEK_SET:
362 nd = rb_first(root);
363 break;
364 case SEEK_CUR:
365 nd = self->first_visible_entry;
366 break;
367 case SEEK_END:
368 nd = rb_last(root);
369 break;
370 default:
371 return;
372 }
373
374 if (offset > 0) {
375 while (offset-- != 0)
376 nd = rb_next(nd);
377 } else {
378 while (offset++ != 0)
379 nd = rb_prev(nd);
380 }
381
382 self->first_visible_entry = nd;
383}
384
385static unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self)
386{
387 struct rb_node *nd;
388 int row = 0;
389
390 if (self->first_visible_entry == NULL)
391 self->first_visible_entry = rb_first(self->entries);
392
393 nd = self->first_visible_entry;
394
395 while (nd != NULL) {
396 SLsmg_gotorc(self->top + row, self->left);
397 self->write(self, nd, row);
398 if (++row == self->height)
399 break;
400 nd = rb_next(nd);
401 }
402
403 return row;
404}
405
Arnaldo Carvalho de Melo8c694d22010-06-21 12:44:42 -0300406static bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
407{
408 return (self->first_visible_entry_idx + row) == self->index;
409}
410
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300411static void ui_browser__refresh_dimensions(struct ui_browser *self)
412{
413 int cols, rows;
414 newtGetScreenSize(&cols, &rows);
415
416 if (self->width > cols - 4)
417 self->width = cols - 4;
418 self->height = rows - 5;
419 if (self->height > self->nr_entries)
420 self->height = self->nr_entries;
421 self->top = (rows - self->height) / 2;
422 self->left = (cols - self->width) / 2;
423}
424
425static void ui_browser__reset_index(struct ui_browser *self)
426{
Arnaldo Carvalho de Melo8c694d22010-06-21 12:44:42 -0300427 self->index = self->first_visible_entry_idx = 0;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300428 self->seek(self, 0, SEEK_SET);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300429}
430
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300431static int ui_browser__show(struct ui_browser *self, const char *title)
432{
Arnaldo Carvalho de Melo63160f72010-07-26 13:47:15 -0300433 if (self->form != NULL) {
434 newtFormDestroy(self->form);
435 newtPopWindow();
436 }
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300437 ui_browser__refresh_dimensions(self);
Arnaldo Carvalho de Melo8d8c3692010-07-26 14:08:48 -0300438 newtCenteredWindow(self->width, self->height, title);
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300439 self->form = newt_form__new();
440 if (self->form == NULL)
441 return -1;
442
Arnaldo Carvalho de Melo8d8c3692010-07-26 14:08:48 -0300443 self->sb = newtVerticalScrollbar(self->width, 0, self->height,
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300444 HE_COLORSET_NORMAL,
445 HE_COLORSET_SELECTED);
446 if (self->sb == NULL)
447 return -1;
448
449 newtFormAddHotKey(self->form, NEWT_KEY_UP);
450 newtFormAddHotKey(self->form, NEWT_KEY_DOWN);
451 newtFormAddHotKey(self->form, NEWT_KEY_PGUP);
452 newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
453 newtFormAddHotKey(self->form, NEWT_KEY_HOME);
454 newtFormAddHotKey(self->form, NEWT_KEY_END);
455 newtFormAddComponent(self->form, self->sb);
456 return 0;
457}
458
Arnaldo Carvalho de Melo43730982010-08-06 16:51:12 -0300459static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300460{
Arnaldo Carvalho de Melo43730982010-08-06 16:51:12 -0300461 struct objdump_line *ol = rb_entry(entry, struct objdump_line, node);
462 bool current_entry = ui_browser__is_current_entry(self, row);
463 int width = self->width;
464
465 if (ol->offset != -1) {
466 struct hist_entry *he = self->priv;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300467 struct symbol *sym = he->ms.sym;
Arnaldo Carvalho de Melo43730982010-08-06 16:51:12 -0300468 int len = he->ms.sym->end - he->ms.sym->start;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300469 unsigned int hits = 0;
470 double percent = 0.0;
471 int color;
472 struct sym_priv *priv = symbol__priv(sym);
473 struct sym_ext *sym_ext = priv->ext;
474 struct sym_hist *h = priv->hist;
Arnaldo Carvalho de Melo43730982010-08-06 16:51:12 -0300475 s64 offset = ol->offset;
476 struct objdump_line *next = objdump__get_next_ip_line(self->entries, ol);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300477
478 while (offset < (s64)len &&
479 (next == NULL || offset < next->offset)) {
480 if (sym_ext) {
481 percent += sym_ext[offset].percent;
482 } else
483 hits += h->ip[offset];
484
485 ++offset;
486 }
487
488 if (sym_ext == NULL && h->sum)
489 percent = 100.0 * hits / h->sum;
490
491 color = ui_browser__percent_color(percent, current_entry);
492 SLsmg_set_color(color);
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300493 slsmg_printf(" %7.2f ", percent);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300494 if (!current_entry)
495 SLsmg_set_color(HE_COLORSET_CODE);
496 } else {
497 int color = ui_browser__percent_color(0, current_entry);
498 SLsmg_set_color(color);
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300499 slsmg_write_nstring(" ", 9);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300500 }
501
502 SLsmg_write_char(':');
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300503 slsmg_write_nstring(" ", 8);
Arnaldo Carvalho de Melo43730982010-08-06 16:51:12 -0300504 if (!*ol->line)
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300505 slsmg_write_nstring(" ", width - 18);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300506 else
Arnaldo Carvalho de Melo43730982010-08-06 16:51:12 -0300507 slsmg_write_nstring(ol->line, width - 18);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300508}
509
Arnaldo Carvalho de Melo76ce93d2010-08-05 17:02:54 -0300510static int ui_browser__refresh(struct ui_browser *self)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300511{
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300512 int row;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300513
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300514 newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
Arnaldo Carvalho de Melo76ce93d2010-08-05 17:02:54 -0300515 row = self->refresh(self);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300516 SLsmg_set_color(HE_COLORSET_NORMAL);
517 SLsmg_fill_region(self->top + row, self->left,
518 self->height - row, self->width, ' ');
519
520 return 0;
521}
522
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300523static int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300524{
Arnaldo Carvalho de Melo76ce93d2010-08-05 17:02:54 -0300525 if (ui_browser__refresh(self) < 0)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300526 return -1;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300527
528 while (1) {
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300529 off_t offset;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300530
531 newtFormRun(self->form, es);
532
533 if (es->reason != NEWT_EXIT_HOTKEY)
534 break;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300535 if (is_exit_key(es->u.key))
536 return es->u.key;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300537 switch (es->u.key) {
538 case NEWT_KEY_DOWN:
539 if (self->index == self->nr_entries - 1)
540 break;
541 ++self->index;
542 if (self->index == self->first_visible_entry_idx + self->height) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300543 ++self->first_visible_entry_idx;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300544 self->seek(self, +1, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300545 }
546 break;
547 case NEWT_KEY_UP:
548 if (self->index == 0)
549 break;
550 --self->index;
551 if (self->index < self->first_visible_entry_idx) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300552 --self->first_visible_entry_idx;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300553 self->seek(self, -1, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300554 }
555 break;
556 case NEWT_KEY_PGDN:
Arnaldo Carvalho de Melo17930b42010-05-19 16:03:51 -0300557 case ' ':
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300558 if (self->first_visible_entry_idx + self->height > self->nr_entries - 1)
559 break;
560
561 offset = self->height;
562 if (self->index + offset > self->nr_entries - 1)
563 offset = self->nr_entries - 1 - self->index;
564 self->index += offset;
565 self->first_visible_entry_idx += offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300566 self->seek(self, +offset, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300567 break;
568 case NEWT_KEY_PGUP:
569 if (self->first_visible_entry_idx == 0)
570 break;
571
572 if (self->first_visible_entry_idx < self->height)
573 offset = self->first_visible_entry_idx;
574 else
575 offset = self->height;
576
577 self->index -= offset;
578 self->first_visible_entry_idx -= offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300579 self->seek(self, -offset, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300580 break;
581 case NEWT_KEY_HOME:
582 ui_browser__reset_index(self);
583 break;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300584 case NEWT_KEY_END:
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300585 offset = self->height - 1;
Arnaldo Carvalho de Melo63f20e72010-07-15 07:21:07 -0300586 if (offset >= self->nr_entries)
587 offset = self->nr_entries - 1;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300588
Arnaldo Carvalho de Melo63f20e72010-07-15 07:21:07 -0300589 self->index = self->nr_entries - 1;
590 self->first_visible_entry_idx = self->index - offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300591 self->seek(self, -offset, SEEK_END);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300592 break;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300593 default:
Arnaldo Carvalho de Melob66ecd92010-07-15 07:24:30 -0300594 return es->u.key;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300595 }
Arnaldo Carvalho de Melo76ce93d2010-08-05 17:02:54 -0300596 if (ui_browser__refresh(self) < 0)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300597 return -1;
598 }
599 return 0;
600}
601
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300602static char *callchain_list__sym_name(struct callchain_list *self,
603 char *bf, size_t bfsize)
604{
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300605 if (self->ms.sym)
606 return self->ms.sym->name;
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300607
608 snprintf(bf, bfsize, "%#Lx", self->ip);
609 return bf;
610}
611
Arnaldo Carvalho de Melo43730982010-08-06 16:51:12 -0300612static unsigned int ui_browser__list_head_refresh(struct ui_browser *self)
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300613{
Arnaldo Carvalho de Melo43730982010-08-06 16:51:12 -0300614 struct list_head *pos;
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300615 struct list_head *head = self->entries;
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300616 int row = 0;
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300617
618 if (self->first_visible_entry == NULL || self->first_visible_entry == self->entries)
619 self->first_visible_entry = head->next;
620
Arnaldo Carvalho de Melo43730982010-08-06 16:51:12 -0300621 pos = self->first_visible_entry;
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300622
Arnaldo Carvalho de Melo43730982010-08-06 16:51:12 -0300623 list_for_each_from(pos, head) {
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300624 SLsmg_gotorc(self->top + row, self->left);
Arnaldo Carvalho de Melo43730982010-08-06 16:51:12 -0300625 self->write(self, pos, row);
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300626 if (++row == self->height)
627 break;
628 }
629
630 return row;
631}
632
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300633int hist_entry__tui_annotate(struct hist_entry *self)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300634{
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300635 struct newtExitStruct es;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300636 struct objdump_line *pos, *n;
637 LIST_HEAD(head);
Arnaldo Carvalho de Melo43730982010-08-06 16:51:12 -0300638 struct ui_browser browser = {
639 .entries = &head,
640 .refresh = ui_browser__list_head_refresh,
641 .seek = ui_browser__list_head_seek,
642 .write = annotate_browser__write,
643 .priv = self,
644 };
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300645 int ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300646
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300647 if (self->ms.sym == NULL)
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300648 return -1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300649
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300650 if (self->ms.map->dso->annotate_warned)
651 return -1;
652
653 if (hist_entry__annotate(self, &head) < 0) {
654 ui__error_window(browser__last_msg);
655 return -1;
656 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300657
Arnaldo Carvalho de Melo60553902010-05-15 20:40:34 -0300658 ui_helpline__push("Press <- or ESC to exit");
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300659
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300660 list_for_each_entry(pos, &head, node) {
661 size_t line_len = strlen(pos->line);
662 if (browser.width < line_len)
663 browser.width = line_len;
664 ++browser.nr_entries;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300665 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300666
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300667 browser.width += 18; /* Percentage */
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300668 ui_browser__show(&browser, self->ms.sym->name);
Arnaldo Carvalho de Melob61b55e2010-07-21 17:55:32 -0300669 newtFormAddHotKey(browser.form, ' ');
Srikar Dronamraju0879b102010-06-29 23:02:26 +0530670 ret = ui_browser__run(&browser, &es);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300671 newtFormDestroy(browser.form);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300672 newtPopWindow();
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300673 list_for_each_entry_safe(pos, n, &head, node) {
674 list_del(&pos->node);
675 objdump_line__free(pos);
676 }
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300677 ui_helpline__pop();
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300678 return ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300679}
680
Arnaldo Carvalho de Melo9a725992010-08-05 17:00:42 -0300681/* -------------------------------------------------------------------- */
682
683struct map_browser {
684 struct ui_browser b;
685 struct map *map;
686 u16 namelen;
687 u8 addrlen;
688};
689
690static void map_browser__write(struct ui_browser *self, void *nd, int row)
691{
692 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
693 struct map_browser *mb = container_of(self, struct map_browser, b);
694 bool current_entry = ui_browser__is_current_entry(self, row);
695 int color = ui_browser__percent_color(0, current_entry);
696
697 SLsmg_set_color(color);
698 slsmg_printf("%*llx %*llx %c ",
699 mb->addrlen, sym->start, mb->addrlen, sym->end,
700 sym->binding == STB_GLOBAL ? 'g' :
701 sym->binding == STB_LOCAL ? 'l' : 'w');
702 slsmg_write_nstring(sym->name, mb->namelen);
703}
704
Arnaldo Carvalho de Melo80d50ca2010-08-05 19:28:27 -0300705/* FIXME uber-kludgy, see comment on cmd_report... */
706static u32 *symbol__browser_index(struct symbol *self)
707{
708 return ((void *)self) - sizeof(struct rb_node) - sizeof(u32);
709}
710
711static int map_browser__search(struct map_browser *self)
712{
713 char target[512];
714 struct symbol *sym;
715 int err = ui_entry__read("Search by name/addr", target, sizeof(target), 40);
716
717 if (err)
718 return err;
719
720 if (target[0] == '0' && tolower(target[1]) == 'x') {
721 u64 addr = strtoull(target, NULL, 16);
722 sym = map__find_symbol(self->map, addr, NULL);
723 } else
724 sym = map__find_symbol_by_name(self->map, target, NULL);
725
726 if (sym != NULL) {
727 u32 *idx = symbol__browser_index(sym);
728
729 self->b.first_visible_entry = &sym->rb_node;
730 self->b.index = self->b.first_visible_entry_idx = *idx;
731 } else
732 ui_helpline__fpush("%s not found!", target);
733
734 return 0;
735}
736
737static int map_browser__run(struct map_browser *self, struct newtExitStruct *es)
738{
739 if (ui_browser__show(&self->b, self->map->dso->long_name) < 0)
740 return -1;
741
742 ui_helpline__fpush("Press <- or ESC to exit, %s / to search",
743 verbose ? "" : "restart with -v to use");
744 newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT);
745 newtFormAddHotKey(self->b.form, NEWT_KEY_ENTER);
746 if (verbose)
747 newtFormAddHotKey(self->b.form, '/');
748
749 while (1) {
750 ui_browser__run(&self->b, es);
751
752 if (es->reason != NEWT_EXIT_HOTKEY)
753 break;
754 if (verbose && es->u.key == '/')
755 map_browser__search(self);
756 else
757 break;
758 }
759
760 newtFormDestroy(self->b.form);
761 newtPopWindow();
762 ui_helpline__pop();
763 return 0;
764}
765
Arnaldo Carvalho de Melo9a725992010-08-05 17:00:42 -0300766static int map__browse(struct map *self)
767{
768 struct map_browser mb = {
769 .b = {
770 .entries = &self->dso->symbols[self->type],
Arnaldo Carvalho de Melo76ce93d2010-08-05 17:02:54 -0300771 .refresh = ui_browser__rb_tree_refresh,
Arnaldo Carvalho de Melo9a725992010-08-05 17:00:42 -0300772 .seek = ui_browser__rb_tree_seek,
773 .write = map_browser__write,
774 },
Arnaldo Carvalho de Melo80d50ca2010-08-05 19:28:27 -0300775 .map = self,
Arnaldo Carvalho de Melo9a725992010-08-05 17:00:42 -0300776 };
777 struct newtExitStruct es;
778 struct rb_node *nd;
779 char tmp[BITS_PER_LONG / 4];
780 u64 maxaddr = 0;
Arnaldo Carvalho de Melo9a725992010-08-05 17:00:42 -0300781
782 for (nd = rb_first(mb.b.entries); nd; nd = rb_next(nd)) {
783 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
784
785 if (mb.namelen < pos->namelen)
786 mb.namelen = pos->namelen;
787 if (maxaddr < pos->end)
788 maxaddr = pos->end;
Arnaldo Carvalho de Melo80d50ca2010-08-05 19:28:27 -0300789 if (verbose) {
790 u32 *idx = symbol__browser_index(pos);
791 *idx = mb.b.nr_entries;
792 }
Arnaldo Carvalho de Melo9a725992010-08-05 17:00:42 -0300793 ++mb.b.nr_entries;
794 }
795
796 mb.addrlen = snprintf(tmp, sizeof(tmp), "%llx", maxaddr);
797 mb.b.width += mb.addrlen * 2 + 4 + mb.namelen;
Arnaldo Carvalho de Melo80d50ca2010-08-05 19:28:27 -0300798 return map_browser__run(&mb, &es);
Arnaldo Carvalho de Melo9a725992010-08-05 17:00:42 -0300799}
800
801/* -------------------------------------------------------------------- */
802
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300803struct hist_browser {
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300804 struct ui_browser b;
805 struct hists *hists;
806 struct hist_entry *he_selection;
807 struct map_symbol *selection;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300808};
809
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300810static void hist_browser__reset(struct hist_browser *self);
811static int hist_browser__run(struct hist_browser *self, const char *title,
812 struct newtExitStruct *es);
Arnaldo Carvalho de Melo76ce93d2010-08-05 17:02:54 -0300813static unsigned int hist_browser__refresh(struct ui_browser *self);
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300814static void ui_browser__hists_seek(struct ui_browser *self,
815 off_t offset, int whence);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300816
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300817static struct hist_browser *hist_browser__new(struct hists *hists)
818{
819 struct hist_browser *self = zalloc(sizeof(*self));
820
821 if (self) {
822 self->hists = hists;
Arnaldo Carvalho de Melo76ce93d2010-08-05 17:02:54 -0300823 self->b.refresh = hist_browser__refresh;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300824 self->b.seek = ui_browser__hists_seek;
825 }
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300826
827 return self;
828}
829
830static void hist_browser__delete(struct hist_browser *self)
831{
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300832 newtFormDestroy(self->b.form);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300833 newtPopWindow();
834 free(self);
835}
836
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300837static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300838{
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300839 return self->he_selection;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300840}
841
842static struct thread *hist_browser__selected_thread(struct hist_browser *self)
843{
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300844 return self->he_selection->thread;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300845}
846
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300847static int hist_browser__title(char *bf, size_t size, const char *ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300848 const struct dso *dso, const struct thread *thread)
849{
850 int printed = 0;
851
852 if (thread)
853 printed += snprintf(bf + printed, size - printed,
854 "Thread: %s(%d)",
855 (thread->comm_set ? thread->comm : ""),
856 thread->pid);
857 if (dso)
858 printed += snprintf(bf + printed, size - printed,
859 "%sDSO: %s", thread ? " " : "",
860 dso->short_name);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300861 return printed ?: snprintf(bf, size, "Event: %s", ev_name);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300862}
863
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300864int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300865{
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300866 struct hist_browser *browser = hist_browser__new(self);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300867 struct pstack *fstack;
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300868 const struct thread *thread_filter = NULL;
869 const struct dso *dso_filter = NULL;
870 struct newtExitStruct es;
871 char msg[160];
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300872 int key = -1;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300873
874 if (browser == NULL)
875 return -1;
876
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300877 fstack = pstack__new(2);
878 if (fstack == NULL)
879 goto out;
880
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300881 ui_helpline__push(helpline);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300882
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300883 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300884 dso_filter, thread_filter);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300885
886 while (1) {
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300887 const struct thread *thread;
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300888 const struct dso *dso;
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300889 char *options[16];
890 int nr_options = 0, choice = 0, i,
Arnaldo Carvalho de Melo9a725992010-08-05 17:00:42 -0300891 annotate = -2, zoom_dso = -2, zoom_thread = -2,
892 browse_map = -2;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300893
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300894 if (hist_browser__run(browser, msg, &es))
895 break;
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300896
897 thread = hist_browser__selected_thread(browser);
898 dso = browser->selection->map ? browser->selection->map->dso : NULL;
899
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300900 if (es.reason == NEWT_EXIT_HOTKEY) {
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300901 key = es.u.key;
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300902
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300903 switch (key) {
904 case NEWT_KEY_F1:
905 goto do_help;
906 case NEWT_KEY_TAB:
907 case NEWT_KEY_UNTAB:
908 /*
909 * Exit the browser, let hists__browser_tree
910 * go to the next or previous
911 */
912 goto out_free_stack;
913 default:;
914 }
915
916 key = toupper(key);
917 switch (key) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300918 case 'A':
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -0300919 if (browser->selection->map == NULL &&
920 browser->selection->map->dso->annotate_warned)
921 continue;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300922 goto do_annotate;
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300923 case 'D':
924 goto zoom_dso;
925 case 'T':
926 goto zoom_thread;
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300927 case 'H':
928 case '?':
929do_help:
930 ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
931 "<- Zoom out\n"
932 "a Annotate current symbol\n"
933 "h/?/F1 Show this window\n"
934 "d Zoom into current DSO\n"
935 "t Zoom into current Thread\n"
936 "q/CTRL+C Exit browser");
937 continue;
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300938 default:;
939 }
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300940 if (is_exit_key(key)) {
941 if (key == NEWT_KEY_ESCAPE) {
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300942 if (dialog_yesno("Do you really want to exit?"))
943 break;
944 else
945 continue;
946 } else
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300947 break;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300948 }
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300949
950 if (es.u.key == NEWT_KEY_LEFT) {
951 const void *top;
952
953 if (pstack__empty(fstack))
954 continue;
955 top = pstack__pop(fstack);
956 if (top == &dso_filter)
957 goto zoom_out_dso;
958 if (top == &thread_filter)
959 goto zoom_out_thread;
960 continue;
961 }
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300962 }
963
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300964 if (browser->selection->sym != NULL &&
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -0300965 !browser->selection->map->dso->annotate_warned &&
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300966 asprintf(&options[nr_options], "Annotate %s",
967 browser->selection->sym->name) > 0)
968 annotate = nr_options++;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300969
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300970 if (thread != NULL &&
971 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300972 (thread_filter ? "out of" : "into"),
973 (thread->comm_set ? thread->comm : ""),
974 thread->pid) > 0)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300975 zoom_thread = nr_options++;
976
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300977 if (dso != NULL &&
978 asprintf(&options[nr_options], "Zoom %s %s DSO",
979 (dso_filter ? "out of" : "into"),
980 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
981 zoom_dso = nr_options++;
982
Arnaldo Carvalho de Melo9a725992010-08-05 17:00:42 -0300983 if (browser->selection->map != NULL &&
984 asprintf(&options[nr_options], "Browse map details") > 0)
985 browse_map = nr_options++;
986
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300987 options[nr_options++] = (char *)"Exit";
988
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300989 choice = popup_menu(nr_options, options);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300990
991 for (i = 0; i < nr_options - 1; ++i)
992 free(options[i]);
993
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300994 if (choice == nr_options - 1)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300995 break;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300996
997 if (choice == -1)
998 continue;
Arnaldo Carvalho de Meloc1ec5fe2010-05-15 20:45:31 -0300999
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001000 if (choice == annotate) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001001 struct hist_entry *he;
Arnaldo Carvalho de Meloc1ec5fe2010-05-15 20:45:31 -03001002do_annotate:
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -03001003 if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -03001004 browser->selection->map->dso->annotate_warned = 1;
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001005 ui_helpline__puts("No vmlinux file found, can't "
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -03001006 "annotate with just a "
1007 "kallsyms file");
1008 continue;
1009 }
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001010
1011 he = hist_browser__selected_entry(browser);
1012 if (he == NULL)
1013 continue;
1014
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001015 hist_entry__tui_annotate(he);
Arnaldo Carvalho de Melo9a725992010-08-05 17:00:42 -03001016 } else if (choice == browse_map)
1017 map__browse(browser->selection->map);
1018 else if (choice == zoom_dso) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001019zoom_dso:
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001020 if (dso_filter) {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001021 pstack__remove(fstack, &dso_filter);
1022zoom_out_dso:
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001023 ui_helpline__pop();
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001024 dso_filter = NULL;
1025 } else {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001026 if (dso == NULL)
1027 continue;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001028 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001029 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001030 dso_filter = dso;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001031 pstack__push(fstack, &dso_filter);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001032 }
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001033 hists__filter_by_dso(self, dso_filter);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001034 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001035 dso_filter, thread_filter);
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001036 hist_browser__reset(browser);
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001037 } else if (choice == zoom_thread) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001038zoom_thread:
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001039 if (thread_filter) {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001040 pstack__remove(fstack, &thread_filter);
1041zoom_out_thread:
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001042 ui_helpline__pop();
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001043 thread_filter = NULL;
1044 } else {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001045 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001046 thread->comm_set ? thread->comm : "",
1047 thread->pid);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001048 thread_filter = thread;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001049 pstack__push(fstack, &thread_filter);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001050 }
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001051 hists__filter_by_thread(self, thread_filter);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001052 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001053 dso_filter, thread_filter);
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001054 hist_browser__reset(browser);
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -03001055 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001056 }
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001057out_free_stack:
1058 pstack__delete(fstack);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -03001059out:
1060 hist_browser__delete(browser);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001061 return key;
1062}
1063
1064int hists__tui_browse_tree(struct rb_root *self, const char *help)
1065{
1066 struct rb_node *first = rb_first(self), *nd = first, *next;
1067 int key = 0;
1068
1069 while (nd) {
1070 struct hists *hists = rb_entry(nd, struct hists, rb_node);
1071 const char *ev_name = __event_name(hists->type, hists->config);
1072
1073 key = hists__browse(hists, help, ev_name);
1074
1075 if (is_exit_key(key))
1076 break;
1077
1078 switch (key) {
1079 case NEWT_KEY_TAB:
1080 next = rb_next(nd);
1081 if (next)
1082 nd = next;
1083 break;
1084 case NEWT_KEY_UNTAB:
1085 if (nd == first)
1086 continue;
1087 nd = rb_prev(nd);
1088 default:
1089 break;
1090 }
1091 }
1092
1093 return key;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001094}
1095
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001096static struct newtPercentTreeColors {
1097 const char *topColorFg, *topColorBg;
1098 const char *mediumColorFg, *mediumColorBg;
1099 const char *normalColorFg, *normalColorBg;
1100 const char *selColorFg, *selColorBg;
1101 const char *codeColorFg, *codeColorBg;
1102} defaultPercentTreeColors = {
1103 "red", "lightgray",
1104 "green", "lightgray",
1105 "black", "lightgray",
1106 "lightgray", "magenta",
1107 "blue", "lightgray",
1108};
1109
Arnaldo Carvalho de Melo73ae8f82010-07-30 10:06:06 -03001110static void newt_suspend(void *d __used)
1111{
1112 newtSuspend();
1113 raise(SIGTSTP);
1114 newtResume();
1115}
1116
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001117void setup_browser(void)
1118{
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001119 struct newtPercentTreeColors *c = &defaultPercentTreeColors;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001120
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001121 if (!isatty(1) || !use_browser || dump_trace) {
Arnaldo Carvalho de Melo62e34362010-05-26 13:22:26 -03001122 use_browser = 0;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -03001123 setup_pager();
1124 return;
1125 }
1126
1127 use_browser = 1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001128 newtInit();
1129 newtCls();
Arnaldo Carvalho de Melo73ae8f82010-07-30 10:06:06 -03001130 newtSetSuspendCallback(newt_suspend, NULL);
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001131 ui_helpline__puts(" ");
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -03001132 sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
1133 sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
1134 sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
1135 sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
1136 sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001137}
1138
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -03001139void exit_browser(bool wait_for_ok)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001140{
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -03001141 if (use_browser > 0) {
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -03001142 if (wait_for_ok) {
1143 char title[] = "Fatal Error", ok[] = "Ok";
1144 newtWinMessage(title, ok, browser__last_msg);
1145 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001146 newtFinished();
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -03001147 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001148}
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001149
1150static void hist_browser__refresh_dimensions(struct hist_browser *self)
1151{
1152 /* 3 == +/- toggle symbol before actual hist_entry rendering */
1153 self->b.width = 3 + (hists__sort_list_width(self->hists) +
1154 sizeof("[k]"));
1155}
1156
1157static void hist_browser__reset(struct hist_browser *self)
1158{
1159 self->b.nr_entries = self->hists->nr_entries;
1160 hist_browser__refresh_dimensions(self);
1161 ui_browser__reset_index(&self->b);
1162}
1163
1164static char tree__folded_sign(bool unfolded)
1165{
1166 return unfolded ? '-' : '+';
1167}
1168
1169static char map_symbol__folded(const struct map_symbol *self)
1170{
1171 return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
1172}
1173
1174static char hist_entry__folded(const struct hist_entry *self)
1175{
1176 return map_symbol__folded(&self->ms);
1177}
1178
1179static char callchain_list__folded(const struct callchain_list *self)
1180{
1181 return map_symbol__folded(&self->ms);
1182}
1183
1184static bool map_symbol__toggle_fold(struct map_symbol *self)
1185{
1186 if (!self->has_children)
1187 return false;
1188
1189 self->unfolded = !self->unfolded;
1190 return true;
1191}
1192
1193#define LEVEL_OFFSET_STEP 3
1194
1195static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
1196 struct callchain_node *chain_node,
1197 u64 total, int level,
1198 unsigned short row,
1199 off_t *row_offset,
1200 bool *is_current_entry)
1201{
1202 struct rb_node *node;
1203 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
1204 u64 new_total, remaining;
1205
1206 if (callchain_param.mode == CHAIN_GRAPH_REL)
1207 new_total = chain_node->children_hit;
1208 else
1209 new_total = total;
1210
1211 remaining = new_total;
1212 node = rb_first(&chain_node->rb_root);
1213 while (node) {
1214 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
1215 struct rb_node *next = rb_next(node);
1216 u64 cumul = cumul_hits(child);
1217 struct callchain_list *chain;
1218 char folded_sign = ' ';
1219 int first = true;
1220 int extra_offset = 0;
1221
1222 remaining -= cumul;
1223
1224 list_for_each_entry(chain, &child->val, list) {
1225 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
1226 const char *str;
1227 int color;
1228 bool was_first = first;
1229
1230 if (first) {
1231 first = false;
1232 chain->ms.has_children = chain->list.next != &child->val ||
1233 rb_first(&child->rb_root) != NULL;
1234 } else {
1235 extra_offset = LEVEL_OFFSET_STEP;
1236 chain->ms.has_children = chain->list.next == &child->val &&
1237 rb_first(&child->rb_root) != NULL;
1238 }
1239
1240 folded_sign = callchain_list__folded(chain);
1241 if (*row_offset != 0) {
1242 --*row_offset;
1243 goto do_next;
1244 }
1245
1246 alloc_str = NULL;
1247 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
1248 if (was_first) {
1249 double percent = cumul * 100.0 / new_total;
1250
1251 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
1252 str = "Not enough memory!";
1253 else
1254 str = alloc_str;
1255 }
1256
1257 color = HE_COLORSET_NORMAL;
1258 width = self->b.width - (offset + extra_offset + 2);
1259 if (ui_browser__is_current_entry(&self->b, row)) {
1260 self->selection = &chain->ms;
1261 color = HE_COLORSET_SELECTED;
1262 *is_current_entry = true;
1263 }
1264
1265 SLsmg_set_color(color);
1266 SLsmg_gotorc(self->b.top + row, self->b.left);
1267 slsmg_write_nstring(" ", offset + extra_offset);
1268 slsmg_printf("%c ", folded_sign);
1269 slsmg_write_nstring(str, width);
1270 free(alloc_str);
1271
1272 if (++row == self->b.height)
1273 goto out;
1274do_next:
1275 if (folded_sign == '+')
1276 break;
1277 }
1278
1279 if (folded_sign == '-') {
1280 const int new_level = level + (extra_offset ? 2 : 1);
1281 row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
1282 new_level, row, row_offset,
1283 is_current_entry);
1284 }
1285 if (row == self->b.height)
1286 goto out;
1287 node = next;
1288 }
1289out:
1290 return row - first_row;
1291}
1292
1293static int hist_browser__show_callchain_node(struct hist_browser *self,
1294 struct callchain_node *node,
1295 int level, unsigned short row,
1296 off_t *row_offset,
1297 bool *is_current_entry)
1298{
1299 struct callchain_list *chain;
1300 int first_row = row,
1301 offset = level * LEVEL_OFFSET_STEP,
1302 width = self->b.width - offset;
1303 char folded_sign = ' ';
1304
1305 list_for_each_entry(chain, &node->val, list) {
1306 char ipstr[BITS_PER_LONG / 4 + 1], *s;
1307 int color;
1308 /*
1309 * FIXME: This should be moved to somewhere else,
1310 * probably when the callchain is created, so as not to
1311 * traverse it all over again
1312 */
1313 chain->ms.has_children = rb_first(&node->rb_root) != NULL;
1314 folded_sign = callchain_list__folded(chain);
1315
1316 if (*row_offset != 0) {
1317 --*row_offset;
1318 continue;
1319 }
1320
1321 color = HE_COLORSET_NORMAL;
1322 if (ui_browser__is_current_entry(&self->b, row)) {
1323 self->selection = &chain->ms;
1324 color = HE_COLORSET_SELECTED;
1325 *is_current_entry = true;
1326 }
1327
1328 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
1329 SLsmg_gotorc(self->b.top + row, self->b.left);
1330 SLsmg_set_color(color);
1331 slsmg_write_nstring(" ", offset);
1332 slsmg_printf("%c ", folded_sign);
1333 slsmg_write_nstring(s, width - 2);
1334
1335 if (++row == self->b.height)
1336 goto out;
1337 }
1338
1339 if (folded_sign == '-')
1340 row += hist_browser__show_callchain_node_rb_tree(self, node,
1341 self->hists->stats.total_period,
1342 level + 1, row,
1343 row_offset,
1344 is_current_entry);
1345out:
1346 return row - first_row;
1347}
1348
1349static int hist_browser__show_callchain(struct hist_browser *self,
1350 struct rb_root *chain,
1351 int level, unsigned short row,
1352 off_t *row_offset,
1353 bool *is_current_entry)
1354{
1355 struct rb_node *nd;
1356 int first_row = row;
1357
1358 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
1359 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
1360
1361 row += hist_browser__show_callchain_node(self, node, level,
1362 row, row_offset,
1363 is_current_entry);
1364 if (row == self->b.height)
1365 break;
1366 }
1367
1368 return row - first_row;
1369}
1370
1371static int hist_browser__show_entry(struct hist_browser *self,
1372 struct hist_entry *entry,
1373 unsigned short row)
1374{
1375 char s[256];
1376 double percent;
1377 int printed = 0;
1378 int color, width = self->b.width;
1379 char folded_sign = ' ';
1380 bool current_entry = ui_browser__is_current_entry(&self->b, row);
1381 off_t row_offset = entry->row_offset;
1382
1383 if (current_entry) {
1384 self->he_selection = entry;
1385 self->selection = &entry->ms;
1386 }
1387
1388 if (symbol_conf.use_callchain) {
1389 entry->ms.has_children = !RB_EMPTY_ROOT(&entry->sorted_chain);
1390 folded_sign = hist_entry__folded(entry);
1391 }
1392
1393 if (row_offset == 0) {
1394 hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false,
1395 0, false, self->hists->stats.total_period);
1396 percent = (entry->period * 100.0) / self->hists->stats.total_period;
1397
1398 color = HE_COLORSET_SELECTED;
1399 if (!current_entry) {
1400 if (percent >= MIN_RED)
1401 color = HE_COLORSET_TOP;
1402 else if (percent >= MIN_GREEN)
1403 color = HE_COLORSET_MEDIUM;
1404 else
1405 color = HE_COLORSET_NORMAL;
1406 }
1407
1408 SLsmg_set_color(color);
1409 SLsmg_gotorc(self->b.top + row, self->b.left);
1410 if (symbol_conf.use_callchain) {
1411 slsmg_printf("%c ", folded_sign);
1412 width -= 2;
1413 }
1414 slsmg_write_nstring(s, width);
1415 ++row;
1416 ++printed;
1417 } else
1418 --row_offset;
1419
1420 if (folded_sign == '-' && row != self->b.height) {
1421 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
1422 1, row, &row_offset,
1423 &current_entry);
1424 if (current_entry)
1425 self->he_selection = entry;
1426 }
1427
1428 return printed;
1429}
1430
Arnaldo Carvalho de Melo76ce93d2010-08-05 17:02:54 -03001431static unsigned int hist_browser__refresh(struct ui_browser *self)
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001432{
1433 unsigned row = 0;
1434 struct rb_node *nd;
1435 struct hist_browser *hb = container_of(self, struct hist_browser, b);
1436
1437 if (self->first_visible_entry == NULL)
1438 self->first_visible_entry = rb_first(&hb->hists->entries);
1439
1440 for (nd = self->first_visible_entry; nd; nd = rb_next(nd)) {
1441 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1442
1443 if (h->filtered)
1444 continue;
1445
1446 row += hist_browser__show_entry(hb, h, row);
1447 if (row == self->height)
1448 break;
1449 }
1450
1451 return row;
1452}
1453
1454static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
1455{
1456 struct rb_node *nd = rb_first(&self->rb_root);
1457
1458 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
1459 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
1460 struct callchain_list *chain;
1461 int first = true;
1462
1463 list_for_each_entry(chain, &child->val, list) {
1464 if (first) {
1465 first = false;
1466 chain->ms.has_children = chain->list.next != &child->val ||
1467 rb_first(&child->rb_root) != NULL;
1468 } else
1469 chain->ms.has_children = chain->list.next == &child->val &&
1470 rb_first(&child->rb_root) != NULL;
1471 }
1472
1473 callchain_node__init_have_children_rb_tree(child);
1474 }
1475}
1476
1477static void callchain_node__init_have_children(struct callchain_node *self)
1478{
1479 struct callchain_list *chain;
1480
1481 list_for_each_entry(chain, &self->val, list)
1482 chain->ms.has_children = rb_first(&self->rb_root) != NULL;
1483
1484 callchain_node__init_have_children_rb_tree(self);
1485}
1486
1487static void callchain__init_have_children(struct rb_root *self)
1488{
1489 struct rb_node *nd;
1490
1491 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
1492 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
1493 callchain_node__init_have_children(node);
1494 }
1495}
1496
1497static void hist_entry__init_have_children(struct hist_entry *self)
1498{
1499 if (!self->init_have_children) {
1500 callchain__init_have_children(&self->sorted_chain);
1501 self->init_have_children = true;
1502 }
1503}
1504
1505static struct rb_node *hists__filter_entries(struct rb_node *nd)
1506{
1507 while (nd != NULL) {
1508 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1509 if (!h->filtered)
1510 return nd;
1511
1512 nd = rb_next(nd);
1513 }
1514
1515 return NULL;
1516}
1517
1518static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
1519{
1520 while (nd != NULL) {
1521 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1522 if (!h->filtered)
1523 return nd;
1524
1525 nd = rb_prev(nd);
1526 }
1527
1528 return NULL;
1529}
1530
1531static void ui_browser__hists_seek(struct ui_browser *self,
1532 off_t offset, int whence)
1533{
1534 struct hist_entry *h;
1535 struct rb_node *nd;
1536 bool first = true;
1537
1538 switch (whence) {
1539 case SEEK_SET:
1540 nd = hists__filter_entries(rb_first(self->entries));
1541 break;
1542 case SEEK_CUR:
1543 nd = self->first_visible_entry;
1544 goto do_offset;
1545 case SEEK_END:
1546 nd = hists__filter_prev_entries(rb_last(self->entries));
1547 first = false;
1548 break;
1549 default:
1550 return;
1551 }
1552
1553 /*
1554 * Moves not relative to the first visible entry invalidates its
1555 * row_offset:
1556 */
1557 h = rb_entry(self->first_visible_entry, struct hist_entry, rb_node);
1558 h->row_offset = 0;
1559
1560 /*
1561 * Here we have to check if nd is expanded (+), if it is we can't go
1562 * the next top level hist_entry, instead we must compute an offset of
1563 * what _not_ to show and not change the first visible entry.
1564 *
1565 * This offset increments when we are going from top to bottom and
1566 * decreases when we're going from bottom to top.
1567 *
1568 * As we don't have backpointers to the top level in the callchains
1569 * structure, we need to always print the whole hist_entry callchain,
1570 * skipping the first ones that are before the first visible entry
1571 * and stop when we printed enough lines to fill the screen.
1572 */
1573do_offset:
1574 if (offset > 0) {
1575 do {
1576 h = rb_entry(nd, struct hist_entry, rb_node);
1577 if (h->ms.unfolded) {
1578 u16 remaining = h->nr_rows - h->row_offset;
1579 if (offset > remaining) {
1580 offset -= remaining;
1581 h->row_offset = 0;
1582 } else {
1583 h->row_offset += offset;
1584 offset = 0;
1585 self->first_visible_entry = nd;
1586 break;
1587 }
1588 }
1589 nd = hists__filter_entries(rb_next(nd));
1590 if (nd == NULL)
1591 break;
1592 --offset;
1593 self->first_visible_entry = nd;
1594 } while (offset != 0);
1595 } else if (offset < 0) {
1596 while (1) {
1597 h = rb_entry(nd, struct hist_entry, rb_node);
1598 if (h->ms.unfolded) {
1599 if (first) {
1600 if (-offset > h->row_offset) {
1601 offset += h->row_offset;
1602 h->row_offset = 0;
1603 } else {
1604 h->row_offset += offset;
1605 offset = 0;
1606 self->first_visible_entry = nd;
1607 break;
1608 }
1609 } else {
1610 if (-offset > h->nr_rows) {
1611 offset += h->nr_rows;
1612 h->row_offset = 0;
1613 } else {
1614 h->row_offset = h->nr_rows + offset;
1615 offset = 0;
1616 self->first_visible_entry = nd;
1617 break;
1618 }
1619 }
1620 }
1621
1622 nd = hists__filter_prev_entries(rb_prev(nd));
1623 if (nd == NULL)
1624 break;
1625 ++offset;
1626 self->first_visible_entry = nd;
1627 if (offset == 0) {
1628 /*
1629 * Last unfiltered hist_entry, check if it is
1630 * unfolded, if it is then we should have
1631 * row_offset at its last entry.
1632 */
1633 h = rb_entry(nd, struct hist_entry, rb_node);
1634 if (h->ms.unfolded)
1635 h->row_offset = h->nr_rows;
1636 break;
1637 }
1638 first = false;
1639 }
1640 } else {
1641 self->first_visible_entry = nd;
1642 h = rb_entry(nd, struct hist_entry, rb_node);
1643 h->row_offset = 0;
1644 }
1645}
1646
1647static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
1648{
1649 int n = 0;
1650 struct rb_node *nd;
1651
1652 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
1653 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
1654 struct callchain_list *chain;
1655 char folded_sign = ' '; /* No children */
1656
1657 list_for_each_entry(chain, &child->val, list) {
1658 ++n;
1659 /* We need this because we may not have children */
1660 folded_sign = callchain_list__folded(chain);
1661 if (folded_sign == '+')
1662 break;
1663 }
1664
1665 if (folded_sign == '-') /* Have children and they're unfolded */
1666 n += callchain_node__count_rows_rb_tree(child);
1667 }
1668
1669 return n;
1670}
1671
1672static int callchain_node__count_rows(struct callchain_node *node)
1673{
1674 struct callchain_list *chain;
1675 bool unfolded = false;
1676 int n = 0;
1677
1678 list_for_each_entry(chain, &node->val, list) {
1679 ++n;
1680 unfolded = chain->ms.unfolded;
1681 }
1682
1683 if (unfolded)
1684 n += callchain_node__count_rows_rb_tree(node);
1685
1686 return n;
1687}
1688
1689static int callchain__count_rows(struct rb_root *chain)
1690{
1691 struct rb_node *nd;
1692 int n = 0;
1693
1694 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
1695 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
1696 n += callchain_node__count_rows(node);
1697 }
1698
1699 return n;
1700}
1701
1702static bool hist_browser__toggle_fold(struct hist_browser *self)
1703{
1704 if (map_symbol__toggle_fold(self->selection)) {
1705 struct hist_entry *he = self->he_selection;
1706
1707 hist_entry__init_have_children(he);
1708 self->hists->nr_entries -= he->nr_rows;
1709
1710 if (he->ms.unfolded)
1711 he->nr_rows = callchain__count_rows(&he->sorted_chain);
1712 else
1713 he->nr_rows = 0;
1714 self->hists->nr_entries += he->nr_rows;
1715 self->b.nr_entries = self->hists->nr_entries;
1716
1717 return true;
1718 }
1719
1720 /* If it doesn't have children, no toggling performed */
1721 return false;
1722}
1723
1724static int hist_browser__run(struct hist_browser *self, const char *title,
1725 struct newtExitStruct *es)
1726{
1727 char str[256], unit;
1728 unsigned long nr_events = self->hists->stats.nr_events[PERF_RECORD_SAMPLE];
1729
1730 self->b.entries = &self->hists->entries;
1731 self->b.nr_entries = self->hists->nr_entries;
1732
1733 hist_browser__refresh_dimensions(self);
1734
1735 nr_events = convert_unit(nr_events, &unit);
1736 snprintf(str, sizeof(str), "Events: %lu%c ",
1737 nr_events, unit);
1738 newtDrawRootText(0, 0, str);
1739
1740 if (ui_browser__show(&self->b, title) < 0)
1741 return -1;
1742
1743 newtFormAddHotKey(self->b.form, 'A');
1744 newtFormAddHotKey(self->b.form, 'a');
1745 newtFormAddHotKey(self->b.form, '?');
1746 newtFormAddHotKey(self->b.form, 'h');
1747 newtFormAddHotKey(self->b.form, 'H');
1748 newtFormAddHotKey(self->b.form, 'd');
1749
1750 newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT);
1751 newtFormAddHotKey(self->b.form, NEWT_KEY_RIGHT);
1752 newtFormAddHotKey(self->b.form, NEWT_KEY_ENTER);
1753
1754 while (1) {
1755 ui_browser__run(&self->b, es);
1756
1757 if (es->reason != NEWT_EXIT_HOTKEY)
1758 break;
1759 switch (es->u.key) {
1760 case 'd': { /* Debug */
1761 static int seq;
1762 struct hist_entry *h = rb_entry(self->b.first_visible_entry,
1763 struct hist_entry, rb_node);
1764 ui_helpline__pop();
1765 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
1766 seq++, self->b.nr_entries,
1767 self->hists->nr_entries,
1768 self->b.height,
1769 self->b.index,
1770 self->b.first_visible_entry_idx,
1771 h->row_offset, h->nr_rows);
1772 }
1773 continue;
1774 case NEWT_KEY_ENTER:
1775 if (hist_browser__toggle_fold(self))
1776 break;
1777 /* fall thru */
1778 default:
1779 return 0;
1780 }
1781 }
1782 return 0;
1783}