newt.c 24 KB

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