blob: 7979003adeafb73599c4f4f069cffaf75daba369 [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{
345 if (self->form != NULL)
346 return 0;
347 ui_browser__refresh_dimensions(self);
348 newtCenteredWindow(self->width + 2, self->height, title);
349 self->form = newt_form__new();
350 if (self->form == NULL)
351 return -1;
352
353 self->sb = newtVerticalScrollbar(self->width + 1, 0, self->height,
354 HE_COLORSET_NORMAL,
355 HE_COLORSET_SELECTED);
356 if (self->sb == NULL)
357 return -1;
358
359 newtFormAddHotKey(self->form, NEWT_KEY_UP);
360 newtFormAddHotKey(self->form, NEWT_KEY_DOWN);
361 newtFormAddHotKey(self->form, NEWT_KEY_PGUP);
362 newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
363 newtFormAddHotKey(self->form, NEWT_KEY_HOME);
364 newtFormAddHotKey(self->form, NEWT_KEY_END);
365 newtFormAddComponent(self->form, self->sb);
366 return 0;
367}
368
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300369static int objdump_line__show(struct objdump_line *self, struct list_head *head,
370 int width, struct hist_entry *he, int len,
371 bool current_entry)
372{
373 if (self->offset != -1) {
374 struct symbol *sym = he->ms.sym;
375 unsigned int hits = 0;
376 double percent = 0.0;
377 int color;
378 struct sym_priv *priv = symbol__priv(sym);
379 struct sym_ext *sym_ext = priv->ext;
380 struct sym_hist *h = priv->hist;
381 s64 offset = self->offset;
382 struct objdump_line *next = objdump__get_next_ip_line(head, self);
383
384 while (offset < (s64)len &&
385 (next == NULL || offset < next->offset)) {
386 if (sym_ext) {
387 percent += sym_ext[offset].percent;
388 } else
389 hits += h->ip[offset];
390
391 ++offset;
392 }
393
394 if (sym_ext == NULL && h->sum)
395 percent = 100.0 * hits / h->sum;
396
397 color = ui_browser__percent_color(percent, current_entry);
398 SLsmg_set_color(color);
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300399 slsmg_printf(" %7.2f ", percent);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300400 if (!current_entry)
401 SLsmg_set_color(HE_COLORSET_CODE);
402 } else {
403 int color = ui_browser__percent_color(0, current_entry);
404 SLsmg_set_color(color);
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300405 slsmg_write_nstring(" ", 9);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300406 }
407
408 SLsmg_write_char(':');
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300409 slsmg_write_nstring(" ", 8);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300410 if (!*self->line)
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300411 slsmg_write_nstring(" ", width - 18);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300412 else
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -0300413 slsmg_write_nstring(self->line, width - 18);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300414
415 return 0;
416}
417
418static int ui_browser__refresh_entries(struct ui_browser *self)
419{
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300420 int row;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300421
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300422 newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
423 row = self->refresh_entries(self);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300424 SLsmg_set_color(HE_COLORSET_NORMAL);
425 SLsmg_fill_region(self->top + row, self->left,
426 self->height - row, self->width, ' ');
427
428 return 0;
429}
430
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300431static int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300432{
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300433 if (ui_browser__refresh_entries(self) < 0)
434 return -1;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300435
436 while (1) {
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300437 off_t offset;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300438
439 newtFormRun(self->form, es);
440
441 if (es->reason != NEWT_EXIT_HOTKEY)
442 break;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300443 if (is_exit_key(es->u.key))
444 return es->u.key;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300445 switch (es->u.key) {
446 case NEWT_KEY_DOWN:
447 if (self->index == self->nr_entries - 1)
448 break;
449 ++self->index;
450 if (self->index == self->first_visible_entry_idx + self->height) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300451 ++self->first_visible_entry_idx;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300452 self->seek(self, +1, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300453 }
454 break;
455 case NEWT_KEY_UP:
456 if (self->index == 0)
457 break;
458 --self->index;
459 if (self->index < self->first_visible_entry_idx) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300460 --self->first_visible_entry_idx;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300461 self->seek(self, -1, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300462 }
463 break;
464 case NEWT_KEY_PGDN:
Arnaldo Carvalho de Melo17930b42010-05-19 16:03:51 -0300465 case ' ':
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300466 if (self->first_visible_entry_idx + self->height > self->nr_entries - 1)
467 break;
468
469 offset = self->height;
470 if (self->index + offset > self->nr_entries - 1)
471 offset = self->nr_entries - 1 - self->index;
472 self->index += offset;
473 self->first_visible_entry_idx += offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300474 self->seek(self, +offset, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300475 break;
476 case NEWT_KEY_PGUP:
477 if (self->first_visible_entry_idx == 0)
478 break;
479
480 if (self->first_visible_entry_idx < self->height)
481 offset = self->first_visible_entry_idx;
482 else
483 offset = self->height;
484
485 self->index -= offset;
486 self->first_visible_entry_idx -= offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300487 self->seek(self, -offset, SEEK_CUR);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300488 break;
489 case NEWT_KEY_HOME:
490 ui_browser__reset_index(self);
491 break;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300492 case NEWT_KEY_END:
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300493 offset = self->height - 1;
Arnaldo Carvalho de Melo63f20e72010-07-15 07:21:07 -0300494 if (offset >= self->nr_entries)
495 offset = self->nr_entries - 1;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300496
Arnaldo Carvalho de Melo63f20e72010-07-15 07:21:07 -0300497 self->index = self->nr_entries - 1;
498 self->first_visible_entry_idx = self->index - offset;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300499 self->seek(self, -offset, SEEK_END);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300500 break;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300501 default:
Arnaldo Carvalho de Melob66ecd92010-07-15 07:24:30 -0300502 return es->u.key;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300503 }
504 if (ui_browser__refresh_entries(self) < 0)
505 return -1;
506 }
507 return 0;
508}
509
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300510/*
511 * When debugging newt problems it was useful to be able to "unroll"
512 * the calls to newtCheckBoxTreeAdd{Array,Item}, so that we can generate
513 * a source file with the sequence of calls to these methods, to then
514 * tweak the arrays to get the intended results, so I'm keeping this code
515 * here, may be useful again in the future.
516 */
517#undef NEWT_DEBUG
518
519static void newt_checkbox_tree__add(newtComponent tree, const char *str,
520 void *priv, int *indexes)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300521{
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300522#ifdef NEWT_DEBUG
523 /* Print the newtCheckboxTreeAddArray to tinker with its index arrays */
524 int i = 0, len = 40 - strlen(str);
525
526 fprintf(stderr,
527 "\tnewtCheckboxTreeAddItem(tree, %*.*s\"%s\", (void *)%p, 0, ",
528 len, len, " ", str, priv);
529 while (indexes[i] != NEWT_ARG_LAST) {
530 if (indexes[i] != NEWT_ARG_APPEND)
531 fprintf(stderr, " %d,", indexes[i]);
532 else
533 fprintf(stderr, " %s,", "NEWT_ARG_APPEND");
534 ++i;
535 }
536 fprintf(stderr, " %s", " NEWT_ARG_LAST);\n");
537 fflush(stderr);
538#endif
539 newtCheckboxTreeAddArray(tree, str, priv, 0, indexes);
540}
541
542static char *callchain_list__sym_name(struct callchain_list *self,
543 char *bf, size_t bfsize)
544{
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300545 if (self->ms.sym)
546 return self->ms.sym->name;
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300547
548 snprintf(bf, bfsize, "%#Lx", self->ip);
549 return bf;
550}
551
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300552static unsigned int hist_entry__annotate_browser_refresh(struct ui_browser *self)
553{
554 struct objdump_line *pos;
555 struct list_head *head = self->entries;
556 struct hist_entry *he = self->priv;
557 int row = 0;
558 int len = he->ms.sym->end - he->ms.sym->start;
559
560 if (self->first_visible_entry == NULL || self->first_visible_entry == self->entries)
561 self->first_visible_entry = head->next;
562
563 pos = list_entry(self->first_visible_entry, struct objdump_line, node);
564
565 list_for_each_entry_from(pos, head, node) {
566 bool current_entry = ui_browser__is_current_entry(self, row);
567 SLsmg_gotorc(self->top + row, self->left);
568 objdump_line__show(pos, head, self->width,
569 he, len, current_entry);
570 if (++row == self->height)
571 break;
572 }
573
574 return row;
575}
576
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300577static void __callchain__append_graph_browser(struct callchain_node *self,
578 newtComponent tree, u64 total,
579 int *indexes, int depth)
580{
581 struct rb_node *node;
582 u64 new_total, remaining;
583 int idx = 0;
584
585 if (callchain_param.mode == CHAIN_GRAPH_REL)
586 new_total = self->children_hit;
587 else
588 new_total = total;
589
590 remaining = new_total;
591 node = rb_first(&self->rb_root);
592 while (node) {
593 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
594 struct rb_node *next = rb_next(node);
595 u64 cumul = cumul_hits(child);
596 struct callchain_list *chain;
597 int first = true, printed = 0;
598 int chain_idx = -1;
599 remaining -= cumul;
600
601 indexes[depth] = NEWT_ARG_APPEND;
602 indexes[depth + 1] = NEWT_ARG_LAST;
603
604 list_for_each_entry(chain, &child->val, list) {
605 char ipstr[BITS_PER_LONG / 4 + 1],
606 *alloc_str = NULL;
607 const char *str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
608
609 if (first) {
610 double percent = cumul * 100.0 / new_total;
611
612 first = false;
613 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
614 str = "Not enough memory!";
615 else
616 str = alloc_str;
617 } else {
618 indexes[depth] = idx;
619 indexes[depth + 1] = NEWT_ARG_APPEND;
620 indexes[depth + 2] = NEWT_ARG_LAST;
621 ++chain_idx;
622 }
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300623 newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300624 free(alloc_str);
625 ++printed;
626 }
627
628 indexes[depth] = idx;
629 if (chain_idx != -1)
630 indexes[depth + 1] = chain_idx;
631 if (printed != 0)
632 ++idx;
633 __callchain__append_graph_browser(child, tree, new_total, indexes,
634 depth + (chain_idx != -1 ? 2 : 1));
635 node = next;
636 }
637}
638
639static void callchain__append_graph_browser(struct callchain_node *self,
640 newtComponent tree, u64 total,
641 int *indexes, int parent_idx)
642{
643 struct callchain_list *chain;
644 int i = 0;
645
646 indexes[1] = NEWT_ARG_APPEND;
647 indexes[2] = NEWT_ARG_LAST;
648
649 list_for_each_entry(chain, &self->val, list) {
650 char ipstr[BITS_PER_LONG / 4 + 1], *str;
651
652 if (chain->ip >= PERF_CONTEXT_MAX)
653 continue;
654
655 if (!i++ && sort__first_dimension == SORT_SYM)
656 continue;
657
658 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300659 newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300660 }
661
662 indexes[1] = parent_idx;
663 indexes[2] = NEWT_ARG_APPEND;
664 indexes[3] = NEWT_ARG_LAST;
665 __callchain__append_graph_browser(self, tree, total, indexes, 2);
666}
667
668static void hist_entry__append_callchain_browser(struct hist_entry *self,
669 newtComponent tree, u64 total, int parent_idx)
670{
671 struct rb_node *rb_node;
672 int indexes[1024] = { [0] = parent_idx, };
673 int idx = 0;
674 struct callchain_node *chain;
675
676 rb_node = rb_first(&self->sorted_chain);
677 while (rb_node) {
678 chain = rb_entry(rb_node, struct callchain_node, rb_node);
679 switch (callchain_param.mode) {
680 case CHAIN_FLAT:
681 break;
682 case CHAIN_GRAPH_ABS: /* falldown */
683 case CHAIN_GRAPH_REL:
684 callchain__append_graph_browser(chain, tree, total, indexes, idx++);
685 break;
686 case CHAIN_NONE:
687 default:
688 break;
689 }
690 rb_node = rb_next(rb_node);
691 }
692}
693
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300694static size_t hist_entry__append_browser(struct hist_entry *self,
695 newtComponent tree, u64 total)
696{
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300697 char s[256];
698 size_t ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300699
700 if (symbol_conf.exclude_other && !self->parent)
701 return 0;
702
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300703 ret = hist_entry__snprintf(self, s, sizeof(s), NULL,
704 false, 0, false, total);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300705 if (symbol_conf.use_callchain) {
706 int indexes[2];
707
708 indexes[0] = NEWT_ARG_APPEND;
709 indexes[1] = NEWT_ARG_LAST;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300710 newt_checkbox_tree__add(tree, s, &self->ms, indexes);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300711 } else
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300712 newtListboxAppendEntry(tree, s, &self->ms);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300713
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300714 return ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300715}
716
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300717int hist_entry__tui_annotate(struct hist_entry *self)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300718{
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300719 struct ui_browser browser;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300720 struct newtExitStruct es;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300721 struct objdump_line *pos, *n;
722 LIST_HEAD(head);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300723 int ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300724
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300725 if (self->ms.sym == NULL)
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300726 return -1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300727
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300728 if (self->ms.map->dso->annotate_warned)
729 return -1;
730
731 if (hist_entry__annotate(self, &head) < 0) {
732 ui__error_window(browser__last_msg);
733 return -1;
734 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300735
Arnaldo Carvalho de Melo60553902010-05-15 20:40:34 -0300736 ui_helpline__push("Press <- or ESC to exit");
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300737
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300738 memset(&browser, 0, sizeof(browser));
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300739 browser.entries = &head;
Arnaldo Carvalho de Melo9f61d852010-06-21 19:38:33 -0300740 browser.refresh_entries = hist_entry__annotate_browser_refresh;
Arnaldo Carvalho de Melo46b0a072010-06-21 13:36:20 -0300741 browser.seek = ui_browser__list_head_seek;
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300742 browser.priv = self;
743 list_for_each_entry(pos, &head, node) {
744 size_t line_len = strlen(pos->line);
745 if (browser.width < line_len)
746 browser.width = line_len;
747 ++browser.nr_entries;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300748 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300749
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300750 browser.width += 18; /* Percentage */
Arnaldo Carvalho de Melo13f499f2010-06-21 18:04:02 -0300751 ui_browser__show(&browser, self->ms.sym->name);
Srikar Dronamraju0879b102010-06-29 23:02:26 +0530752 ret = ui_browser__run(&browser, &es);
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300753 newtFormDestroy(browser.form);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300754 newtPopWindow();
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300755 list_for_each_entry_safe(pos, n, &head, node) {
756 list_del(&pos->node);
757 objdump_line__free(pos);
758 }
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300759 ui_helpline__pop();
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300760 return ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300761}
762
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300763static const void *newt__symbol_tree_get_current(newtComponent self)
764{
765 if (symbol_conf.use_callchain)
766 return newtCheckboxTreeGetCurrent(self);
767 return newtListboxGetCurrent(self);
768}
769
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300770static void hist_browser__selection(newtComponent self, void *data)
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300771{
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300772 const struct map_symbol **symbol_ptr = data;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300773 *symbol_ptr = newt__symbol_tree_get_current(self);
774}
775
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300776struct hist_browser {
777 newtComponent form, tree;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300778 const struct map_symbol *selection;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300779};
780
781static struct hist_browser *hist_browser__new(void)
782{
783 struct hist_browser *self = malloc(sizeof(*self));
784
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300785 if (self != NULL)
786 self->form = NULL;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300787
788 return self;
789}
790
791static void hist_browser__delete(struct hist_browser *self)
792{
793 newtFormDestroy(self->form);
794 newtPopWindow();
795 free(self);
796}
797
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300798static int hist_browser__populate(struct hist_browser *self, struct hists *hists,
799 const char *title)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300800{
801 int max_len = 0, idx, cols, rows;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300802 struct ui_progress *progress;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300803 struct rb_node *nd;
804 u64 curr_hist = 0;
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300805 char seq[] = ".", unit;
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300806 char str[256];
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300807 unsigned long nr_events = hists->stats.nr_events[PERF_RECORD_SAMPLE];
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300808
809 if (self->form) {
810 newtFormDestroy(self->form);
811 newtPopWindow();
812 }
813
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300814 nr_events = convert_unit(nr_events, &unit);
815 snprintf(str, sizeof(str), "Events: %lu%c ",
816 nr_events, unit);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300817 newtDrawRootText(0, 0, str);
818
819 newtGetScreenSize(NULL, &rows);
820
821 if (symbol_conf.use_callchain)
822 self->tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq,
823 NEWT_FLAG_SCROLL);
824 else
825 self->tree = newtListbox(0, 0, rows - 5,
826 (NEWT_FLAG_SCROLL |
827 NEWT_FLAG_RETURNEXIT));
828
829 newtComponentAddCallback(self->tree, hist_browser__selection,
830 &self->selection);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300831
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300832 progress = ui_progress__new("Adding entries to the browser...",
833 hists->nr_entries);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300834 if (progress == NULL)
835 return -1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300836
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300837 idx = 0;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300838 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300839 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300840 int len;
841
842 if (h->filtered)
843 continue;
844
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300845 len = hist_entry__append_browser(h, self->tree, hists->stats.total_period);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300846 if (len > max_len)
847 max_len = len;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300848 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300849 hist_entry__append_callchain_browser(h, self->tree,
Arnaldo Carvalho de Melocee75ac2010-05-14 13:16:55 -0300850 hists->stats.total_period, idx++);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300851 ++curr_hist;
852 if (curr_hist % 5)
853 ui_progress__update(progress, curr_hist);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300854 }
855
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300856 ui_progress__delete(progress);
857
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300858 newtGetScreenSize(&cols, &rows);
859
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300860 if (max_len > cols)
861 max_len = cols - 3;
862
863 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300864 newtListboxSetWidth(self->tree, max_len);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300865
866 newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0),
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300867 rows - 5, title);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300868 self->form = newt_form__new();
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300869 if (self->form == NULL)
870 return -1;
871
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300872 newtFormAddHotKey(self->form, 'A');
873 newtFormAddHotKey(self->form, 'a');
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300874 newtFormAddHotKey(self->form, 'D');
875 newtFormAddHotKey(self->form, 'd');
876 newtFormAddHotKey(self->form, 'T');
877 newtFormAddHotKey(self->form, 't');
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300878 newtFormAddHotKey(self->form, '?');
879 newtFormAddHotKey(self->form, 'H');
880 newtFormAddHotKey(self->form, 'h');
881 newtFormAddHotKey(self->form, NEWT_KEY_F1);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300882 newtFormAddHotKey(self->form, NEWT_KEY_RIGHT);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300883 newtFormAddHotKey(self->form, NEWT_KEY_TAB);
884 newtFormAddHotKey(self->form, NEWT_KEY_UNTAB);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300885 newtFormAddComponents(self->form, self->tree, NULL);
886 self->selection = newt__symbol_tree_get_current(self->tree);
887
888 return 0;
889}
890
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300891static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300892{
893 int *indexes;
894
895 if (!symbol_conf.use_callchain)
896 goto out;
897
898 indexes = newtCheckboxTreeFindItem(self->tree, (void *)self->selection);
899 if (indexes) {
900 bool is_hist_entry = indexes[1] == NEWT_ARG_LAST;
901 free(indexes);
902 if (is_hist_entry)
903 goto out;
904 }
905 return NULL;
906out:
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -0300907 return container_of(self->selection, struct hist_entry, ms);
908}
909
910static struct thread *hist_browser__selected_thread(struct hist_browser *self)
911{
912 struct hist_entry *he = hist_browser__selected_entry(self);
913 return he ? he->thread : NULL;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300914}
915
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300916static int hist_browser__title(char *bf, size_t size, const char *ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300917 const struct dso *dso, const struct thread *thread)
918{
919 int printed = 0;
920
921 if (thread)
922 printed += snprintf(bf + printed, size - printed,
923 "Thread: %s(%d)",
924 (thread->comm_set ? thread->comm : ""),
925 thread->pid);
926 if (dso)
927 printed += snprintf(bf + printed, size - printed,
928 "%sDSO: %s", thread ? " " : "",
929 dso->short_name);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300930 return printed ?: snprintf(bf, size, "Event: %s", ev_name);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300931}
932
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300933int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300934{
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300935 struct hist_browser *browser = hist_browser__new();
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300936 struct pstack *fstack;
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300937 const struct thread *thread_filter = NULL;
938 const struct dso *dso_filter = NULL;
939 struct newtExitStruct es;
940 char msg[160];
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300941 int key = -1;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300942
943 if (browser == NULL)
944 return -1;
945
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300946 fstack = pstack__new(2);
947 if (fstack == NULL)
948 goto out;
949
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300950 ui_helpline__push(helpline);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300951
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300952 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300953 dso_filter, thread_filter);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300954 if (hist_browser__populate(browser, self, msg) < 0)
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -0300955 goto out_free_stack;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300956
957 while (1) {
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300958 const struct thread *thread;
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300959 const struct dso *dso;
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300960 char *options[16];
961 int nr_options = 0, choice = 0, i,
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300962 annotate = -2, zoom_dso = -2, zoom_thread = -2;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300963
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300964 newtFormRun(browser->form, &es);
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300965
966 thread = hist_browser__selected_thread(browser);
967 dso = browser->selection->map ? browser->selection->map->dso : NULL;
968
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300969 if (es.reason == NEWT_EXIT_HOTKEY) {
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300970 key = es.u.key;
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300971
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -0300972 switch (key) {
973 case NEWT_KEY_F1:
974 goto do_help;
975 case NEWT_KEY_TAB:
976 case NEWT_KEY_UNTAB:
977 /*
978 * Exit the browser, let hists__browser_tree
979 * go to the next or previous
980 */
981 goto out_free_stack;
982 default:;
983 }
984
985 key = toupper(key);
986 switch (key) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300987 case 'A':
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -0300988 if (browser->selection->map == NULL &&
989 browser->selection->map->dso->annotate_warned)
990 continue;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300991 goto do_annotate;
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -0300992 case 'D':
993 goto zoom_dso;
994 case 'T':
995 goto zoom_thread;
Arnaldo Carvalho de Meloa9a4ab72010-05-16 21:04:27 -0300996 case 'H':
997 case '?':
998do_help:
999 ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
1000 "<- Zoom out\n"
1001 "a Annotate current symbol\n"
1002 "h/?/F1 Show this window\n"
1003 "d Zoom into current DSO\n"
1004 "t Zoom into current Thread\n"
1005 "q/CTRL+C Exit browser");
1006 continue;
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001007 default:;
1008 }
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001009 if (is_exit_key(key)) {
1010 if (key == NEWT_KEY_ESCAPE) {
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001011 if (dialog_yesno("Do you really want to exit?"))
1012 break;
1013 else
1014 continue;
1015 } else
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001016 break;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001017 }
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001018
1019 if (es.u.key == NEWT_KEY_LEFT) {
1020 const void *top;
1021
1022 if (pstack__empty(fstack))
1023 continue;
1024 top = pstack__pop(fstack);
1025 if (top == &dso_filter)
1026 goto zoom_out_dso;
1027 if (top == &thread_filter)
1028 goto zoom_out_thread;
1029 continue;
1030 }
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001031 }
1032
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001033 if (browser->selection->sym != NULL &&
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -03001034 !browser->selection->map->dso->annotate_warned &&
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001035 asprintf(&options[nr_options], "Annotate %s",
1036 browser->selection->sym->name) > 0)
1037 annotate = nr_options++;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001038
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001039 if (thread != NULL &&
1040 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001041 (thread_filter ? "out of" : "into"),
1042 (thread->comm_set ? thread->comm : ""),
1043 thread->pid) > 0)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001044 zoom_thread = nr_options++;
1045
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001046 if (dso != NULL &&
1047 asprintf(&options[nr_options], "Zoom %s %s DSO",
1048 (dso_filter ? "out of" : "into"),
1049 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
1050 zoom_dso = nr_options++;
1051
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001052 options[nr_options++] = (char *)"Exit";
1053
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001054 choice = popup_menu(nr_options, options);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001055
1056 for (i = 0; i < nr_options - 1; ++i)
1057 free(options[i]);
1058
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -03001059 if (choice == nr_options - 1)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001060 break;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001061
1062 if (choice == -1)
1063 continue;
Arnaldo Carvalho de Meloc1ec5fe2010-05-15 20:45:31 -03001064
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001065 if (choice == annotate) {
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001066 struct hist_entry *he;
Arnaldo Carvalho de Meloc1ec5fe2010-05-15 20:45:31 -03001067do_annotate:
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -03001068 if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
Arnaldo Carvalho de Melo6e78c9f2010-05-22 11:20:24 -03001069 browser->selection->map->dso->annotate_warned = 1;
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001070 ui_helpline__puts("No vmlinux file found, can't "
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -03001071 "annotate with just a "
1072 "kallsyms file");
1073 continue;
1074 }
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001075
1076 he = hist_browser__selected_entry(browser);
1077 if (he == NULL)
1078 continue;
1079
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001080 hist_entry__tui_annotate(he);
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001081 } else if (choice == zoom_dso) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001082zoom_dso:
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001083 if (dso_filter) {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001084 pstack__remove(fstack, &dso_filter);
1085zoom_out_dso:
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001086 ui_helpline__pop();
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001087 dso_filter = NULL;
1088 } else {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001089 if (dso == NULL)
1090 continue;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001091 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001092 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001093 dso_filter = dso;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001094 pstack__push(fstack, &dso_filter);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001095 }
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001096 hists__filter_by_dso(self, dso_filter);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001097 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001098 dso_filter, thread_filter);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001099 if (hist_browser__populate(browser, self, msg) < 0)
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -03001100 goto out;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001101 } else if (choice == zoom_thread) {
Arnaldo Carvalho de Melo9d192e12010-05-15 21:15:01 -03001102zoom_thread:
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001103 if (thread_filter) {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001104 pstack__remove(fstack, &thread_filter);
1105zoom_out_thread:
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001106 ui_helpline__pop();
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001107 thread_filter = NULL;
1108 } else {
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001109 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001110 thread->comm_set ? thread->comm : "",
1111 thread->pid);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001112 thread_filter = thread;
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001113 pstack__push(fstack, &thread_filter);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001114 }
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001115 hists__filter_by_thread(self, thread_filter);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001116 hist_browser__title(msg, sizeof(msg), ev_name,
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -03001117 dso_filter, thread_filter);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001118 if (hist_browser__populate(browser, self, msg) < 0)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -03001119 goto out;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -03001120 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001121 }
Arnaldo Carvalho de Melo3e1bbdc32010-05-14 20:05:21 -03001122out_free_stack:
1123 pstack__delete(fstack);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -03001124out:
1125 hist_browser__delete(browser);
Arnaldo Carvalho de Melod67f0882010-05-23 22:36:51 -03001126 return key;
1127}
1128
1129int hists__tui_browse_tree(struct rb_root *self, const char *help)
1130{
1131 struct rb_node *first = rb_first(self), *nd = first, *next;
1132 int key = 0;
1133
1134 while (nd) {
1135 struct hists *hists = rb_entry(nd, struct hists, rb_node);
1136 const char *ev_name = __event_name(hists->type, hists->config);
1137
1138 key = hists__browse(hists, help, ev_name);
1139
1140 if (is_exit_key(key))
1141 break;
1142
1143 switch (key) {
1144 case NEWT_KEY_TAB:
1145 next = rb_next(nd);
1146 if (next)
1147 nd = next;
1148 break;
1149 case NEWT_KEY_UNTAB:
1150 if (nd == first)
1151 continue;
1152 nd = rb_prev(nd);
1153 default:
1154 break;
1155 }
1156 }
1157
1158 return key;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001159}
1160
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001161static struct newtPercentTreeColors {
1162 const char *topColorFg, *topColorBg;
1163 const char *mediumColorFg, *mediumColorBg;
1164 const char *normalColorFg, *normalColorBg;
1165 const char *selColorFg, *selColorBg;
1166 const char *codeColorFg, *codeColorBg;
1167} defaultPercentTreeColors = {
1168 "red", "lightgray",
1169 "green", "lightgray",
1170 "black", "lightgray",
1171 "lightgray", "magenta",
1172 "blue", "lightgray",
1173};
1174
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001175void setup_browser(void)
1176{
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001177 struct newtPercentTreeColors *c = &defaultPercentTreeColors;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001178
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -03001179 if (!isatty(1) || !use_browser || dump_trace) {
Arnaldo Carvalho de Melo62e34362010-05-26 13:22:26 -03001180 use_browser = 0;
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -03001181 setup_pager();
1182 return;
1183 }
1184
1185 use_browser = 1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001186 newtInit();
1187 newtCls();
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -03001188 ui_helpline__puts(" ");
Arnaldo Carvalho de Melodc4ff192010-05-17 12:25:09 -03001189 sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
1190 sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
1191 sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
1192 sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
1193 sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001194}
1195
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -03001196void exit_browser(bool wait_for_ok)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001197{
Arnaldo Carvalho de Melo5d06e692010-05-20 22:01:10 -03001198 if (use_browser > 0) {
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -03001199 if (wait_for_ok) {
1200 char title[] = "Fatal Error", ok[] = "Ok";
1201 newtWinMessage(title, ok, browser__last_msg);
1202 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001203 newtFinished();
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -03001204 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001205}