newt.c 24 KB

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