Commit | Line | Data |
---|---|---|
f9224c5c ACM |
1 | #define _GNU_SOURCE |
2 | #include <stdio.h> | |
3 | #undef _GNU_SOURCE | |
4 | ||
ef7b93a1 | 5 | #include <slang.h> |
f9224c5c ACM |
6 | #include <stdlib.h> |
7 | #include <newt.h> | |
7081e087 | 8 | #include <sys/ttydefaults.h> |
f9224c5c ACM |
9 | |
10 | #include "cache.h" | |
11 | #include "hist.h" | |
3e1bbdc3 | 12 | #include "pstack.h" |
f9224c5c ACM |
13 | #include "session.h" |
14 | #include "sort.h" | |
15 | #include "symbol.h" | |
16 | ||
5f4d3f88 ACM |
17 | struct ui_progress { |
18 | newtComponent form, scale; | |
19 | }; | |
20 | ||
21 | struct ui_progress *ui_progress__new(const char *title, u64 total) | |
22 | { | |
23 | struct ui_progress *self = malloc(sizeof(*self)); | |
24 | ||
25 | if (self != NULL) { | |
26 | int cols; | |
27 | newtGetScreenSize(&cols, NULL); | |
28 | cols -= 4; | |
29 | newtCenteredWindow(cols, 1, title); | |
30 | self->form = newtForm(NULL, NULL, 0); | |
31 | if (self->form == NULL) | |
32 | goto out_free_self; | |
33 | self->scale = newtScale(0, 0, cols, total); | |
34 | if (self->scale == NULL) | |
35 | goto out_free_form; | |
7f826453 | 36 | newtFormAddComponent(self->form, self->scale); |
5f4d3f88 ACM |
37 | newtRefresh(); |
38 | } | |
39 | ||
40 | return self; | |
41 | ||
42 | out_free_form: | |
43 | newtFormDestroy(self->form); | |
44 | out_free_self: | |
45 | free(self); | |
46 | return NULL; | |
47 | } | |
48 | ||
49 | void ui_progress__update(struct ui_progress *self, u64 curr) | |
50 | { | |
51 | newtScaleSet(self->scale, curr); | |
52 | newtRefresh(); | |
53 | } | |
54 | ||
55 | void ui_progress__delete(struct ui_progress *self) | |
56 | { | |
57 | newtFormDestroy(self->form); | |
58 | newtPopWindow(); | |
59 | free(self); | |
60 | } | |
61 | ||
3798ed7b ACM |
62 | static void ui_helpline__pop(void) |
63 | { | |
64 | newtPopHelpLine(); | |
65 | } | |
66 | ||
67 | static void ui_helpline__push(const char *msg) | |
68 | { | |
69 | newtPushHelpLine(msg); | |
70 | } | |
71 | ||
72 | static void ui_helpline__vpush(const char *fmt, va_list ap) | |
73 | { | |
74 | char *s; | |
75 | ||
76 | if (vasprintf(&s, fmt, ap) < 0) | |
77 | vfprintf(stderr, fmt, ap); | |
78 | else { | |
79 | ui_helpline__push(s); | |
80 | free(s); | |
81 | } | |
82 | } | |
83 | ||
84 | static void ui_helpline__fpush(const char *fmt, ...) | |
85 | { | |
86 | va_list ap; | |
87 | ||
88 | va_start(ap, fmt); | |
89 | ui_helpline__vpush(fmt, ap); | |
90 | va_end(ap); | |
91 | } | |
92 | ||
93 | static void ui_helpline__puts(const char *msg) | |
94 | { | |
95 | ui_helpline__pop(); | |
96 | ui_helpline__push(msg); | |
97 | } | |
98 | ||
5f4d3f88 ACM |
99 | static char browser__last_msg[1024]; |
100 | ||
101 | int browser__show_help(const char *format, va_list ap) | |
102 | { | |
103 | int ret; | |
104 | static int backlog; | |
105 | ||
106 | ret = vsnprintf(browser__last_msg + backlog, | |
107 | sizeof(browser__last_msg) - backlog, format, ap); | |
108 | backlog += ret; | |
109 | ||
110 | if (browser__last_msg[backlog - 1] == '\n') { | |
3798ed7b | 111 | ui_helpline__puts(browser__last_msg); |
5f4d3f88 ACM |
112 | newtRefresh(); |
113 | backlog = 0; | |
114 | } | |
115 | ||
116 | return ret; | |
117 | } | |
118 | ||
7081e087 ACM |
119 | static void newt_form__set_exit_keys(newtComponent self) |
120 | { | |
a308f3a8 | 121 | newtFormAddHotKey(self, NEWT_KEY_LEFT); |
7081e087 ACM |
122 | newtFormAddHotKey(self, NEWT_KEY_ESCAPE); |
123 | newtFormAddHotKey(self, 'Q'); | |
124 | newtFormAddHotKey(self, 'q'); | |
125 | newtFormAddHotKey(self, CTRL('c')); | |
126 | } | |
127 | ||
128 | static newtComponent newt_form__new(void) | |
129 | { | |
130 | newtComponent self = newtForm(NULL, NULL, 0); | |
131 | if (self) | |
132 | newt_form__set_exit_keys(self); | |
133 | return self; | |
134 | } | |
135 | ||
83753190 | 136 | static int popup_menu(int argc, char * const argv[]) |
53c54019 ACM |
137 | { |
138 | struct newtExitStruct es; | |
139 | int i, rc = -1, max_len = 5; | |
140 | newtComponent listbox, form = newt_form__new(); | |
141 | ||
142 | if (form == NULL) | |
143 | return -1; | |
144 | ||
145 | listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT); | |
146 | if (listbox == NULL) | |
147 | goto out_destroy_form; | |
148 | ||
7f826453 | 149 | newtFormAddComponent(form, listbox); |
53c54019 ACM |
150 | |
151 | for (i = 0; i < argc; ++i) { | |
152 | int len = strlen(argv[i]); | |
153 | if (len > max_len) | |
154 | max_len = len; | |
155 | if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i)) | |
156 | goto out_destroy_form; | |
157 | } | |
158 | ||
159 | newtCenteredWindow(max_len, argc, NULL); | |
160 | newtFormRun(form, &es); | |
161 | rc = newtListboxGetCurrent(listbox) - NULL; | |
162 | if (es.reason == NEWT_EXIT_HOTKEY) | |
163 | rc = -1; | |
164 | newtPopWindow(); | |
165 | out_destroy_form: | |
166 | newtFormDestroy(form); | |
167 | return rc; | |
168 | } | |
169 | ||
170 | static bool dialog_yesno(const char *msg) | |
171 | { | |
172 | /* newtWinChoice should really be accepting const char pointers... */ | |
173 | char yes[] = "Yes", no[] = "No"; | |
c0ed55d2 | 174 | return newtWinChoice(NULL, yes, no, (char *)msg) == 1; |
53c54019 ACM |
175 | } |
176 | ||
ef7b93a1 ACM |
177 | #define HE_COLORSET_TOP 50 |
178 | #define HE_COLORSET_MEDIUM 51 | |
179 | #define HE_COLORSET_NORMAL 52 | |
180 | #define HE_COLORSET_SELECTED 53 | |
181 | #define HE_COLORSET_CODE 54 | |
182 | ||
183 | static int ui_browser__percent_color(double percent, bool current) | |
184 | { | |
185 | if (current) | |
186 | return HE_COLORSET_SELECTED; | |
187 | if (percent >= MIN_RED) | |
188 | return HE_COLORSET_TOP; | |
189 | if (percent >= MIN_GREEN) | |
190 | return HE_COLORSET_MEDIUM; | |
191 | return HE_COLORSET_NORMAL; | |
192 | } | |
193 | ||
194 | struct ui_browser { | |
195 | newtComponent form, sb; | |
196 | u64 index, first_visible_entry_idx; | |
197 | void *first_visible_entry, *entries; | |
198 | u16 top, left, width, height; | |
199 | void *priv; | |
200 | u32 nr_entries; | |
201 | }; | |
202 | ||
203 | static void ui_browser__refresh_dimensions(struct ui_browser *self) | |
204 | { | |
205 | int cols, rows; | |
206 | newtGetScreenSize(&cols, &rows); | |
207 | ||
208 | if (self->width > cols - 4) | |
209 | self->width = cols - 4; | |
210 | self->height = rows - 5; | |
211 | if (self->height > self->nr_entries) | |
212 | self->height = self->nr_entries; | |
213 | self->top = (rows - self->height) / 2; | |
214 | self->left = (cols - self->width) / 2; | |
215 | } | |
216 | ||
217 | static void ui_browser__reset_index(struct ui_browser *self) | |
218 | { | |
219 | self->index = self->first_visible_entry_idx = 0; | |
220 | self->first_visible_entry = NULL; | |
221 | } | |
222 | ||
223 | static int objdump_line__show(struct objdump_line *self, struct list_head *head, | |
224 | int width, struct hist_entry *he, int len, | |
225 | bool current_entry) | |
226 | { | |
227 | if (self->offset != -1) { | |
228 | struct symbol *sym = he->ms.sym; | |
229 | unsigned int hits = 0; | |
230 | double percent = 0.0; | |
231 | int color; | |
232 | struct sym_priv *priv = symbol__priv(sym); | |
233 | struct sym_ext *sym_ext = priv->ext; | |
234 | struct sym_hist *h = priv->hist; | |
235 | s64 offset = self->offset; | |
236 | struct objdump_line *next = objdump__get_next_ip_line(head, self); | |
237 | ||
238 | while (offset < (s64)len && | |
239 | (next == NULL || offset < next->offset)) { | |
240 | if (sym_ext) { | |
241 | percent += sym_ext[offset].percent; | |
242 | } else | |
243 | hits += h->ip[offset]; | |
244 | ||
245 | ++offset; | |
246 | } | |
247 | ||
248 | if (sym_ext == NULL && h->sum) | |
249 | percent = 100.0 * hits / h->sum; | |
250 | ||
251 | color = ui_browser__percent_color(percent, current_entry); | |
252 | SLsmg_set_color(color); | |
253 | SLsmg_printf(" %7.2f ", percent); | |
254 | if (!current_entry) | |
255 | SLsmg_set_color(HE_COLORSET_CODE); | |
256 | } else { | |
257 | int color = ui_browser__percent_color(0, current_entry); | |
258 | SLsmg_set_color(color); | |
259 | SLsmg_write_nstring(" ", 9); | |
260 | } | |
261 | ||
262 | SLsmg_write_char(':'); | |
263 | SLsmg_write_nstring(" ", 8); | |
264 | if (!*self->line) | |
265 | SLsmg_write_nstring(" ", width - 18); | |
266 | else | |
267 | SLsmg_write_nstring(self->line, width - 18); | |
268 | ||
269 | return 0; | |
270 | } | |
271 | ||
272 | static int ui_browser__refresh_entries(struct ui_browser *self) | |
273 | { | |
274 | struct objdump_line *pos; | |
275 | struct list_head *head = self->entries; | |
276 | struct hist_entry *he = self->priv; | |
277 | int row = 0; | |
278 | int len = he->ms.sym->end - he->ms.sym->start; | |
279 | ||
280 | if (self->first_visible_entry == NULL || self->first_visible_entry == self->entries) | |
281 | self->first_visible_entry = head->next; | |
282 | ||
283 | pos = list_entry(self->first_visible_entry, struct objdump_line, node); | |
284 | ||
285 | list_for_each_entry_from(pos, head, node) { | |
286 | bool current_entry = (self->first_visible_entry_idx + row) == self->index; | |
287 | SLsmg_gotorc(self->top + row, self->left); | |
288 | objdump_line__show(pos, head, self->width, | |
289 | he, len, current_entry); | |
290 | if (++row == self->height) | |
291 | break; | |
292 | } | |
293 | ||
294 | SLsmg_set_color(HE_COLORSET_NORMAL); | |
295 | SLsmg_fill_region(self->top + row, self->left, | |
296 | self->height - row, self->width, ' '); | |
297 | ||
298 | return 0; | |
299 | } | |
300 | ||
301 | static int ui_browser__run(struct ui_browser *self, const char *title, | |
302 | struct newtExitStruct *es) | |
303 | { | |
304 | if (self->form) { | |
305 | newtFormDestroy(self->form); | |
306 | newtPopWindow(); | |
307 | } | |
308 | ||
309 | ui_browser__refresh_dimensions(self); | |
310 | newtCenteredWindow(self->width + 2, self->height, title); | |
311 | self->form = newt_form__new(); | |
312 | if (self->form == NULL) | |
313 | return -1; | |
314 | ||
315 | self->sb = newtVerticalScrollbar(self->width + 1, 0, self->height, | |
316 | HE_COLORSET_NORMAL, | |
317 | HE_COLORSET_SELECTED); | |
318 | if (self->sb == NULL) | |
319 | return -1; | |
320 | ||
321 | newtFormAddHotKey(self->form, NEWT_KEY_UP); | |
322 | newtFormAddHotKey(self->form, NEWT_KEY_DOWN); | |
323 | newtFormAddHotKey(self->form, NEWT_KEY_PGUP); | |
324 | newtFormAddHotKey(self->form, NEWT_KEY_PGDN); | |
325 | newtFormAddHotKey(self->form, NEWT_KEY_HOME); | |
326 | newtFormAddHotKey(self->form, NEWT_KEY_END); | |
327 | ||
328 | if (ui_browser__refresh_entries(self) < 0) | |
329 | return -1; | |
330 | newtFormAddComponent(self->form, self->sb); | |
331 | ||
332 | while (1) { | |
333 | unsigned int offset; | |
334 | ||
335 | newtFormRun(self->form, es); | |
336 | ||
337 | if (es->reason != NEWT_EXIT_HOTKEY) | |
338 | break; | |
339 | switch (es->u.key) { | |
340 | case NEWT_KEY_DOWN: | |
341 | if (self->index == self->nr_entries - 1) | |
342 | break; | |
343 | ++self->index; | |
344 | if (self->index == self->first_visible_entry_idx + self->height) { | |
345 | struct list_head *pos = self->first_visible_entry; | |
346 | ++self->first_visible_entry_idx; | |
347 | self->first_visible_entry = pos->next; | |
348 | } | |
349 | break; | |
350 | case NEWT_KEY_UP: | |
351 | if (self->index == 0) | |
352 | break; | |
353 | --self->index; | |
354 | if (self->index < self->first_visible_entry_idx) { | |
355 | struct list_head *pos = self->first_visible_entry; | |
356 | --self->first_visible_entry_idx; | |
357 | self->first_visible_entry = pos->prev; | |
358 | } | |
359 | break; | |
360 | case NEWT_KEY_PGDN: | |
361 | if (self->first_visible_entry_idx + self->height > self->nr_entries - 1) | |
362 | break; | |
363 | ||
364 | offset = self->height; | |
365 | if (self->index + offset > self->nr_entries - 1) | |
366 | offset = self->nr_entries - 1 - self->index; | |
367 | self->index += offset; | |
368 | self->first_visible_entry_idx += offset; | |
369 | ||
370 | while (offset--) { | |
371 | struct list_head *pos = self->first_visible_entry; | |
372 | self->first_visible_entry = pos->next; | |
373 | } | |
374 | ||
375 | break; | |
376 | case NEWT_KEY_PGUP: | |
377 | if (self->first_visible_entry_idx == 0) | |
378 | break; | |
379 | ||
380 | if (self->first_visible_entry_idx < self->height) | |
381 | offset = self->first_visible_entry_idx; | |
382 | else | |
383 | offset = self->height; | |
384 | ||
385 | self->index -= offset; | |
386 | self->first_visible_entry_idx -= offset; | |
387 | ||
388 | while (offset--) { | |
389 | struct list_head *pos = self->first_visible_entry; | |
390 | self->first_visible_entry = pos->prev; | |
391 | } | |
392 | break; | |
393 | case NEWT_KEY_HOME: | |
394 | ui_browser__reset_index(self); | |
395 | break; | |
396 | case NEWT_KEY_END: { | |
397 | struct list_head *head = self->entries; | |
398 | offset = self->height - 1; | |
399 | ||
400 | if (offset > self->nr_entries) | |
401 | offset = self->nr_entries; | |
402 | ||
403 | self->index = self->first_visible_entry_idx = self->nr_entries - 1 - offset; | |
404 | self->first_visible_entry = head->prev; | |
405 | while (offset-- != 0) { | |
406 | struct list_head *pos = self->first_visible_entry; | |
407 | self->first_visible_entry = pos->prev; | |
408 | } | |
409 | } | |
410 | break; | |
411 | case NEWT_KEY_ESCAPE: | |
60553903 | 412 | case NEWT_KEY_LEFT: |
ef7b93a1 ACM |
413 | case CTRL('c'): |
414 | case 'Q': | |
415 | case 'q': | |
416 | return 0; | |
417 | default: | |
418 | continue; | |
419 | } | |
420 | if (ui_browser__refresh_entries(self) < 0) | |
421 | return -1; | |
422 | } | |
423 | return 0; | |
424 | } | |
425 | ||
4ded2b25 ACM |
426 | /* |
427 | * When debugging newt problems it was useful to be able to "unroll" | |
428 | * the calls to newtCheckBoxTreeAdd{Array,Item}, so that we can generate | |
429 | * a source file with the sequence of calls to these methods, to then | |
430 | * tweak the arrays to get the intended results, so I'm keeping this code | |
431 | * here, may be useful again in the future. | |
432 | */ | |
433 | #undef NEWT_DEBUG | |
434 | ||
435 | static void newt_checkbox_tree__add(newtComponent tree, const char *str, | |
436 | void *priv, int *indexes) | |
437 | { | |
438 | #ifdef NEWT_DEBUG | |
439 | /* Print the newtCheckboxTreeAddArray to tinker with its index arrays */ | |
440 | int i = 0, len = 40 - strlen(str); | |
441 | ||
442 | fprintf(stderr, | |
443 | "\tnewtCheckboxTreeAddItem(tree, %*.*s\"%s\", (void *)%p, 0, ", | |
444 | len, len, " ", str, priv); | |
445 | while (indexes[i] != NEWT_ARG_LAST) { | |
446 | if (indexes[i] != NEWT_ARG_APPEND) | |
447 | fprintf(stderr, " %d,", indexes[i]); | |
448 | else | |
449 | fprintf(stderr, " %s,", "NEWT_ARG_APPEND"); | |
450 | ++i; | |
451 | } | |
452 | fprintf(stderr, " %s", " NEWT_ARG_LAST);\n"); | |
453 | fflush(stderr); | |
454 | #endif | |
455 | newtCheckboxTreeAddArray(tree, str, priv, 0, indexes); | |
456 | } | |
457 | ||
458 | static char *callchain_list__sym_name(struct callchain_list *self, | |
459 | char *bf, size_t bfsize) | |
460 | { | |
b3c9ac08 ACM |
461 | if (self->ms.sym) |
462 | return self->ms.sym->name; | |
4ded2b25 ACM |
463 | |
464 | snprintf(bf, bfsize, "%#Lx", self->ip); | |
465 | return bf; | |
466 | } | |
467 | ||
468 | static void __callchain__append_graph_browser(struct callchain_node *self, | |
469 | newtComponent tree, u64 total, | |
470 | int *indexes, int depth) | |
471 | { | |
472 | struct rb_node *node; | |
473 | u64 new_total, remaining; | |
474 | int idx = 0; | |
475 | ||
476 | if (callchain_param.mode == CHAIN_GRAPH_REL) | |
477 | new_total = self->children_hit; | |
478 | else | |
479 | new_total = total; | |
480 | ||
481 | remaining = new_total; | |
482 | node = rb_first(&self->rb_root); | |
483 | while (node) { | |
484 | struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node); | |
485 | struct rb_node *next = rb_next(node); | |
486 | u64 cumul = cumul_hits(child); | |
487 | struct callchain_list *chain; | |
488 | int first = true, printed = 0; | |
489 | int chain_idx = -1; | |
490 | remaining -= cumul; | |
491 | ||
492 | indexes[depth] = NEWT_ARG_APPEND; | |
493 | indexes[depth + 1] = NEWT_ARG_LAST; | |
494 | ||
495 | list_for_each_entry(chain, &child->val, list) { | |
496 | char ipstr[BITS_PER_LONG / 4 + 1], | |
497 | *alloc_str = NULL; | |
498 | const char *str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr)); | |
499 | ||
500 | if (first) { | |
501 | double percent = cumul * 100.0 / new_total; | |
502 | ||
503 | first = false; | |
504 | if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0) | |
505 | str = "Not enough memory!"; | |
506 | else | |
507 | str = alloc_str; | |
508 | } else { | |
509 | indexes[depth] = idx; | |
510 | indexes[depth + 1] = NEWT_ARG_APPEND; | |
511 | indexes[depth + 2] = NEWT_ARG_LAST; | |
512 | ++chain_idx; | |
513 | } | |
d5679ae4 | 514 | newt_checkbox_tree__add(tree, str, &chain->ms, indexes); |
4ded2b25 ACM |
515 | free(alloc_str); |
516 | ++printed; | |
517 | } | |
518 | ||
519 | indexes[depth] = idx; | |
520 | if (chain_idx != -1) | |
521 | indexes[depth + 1] = chain_idx; | |
522 | if (printed != 0) | |
523 | ++idx; | |
524 | __callchain__append_graph_browser(child, tree, new_total, indexes, | |
525 | depth + (chain_idx != -1 ? 2 : 1)); | |
526 | node = next; | |
527 | } | |
528 | } | |
529 | ||
530 | static void callchain__append_graph_browser(struct callchain_node *self, | |
531 | newtComponent tree, u64 total, | |
532 | int *indexes, int parent_idx) | |
533 | { | |
534 | struct callchain_list *chain; | |
535 | int i = 0; | |
536 | ||
537 | indexes[1] = NEWT_ARG_APPEND; | |
538 | indexes[2] = NEWT_ARG_LAST; | |
539 | ||
540 | list_for_each_entry(chain, &self->val, list) { | |
541 | char ipstr[BITS_PER_LONG / 4 + 1], *str; | |
542 | ||
543 | if (chain->ip >= PERF_CONTEXT_MAX) | |
544 | continue; | |
545 | ||
546 | if (!i++ && sort__first_dimension == SORT_SYM) | |
547 | continue; | |
548 | ||
549 | str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr)); | |
d5679ae4 | 550 | newt_checkbox_tree__add(tree, str, &chain->ms, indexes); |
4ded2b25 ACM |
551 | } |
552 | ||
553 | indexes[1] = parent_idx; | |
554 | indexes[2] = NEWT_ARG_APPEND; | |
555 | indexes[3] = NEWT_ARG_LAST; | |
556 | __callchain__append_graph_browser(self, tree, total, indexes, 2); | |
557 | } | |
558 | ||
559 | static void hist_entry__append_callchain_browser(struct hist_entry *self, | |
560 | newtComponent tree, u64 total, int parent_idx) | |
561 | { | |
562 | struct rb_node *rb_node; | |
563 | int indexes[1024] = { [0] = parent_idx, }; | |
564 | int idx = 0; | |
565 | struct callchain_node *chain; | |
566 | ||
567 | rb_node = rb_first(&self->sorted_chain); | |
568 | while (rb_node) { | |
569 | chain = rb_entry(rb_node, struct callchain_node, rb_node); | |
570 | switch (callchain_param.mode) { | |
571 | case CHAIN_FLAT: | |
572 | break; | |
573 | case CHAIN_GRAPH_ABS: /* falldown */ | |
574 | case CHAIN_GRAPH_REL: | |
575 | callchain__append_graph_browser(chain, tree, total, indexes, idx++); | |
576 | break; | |
577 | case CHAIN_NONE: | |
578 | default: | |
579 | break; | |
580 | } | |
581 | rb_node = rb_next(rb_node); | |
582 | } | |
583 | } | |
584 | ||
f9224c5c | 585 | static size_t hist_entry__append_browser(struct hist_entry *self, |
4ded2b25 | 586 | newtComponent tree, u64 total) |
f9224c5c | 587 | { |
a4e3b956 ACM |
588 | char s[256]; |
589 | size_t ret; | |
f9224c5c ACM |
590 | |
591 | if (symbol_conf.exclude_other && !self->parent) | |
592 | return 0; | |
593 | ||
a4e3b956 ACM |
594 | ret = hist_entry__snprintf(self, s, sizeof(s), NULL, |
595 | false, 0, false, total); | |
4ded2b25 ACM |
596 | if (symbol_conf.use_callchain) { |
597 | int indexes[2]; | |
598 | ||
599 | indexes[0] = NEWT_ARG_APPEND; | |
600 | indexes[1] = NEWT_ARG_LAST; | |
d5679ae4 | 601 | newt_checkbox_tree__add(tree, s, &self->ms, indexes); |
4ded2b25 | 602 | } else |
d5679ae4 | 603 | newtListboxAppendEntry(tree, s, &self->ms); |
4ded2b25 | 604 | |
a4e3b956 | 605 | return ret; |
f9224c5c ACM |
606 | } |
607 | ||
ef7b93a1 | 608 | static void hist_entry__annotate_browser(struct hist_entry *self) |
f9224c5c | 609 | { |
ef7b93a1 | 610 | struct ui_browser browser; |
f9224c5c | 611 | struct newtExitStruct es; |
ef7b93a1 ACM |
612 | struct objdump_line *pos, *n; |
613 | LIST_HEAD(head); | |
f9224c5c | 614 | |
ef7b93a1 | 615 | if (self->ms.sym == NULL) |
f9224c5c ACM |
616 | return; |
617 | ||
ef7b93a1 | 618 | if (hist_entry__annotate(self, &head) < 0) |
f9224c5c ACM |
619 | return; |
620 | ||
60553903 | 621 | ui_helpline__push("Press <- or ESC to exit"); |
f9224c5c | 622 | |
ef7b93a1 ACM |
623 | memset(&browser, 0, sizeof(browser)); |
624 | browser.entries = &head; | |
625 | browser.priv = self; | |
626 | list_for_each_entry(pos, &head, node) { | |
627 | size_t line_len = strlen(pos->line); | |
628 | if (browser.width < line_len) | |
629 | browser.width = line_len; | |
630 | ++browser.nr_entries; | |
f9224c5c | 631 | } |
f9224c5c | 632 | |
ef7b93a1 ACM |
633 | browser.width += 18; /* Percentage */ |
634 | ui_browser__run(&browser, self->ms.sym->name, &es); | |
635 | newtFormDestroy(browser.form); | |
f9224c5c | 636 | newtPopWindow(); |
ef7b93a1 ACM |
637 | list_for_each_entry_safe(pos, n, &head, node) { |
638 | list_del(&pos->node); | |
639 | objdump_line__free(pos); | |
640 | } | |
3798ed7b | 641 | ui_helpline__pop(); |
f9224c5c ACM |
642 | } |
643 | ||
53c54019 ACM |
644 | static const void *newt__symbol_tree_get_current(newtComponent self) |
645 | { | |
646 | if (symbol_conf.use_callchain) | |
647 | return newtCheckboxTreeGetCurrent(self); | |
648 | return newtListboxGetCurrent(self); | |
649 | } | |
650 | ||
e65713ea | 651 | static void hist_browser__selection(newtComponent self, void *data) |
53c54019 | 652 | { |
d5679ae4 | 653 | const struct map_symbol **symbol_ptr = data; |
53c54019 ACM |
654 | *symbol_ptr = newt__symbol_tree_get_current(self); |
655 | } | |
656 | ||
e65713ea ACM |
657 | struct hist_browser { |
658 | newtComponent form, tree; | |
659 | const struct map_symbol *selection; | |
660 | }; | |
661 | ||
662 | static struct hist_browser *hist_browser__new(void) | |
663 | { | |
664 | struct hist_browser *self = malloc(sizeof(*self)); | |
665 | ||
83753190 ACM |
666 | if (self != NULL) |
667 | self->form = NULL; | |
e65713ea ACM |
668 | |
669 | return self; | |
670 | } | |
671 | ||
672 | static void hist_browser__delete(struct hist_browser *self) | |
673 | { | |
674 | newtFormDestroy(self->form); | |
675 | newtPopWindow(); | |
676 | free(self); | |
677 | } | |
678 | ||
b09e0190 ACM |
679 | static int hist_browser__populate(struct hist_browser *self, struct hists *hists, |
680 | const char *title) | |
f9224c5c | 681 | { |
e65713ea ACM |
682 | int max_len = 0, idx, cols, rows; |
683 | struct ui_progress *progress; | |
f9224c5c | 684 | struct rb_node *nd; |
5f4d3f88 | 685 | u64 curr_hist = 0; |
c82ee828 | 686 | char seq[] = ".", unit; |
83753190 | 687 | char str[256]; |
c82ee828 | 688 | unsigned long nr_events = hists->stats.nr_events[PERF_RECORD_SAMPLE]; |
83753190 ACM |
689 | |
690 | if (self->form) { | |
691 | newtFormDestroy(self->form); | |
692 | newtPopWindow(); | |
693 | } | |
694 | ||
c82ee828 ACM |
695 | nr_events = convert_unit(nr_events, &unit); |
696 | snprintf(str, sizeof(str), "Events: %lu%c ", | |
697 | nr_events, unit); | |
83753190 ACM |
698 | newtDrawRootText(0, 0, str); |
699 | ||
700 | newtGetScreenSize(NULL, &rows); | |
701 | ||
702 | if (symbol_conf.use_callchain) | |
703 | self->tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq, | |
704 | NEWT_FLAG_SCROLL); | |
705 | else | |
706 | self->tree = newtListbox(0, 0, rows - 5, | |
707 | (NEWT_FLAG_SCROLL | | |
708 | NEWT_FLAG_RETURNEXIT)); | |
709 | ||
710 | newtComponentAddCallback(self->tree, hist_browser__selection, | |
711 | &self->selection); | |
5f4d3f88 | 712 | |
b09e0190 ACM |
713 | progress = ui_progress__new("Adding entries to the browser...", |
714 | hists->nr_entries); | |
5f4d3f88 ACM |
715 | if (progress == NULL) |
716 | return -1; | |
f9224c5c | 717 | |
4ded2b25 | 718 | idx = 0; |
b09e0190 | 719 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
f9224c5c | 720 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
83753190 ACM |
721 | int len; |
722 | ||
723 | if (h->filtered) | |
724 | continue; | |
725 | ||
cee75ac7 | 726 | len = hist_entry__append_browser(h, self->tree, hists->stats.total_period); |
f9224c5c ACM |
727 | if (len > max_len) |
728 | max_len = len; | |
d5679ae4 | 729 | if (symbol_conf.use_callchain) |
e65713ea | 730 | hist_entry__append_callchain_browser(h, self->tree, |
cee75ac7 | 731 | hists->stats.total_period, idx++); |
5f4d3f88 ACM |
732 | ++curr_hist; |
733 | if (curr_hist % 5) | |
734 | ui_progress__update(progress, curr_hist); | |
f9224c5c ACM |
735 | } |
736 | ||
5f4d3f88 ACM |
737 | ui_progress__delete(progress); |
738 | ||
e65713ea ACM |
739 | newtGetScreenSize(&cols, &rows); |
740 | ||
4ded2b25 ACM |
741 | if (max_len > cols) |
742 | max_len = cols - 3; | |
743 | ||
744 | if (!symbol_conf.use_callchain) | |
e65713ea | 745 | newtListboxSetWidth(self->tree, max_len); |
4ded2b25 ACM |
746 | |
747 | newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0), | |
6e7ab4c6 | 748 | rows - 5, title); |
e65713ea | 749 | self->form = newt_form__new(); |
83753190 ACM |
750 | if (self->form == NULL) |
751 | return -1; | |
752 | ||
e65713ea ACM |
753 | newtFormAddHotKey(self->form, 'A'); |
754 | newtFormAddHotKey(self->form, 'a'); | |
9d192e11 ACM |
755 | newtFormAddHotKey(self->form, 'D'); |
756 | newtFormAddHotKey(self->form, 'd'); | |
757 | newtFormAddHotKey(self->form, 'T'); | |
758 | newtFormAddHotKey(self->form, 't'); | |
e65713ea ACM |
759 | newtFormAddHotKey(self->form, NEWT_KEY_RIGHT); |
760 | newtFormAddComponents(self->form, self->tree, NULL); | |
761 | self->selection = newt__symbol_tree_get_current(self->tree); | |
762 | ||
763 | return 0; | |
764 | } | |
765 | ||
ef7b93a1 | 766 | static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self) |
a5e29aca ACM |
767 | { |
768 | int *indexes; | |
769 | ||
770 | if (!symbol_conf.use_callchain) | |
771 | goto out; | |
772 | ||
773 | indexes = newtCheckboxTreeFindItem(self->tree, (void *)self->selection); | |
774 | if (indexes) { | |
775 | bool is_hist_entry = indexes[1] == NEWT_ARG_LAST; | |
776 | free(indexes); | |
777 | if (is_hist_entry) | |
778 | goto out; | |
779 | } | |
780 | return NULL; | |
781 | out: | |
ef7b93a1 ACM |
782 | return container_of(self->selection, struct hist_entry, ms); |
783 | } | |
784 | ||
785 | static struct thread *hist_browser__selected_thread(struct hist_browser *self) | |
786 | { | |
787 | struct hist_entry *he = hist_browser__selected_entry(self); | |
788 | return he ? he->thread : NULL; | |
a5e29aca ACM |
789 | } |
790 | ||
6e7ab4c6 ACM |
791 | static int hist_browser__title(char *bf, size_t size, const char *input_name, |
792 | const struct dso *dso, const struct thread *thread) | |
793 | { | |
794 | int printed = 0; | |
795 | ||
796 | if (thread) | |
797 | printed += snprintf(bf + printed, size - printed, | |
798 | "Thread: %s(%d)", | |
799 | (thread->comm_set ? thread->comm : ""), | |
800 | thread->pid); | |
801 | if (dso) | |
802 | printed += snprintf(bf + printed, size - printed, | |
803 | "%sDSO: %s", thread ? " " : "", | |
804 | dso->short_name); | |
805 | return printed ?: snprintf(bf, size, "Report: %s", input_name); | |
806 | } | |
807 | ||
b09e0190 | 808 | int hists__browse(struct hists *self, const char *helpline, const char *input_name) |
e65713ea | 809 | { |
6e7ab4c6 | 810 | struct hist_browser *browser = hist_browser__new(); |
3e1bbdc3 | 811 | struct pstack *fstack = pstack__new(2); |
6e7ab4c6 ACM |
812 | const struct thread *thread_filter = NULL; |
813 | const struct dso *dso_filter = NULL; | |
e65713ea | 814 | struct newtExitStruct es; |
6e7ab4c6 | 815 | char msg[160]; |
e65713ea | 816 | int err = -1; |
e65713ea ACM |
817 | |
818 | if (browser == NULL) | |
819 | return -1; | |
820 | ||
3e1bbdc3 ACM |
821 | fstack = pstack__new(2); |
822 | if (fstack == NULL) | |
823 | goto out; | |
824 | ||
3798ed7b | 825 | ui_helpline__push(helpline); |
e65713ea | 826 | |
6e7ab4c6 ACM |
827 | hist_browser__title(msg, sizeof(msg), input_name, |
828 | dso_filter, thread_filter); | |
b09e0190 | 829 | if (hist_browser__populate(browser, self, msg) < 0) |
3e1bbdc3 | 830 | goto out_free_stack; |
f9224c5c ACM |
831 | |
832 | while (1) { | |
a5e29aca | 833 | const struct thread *thread; |
6e7ab4c6 | 834 | const struct dso *dso; |
83753190 ACM |
835 | char *options[16]; |
836 | int nr_options = 0, choice = 0, i, | |
a5e29aca | 837 | annotate = -2, zoom_dso = -2, zoom_thread = -2; |
f9224c5c | 838 | |
e65713ea | 839 | newtFormRun(browser->form, &es); |
9d192e11 ACM |
840 | |
841 | thread = hist_browser__selected_thread(browser); | |
842 | dso = browser->selection->map ? browser->selection->map->dso : NULL; | |
843 | ||
53c54019 | 844 | if (es.reason == NEWT_EXIT_HOTKEY) { |
9d192e11 ACM |
845 | switch (toupper(es.u.key)) { |
846 | case 'A': | |
d5679ae4 | 847 | goto do_annotate; |
9d192e11 ACM |
848 | case 'D': |
849 | goto zoom_dso; | |
850 | case 'T': | |
851 | goto zoom_thread; | |
852 | default:; | |
853 | } | |
29351db6 ACM |
854 | if (toupper(es.u.key) == 'Q' || |
855 | es.u.key == CTRL('c')) | |
856 | break; | |
857 | if (es.u.key == NEWT_KEY_ESCAPE) { | |
53c54019 ACM |
858 | if (dialog_yesno("Do you really want to exit?")) |
859 | break; | |
860 | else | |
861 | continue; | |
862 | } | |
3e1bbdc3 ACM |
863 | |
864 | if (es.u.key == NEWT_KEY_LEFT) { | |
865 | const void *top; | |
866 | ||
867 | if (pstack__empty(fstack)) | |
868 | continue; | |
869 | top = pstack__pop(fstack); | |
870 | if (top == &dso_filter) | |
871 | goto zoom_out_dso; | |
872 | if (top == &thread_filter) | |
873 | goto zoom_out_thread; | |
874 | continue; | |
875 | } | |
53c54019 ACM |
876 | } |
877 | ||
83753190 ACM |
878 | if (browser->selection->sym != NULL && |
879 | asprintf(&options[nr_options], "Annotate %s", | |
880 | browser->selection->sym->name) > 0) | |
881 | annotate = nr_options++; | |
882 | ||
a5e29aca ACM |
883 | if (thread != NULL && |
884 | asprintf(&options[nr_options], "Zoom %s %s(%d) thread", | |
6e7ab4c6 ACM |
885 | (thread_filter ? "out of" : "into"), |
886 | (thread->comm_set ? thread->comm : ""), | |
887 | thread->pid) > 0) | |
a5e29aca ACM |
888 | zoom_thread = nr_options++; |
889 | ||
6e7ab4c6 ACM |
890 | if (dso != NULL && |
891 | asprintf(&options[nr_options], "Zoom %s %s DSO", | |
892 | (dso_filter ? "out of" : "into"), | |
893 | (dso->kernel ? "the Kernel" : dso->short_name)) > 0) | |
894 | zoom_dso = nr_options++; | |
895 | ||
83753190 | 896 | options[nr_options++] = (char *)"Exit"; |
53c54019 | 897 | |
53c54019 | 898 | choice = popup_menu(nr_options, options); |
83753190 ACM |
899 | |
900 | for (i = 0; i < nr_options - 1; ++i) | |
901 | free(options[i]); | |
902 | ||
53c54019 | 903 | if (choice == nr_options - 1) |
f9224c5c | 904 | break; |
a5e29aca ACM |
905 | |
906 | if (choice == -1) | |
907 | continue; | |
c1ec5fef | 908 | |
83753190 | 909 | if (choice == annotate) { |
ef7b93a1 | 910 | struct hist_entry *he; |
c1ec5fef | 911 | do_annotate: |
e65713ea | 912 | if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) { |
3798ed7b | 913 | ui_helpline__puts("No vmlinux file found, can't " |
d5679ae4 ACM |
914 | "annotate with just a " |
915 | "kallsyms file"); | |
916 | continue; | |
917 | } | |
ef7b93a1 ACM |
918 | |
919 | he = hist_browser__selected_entry(browser); | |
920 | if (he == NULL) | |
921 | continue; | |
922 | ||
923 | hist_entry__annotate_browser(he); | |
a5e29aca | 924 | } else if (choice == zoom_dso) { |
9d192e11 | 925 | zoom_dso: |
6e7ab4c6 | 926 | if (dso_filter) { |
3e1bbdc3 ACM |
927 | pstack__remove(fstack, &dso_filter); |
928 | zoom_out_dso: | |
3798ed7b | 929 | ui_helpline__pop(); |
6e7ab4c6 ACM |
930 | dso_filter = NULL; |
931 | } else { | |
9d192e11 ACM |
932 | if (dso == NULL) |
933 | continue; | |
3e1bbdc3 | 934 | ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"", |
3798ed7b | 935 | dso->kernel ? "the Kernel" : dso->short_name); |
6e7ab4c6 | 936 | dso_filter = dso; |
3e1bbdc3 | 937 | pstack__push(fstack, &dso_filter); |
6e7ab4c6 | 938 | } |
b09e0190 | 939 | hists__filter_by_dso(self, dso_filter); |
6e7ab4c6 ACM |
940 | hist_browser__title(msg, sizeof(msg), input_name, |
941 | dso_filter, thread_filter); | |
b09e0190 | 942 | if (hist_browser__populate(browser, self, msg) < 0) |
83753190 | 943 | goto out; |
a5e29aca | 944 | } else if (choice == zoom_thread) { |
9d192e11 | 945 | zoom_thread: |
6e7ab4c6 | 946 | if (thread_filter) { |
3e1bbdc3 ACM |
947 | pstack__remove(fstack, &thread_filter); |
948 | zoom_out_thread: | |
3798ed7b | 949 | ui_helpline__pop(); |
6e7ab4c6 ACM |
950 | thread_filter = NULL; |
951 | } else { | |
3e1bbdc3 | 952 | ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"", |
3798ed7b ACM |
953 | thread->comm_set ? thread->comm : "", |
954 | thread->pid); | |
6e7ab4c6 | 955 | thread_filter = thread; |
3e1bbdc3 | 956 | pstack__push(fstack, &thread_filter); |
6e7ab4c6 | 957 | } |
b09e0190 | 958 | hists__filter_by_thread(self, thread_filter); |
6e7ab4c6 ACM |
959 | hist_browser__title(msg, sizeof(msg), input_name, |
960 | dso_filter, thread_filter); | |
b09e0190 | 961 | if (hist_browser__populate(browser, self, msg) < 0) |
a5e29aca | 962 | goto out; |
d5679ae4 | 963 | } |
f9224c5c | 964 | } |
e65713ea | 965 | err = 0; |
3e1bbdc3 ACM |
966 | out_free_stack: |
967 | pstack__delete(fstack); | |
e65713ea ACM |
968 | out: |
969 | hist_browser__delete(browser); | |
970 | return err; | |
f9224c5c ACM |
971 | } |
972 | ||
ef7b93a1 ACM |
973 | static struct newtPercentTreeColors { |
974 | const char *topColorFg, *topColorBg; | |
975 | const char *mediumColorFg, *mediumColorBg; | |
976 | const char *normalColorFg, *normalColorBg; | |
977 | const char *selColorFg, *selColorBg; | |
978 | const char *codeColorFg, *codeColorBg; | |
979 | } defaultPercentTreeColors = { | |
980 | "red", "lightgray", | |
981 | "green", "lightgray", | |
982 | "black", "lightgray", | |
983 | "lightgray", "magenta", | |
984 | "blue", "lightgray", | |
985 | }; | |
986 | ||
f9224c5c ACM |
987 | void setup_browser(void) |
988 | { | |
ef7b93a1 | 989 | struct newtPercentTreeColors *c = &defaultPercentTreeColors; |
f9224c5c ACM |
990 | if (!isatty(1)) |
991 | return; | |
992 | ||
993 | use_browser = true; | |
994 | newtInit(); | |
995 | newtCls(); | |
3798ed7b | 996 | ui_helpline__puts(" "); |
ef7b93a1 ACM |
997 | SLtt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg); |
998 | SLtt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg); | |
999 | SLtt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg); | |
1000 | SLtt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg); | |
1001 | SLtt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg); | |
f9224c5c ACM |
1002 | } |
1003 | ||
f3a1f0ea | 1004 | void exit_browser(bool wait_for_ok) |
f9224c5c | 1005 | { |
f3a1f0ea ACM |
1006 | if (use_browser) { |
1007 | if (wait_for_ok) { | |
1008 | char title[] = "Fatal Error", ok[] = "Ok"; | |
1009 | newtWinMessage(title, ok, browser__last_msg); | |
1010 | } | |
f9224c5c | 1011 | newtFinished(); |
f3a1f0ea | 1012 | } |
f9224c5c | 1013 | } |