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