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); |
Arnaldo Carvalho de Melo | 6055390 | 2010-05-15 20:40:34 -0300 | [diff] [blame] | 326 | newtFormAddHotKey(self->form, NEWT_KEY_LEFT); |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 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: |
Arnaldo Carvalho de Melo | 6055390 | 2010-05-15 20:40:34 -0300 | [diff] [blame] | 412 | case NEWT_KEY_LEFT: |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | 4ded2b2 | 2010-03-22 17:52:49 -0300 | [diff] [blame] | 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) |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 437 | { |
Arnaldo Carvalho de Melo | 4ded2b2 | 2010-03-22 17:52:49 -0300 | [diff] [blame] | 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 | { |
Arnaldo Carvalho de Melo | b3c9ac0 | 2010-03-24 16:40:18 -0300 | [diff] [blame] | 461 | if (self->ms.sym) |
| 462 | return self->ms.sym->name; |
Arnaldo Carvalho de Melo | 4ded2b2 | 2010-03-22 17:52:49 -0300 | [diff] [blame] | 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 | } |
Arnaldo Carvalho de Melo | d5679ae | 2010-03-24 16:40:19 -0300 | [diff] [blame] | 514 | newt_checkbox_tree__add(tree, str, &chain->ms, indexes); |
Arnaldo Carvalho de Melo | 4ded2b2 | 2010-03-22 17:52:49 -0300 | [diff] [blame] | 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)); |
Arnaldo Carvalho de Melo | d5679ae | 2010-03-24 16:40:19 -0300 | [diff] [blame] | 550 | newt_checkbox_tree__add(tree, str, &chain->ms, indexes); |
Arnaldo Carvalho de Melo | 4ded2b2 | 2010-03-22 17:52:49 -0300 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | 4ded2b2 | 2010-03-22 17:52:49 -0300 | [diff] [blame] | 585 | static size_t hist_entry__append_browser(struct hist_entry *self, |
| 586 | newtComponent tree, u64 total) |
| 587 | { |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 588 | char s[256]; |
| 589 | size_t ret; |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 590 | |
| 591 | if (symbol_conf.exclude_other && !self->parent) |
| 592 | return 0; |
| 593 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 594 | ret = hist_entry__snprintf(self, s, sizeof(s), NULL, |
| 595 | false, 0, false, total); |
Arnaldo Carvalho de Melo | 4ded2b2 | 2010-03-22 17:52:49 -0300 | [diff] [blame] | 596 | if (symbol_conf.use_callchain) { |
| 597 | int indexes[2]; |
| 598 | |
| 599 | indexes[0] = NEWT_ARG_APPEND; |
| 600 | indexes[1] = NEWT_ARG_LAST; |
Arnaldo Carvalho de Melo | d5679ae | 2010-03-24 16:40:19 -0300 | [diff] [blame] | 601 | newt_checkbox_tree__add(tree, s, &self->ms, indexes); |
Arnaldo Carvalho de Melo | 4ded2b2 | 2010-03-22 17:52:49 -0300 | [diff] [blame] | 602 | } else |
Arnaldo Carvalho de Melo | d5679ae | 2010-03-24 16:40:19 -0300 | [diff] [blame] | 603 | newtListboxAppendEntry(tree, s, &self->ms); |
Arnaldo Carvalho de Melo | 4ded2b2 | 2010-03-22 17:52:49 -0300 | [diff] [blame] | 604 | |
Arnaldo Carvalho de Melo | a4e3b95 | 2010-03-31 11:33:40 -0300 | [diff] [blame] | 605 | return ret; |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 606 | } |
| 607 | |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 608 | static void hist_entry__annotate_browser(struct hist_entry *self) |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 609 | { |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 610 | struct ui_browser browser; |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 611 | struct newtExitStruct es; |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 612 | struct objdump_line *pos, *n; |
| 613 | LIST_HEAD(head); |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 614 | |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 615 | if (self->ms.sym == NULL) |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 616 | return; |
| 617 | |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 618 | if (hist_entry__annotate(self, &head) < 0) |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 619 | return; |
| 620 | |
Arnaldo Carvalho de Melo | 6055390 | 2010-05-15 20:40:34 -0300 | [diff] [blame] | 621 | ui_helpline__push("Press <- or ESC to exit"); |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 622 | |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 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; |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 631 | } |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 632 | |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 633 | browser.width += 18; /* Percentage */ |
| 634 | ui_browser__run(&browser, self->ms.sym->name, &es); |
| 635 | newtFormDestroy(browser.form); |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 636 | newtPopWindow(); |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 637 | list_for_each_entry_safe(pos, n, &head, node) { |
| 638 | list_del(&pos->node); |
| 639 | objdump_line__free(pos); |
| 640 | } |
Arnaldo Carvalho de Melo | 3798ed7 | 2010-05-11 18:01:23 -0300 | [diff] [blame] | 641 | ui_helpline__pop(); |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 642 | } |
| 643 | |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 651 | static void hist_browser__selection(newtComponent self, void *data) |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 652 | { |
Arnaldo Carvalho de Melo | d5679ae | 2010-03-24 16:40:19 -0300 | [diff] [blame] | 653 | const struct map_symbol **symbol_ptr = data; |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 654 | *symbol_ptr = newt__symbol_tree_get_current(self); |
| 655 | } |
| 656 | |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 657 | struct hist_browser { |
| 658 | newtComponent form, tree; |
Arnaldo Carvalho de Melo | d5679ae | 2010-03-24 16:40:19 -0300 | [diff] [blame] | 659 | const struct map_symbol *selection; |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 660 | }; |
| 661 | |
| 662 | static struct hist_browser *hist_browser__new(void) |
| 663 | { |
| 664 | struct hist_browser *self = malloc(sizeof(*self)); |
| 665 | |
Arnaldo Carvalho de Melo | 8375319 | 2010-04-03 16:30:44 -0300 | [diff] [blame] | 666 | if (self != NULL) |
| 667 | self->form = NULL; |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 679 | static int hist_browser__populate(struct hist_browser *self, struct hists *hists, |
| 680 | const char *title) |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 681 | { |
| 682 | int max_len = 0, idx, cols, rows; |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 683 | struct ui_progress *progress; |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 684 | struct rb_node *nd; |
| 685 | u64 curr_hist = 0; |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 686 | char seq[] = ".", unit; |
Arnaldo Carvalho de Melo | 8375319 | 2010-04-03 16:30:44 -0300 | [diff] [blame] | 687 | char str[256]; |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 688 | 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] | 689 | |
| 690 | if (self->form) { |
| 691 | newtFormDestroy(self->form); |
| 692 | newtPopWindow(); |
| 693 | } |
| 694 | |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 695 | nr_events = convert_unit(nr_events, &unit); |
| 696 | snprintf(str, sizeof(str), "Events: %lu%c ", |
| 697 | nr_events, unit); |
Arnaldo Carvalho de Melo | 8375319 | 2010-04-03 16:30:44 -0300 | [diff] [blame] | 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); |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 712 | |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 713 | progress = ui_progress__new("Adding entries to the browser...", |
| 714 | hists->nr_entries); |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 715 | if (progress == NULL) |
| 716 | return -1; |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 717 | |
Arnaldo Carvalho de Melo | 4ded2b2 | 2010-03-22 17:52:49 -0300 | [diff] [blame] | 718 | idx = 0; |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 719 | 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] | 720 | 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] | 721 | int len; |
| 722 | |
| 723 | if (h->filtered) |
| 724 | continue; |
| 725 | |
Arnaldo Carvalho de Melo | cee75ac | 2010-05-14 13:16:55 -0300 | [diff] [blame] | 726 | 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] | 727 | if (len > max_len) |
| 728 | max_len = len; |
Arnaldo Carvalho de Melo | d5679ae | 2010-03-24 16:40:19 -0300 | [diff] [blame] | 729 | if (symbol_conf.use_callchain) |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 730 | hist_entry__append_callchain_browser(h, self->tree, |
Arnaldo Carvalho de Melo | cee75ac | 2010-05-14 13:16:55 -0300 | [diff] [blame] | 731 | hists->stats.total_period, idx++); |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 732 | ++curr_hist; |
| 733 | if (curr_hist % 5) |
| 734 | ui_progress__update(progress, curr_hist); |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 735 | } |
| 736 | |
Arnaldo Carvalho de Melo | 5f4d3f8 | 2010-03-26 21:16:22 -0300 | [diff] [blame] | 737 | ui_progress__delete(progress); |
| 738 | |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 739 | newtGetScreenSize(&cols, &rows); |
| 740 | |
Arnaldo Carvalho de Melo | 4ded2b2 | 2010-03-22 17:52:49 -0300 | [diff] [blame] | 741 | if (max_len > cols) |
| 742 | max_len = cols - 3; |
| 743 | |
| 744 | if (!symbol_conf.use_callchain) |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 745 | newtListboxSetWidth(self->tree, max_len); |
Arnaldo Carvalho de Melo | 4ded2b2 | 2010-03-22 17:52:49 -0300 | [diff] [blame] | 746 | |
| 747 | newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0), |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 748 | rows - 5, title); |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 749 | self->form = newt_form__new(); |
Arnaldo Carvalho de Melo | 8375319 | 2010-04-03 16:30:44 -0300 | [diff] [blame] | 750 | if (self->form == NULL) |
| 751 | return -1; |
| 752 | |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 753 | newtFormAddHotKey(self->form, 'A'); |
| 754 | newtFormAddHotKey(self->form, 'a'); |
| 755 | newtFormAddHotKey(self->form, NEWT_KEY_RIGHT); |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 756 | newtFormAddHotKey(self->form, NEWT_KEY_LEFT); |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 757 | newtFormAddComponents(self->form, self->tree, NULL); |
| 758 | self->selection = newt__symbol_tree_get_current(self->tree); |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 763 | 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] | 764 | { |
| 765 | int *indexes; |
| 766 | |
| 767 | if (!symbol_conf.use_callchain) |
| 768 | goto out; |
| 769 | |
| 770 | indexes = newtCheckboxTreeFindItem(self->tree, (void *)self->selection); |
| 771 | if (indexes) { |
| 772 | bool is_hist_entry = indexes[1] == NEWT_ARG_LAST; |
| 773 | free(indexes); |
| 774 | if (is_hist_entry) |
| 775 | goto out; |
| 776 | } |
| 777 | return NULL; |
| 778 | out: |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 779 | return container_of(self->selection, struct hist_entry, ms); |
| 780 | } |
| 781 | |
| 782 | static struct thread *hist_browser__selected_thread(struct hist_browser *self) |
| 783 | { |
| 784 | struct hist_entry *he = hist_browser__selected_entry(self); |
| 785 | return he ? he->thread : NULL; |
Arnaldo Carvalho de Melo | a5e29ac | 2010-04-03 22:44:37 -0300 | [diff] [blame] | 786 | } |
| 787 | |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 788 | static int hist_browser__title(char *bf, size_t size, const char *input_name, |
| 789 | const struct dso *dso, const struct thread *thread) |
| 790 | { |
| 791 | int printed = 0; |
| 792 | |
| 793 | if (thread) |
| 794 | printed += snprintf(bf + printed, size - printed, |
| 795 | "Thread: %s(%d)", |
| 796 | (thread->comm_set ? thread->comm : ""), |
| 797 | thread->pid); |
| 798 | if (dso) |
| 799 | printed += snprintf(bf + printed, size - printed, |
| 800 | "%sDSO: %s", thread ? " " : "", |
| 801 | dso->short_name); |
| 802 | return printed ?: snprintf(bf, size, "Report: %s", input_name); |
| 803 | } |
| 804 | |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 805 | 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] | 806 | { |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 807 | struct hist_browser *browser = hist_browser__new(); |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 808 | struct pstack *fstack = pstack__new(2); |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 809 | const struct thread *thread_filter = NULL; |
| 810 | const struct dso *dso_filter = NULL; |
| 811 | struct newtExitStruct es; |
| 812 | char msg[160]; |
| 813 | int err = -1; |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 814 | |
| 815 | if (browser == NULL) |
| 816 | return -1; |
| 817 | |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 818 | fstack = pstack__new(2); |
| 819 | if (fstack == NULL) |
| 820 | goto out; |
| 821 | |
Arnaldo Carvalho de Melo | 3798ed7 | 2010-05-11 18:01:23 -0300 | [diff] [blame] | 822 | ui_helpline__push(helpline); |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 823 | |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 824 | hist_browser__title(msg, sizeof(msg), input_name, |
| 825 | dso_filter, thread_filter); |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 826 | if (hist_browser__populate(browser, self, msg) < 0) |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 827 | goto out_free_stack; |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 828 | |
| 829 | while (1) { |
Arnaldo Carvalho de Melo | a5e29ac | 2010-04-03 22:44:37 -0300 | [diff] [blame] | 830 | const struct thread *thread; |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 831 | const struct dso *dso; |
Arnaldo Carvalho de Melo | 8375319 | 2010-04-03 16:30:44 -0300 | [diff] [blame] | 832 | char *options[16]; |
| 833 | int nr_options = 0, choice = 0, i, |
Arnaldo Carvalho de Melo | a5e29ac | 2010-04-03 22:44:37 -0300 | [diff] [blame] | 834 | annotate = -2, zoom_dso = -2, zoom_thread = -2; |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 835 | |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 836 | newtFormRun(browser->form, &es); |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 837 | if (es.reason == NEWT_EXIT_HOTKEY) { |
Arnaldo Carvalho de Melo | d5679ae | 2010-03-24 16:40:19 -0300 | [diff] [blame] | 838 | if (toupper(es.u.key) == 'A') |
| 839 | goto do_annotate; |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 840 | if (es.u.key == NEWT_KEY_ESCAPE || |
| 841 | toupper(es.u.key) == 'Q' || |
| 842 | es.u.key == CTRL('c')) { |
| 843 | if (dialog_yesno("Do you really want to exit?")) |
| 844 | break; |
| 845 | else |
| 846 | continue; |
| 847 | } |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 848 | |
| 849 | if (es.u.key == NEWT_KEY_LEFT) { |
| 850 | const void *top; |
| 851 | |
| 852 | if (pstack__empty(fstack)) |
| 853 | continue; |
| 854 | top = pstack__pop(fstack); |
| 855 | if (top == &dso_filter) |
| 856 | goto zoom_out_dso; |
| 857 | if (top == &thread_filter) |
| 858 | goto zoom_out_thread; |
| 859 | continue; |
| 860 | } |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 861 | } |
| 862 | |
Arnaldo Carvalho de Melo | 8375319 | 2010-04-03 16:30:44 -0300 | [diff] [blame] | 863 | if (browser->selection->sym != NULL && |
| 864 | asprintf(&options[nr_options], "Annotate %s", |
| 865 | browser->selection->sym->name) > 0) |
| 866 | annotate = nr_options++; |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 867 | |
Arnaldo Carvalho de Melo | a5e29ac | 2010-04-03 22:44:37 -0300 | [diff] [blame] | 868 | thread = hist_browser__selected_thread(browser); |
| 869 | if (thread != NULL && |
| 870 | asprintf(&options[nr_options], "Zoom %s %s(%d) thread", |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 871 | (thread_filter ? "out of" : "into"), |
| 872 | (thread->comm_set ? thread->comm : ""), |
| 873 | thread->pid) > 0) |
Arnaldo Carvalho de Melo | a5e29ac | 2010-04-03 22:44:37 -0300 | [diff] [blame] | 874 | zoom_thread = nr_options++; |
| 875 | |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 876 | dso = browser->selection->map ? browser->selection->map->dso : NULL; |
| 877 | if (dso != NULL && |
| 878 | asprintf(&options[nr_options], "Zoom %s %s DSO", |
| 879 | (dso_filter ? "out of" : "into"), |
| 880 | (dso->kernel ? "the Kernel" : dso->short_name)) > 0) |
| 881 | zoom_dso = nr_options++; |
| 882 | |
Arnaldo Carvalho de Melo | 8375319 | 2010-04-03 16:30:44 -0300 | [diff] [blame] | 883 | options[nr_options++] = (char *)"Exit"; |
| 884 | |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 885 | choice = popup_menu(nr_options, options); |
Arnaldo Carvalho de Melo | 8375319 | 2010-04-03 16:30:44 -0300 | [diff] [blame] | 886 | |
| 887 | for (i = 0; i < nr_options - 1; ++i) |
| 888 | free(options[i]); |
| 889 | |
Arnaldo Carvalho de Melo | 53c5401 | 2010-03-24 16:40:14 -0300 | [diff] [blame] | 890 | if (choice == nr_options - 1) |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 891 | break; |
Arnaldo Carvalho de Melo | a5e29ac | 2010-04-03 22:44:37 -0300 | [diff] [blame] | 892 | |
| 893 | if (choice == -1) |
| 894 | continue; |
Arnaldo Carvalho de Melo | c1ec5fe | 2010-05-15 20:45:31 -0300 | [diff] [blame^] | 895 | |
Arnaldo Carvalho de Melo | 8375319 | 2010-04-03 16:30:44 -0300 | [diff] [blame] | 896 | if (choice == annotate) { |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 897 | struct hist_entry *he; |
Arnaldo Carvalho de Melo | c1ec5fe | 2010-05-15 20:45:31 -0300 | [diff] [blame^] | 898 | do_annotate: |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 899 | if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) { |
Arnaldo Carvalho de Melo | 3798ed7 | 2010-05-11 18:01:23 -0300 | [diff] [blame] | 900 | ui_helpline__puts("No vmlinux file found, can't " |
Arnaldo Carvalho de Melo | d5679ae | 2010-03-24 16:40:19 -0300 | [diff] [blame] | 901 | "annotate with just a " |
| 902 | "kallsyms file"); |
| 903 | continue; |
| 904 | } |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 905 | |
| 906 | he = hist_browser__selected_entry(browser); |
| 907 | if (he == NULL) |
| 908 | continue; |
| 909 | |
| 910 | hist_entry__annotate_browser(he); |
Arnaldo Carvalho de Melo | a5e29ac | 2010-04-03 22:44:37 -0300 | [diff] [blame] | 911 | } else if (choice == zoom_dso) { |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 912 | if (dso_filter) { |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 913 | pstack__remove(fstack, &dso_filter); |
| 914 | zoom_out_dso: |
Arnaldo Carvalho de Melo | 3798ed7 | 2010-05-11 18:01:23 -0300 | [diff] [blame] | 915 | ui_helpline__pop(); |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 916 | dso_filter = NULL; |
| 917 | } else { |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 918 | 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] | 919 | dso->kernel ? "the Kernel" : dso->short_name); |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 920 | dso_filter = dso; |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 921 | pstack__push(fstack, &dso_filter); |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 922 | } |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 923 | hists__filter_by_dso(self, dso_filter); |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 924 | hist_browser__title(msg, sizeof(msg), input_name, |
| 925 | dso_filter, thread_filter); |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 926 | if (hist_browser__populate(browser, self, msg) < 0) |
Arnaldo Carvalho de Melo | 8375319 | 2010-04-03 16:30:44 -0300 | [diff] [blame] | 927 | goto out; |
Arnaldo Carvalho de Melo | a5e29ac | 2010-04-03 22:44:37 -0300 | [diff] [blame] | 928 | } else if (choice == zoom_thread) { |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 929 | if (thread_filter) { |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 930 | pstack__remove(fstack, &thread_filter); |
| 931 | zoom_out_thread: |
Arnaldo Carvalho de Melo | 3798ed7 | 2010-05-11 18:01:23 -0300 | [diff] [blame] | 932 | ui_helpline__pop(); |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 933 | thread_filter = NULL; |
| 934 | } else { |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 935 | 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] | 936 | thread->comm_set ? thread->comm : "", |
| 937 | thread->pid); |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 938 | thread_filter = thread; |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 939 | pstack__push(fstack, &thread_filter); |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 940 | } |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 941 | hists__filter_by_thread(self, thread_filter); |
Arnaldo Carvalho de Melo | 6e7ab4c | 2010-04-05 12:02:18 -0300 | [diff] [blame] | 942 | hist_browser__title(msg, sizeof(msg), input_name, |
| 943 | dso_filter, thread_filter); |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 944 | if (hist_browser__populate(browser, self, msg) < 0) |
Arnaldo Carvalho de Melo | a5e29ac | 2010-04-03 22:44:37 -0300 | [diff] [blame] | 945 | goto out; |
Arnaldo Carvalho de Melo | d5679ae | 2010-03-24 16:40:19 -0300 | [diff] [blame] | 946 | } |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 947 | } |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 948 | err = 0; |
Arnaldo Carvalho de Melo | 3e1bbdc3 | 2010-05-14 20:05:21 -0300 | [diff] [blame] | 949 | out_free_stack: |
| 950 | pstack__delete(fstack); |
Arnaldo Carvalho de Melo | e65713e | 2010-04-03 11:25:56 -0300 | [diff] [blame] | 951 | out: |
| 952 | hist_browser__delete(browser); |
| 953 | return err; |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 954 | } |
| 955 | |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 956 | static struct newtPercentTreeColors { |
| 957 | const char *topColorFg, *topColorBg; |
| 958 | const char *mediumColorFg, *mediumColorBg; |
| 959 | const char *normalColorFg, *normalColorBg; |
| 960 | const char *selColorFg, *selColorBg; |
| 961 | const char *codeColorFg, *codeColorBg; |
| 962 | } defaultPercentTreeColors = { |
| 963 | "red", "lightgray", |
| 964 | "green", "lightgray", |
| 965 | "black", "lightgray", |
| 966 | "lightgray", "magenta", |
| 967 | "blue", "lightgray", |
| 968 | }; |
| 969 | |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 970 | void setup_browser(void) |
| 971 | { |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 972 | struct newtPercentTreeColors *c = &defaultPercentTreeColors; |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 973 | if (!isatty(1)) |
| 974 | return; |
| 975 | |
| 976 | use_browser = true; |
| 977 | newtInit(); |
| 978 | newtCls(); |
Arnaldo Carvalho de Melo | 3798ed7 | 2010-05-11 18:01:23 -0300 | [diff] [blame] | 979 | ui_helpline__puts(" "); |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 980 | SLtt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg); |
| 981 | SLtt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg); |
| 982 | SLtt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg); |
| 983 | SLtt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg); |
| 984 | 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] | 985 | } |
| 986 | |
Arnaldo Carvalho de Melo | f3a1f0e | 2010-03-22 13:10:25 -0300 | [diff] [blame] | 987 | void exit_browser(bool wait_for_ok) |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 988 | { |
Arnaldo Carvalho de Melo | f3a1f0e | 2010-03-22 13:10:25 -0300 | [diff] [blame] | 989 | if (use_browser) { |
| 990 | if (wait_for_ok) { |
| 991 | char title[] = "Fatal Error", ok[] = "Ok"; |
| 992 | newtWinMessage(title, ok, browser__last_msg); |
| 993 | } |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 994 | newtFinished(); |
Arnaldo Carvalho de Melo | f3a1f0e | 2010-03-22 13:10:25 -0300 | [diff] [blame] | 995 | } |
Arnaldo Carvalho de Melo | f9224c5 | 2010-03-11 20:12:44 -0300 | [diff] [blame] | 996 | } |