newt.c 29 KB

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