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