blob: 28f74eb8fe7c686bbb94044087fe46c14c240e6b [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 Melof9224c52010-03-11 20:12:44 -030014#include <stdlib.h>
15#include <newt.h>
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -030016#include <sys/ttydefaults.h>
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -030017
18#include "cache.h"
19#include "hist.h"
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030020#include "pstack.h"
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -030021#include "session.h"
22#include "sort.h"
23#include "symbol.h"
24
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -030025#if SLANG_VERSION < 20104
26#define slsmg_printf(msg, args...) SLsmg_printf((char *)msg, ##args)
27#define slsmg_write_nstring(msg, len) SLsmg_write_nstring((char *)msg, len)
28#define sltt_set_color(obj, name, fg, bg) SLtt_set_color(obj,(char *)name,\
29 (char *)fg, (char *)bg)
30#else
31#define slsmg_printf SLsmg_printf
32#define slsmg_write_nstring SLsmg_write_nstring
33#define sltt_set_color SLtt_set_color
34#endif
35
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030036struct ui_progress {
37 newtComponent form, scale;
38};
39
40struct ui_progress *ui_progress__new(const char *title, u64 total)
41{
42 struct ui_progress *self = malloc(sizeof(*self));
43
44 if (self != NULL) {
45 int cols;
Arnaldo Carvalho de Melo1d90f2e2010-06-09 07:13:16 -030046
47 if (use_browser <= 0)
48 return self;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030049 newtGetScreenSize(&cols, NULL);
50 cols -= 4;
51 newtCenteredWindow(cols, 1, title);
52 self->form = newtForm(NULL, NULL, 0);
53 if (self->form == NULL)
54 goto out_free_self;
55 self->scale = newtScale(0, 0, cols, total);
56 if (self->scale == NULL)
57 goto out_free_form;
Arnaldo Carvalho de Melo7f826452010-05-10 10:51:25 -030058 newtFormAddComponent(self->form, self->scale);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030059 newtRefresh();
60 }
61
62 return self;
63
64out_free_form:
65 newtFormDestroy(self->form);
66out_free_self:
67 free(self);
68 return NULL;
69}
70
71void ui_progress__update(struct ui_progress *self, u64 curr)
72{
Arnaldo Carvalho de Melo1d90f2e2010-06-09 07:13:16 -030073 /*
74 * FIXME: We should have a per UI backend way of showing progress,
75 * stdio will just show a percentage as NN%, etc.
76 */
77 if (use_browser <= 0)
78 return;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030079 newtScaleSet(self->scale, curr);
80 newtRefresh();
81}
82
83void ui_progress__delete(struct ui_progress *self)
84{
Arnaldo Carvalho de Melo1d90f2e2010-06-09 07:13:16 -030085 if (use_browser > 0) {
86 newtFormDestroy(self->form);
87 newtPopWindow();
88 }
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030089 free(self);
90}
91
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -030092static void ui_helpline__pop(void)
93{
94 newtPopHelpLine();
95}
96
97static void ui_helpline__push(const char *msg)
98{
99 newtPushHelpLine(msg);
100}
101
102static void ui_helpline__vpush(const char *fmt, va_list ap)
103{
104 char *s;
105
106 if (vasprintf(&s, fmt, ap) < 0)
107 vfprintf(stderr, fmt, ap);
108 else {
109 ui_helpline__push(s);
110 free(s);
111 }
112}
113
114static void ui_helpline__fpush(const char *fmt, ...)
115{
116 va_list ap;
117
118 va_start(ap, fmt);
119 ui_helpline__vpush(fmt, ap);
120 va_end(ap);
121}
122
123static void ui_helpline__puts(const char *msg)
124{
125 ui_helpline__pop();
126 ui_helpline__push(msg);
127}
128
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300129static char browser__last_msg[1024];
130
131int browser__show_help(const char *format, va_list ap)
132{
133 int ret;
134 static int backlog;
135
136 ret = vsnprintf(browser__last_msg + backlog,
137 sizeof(browser__last_msg) - backlog, format, ap);
138 backlog += ret;
139
140 if (browser__last_msg[backlog - 1] == '\n') {
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300141 ui_helpline__puts(browser__last_msg);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300142 newtRefresh();
143 backlog = 0;
144 }
145
146 return ret;
147}
148
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -0300149static void newt_form__set_exit_keys(newtComponent self)
150{
Arnaldo Carvalho de Meloa308f3a2010-05-16 20:29:38 -0300151 newtFormAddHotKey(self, NEWT_KEY_LEFT);
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -0300152 newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
153 newtFormAddHotKey(self, 'Q');
154 newtFormAddHotKey(self, 'q');
155 newtFormAddHotKey(self, CTRL('c'));
156}
157
158static newtComponent newt_form__new(void)
159{
160 newtComponent self = newtForm(NULL, NULL, 0);
161 if (self)
162 newt_form__set_exit_keys(self);
163 return self;
164}
165
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300166static int popup_menu(int argc, char * const argv[])
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300167{
168 struct newtExitStruct es;
169 int i, rc = -1, max_len = 5;
170 newtComponent listbox, form = newt_form__new();
171
172 if (form == NULL)
173 return -1;
174
175 listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
176 if (listbox == NULL)
177 goto out_destroy_form;
178
Arnaldo Carvalho de Melo7f826452010-05-10 10:51:25 -0300179 newtFormAddComponent(form, listbox);
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300180
181 for (i = 0; i < argc; ++i) {
182 int len = strlen(argv[i]);
183 if (len > max_len)
184 max_len = len;
185 if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
186 goto out_destroy_form;
187 }
188
189 newtCenteredWindow(max_len, argc, NULL);
190 newtFormRun(form, &es);
191 rc = newtListboxGetCurrent(listbox) - NULL;
192 if (es.reason == NEWT_EXIT_HOTKEY)
193 rc = -1;
194 newtPopWindow();
195out_destroy_form:
196 newtFormDestroy(form);
197 return rc;
198}
199
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300200static int ui__help_window(const char *text)
201{
202 struct newtExitStruct es;
203 newtComponent tb, form = newt_form__new();
204 int rc = -1;
205 int max_len = 0, nr_lines = 0;
206 const char *t;
207
208 if (form == NULL)
209 return -1;
210
211 t = text;
212 while (1) {
213 const char *sep = strchr(t, '\n');
214 int len;
215
216 if (sep == NULL)
217 sep = strchr(t, '\0');
218 len = sep - t;
219 if (max_len < len)
220 max_len = len;
221 ++nr_lines;
222 if (*sep == '\0')
223 break;
224 t = sep + 1;
225 }
226
227 tb = newtTextbox(0, 0, max_len, nr_lines, 0);
228 if (tb == NULL)
229 goto out_destroy_form;
230
231 newtTextboxSetText(tb, text);
232 newtFormAddComponent(form, tb);
233 newtCenteredWindow(max_len, nr_lines, NULL);
234 newtFormRun(form, &es);
235 newtPopWindow();
236 rc = 0;
237out_destroy_form:
238 newtFormDestroy(form);
239 return rc;
240}
241
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300242static bool dialog_yesno(const char *msg)
243{
244 /* newtWinChoice should really be accepting const char pointers... */
245 char yes[] = "Yes", no[] = "No";
Arnaldo Carvalho de Meloc0ed55d2010-04-05 12:04:23 -0300246 return newtWinChoice(NULL, yes, no, (char *)msg) == 1;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300247}
248
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300249static void ui__error_window(const char *fmt, ...)
250{
251 va_list ap;
252
253 va_start(ap, fmt);
254 newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
255 va_end(ap);
256}
257
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300258#define HE_COLORSET_TOP 50
259#define HE_COLORSET_MEDIUM 51
260#define HE_COLORSET_NORMAL 52
261#define HE_COLORSET_SELECTED 53
262#define HE_COLORSET_CODE 54
263
264static int ui_browser__percent_color(double percent, bool current)
265{
266 if (current)
267 return HE_COLORSET_SELECTED;
268 if (percent >= MIN_RED)
269 return HE_COLORSET_TOP;
270 if (percent >= MIN_GREEN)
271 return HE_COLORSET_MEDIUM;
272 return HE_COLORSET_NORMAL;
273}
274
275struct ui_browser {
276 newtComponent form, sb;
277 u64 index, first_visible_entry_idx;
278 void *first_visible_entry, *entries;
279 u16 top, left, width, height;
280 void *priv;
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300281 unsigned int (*refresh_entries)(struct ui_browser *self);
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300282 void (*seek)(struct ui_browser *self,
283 off_t offset, int whence);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300284 u32 nr_entries;
285};
286
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300287static void ui_browser__list_head_seek(struct ui_browser *self,
288 off_t offset, int whence)
289{
290 struct list_head *head = self->entries;
291 struct list_head *pos;
292
293 switch (whence) {
294 case SEEK_SET:
295 pos = head->next;
296 break;
297 case SEEK_CUR:
298 pos = self->first_visible_entry;
299 break;
300 case SEEK_END:
301 pos = head->prev;
302 break;
303 default:
304 return;
305 }
306
307 if (offset > 0) {
308 while (offset-- != 0)
309 pos = pos->next;
310 } else {
311 while (offset++ != 0)
312 pos = pos->prev;
313 }
314
315 self->first_visible_entry = pos;
316}
317
Arnaldo Carvalho de Melo8c694d22010-06-21 12:44:42 -0300318static bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
319{
320 return (self->first_visible_entry_idx + row) == self->index;
321}
322
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300323static void ui_browser__refresh_dimensions(struct ui_browser *self)
324{
325 int cols, rows;
326 newtGetScreenSize(&cols, &rows);
327
328 if (self->width > cols - 4)
329 self->width = cols - 4;
330 self->height = rows - 5;
331 if (self->height > self->nr_entries)
332 self->height = self->nr_entries;
333 self->top = (rows - self->height) / 2;
334 self->left = (cols - self->width) / 2;
335}
336
337static void ui_browser__reset_index(struct ui_browser *self)
338{
Arnaldo Carvalho de Melo8c694d22010-06-21 12:44:42 -0300339 self->index = self->first_visible_entry_idx = 0;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300340 self->seek(self, 0, SEEK_SET);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300341}
342
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300343static int ui_browser__show(struct ui_browser *self, const char *title)
344{
Arnaldo Carvalho de Melo63160f72010-07-26 13:47:15 -0300345 if (self->form != NULL) {
346 newtFormDestroy(self->form);
347 newtPopWindow();
348 }
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300349 ui_browser__refresh_dimensions(self);
Arnaldo Carvalho de Melo8d8c3692010-07-26 14:08:48 -0300350 newtCenteredWindow(self->width, self->height, title);
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300351 self->form = newt_form__new();
352 if (self->form == NULL)
353 return -1;
354
Arnaldo Carvalho de Melo8d8c3692010-07-26 14:08:48 -0300355 self->sb = newtVerticalScrollbar(self->width, 0, self->height,
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300356 HE_COLORSET_NORMAL,
357 HE_COLORSET_SELECTED);
358 if (self->sb == NULL)
359 return -1;
360
361 newtFormAddHotKey(self->form, NEWT_KEY_UP);
362 newtFormAddHotKey(self->form, NEWT_KEY_DOWN);
363 newtFormAddHotKey(self->form, NEWT_KEY_PGUP);
364 newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
365 newtFormAddHotKey(self->form, NEWT_KEY_HOME);
366 newtFormAddHotKey(self->form, NEWT_KEY_END);
367 newtFormAddComponent(self->form, self->sb);
368 return 0;
369}
370
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300371static int objdump_line__show(struct objdump_line *self, struct list_head *head,
372 int width, struct hist_entry *he, int len,
373 bool current_entry)
374{
375 if (self->offset != -1) {
376 struct symbol *sym = he->ms.sym;
377 unsigned int hits = 0;
378 double percent = 0.0;
379 int color;
380 struct sym_priv *priv = symbol__priv(sym);
381 struct sym_ext *sym_ext = priv->ext;
382 struct sym_hist *h = priv->hist;
383 s64 offset = self->offset;
384 struct objdump_line *next = objdump__get_next_ip_line(head, self);
385
386 while (offset < (s64)len &&
387 (next == NULL || offset < next->offset)) {
388 if (sym_ext) {
389 percent += sym_ext[offset].percent;
390 } else
391 hits += h->ip[offset];
392
393 ++offset;
394 }
395
396 if (sym_ext == NULL && h->sum)
397 percent = 100.0 * hits / h->sum;
398
399 color = ui_browser__percent_color(percent, current_entry);
400 SLsmg_set_color(color);
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300401 slsmg_printf(" %7.2f ", percent);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300402 if (!current_entry)
403 SLsmg_set_color(HE_COLORSET_CODE);
404 } else {
405 int color = ui_browser__percent_color(0, current_entry);
406 SLsmg_set_color(color);
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300407 slsmg_write_nstring(" ", 9);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300408 }
409
410 SLsmg_write_char(':');
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300411 slsmg_write_nstring(" ", 8);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300412 if (!*self->line)
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300413 slsmg_write_nstring(" ", width - 18);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300414 else
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300415 slsmg_write_nstring(self->line, width - 18);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300416
417 return 0;
418}
419
420static int ui_browser__refresh_entries(struct ui_browser *self)
421{
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300422 int row;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300423
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300424 newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
425 row = self->refresh_entries(self);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300426 SLsmg_set_color(HE_COLORSET_NORMAL);
427 SLsmg_fill_region(self->top + row, self->left,
428 self->height - row, self->width, ' ');
429
430 return 0;
431}
432
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300433static int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300434{
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300435 if (ui_browser__refresh_entries(self) < 0)
436 return -1;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300437
438 while (1) {
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300439 off_t offset;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300440
441 newtFormRun(self->form, es);
442
443 if (es->reason != NEWT_EXIT_HOTKEY)
444 break;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300445 if (is_exit_key(es->u.key))
446 return es->u.key;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300447 switch (es->u.key) {
448 case NEWT_KEY_DOWN:
449 if (self->index == self->nr_entries - 1)
450 break;
451 ++self->index;
452 if (self->index == self->first_visible_entry_idx + self->height) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300453 ++self->first_visible_entry_idx;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300454 self->seek(self, +1, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300455 }
456 break;
457 case NEWT_KEY_UP:
458 if (self->index == 0)
459 break;
460 --self->index;
461 if (self->index < self->first_visible_entry_idx) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300462 --self->first_visible_entry_idx;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300463 self->seek(self, -1, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300464 }
465 break;
466 case NEWT_KEY_PGDN:
Arnaldo Carvalho de Melo17930b42010-05-19 16:03:51 -0300467 case ' ':
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300468 if (self->first_visible_entry_idx + self->height > self->nr_entries - 1)
469 break;
470
471 offset = self->height;
472 if (self->index + offset > self->nr_entries - 1)
473 offset = self->nr_entries - 1 - self->index;
474 self->index += offset;
475 self->first_visible_entry_idx += offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300476 self->seek(self, +offset, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300477 break;
478 case NEWT_KEY_PGUP:
479 if (self->first_visible_entry_idx == 0)
480 break;
481
482 if (self->first_visible_entry_idx < self->height)
483 offset = self->first_visible_entry_idx;
484 else
485 offset = self->height;
486
487 self->index -= offset;
488 self->first_visible_entry_idx -= offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300489 self->seek(self, -offset, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300490 break;
491 case NEWT_KEY_HOME:
492 ui_browser__reset_index(self);
493 break;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300494 case NEWT_KEY_END:
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300495 offset = self->height - 1;
Arnaldo Carvalho de Melo63f20e72010-07-15 07:21:07 -0300496 if (offset >= self->nr_entries)
497 offset = self->nr_entries - 1;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300498
Arnaldo Carvalho de Melo63f20e72010-07-15 07:21:07 -0300499 self->index = self->nr_entries - 1;
500 self->first_visible_entry_idx = self->index - offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300501 self->seek(self, -offset, SEEK_END);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300502 break;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300503 default:
Arnaldo Carvalho de Melob66ecd92010-07-15 07:24:30 -0300504 return es->u.key;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300505 }
506 if (ui_browser__refresh_entries(self) < 0)
507 return -1;
508 }
509 return 0;
510}
511
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300512static char *callchain_list__sym_name(struct callchain_list *self,
513 char *bf, size_t bfsize)
514{
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300515 if (self->ms.sym)
516 return self->ms.sym->name;
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300517
518 snprintf(bf, bfsize, "%#Lx", self->ip);
519 return bf;
520}
521
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300522static unsigned int hist_entry__annotate_browser_refresh(struct ui_browser *self)
523{
524 struct objdump_line *pos;
525 struct list_head *head = self->entries;
526 struct hist_entry *he = self->priv;
527 int row = 0;
528 int len = he->ms.sym->end - he->ms.sym->start;
529
530 if (self->first_visible_entry == NULL || self->first_visible_entry == self->entries)
531 self->first_visible_entry = head->next;
532
533 pos = list_entry(self->first_visible_entry, struct objdump_line, node);
534
535 list_for_each_entry_from(pos, head, node) {
536 bool current_entry = ui_browser__is_current_entry(self, row);
537 SLsmg_gotorc(self->top + row, self->left);
538 objdump_line__show(pos, head, self->width,
539 he, len, current_entry);
540 if (++row == self->height)
541 break;
542 }
543
544 return row;
545}
546
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300547int hist_entry__tui_annotate(struct hist_entry *self)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300548{
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300549 struct ui_browser browser;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300550 struct newtExitStruct es;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300551 struct objdump_line *pos, *n;
552 LIST_HEAD(head);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300553 int ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300554
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300555 if (self->ms.sym == NULL)
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300556 return -1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300557
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300558 if (self->ms.map->dso->annotate_warned)
559 return -1;
560
561 if (hist_entry__annotate(self, &head) < 0) {
562 ui__error_window(browser__last_msg);
563 return -1;
564 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300565
Arnaldo Carvalho de Melo60553902010-05-15 20:40:34 -0300566 ui_helpline__push("Press <- or ESC to exit");
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300567
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300568 memset(&browser, 0, sizeof(browser));
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300569 browser.entries = &head;
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300570 browser.refresh_entries = hist_entry__annotate_browser_refresh;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300571 browser.seek = ui_browser__list_head_seek;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300572 browser.priv = self;
573 list_for_each_entry(pos, &head, node) {
574 size_t line_len = strlen(pos->line);
575 if (browser.width < line_len)
576 browser.width = line_len;
577 ++browser.nr_entries;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300578 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300579
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300580 browser.width += 18; /* Percentage */
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300581 ui_browser__show(&browser, self->ms.sym->name);
Arnaldo Carvalho de Melob61b55e2010-07-21 17:55:32 -0300582 newtFormAddHotKey(browser.form, ' ');
Srikar Dronamraju0879b102010-06-29 23:02:26 +0530583 ret = ui_browser__run(&browser, &es);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300584 newtFormDestroy(browser.form);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300585 newtPopWindow();
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300586 list_for_each_entry_safe(pos, n, &head, node) {
587 list_del(&pos->node);
588 objdump_line__free(pos);
589 }
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300590 ui_helpline__pop();
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300591 return ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300592}
593
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300594struct hist_browser {
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300595 struct ui_browser b;
596 struct hists *hists;
597 struct hist_entry *he_selection;
598 struct map_symbol *selection;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300599};
600
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300601static void hist_browser__reset(struct hist_browser *self);
602static int hist_browser__run(struct hist_browser *self, const char *title,
603 struct newtExitStruct *es);
604static unsigned int hist_browser__refresh_entries(struct ui_browser *self);
605static void ui_browser__hists_seek(struct ui_browser *self,
606 off_t offset, int whence);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300607
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300608static struct hist_browser *hist_browser__new(struct hists *hists)
609{
610 struct hist_browser *self = zalloc(sizeof(*self));
611
612 if (self) {
613 self->hists = hists;
614 self->b.refresh_entries = hist_browser__refresh_entries;
615 self->b.seek = ui_browser__hists_seek;
616 }
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300617
618 return self;
619}
620
621static void hist_browser__delete(struct hist_browser *self)
622{
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300623 newtFormDestroy(self->b.form);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300624 newtPopWindow();
625 free(self);
626}
627
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300628static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300629{
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300630 return self->he_selection;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300631}
632
633static struct thread *hist_browser__selected_thread(struct hist_browser *self)
634{
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300635 return self->he_selection->thread;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300636}
637
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300638static int hist_browser__title(char *bf, size_t size, const char *ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300639 const struct dso *dso, const struct thread *thread)
640{
641 int printed = 0;
642
643 if (thread)
644 printed += snprintf(bf + printed, size - printed,
645 "Thread: %s(%d)",
646 (thread->comm_set ? thread->comm : ""),
647 thread->pid);
648 if (dso)
649 printed += snprintf(bf + printed, size - printed,
650 "%sDSO: %s", thread ? " " : "",
651 dso->short_name);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300652 return printed ?: snprintf(bf, size, "Event: %s", ev_name);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300653}
654
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300655int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300656{
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300657 struct hist_browser *browser = hist_browser__new(self);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300658 struct pstack *fstack;
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300659 const struct thread *thread_filter = NULL;
660 const struct dso *dso_filter = NULL;
661 struct newtExitStruct es;
662 char msg[160];
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300663 int key = -1;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300664
665 if (browser == NULL)
666 return -1;
667
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300668 fstack = pstack__new(2);
669 if (fstack == NULL)
670 goto out;
671
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300672 ui_helpline__push(helpline);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300673
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300674 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300675 dso_filter, thread_filter);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300676
677 while (1) {
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300678 const struct thread *thread;
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300679 const struct dso *dso;
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300680 char *options[16];
681 int nr_options = 0, choice = 0, i,
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300682 annotate = -2, zoom_dso = -2, zoom_thread = -2;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300683
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300684 if (hist_browser__run(browser, msg, &es))
685 break;
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300686
687 thread = hist_browser__selected_thread(browser);
688 dso = browser->selection->map ? browser->selection->map->dso : NULL;
689
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300690 if (es.reason == NEWT_EXIT_HOTKEY) {
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300691 key = es.u.key;
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300692
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300693 switch (key) {
694 case NEWT_KEY_F1:
695 goto do_help;
696 case NEWT_KEY_TAB:
697 case NEWT_KEY_UNTAB:
698 /*
699 * Exit the browser, let hists__browser_tree
700 * go to the next or previous
701 */
702 goto out_free_stack;
703 default:;
704 }
705
706 key = toupper(key);
707 switch (key) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300708 case 'A':
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -0300709 if (browser->selection->map == NULL &&
710 browser->selection->map->dso->annotate_warned)
711 continue;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300712 goto do_annotate;
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300713 case 'D':
714 goto zoom_dso;
715 case 'T':
716 goto zoom_thread;
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300717 case 'H':
718 case '?':
719do_help:
720 ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
721 "<- Zoom out\n"
722 "a Annotate current symbol\n"
723 "h/?/F1 Show this window\n"
724 "d Zoom into current DSO\n"
725 "t Zoom into current Thread\n"
726 "q/CTRL+C Exit browser");
727 continue;
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300728 default:;
729 }
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300730 if (is_exit_key(key)) {
731 if (key == NEWT_KEY_ESCAPE) {
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300732 if (dialog_yesno("Do you really want to exit?"))
733 break;
734 else
735 continue;
736 } else
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300737 break;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300738 }
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300739
740 if (es.u.key == NEWT_KEY_LEFT) {
741 const void *top;
742
743 if (pstack__empty(fstack))
744 continue;
745 top = pstack__pop(fstack);
746 if (top == &dso_filter)
747 goto zoom_out_dso;
748 if (top == &thread_filter)
749 goto zoom_out_thread;
750 continue;
751 }
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300752 }
753
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300754 if (browser->selection->sym != NULL &&
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -0300755 !browser->selection->map->dso->annotate_warned &&
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300756 asprintf(&options[nr_options], "Annotate %s",
757 browser->selection->sym->name) > 0)
758 annotate = nr_options++;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300759
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300760 if (thread != NULL &&
761 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300762 (thread_filter ? "out of" : "into"),
763 (thread->comm_set ? thread->comm : ""),
764 thread->pid) > 0)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300765 zoom_thread = nr_options++;
766
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300767 if (dso != NULL &&
768 asprintf(&options[nr_options], "Zoom %s %s DSO",
769 (dso_filter ? "out of" : "into"),
770 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
771 zoom_dso = nr_options++;
772
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300773 options[nr_options++] = (char *)"Exit";
774
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300775 choice = popup_menu(nr_options, options);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300776
777 for (i = 0; i < nr_options - 1; ++i)
778 free(options[i]);
779
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300780 if (choice == nr_options - 1)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300781 break;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300782
783 if (choice == -1)
784 continue;
Arnaldo Carvalho de Meloc1ec5fe2010-05-15 20:45:31 -0300785
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300786 if (choice == annotate) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300787 struct hist_entry *he;
Arnaldo Carvalho de Meloc1ec5fe2010-05-15 20:45:31 -0300788do_annotate:
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300789 if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -0300790 browser->selection->map->dso->annotate_warned = 1;
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300791 ui_helpline__puts("No vmlinux file found, can't "
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300792 "annotate with just a "
793 "kallsyms file");
794 continue;
795 }
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300796
797 he = hist_browser__selected_entry(browser);
798 if (he == NULL)
799 continue;
800
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300801 hist_entry__tui_annotate(he);
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300802 } else if (choice == zoom_dso) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300803zoom_dso:
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300804 if (dso_filter) {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300805 pstack__remove(fstack, &dso_filter);
806zoom_out_dso:
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300807 ui_helpline__pop();
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300808 dso_filter = NULL;
809 } else {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300810 if (dso == NULL)
811 continue;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300812 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300813 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300814 dso_filter = dso;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300815 pstack__push(fstack, &dso_filter);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300816 }
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300817 hists__filter_by_dso(self, dso_filter);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300818 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300819 dso_filter, thread_filter);
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300820 hist_browser__reset(browser);
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300821 } else if (choice == zoom_thread) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300822zoom_thread:
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300823 if (thread_filter) {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300824 pstack__remove(fstack, &thread_filter);
825zoom_out_thread:
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300826 ui_helpline__pop();
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300827 thread_filter = NULL;
828 } else {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300829 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300830 thread->comm_set ? thread->comm : "",
831 thread->pid);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300832 thread_filter = thread;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300833 pstack__push(fstack, &thread_filter);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300834 }
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300835 hists__filter_by_thread(self, thread_filter);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300836 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300837 dso_filter, thread_filter);
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300838 hist_browser__reset(browser);
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300839 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300840 }
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300841out_free_stack:
842 pstack__delete(fstack);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300843out:
844 hist_browser__delete(browser);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300845 return key;
846}
847
848int hists__tui_browse_tree(struct rb_root *self, const char *help)
849{
850 struct rb_node *first = rb_first(self), *nd = first, *next;
851 int key = 0;
852
853 while (nd) {
854 struct hists *hists = rb_entry(nd, struct hists, rb_node);
855 const char *ev_name = __event_name(hists->type, hists->config);
856
857 key = hists__browse(hists, help, ev_name);
858
859 if (is_exit_key(key))
860 break;
861
862 switch (key) {
863 case NEWT_KEY_TAB:
864 next = rb_next(nd);
865 if (next)
866 nd = next;
867 break;
868 case NEWT_KEY_UNTAB:
869 if (nd == first)
870 continue;
871 nd = rb_prev(nd);
872 default:
873 break;
874 }
875 }
876
877 return key;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300878}
879
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300880static struct newtPercentTreeColors {
881 const char *topColorFg, *topColorBg;
882 const char *mediumColorFg, *mediumColorBg;
883 const char *normalColorFg, *normalColorBg;
884 const char *selColorFg, *selColorBg;
885 const char *codeColorFg, *codeColorBg;
886} defaultPercentTreeColors = {
887 "red", "lightgray",
888 "green", "lightgray",
889 "black", "lightgray",
890 "lightgray", "magenta",
891 "blue", "lightgray",
892};
893
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300894void setup_browser(void)
895{
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300896 struct newtPercentTreeColors *c = &defaultPercentTreeColors;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300897
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300898 if (!isatty(1) || !use_browser || dump_trace) {
Arnaldo Carvalho de Melo62e34362010-05-26 13:22:26 -0300899 use_browser = 0;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300900 setup_pager();
901 return;
902 }
903
904 use_browser = 1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300905 newtInit();
906 newtCls();
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300907 ui_helpline__puts(" ");
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300908 sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
909 sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
910 sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
911 sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
912 sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300913}
914
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300915void exit_browser(bool wait_for_ok)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300916{
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -0300917 if (use_browser > 0) {
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300918 if (wait_for_ok) {
919 char title[] = "Fatal Error", ok[] = "Ok";
920 newtWinMessage(title, ok, browser__last_msg);
921 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300922 newtFinished();
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300923 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300924}
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -0300925
926static void hist_browser__refresh_dimensions(struct hist_browser *self)
927{
928 /* 3 == +/- toggle symbol before actual hist_entry rendering */
929 self->b.width = 3 + (hists__sort_list_width(self->hists) +
930 sizeof("[k]"));
931}
932
933static void hist_browser__reset(struct hist_browser *self)
934{
935 self->b.nr_entries = self->hists->nr_entries;
936 hist_browser__refresh_dimensions(self);
937 ui_browser__reset_index(&self->b);
938}
939
940static char tree__folded_sign(bool unfolded)
941{
942 return unfolded ? '-' : '+';
943}
944
945static char map_symbol__folded(const struct map_symbol *self)
946{
947 return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
948}
949
950static char hist_entry__folded(const struct hist_entry *self)
951{
952 return map_symbol__folded(&self->ms);
953}
954
955static char callchain_list__folded(const struct callchain_list *self)
956{
957 return map_symbol__folded(&self->ms);
958}
959
960static bool map_symbol__toggle_fold(struct map_symbol *self)
961{
962 if (!self->has_children)
963 return false;
964
965 self->unfolded = !self->unfolded;
966 return true;
967}
968
969#define LEVEL_OFFSET_STEP 3
970
971static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
972 struct callchain_node *chain_node,
973 u64 total, int level,
974 unsigned short row,
975 off_t *row_offset,
976 bool *is_current_entry)
977{
978 struct rb_node *node;
979 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
980 u64 new_total, remaining;
981
982 if (callchain_param.mode == CHAIN_GRAPH_REL)
983 new_total = chain_node->children_hit;
984 else
985 new_total = total;
986
987 remaining = new_total;
988 node = rb_first(&chain_node->rb_root);
989 while (node) {
990 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
991 struct rb_node *next = rb_next(node);
992 u64 cumul = cumul_hits(child);
993 struct callchain_list *chain;
994 char folded_sign = ' ';
995 int first = true;
996 int extra_offset = 0;
997
998 remaining -= cumul;
999
1000 list_for_each_entry(chain, &child->val, list) {
1001 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
1002 const char *str;
1003 int color;
1004 bool was_first = first;
1005
1006 if (first) {
1007 first = false;
1008 chain->ms.has_children = chain->list.next != &child->val ||
1009 rb_first(&child->rb_root) != NULL;
1010 } else {
1011 extra_offset = LEVEL_OFFSET_STEP;
1012 chain->ms.has_children = chain->list.next == &child->val &&
1013 rb_first(&child->rb_root) != NULL;
1014 }
1015
1016 folded_sign = callchain_list__folded(chain);
1017 if (*row_offset != 0) {
1018 --*row_offset;
1019 goto do_next;
1020 }
1021
1022 alloc_str = NULL;
1023 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
1024 if (was_first) {
1025 double percent = cumul * 100.0 / new_total;
1026
1027 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
1028 str = "Not enough memory!";
1029 else
1030 str = alloc_str;
1031 }
1032
1033 color = HE_COLORSET_NORMAL;
1034 width = self->b.width - (offset + extra_offset + 2);
1035 if (ui_browser__is_current_entry(&self->b, row)) {
1036 self->selection = &chain->ms;
1037 color = HE_COLORSET_SELECTED;
1038 *is_current_entry = true;
1039 }
1040
1041 SLsmg_set_color(color);
1042 SLsmg_gotorc(self->b.top + row, self->b.left);
1043 slsmg_write_nstring(" ", offset + extra_offset);
1044 slsmg_printf("%c ", folded_sign);
1045 slsmg_write_nstring(str, width);
1046 free(alloc_str);
1047
1048 if (++row == self->b.height)
1049 goto out;
1050do_next:
1051 if (folded_sign == '+')
1052 break;
1053 }
1054
1055 if (folded_sign == '-') {
1056 const int new_level = level + (extra_offset ? 2 : 1);
1057 row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
1058 new_level, row, row_offset,
1059 is_current_entry);
1060 }
1061 if (row == self->b.height)
1062 goto out;
1063 node = next;
1064 }
1065out:
1066 return row - first_row;
1067}
1068
1069static int hist_browser__show_callchain_node(struct hist_browser *self,
1070 struct callchain_node *node,
1071 int level, unsigned short row,
1072 off_t *row_offset,
1073 bool *is_current_entry)
1074{
1075 struct callchain_list *chain;
1076 int first_row = row,
1077 offset = level * LEVEL_OFFSET_STEP,
1078 width = self->b.width - offset;
1079 char folded_sign = ' ';
1080
1081 list_for_each_entry(chain, &node->val, list) {
1082 char ipstr[BITS_PER_LONG / 4 + 1], *s;
1083 int color;
1084 /*
1085 * FIXME: This should be moved to somewhere else,
1086 * probably when the callchain is created, so as not to
1087 * traverse it all over again
1088 */
1089 chain->ms.has_children = rb_first(&node->rb_root) != NULL;
1090 folded_sign = callchain_list__folded(chain);
1091
1092 if (*row_offset != 0) {
1093 --*row_offset;
1094 continue;
1095 }
1096
1097 color = HE_COLORSET_NORMAL;
1098 if (ui_browser__is_current_entry(&self->b, row)) {
1099 self->selection = &chain->ms;
1100 color = HE_COLORSET_SELECTED;
1101 *is_current_entry = true;
1102 }
1103
1104 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
1105 SLsmg_gotorc(self->b.top + row, self->b.left);
1106 SLsmg_set_color(color);
1107 slsmg_write_nstring(" ", offset);
1108 slsmg_printf("%c ", folded_sign);
1109 slsmg_write_nstring(s, width - 2);
1110
1111 if (++row == self->b.height)
1112 goto out;
1113 }
1114
1115 if (folded_sign == '-')
1116 row += hist_browser__show_callchain_node_rb_tree(self, node,
1117 self->hists->stats.total_period,
1118 level + 1, row,
1119 row_offset,
1120 is_current_entry);
1121out:
1122 return row - first_row;
1123}
1124
1125static int hist_browser__show_callchain(struct hist_browser *self,
1126 struct rb_root *chain,
1127 int level, unsigned short row,
1128 off_t *row_offset,
1129 bool *is_current_entry)
1130{
1131 struct rb_node *nd;
1132 int first_row = row;
1133
1134 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
1135 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
1136
1137 row += hist_browser__show_callchain_node(self, node, level,
1138 row, row_offset,
1139 is_current_entry);
1140 if (row == self->b.height)
1141 break;
1142 }
1143
1144 return row - first_row;
1145}
1146
1147static int hist_browser__show_entry(struct hist_browser *self,
1148 struct hist_entry *entry,
1149 unsigned short row)
1150{
1151 char s[256];
1152 double percent;
1153 int printed = 0;
1154 int color, width = self->b.width;
1155 char folded_sign = ' ';
1156 bool current_entry = ui_browser__is_current_entry(&self->b, row);
1157 off_t row_offset = entry->row_offset;
1158
1159 if (current_entry) {
1160 self->he_selection = entry;
1161 self->selection = &entry->ms;
1162 }
1163
1164 if (symbol_conf.use_callchain) {
1165 entry->ms.has_children = !RB_EMPTY_ROOT(&entry->sorted_chain);
1166 folded_sign = hist_entry__folded(entry);
1167 }
1168
1169 if (row_offset == 0) {
1170 hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false,
1171 0, false, self->hists->stats.total_period);
1172 percent = (entry->period * 100.0) / self->hists->stats.total_period;
1173
1174 color = HE_COLORSET_SELECTED;
1175 if (!current_entry) {
1176 if (percent >= MIN_RED)
1177 color = HE_COLORSET_TOP;
1178 else if (percent >= MIN_GREEN)
1179 color = HE_COLORSET_MEDIUM;
1180 else
1181 color = HE_COLORSET_NORMAL;
1182 }
1183
1184 SLsmg_set_color(color);
1185 SLsmg_gotorc(self->b.top + row, self->b.left);
1186 if (symbol_conf.use_callchain) {
1187 slsmg_printf("%c ", folded_sign);
1188 width -= 2;
1189 }
1190 slsmg_write_nstring(s, width);
1191 ++row;
1192 ++printed;
1193 } else
1194 --row_offset;
1195
1196 if (folded_sign == '-' && row != self->b.height) {
1197 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
1198 1, row, &row_offset,
1199 &current_entry);
1200 if (current_entry)
1201 self->he_selection = entry;
1202 }
1203
1204 return printed;
1205}
1206
1207static unsigned int hist_browser__refresh_entries(struct ui_browser *self)
1208{
1209 unsigned row = 0;
1210 struct rb_node *nd;
1211 struct hist_browser *hb = container_of(self, struct hist_browser, b);
1212
1213 if (self->first_visible_entry == NULL)
1214 self->first_visible_entry = rb_first(&hb->hists->entries);
1215
1216 for (nd = self->first_visible_entry; nd; nd = rb_next(nd)) {
1217 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1218
1219 if (h->filtered)
1220 continue;
1221
1222 row += hist_browser__show_entry(hb, h, row);
1223 if (row == self->height)
1224 break;
1225 }
1226
1227 return row;
1228}
1229
1230static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
1231{
1232 struct rb_node *nd = rb_first(&self->rb_root);
1233
1234 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
1235 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
1236 struct callchain_list *chain;
1237 int first = true;
1238
1239 list_for_each_entry(chain, &child->val, list) {
1240 if (first) {
1241 first = false;
1242 chain->ms.has_children = chain->list.next != &child->val ||
1243 rb_first(&child->rb_root) != NULL;
1244 } else
1245 chain->ms.has_children = chain->list.next == &child->val &&
1246 rb_first(&child->rb_root) != NULL;
1247 }
1248
1249 callchain_node__init_have_children_rb_tree(child);
1250 }
1251}
1252
1253static void callchain_node__init_have_children(struct callchain_node *self)
1254{
1255 struct callchain_list *chain;
1256
1257 list_for_each_entry(chain, &self->val, list)
1258 chain->ms.has_children = rb_first(&self->rb_root) != NULL;
1259
1260 callchain_node__init_have_children_rb_tree(self);
1261}
1262
1263static void callchain__init_have_children(struct rb_root *self)
1264{
1265 struct rb_node *nd;
1266
1267 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
1268 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
1269 callchain_node__init_have_children(node);
1270 }
1271}
1272
1273static void hist_entry__init_have_children(struct hist_entry *self)
1274{
1275 if (!self->init_have_children) {
1276 callchain__init_have_children(&self->sorted_chain);
1277 self->init_have_children = true;
1278 }
1279}
1280
1281static struct rb_node *hists__filter_entries(struct rb_node *nd)
1282{
1283 while (nd != NULL) {
1284 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1285 if (!h->filtered)
1286 return nd;
1287
1288 nd = rb_next(nd);
1289 }
1290
1291 return NULL;
1292}
1293
1294static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
1295{
1296 while (nd != NULL) {
1297 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1298 if (!h->filtered)
1299 return nd;
1300
1301 nd = rb_prev(nd);
1302 }
1303
1304 return NULL;
1305}
1306
1307static void ui_browser__hists_seek(struct ui_browser *self,
1308 off_t offset, int whence)
1309{
1310 struct hist_entry *h;
1311 struct rb_node *nd;
1312 bool first = true;
1313
1314 switch (whence) {
1315 case SEEK_SET:
1316 nd = hists__filter_entries(rb_first(self->entries));
1317 break;
1318 case SEEK_CUR:
1319 nd = self->first_visible_entry;
1320 goto do_offset;
1321 case SEEK_END:
1322 nd = hists__filter_prev_entries(rb_last(self->entries));
1323 first = false;
1324 break;
1325 default:
1326 return;
1327 }
1328
1329 /*
1330 * Moves not relative to the first visible entry invalidates its
1331 * row_offset:
1332 */
1333 h = rb_entry(self->first_visible_entry, struct hist_entry, rb_node);
1334 h->row_offset = 0;
1335
1336 /*
1337 * Here we have to check if nd is expanded (+), if it is we can't go
1338 * the next top level hist_entry, instead we must compute an offset of
1339 * what _not_ to show and not change the first visible entry.
1340 *
1341 * This offset increments when we are going from top to bottom and
1342 * decreases when we're going from bottom to top.
1343 *
1344 * As we don't have backpointers to the top level in the callchains
1345 * structure, we need to always print the whole hist_entry callchain,
1346 * skipping the first ones that are before the first visible entry
1347 * and stop when we printed enough lines to fill the screen.
1348 */
1349do_offset:
1350 if (offset > 0) {
1351 do {
1352 h = rb_entry(nd, struct hist_entry, rb_node);
1353 if (h->ms.unfolded) {
1354 u16 remaining = h->nr_rows - h->row_offset;
1355 if (offset > remaining) {
1356 offset -= remaining;
1357 h->row_offset = 0;
1358 } else {
1359 h->row_offset += offset;
1360 offset = 0;
1361 self->first_visible_entry = nd;
1362 break;
1363 }
1364 }
1365 nd = hists__filter_entries(rb_next(nd));
1366 if (nd == NULL)
1367 break;
1368 --offset;
1369 self->first_visible_entry = nd;
1370 } while (offset != 0);
1371 } else if (offset < 0) {
1372 while (1) {
1373 h = rb_entry(nd, struct hist_entry, rb_node);
1374 if (h->ms.unfolded) {
1375 if (first) {
1376 if (-offset > h->row_offset) {
1377 offset += h->row_offset;
1378 h->row_offset = 0;
1379 } else {
1380 h->row_offset += offset;
1381 offset = 0;
1382 self->first_visible_entry = nd;
1383 break;
1384 }
1385 } else {
1386 if (-offset > h->nr_rows) {
1387 offset += h->nr_rows;
1388 h->row_offset = 0;
1389 } else {
1390 h->row_offset = h->nr_rows + offset;
1391 offset = 0;
1392 self->first_visible_entry = nd;
1393 break;
1394 }
1395 }
1396 }
1397
1398 nd = hists__filter_prev_entries(rb_prev(nd));
1399 if (nd == NULL)
1400 break;
1401 ++offset;
1402 self->first_visible_entry = nd;
1403 if (offset == 0) {
1404 /*
1405 * Last unfiltered hist_entry, check if it is
1406 * unfolded, if it is then we should have
1407 * row_offset at its last entry.
1408 */
1409 h = rb_entry(nd, struct hist_entry, rb_node);
1410 if (h->ms.unfolded)
1411 h->row_offset = h->nr_rows;
1412 break;
1413 }
1414 first = false;
1415 }
1416 } else {
1417 self->first_visible_entry = nd;
1418 h = rb_entry(nd, struct hist_entry, rb_node);
1419 h->row_offset = 0;
1420 }
1421}
1422
1423static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
1424{
1425 int n = 0;
1426 struct rb_node *nd;
1427
1428 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
1429 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
1430 struct callchain_list *chain;
1431 char folded_sign = ' '; /* No children */
1432
1433 list_for_each_entry(chain, &child->val, list) {
1434 ++n;
1435 /* We need this because we may not have children */
1436 folded_sign = callchain_list__folded(chain);
1437 if (folded_sign == '+')
1438 break;
1439 }
1440
1441 if (folded_sign == '-') /* Have children and they're unfolded */
1442 n += callchain_node__count_rows_rb_tree(child);
1443 }
1444
1445 return n;
1446}
1447
1448static int callchain_node__count_rows(struct callchain_node *node)
1449{
1450 struct callchain_list *chain;
1451 bool unfolded = false;
1452 int n = 0;
1453
1454 list_for_each_entry(chain, &node->val, list) {
1455 ++n;
1456 unfolded = chain->ms.unfolded;
1457 }
1458
1459 if (unfolded)
1460 n += callchain_node__count_rows_rb_tree(node);
1461
1462 return n;
1463}
1464
1465static int callchain__count_rows(struct rb_root *chain)
1466{
1467 struct rb_node *nd;
1468 int n = 0;
1469
1470 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
1471 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
1472 n += callchain_node__count_rows(node);
1473 }
1474
1475 return n;
1476}
1477
1478static bool hist_browser__toggle_fold(struct hist_browser *self)
1479{
1480 if (map_symbol__toggle_fold(self->selection)) {
1481 struct hist_entry *he = self->he_selection;
1482
1483 hist_entry__init_have_children(he);
1484 self->hists->nr_entries -= he->nr_rows;
1485
1486 if (he->ms.unfolded)
1487 he->nr_rows = callchain__count_rows(&he->sorted_chain);
1488 else
1489 he->nr_rows = 0;
1490 self->hists->nr_entries += he->nr_rows;
1491 self->b.nr_entries = self->hists->nr_entries;
1492
1493 return true;
1494 }
1495
1496 /* If it doesn't have children, no toggling performed */
1497 return false;
1498}
1499
1500static int hist_browser__run(struct hist_browser *self, const char *title,
1501 struct newtExitStruct *es)
1502{
1503 char str[256], unit;
1504 unsigned long nr_events = self->hists->stats.nr_events[PERF_RECORD_SAMPLE];
1505
1506 self->b.entries = &self->hists->entries;
1507 self->b.nr_entries = self->hists->nr_entries;
1508
1509 hist_browser__refresh_dimensions(self);
1510
1511 nr_events = convert_unit(nr_events, &unit);
1512 snprintf(str, sizeof(str), "Events: %lu%c ",
1513 nr_events, unit);
1514 newtDrawRootText(0, 0, str);
1515
1516 if (ui_browser__show(&self->b, title) < 0)
1517 return -1;
1518
1519 newtFormAddHotKey(self->b.form, 'A');
1520 newtFormAddHotKey(self->b.form, 'a');
1521 newtFormAddHotKey(self->b.form, '?');
1522 newtFormAddHotKey(self->b.form, 'h');
1523 newtFormAddHotKey(self->b.form, 'H');
1524 newtFormAddHotKey(self->b.form, 'd');
1525
1526 newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT);
1527 newtFormAddHotKey(self->b.form, NEWT_KEY_RIGHT);
1528 newtFormAddHotKey(self->b.form, NEWT_KEY_ENTER);
1529
1530 while (1) {
1531 ui_browser__run(&self->b, es);
1532
1533 if (es->reason != NEWT_EXIT_HOTKEY)
1534 break;
1535 switch (es->u.key) {
1536 case 'd': { /* Debug */
1537 static int seq;
1538 struct hist_entry *h = rb_entry(self->b.first_visible_entry,
1539 struct hist_entry, rb_node);
1540 ui_helpline__pop();
1541 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
1542 seq++, self->b.nr_entries,
1543 self->hists->nr_entries,
1544 self->b.height,
1545 self->b.index,
1546 self->b.first_visible_entry_idx,
1547 h->row_offset, h->nr_rows);
1548 }
1549 continue;
1550 case NEWT_KEY_ENTER:
1551 if (hist_browser__toggle_fold(self))
1552 break;
1553 /* fall thru */
1554 default:
1555 return 0;
1556 }
1557 }
1558 return 0;
1559}