newt.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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. newtFormAddHotKey(self->form, NEWT_KEY_LEFT);
  274. if (ui_browser__refresh_entries(self) < 0)
  275. return -1;
  276. newtFormAddComponent(self->form, self->sb);
  277. while (1) {
  278. unsigned int offset;
  279. newtFormRun(self->form, es);
  280. if (es->reason != NEWT_EXIT_HOTKEY)
  281. break;
  282. switch (es->u.key) {
  283. case NEWT_KEY_DOWN:
  284. if (self->index == self->nr_entries - 1)
  285. break;
  286. ++self->index;
  287. if (self->index == self->first_visible_entry_idx + self->height) {
  288. struct list_head *pos = self->first_visible_entry;
  289. ++self->first_visible_entry_idx;
  290. self->first_visible_entry = pos->next;
  291. }
  292. break;
  293. case NEWT_KEY_UP:
  294. if (self->index == 0)
  295. break;
  296. --self->index;
  297. if (self->index < self->first_visible_entry_idx) {
  298. struct list_head *pos = self->first_visible_entry;
  299. --self->first_visible_entry_idx;
  300. self->first_visible_entry = pos->prev;
  301. }
  302. break;
  303. case NEWT_KEY_PGDN:
  304. if (self->first_visible_entry_idx + self->height > self->nr_entries - 1)
  305. break;
  306. offset = self->height;
  307. if (self->index + offset > self->nr_entries - 1)
  308. offset = self->nr_entries - 1 - self->index;
  309. self->index += offset;
  310. self->first_visible_entry_idx += offset;
  311. while (offset--) {
  312. struct list_head *pos = self->first_visible_entry;
  313. self->first_visible_entry = pos->next;
  314. }
  315. break;
  316. case NEWT_KEY_PGUP:
  317. if (self->first_visible_entry_idx == 0)
  318. break;
  319. if (self->first_visible_entry_idx < self->height)
  320. offset = self->first_visible_entry_idx;
  321. else
  322. offset = self->height;
  323. self->index -= offset;
  324. self->first_visible_entry_idx -= offset;
  325. while (offset--) {
  326. struct list_head *pos = self->first_visible_entry;
  327. self->first_visible_entry = pos->prev;
  328. }
  329. break;
  330. case NEWT_KEY_HOME:
  331. ui_browser__reset_index(self);
  332. break;
  333. case NEWT_KEY_END: {
  334. struct list_head *head = self->entries;
  335. offset = self->height - 1;
  336. if (offset > self->nr_entries)
  337. offset = self->nr_entries;
  338. self->index = self->first_visible_entry_idx = self->nr_entries - 1 - offset;
  339. self->first_visible_entry = head->prev;
  340. while (offset-- != 0) {
  341. struct list_head *pos = self->first_visible_entry;
  342. self->first_visible_entry = pos->prev;
  343. }
  344. }
  345. break;
  346. case NEWT_KEY_ESCAPE:
  347. case NEWT_KEY_LEFT:
  348. case CTRL('c'):
  349. case 'Q':
  350. case 'q':
  351. return 0;
  352. default:
  353. continue;
  354. }
  355. if (ui_browser__refresh_entries(self) < 0)
  356. return -1;
  357. }
  358. return 0;
  359. }
  360. /*
  361. * When debugging newt problems it was useful to be able to "unroll"
  362. * the calls to newtCheckBoxTreeAdd{Array,Item}, so that we can generate
  363. * a source file with the sequence of calls to these methods, to then
  364. * tweak the arrays to get the intended results, so I'm keeping this code
  365. * here, may be useful again in the future.
  366. */
  367. #undef NEWT_DEBUG
  368. static void newt_checkbox_tree__add(newtComponent tree, const char *str,
  369. void *priv, int *indexes)
  370. {
  371. #ifdef NEWT_DEBUG
  372. /* Print the newtCheckboxTreeAddArray to tinker with its index arrays */
  373. int i = 0, len = 40 - strlen(str);
  374. fprintf(stderr,
  375. "\tnewtCheckboxTreeAddItem(tree, %*.*s\"%s\", (void *)%p, 0, ",
  376. len, len, " ", str, priv);
  377. while (indexes[i] != NEWT_ARG_LAST) {
  378. if (indexes[i] != NEWT_ARG_APPEND)
  379. fprintf(stderr, " %d,", indexes[i]);
  380. else
  381. fprintf(stderr, " %s,", "NEWT_ARG_APPEND");
  382. ++i;
  383. }
  384. fprintf(stderr, " %s", " NEWT_ARG_LAST);\n");
  385. fflush(stderr);
  386. #endif
  387. newtCheckboxTreeAddArray(tree, str, priv, 0, indexes);
  388. }
  389. static char *callchain_list__sym_name(struct callchain_list *self,
  390. char *bf, size_t bfsize)
  391. {
  392. if (self->ms.sym)
  393. return self->ms.sym->name;
  394. snprintf(bf, bfsize, "%#Lx", self->ip);
  395. return bf;
  396. }
  397. static void __callchain__append_graph_browser(struct callchain_node *self,
  398. newtComponent tree, u64 total,
  399. int *indexes, int depth)
  400. {
  401. struct rb_node *node;
  402. u64 new_total, remaining;
  403. int idx = 0;
  404. if (callchain_param.mode == CHAIN_GRAPH_REL)
  405. new_total = self->children_hit;
  406. else
  407. new_total = total;
  408. remaining = new_total;
  409. node = rb_first(&self->rb_root);
  410. while (node) {
  411. struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
  412. struct rb_node *next = rb_next(node);
  413. u64 cumul = cumul_hits(child);
  414. struct callchain_list *chain;
  415. int first = true, printed = 0;
  416. int chain_idx = -1;
  417. remaining -= cumul;
  418. indexes[depth] = NEWT_ARG_APPEND;
  419. indexes[depth + 1] = NEWT_ARG_LAST;
  420. list_for_each_entry(chain, &child->val, list) {
  421. char ipstr[BITS_PER_LONG / 4 + 1],
  422. *alloc_str = NULL;
  423. const char *str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  424. if (first) {
  425. double percent = cumul * 100.0 / new_total;
  426. first = false;
  427. if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
  428. str = "Not enough memory!";
  429. else
  430. str = alloc_str;
  431. } else {
  432. indexes[depth] = idx;
  433. indexes[depth + 1] = NEWT_ARG_APPEND;
  434. indexes[depth + 2] = NEWT_ARG_LAST;
  435. ++chain_idx;
  436. }
  437. newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
  438. free(alloc_str);
  439. ++printed;
  440. }
  441. indexes[depth] = idx;
  442. if (chain_idx != -1)
  443. indexes[depth + 1] = chain_idx;
  444. if (printed != 0)
  445. ++idx;
  446. __callchain__append_graph_browser(child, tree, new_total, indexes,
  447. depth + (chain_idx != -1 ? 2 : 1));
  448. node = next;
  449. }
  450. }
  451. static void callchain__append_graph_browser(struct callchain_node *self,
  452. newtComponent tree, u64 total,
  453. int *indexes, int parent_idx)
  454. {
  455. struct callchain_list *chain;
  456. int i = 0;
  457. indexes[1] = NEWT_ARG_APPEND;
  458. indexes[2] = NEWT_ARG_LAST;
  459. list_for_each_entry(chain, &self->val, list) {
  460. char ipstr[BITS_PER_LONG / 4 + 1], *str;
  461. if (chain->ip >= PERF_CONTEXT_MAX)
  462. continue;
  463. if (!i++ && sort__first_dimension == SORT_SYM)
  464. continue;
  465. str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  466. newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
  467. }
  468. indexes[1] = parent_idx;
  469. indexes[2] = NEWT_ARG_APPEND;
  470. indexes[3] = NEWT_ARG_LAST;
  471. __callchain__append_graph_browser(self, tree, total, indexes, 2);
  472. }
  473. static void hist_entry__append_callchain_browser(struct hist_entry *self,
  474. newtComponent tree, u64 total, int parent_idx)
  475. {
  476. struct rb_node *rb_node;
  477. int indexes[1024] = { [0] = parent_idx, };
  478. int idx = 0;
  479. struct callchain_node *chain;
  480. rb_node = rb_first(&self->sorted_chain);
  481. while (rb_node) {
  482. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  483. switch (callchain_param.mode) {
  484. case CHAIN_FLAT:
  485. break;
  486. case CHAIN_GRAPH_ABS: /* falldown */
  487. case CHAIN_GRAPH_REL:
  488. callchain__append_graph_browser(chain, tree, total, indexes, idx++);
  489. break;
  490. case CHAIN_NONE:
  491. default:
  492. break;
  493. }
  494. rb_node = rb_next(rb_node);
  495. }
  496. }
  497. static size_t hist_entry__append_browser(struct hist_entry *self,
  498. newtComponent tree, u64 total)
  499. {
  500. char s[256];
  501. size_t ret;
  502. if (symbol_conf.exclude_other && !self->parent)
  503. return 0;
  504. ret = hist_entry__snprintf(self, s, sizeof(s), NULL,
  505. false, 0, false, total);
  506. if (symbol_conf.use_callchain) {
  507. int indexes[2];
  508. indexes[0] = NEWT_ARG_APPEND;
  509. indexes[1] = NEWT_ARG_LAST;
  510. newt_checkbox_tree__add(tree, s, &self->ms, indexes);
  511. } else
  512. newtListboxAppendEntry(tree, s, &self->ms);
  513. return ret;
  514. }
  515. static void hist_entry__annotate_browser(struct hist_entry *self)
  516. {
  517. struct ui_browser browser;
  518. struct newtExitStruct es;
  519. struct objdump_line *pos, *n;
  520. LIST_HEAD(head);
  521. if (self->ms.sym == NULL)
  522. return;
  523. if (hist_entry__annotate(self, &head) < 0)
  524. return;
  525. ui_helpline__push("Press <- or ESC to exit");
  526. memset(&browser, 0, sizeof(browser));
  527. browser.entries = &head;
  528. browser.priv = self;
  529. list_for_each_entry(pos, &head, node) {
  530. size_t line_len = strlen(pos->line);
  531. if (browser.width < line_len)
  532. browser.width = line_len;
  533. ++browser.nr_entries;
  534. }
  535. browser.width += 18; /* Percentage */
  536. ui_browser__run(&browser, self->ms.sym->name, &es);
  537. newtFormDestroy(browser.form);
  538. newtPopWindow();
  539. list_for_each_entry_safe(pos, n, &head, node) {
  540. list_del(&pos->node);
  541. objdump_line__free(pos);
  542. }
  543. ui_helpline__pop();
  544. }
  545. static const void *newt__symbol_tree_get_current(newtComponent self)
  546. {
  547. if (symbol_conf.use_callchain)
  548. return newtCheckboxTreeGetCurrent(self);
  549. return newtListboxGetCurrent(self);
  550. }
  551. static void hist_browser__selection(newtComponent self, void *data)
  552. {
  553. const struct map_symbol **symbol_ptr = data;
  554. *symbol_ptr = newt__symbol_tree_get_current(self);
  555. }
  556. struct hist_browser {
  557. newtComponent form, tree;
  558. const struct map_symbol *selection;
  559. };
  560. static struct hist_browser *hist_browser__new(void)
  561. {
  562. struct hist_browser *self = malloc(sizeof(*self));
  563. if (self != NULL)
  564. self->form = NULL;
  565. return self;
  566. }
  567. static void hist_browser__delete(struct hist_browser *self)
  568. {
  569. newtFormDestroy(self->form);
  570. newtPopWindow();
  571. free(self);
  572. }
  573. static int hist_browser__populate(struct hist_browser *self, struct hists *hists,
  574. const char *title)
  575. {
  576. int max_len = 0, idx, cols, rows;
  577. struct ui_progress *progress;
  578. struct rb_node *nd;
  579. u64 curr_hist = 0;
  580. char seq[] = ".", unit;
  581. char str[256];
  582. unsigned long nr_events = hists->stats.nr_events[PERF_RECORD_SAMPLE];
  583. if (self->form) {
  584. newtFormDestroy(self->form);
  585. newtPopWindow();
  586. }
  587. nr_events = convert_unit(nr_events, &unit);
  588. snprintf(str, sizeof(str), "Events: %lu%c ",
  589. nr_events, unit);
  590. newtDrawRootText(0, 0, str);
  591. newtGetScreenSize(NULL, &rows);
  592. if (symbol_conf.use_callchain)
  593. self->tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq,
  594. NEWT_FLAG_SCROLL);
  595. else
  596. self->tree = newtListbox(0, 0, rows - 5,
  597. (NEWT_FLAG_SCROLL |
  598. NEWT_FLAG_RETURNEXIT));
  599. newtComponentAddCallback(self->tree, hist_browser__selection,
  600. &self->selection);
  601. progress = ui_progress__new("Adding entries to the browser...",
  602. hists->nr_entries);
  603. if (progress == NULL)
  604. return -1;
  605. idx = 0;
  606. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  607. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  608. int len;
  609. if (h->filtered)
  610. continue;
  611. len = hist_entry__append_browser(h, self->tree, hists->stats.total_period);
  612. if (len > max_len)
  613. max_len = len;
  614. if (symbol_conf.use_callchain)
  615. hist_entry__append_callchain_browser(h, self->tree,
  616. hists->stats.total_period, idx++);
  617. ++curr_hist;
  618. if (curr_hist % 5)
  619. ui_progress__update(progress, curr_hist);
  620. }
  621. ui_progress__delete(progress);
  622. newtGetScreenSize(&cols, &rows);
  623. if (max_len > cols)
  624. max_len = cols - 3;
  625. if (!symbol_conf.use_callchain)
  626. newtListboxSetWidth(self->tree, max_len);
  627. newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0),
  628. rows - 5, title);
  629. self->form = newt_form__new();
  630. if (self->form == NULL)
  631. return -1;
  632. newtFormAddHotKey(self->form, 'A');
  633. newtFormAddHotKey(self->form, 'a');
  634. newtFormAddHotKey(self->form, 'D');
  635. newtFormAddHotKey(self->form, 'd');
  636. newtFormAddHotKey(self->form, 'T');
  637. newtFormAddHotKey(self->form, 't');
  638. newtFormAddHotKey(self->form, NEWT_KEY_RIGHT);
  639. newtFormAddHotKey(self->form, NEWT_KEY_LEFT);
  640. newtFormAddComponents(self->form, self->tree, NULL);
  641. self->selection = newt__symbol_tree_get_current(self->tree);
  642. return 0;
  643. }
  644. static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
  645. {
  646. int *indexes;
  647. if (!symbol_conf.use_callchain)
  648. goto out;
  649. indexes = newtCheckboxTreeFindItem(self->tree, (void *)self->selection);
  650. if (indexes) {
  651. bool is_hist_entry = indexes[1] == NEWT_ARG_LAST;
  652. free(indexes);
  653. if (is_hist_entry)
  654. goto out;
  655. }
  656. return NULL;
  657. out:
  658. return container_of(self->selection, struct hist_entry, ms);
  659. }
  660. static struct thread *hist_browser__selected_thread(struct hist_browser *self)
  661. {
  662. struct hist_entry *he = hist_browser__selected_entry(self);
  663. return he ? he->thread : NULL;
  664. }
  665. static int hist_browser__title(char *bf, size_t size, const char *input_name,
  666. const struct dso *dso, const struct thread *thread)
  667. {
  668. int printed = 0;
  669. if (thread)
  670. printed += snprintf(bf + printed, size - printed,
  671. "Thread: %s(%d)",
  672. (thread->comm_set ? thread->comm : ""),
  673. thread->pid);
  674. if (dso)
  675. printed += snprintf(bf + printed, size - printed,
  676. "%sDSO: %s", thread ? " " : "",
  677. dso->short_name);
  678. return printed ?: snprintf(bf, size, "Report: %s", input_name);
  679. }
  680. int hists__browse(struct hists *self, const char *helpline, const char *input_name)
  681. {
  682. struct hist_browser *browser = hist_browser__new();
  683. struct pstack *fstack = pstack__new(2);
  684. const struct thread *thread_filter = NULL;
  685. const struct dso *dso_filter = NULL;
  686. struct newtExitStruct es;
  687. char msg[160];
  688. int err = -1;
  689. if (browser == NULL)
  690. return -1;
  691. fstack = pstack__new(2);
  692. if (fstack == NULL)
  693. goto out;
  694. ui_helpline__push(helpline);
  695. hist_browser__title(msg, sizeof(msg), input_name,
  696. dso_filter, thread_filter);
  697. if (hist_browser__populate(browser, self, msg) < 0)
  698. goto out_free_stack;
  699. while (1) {
  700. const struct thread *thread;
  701. const struct dso *dso;
  702. char *options[16];
  703. int nr_options = 0, choice = 0, i,
  704. annotate = -2, zoom_dso = -2, zoom_thread = -2;
  705. newtFormRun(browser->form, &es);
  706. thread = hist_browser__selected_thread(browser);
  707. dso = browser->selection->map ? browser->selection->map->dso : NULL;
  708. if (es.reason == NEWT_EXIT_HOTKEY) {
  709. switch (toupper(es.u.key)) {
  710. case 'A':
  711. goto do_annotate;
  712. case 'D':
  713. goto zoom_dso;
  714. case 'T':
  715. goto zoom_thread;
  716. default:;
  717. }
  718. if (toupper(es.u.key) == 'Q' ||
  719. es.u.key == CTRL('c'))
  720. break;
  721. if (es.u.key == NEWT_KEY_ESCAPE) {
  722. if (dialog_yesno("Do you really want to exit?"))
  723. break;
  724. else
  725. continue;
  726. }
  727. if (es.u.key == NEWT_KEY_LEFT) {
  728. const void *top;
  729. if (pstack__empty(fstack))
  730. continue;
  731. top = pstack__pop(fstack);
  732. if (top == &dso_filter)
  733. goto zoom_out_dso;
  734. if (top == &thread_filter)
  735. goto zoom_out_thread;
  736. continue;
  737. }
  738. }
  739. if (browser->selection->sym != NULL &&
  740. asprintf(&options[nr_options], "Annotate %s",
  741. browser->selection->sym->name) > 0)
  742. annotate = nr_options++;
  743. if (thread != NULL &&
  744. asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
  745. (thread_filter ? "out of" : "into"),
  746. (thread->comm_set ? thread->comm : ""),
  747. thread->pid) > 0)
  748. zoom_thread = nr_options++;
  749. if (dso != NULL &&
  750. asprintf(&options[nr_options], "Zoom %s %s DSO",
  751. (dso_filter ? "out of" : "into"),
  752. (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
  753. zoom_dso = nr_options++;
  754. options[nr_options++] = (char *)"Exit";
  755. choice = popup_menu(nr_options, options);
  756. for (i = 0; i < nr_options - 1; ++i)
  757. free(options[i]);
  758. if (choice == nr_options - 1)
  759. break;
  760. if (choice == -1)
  761. continue;
  762. if (choice == annotate) {
  763. struct hist_entry *he;
  764. do_annotate:
  765. if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
  766. ui_helpline__puts("No vmlinux file found, can't "
  767. "annotate with just a "
  768. "kallsyms file");
  769. continue;
  770. }
  771. he = hist_browser__selected_entry(browser);
  772. if (he == NULL)
  773. continue;
  774. hist_entry__annotate_browser(he);
  775. } else if (choice == zoom_dso) {
  776. zoom_dso:
  777. if (dso_filter) {
  778. pstack__remove(fstack, &dso_filter);
  779. zoom_out_dso:
  780. ui_helpline__pop();
  781. dso_filter = NULL;
  782. } else {
  783. if (dso == NULL)
  784. continue;
  785. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
  786. dso->kernel ? "the Kernel" : dso->short_name);
  787. dso_filter = dso;
  788. pstack__push(fstack, &dso_filter);
  789. }
  790. hists__filter_by_dso(self, dso_filter);
  791. hist_browser__title(msg, sizeof(msg), input_name,
  792. dso_filter, thread_filter);
  793. if (hist_browser__populate(browser, self, msg) < 0)
  794. goto out;
  795. } else if (choice == zoom_thread) {
  796. zoom_thread:
  797. if (thread_filter) {
  798. pstack__remove(fstack, &thread_filter);
  799. zoom_out_thread:
  800. ui_helpline__pop();
  801. thread_filter = NULL;
  802. } else {
  803. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
  804. thread->comm_set ? thread->comm : "",
  805. thread->pid);
  806. thread_filter = thread;
  807. pstack__push(fstack, &thread_filter);
  808. }
  809. hists__filter_by_thread(self, thread_filter);
  810. hist_browser__title(msg, sizeof(msg), input_name,
  811. dso_filter, thread_filter);
  812. if (hist_browser__populate(browser, self, msg) < 0)
  813. goto out;
  814. }
  815. }
  816. err = 0;
  817. out_free_stack:
  818. pstack__delete(fstack);
  819. out:
  820. hist_browser__delete(browser);
  821. return err;
  822. }
  823. static struct newtPercentTreeColors {
  824. const char *topColorFg, *topColorBg;
  825. const char *mediumColorFg, *mediumColorBg;
  826. const char *normalColorFg, *normalColorBg;
  827. const char *selColorFg, *selColorBg;
  828. const char *codeColorFg, *codeColorBg;
  829. } defaultPercentTreeColors = {
  830. "red", "lightgray",
  831. "green", "lightgray",
  832. "black", "lightgray",
  833. "lightgray", "magenta",
  834. "blue", "lightgray",
  835. };
  836. void setup_browser(void)
  837. {
  838. struct newtPercentTreeColors *c = &defaultPercentTreeColors;
  839. if (!isatty(1))
  840. return;
  841. use_browser = true;
  842. newtInit();
  843. newtCls();
  844. ui_helpline__puts(" ");
  845. SLtt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
  846. SLtt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
  847. SLtt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
  848. SLtt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
  849. SLtt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
  850. }
  851. void exit_browser(bool wait_for_ok)
  852. {
  853. if (use_browser) {
  854. if (wait_for_ok) {
  855. char title[] = "Fatal Error", ok[] = "Ok";
  856. newtWinMessage(title, ok, browser__last_msg);
  857. }
  858. newtFinished();
  859. }
  860. }