perf symbols: map_groups__find_symbol must return the map too
[linux-2.6-block.git] / tools / perf / util / newt.c
CommitLineData
f9224c5c
ACM
1#define _GNU_SOURCE
2#include <stdio.h>
3#undef _GNU_SOURCE
4
5#include <stdlib.h>
6#include <newt.h>
7081e087 7#include <sys/ttydefaults.h>
f9224c5c
ACM
8
9#include "cache.h"
10#include "hist.h"
11#include "session.h"
12#include "sort.h"
13#include "symbol.h"
14
7081e087
ACM
15static void newt_form__set_exit_keys(newtComponent self)
16{
17 newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
18 newtFormAddHotKey(self, 'Q');
19 newtFormAddHotKey(self, 'q');
20 newtFormAddHotKey(self, CTRL('c'));
21}
22
23static newtComponent newt_form__new(void)
24{
25 newtComponent self = newtForm(NULL, NULL, 0);
26 if (self)
27 newt_form__set_exit_keys(self);
28 return self;
29}
30
53c54019
ACM
31static int popup_menu(int argc, const char *argv[])
32{
33 struct newtExitStruct es;
34 int i, rc = -1, max_len = 5;
35 newtComponent listbox, form = newt_form__new();
36
37 if (form == NULL)
38 return -1;
39
40 listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
41 if (listbox == NULL)
42 goto out_destroy_form;
43
44 newtFormAddComponents(form, listbox, NULL);
45
46 for (i = 0; i < argc; ++i) {
47 int len = strlen(argv[i]);
48 if (len > max_len)
49 max_len = len;
50 if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
51 goto out_destroy_form;
52 }
53
54 newtCenteredWindow(max_len, argc, NULL);
55 newtFormRun(form, &es);
56 rc = newtListboxGetCurrent(listbox) - NULL;
57 if (es.reason == NEWT_EXIT_HOTKEY)
58 rc = -1;
59 newtPopWindow();
60out_destroy_form:
61 newtFormDestroy(form);
62 return rc;
63}
64
65static bool dialog_yesno(const char *msg)
66{
67 /* newtWinChoice should really be accepting const char pointers... */
68 char yes[] = "Yes", no[] = "No";
69 return newtWinChoice(NULL, no, yes, (char *)msg) == 2;
70}
71
4ded2b25
ACM
72/*
73 * When debugging newt problems it was useful to be able to "unroll"
74 * the calls to newtCheckBoxTreeAdd{Array,Item}, so that we can generate
75 * a source file with the sequence of calls to these methods, to then
76 * tweak the arrays to get the intended results, so I'm keeping this code
77 * here, may be useful again in the future.
78 */
79#undef NEWT_DEBUG
80
81static void newt_checkbox_tree__add(newtComponent tree, const char *str,
82 void *priv, int *indexes)
83{
84#ifdef NEWT_DEBUG
85 /* Print the newtCheckboxTreeAddArray to tinker with its index arrays */
86 int i = 0, len = 40 - strlen(str);
87
88 fprintf(stderr,
89 "\tnewtCheckboxTreeAddItem(tree, %*.*s\"%s\", (void *)%p, 0, ",
90 len, len, " ", str, priv);
91 while (indexes[i] != NEWT_ARG_LAST) {
92 if (indexes[i] != NEWT_ARG_APPEND)
93 fprintf(stderr, " %d,", indexes[i]);
94 else
95 fprintf(stderr, " %s,", "NEWT_ARG_APPEND");
96 ++i;
97 }
98 fprintf(stderr, " %s", " NEWT_ARG_LAST);\n");
99 fflush(stderr);
100#endif
101 newtCheckboxTreeAddArray(tree, str, priv, 0, indexes);
102}
103
104static char *callchain_list__sym_name(struct callchain_list *self,
105 char *bf, size_t bfsize)
106{
b3c9ac08
ACM
107 if (self->ms.sym)
108 return self->ms.sym->name;
4ded2b25
ACM
109
110 snprintf(bf, bfsize, "%#Lx", self->ip);
111 return bf;
112}
113
114static void __callchain__append_graph_browser(struct callchain_node *self,
115 newtComponent tree, u64 total,
116 int *indexes, int depth)
117{
118 struct rb_node *node;
119 u64 new_total, remaining;
120 int idx = 0;
121
122 if (callchain_param.mode == CHAIN_GRAPH_REL)
123 new_total = self->children_hit;
124 else
125 new_total = total;
126
127 remaining = new_total;
128 node = rb_first(&self->rb_root);
129 while (node) {
130 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
131 struct rb_node *next = rb_next(node);
132 u64 cumul = cumul_hits(child);
133 struct callchain_list *chain;
134 int first = true, printed = 0;
135 int chain_idx = -1;
136 remaining -= cumul;
137
138 indexes[depth] = NEWT_ARG_APPEND;
139 indexes[depth + 1] = NEWT_ARG_LAST;
140
141 list_for_each_entry(chain, &child->val, list) {
142 char ipstr[BITS_PER_LONG / 4 + 1],
143 *alloc_str = NULL;
144 const char *str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
145
146 if (first) {
147 double percent = cumul * 100.0 / new_total;
148
149 first = false;
150 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
151 str = "Not enough memory!";
152 else
153 str = alloc_str;
154 } else {
155 indexes[depth] = idx;
156 indexes[depth + 1] = NEWT_ARG_APPEND;
157 indexes[depth + 2] = NEWT_ARG_LAST;
158 ++chain_idx;
159 }
d5679ae4 160 newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
4ded2b25
ACM
161 free(alloc_str);
162 ++printed;
163 }
164
165 indexes[depth] = idx;
166 if (chain_idx != -1)
167 indexes[depth + 1] = chain_idx;
168 if (printed != 0)
169 ++idx;
170 __callchain__append_graph_browser(child, tree, new_total, indexes,
171 depth + (chain_idx != -1 ? 2 : 1));
172 node = next;
173 }
174}
175
176static void callchain__append_graph_browser(struct callchain_node *self,
177 newtComponent tree, u64 total,
178 int *indexes, int parent_idx)
179{
180 struct callchain_list *chain;
181 int i = 0;
182
183 indexes[1] = NEWT_ARG_APPEND;
184 indexes[2] = NEWT_ARG_LAST;
185
186 list_for_each_entry(chain, &self->val, list) {
187 char ipstr[BITS_PER_LONG / 4 + 1], *str;
188
189 if (chain->ip >= PERF_CONTEXT_MAX)
190 continue;
191
192 if (!i++ && sort__first_dimension == SORT_SYM)
193 continue;
194
195 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
d5679ae4 196 newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
4ded2b25
ACM
197 }
198
199 indexes[1] = parent_idx;
200 indexes[2] = NEWT_ARG_APPEND;
201 indexes[3] = NEWT_ARG_LAST;
202 __callchain__append_graph_browser(self, tree, total, indexes, 2);
203}
204
205static void hist_entry__append_callchain_browser(struct hist_entry *self,
206 newtComponent tree, u64 total, int parent_idx)
207{
208 struct rb_node *rb_node;
209 int indexes[1024] = { [0] = parent_idx, };
210 int idx = 0;
211 struct callchain_node *chain;
212
213 rb_node = rb_first(&self->sorted_chain);
214 while (rb_node) {
215 chain = rb_entry(rb_node, struct callchain_node, rb_node);
216 switch (callchain_param.mode) {
217 case CHAIN_FLAT:
218 break;
219 case CHAIN_GRAPH_ABS: /* falldown */
220 case CHAIN_GRAPH_REL:
221 callchain__append_graph_browser(chain, tree, total, indexes, idx++);
222 break;
223 case CHAIN_NONE:
224 default:
225 break;
226 }
227 rb_node = rb_next(rb_node);
228 }
229}
230
231/*
232 * FIXME: get lib/string.c linked with perf somehow
233 */
234static char *skip_spaces(const char *str)
235{
236 while (isspace(*str))
237 ++str;
238 return (char *)str;
239}
240
241static char *strim(char *s)
242{
243 size_t size;
244 char *end;
245
246 s = skip_spaces(s);
247 size = strlen(s);
248 if (!size)
249 return s;
250
251 end = s + size - 1;
252 while (end >= s && isspace(*end))
253 end--;
254 *(end + 1) = '\0';
255
256 return s;
257}
258
f9224c5c 259static size_t hist_entry__append_browser(struct hist_entry *self,
4ded2b25 260 newtComponent tree, u64 total)
f9224c5c 261{
4ded2b25 262 char bf[1024], *s;
f9224c5c
ACM
263 FILE *fp;
264
265 if (symbol_conf.exclude_other && !self->parent)
266 return 0;
267
268 fp = fmemopen(bf, sizeof(bf), "w");
269 if (fp == NULL)
270 return 0;
271
4ded2b25 272 hist_entry__fprintf(self, NULL, false, 0, fp, total);
f9224c5c 273 fclose(fp);
4ded2b25
ACM
274
275 /*
276 * FIXME: We shouldn't need to trim, as the printing routines shouldn't
277 * add spaces it in the first place, the stdio output routines should
278 * call a __snprintf method instead of the current __print (that
279 * actually is a __fprintf) one, but get the raw string and _then_ add
280 * the newline, as this is a detail of stdio printing, not needed in
281 * other UIs, e.g. newt.
282 */
283 s = strim(bf);
284
285 if (symbol_conf.use_callchain) {
286 int indexes[2];
287
288 indexes[0] = NEWT_ARG_APPEND;
289 indexes[1] = NEWT_ARG_LAST;
d5679ae4 290 newt_checkbox_tree__add(tree, s, &self->ms, indexes);
4ded2b25 291 } else
d5679ae4 292 newtListboxAppendEntry(tree, s, &self->ms);
4ded2b25
ACM
293
294 return strlen(s);
f9224c5c
ACM
295}
296
d5679ae4 297static void map_symbol__annotate_browser(const struct map_symbol *self)
f9224c5c
ACM
298{
299 FILE *fp;
cb7afb70 300 int cols, rows;
4ded2b25 301 newtComponent form, tree;
f9224c5c
ACM
302 struct newtExitStruct es;
303 char *str;
304 size_t line_len, max_line_len = 0;
305 size_t max_usable_width;
306 char *line = NULL;
307
d5679ae4 308 if (self->sym == NULL)
f9224c5c
ACM
309 return;
310
d5679ae4
ACM
311 if (asprintf(&str, "perf annotate -d \"%s\" %s 2>&1 | expand",
312 self->map->dso->name, self->sym->name) < 0)
f9224c5c
ACM
313 return;
314
315 fp = popen(str, "r");
316 if (fp == NULL)
317 goto out_free_str;
318
319 newtPushHelpLine("Press ESC to exit");
cb7afb70 320 newtGetScreenSize(&cols, &rows);
4ded2b25 321 tree = newtListbox(0, 0, rows - 5, NEWT_FLAG_SCROLL);
f9224c5c
ACM
322
323 while (!feof(fp)) {
324 if (getline(&line, &line_len, fp) < 0 || !line_len)
325 break;
326 while (line_len != 0 && isspace(line[line_len - 1]))
327 line[--line_len] = '\0';
328
329 if (line_len > max_line_len)
330 max_line_len = line_len;
4ded2b25 331 newtListboxAppendEntry(tree, line, NULL);
f9224c5c
ACM
332 }
333 fclose(fp);
334 free(line);
335
cb7afb70 336 max_usable_width = cols - 22;
f9224c5c
ACM
337 if (max_line_len > max_usable_width)
338 max_line_len = max_usable_width;
339
4ded2b25 340 newtListboxSetWidth(tree, max_line_len);
f9224c5c 341
d5679ae4 342 newtCenteredWindow(max_line_len + 2, rows - 5, self->sym->name);
7081e087 343 form = newt_form__new();
4ded2b25 344 newtFormAddComponents(form, tree, NULL);
f9224c5c
ACM
345
346 newtFormRun(form, &es);
347 newtFormDestroy(form);
348 newtPopWindow();
349 newtPopHelpLine();
350out_free_str:
351 free(str);
352}
353
53c54019
ACM
354static const void *newt__symbol_tree_get_current(newtComponent self)
355{
356 if (symbol_conf.use_callchain)
357 return newtCheckboxTreeGetCurrent(self);
358 return newtListboxGetCurrent(self);
359}
360
361static void perf_session__selection(newtComponent self, void *data)
362{
d5679ae4 363 const struct map_symbol **symbol_ptr = data;
53c54019
ACM
364 *symbol_ptr = newt__symbol_tree_get_current(self);
365}
366
f9224c5c
ACM
367void perf_session__browse_hists(struct rb_root *hists, u64 session_total,
368 const char *helpline)
369{
370 struct sort_entry *se;
371 struct rb_node *nd;
4ded2b25 372 char seq[] = ".";
f9224c5c
ACM
373 unsigned int width;
374 char *col_width = symbol_conf.col_width_list_str;
4ded2b25
ACM
375 int rows, cols, idx;
376 int max_len = 0;
f9224c5c 377 char str[1024];
4ded2b25 378 newtComponent form, tree;
f9224c5c 379 struct newtExitStruct es;
d5679ae4 380 const struct map_symbol *selection;
f9224c5c
ACM
381
382 snprintf(str, sizeof(str), "Samples: %Ld", session_total);
383 newtDrawRootText(0, 0, str);
384 newtPushHelpLine(helpline);
385
4ded2b25 386 newtGetScreenSize(&cols, &rows);
f9224c5c 387
4ded2b25
ACM
388 if (symbol_conf.use_callchain)
389 tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq,
390 NEWT_FLAG_SCROLL);
391 else
392 tree = newtListbox(0, 0, rows - 5, (NEWT_FLAG_SCROLL |
393 NEWT_FLAG_RETURNEXIT));
f9224c5c 394
53c54019
ACM
395 newtComponentAddCallback(tree, perf_session__selection, &selection);
396
f9224c5c
ACM
397 list_for_each_entry(se, &hist_entry__sort_list, list) {
398 if (se->elide)
399 continue;
400 width = strlen(se->header);
401 if (se->width) {
402 if (symbol_conf.col_width_list_str) {
403 if (col_width) {
404 *se->width = atoi(col_width);
405 col_width = strchr(col_width, ',');
406 if (col_width)
407 ++col_width;
408 }
409 }
410 *se->width = max(*se->width, width);
411 }
412 }
413
4ded2b25 414 idx = 0;
f9224c5c
ACM
415 for (nd = rb_first(hists); nd; nd = rb_next(nd)) {
416 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
4ded2b25 417 int len = hist_entry__append_browser(h, tree, session_total);
f9224c5c
ACM
418 if (len > max_len)
419 max_len = len;
d5679ae4 420 if (symbol_conf.use_callchain)
4ded2b25 421 hist_entry__append_callchain_browser(h, tree, session_total, idx++);
f9224c5c
ACM
422 }
423
4ded2b25
ACM
424 if (max_len > cols)
425 max_len = cols - 3;
426
427 if (!symbol_conf.use_callchain)
428 newtListboxSetWidth(tree, max_len);
429
430 newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0),
431 rows - 5, "Report");
432 form = newt_form__new();
433 newtFormAddHotKey(form, 'A');
434 newtFormAddHotKey(form, 'a');
53c54019 435 newtFormAddHotKey(form, NEWT_KEY_RIGHT);
4ded2b25 436 newtFormAddComponents(form, tree, NULL);
53c54019 437 selection = newt__symbol_tree_get_current(tree);
f9224c5c
ACM
438
439 while (1) {
53c54019
ACM
440 char annotate[512];
441 const char *options[2];
d5679ae4 442 int nr_options = 0, choice = 0;
f9224c5c
ACM
443
444 newtFormRun(form, &es);
53c54019 445 if (es.reason == NEWT_EXIT_HOTKEY) {
d5679ae4
ACM
446 if (toupper(es.u.key) == 'A')
447 goto do_annotate;
53c54019
ACM
448 if (es.u.key == NEWT_KEY_ESCAPE ||
449 toupper(es.u.key) == 'Q' ||
450 es.u.key == CTRL('c')) {
451 if (dialog_yesno("Do you really want to exit?"))
452 break;
453 else
454 continue;
455 }
456 }
457
d5679ae4 458 if (selection->sym != NULL) {
53c54019 459 snprintf(annotate, sizeof(annotate),
d5679ae4 460 "Annotate %s", selection->sym->name);
53c54019
ACM
461 options[nr_options++] = annotate;
462 }
463
464 options[nr_options++] = "Exit";
465 choice = popup_menu(nr_options, options);
466 if (choice == nr_options - 1)
f9224c5c 467 break;
d5679ae4
ACM
468do_annotate:
469 if (selection->sym != NULL && choice >= 0) {
470 if (selection->map->dso->origin == DSO__ORIG_KERNEL) {
471 newtPopHelpLine();
472 newtPushHelpLine("No vmlinux file found, can't "
473 "annotate with just a "
474 "kallsyms file");
475 continue;
476 }
477 map_symbol__annotate_browser(selection);
478 }
f9224c5c
ACM
479 }
480
481 newtFormDestroy(form);
4ded2b25 482 newtPopWindow();
f9224c5c
ACM
483}
484
f3a1f0ea
ACM
485static char browser__last_msg[1024];
486
f9224c5c
ACM
487int browser__show_help(const char *format, va_list ap)
488{
489 int ret;
490 static int backlog;
f9224c5c 491
f3a1f0ea
ACM
492 ret = vsnprintf(browser__last_msg + backlog,
493 sizeof(browser__last_msg) - backlog, format, ap);
f9224c5c
ACM
494 backlog += ret;
495
f3a1f0ea 496 if (browser__last_msg[backlog - 1] == '\n') {
f9224c5c 497 newtPopHelpLine();
f3a1f0ea 498 newtPushHelpLine(browser__last_msg);
f9224c5c
ACM
499 newtRefresh();
500 backlog = 0;
501 }
502
503 return ret;
504}
505
f9224c5c
ACM
506void setup_browser(void)
507{
508 if (!isatty(1))
509 return;
510
511 use_browser = true;
512 newtInit();
513 newtCls();
514 newtPushHelpLine(" ");
515}
516
f3a1f0ea 517void exit_browser(bool wait_for_ok)
f9224c5c 518{
f3a1f0ea
ACM
519 if (use_browser) {
520 if (wait_for_ok) {
521 char title[] = "Fatal Error", ok[] = "Ok";
522 newtWinMessage(title, ok, browser__last_msg);
523 }
f9224c5c 524 newtFinished();
f3a1f0ea 525 }
f9224c5c 526}