blob: 9fa5b20d4090e5554111284c3d8b1e6f880ea059 [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;
46 newtGetScreenSize(&cols, NULL);
47 cols -= 4;
48 newtCenteredWindow(cols, 1, title);
49 self->form = newtForm(NULL, NULL, 0);
50 if (self->form == NULL)
51 goto out_free_self;
52 self->scale = newtScale(0, 0, cols, total);
53 if (self->scale == NULL)
54 goto out_free_form;
Arnaldo Carvalho de Melo7f826452010-05-10 10:51:25 -030055 newtFormAddComponent(self->form, self->scale);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030056 newtRefresh();
57 }
58
59 return self;
60
61out_free_form:
62 newtFormDestroy(self->form);
63out_free_self:
64 free(self);
65 return NULL;
66}
67
68void ui_progress__update(struct ui_progress *self, u64 curr)
69{
70 newtScaleSet(self->scale, curr);
71 newtRefresh();
72}
73
74void ui_progress__delete(struct ui_progress *self)
75{
76 newtFormDestroy(self->form);
77 newtPopWindow();
78 free(self);
79}
80
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -030081static void ui_helpline__pop(void)
82{
83 newtPopHelpLine();
84}
85
86static void ui_helpline__push(const char *msg)
87{
88 newtPushHelpLine(msg);
89}
90
91static void ui_helpline__vpush(const char *fmt, va_list ap)
92{
93 char *s;
94
95 if (vasprintf(&s, fmt, ap) < 0)
96 vfprintf(stderr, fmt, ap);
97 else {
98 ui_helpline__push(s);
99 free(s);
100 }
101}
102
103static void ui_helpline__fpush(const char *fmt, ...)
104{
105 va_list ap;
106
107 va_start(ap, fmt);
108 ui_helpline__vpush(fmt, ap);
109 va_end(ap);
110}
111
112static void ui_helpline__puts(const char *msg)
113{
114 ui_helpline__pop();
115 ui_helpline__push(msg);
116}
117
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300118static char browser__last_msg[1024];
119
120int browser__show_help(const char *format, va_list ap)
121{
122 int ret;
123 static int backlog;
124
125 ret = vsnprintf(browser__last_msg + backlog,
126 sizeof(browser__last_msg) - backlog, format, ap);
127 backlog += ret;
128
129 if (browser__last_msg[backlog - 1] == '\n') {
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300130 ui_helpline__puts(browser__last_msg);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300131 newtRefresh();
132 backlog = 0;
133 }
134
135 return ret;
136}
137
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -0300138static void newt_form__set_exit_keys(newtComponent self)
139{
Arnaldo Carvalho de Meloa308f3a2010-05-16 20:29:38 -0300140 newtFormAddHotKey(self, NEWT_KEY_LEFT);
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -0300141 newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
142 newtFormAddHotKey(self, 'Q');
143 newtFormAddHotKey(self, 'q');
144 newtFormAddHotKey(self, CTRL('c'));
145}
146
147static newtComponent newt_form__new(void)
148{
149 newtComponent self = newtForm(NULL, NULL, 0);
150 if (self)
151 newt_form__set_exit_keys(self);
152 return self;
153}
154
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300155static int popup_menu(int argc, char * const argv[])
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300156{
157 struct newtExitStruct es;
158 int i, rc = -1, max_len = 5;
159 newtComponent listbox, form = newt_form__new();
160
161 if (form == NULL)
162 return -1;
163
164 listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
165 if (listbox == NULL)
166 goto out_destroy_form;
167
Arnaldo Carvalho de Melo7f826452010-05-10 10:51:25 -0300168 newtFormAddComponent(form, listbox);
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300169
170 for (i = 0; i < argc; ++i) {
171 int len = strlen(argv[i]);
172 if (len > max_len)
173 max_len = len;
174 if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
175 goto out_destroy_form;
176 }
177
178 newtCenteredWindow(max_len, argc, NULL);
179 newtFormRun(form, &es);
180 rc = newtListboxGetCurrent(listbox) - NULL;
181 if (es.reason == NEWT_EXIT_HOTKEY)
182 rc = -1;
183 newtPopWindow();
184out_destroy_form:
185 newtFormDestroy(form);
186 return rc;
187}
188
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300189static int ui__help_window(const char *text)
190{
191 struct newtExitStruct es;
192 newtComponent tb, form = newt_form__new();
193 int rc = -1;
194 int max_len = 0, nr_lines = 0;
195 const char *t;
196
197 if (form == NULL)
198 return -1;
199
200 t = text;
201 while (1) {
202 const char *sep = strchr(t, '\n');
203 int len;
204
205 if (sep == NULL)
206 sep = strchr(t, '\0');
207 len = sep - t;
208 if (max_len < len)
209 max_len = len;
210 ++nr_lines;
211 if (*sep == '\0')
212 break;
213 t = sep + 1;
214 }
215
216 tb = newtTextbox(0, 0, max_len, nr_lines, 0);
217 if (tb == NULL)
218 goto out_destroy_form;
219
220 newtTextboxSetText(tb, text);
221 newtFormAddComponent(form, tb);
222 newtCenteredWindow(max_len, nr_lines, NULL);
223 newtFormRun(form, &es);
224 newtPopWindow();
225 rc = 0;
226out_destroy_form:
227 newtFormDestroy(form);
228 return rc;
229}
230
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300231static bool dialog_yesno(const char *msg)
232{
233 /* newtWinChoice should really be accepting const char pointers... */
234 char yes[] = "Yes", no[] = "No";
Arnaldo Carvalho de Meloc0ed55d2010-04-05 12:04:23 -0300235 return newtWinChoice(NULL, yes, no, (char *)msg) == 1;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300236}
237
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300238static void ui__error_window(const char *fmt, ...)
239{
240 va_list ap;
241
242 va_start(ap, fmt);
243 newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
244 va_end(ap);
245}
246
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300247#define HE_COLORSET_TOP 50
248#define HE_COLORSET_MEDIUM 51
249#define HE_COLORSET_NORMAL 52
250#define HE_COLORSET_SELECTED 53
251#define HE_COLORSET_CODE 54
252
253static int ui_browser__percent_color(double percent, bool current)
254{
255 if (current)
256 return HE_COLORSET_SELECTED;
257 if (percent >= MIN_RED)
258 return HE_COLORSET_TOP;
259 if (percent >= MIN_GREEN)
260 return HE_COLORSET_MEDIUM;
261 return HE_COLORSET_NORMAL;
262}
263
264struct ui_browser {
265 newtComponent form, sb;
266 u64 index, first_visible_entry_idx;
267 void *first_visible_entry, *entries;
268 u16 top, left, width, height;
269 void *priv;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300270 void (*seek)(struct ui_browser *self,
271 off_t offset, int whence);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300272 u32 nr_entries;
273};
274
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300275static void ui_browser__list_head_seek(struct ui_browser *self,
276 off_t offset, int whence)
277{
278 struct list_head *head = self->entries;
279 struct list_head *pos;
280
281 switch (whence) {
282 case SEEK_SET:
283 pos = head->next;
284 break;
285 case SEEK_CUR:
286 pos = self->first_visible_entry;
287 break;
288 case SEEK_END:
289 pos = head->prev;
290 break;
291 default:
292 return;
293 }
294
295 if (offset > 0) {
296 while (offset-- != 0)
297 pos = pos->next;
298 } else {
299 while (offset++ != 0)
300 pos = pos->prev;
301 }
302
303 self->first_visible_entry = pos;
304}
305
Arnaldo Carvalho de Melo8c694d22010-06-21 12:44:42 -0300306static bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
307{
308 return (self->first_visible_entry_idx + row) == self->index;
309}
310
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300311static void ui_browser__refresh_dimensions(struct ui_browser *self)
312{
313 int cols, rows;
314 newtGetScreenSize(&cols, &rows);
315
316 if (self->width > cols - 4)
317 self->width = cols - 4;
318 self->height = rows - 5;
319 if (self->height > self->nr_entries)
320 self->height = self->nr_entries;
321 self->top = (rows - self->height) / 2;
322 self->left = (cols - self->width) / 2;
323}
324
325static void ui_browser__reset_index(struct ui_browser *self)
326{
Arnaldo Carvalho de Melo8c694d22010-06-21 12:44:42 -0300327 self->index = self->first_visible_entry_idx = 0;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300328 self->seek(self, 0, SEEK_SET);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300329}
330
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300331static int ui_browser__show(struct ui_browser *self, const char *title)
332{
333 if (self->form != NULL)
334 return 0;
335 ui_browser__refresh_dimensions(self);
336 newtCenteredWindow(self->width + 2, self->height, title);
337 self->form = newt_form__new();
338 if (self->form == NULL)
339 return -1;
340
341 self->sb = newtVerticalScrollbar(self->width + 1, 0, self->height,
342 HE_COLORSET_NORMAL,
343 HE_COLORSET_SELECTED);
344 if (self->sb == NULL)
345 return -1;
346
347 newtFormAddHotKey(self->form, NEWT_KEY_UP);
348 newtFormAddHotKey(self->form, NEWT_KEY_DOWN);
349 newtFormAddHotKey(self->form, NEWT_KEY_PGUP);
350 newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
351 newtFormAddHotKey(self->form, NEWT_KEY_HOME);
352 newtFormAddHotKey(self->form, NEWT_KEY_END);
353 newtFormAddComponent(self->form, self->sb);
354 return 0;
355}
356
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300357static int objdump_line__show(struct objdump_line *self, struct list_head *head,
358 int width, struct hist_entry *he, int len,
359 bool current_entry)
360{
361 if (self->offset != -1) {
362 struct symbol *sym = he->ms.sym;
363 unsigned int hits = 0;
364 double percent = 0.0;
365 int color;
366 struct sym_priv *priv = symbol__priv(sym);
367 struct sym_ext *sym_ext = priv->ext;
368 struct sym_hist *h = priv->hist;
369 s64 offset = self->offset;
370 struct objdump_line *next = objdump__get_next_ip_line(head, self);
371
372 while (offset < (s64)len &&
373 (next == NULL || offset < next->offset)) {
374 if (sym_ext) {
375 percent += sym_ext[offset].percent;
376 } else
377 hits += h->ip[offset];
378
379 ++offset;
380 }
381
382 if (sym_ext == NULL && h->sum)
383 percent = 100.0 * hits / h->sum;
384
385 color = ui_browser__percent_color(percent, current_entry);
386 SLsmg_set_color(color);
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300387 slsmg_printf(" %7.2f ", percent);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300388 if (!current_entry)
389 SLsmg_set_color(HE_COLORSET_CODE);
390 } else {
391 int color = ui_browser__percent_color(0, current_entry);
392 SLsmg_set_color(color);
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300393 slsmg_write_nstring(" ", 9);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300394 }
395
396 SLsmg_write_char(':');
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300397 slsmg_write_nstring(" ", 8);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300398 if (!*self->line)
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300399 slsmg_write_nstring(" ", width - 18);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300400 else
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300401 slsmg_write_nstring(self->line, width - 18);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300402
403 return 0;
404}
405
406static int ui_browser__refresh_entries(struct ui_browser *self)
407{
408 struct objdump_line *pos;
409 struct list_head *head = self->entries;
410 struct hist_entry *he = self->priv;
411 int row = 0;
412 int len = he->ms.sym->end - he->ms.sym->start;
413
414 if (self->first_visible_entry == NULL || self->first_visible_entry == self->entries)
415 self->first_visible_entry = head->next;
416
417 pos = list_entry(self->first_visible_entry, struct objdump_line, node);
418
419 list_for_each_entry_from(pos, head, node) {
Arnaldo Carvalho de Melo8c694d22010-06-21 12:44:42 -0300420 bool current_entry = ui_browser__is_current_entry(self, row);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300421 SLsmg_gotorc(self->top + row, self->left);
422 objdump_line__show(pos, head, self->width,
423 he, len, current_entry);
424 if (++row == self->height)
425 break;
426 }
427
428 SLsmg_set_color(HE_COLORSET_NORMAL);
429 SLsmg_fill_region(self->top + row, self->left,
430 self->height - row, self->width, ' ');
431
432 return 0;
433}
434
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300435static int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300436{
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300437 if (ui_browser__refresh_entries(self) < 0)
438 return -1;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300439
440 while (1) {
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300441 off_t offset;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300442
443 newtFormRun(self->form, es);
444
445 if (es->reason != NEWT_EXIT_HOTKEY)
446 break;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300447 if (is_exit_key(es->u.key))
448 return es->u.key;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300449 switch (es->u.key) {
450 case NEWT_KEY_DOWN:
451 if (self->index == self->nr_entries - 1)
452 break;
453 ++self->index;
454 if (self->index == self->first_visible_entry_idx + self->height) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300455 ++self->first_visible_entry_idx;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300456 self->seek(self, +1, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300457 }
458 break;
459 case NEWT_KEY_UP:
460 if (self->index == 0)
461 break;
462 --self->index;
463 if (self->index < self->first_visible_entry_idx) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300464 --self->first_visible_entry_idx;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300465 self->seek(self, -1, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300466 }
467 break;
468 case NEWT_KEY_PGDN:
Arnaldo Carvalho de Melo17930b42010-05-19 16:03:51 -0300469 case ' ':
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300470 if (self->first_visible_entry_idx + self->height > self->nr_entries - 1)
471 break;
472
473 offset = self->height;
474 if (self->index + offset > self->nr_entries - 1)
475 offset = self->nr_entries - 1 - self->index;
476 self->index += offset;
477 self->first_visible_entry_idx += offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300478 self->seek(self, +offset, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300479 break;
480 case NEWT_KEY_PGUP:
481 if (self->first_visible_entry_idx == 0)
482 break;
483
484 if (self->first_visible_entry_idx < self->height)
485 offset = self->first_visible_entry_idx;
486 else
487 offset = self->height;
488
489 self->index -= offset;
490 self->first_visible_entry_idx -= offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300491 self->seek(self, -offset, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300492 break;
493 case NEWT_KEY_HOME:
494 ui_browser__reset_index(self);
495 break;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300496 case NEWT_KEY_END:
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300497 offset = self->height - 1;
498
499 if (offset > self->nr_entries)
500 offset = self->nr_entries;
501
502 self->index = self->first_visible_entry_idx = self->nr_entries - 1 - offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300503 self->seek(self, -offset, SEEK_END);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300504 break;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300505 case NEWT_KEY_RIGHT:
Arnaldo Carvalho de Melo60553902010-05-15 20:40:34 -0300506 case NEWT_KEY_LEFT:
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300507 case NEWT_KEY_TAB:
508 return es->u.key;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300509 default:
510 continue;
511 }
512 if (ui_browser__refresh_entries(self) < 0)
513 return -1;
514 }
515 return 0;
516}
517
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300518/*
519 * When debugging newt problems it was useful to be able to "unroll"
520 * the calls to newtCheckBoxTreeAdd{Array,Item}, so that we can generate
521 * a source file with the sequence of calls to these methods, to then
522 * tweak the arrays to get the intended results, so I'm keeping this code
523 * here, may be useful again in the future.
524 */
525#undef NEWT_DEBUG
526
527static void newt_checkbox_tree__add(newtComponent tree, const char *str,
528 void *priv, int *indexes)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300529{
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300530#ifdef NEWT_DEBUG
531 /* Print the newtCheckboxTreeAddArray to tinker with its index arrays */
532 int i = 0, len = 40 - strlen(str);
533
534 fprintf(stderr,
535 "\tnewtCheckboxTreeAddItem(tree, %*.*s\"%s\", (void *)%p, 0, ",
536 len, len, " ", str, priv);
537 while (indexes[i] != NEWT_ARG_LAST) {
538 if (indexes[i] != NEWT_ARG_APPEND)
539 fprintf(stderr, " %d,", indexes[i]);
540 else
541 fprintf(stderr, " %s,", "NEWT_ARG_APPEND");
542 ++i;
543 }
544 fprintf(stderr, " %s", " NEWT_ARG_LAST);\n");
545 fflush(stderr);
546#endif
547 newtCheckboxTreeAddArray(tree, str, priv, 0, indexes);
548}
549
550static char *callchain_list__sym_name(struct callchain_list *self,
551 char *bf, size_t bfsize)
552{
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300553 if (self->ms.sym)
554 return self->ms.sym->name;
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300555
556 snprintf(bf, bfsize, "%#Lx", self->ip);
557 return bf;
558}
559
560static void __callchain__append_graph_browser(struct callchain_node *self,
561 newtComponent tree, u64 total,
562 int *indexes, int depth)
563{
564 struct rb_node *node;
565 u64 new_total, remaining;
566 int idx = 0;
567
568 if (callchain_param.mode == CHAIN_GRAPH_REL)
569 new_total = self->children_hit;
570 else
571 new_total = total;
572
573 remaining = new_total;
574 node = rb_first(&self->rb_root);
575 while (node) {
576 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
577 struct rb_node *next = rb_next(node);
578 u64 cumul = cumul_hits(child);
579 struct callchain_list *chain;
580 int first = true, printed = 0;
581 int chain_idx = -1;
582 remaining -= cumul;
583
584 indexes[depth] = NEWT_ARG_APPEND;
585 indexes[depth + 1] = NEWT_ARG_LAST;
586
587 list_for_each_entry(chain, &child->val, list) {
588 char ipstr[BITS_PER_LONG / 4 + 1],
589 *alloc_str = NULL;
590 const char *str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
591
592 if (first) {
593 double percent = cumul * 100.0 / new_total;
594
595 first = false;
596 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
597 str = "Not enough memory!";
598 else
599 str = alloc_str;
600 } else {
601 indexes[depth] = idx;
602 indexes[depth + 1] = NEWT_ARG_APPEND;
603 indexes[depth + 2] = NEWT_ARG_LAST;
604 ++chain_idx;
605 }
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300606 newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300607 free(alloc_str);
608 ++printed;
609 }
610
611 indexes[depth] = idx;
612 if (chain_idx != -1)
613 indexes[depth + 1] = chain_idx;
614 if (printed != 0)
615 ++idx;
616 __callchain__append_graph_browser(child, tree, new_total, indexes,
617 depth + (chain_idx != -1 ? 2 : 1));
618 node = next;
619 }
620}
621
622static void callchain__append_graph_browser(struct callchain_node *self,
623 newtComponent tree, u64 total,
624 int *indexes, int parent_idx)
625{
626 struct callchain_list *chain;
627 int i = 0;
628
629 indexes[1] = NEWT_ARG_APPEND;
630 indexes[2] = NEWT_ARG_LAST;
631
632 list_for_each_entry(chain, &self->val, list) {
633 char ipstr[BITS_PER_LONG / 4 + 1], *str;
634
635 if (chain->ip >= PERF_CONTEXT_MAX)
636 continue;
637
638 if (!i++ && sort__first_dimension == SORT_SYM)
639 continue;
640
641 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300642 newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300643 }
644
645 indexes[1] = parent_idx;
646 indexes[2] = NEWT_ARG_APPEND;
647 indexes[3] = NEWT_ARG_LAST;
648 __callchain__append_graph_browser(self, tree, total, indexes, 2);
649}
650
651static void hist_entry__append_callchain_browser(struct hist_entry *self,
652 newtComponent tree, u64 total, int parent_idx)
653{
654 struct rb_node *rb_node;
655 int indexes[1024] = { [0] = parent_idx, };
656 int idx = 0;
657 struct callchain_node *chain;
658
659 rb_node = rb_first(&self->sorted_chain);
660 while (rb_node) {
661 chain = rb_entry(rb_node, struct callchain_node, rb_node);
662 switch (callchain_param.mode) {
663 case CHAIN_FLAT:
664 break;
665 case CHAIN_GRAPH_ABS: /* falldown */
666 case CHAIN_GRAPH_REL:
667 callchain__append_graph_browser(chain, tree, total, indexes, idx++);
668 break;
669 case CHAIN_NONE:
670 default:
671 break;
672 }
673 rb_node = rb_next(rb_node);
674 }
675}
676
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300677static size_t hist_entry__append_browser(struct hist_entry *self,
678 newtComponent tree, u64 total)
679{
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300680 char s[256];
681 size_t ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300682
683 if (symbol_conf.exclude_other && !self->parent)
684 return 0;
685
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300686 ret = hist_entry__snprintf(self, s, sizeof(s), NULL,
687 false, 0, false, total);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300688 if (symbol_conf.use_callchain) {
689 int indexes[2];
690
691 indexes[0] = NEWT_ARG_APPEND;
692 indexes[1] = NEWT_ARG_LAST;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300693 newt_checkbox_tree__add(tree, s, &self->ms, indexes);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300694 } else
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300695 newtListboxAppendEntry(tree, s, &self->ms);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300696
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300697 return ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300698}
699
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300700int hist_entry__tui_annotate(struct hist_entry *self)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300701{
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300702 struct ui_browser browser;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300703 struct newtExitStruct es;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300704 struct objdump_line *pos, *n;
705 LIST_HEAD(head);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300706 int ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300707
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300708 if (self->ms.sym == NULL)
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300709 return -1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300710
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300711 if (self->ms.map->dso->annotate_warned)
712 return -1;
713
714 if (hist_entry__annotate(self, &head) < 0) {
715 ui__error_window(browser__last_msg);
716 return -1;
717 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300718
Arnaldo Carvalho de Melo60553902010-05-15 20:40:34 -0300719 ui_helpline__push("Press <- or ESC to exit");
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300720
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300721 memset(&browser, 0, sizeof(browser));
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300722 browser.entries = &head;
723 browser.seek = ui_browser__list_head_seek;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300724 browser.priv = self;
725 list_for_each_entry(pos, &head, node) {
726 size_t line_len = strlen(pos->line);
727 if (browser.width < line_len)
728 browser.width = line_len;
729 ++browser.nr_entries;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300730 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300731
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300732 browser.width += 18; /* Percentage */
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300733 ui_browser__show(&browser, self->ms.sym->name);
734 ui_browser__run(&browser, &es);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300735 newtFormDestroy(browser.form);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300736 newtPopWindow();
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300737 list_for_each_entry_safe(pos, n, &head, node) {
738 list_del(&pos->node);
739 objdump_line__free(pos);
740 }
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300741 ui_helpline__pop();
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300742 return ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300743}
744
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300745static const void *newt__symbol_tree_get_current(newtComponent self)
746{
747 if (symbol_conf.use_callchain)
748 return newtCheckboxTreeGetCurrent(self);
749 return newtListboxGetCurrent(self);
750}
751
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300752static void hist_browser__selection(newtComponent self, void *data)
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300753{
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300754 const struct map_symbol **symbol_ptr = data;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300755 *symbol_ptr = newt__symbol_tree_get_current(self);
756}
757
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300758struct hist_browser {
759 newtComponent form, tree;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300760 const struct map_symbol *selection;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300761};
762
763static struct hist_browser *hist_browser__new(void)
764{
765 struct hist_browser *self = malloc(sizeof(*self));
766
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300767 if (self != NULL)
768 self->form = NULL;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300769
770 return self;
771}
772
773static void hist_browser__delete(struct hist_browser *self)
774{
775 newtFormDestroy(self->form);
776 newtPopWindow();
777 free(self);
778}
779
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300780static int hist_browser__populate(struct hist_browser *self, struct hists *hists,
781 const char *title)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300782{
783 int max_len = 0, idx, cols, rows;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300784 struct ui_progress *progress;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300785 struct rb_node *nd;
786 u64 curr_hist = 0;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300787 char seq[] = ".", unit;
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300788 char str[256];
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300789 unsigned long nr_events = hists->stats.nr_events[PERF_RECORD_SAMPLE];
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300790
791 if (self->form) {
792 newtFormDestroy(self->form);
793 newtPopWindow();
794 }
795
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300796 nr_events = convert_unit(nr_events, &unit);
797 snprintf(str, sizeof(str), "Events: %lu%c ",
798 nr_events, unit);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300799 newtDrawRootText(0, 0, str);
800
801 newtGetScreenSize(NULL, &rows);
802
803 if (symbol_conf.use_callchain)
804 self->tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq,
805 NEWT_FLAG_SCROLL);
806 else
807 self->tree = newtListbox(0, 0, rows - 5,
808 (NEWT_FLAG_SCROLL |
809 NEWT_FLAG_RETURNEXIT));
810
811 newtComponentAddCallback(self->tree, hist_browser__selection,
812 &self->selection);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300813
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300814 progress = ui_progress__new("Adding entries to the browser...",
815 hists->nr_entries);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300816 if (progress == NULL)
817 return -1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300818
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300819 idx = 0;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300820 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300821 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300822 int len;
823
824 if (h->filtered)
825 continue;
826
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300827 len = hist_entry__append_browser(h, self->tree, hists->stats.total_period);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300828 if (len > max_len)
829 max_len = len;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300830 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300831 hist_entry__append_callchain_browser(h, self->tree,
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300832 hists->stats.total_period, idx++);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300833 ++curr_hist;
834 if (curr_hist % 5)
835 ui_progress__update(progress, curr_hist);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300836 }
837
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300838 ui_progress__delete(progress);
839
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300840 newtGetScreenSize(&cols, &rows);
841
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300842 if (max_len > cols)
843 max_len = cols - 3;
844
845 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300846 newtListboxSetWidth(self->tree, max_len);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300847
848 newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0),
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300849 rows - 5, title);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300850 self->form = newt_form__new();
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300851 if (self->form == NULL)
852 return -1;
853
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300854 newtFormAddHotKey(self->form, 'A');
855 newtFormAddHotKey(self->form, 'a');
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300856 newtFormAddHotKey(self->form, 'D');
857 newtFormAddHotKey(self->form, 'd');
858 newtFormAddHotKey(self->form, 'T');
859 newtFormAddHotKey(self->form, 't');
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300860 newtFormAddHotKey(self->form, '?');
861 newtFormAddHotKey(self->form, 'H');
862 newtFormAddHotKey(self->form, 'h');
863 newtFormAddHotKey(self->form, NEWT_KEY_F1);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300864 newtFormAddHotKey(self->form, NEWT_KEY_RIGHT);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300865 newtFormAddHotKey(self->form, NEWT_KEY_TAB);
866 newtFormAddHotKey(self->form, NEWT_KEY_UNTAB);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300867 newtFormAddComponents(self->form, self->tree, NULL);
868 self->selection = newt__symbol_tree_get_current(self->tree);
869
870 return 0;
871}
872
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300873static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300874{
875 int *indexes;
876
877 if (!symbol_conf.use_callchain)
878 goto out;
879
880 indexes = newtCheckboxTreeFindItem(self->tree, (void *)self->selection);
881 if (indexes) {
882 bool is_hist_entry = indexes[1] == NEWT_ARG_LAST;
883 free(indexes);
884 if (is_hist_entry)
885 goto out;
886 }
887 return NULL;
888out:
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300889 return container_of(self->selection, struct hist_entry, ms);
890}
891
892static struct thread *hist_browser__selected_thread(struct hist_browser *self)
893{
894 struct hist_entry *he = hist_browser__selected_entry(self);
895 return he ? he->thread : NULL;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300896}
897
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300898static int hist_browser__title(char *bf, size_t size, const char *ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300899 const struct dso *dso, const struct thread *thread)
900{
901 int printed = 0;
902
903 if (thread)
904 printed += snprintf(bf + printed, size - printed,
905 "Thread: %s(%d)",
906 (thread->comm_set ? thread->comm : ""),
907 thread->pid);
908 if (dso)
909 printed += snprintf(bf + printed, size - printed,
910 "%sDSO: %s", thread ? " " : "",
911 dso->short_name);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300912 return printed ?: snprintf(bf, size, "Event: %s", ev_name);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300913}
914
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300915int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300916{
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300917 struct hist_browser *browser = hist_browser__new();
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300918 struct pstack *fstack;
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300919 const struct thread *thread_filter = NULL;
920 const struct dso *dso_filter = NULL;
921 struct newtExitStruct es;
922 char msg[160];
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300923 int key = -1;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300924
925 if (browser == NULL)
926 return -1;
927
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300928 fstack = pstack__new(2);
929 if (fstack == NULL)
930 goto out;
931
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300932 ui_helpline__push(helpline);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300933
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300934 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300935 dso_filter, thread_filter);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300936 if (hist_browser__populate(browser, self, msg) < 0)
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300937 goto out_free_stack;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300938
939 while (1) {
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300940 const struct thread *thread;
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300941 const struct dso *dso;
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300942 char *options[16];
943 int nr_options = 0, choice = 0, i,
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300944 annotate = -2, zoom_dso = -2, zoom_thread = -2;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300945
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300946 newtFormRun(browser->form, &es);
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300947
948 thread = hist_browser__selected_thread(browser);
949 dso = browser->selection->map ? browser->selection->map->dso : NULL;
950
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300951 if (es.reason == NEWT_EXIT_HOTKEY) {
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300952 key = es.u.key;
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300953
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300954 switch (key) {
955 case NEWT_KEY_F1:
956 goto do_help;
957 case NEWT_KEY_TAB:
958 case NEWT_KEY_UNTAB:
959 /*
960 * Exit the browser, let hists__browser_tree
961 * go to the next or previous
962 */
963 goto out_free_stack;
964 default:;
965 }
966
967 key = toupper(key);
968 switch (key) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300969 case 'A':
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -0300970 if (browser->selection->map == NULL &&
971 browser->selection->map->dso->annotate_warned)
972 continue;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300973 goto do_annotate;
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300974 case 'D':
975 goto zoom_dso;
976 case 'T':
977 goto zoom_thread;
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300978 case 'H':
979 case '?':
980do_help:
981 ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
982 "<- Zoom out\n"
983 "a Annotate current symbol\n"
984 "h/?/F1 Show this window\n"
985 "d Zoom into current DSO\n"
986 "t Zoom into current Thread\n"
987 "q/CTRL+C Exit browser");
988 continue;
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300989 default:;
990 }
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300991 if (is_exit_key(key)) {
992 if (key == NEWT_KEY_ESCAPE) {
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300993 if (dialog_yesno("Do you really want to exit?"))
994 break;
995 else
996 continue;
997 } else
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300998 break;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300999 }
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001000
1001 if (es.u.key == NEWT_KEY_LEFT) {
1002 const void *top;
1003
1004 if (pstack__empty(fstack))
1005 continue;
1006 top = pstack__pop(fstack);
1007 if (top == &dso_filter)
1008 goto zoom_out_dso;
1009 if (top == &thread_filter)
1010 goto zoom_out_thread;
1011 continue;
1012 }
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001013 }
1014
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001015 if (browser->selection->sym != NULL &&
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -03001016 !browser->selection->map->dso->annotate_warned &&
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001017 asprintf(&options[nr_options], "Annotate %s",
1018 browser->selection->sym->name) > 0)
1019 annotate = nr_options++;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001020
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001021 if (thread != NULL &&
1022 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001023 (thread_filter ? "out of" : "into"),
1024 (thread->comm_set ? thread->comm : ""),
1025 thread->pid) > 0)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001026 zoom_thread = nr_options++;
1027
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001028 if (dso != NULL &&
1029 asprintf(&options[nr_options], "Zoom %s %s DSO",
1030 (dso_filter ? "out of" : "into"),
1031 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
1032 zoom_dso = nr_options++;
1033
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001034 options[nr_options++] = (char *)"Exit";
1035
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001036 choice = popup_menu(nr_options, options);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001037
1038 for (i = 0; i < nr_options - 1; ++i)
1039 free(options[i]);
1040
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001041 if (choice == nr_options - 1)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001042 break;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001043
1044 if (choice == -1)
1045 continue;
Arnaldo Carvalho de Meloc1ec5fe2010-05-15 20:45:31 -03001046
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001047 if (choice == annotate) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001048 struct hist_entry *he;
Arnaldo Carvalho de Meloc1ec5fe2010-05-15 20:45:31 -03001049do_annotate:
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -03001050 if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -03001051 browser->selection->map->dso->annotate_warned = 1;
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001052 ui_helpline__puts("No vmlinux file found, can't "
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -03001053 "annotate with just a "
1054 "kallsyms file");
1055 continue;
1056 }
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001057
1058 he = hist_browser__selected_entry(browser);
1059 if (he == NULL)
1060 continue;
1061
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001062 hist_entry__tui_annotate(he);
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001063 } else if (choice == zoom_dso) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001064zoom_dso:
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001065 if (dso_filter) {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001066 pstack__remove(fstack, &dso_filter);
1067zoom_out_dso:
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001068 ui_helpline__pop();
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001069 dso_filter = NULL;
1070 } else {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001071 if (dso == NULL)
1072 continue;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001073 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001074 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001075 dso_filter = dso;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001076 pstack__push(fstack, &dso_filter);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001077 }
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001078 hists__filter_by_dso(self, dso_filter);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001079 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001080 dso_filter, thread_filter);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001081 if (hist_browser__populate(browser, self, msg) < 0)
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001082 goto out;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001083 } else if (choice == zoom_thread) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001084zoom_thread:
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001085 if (thread_filter) {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001086 pstack__remove(fstack, &thread_filter);
1087zoom_out_thread:
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001088 ui_helpline__pop();
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001089 thread_filter = NULL;
1090 } else {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001091 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001092 thread->comm_set ? thread->comm : "",
1093 thread->pid);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001094 thread_filter = thread;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001095 pstack__push(fstack, &thread_filter);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001096 }
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001097 hists__filter_by_thread(self, thread_filter);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001098 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001099 dso_filter, thread_filter);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001100 if (hist_browser__populate(browser, self, msg) < 0)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001101 goto out;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -03001102 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001103 }
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001104out_free_stack:
1105 pstack__delete(fstack);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -03001106out:
1107 hist_browser__delete(browser);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001108 return key;
1109}
1110
1111int hists__tui_browse_tree(struct rb_root *self, const char *help)
1112{
1113 struct rb_node *first = rb_first(self), *nd = first, *next;
1114 int key = 0;
1115
1116 while (nd) {
1117 struct hists *hists = rb_entry(nd, struct hists, rb_node);
1118 const char *ev_name = __event_name(hists->type, hists->config);
1119
1120 key = hists__browse(hists, help, ev_name);
1121
1122 if (is_exit_key(key))
1123 break;
1124
1125 switch (key) {
1126 case NEWT_KEY_TAB:
1127 next = rb_next(nd);
1128 if (next)
1129 nd = next;
1130 break;
1131 case NEWT_KEY_UNTAB:
1132 if (nd == first)
1133 continue;
1134 nd = rb_prev(nd);
1135 default:
1136 break;
1137 }
1138 }
1139
1140 return key;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001141}
1142
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001143static struct newtPercentTreeColors {
1144 const char *topColorFg, *topColorBg;
1145 const char *mediumColorFg, *mediumColorBg;
1146 const char *normalColorFg, *normalColorBg;
1147 const char *selColorFg, *selColorBg;
1148 const char *codeColorFg, *codeColorBg;
1149} defaultPercentTreeColors = {
1150 "red", "lightgray",
1151 "green", "lightgray",
1152 "black", "lightgray",
1153 "lightgray", "magenta",
1154 "blue", "lightgray",
1155};
1156
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001157void setup_browser(void)
1158{
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001159 struct newtPercentTreeColors *c = &defaultPercentTreeColors;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001160
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001161 if (!isatty(1) || !use_browser || dump_trace) {
Arnaldo Carvalho de Melo62e34362010-05-26 13:22:26 -03001162 use_browser = 0;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -03001163 setup_pager();
1164 return;
1165 }
1166
1167 use_browser = 1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001168 newtInit();
1169 newtCls();
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001170 ui_helpline__puts(" ");
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -03001171 sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
1172 sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
1173 sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
1174 sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
1175 sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001176}
1177
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -03001178void exit_browser(bool wait_for_ok)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001179{
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -03001180 if (use_browser > 0) {
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -03001181 if (wait_for_ok) {
1182 char title[] = "Fatal Error", ok[] = "Ok";
1183 newtWinMessage(title, ok, browser__last_msg);
1184 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001185 newtFinished();
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -03001186 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001187}