blob: 7bdbfd3e24d25efbb31f3fad02e91747aca16597 [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 Melo9f61d852010-06-21 19:38:33 -0300270 unsigned int (*refresh_entries)(struct ui_browser *self);
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300271 void (*seek)(struct ui_browser *self,
272 off_t offset, int whence);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300273 u32 nr_entries;
274};
275
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300276static void ui_browser__list_head_seek(struct ui_browser *self,
277 off_t offset, int whence)
278{
279 struct list_head *head = self->entries;
280 struct list_head *pos;
281
282 switch (whence) {
283 case SEEK_SET:
284 pos = head->next;
285 break;
286 case SEEK_CUR:
287 pos = self->first_visible_entry;
288 break;
289 case SEEK_END:
290 pos = head->prev;
291 break;
292 default:
293 return;
294 }
295
296 if (offset > 0) {
297 while (offset-- != 0)
298 pos = pos->next;
299 } else {
300 while (offset++ != 0)
301 pos = pos->prev;
302 }
303
304 self->first_visible_entry = pos;
305}
306
Arnaldo Carvalho de Melo8c694d22010-06-21 12:44:42 -0300307static bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
308{
309 return (self->first_visible_entry_idx + row) == self->index;
310}
311
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300312static void ui_browser__refresh_dimensions(struct ui_browser *self)
313{
314 int cols, rows;
315 newtGetScreenSize(&cols, &rows);
316
317 if (self->width > cols - 4)
318 self->width = cols - 4;
319 self->height = rows - 5;
320 if (self->height > self->nr_entries)
321 self->height = self->nr_entries;
322 self->top = (rows - self->height) / 2;
323 self->left = (cols - self->width) / 2;
324}
325
326static void ui_browser__reset_index(struct ui_browser *self)
327{
Arnaldo Carvalho de Melo8c694d22010-06-21 12:44:42 -0300328 self->index = self->first_visible_entry_idx = 0;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300329 self->seek(self, 0, SEEK_SET);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300330}
331
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300332static int ui_browser__show(struct ui_browser *self, const char *title)
333{
334 if (self->form != NULL)
335 return 0;
336 ui_browser__refresh_dimensions(self);
337 newtCenteredWindow(self->width + 2, self->height, title);
338 self->form = newt_form__new();
339 if (self->form == NULL)
340 return -1;
341
342 self->sb = newtVerticalScrollbar(self->width + 1, 0, self->height,
343 HE_COLORSET_NORMAL,
344 HE_COLORSET_SELECTED);
345 if (self->sb == NULL)
346 return -1;
347
348 newtFormAddHotKey(self->form, NEWT_KEY_UP);
349 newtFormAddHotKey(self->form, NEWT_KEY_DOWN);
350 newtFormAddHotKey(self->form, NEWT_KEY_PGUP);
351 newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
352 newtFormAddHotKey(self->form, NEWT_KEY_HOME);
353 newtFormAddHotKey(self->form, NEWT_KEY_END);
354 newtFormAddComponent(self->form, self->sb);
355 return 0;
356}
357
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300358static int objdump_line__show(struct objdump_line *self, struct list_head *head,
359 int width, struct hist_entry *he, int len,
360 bool current_entry)
361{
362 if (self->offset != -1) {
363 struct symbol *sym = he->ms.sym;
364 unsigned int hits = 0;
365 double percent = 0.0;
366 int color;
367 struct sym_priv *priv = symbol__priv(sym);
368 struct sym_ext *sym_ext = priv->ext;
369 struct sym_hist *h = priv->hist;
370 s64 offset = self->offset;
371 struct objdump_line *next = objdump__get_next_ip_line(head, self);
372
373 while (offset < (s64)len &&
374 (next == NULL || offset < next->offset)) {
375 if (sym_ext) {
376 percent += sym_ext[offset].percent;
377 } else
378 hits += h->ip[offset];
379
380 ++offset;
381 }
382
383 if (sym_ext == NULL && h->sum)
384 percent = 100.0 * hits / h->sum;
385
386 color = ui_browser__percent_color(percent, current_entry);
387 SLsmg_set_color(color);
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300388 slsmg_printf(" %7.2f ", percent);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300389 if (!current_entry)
390 SLsmg_set_color(HE_COLORSET_CODE);
391 } else {
392 int color = ui_browser__percent_color(0, current_entry);
393 SLsmg_set_color(color);
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300394 slsmg_write_nstring(" ", 9);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300395 }
396
397 SLsmg_write_char(':');
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300398 slsmg_write_nstring(" ", 8);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300399 if (!*self->line)
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300400 slsmg_write_nstring(" ", width - 18);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300401 else
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300402 slsmg_write_nstring(self->line, width - 18);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300403
404 return 0;
405}
406
407static int ui_browser__refresh_entries(struct ui_browser *self)
408{
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300409 int row;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300410
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300411 newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
412 row = self->refresh_entries(self);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300413 SLsmg_set_color(HE_COLORSET_NORMAL);
414 SLsmg_fill_region(self->top + row, self->left,
415 self->height - row, self->width, ' ');
416
417 return 0;
418}
419
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300420static int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300421{
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300422 if (ui_browser__refresh_entries(self) < 0)
423 return -1;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300424
425 while (1) {
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300426 off_t offset;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300427
428 newtFormRun(self->form, es);
429
430 if (es->reason != NEWT_EXIT_HOTKEY)
431 break;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300432 if (is_exit_key(es->u.key))
433 return es->u.key;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300434 switch (es->u.key) {
435 case NEWT_KEY_DOWN:
436 if (self->index == self->nr_entries - 1)
437 break;
438 ++self->index;
439 if (self->index == self->first_visible_entry_idx + self->height) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300440 ++self->first_visible_entry_idx;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300441 self->seek(self, +1, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300442 }
443 break;
444 case NEWT_KEY_UP:
445 if (self->index == 0)
446 break;
447 --self->index;
448 if (self->index < self->first_visible_entry_idx) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300449 --self->first_visible_entry_idx;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300450 self->seek(self, -1, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300451 }
452 break;
453 case NEWT_KEY_PGDN:
Arnaldo Carvalho de Melo17930b42010-05-19 16:03:51 -0300454 case ' ':
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300455 if (self->first_visible_entry_idx + self->height > self->nr_entries - 1)
456 break;
457
458 offset = self->height;
459 if (self->index + offset > self->nr_entries - 1)
460 offset = self->nr_entries - 1 - self->index;
461 self->index += offset;
462 self->first_visible_entry_idx += offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300463 self->seek(self, +offset, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300464 break;
465 case NEWT_KEY_PGUP:
466 if (self->first_visible_entry_idx == 0)
467 break;
468
469 if (self->first_visible_entry_idx < self->height)
470 offset = self->first_visible_entry_idx;
471 else
472 offset = self->height;
473
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_HOME:
479 ui_browser__reset_index(self);
480 break;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300481 case NEWT_KEY_END:
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300482 offset = self->height - 1;
483
484 if (offset > self->nr_entries)
485 offset = self->nr_entries;
486
487 self->index = self->first_visible_entry_idx = self->nr_entries - 1 - offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300488 self->seek(self, -offset, SEEK_END);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300489 break;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300490 case NEWT_KEY_RIGHT:
Arnaldo Carvalho de Melo60553902010-05-15 20:40:34 -0300491 case NEWT_KEY_LEFT:
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300492 case NEWT_KEY_TAB:
493 return es->u.key;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300494 default:
495 continue;
496 }
497 if (ui_browser__refresh_entries(self) < 0)
498 return -1;
499 }
500 return 0;
501}
502
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300503/*
504 * When debugging newt problems it was useful to be able to "unroll"
505 * the calls to newtCheckBoxTreeAdd{Array,Item}, so that we can generate
506 * a source file with the sequence of calls to these methods, to then
507 * tweak the arrays to get the intended results, so I'm keeping this code
508 * here, may be useful again in the future.
509 */
510#undef NEWT_DEBUG
511
512static void newt_checkbox_tree__add(newtComponent tree, const char *str,
513 void *priv, int *indexes)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300514{
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300515#ifdef NEWT_DEBUG
516 /* Print the newtCheckboxTreeAddArray to tinker with its index arrays */
517 int i = 0, len = 40 - strlen(str);
518
519 fprintf(stderr,
520 "\tnewtCheckboxTreeAddItem(tree, %*.*s\"%s\", (void *)%p, 0, ",
521 len, len, " ", str, priv);
522 while (indexes[i] != NEWT_ARG_LAST) {
523 if (indexes[i] != NEWT_ARG_APPEND)
524 fprintf(stderr, " %d,", indexes[i]);
525 else
526 fprintf(stderr, " %s,", "NEWT_ARG_APPEND");
527 ++i;
528 }
529 fprintf(stderr, " %s", " NEWT_ARG_LAST);\n");
530 fflush(stderr);
531#endif
532 newtCheckboxTreeAddArray(tree, str, priv, 0, indexes);
533}
534
535static char *callchain_list__sym_name(struct callchain_list *self,
536 char *bf, size_t bfsize)
537{
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300538 if (self->ms.sym)
539 return self->ms.sym->name;
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300540
541 snprintf(bf, bfsize, "%#Lx", self->ip);
542 return bf;
543}
544
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300545static unsigned int hist_entry__annotate_browser_refresh(struct ui_browser *self)
546{
547 struct objdump_line *pos;
548 struct list_head *head = self->entries;
549 struct hist_entry *he = self->priv;
550 int row = 0;
551 int len = he->ms.sym->end - he->ms.sym->start;
552
553 if (self->first_visible_entry == NULL || self->first_visible_entry == self->entries)
554 self->first_visible_entry = head->next;
555
556 pos = list_entry(self->first_visible_entry, struct objdump_line, node);
557
558 list_for_each_entry_from(pos, head, node) {
559 bool current_entry = ui_browser__is_current_entry(self, row);
560 SLsmg_gotorc(self->top + row, self->left);
561 objdump_line__show(pos, head, self->width,
562 he, len, current_entry);
563 if (++row == self->height)
564 break;
565 }
566
567 return row;
568}
569
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300570static void __callchain__append_graph_browser(struct callchain_node *self,
571 newtComponent tree, u64 total,
572 int *indexes, int depth)
573{
574 struct rb_node *node;
575 u64 new_total, remaining;
576 int idx = 0;
577
578 if (callchain_param.mode == CHAIN_GRAPH_REL)
579 new_total = self->children_hit;
580 else
581 new_total = total;
582
583 remaining = new_total;
584 node = rb_first(&self->rb_root);
585 while (node) {
586 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
587 struct rb_node *next = rb_next(node);
588 u64 cumul = cumul_hits(child);
589 struct callchain_list *chain;
590 int first = true, printed = 0;
591 int chain_idx = -1;
592 remaining -= cumul;
593
594 indexes[depth] = NEWT_ARG_APPEND;
595 indexes[depth + 1] = NEWT_ARG_LAST;
596
597 list_for_each_entry(chain, &child->val, list) {
598 char ipstr[BITS_PER_LONG / 4 + 1],
599 *alloc_str = NULL;
600 const char *str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
601
602 if (first) {
603 double percent = cumul * 100.0 / new_total;
604
605 first = false;
606 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
607 str = "Not enough memory!";
608 else
609 str = alloc_str;
610 } else {
611 indexes[depth] = idx;
612 indexes[depth + 1] = NEWT_ARG_APPEND;
613 indexes[depth + 2] = NEWT_ARG_LAST;
614 ++chain_idx;
615 }
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300616 newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300617 free(alloc_str);
618 ++printed;
619 }
620
621 indexes[depth] = idx;
622 if (chain_idx != -1)
623 indexes[depth + 1] = chain_idx;
624 if (printed != 0)
625 ++idx;
626 __callchain__append_graph_browser(child, tree, new_total, indexes,
627 depth + (chain_idx != -1 ? 2 : 1));
628 node = next;
629 }
630}
631
632static void callchain__append_graph_browser(struct callchain_node *self,
633 newtComponent tree, u64 total,
634 int *indexes, int parent_idx)
635{
636 struct callchain_list *chain;
637 int i = 0;
638
639 indexes[1] = NEWT_ARG_APPEND;
640 indexes[2] = NEWT_ARG_LAST;
641
642 list_for_each_entry(chain, &self->val, list) {
643 char ipstr[BITS_PER_LONG / 4 + 1], *str;
644
645 if (chain->ip >= PERF_CONTEXT_MAX)
646 continue;
647
648 if (!i++ && sort__first_dimension == SORT_SYM)
649 continue;
650
651 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300652 newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300653 }
654
655 indexes[1] = parent_idx;
656 indexes[2] = NEWT_ARG_APPEND;
657 indexes[3] = NEWT_ARG_LAST;
658 __callchain__append_graph_browser(self, tree, total, indexes, 2);
659}
660
661static void hist_entry__append_callchain_browser(struct hist_entry *self,
662 newtComponent tree, u64 total, int parent_idx)
663{
664 struct rb_node *rb_node;
665 int indexes[1024] = { [0] = parent_idx, };
666 int idx = 0;
667 struct callchain_node *chain;
668
669 rb_node = rb_first(&self->sorted_chain);
670 while (rb_node) {
671 chain = rb_entry(rb_node, struct callchain_node, rb_node);
672 switch (callchain_param.mode) {
673 case CHAIN_FLAT:
674 break;
675 case CHAIN_GRAPH_ABS: /* falldown */
676 case CHAIN_GRAPH_REL:
677 callchain__append_graph_browser(chain, tree, total, indexes, idx++);
678 break;
679 case CHAIN_NONE:
680 default:
681 break;
682 }
683 rb_node = rb_next(rb_node);
684 }
685}
686
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300687static size_t hist_entry__append_browser(struct hist_entry *self,
688 newtComponent tree, u64 total)
689{
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300690 char s[256];
691 size_t ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300692
693 if (symbol_conf.exclude_other && !self->parent)
694 return 0;
695
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300696 ret = hist_entry__snprintf(self, s, sizeof(s), NULL,
697 false, 0, false, total);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300698 if (symbol_conf.use_callchain) {
699 int indexes[2];
700
701 indexes[0] = NEWT_ARG_APPEND;
702 indexes[1] = NEWT_ARG_LAST;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300703 newt_checkbox_tree__add(tree, s, &self->ms, indexes);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300704 } else
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300705 newtListboxAppendEntry(tree, s, &self->ms);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300706
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300707 return ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300708}
709
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300710int hist_entry__tui_annotate(struct hist_entry *self)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300711{
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300712 struct ui_browser browser;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300713 struct newtExitStruct es;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300714 struct objdump_line *pos, *n;
715 LIST_HEAD(head);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300716 int ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300717
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300718 if (self->ms.sym == NULL)
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300719 return -1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300720
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300721 if (self->ms.map->dso->annotate_warned)
722 return -1;
723
724 if (hist_entry__annotate(self, &head) < 0) {
725 ui__error_window(browser__last_msg);
726 return -1;
727 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300728
Arnaldo Carvalho de Melo60553902010-05-15 20:40:34 -0300729 ui_helpline__push("Press <- or ESC to exit");
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300730
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300731 memset(&browser, 0, sizeof(browser));
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300732 browser.entries = &head;
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300733 browser.refresh_entries = hist_entry__annotate_browser_refresh;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300734 browser.seek = ui_browser__list_head_seek;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300735 browser.priv = self;
736 list_for_each_entry(pos, &head, node) {
737 size_t line_len = strlen(pos->line);
738 if (browser.width < line_len)
739 browser.width = line_len;
740 ++browser.nr_entries;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300741 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300742
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300743 browser.width += 18; /* Percentage */
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300744 ui_browser__show(&browser, self->ms.sym->name);
745 ui_browser__run(&browser, &es);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300746 newtFormDestroy(browser.form);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300747 newtPopWindow();
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300748 list_for_each_entry_safe(pos, n, &head, node) {
749 list_del(&pos->node);
750 objdump_line__free(pos);
751 }
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300752 ui_helpline__pop();
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300753 return ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300754}
755
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300756static const void *newt__symbol_tree_get_current(newtComponent self)
757{
758 if (symbol_conf.use_callchain)
759 return newtCheckboxTreeGetCurrent(self);
760 return newtListboxGetCurrent(self);
761}
762
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300763static void hist_browser__selection(newtComponent self, void *data)
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300764{
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300765 const struct map_symbol **symbol_ptr = data;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300766 *symbol_ptr = newt__symbol_tree_get_current(self);
767}
768
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300769struct hist_browser {
770 newtComponent form, tree;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300771 const struct map_symbol *selection;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300772};
773
774static struct hist_browser *hist_browser__new(void)
775{
776 struct hist_browser *self = malloc(sizeof(*self));
777
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300778 if (self != NULL)
779 self->form = NULL;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300780
781 return self;
782}
783
784static void hist_browser__delete(struct hist_browser *self)
785{
786 newtFormDestroy(self->form);
787 newtPopWindow();
788 free(self);
789}
790
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300791static int hist_browser__populate(struct hist_browser *self, struct hists *hists,
792 const char *title)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300793{
794 int max_len = 0, idx, cols, rows;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300795 struct ui_progress *progress;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300796 struct rb_node *nd;
797 u64 curr_hist = 0;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300798 char seq[] = ".", unit;
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300799 char str[256];
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300800 unsigned long nr_events = hists->stats.nr_events[PERF_RECORD_SAMPLE];
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300801
802 if (self->form) {
803 newtFormDestroy(self->form);
804 newtPopWindow();
805 }
806
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300807 nr_events = convert_unit(nr_events, &unit);
808 snprintf(str, sizeof(str), "Events: %lu%c ",
809 nr_events, unit);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300810 newtDrawRootText(0, 0, str);
811
812 newtGetScreenSize(NULL, &rows);
813
814 if (symbol_conf.use_callchain)
815 self->tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq,
816 NEWT_FLAG_SCROLL);
817 else
818 self->tree = newtListbox(0, 0, rows - 5,
819 (NEWT_FLAG_SCROLL |
820 NEWT_FLAG_RETURNEXIT));
821
822 newtComponentAddCallback(self->tree, hist_browser__selection,
823 &self->selection);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300824
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300825 progress = ui_progress__new("Adding entries to the browser...",
826 hists->nr_entries);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300827 if (progress == NULL)
828 return -1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300829
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300830 idx = 0;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300831 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300832 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300833 int len;
834
835 if (h->filtered)
836 continue;
837
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300838 len = hist_entry__append_browser(h, self->tree, hists->stats.total_period);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300839 if (len > max_len)
840 max_len = len;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300841 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300842 hist_entry__append_callchain_browser(h, self->tree,
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300843 hists->stats.total_period, idx++);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300844 ++curr_hist;
845 if (curr_hist % 5)
846 ui_progress__update(progress, curr_hist);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300847 }
848
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300849 ui_progress__delete(progress);
850
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300851 newtGetScreenSize(&cols, &rows);
852
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300853 if (max_len > cols)
854 max_len = cols - 3;
855
856 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300857 newtListboxSetWidth(self->tree, max_len);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300858
859 newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0),
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300860 rows - 5, title);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300861 self->form = newt_form__new();
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300862 if (self->form == NULL)
863 return -1;
864
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300865 newtFormAddHotKey(self->form, 'A');
866 newtFormAddHotKey(self->form, 'a');
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300867 newtFormAddHotKey(self->form, 'D');
868 newtFormAddHotKey(self->form, 'd');
869 newtFormAddHotKey(self->form, 'T');
870 newtFormAddHotKey(self->form, 't');
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300871 newtFormAddHotKey(self->form, '?');
872 newtFormAddHotKey(self->form, 'H');
873 newtFormAddHotKey(self->form, 'h');
874 newtFormAddHotKey(self->form, NEWT_KEY_F1);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300875 newtFormAddHotKey(self->form, NEWT_KEY_RIGHT);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300876 newtFormAddHotKey(self->form, NEWT_KEY_TAB);
877 newtFormAddHotKey(self->form, NEWT_KEY_UNTAB);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300878 newtFormAddComponents(self->form, self->tree, NULL);
879 self->selection = newt__symbol_tree_get_current(self->tree);
880
881 return 0;
882}
883
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300884static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300885{
886 int *indexes;
887
888 if (!symbol_conf.use_callchain)
889 goto out;
890
891 indexes = newtCheckboxTreeFindItem(self->tree, (void *)self->selection);
892 if (indexes) {
893 bool is_hist_entry = indexes[1] == NEWT_ARG_LAST;
894 free(indexes);
895 if (is_hist_entry)
896 goto out;
897 }
898 return NULL;
899out:
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300900 return container_of(self->selection, struct hist_entry, ms);
901}
902
903static struct thread *hist_browser__selected_thread(struct hist_browser *self)
904{
905 struct hist_entry *he = hist_browser__selected_entry(self);
906 return he ? he->thread : NULL;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300907}
908
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300909static int hist_browser__title(char *bf, size_t size, const char *ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300910 const struct dso *dso, const struct thread *thread)
911{
912 int printed = 0;
913
914 if (thread)
915 printed += snprintf(bf + printed, size - printed,
916 "Thread: %s(%d)",
917 (thread->comm_set ? thread->comm : ""),
918 thread->pid);
919 if (dso)
920 printed += snprintf(bf + printed, size - printed,
921 "%sDSO: %s", thread ? " " : "",
922 dso->short_name);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300923 return printed ?: snprintf(bf, size, "Event: %s", ev_name);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300924}
925
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300926int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300927{
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300928 struct hist_browser *browser = hist_browser__new();
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300929 struct pstack *fstack;
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300930 const struct thread *thread_filter = NULL;
931 const struct dso *dso_filter = NULL;
932 struct newtExitStruct es;
933 char msg[160];
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300934 int key = -1;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300935
936 if (browser == NULL)
937 return -1;
938
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300939 fstack = pstack__new(2);
940 if (fstack == NULL)
941 goto out;
942
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300943 ui_helpline__push(helpline);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300944
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300945 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300946 dso_filter, thread_filter);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300947 if (hist_browser__populate(browser, self, msg) < 0)
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300948 goto out_free_stack;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300949
950 while (1) {
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300951 const struct thread *thread;
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300952 const struct dso *dso;
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300953 char *options[16];
954 int nr_options = 0, choice = 0, i,
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300955 annotate = -2, zoom_dso = -2, zoom_thread = -2;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300956
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300957 newtFormRun(browser->form, &es);
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300958
959 thread = hist_browser__selected_thread(browser);
960 dso = browser->selection->map ? browser->selection->map->dso : NULL;
961
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300962 if (es.reason == NEWT_EXIT_HOTKEY) {
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300963 key = es.u.key;
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300964
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300965 switch (key) {
966 case NEWT_KEY_F1:
967 goto do_help;
968 case NEWT_KEY_TAB:
969 case NEWT_KEY_UNTAB:
970 /*
971 * Exit the browser, let hists__browser_tree
972 * go to the next or previous
973 */
974 goto out_free_stack;
975 default:;
976 }
977
978 key = toupper(key);
979 switch (key) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300980 case 'A':
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -0300981 if (browser->selection->map == NULL &&
982 browser->selection->map->dso->annotate_warned)
983 continue;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300984 goto do_annotate;
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300985 case 'D':
986 goto zoom_dso;
987 case 'T':
988 goto zoom_thread;
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300989 case 'H':
990 case '?':
991do_help:
992 ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
993 "<- Zoom out\n"
994 "a Annotate current symbol\n"
995 "h/?/F1 Show this window\n"
996 "d Zoom into current DSO\n"
997 "t Zoom into current Thread\n"
998 "q/CTRL+C Exit browser");
999 continue;
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001000 default:;
1001 }
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001002 if (is_exit_key(key)) {
1003 if (key == NEWT_KEY_ESCAPE) {
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001004 if (dialog_yesno("Do you really want to exit?"))
1005 break;
1006 else
1007 continue;
1008 } else
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001009 break;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001010 }
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001011
1012 if (es.u.key == NEWT_KEY_LEFT) {
1013 const void *top;
1014
1015 if (pstack__empty(fstack))
1016 continue;
1017 top = pstack__pop(fstack);
1018 if (top == &dso_filter)
1019 goto zoom_out_dso;
1020 if (top == &thread_filter)
1021 goto zoom_out_thread;
1022 continue;
1023 }
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001024 }
1025
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001026 if (browser->selection->sym != NULL &&
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -03001027 !browser->selection->map->dso->annotate_warned &&
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001028 asprintf(&options[nr_options], "Annotate %s",
1029 browser->selection->sym->name) > 0)
1030 annotate = nr_options++;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001031
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001032 if (thread != NULL &&
1033 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001034 (thread_filter ? "out of" : "into"),
1035 (thread->comm_set ? thread->comm : ""),
1036 thread->pid) > 0)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001037 zoom_thread = nr_options++;
1038
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001039 if (dso != NULL &&
1040 asprintf(&options[nr_options], "Zoom %s %s DSO",
1041 (dso_filter ? "out of" : "into"),
1042 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
1043 zoom_dso = nr_options++;
1044
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001045 options[nr_options++] = (char *)"Exit";
1046
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001047 choice = popup_menu(nr_options, options);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001048
1049 for (i = 0; i < nr_options - 1; ++i)
1050 free(options[i]);
1051
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001052 if (choice == nr_options - 1)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001053 break;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001054
1055 if (choice == -1)
1056 continue;
Arnaldo Carvalho de Meloc1ec5fe2010-05-15 20:45:31 -03001057
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001058 if (choice == annotate) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001059 struct hist_entry *he;
Arnaldo Carvalho de Meloc1ec5fe2010-05-15 20:45:31 -03001060do_annotate:
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -03001061 if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -03001062 browser->selection->map->dso->annotate_warned = 1;
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001063 ui_helpline__puts("No vmlinux file found, can't "
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -03001064 "annotate with just a "
1065 "kallsyms file");
1066 continue;
1067 }
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001068
1069 he = hist_browser__selected_entry(browser);
1070 if (he == NULL)
1071 continue;
1072
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001073 hist_entry__tui_annotate(he);
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001074 } else if (choice == zoom_dso) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001075zoom_dso:
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001076 if (dso_filter) {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001077 pstack__remove(fstack, &dso_filter);
1078zoom_out_dso:
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001079 ui_helpline__pop();
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001080 dso_filter = NULL;
1081 } else {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001082 if (dso == NULL)
1083 continue;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001084 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001085 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001086 dso_filter = dso;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001087 pstack__push(fstack, &dso_filter);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001088 }
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001089 hists__filter_by_dso(self, dso_filter);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001090 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001091 dso_filter, thread_filter);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001092 if (hist_browser__populate(browser, self, msg) < 0)
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001093 goto out;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001094 } else if (choice == zoom_thread) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001095zoom_thread:
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001096 if (thread_filter) {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001097 pstack__remove(fstack, &thread_filter);
1098zoom_out_thread:
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001099 ui_helpline__pop();
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001100 thread_filter = NULL;
1101 } else {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001102 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001103 thread->comm_set ? thread->comm : "",
1104 thread->pid);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001105 thread_filter = thread;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001106 pstack__push(fstack, &thread_filter);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001107 }
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001108 hists__filter_by_thread(self, thread_filter);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001109 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001110 dso_filter, thread_filter);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001111 if (hist_browser__populate(browser, self, msg) < 0)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001112 goto out;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -03001113 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001114 }
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001115out_free_stack:
1116 pstack__delete(fstack);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -03001117out:
1118 hist_browser__delete(browser);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001119 return key;
1120}
1121
1122int hists__tui_browse_tree(struct rb_root *self, const char *help)
1123{
1124 struct rb_node *first = rb_first(self), *nd = first, *next;
1125 int key = 0;
1126
1127 while (nd) {
1128 struct hists *hists = rb_entry(nd, struct hists, rb_node);
1129 const char *ev_name = __event_name(hists->type, hists->config);
1130
1131 key = hists__browse(hists, help, ev_name);
1132
1133 if (is_exit_key(key))
1134 break;
1135
1136 switch (key) {
1137 case NEWT_KEY_TAB:
1138 next = rb_next(nd);
1139 if (next)
1140 nd = next;
1141 break;
1142 case NEWT_KEY_UNTAB:
1143 if (nd == first)
1144 continue;
1145 nd = rb_prev(nd);
1146 default:
1147 break;
1148 }
1149 }
1150
1151 return key;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001152}
1153
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001154static struct newtPercentTreeColors {
1155 const char *topColorFg, *topColorBg;
1156 const char *mediumColorFg, *mediumColorBg;
1157 const char *normalColorFg, *normalColorBg;
1158 const char *selColorFg, *selColorBg;
1159 const char *codeColorFg, *codeColorBg;
1160} defaultPercentTreeColors = {
1161 "red", "lightgray",
1162 "green", "lightgray",
1163 "black", "lightgray",
1164 "lightgray", "magenta",
1165 "blue", "lightgray",
1166};
1167
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001168void setup_browser(void)
1169{
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001170 struct newtPercentTreeColors *c = &defaultPercentTreeColors;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001171
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001172 if (!isatty(1) || !use_browser || dump_trace) {
Arnaldo Carvalho de Melo62e34362010-05-26 13:22:26 -03001173 use_browser = 0;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -03001174 setup_pager();
1175 return;
1176 }
1177
1178 use_browser = 1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001179 newtInit();
1180 newtCls();
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001181 ui_helpline__puts(" ");
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -03001182 sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
1183 sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
1184 sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
1185 sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
1186 sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001187}
1188
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -03001189void exit_browser(bool wait_for_ok)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001190{
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -03001191 if (use_browser > 0) {
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -03001192 if (wait_for_ok) {
1193 char title[] = "Fatal Error", ok[] = "Ok";
1194 newtWinMessage(title, ok, browser__last_msg);
1195 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001196 newtFinished();
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -03001197 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001198}