blob: daa86efffce664ece10c5b5c1d7f07d2787bfea0 [file] [log] [blame]
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03001#define _GNU_SOURCE
2#include <stdio.h>
3#undef _GNU_SOURCE
4
5#include <stdlib.h>
6#include <newt.h>
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -03007#include <sys/ttydefaults.h>
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -03008
9#include "cache.h"
10#include "hist.h"
11#include "session.h"
12#include "sort.h"
13#include "symbol.h"
14
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030015struct ui_progress {
16 newtComponent form, scale;
17};
18
19struct ui_progress *ui_progress__new(const char *title, u64 total)
20{
21 struct ui_progress *self = malloc(sizeof(*self));
22
23 if (self != NULL) {
24 int cols;
25 newtGetScreenSize(&cols, NULL);
26 cols -= 4;
27 newtCenteredWindow(cols, 1, title);
28 self->form = newtForm(NULL, NULL, 0);
29 if (self->form == NULL)
30 goto out_free_self;
31 self->scale = newtScale(0, 0, cols, total);
32 if (self->scale == NULL)
33 goto out_free_form;
Arnaldo Carvalho de Melo7f826452010-05-10 10:51:25 -030034 newtFormAddComponent(self->form, self->scale);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030035 newtRefresh();
36 }
37
38 return self;
39
40out_free_form:
41 newtFormDestroy(self->form);
42out_free_self:
43 free(self);
44 return NULL;
45}
46
47void ui_progress__update(struct ui_progress *self, u64 curr)
48{
49 newtScaleSet(self->scale, curr);
50 newtRefresh();
51}
52
53void ui_progress__delete(struct ui_progress *self)
54{
55 newtFormDestroy(self->form);
56 newtPopWindow();
57 free(self);
58}
59
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -030060static void ui_helpline__pop(void)
61{
62 newtPopHelpLine();
63}
64
65static void ui_helpline__push(const char *msg)
66{
67 newtPushHelpLine(msg);
68}
69
70static void ui_helpline__vpush(const char *fmt, va_list ap)
71{
72 char *s;
73
74 if (vasprintf(&s, fmt, ap) < 0)
75 vfprintf(stderr, fmt, ap);
76 else {
77 ui_helpline__push(s);
78 free(s);
79 }
80}
81
82static void ui_helpline__fpush(const char *fmt, ...)
83{
84 va_list ap;
85
86 va_start(ap, fmt);
87 ui_helpline__vpush(fmt, ap);
88 va_end(ap);
89}
90
91static void ui_helpline__puts(const char *msg)
92{
93 ui_helpline__pop();
94 ui_helpline__push(msg);
95}
96
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -030097static char browser__last_msg[1024];
98
99int browser__show_help(const char *format, va_list ap)
100{
101 int ret;
102 static int backlog;
103
104 ret = vsnprintf(browser__last_msg + backlog,
105 sizeof(browser__last_msg) - backlog, format, ap);
106 backlog += ret;
107
108 if (browser__last_msg[backlog - 1] == '\n') {
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300109 ui_helpline__puts(browser__last_msg);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300110 newtRefresh();
111 backlog = 0;
112 }
113
114 return ret;
115}
116
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -0300117static void newt_form__set_exit_keys(newtComponent self)
118{
119 newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
120 newtFormAddHotKey(self, 'Q');
121 newtFormAddHotKey(self, 'q');
122 newtFormAddHotKey(self, CTRL('c'));
123}
124
125static newtComponent newt_form__new(void)
126{
127 newtComponent self = newtForm(NULL, NULL, 0);
128 if (self)
129 newt_form__set_exit_keys(self);
130 return self;
131}
132
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300133static int popup_menu(int argc, char * const argv[])
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300134{
135 struct newtExitStruct es;
136 int i, rc = -1, max_len = 5;
137 newtComponent listbox, form = newt_form__new();
138
139 if (form == NULL)
140 return -1;
141
142 listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
143 if (listbox == NULL)
144 goto out_destroy_form;
145
Arnaldo Carvalho de Melo7f826452010-05-10 10:51:25 -0300146 newtFormAddComponent(form, listbox);
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300147
148 for (i = 0; i < argc; ++i) {
149 int len = strlen(argv[i]);
150 if (len > max_len)
151 max_len = len;
152 if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
153 goto out_destroy_form;
154 }
155
156 newtCenteredWindow(max_len, argc, NULL);
157 newtFormRun(form, &es);
158 rc = newtListboxGetCurrent(listbox) - NULL;
159 if (es.reason == NEWT_EXIT_HOTKEY)
160 rc = -1;
161 newtPopWindow();
162out_destroy_form:
163 newtFormDestroy(form);
164 return rc;
165}
166
167static bool dialog_yesno(const char *msg)
168{
169 /* newtWinChoice should really be accepting const char pointers... */
170 char yes[] = "Yes", no[] = "No";
Arnaldo Carvalho de Meloc0ed55d2010-04-05 12:04:23 -0300171 return newtWinChoice(NULL, yes, no, (char *)msg) == 1;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300172}
173
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300174/*
175 * When debugging newt problems it was useful to be able to "unroll"
176 * the calls to newtCheckBoxTreeAdd{Array,Item}, so that we can generate
177 * a source file with the sequence of calls to these methods, to then
178 * tweak the arrays to get the intended results, so I'm keeping this code
179 * here, may be useful again in the future.
180 */
181#undef NEWT_DEBUG
182
183static void newt_checkbox_tree__add(newtComponent tree, const char *str,
184 void *priv, int *indexes)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300185{
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300186#ifdef NEWT_DEBUG
187 /* Print the newtCheckboxTreeAddArray to tinker with its index arrays */
188 int i = 0, len = 40 - strlen(str);
189
190 fprintf(stderr,
191 "\tnewtCheckboxTreeAddItem(tree, %*.*s\"%s\", (void *)%p, 0, ",
192 len, len, " ", str, priv);
193 while (indexes[i] != NEWT_ARG_LAST) {
194 if (indexes[i] != NEWT_ARG_APPEND)
195 fprintf(stderr, " %d,", indexes[i]);
196 else
197 fprintf(stderr, " %s,", "NEWT_ARG_APPEND");
198 ++i;
199 }
200 fprintf(stderr, " %s", " NEWT_ARG_LAST);\n");
201 fflush(stderr);
202#endif
203 newtCheckboxTreeAddArray(tree, str, priv, 0, indexes);
204}
205
206static char *callchain_list__sym_name(struct callchain_list *self,
207 char *bf, size_t bfsize)
208{
Arnaldo Carvalho de Melob3c9ac02010-03-24 16:40:18 -0300209 if (self->ms.sym)
210 return self->ms.sym->name;
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300211
212 snprintf(bf, bfsize, "%#Lx", self->ip);
213 return bf;
214}
215
216static void __callchain__append_graph_browser(struct callchain_node *self,
217 newtComponent tree, u64 total,
218 int *indexes, int depth)
219{
220 struct rb_node *node;
221 u64 new_total, remaining;
222 int idx = 0;
223
224 if (callchain_param.mode == CHAIN_GRAPH_REL)
225 new_total = self->children_hit;
226 else
227 new_total = total;
228
229 remaining = new_total;
230 node = rb_first(&self->rb_root);
231 while (node) {
232 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
233 struct rb_node *next = rb_next(node);
234 u64 cumul = cumul_hits(child);
235 struct callchain_list *chain;
236 int first = true, printed = 0;
237 int chain_idx = -1;
238 remaining -= cumul;
239
240 indexes[depth] = NEWT_ARG_APPEND;
241 indexes[depth + 1] = NEWT_ARG_LAST;
242
243 list_for_each_entry(chain, &child->val, list) {
244 char ipstr[BITS_PER_LONG / 4 + 1],
245 *alloc_str = NULL;
246 const char *str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
247
248 if (first) {
249 double percent = cumul * 100.0 / new_total;
250
251 first = false;
252 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
253 str = "Not enough memory!";
254 else
255 str = alloc_str;
256 } else {
257 indexes[depth] = idx;
258 indexes[depth + 1] = NEWT_ARG_APPEND;
259 indexes[depth + 2] = NEWT_ARG_LAST;
260 ++chain_idx;
261 }
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300262 newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300263 free(alloc_str);
264 ++printed;
265 }
266
267 indexes[depth] = idx;
268 if (chain_idx != -1)
269 indexes[depth + 1] = chain_idx;
270 if (printed != 0)
271 ++idx;
272 __callchain__append_graph_browser(child, tree, new_total, indexes,
273 depth + (chain_idx != -1 ? 2 : 1));
274 node = next;
275 }
276}
277
278static void callchain__append_graph_browser(struct callchain_node *self,
279 newtComponent tree, u64 total,
280 int *indexes, int parent_idx)
281{
282 struct callchain_list *chain;
283 int i = 0;
284
285 indexes[1] = NEWT_ARG_APPEND;
286 indexes[2] = NEWT_ARG_LAST;
287
288 list_for_each_entry(chain, &self->val, list) {
289 char ipstr[BITS_PER_LONG / 4 + 1], *str;
290
291 if (chain->ip >= PERF_CONTEXT_MAX)
292 continue;
293
294 if (!i++ && sort__first_dimension == SORT_SYM)
295 continue;
296
297 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300298 newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300299 }
300
301 indexes[1] = parent_idx;
302 indexes[2] = NEWT_ARG_APPEND;
303 indexes[3] = NEWT_ARG_LAST;
304 __callchain__append_graph_browser(self, tree, total, indexes, 2);
305}
306
307static void hist_entry__append_callchain_browser(struct hist_entry *self,
308 newtComponent tree, u64 total, int parent_idx)
309{
310 struct rb_node *rb_node;
311 int indexes[1024] = { [0] = parent_idx, };
312 int idx = 0;
313 struct callchain_node *chain;
314
315 rb_node = rb_first(&self->sorted_chain);
316 while (rb_node) {
317 chain = rb_entry(rb_node, struct callchain_node, rb_node);
318 switch (callchain_param.mode) {
319 case CHAIN_FLAT:
320 break;
321 case CHAIN_GRAPH_ABS: /* falldown */
322 case CHAIN_GRAPH_REL:
323 callchain__append_graph_browser(chain, tree, total, indexes, idx++);
324 break;
325 case CHAIN_NONE:
326 default:
327 break;
328 }
329 rb_node = rb_next(rb_node);
330 }
331}
332
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300333static size_t hist_entry__append_browser(struct hist_entry *self,
334 newtComponent tree, u64 total)
335{
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300336 char s[256];
337 size_t ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300338
339 if (symbol_conf.exclude_other && !self->parent)
340 return 0;
341
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300342 ret = hist_entry__snprintf(self, s, sizeof(s), NULL,
343 false, 0, false, total);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300344 if (symbol_conf.use_callchain) {
345 int indexes[2];
346
347 indexes[0] = NEWT_ARG_APPEND;
348 indexes[1] = NEWT_ARG_LAST;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300349 newt_checkbox_tree__add(tree, s, &self->ms, indexes);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300350 } else
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300351 newtListboxAppendEntry(tree, s, &self->ms);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300352
Arnaldo Carvalho de Meloa4e3b952010-03-31 11:33:40 -0300353 return ret;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300354}
355
Arnaldo Carvalho de Melo533c46c2010-04-03 11:54:35 -0300356static void map_symbol__annotate_browser(const struct map_symbol *self,
357 const char *input_name)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300358{
359 FILE *fp;
Arnaldo Carvalho de Melocb7afb72010-03-12 12:46:47 -0300360 int cols, rows;
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300361 newtComponent form, tree;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300362 struct newtExitStruct es;
363 char *str;
364 size_t line_len, max_line_len = 0;
365 size_t max_usable_width;
366 char *line = NULL;
367
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300368 if (self->sym == NULL)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300369 return;
370
Arnaldo Carvalho de Melo533c46c2010-04-03 11:54:35 -0300371 if (asprintf(&str, "perf annotate -i \"%s\" -d \"%s\" %s 2>&1 | expand",
372 input_name, self->map->dso->name, self->sym->name) < 0)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300373 return;
374
375 fp = popen(str, "r");
376 if (fp == NULL)
377 goto out_free_str;
378
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300379 ui_helpline__push("Press ESC to exit");
Arnaldo Carvalho de Melocb7afb72010-03-12 12:46:47 -0300380 newtGetScreenSize(&cols, &rows);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300381 tree = newtListbox(0, 0, rows - 5, NEWT_FLAG_SCROLL);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300382
383 while (!feof(fp)) {
384 if (getline(&line, &line_len, fp) < 0 || !line_len)
385 break;
386 while (line_len != 0 && isspace(line[line_len - 1]))
387 line[--line_len] = '\0';
388
389 if (line_len > max_line_len)
390 max_line_len = line_len;
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300391 newtListboxAppendEntry(tree, line, NULL);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300392 }
393 fclose(fp);
394 free(line);
395
Arnaldo Carvalho de Melocb7afb72010-03-12 12:46:47 -0300396 max_usable_width = cols - 22;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300397 if (max_line_len > max_usable_width)
398 max_line_len = max_usable_width;
399
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300400 newtListboxSetWidth(tree, max_line_len);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300401
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300402 newtCenteredWindow(max_line_len + 2, rows - 5, self->sym->name);
Arnaldo Carvalho de Melo7081e082010-03-12 10:48:12 -0300403 form = newt_form__new();
Arnaldo Carvalho de Melo7f826452010-05-10 10:51:25 -0300404 newtFormAddComponent(form, tree);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300405
406 newtFormRun(form, &es);
407 newtFormDestroy(form);
408 newtPopWindow();
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300409 ui_helpline__pop();
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300410out_free_str:
411 free(str);
412}
413
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300414static const void *newt__symbol_tree_get_current(newtComponent self)
415{
416 if (symbol_conf.use_callchain)
417 return newtCheckboxTreeGetCurrent(self);
418 return newtListboxGetCurrent(self);
419}
420
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300421static void hist_browser__selection(newtComponent self, void *data)
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300422{
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300423 const struct map_symbol **symbol_ptr = data;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300424 *symbol_ptr = newt__symbol_tree_get_current(self);
425}
426
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300427struct hist_browser {
428 newtComponent form, tree;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300429 const struct map_symbol *selection;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300430};
431
432static struct hist_browser *hist_browser__new(void)
433{
434 struct hist_browser *self = malloc(sizeof(*self));
435
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300436 if (self != NULL)
437 self->form = NULL;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300438
439 return self;
440}
441
442static void hist_browser__delete(struct hist_browser *self)
443{
444 newtFormDestroy(self->form);
445 newtPopWindow();
446 free(self);
447}
448
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300449static int hist_browser__populate(struct hist_browser *self, struct hists *hists,
450 const char *title)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300451{
452 int max_len = 0, idx, cols, rows;
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300453 struct ui_progress *progress;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300454 struct rb_node *nd;
455 u64 curr_hist = 0;
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300456 char seq[] = ".";
457 char str[256];
458
459 if (self->form) {
460 newtFormDestroy(self->form);
461 newtPopWindow();
462 }
463
464 snprintf(str, sizeof(str), "Samples: %Ld ",
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300465 hists->stats.total);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300466 newtDrawRootText(0, 0, str);
467
468 newtGetScreenSize(NULL, &rows);
469
470 if (symbol_conf.use_callchain)
471 self->tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq,
472 NEWT_FLAG_SCROLL);
473 else
474 self->tree = newtListbox(0, 0, rows - 5,
475 (NEWT_FLAG_SCROLL |
476 NEWT_FLAG_RETURNEXIT));
477
478 newtComponentAddCallback(self->tree, hist_browser__selection,
479 &self->selection);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300480
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300481 progress = ui_progress__new("Adding entries to the browser...",
482 hists->nr_entries);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300483 if (progress == NULL)
484 return -1;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300485
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300486 idx = 0;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300487 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300488 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300489 int len;
490
491 if (h->filtered)
492 continue;
493
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300494 len = hist_entry__append_browser(h, self->tree, hists->stats.total);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300495 if (len > max_len)
496 max_len = len;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300497 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300498 hist_entry__append_callchain_browser(h, self->tree,
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300499 hists->stats.total, idx++);
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300500 ++curr_hist;
501 if (curr_hist % 5)
502 ui_progress__update(progress, curr_hist);
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300503 }
504
Arnaldo Carvalho de Melo5f4d3f82010-03-26 21:16:22 -0300505 ui_progress__delete(progress);
506
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300507 newtGetScreenSize(&cols, &rows);
508
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300509 if (max_len > cols)
510 max_len = cols - 3;
511
512 if (!symbol_conf.use_callchain)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300513 newtListboxSetWidth(self->tree, max_len);
Arnaldo Carvalho de Melo4ded2b22010-03-22 17:52:49 -0300514
515 newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0),
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300516 rows - 5, title);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300517 self->form = newt_form__new();
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300518 if (self->form == NULL)
519 return -1;
520
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300521 newtFormAddHotKey(self->form, 'A');
522 newtFormAddHotKey(self->form, 'a');
523 newtFormAddHotKey(self->form, NEWT_KEY_RIGHT);
524 newtFormAddComponents(self->form, self->tree, NULL);
525 self->selection = newt__symbol_tree_get_current(self->tree);
526
527 return 0;
528}
529
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300530static struct thread *hist_browser__selected_thread(struct hist_browser *self)
531{
532 int *indexes;
533
534 if (!symbol_conf.use_callchain)
535 goto out;
536
537 indexes = newtCheckboxTreeFindItem(self->tree, (void *)self->selection);
538 if (indexes) {
539 bool is_hist_entry = indexes[1] == NEWT_ARG_LAST;
540 free(indexes);
541 if (is_hist_entry)
542 goto out;
543 }
544 return NULL;
545out:
546 return *(struct thread **)(self->selection + 1);
547}
548
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300549static int hist_browser__title(char *bf, size_t size, const char *input_name,
550 const struct dso *dso, const struct thread *thread)
551{
552 int printed = 0;
553
554 if (thread)
555 printed += snprintf(bf + printed, size - printed,
556 "Thread: %s(%d)",
557 (thread->comm_set ? thread->comm : ""),
558 thread->pid);
559 if (dso)
560 printed += snprintf(bf + printed, size - printed,
561 "%sDSO: %s", thread ? " " : "",
562 dso->short_name);
563 return printed ?: snprintf(bf, size, "Report: %s", input_name);
564}
565
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300566int hists__browse(struct hists *self, const char *helpline, const char *input_name)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300567{
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300568 struct hist_browser *browser = hist_browser__new();
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300569 const struct thread *thread_filter = NULL;
570 const struct dso *dso_filter = NULL;
571 struct newtExitStruct es;
572 char msg[160];
573 int err = -1;
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300574
575 if (browser == NULL)
576 return -1;
577
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300578 ui_helpline__push(helpline);
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300579
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300580 hist_browser__title(msg, sizeof(msg), input_name,
581 dso_filter, thread_filter);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300582 if (hist_browser__populate(browser, self, msg) < 0)
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300583 goto out;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300584
585 while (1) {
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300586 const struct thread *thread;
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300587 const struct dso *dso;
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300588 char *options[16];
589 int nr_options = 0, choice = 0, i,
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300590 annotate = -2, zoom_dso = -2, zoom_thread = -2;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300591
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300592 newtFormRun(browser->form, &es);
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300593 if (es.reason == NEWT_EXIT_HOTKEY) {
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300594 if (toupper(es.u.key) == 'A')
595 goto do_annotate;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300596 if (es.u.key == NEWT_KEY_ESCAPE ||
597 toupper(es.u.key) == 'Q' ||
598 es.u.key == CTRL('c')) {
599 if (dialog_yesno("Do you really want to exit?"))
600 break;
601 else
602 continue;
603 }
604 }
605
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300606 if (browser->selection->sym != NULL &&
607 asprintf(&options[nr_options], "Annotate %s",
608 browser->selection->sym->name) > 0)
609 annotate = nr_options++;
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300610
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300611 thread = hist_browser__selected_thread(browser);
612 if (thread != NULL &&
613 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300614 (thread_filter ? "out of" : "into"),
615 (thread->comm_set ? thread->comm : ""),
616 thread->pid) > 0)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300617 zoom_thread = nr_options++;
618
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300619 dso = browser->selection->map ? browser->selection->map->dso : NULL;
620 if (dso != NULL &&
621 asprintf(&options[nr_options], "Zoom %s %s DSO",
622 (dso_filter ? "out of" : "into"),
623 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
624 zoom_dso = nr_options++;
625
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300626 options[nr_options++] = (char *)"Exit";
627
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300628 choice = popup_menu(nr_options, options);
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300629
630 for (i = 0; i < nr_options - 1; ++i)
631 free(options[i]);
632
Arnaldo Carvalho de Melo53c54012010-03-24 16:40:14 -0300633 if (choice == nr_options - 1)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300634 break;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300635
636 if (choice == -1)
637 continue;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300638do_annotate:
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300639 if (choice == annotate) {
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300640 if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300641 ui_helpline__puts("No vmlinux file found, can't "
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300642 "annotate with just a "
643 "kallsyms file");
644 continue;
645 }
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300646 map_symbol__annotate_browser(browser->selection, input_name);
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300647 } else if (choice == zoom_dso) {
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300648 if (dso_filter) {
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300649 ui_helpline__pop();
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300650 dso_filter = NULL;
651 } else {
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300652 ui_helpline__fpush("To zoom out press -> + \"Zoom out of %s DSO\"",
653 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300654 dso_filter = dso;
655 }
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300656 hists__filter_by_dso(self, dso_filter);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300657 hist_browser__title(msg, sizeof(msg), input_name,
658 dso_filter, thread_filter);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300659 if (hist_browser__populate(browser, self, msg) < 0)
Arnaldo Carvalho de Melo83753192010-04-03 16:30:44 -0300660 goto out;
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300661 } else if (choice == zoom_thread) {
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300662 if (thread_filter) {
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300663 ui_helpline__pop();
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300664 thread_filter = NULL;
665 } else {
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300666 ui_helpline__fpush("To zoom out press -> + \"Zoom out of %s(%d) thread\"",
667 thread->comm_set ? thread->comm : "",
668 thread->pid);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300669 thread_filter = thread;
670 }
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300671 hists__filter_by_thread(self, thread_filter);
Arnaldo Carvalho de Melo6e7ab4c2010-04-05 12:02:18 -0300672 hist_browser__title(msg, sizeof(msg), input_name,
673 dso_filter, thread_filter);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -0300674 if (hist_browser__populate(browser, self, msg) < 0)
Arnaldo Carvalho de Meloa5e29ac2010-04-03 22:44:37 -0300675 goto out;
Arnaldo Carvalho de Melod5679ae2010-03-24 16:40:19 -0300676 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300677 }
Arnaldo Carvalho de Meloe65713e2010-04-03 11:25:56 -0300678 err = 0;
679out:
680 hist_browser__delete(browser);
681 return err;
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300682}
683
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300684void setup_browser(void)
685{
686 if (!isatty(1))
687 return;
688
689 use_browser = true;
690 newtInit();
691 newtCls();
Arnaldo Carvalho de Melo3798ed72010-05-11 18:01:23 -0300692 ui_helpline__puts(" ");
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300693}
694
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300695void exit_browser(bool wait_for_ok)
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300696{
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300697 if (use_browser) {
698 if (wait_for_ok) {
699 char title[] = "Fatal Error", ok[] = "Ok";
700 newtWinMessage(title, ok, browser__last_msg);
701 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300702 newtFinished();
Arnaldo Carvalho de Melof3a1f0e2010-03-22 13:10:25 -0300703 }
Arnaldo Carvalho de Melof9224c52010-03-11 20:12:44 -0300704}