newt.c 26 KB

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