blob: aed21490ccdc046f93720a0a1be323090beab187 [file] [log] [blame]
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001#define _GNU_SOURCE
2#include <stdio.h>
3#undef _GNU_SOURCE
Arnaldo Carvalho de Melo32ec6ac2010-05-18 00:23:14 -03004/*
5 * slang versions <= 2.0.6 have a "#if HAVE_LONG_LONG" that breaks
6 * the build if it isn't defined. Use the equivalent one that glibc
7 * has on features.h.
8 */
9#include <features.h>
10#ifndef HAVE_LONG_LONG
11#define HAVE_LONG_LONG __GLIBC_HAVE_LONG_LONG
12#endif
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -030013#include <slang.h>
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -030014#include <stdlib.h>
15#include <newt.h>
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -030016#include <sys/ttydefaults.h>
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -030017
18#include "cache.h"
19#include "hist.h"
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -030020#include "pstack.h"
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -030021#include "session.h"
22#include "sort.h"
23#include "symbol.h"
24
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -030025#if SLANG_VERSION < 20104
26#define slsmg_printf(msg, args...) SLsmg_printf((char *)msg, ##args)
27#define slsmg_write_nstring(msg, len) SLsmg_write_nstring((char *)msg, len)
28#define sltt_set_color(obj, name, fg, bg) SLtt_set_color(obj,(char *)name,\
29 (char *)fg, (char *)bg)
30#else
31#define slsmg_printf SLsmg_printf
32#define slsmg_write_nstring SLsmg_write_nstring
33#define sltt_set_color SLtt_set_color
34#endif
35
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030036struct ui_progress {
37 newtComponent form, scale;
38};
39
40struct ui_progress *ui_progress__new(const char *title, u64 total)
41{
42 struct ui_progress *self = malloc(sizeof(*self));
43
44 if (self != NULL) {
45 int cols;
Arnaldo Carvalho de Melo1d90f2e2010-06-09 07:13:16 -030046
47 if (use_browser <= 0)
48 return self;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030049 newtGetScreenSize(&cols, NULL);
50 cols -= 4;
51 newtCenteredWindow(cols, 1, title);
52 self->form = newtForm(NULL, NULL, 0);
53 if (self->form == NULL)
54 goto out_free_self;
55 self->scale = newtScale(0, 0, cols, total);
56 if (self->scale == NULL)
57 goto out_free_form;
Arnaldo Carvalho de Melo7f826452010-05-10 10:51:25 -030058 newtFormAddComponent(self->form, self->scale);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030059 newtRefresh();
60 }
61
62 return self;
63
64out_free_form:
65 newtFormDestroy(self->form);
66out_free_self:
67 free(self);
68 return NULL;
69}
70
71void ui_progress__update(struct ui_progress *self, u64 curr)
72{
Arnaldo Carvalho de Melo1d90f2e2010-06-09 07:13:16 -030073 /*
74 * FIXME: We should have a per UI backend way of showing progress,
75 * stdio will just show a percentage as NN%, etc.
76 */
77 if (use_browser <= 0)
78 return;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030079 newtScaleSet(self->scale, curr);
80 newtRefresh();
81}
82
83void ui_progress__delete(struct ui_progress *self)
84{
Arnaldo Carvalho de Melo1d90f2e2010-06-09 07:13:16 -030085 if (use_browser > 0) {
86 newtFormDestroy(self->form);
87 newtPopWindow();
88 }
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030089 free(self);
90}
91
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -030092static void ui_helpline__pop(void)
93{
94 newtPopHelpLine();
95}
96
97static void ui_helpline__push(const char *msg)
98{
99 newtPushHelpLine(msg);
100}
101
102static void ui_helpline__vpush(const char *fmt, va_list ap)
103{
104 char *s;
105
106 if (vasprintf(&s, fmt, ap) < 0)
107 vfprintf(stderr, fmt, ap);
108 else {
109 ui_helpline__push(s);
110 free(s);
111 }
112}
113
114static void ui_helpline__fpush(const char *fmt, ...)
115{
116 va_list ap;
117
118 va_start(ap, fmt);
119 ui_helpline__vpush(fmt, ap);
120 va_end(ap);
121}
122
123static void ui_helpline__puts(const char *msg)
124{
125 ui_helpline__pop();
126 ui_helpline__push(msg);
127}
128
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300129static char browser__last_msg[1024];
130
131int browser__show_help(const char *format, va_list ap)
132{
133 int ret;
134 static int backlog;
135
136 ret = vsnprintf(browser__last_msg + backlog,
137 sizeof(browser__last_msg) - backlog, format, ap);
138 backlog += ret;
139
140 if (browser__last_msg[backlog - 1] == '\n') {
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300141 ui_helpline__puts(browser__last_msg);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300142 newtRefresh();
143 backlog = 0;
144 }
145
146 return ret;
147}
148
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -0300149static void newt_form__set_exit_keys(newtComponent self)
150{
Arnaldo Carvalho de Meloa308f3a2010-05-16 20:29:38 -0300151 newtFormAddHotKey(self, NEWT_KEY_LEFT);
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -0300152 newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
153 newtFormAddHotKey(self, 'Q');
154 newtFormAddHotKey(self, 'q');
155 newtFormAddHotKey(self, CTRL('c'));
156}
157
158static newtComponent newt_form__new(void)
159{
160 newtComponent self = newtForm(NULL, NULL, 0);
161 if (self)
162 newt_form__set_exit_keys(self);
163 return self;
164}
165
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300166static int popup_menu(int argc, char * const argv[])
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300167{
168 struct newtExitStruct es;
169 int i, rc = -1, max_len = 5;
170 newtComponent listbox, form = newt_form__new();
171
172 if (form == NULL)
173 return -1;
174
175 listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
176 if (listbox == NULL)
177 goto out_destroy_form;
178
Arnaldo Carvalho de Melo7f826452010-05-10 10:51:25 -0300179 newtFormAddComponent(form, listbox);
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300180
181 for (i = 0; i < argc; ++i) {
182 int len = strlen(argv[i]);
183 if (len > max_len)
184 max_len = len;
185 if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
186 goto out_destroy_form;
187 }
188
189 newtCenteredWindow(max_len, argc, NULL);
190 newtFormRun(form, &es);
191 rc = newtListboxGetCurrent(listbox) - NULL;
192 if (es.reason == NEWT_EXIT_HOTKEY)
193 rc = -1;
194 newtPopWindow();
195out_destroy_form:
196 newtFormDestroy(form);
197 return rc;
198}
199
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300200static int ui__help_window(const char *text)
201{
202 struct newtExitStruct es;
203 newtComponent tb, form = newt_form__new();
204 int rc = -1;
205 int max_len = 0, nr_lines = 0;
206 const char *t;
207
208 if (form == NULL)
209 return -1;
210
211 t = text;
212 while (1) {
213 const char *sep = strchr(t, '\n');
214 int len;
215
216 if (sep == NULL)
217 sep = strchr(t, '\0');
218 len = sep - t;
219 if (max_len < len)
220 max_len = len;
221 ++nr_lines;
222 if (*sep == '\0')
223 break;
224 t = sep + 1;
225 }
226
227 tb = newtTextbox(0, 0, max_len, nr_lines, 0);
228 if (tb == NULL)
229 goto out_destroy_form;
230
231 newtTextboxSetText(tb, text);
232 newtFormAddComponent(form, tb);
233 newtCenteredWindow(max_len, nr_lines, NULL);
234 newtFormRun(form, &es);
235 newtPopWindow();
236 rc = 0;
237out_destroy_form:
238 newtFormDestroy(form);
239 return rc;
240}
241
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300242static bool dialog_yesno(const char *msg)
243{
244 /* newtWinChoice should really be accepting const char pointers... */
245 char yes[] = "Yes", no[] = "No";
Arnaldo Carvalho de Meloc0ed55d2010-04-05 12:04:23 -0300246 return newtWinChoice(NULL, yes, no, (char *)msg) == 1;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300247}
248
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300249static void ui__error_window(const char *fmt, ...)
250{
251 va_list ap;
252
253 va_start(ap, fmt);
254 newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
255 va_end(ap);
256}
257
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300258#define HE_COLORSET_TOP 50
259#define HE_COLORSET_MEDIUM 51
260#define HE_COLORSET_NORMAL 52
261#define HE_COLORSET_SELECTED 53
262#define HE_COLORSET_CODE 54
263
264static int ui_browser__percent_color(double percent, bool current)
265{
266 if (current)
267 return HE_COLORSET_SELECTED;
268 if (percent >= MIN_RED)
269 return HE_COLORSET_TOP;
270 if (percent >= MIN_GREEN)
271 return HE_COLORSET_MEDIUM;
272 return HE_COLORSET_NORMAL;
273}
274
275struct ui_browser {
276 newtComponent form, sb;
277 u64 index, first_visible_entry_idx;
278 void *first_visible_entry, *entries;
279 u16 top, left, width, height;
280 void *priv;
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300281 unsigned int (*refresh_entries)(struct ui_browser *self);
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300282 void (*seek)(struct ui_browser *self,
283 off_t offset, int whence);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300284 u32 nr_entries;
285};
286
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300287static void ui_browser__list_head_seek(struct ui_browser *self,
288 off_t offset, int whence)
289{
290 struct list_head *head = self->entries;
291 struct list_head *pos;
292
293 switch (whence) {
294 case SEEK_SET:
295 pos = head->next;
296 break;
297 case SEEK_CUR:
298 pos = self->first_visible_entry;
299 break;
300 case SEEK_END:
301 pos = head->prev;
302 break;
303 default:
304 return;
305 }
306
307 if (offset > 0) {
308 while (offset-- != 0)
309 pos = pos->next;
310 } else {
311 while (offset++ != 0)
312 pos = pos->prev;
313 }
314
315 self->first_visible_entry = pos;
316}
317
Arnaldo Carvalho de Melo8c694d22010-06-21 12:44:42 -0300318static bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
319{
320 return (self->first_visible_entry_idx + row) == self->index;
321}
322
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300323static void ui_browser__refresh_dimensions(struct ui_browser *self)
324{
325 int cols, rows;
326 newtGetScreenSize(&cols, &rows);
327
328 if (self->width > cols - 4)
329 self->width = cols - 4;
330 self->height = rows - 5;
331 if (self->height > self->nr_entries)
332 self->height = self->nr_entries;
333 self->top = (rows - self->height) / 2;
334 self->left = (cols - self->width) / 2;
335}
336
337static void ui_browser__reset_index(struct ui_browser *self)
338{
Arnaldo Carvalho de Melo8c694d22010-06-21 12:44:42 -0300339 self->index = self->first_visible_entry_idx = 0;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300340 self->seek(self, 0, SEEK_SET);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300341}
342
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300343static int ui_browser__show(struct ui_browser *self, const char *title)
344{
Arnaldo Carvalho de Melo63160f72010-07-26 13:47:15 -0300345 if (self->form != NULL) {
346 newtFormDestroy(self->form);
347 newtPopWindow();
348 }
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300349 ui_browser__refresh_dimensions(self);
350 newtCenteredWindow(self->width + 2, self->height, title);
351 self->form = newt_form__new();
352 if (self->form == NULL)
353 return -1;
354
355 self->sb = newtVerticalScrollbar(self->width + 1, 0, self->height,
356 HE_COLORSET_NORMAL,
357 HE_COLORSET_SELECTED);
358 if (self->sb == NULL)
359 return -1;
360
361 newtFormAddHotKey(self->form, NEWT_KEY_UP);
362 newtFormAddHotKey(self->form, NEWT_KEY_DOWN);
363 newtFormAddHotKey(self->form, NEWT_KEY_PGUP);
364 newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
365 newtFormAddHotKey(self->form, NEWT_KEY_HOME);
366 newtFormAddHotKey(self->form, NEWT_KEY_END);
367 newtFormAddComponent(self->form, self->sb);
368 return 0;
369}
370
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300371static int objdump_line__show(struct objdump_line *self, struct list_head *head,
372 int width, struct hist_entry *he, int len,
373 bool current_entry)
374{
375 if (self->offset != -1) {
376 struct symbol *sym = he->ms.sym;
377 unsigned int hits = 0;
378 double percent = 0.0;
379 int color;
380 struct sym_priv *priv = symbol__priv(sym);
381 struct sym_ext *sym_ext = priv->ext;
382 struct sym_hist *h = priv->hist;
383 s64 offset = self->offset;
384 struct objdump_line *next = objdump__get_next_ip_line(head, self);
385
386 while (offset < (s64)len &&
387 (next == NULL || offset < next->offset)) {
388 if (sym_ext) {
389 percent += sym_ext[offset].percent;
390 } else
391 hits += h->ip[offset];
392
393 ++offset;
394 }
395
396 if (sym_ext == NULL && h->sum)
397 percent = 100.0 * hits / h->sum;
398
399 color = ui_browser__percent_color(percent, current_entry);
400 SLsmg_set_color(color);
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300401 slsmg_printf(" %7.2f ", percent);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300402 if (!current_entry)
403 SLsmg_set_color(HE_COLORSET_CODE);
404 } else {
405 int color = ui_browser__percent_color(0, current_entry);
406 SLsmg_set_color(color);
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300407 slsmg_write_nstring(" ", 9);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300408 }
409
410 SLsmg_write_char(':');
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300411 slsmg_write_nstring(" ", 8);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300412 if (!*self->line)
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300413 slsmg_write_nstring(" ", width - 18);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300414 else
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300415 slsmg_write_nstring(self->line, width - 18);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300416
417 return 0;
418}
419
420static int ui_browser__refresh_entries(struct ui_browser *self)
421{
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300422 int row;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300423
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300424 newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
425 row = self->refresh_entries(self);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300426 SLsmg_set_color(HE_COLORSET_NORMAL);
427 SLsmg_fill_region(self->top + row, self->left,
428 self->height - row, self->width, ' ');
429
430 return 0;
431}
432
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300433static int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300434{
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300435 if (ui_browser__refresh_entries(self) < 0)
436 return -1;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300437
438 while (1) {
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300439 off_t offset;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300440
441 newtFormRun(self->form, es);
442
443 if (es->reason != NEWT_EXIT_HOTKEY)
444 break;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300445 if (is_exit_key(es->u.key))
446 return es->u.key;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300447 switch (es->u.key) {
448 case NEWT_KEY_DOWN:
449 if (self->index == self->nr_entries - 1)
450 break;
451 ++self->index;
452 if (self->index == self->first_visible_entry_idx + self->height) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300453 ++self->first_visible_entry_idx;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300454 self->seek(self, +1, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300455 }
456 break;
457 case NEWT_KEY_UP:
458 if (self->index == 0)
459 break;
460 --self->index;
461 if (self->index < self->first_visible_entry_idx) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300462 --self->first_visible_entry_idx;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300463 self->seek(self, -1, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300464 }
465 break;
466 case NEWT_KEY_PGDN:
Arnaldo Carvalho de Melo17930b42010-05-19 16:03:51 -0300467 case ' ':
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300468 if (self->first_visible_entry_idx + self->height > self->nr_entries - 1)
469 break;
470
471 offset = self->height;
472 if (self->index + offset > self->nr_entries - 1)
473 offset = self->nr_entries - 1 - self->index;
474 self->index += offset;
475 self->first_visible_entry_idx += offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300476 self->seek(self, +offset, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300477 break;
478 case NEWT_KEY_PGUP:
479 if (self->first_visible_entry_idx == 0)
480 break;
481
482 if (self->first_visible_entry_idx < self->height)
483 offset = self->first_visible_entry_idx;
484 else
485 offset = self->height;
486
487 self->index -= offset;
488 self->first_visible_entry_idx -= offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300489 self->seek(self, -offset, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300490 break;
491 case NEWT_KEY_HOME:
492 ui_browser__reset_index(self);
493 break;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300494 case NEWT_KEY_END:
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300495 offset = self->height - 1;
Arnaldo Carvalho de Melo63f20e72010-07-15 07:21:07 -0300496 if (offset >= self->nr_entries)
497 offset = self->nr_entries - 1;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300498
Arnaldo Carvalho de Melo63f20e72010-07-15 07:21:07 -0300499 self->index = self->nr_entries - 1;
500 self->first_visible_entry_idx = self->index - offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300501 self->seek(self, -offset, SEEK_END);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300502 break;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300503 default:
Arnaldo Carvalho de Melob66ecd92010-07-15 07:24:30 -0300504 return es->u.key;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300505 }
506 if (ui_browser__refresh_entries(self) < 0)
507 return -1;
508 }
509 return 0;
510}
511
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300512/*
513 * When debugging newt problems it was useful to be able to "unroll"
514 * the calls to newtCheckBoxTreeAdd{Array,Item}, so that we can generate
515 * a source file with the sequence of calls to these methods, to then
516 * tweak the arrays to get the intended results, so I'm keeping this code
517 * here, may be useful again in the future.
518 */
519#undef NEWT_DEBUG
520
521static void newt_checkbox_tree__add(newtComponent tree, const char *str,
522 void *priv, int *indexes)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300523{
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300524#ifdef NEWT_DEBUG
525 /* Print the newtCheckboxTreeAddArray to tinker with its index arrays */
526 int i = 0, len = 40 - strlen(str);
527
528 fprintf(stderr,
529 "\tnewtCheckboxTreeAddItem(tree, %*.*s\"%s\", (void *)%p, 0, ",
530 len, len, " ", str, priv);
531 while (indexes[i] != NEWT_ARG_LAST) {
532 if (indexes[i] != NEWT_ARG_APPEND)
533 fprintf(stderr, " %d,", indexes[i]);
534 else
535 fprintf(stderr, " %s,", "NEWT_ARG_APPEND");
536 ++i;
537 }
538 fprintf(stderr, " %s", " NEWT_ARG_LAST);\n");
539 fflush(stderr);
540#endif
541 newtCheckboxTreeAddArray(tree, str, priv, 0, indexes);
542}
543
544static char *callchain_list__sym_name(struct callchain_list *self,
545 char *bf, size_t bfsize)
546{
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300547 if (self->ms.sym)
548 return self->ms.sym->name;
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300549
550 snprintf(bf, bfsize, "%#Lx", self->ip);
551 return bf;
552}
553
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300554static unsigned int hist_entry__annotate_browser_refresh(struct ui_browser *self)
555{
556 struct objdump_line *pos;
557 struct list_head *head = self->entries;
558 struct hist_entry *he = self->priv;
559 int row = 0;
560 int len = he->ms.sym->end - he->ms.sym->start;
561
562 if (self->first_visible_entry == NULL || self->first_visible_entry == self->entries)
563 self->first_visible_entry = head->next;
564
565 pos = list_entry(self->first_visible_entry, struct objdump_line, node);
566
567 list_for_each_entry_from(pos, head, node) {
568 bool current_entry = ui_browser__is_current_entry(self, row);
569 SLsmg_gotorc(self->top + row, self->left);
570 objdump_line__show(pos, head, self->width,
571 he, len, current_entry);
572 if (++row == self->height)
573 break;
574 }
575
576 return row;
577}
578
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300579static void __callchain__append_graph_browser(struct callchain_node *self,
580 newtComponent tree, u64 total,
581 int *indexes, int depth)
582{
583 struct rb_node *node;
584 u64 new_total, remaining;
585 int idx = 0;
586
587 if (callchain_param.mode == CHAIN_GRAPH_REL)
588 new_total = self->children_hit;
589 else
590 new_total = total;
591
592 remaining = new_total;
593 node = rb_first(&self->rb_root);
594 while (node) {
595 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
596 struct rb_node *next = rb_next(node);
597 u64 cumul = cumul_hits(child);
598 struct callchain_list *chain;
599 int first = true, printed = 0;
600 int chain_idx = -1;
601 remaining -= cumul;
602
603 indexes[depth] = NEWT_ARG_APPEND;
604 indexes[depth + 1] = NEWT_ARG_LAST;
605
606 list_for_each_entry(chain, &child->val, list) {
607 char ipstr[BITS_PER_LONG / 4 + 1],
608 *alloc_str = NULL;
609 const char *str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
610
611 if (first) {
612 double percent = cumul * 100.0 / new_total;
613
614 first = false;
615 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
616 str = "Not enough memory!";
617 else
618 str = alloc_str;
619 } else {
620 indexes[depth] = idx;
621 indexes[depth + 1] = NEWT_ARG_APPEND;
622 indexes[depth + 2] = NEWT_ARG_LAST;
623 ++chain_idx;
624 }
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300625 newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300626 free(alloc_str);
627 ++printed;
628 }
629
630 indexes[depth] = idx;
631 if (chain_idx != -1)
632 indexes[depth + 1] = chain_idx;
633 if (printed != 0)
634 ++idx;
635 __callchain__append_graph_browser(child, tree, new_total, indexes,
636 depth + (chain_idx != -1 ? 2 : 1));
637 node = next;
638 }
639}
640
641static void callchain__append_graph_browser(struct callchain_node *self,
642 newtComponent tree, u64 total,
643 int *indexes, int parent_idx)
644{
645 struct callchain_list *chain;
646 int i = 0;
647
648 indexes[1] = NEWT_ARG_APPEND;
649 indexes[2] = NEWT_ARG_LAST;
650
651 list_for_each_entry(chain, &self->val, list) {
652 char ipstr[BITS_PER_LONG / 4 + 1], *str;
653
654 if (chain->ip >= PERF_CONTEXT_MAX)
655 continue;
656
657 if (!i++ && sort__first_dimension == SORT_SYM)
658 continue;
659
660 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300661 newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300662 }
663
664 indexes[1] = parent_idx;
665 indexes[2] = NEWT_ARG_APPEND;
666 indexes[3] = NEWT_ARG_LAST;
667 __callchain__append_graph_browser(self, tree, total, indexes, 2);
668}
669
670static void hist_entry__append_callchain_browser(struct hist_entry *self,
671 newtComponent tree, u64 total, int parent_idx)
672{
673 struct rb_node *rb_node;
674 int indexes[1024] = { [0] = parent_idx, };
675 int idx = 0;
676 struct callchain_node *chain;
677
678 rb_node = rb_first(&self->sorted_chain);
679 while (rb_node) {
680 chain = rb_entry(rb_node, struct callchain_node, rb_node);
681 switch (callchain_param.mode) {
682 case CHAIN_FLAT:
683 break;
684 case CHAIN_GRAPH_ABS: /* falldown */
685 case CHAIN_GRAPH_REL:
686 callchain__append_graph_browser(chain, tree, total, indexes, idx++);
687 break;
688 case CHAIN_NONE:
689 default:
690 break;
691 }
692 rb_node = rb_next(rb_node);
693 }
694}
695
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300696static size_t hist_entry__append_browser(struct hist_entry *self,
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300697 newtComponent tree,
698 struct hists *hists)
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300699{
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300700 char s[256];
701 size_t ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300702
703 if (symbol_conf.exclude_other && !self->parent)
704 return 0;
705
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300706 ret = hist_entry__snprintf(self, s, sizeof(s), hists, NULL,
707 false, 0, false, hists->stats.total_period);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300708 if (symbol_conf.use_callchain) {
709 int indexes[2];
710
711 indexes[0] = NEWT_ARG_APPEND;
712 indexes[1] = NEWT_ARG_LAST;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300713 newt_checkbox_tree__add(tree, s, &self->ms, indexes);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300714 } else
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300715 newtListboxAppendEntry(tree, s, &self->ms);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300716
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300717 return ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300718}
719
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300720int hist_entry__tui_annotate(struct hist_entry *self)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300721{
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300722 struct ui_browser browser;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300723 struct newtExitStruct es;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300724 struct objdump_line *pos, *n;
725 LIST_HEAD(head);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300726 int ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300727
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300728 if (self->ms.sym == NULL)
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300729 return -1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300730
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300731 if (self->ms.map->dso->annotate_warned)
732 return -1;
733
734 if (hist_entry__annotate(self, &head) < 0) {
735 ui__error_window(browser__last_msg);
736 return -1;
737 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300738
Arnaldo Carvalho de Melo60553902010-05-15 20:40:34 -0300739 ui_helpline__push("Press <- or ESC to exit");
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300740
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300741 memset(&browser, 0, sizeof(browser));
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300742 browser.entries = &head;
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300743 browser.refresh_entries = hist_entry__annotate_browser_refresh;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300744 browser.seek = ui_browser__list_head_seek;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300745 browser.priv = self;
746 list_for_each_entry(pos, &head, node) {
747 size_t line_len = strlen(pos->line);
748 if (browser.width < line_len)
749 browser.width = line_len;
750 ++browser.nr_entries;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300751 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300752
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300753 browser.width += 18; /* Percentage */
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300754 ui_browser__show(&browser, self->ms.sym->name);
Arnaldo Carvalho de Melob61b55e2010-07-21 17:55:32 -0300755 newtFormAddHotKey(browser.form, ' ');
Srikar Dronamraju0879b102010-06-29 23:02:26 +0530756 ret = ui_browser__run(&browser, &es);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300757 newtFormDestroy(browser.form);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300758 newtPopWindow();
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300759 list_for_each_entry_safe(pos, n, &head, node) {
760 list_del(&pos->node);
761 objdump_line__free(pos);
762 }
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300763 ui_helpline__pop();
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300764 return ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300765}
766
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300767static const void *newt__symbol_tree_get_current(newtComponent self)
768{
769 if (symbol_conf.use_callchain)
770 return newtCheckboxTreeGetCurrent(self);
771 return newtListboxGetCurrent(self);
772}
773
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300774static void hist_browser__selection(newtComponent self, void *data)
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300775{
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300776 const struct map_symbol **symbol_ptr = data;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300777 *symbol_ptr = newt__symbol_tree_get_current(self);
778}
779
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300780struct hist_browser {
781 newtComponent form, tree;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300782 const struct map_symbol *selection;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300783};
784
785static struct hist_browser *hist_browser__new(void)
786{
787 struct hist_browser *self = malloc(sizeof(*self));
788
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300789 if (self != NULL)
790 self->form = NULL;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300791
792 return self;
793}
794
795static void hist_browser__delete(struct hist_browser *self)
796{
797 newtFormDestroy(self->form);
798 newtPopWindow();
799 free(self);
800}
801
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300802static int hist_browser__populate(struct hist_browser *self, struct hists *hists,
803 const char *title)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300804{
805 int max_len = 0, idx, cols, rows;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300806 struct ui_progress *progress;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300807 struct rb_node *nd;
808 u64 curr_hist = 0;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300809 char seq[] = ".", unit;
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300810 char str[256];
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300811 unsigned long nr_events = hists->stats.nr_events[PERF_RECORD_SAMPLE];
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300812
813 if (self->form) {
814 newtFormDestroy(self->form);
815 newtPopWindow();
816 }
817
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300818 nr_events = convert_unit(nr_events, &unit);
819 snprintf(str, sizeof(str), "Events: %lu%c ",
820 nr_events, unit);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300821 newtDrawRootText(0, 0, str);
822
823 newtGetScreenSize(NULL, &rows);
824
825 if (symbol_conf.use_callchain)
826 self->tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq,
827 NEWT_FLAG_SCROLL);
828 else
829 self->tree = newtListbox(0, 0, rows - 5,
830 (NEWT_FLAG_SCROLL |
831 NEWT_FLAG_RETURNEXIT));
832
833 newtComponentAddCallback(self->tree, hist_browser__selection,
834 &self->selection);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300835
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300836 progress = ui_progress__new("Adding entries to the browser...",
837 hists->nr_entries);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300838 if (progress == NULL)
839 return -1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300840
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300841 idx = 0;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300842 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300843 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300844 int len;
845
846 if (h->filtered)
847 continue;
848
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300849 len = hist_entry__append_browser(h, self->tree, hists);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300850 if (len > max_len)
851 max_len = len;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300852 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300853 hist_entry__append_callchain_browser(h, self->tree,
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300854 hists->stats.total_period, idx++);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300855 ++curr_hist;
856 if (curr_hist % 5)
857 ui_progress__update(progress, curr_hist);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300858 }
859
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300860 ui_progress__delete(progress);
861
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300862 newtGetScreenSize(&cols, &rows);
863
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300864 if (max_len > cols)
865 max_len = cols - 3;
866
867 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300868 newtListboxSetWidth(self->tree, max_len);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300869
870 newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0),
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300871 rows - 5, title);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300872 self->form = newt_form__new();
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300873 if (self->form == NULL)
874 return -1;
875
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300876 newtFormAddHotKey(self->form, 'A');
877 newtFormAddHotKey(self->form, 'a');
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300878 newtFormAddHotKey(self->form, 'D');
879 newtFormAddHotKey(self->form, 'd');
880 newtFormAddHotKey(self->form, 'T');
881 newtFormAddHotKey(self->form, 't');
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300882 newtFormAddHotKey(self->form, '?');
883 newtFormAddHotKey(self->form, 'H');
884 newtFormAddHotKey(self->form, 'h');
885 newtFormAddHotKey(self->form, NEWT_KEY_F1);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300886 newtFormAddHotKey(self->form, NEWT_KEY_RIGHT);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300887 newtFormAddHotKey(self->form, NEWT_KEY_TAB);
888 newtFormAddHotKey(self->form, NEWT_KEY_UNTAB);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300889 newtFormAddComponents(self->form, self->tree, NULL);
890 self->selection = newt__symbol_tree_get_current(self->tree);
891
892 return 0;
893}
894
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300895static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300896{
897 int *indexes;
898
899 if (!symbol_conf.use_callchain)
900 goto out;
901
902 indexes = newtCheckboxTreeFindItem(self->tree, (void *)self->selection);
903 if (indexes) {
904 bool is_hist_entry = indexes[1] == NEWT_ARG_LAST;
905 free(indexes);
906 if (is_hist_entry)
907 goto out;
908 }
909 return NULL;
910out:
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300911 return container_of(self->selection, struct hist_entry, ms);
912}
913
914static struct thread *hist_browser__selected_thread(struct hist_browser *self)
915{
916 struct hist_entry *he = hist_browser__selected_entry(self);
917 return he ? he->thread : NULL;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300918}
919
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300920static int hist_browser__title(char *bf, size_t size, const char *ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300921 const struct dso *dso, const struct thread *thread)
922{
923 int printed = 0;
924
925 if (thread)
926 printed += snprintf(bf + printed, size - printed,
927 "Thread: %s(%d)",
928 (thread->comm_set ? thread->comm : ""),
929 thread->pid);
930 if (dso)
931 printed += snprintf(bf + printed, size - printed,
932 "%sDSO: %s", thread ? " " : "",
933 dso->short_name);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300934 return printed ?: snprintf(bf, size, "Event: %s", ev_name);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300935}
936
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300937int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300938{
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300939 struct hist_browser *browser = hist_browser__new();
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300940 struct pstack *fstack;
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300941 const struct thread *thread_filter = NULL;
942 const struct dso *dso_filter = NULL;
943 struct newtExitStruct es;
944 char msg[160];
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300945 int key = -1;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300946
947 if (browser == NULL)
948 return -1;
949
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300950 fstack = pstack__new(2);
951 if (fstack == NULL)
952 goto out;
953
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300954 ui_helpline__push(helpline);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300955
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300956 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300957 dso_filter, thread_filter);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300958 if (hist_browser__populate(browser, self, msg) < 0)
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300959 goto out_free_stack;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300960
961 while (1) {
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300962 const struct thread *thread;
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300963 const struct dso *dso;
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300964 char *options[16];
965 int nr_options = 0, choice = 0, i,
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300966 annotate = -2, zoom_dso = -2, zoom_thread = -2;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300967
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300968 newtFormRun(browser->form, &es);
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300969
970 thread = hist_browser__selected_thread(browser);
971 dso = browser->selection->map ? browser->selection->map->dso : NULL;
972
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300973 if (es.reason == NEWT_EXIT_HOTKEY) {
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300974 key = es.u.key;
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300975
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300976 switch (key) {
977 case NEWT_KEY_F1:
978 goto do_help;
979 case NEWT_KEY_TAB:
980 case NEWT_KEY_UNTAB:
981 /*
982 * Exit the browser, let hists__browser_tree
983 * go to the next or previous
984 */
985 goto out_free_stack;
986 default:;
987 }
988
989 key = toupper(key);
990 switch (key) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300991 case 'A':
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -0300992 if (browser->selection->map == NULL &&
993 browser->selection->map->dso->annotate_warned)
994 continue;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300995 goto do_annotate;
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300996 case 'D':
997 goto zoom_dso;
998 case 'T':
999 goto zoom_thread;
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -03001000 case 'H':
1001 case '?':
1002do_help:
1003 ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
1004 "<- Zoom out\n"
1005 "a Annotate current symbol\n"
1006 "h/?/F1 Show this window\n"
1007 "d Zoom into current DSO\n"
1008 "t Zoom into current Thread\n"
1009 "q/CTRL+C Exit browser");
1010 continue;
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001011 default:;
1012 }
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001013 if (is_exit_key(key)) {
1014 if (key == NEWT_KEY_ESCAPE) {
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001015 if (dialog_yesno("Do you really want to exit?"))
1016 break;
1017 else
1018 continue;
1019 } else
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001020 break;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001021 }
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001022
1023 if (es.u.key == NEWT_KEY_LEFT) {
1024 const void *top;
1025
1026 if (pstack__empty(fstack))
1027 continue;
1028 top = pstack__pop(fstack);
1029 if (top == &dso_filter)
1030 goto zoom_out_dso;
1031 if (top == &thread_filter)
1032 goto zoom_out_thread;
1033 continue;
1034 }
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001035 }
1036
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001037 if (browser->selection->sym != NULL &&
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -03001038 !browser->selection->map->dso->annotate_warned &&
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001039 asprintf(&options[nr_options], "Annotate %s",
1040 browser->selection->sym->name) > 0)
1041 annotate = nr_options++;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001042
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001043 if (thread != NULL &&
1044 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001045 (thread_filter ? "out of" : "into"),
1046 (thread->comm_set ? thread->comm : ""),
1047 thread->pid) > 0)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001048 zoom_thread = nr_options++;
1049
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001050 if (dso != NULL &&
1051 asprintf(&options[nr_options], "Zoom %s %s DSO",
1052 (dso_filter ? "out of" : "into"),
1053 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
1054 zoom_dso = nr_options++;
1055
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001056 options[nr_options++] = (char *)"Exit";
1057
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001058 choice = popup_menu(nr_options, options);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001059
1060 for (i = 0; i < nr_options - 1; ++i)
1061 free(options[i]);
1062
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001063 if (choice == nr_options - 1)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001064 break;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001065
1066 if (choice == -1)
1067 continue;
Arnaldo Carvalho de Meloc1ec5fe2010-05-15 20:45:31 -03001068
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001069 if (choice == annotate) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001070 struct hist_entry *he;
Arnaldo Carvalho de Meloc1ec5fe2010-05-15 20:45:31 -03001071do_annotate:
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -03001072 if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -03001073 browser->selection->map->dso->annotate_warned = 1;
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001074 ui_helpline__puts("No vmlinux file found, can't "
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -03001075 "annotate with just a "
1076 "kallsyms file");
1077 continue;
1078 }
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001079
1080 he = hist_browser__selected_entry(browser);
1081 if (he == NULL)
1082 continue;
1083
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001084 hist_entry__tui_annotate(he);
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001085 } else if (choice == zoom_dso) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001086zoom_dso:
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001087 if (dso_filter) {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001088 pstack__remove(fstack, &dso_filter);
1089zoom_out_dso:
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001090 ui_helpline__pop();
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001091 dso_filter = NULL;
1092 } else {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001093 if (dso == NULL)
1094 continue;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001095 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001096 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001097 dso_filter = dso;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001098 pstack__push(fstack, &dso_filter);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001099 }
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001100 hists__filter_by_dso(self, dso_filter);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001101 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001102 dso_filter, thread_filter);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001103 if (hist_browser__populate(browser, self, msg) < 0)
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001104 goto out;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001105 } else if (choice == zoom_thread) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001106zoom_thread:
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001107 if (thread_filter) {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001108 pstack__remove(fstack, &thread_filter);
1109zoom_out_thread:
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001110 ui_helpline__pop();
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001111 thread_filter = NULL;
1112 } else {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001113 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001114 thread->comm_set ? thread->comm : "",
1115 thread->pid);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001116 thread_filter = thread;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001117 pstack__push(fstack, &thread_filter);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001118 }
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001119 hists__filter_by_thread(self, thread_filter);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001120 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001121 dso_filter, thread_filter);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001122 if (hist_browser__populate(browser, self, msg) < 0)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001123 goto out;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -03001124 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001125 }
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001126out_free_stack:
1127 pstack__delete(fstack);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -03001128out:
1129 hist_browser__delete(browser);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001130 return key;
1131}
1132
1133int hists__tui_browse_tree(struct rb_root *self, const char *help)
1134{
1135 struct rb_node *first = rb_first(self), *nd = first, *next;
1136 int key = 0;
1137
1138 while (nd) {
1139 struct hists *hists = rb_entry(nd, struct hists, rb_node);
1140 const char *ev_name = __event_name(hists->type, hists->config);
1141
1142 key = hists__browse(hists, help, ev_name);
1143
1144 if (is_exit_key(key))
1145 break;
1146
1147 switch (key) {
1148 case NEWT_KEY_TAB:
1149 next = rb_next(nd);
1150 if (next)
1151 nd = next;
1152 break;
1153 case NEWT_KEY_UNTAB:
1154 if (nd == first)
1155 continue;
1156 nd = rb_prev(nd);
1157 default:
1158 break;
1159 }
1160 }
1161
1162 return key;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001163}
1164
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001165static struct newtPercentTreeColors {
1166 const char *topColorFg, *topColorBg;
1167 const char *mediumColorFg, *mediumColorBg;
1168 const char *normalColorFg, *normalColorBg;
1169 const char *selColorFg, *selColorBg;
1170 const char *codeColorFg, *codeColorBg;
1171} defaultPercentTreeColors = {
1172 "red", "lightgray",
1173 "green", "lightgray",
1174 "black", "lightgray",
1175 "lightgray", "magenta",
1176 "blue", "lightgray",
1177};
1178
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001179void setup_browser(void)
1180{
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001181 struct newtPercentTreeColors *c = &defaultPercentTreeColors;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001182
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001183 if (!isatty(1) || !use_browser || dump_trace) {
Arnaldo Carvalho de Melo62e34362010-05-26 13:22:26 -03001184 use_browser = 0;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -03001185 setup_pager();
1186 return;
1187 }
1188
1189 use_browser = 1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001190 newtInit();
1191 newtCls();
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001192 ui_helpline__puts(" ");
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -03001193 sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
1194 sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
1195 sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
1196 sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
1197 sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001198}
1199
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -03001200void exit_browser(bool wait_for_ok)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001201{
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -03001202 if (use_browser > 0) {
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -03001203 if (wait_for_ok) {
1204 char title[] = "Fatal Error", ok[] = "Ok";
1205 newtWinMessage(title, ok, browser__last_msg);
1206 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001207 newtFinished();
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -03001208 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001209}