newt.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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. newtFormDestroy(self->form);
  296. newtPopWindow();
  297. }
  298. ui_browser__refresh_dimensions(self);
  299. newtCenteredWindow(self->width, self->height, title);
  300. self->form = newt_form__new();
  301. if (self->form == NULL)
  302. return -1;
  303. self->sb = newtVerticalScrollbar(self->width, 0, self->height,
  304. HE_COLORSET_NORMAL,
  305. HE_COLORSET_SELECTED);
  306. if (self->sb == NULL)
  307. return -1;
  308. newtFormAddHotKey(self->form, NEWT_KEY_UP);
  309. newtFormAddHotKey(self->form, NEWT_KEY_DOWN);
  310. newtFormAddHotKey(self->form, NEWT_KEY_PGUP);
  311. newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
  312. newtFormAddHotKey(self->form, NEWT_KEY_HOME);
  313. newtFormAddHotKey(self->form, NEWT_KEY_END);
  314. newtFormAddComponent(self->form, self->sb);
  315. return 0;
  316. }
  317. static int objdump_line__show(struct objdump_line *self, struct list_head *head,
  318. int width, struct hist_entry *he, int len,
  319. bool current_entry)
  320. {
  321. if (self->offset != -1) {
  322. struct symbol *sym = he->ms.sym;
  323. unsigned int hits = 0;
  324. double percent = 0.0;
  325. int color;
  326. struct sym_priv *priv = symbol__priv(sym);
  327. struct sym_ext *sym_ext = priv->ext;
  328. struct sym_hist *h = priv->hist;
  329. s64 offset = self->offset;
  330. struct objdump_line *next = objdump__get_next_ip_line(head, self);
  331. while (offset < (s64)len &&
  332. (next == NULL || offset < next->offset)) {
  333. if (sym_ext) {
  334. percent += sym_ext[offset].percent;
  335. } else
  336. hits += h->ip[offset];
  337. ++offset;
  338. }
  339. if (sym_ext == NULL && h->sum)
  340. percent = 100.0 * hits / h->sum;
  341. color = ui_browser__percent_color(percent, current_entry);
  342. SLsmg_set_color(color);
  343. slsmg_printf(" %7.2f ", percent);
  344. if (!current_entry)
  345. SLsmg_set_color(HE_COLORSET_CODE);
  346. } else {
  347. int color = ui_browser__percent_color(0, current_entry);
  348. SLsmg_set_color(color);
  349. slsmg_write_nstring(" ", 9);
  350. }
  351. SLsmg_write_char(':');
  352. slsmg_write_nstring(" ", 8);
  353. if (!*self->line)
  354. slsmg_write_nstring(" ", width - 18);
  355. else
  356. slsmg_write_nstring(self->line, width - 18);
  357. return 0;
  358. }
  359. static int ui_browser__refresh_entries(struct ui_browser *self)
  360. {
  361. int row;
  362. newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
  363. row = self->refresh_entries(self);
  364. SLsmg_set_color(HE_COLORSET_NORMAL);
  365. SLsmg_fill_region(self->top + row, self->left,
  366. self->height - row, self->width, ' ');
  367. return 0;
  368. }
  369. static int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
  370. {
  371. if (ui_browser__refresh_entries(self) < 0)
  372. return -1;
  373. while (1) {
  374. off_t offset;
  375. newtFormRun(self->form, es);
  376. if (es->reason != NEWT_EXIT_HOTKEY)
  377. break;
  378. if (is_exit_key(es->u.key))
  379. return es->u.key;
  380. switch (es->u.key) {
  381. case NEWT_KEY_DOWN:
  382. if (self->index == self->nr_entries - 1)
  383. break;
  384. ++self->index;
  385. if (self->index == self->first_visible_entry_idx + self->height) {
  386. ++self->first_visible_entry_idx;
  387. self->seek(self, +1, SEEK_CUR);
  388. }
  389. break;
  390. case NEWT_KEY_UP:
  391. if (self->index == 0)
  392. break;
  393. --self->index;
  394. if (self->index < self->first_visible_entry_idx) {
  395. --self->first_visible_entry_idx;
  396. self->seek(self, -1, SEEK_CUR);
  397. }
  398. break;
  399. case NEWT_KEY_PGDN:
  400. case ' ':
  401. if (self->first_visible_entry_idx + self->height > self->nr_entries - 1)
  402. break;
  403. offset = self->height;
  404. if (self->index + offset > self->nr_entries - 1)
  405. offset = self->nr_entries - 1 - self->index;
  406. self->index += offset;
  407. self->first_visible_entry_idx += offset;
  408. self->seek(self, +offset, SEEK_CUR);
  409. break;
  410. case NEWT_KEY_PGUP:
  411. if (self->first_visible_entry_idx == 0)
  412. break;
  413. if (self->first_visible_entry_idx < self->height)
  414. offset = self->first_visible_entry_idx;
  415. else
  416. offset = self->height;
  417. self->index -= offset;
  418. self->first_visible_entry_idx -= offset;
  419. self->seek(self, -offset, SEEK_CUR);
  420. break;
  421. case NEWT_KEY_HOME:
  422. ui_browser__reset_index(self);
  423. break;
  424. case NEWT_KEY_END:
  425. offset = self->height - 1;
  426. if (offset >= self->nr_entries)
  427. offset = self->nr_entries - 1;
  428. self->index = self->nr_entries - 1;
  429. self->first_visible_entry_idx = self->index - offset;
  430. self->seek(self, -offset, SEEK_END);
  431. break;
  432. default:
  433. return es->u.key;
  434. }
  435. if (ui_browser__refresh_entries(self) < 0)
  436. return -1;
  437. }
  438. return 0;
  439. }
  440. /*
  441. * When debugging newt problems it was useful to be able to "unroll"
  442. * the calls to newtCheckBoxTreeAdd{Array,Item}, so that we can generate
  443. * a source file with the sequence of calls to these methods, to then
  444. * tweak the arrays to get the intended results, so I'm keeping this code
  445. * here, may be useful again in the future.
  446. */
  447. #undef NEWT_DEBUG
  448. static void newt_checkbox_tree__add(newtComponent tree, const char *str,
  449. void *priv, int *indexes)
  450. {
  451. #ifdef NEWT_DEBUG
  452. /* Print the newtCheckboxTreeAddArray to tinker with its index arrays */
  453. int i = 0, len = 40 - strlen(str);
  454. fprintf(stderr,
  455. "\tnewtCheckboxTreeAddItem(tree, %*.*s\"%s\", (void *)%p, 0, ",
  456. len, len, " ", str, priv);
  457. while (indexes[i] != NEWT_ARG_LAST) {
  458. if (indexes[i] != NEWT_ARG_APPEND)
  459. fprintf(stderr, " %d,", indexes[i]);
  460. else
  461. fprintf(stderr, " %s,", "NEWT_ARG_APPEND");
  462. ++i;
  463. }
  464. fprintf(stderr, " %s", " NEWT_ARG_LAST);\n");
  465. fflush(stderr);
  466. #endif
  467. newtCheckboxTreeAddArray(tree, str, priv, 0, indexes);
  468. }
  469. static char *callchain_list__sym_name(struct callchain_list *self,
  470. char *bf, size_t bfsize)
  471. {
  472. if (self->ms.sym)
  473. return self->ms.sym->name;
  474. snprintf(bf, bfsize, "%#Lx", self->ip);
  475. return bf;
  476. }
  477. static unsigned int hist_entry__annotate_browser_refresh(struct ui_browser *self)
  478. {
  479. struct objdump_line *pos;
  480. struct list_head *head = self->entries;
  481. struct hist_entry *he = self->priv;
  482. int row = 0;
  483. int len = he->ms.sym->end - he->ms.sym->start;
  484. if (self->first_visible_entry == NULL || self->first_visible_entry == self->entries)
  485. self->first_visible_entry = head->next;
  486. pos = list_entry(self->first_visible_entry, struct objdump_line, node);
  487. list_for_each_entry_from(pos, head, node) {
  488. bool current_entry = ui_browser__is_current_entry(self, row);
  489. SLsmg_gotorc(self->top + row, self->left);
  490. objdump_line__show(pos, head, self->width,
  491. he, len, current_entry);
  492. if (++row == self->height)
  493. break;
  494. }
  495. return row;
  496. }
  497. static void __callchain__append_graph_browser(struct callchain_node *self,
  498. newtComponent tree, u64 total,
  499. int *indexes, int depth)
  500. {
  501. struct rb_node *node;
  502. u64 new_total, remaining;
  503. int idx = 0;
  504. if (callchain_param.mode == CHAIN_GRAPH_REL)
  505. new_total = self->children_hit;
  506. else
  507. new_total = total;
  508. remaining = new_total;
  509. node = rb_first(&self->rb_root);
  510. while (node) {
  511. struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
  512. struct rb_node *next = rb_next(node);
  513. u64 cumul = cumul_hits(child);
  514. struct callchain_list *chain;
  515. int first = true, printed = 0;
  516. int chain_idx = -1;
  517. remaining -= cumul;
  518. indexes[depth] = NEWT_ARG_APPEND;
  519. indexes[depth + 1] = NEWT_ARG_LAST;
  520. list_for_each_entry(chain, &child->val, list) {
  521. char ipstr[BITS_PER_LONG / 4 + 1],
  522. *alloc_str = NULL;
  523. const char *str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  524. if (first) {
  525. double percent = cumul * 100.0 / new_total;
  526. first = false;
  527. if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
  528. str = "Not enough memory!";
  529. else
  530. str = alloc_str;
  531. } else {
  532. indexes[depth] = idx;
  533. indexes[depth + 1] = NEWT_ARG_APPEND;
  534. indexes[depth + 2] = NEWT_ARG_LAST;
  535. ++chain_idx;
  536. }
  537. newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
  538. free(alloc_str);
  539. ++printed;
  540. }
  541. indexes[depth] = idx;
  542. if (chain_idx != -1)
  543. indexes[depth + 1] = chain_idx;
  544. if (printed != 0)
  545. ++idx;
  546. __callchain__append_graph_browser(child, tree, new_total, indexes,
  547. depth + (chain_idx != -1 ? 2 : 1));
  548. node = next;
  549. }
  550. }
  551. static void callchain__append_graph_browser(struct callchain_node *self,
  552. newtComponent tree, u64 total,
  553. int *indexes, int parent_idx)
  554. {
  555. struct callchain_list *chain;
  556. int i = 0;
  557. indexes[1] = NEWT_ARG_APPEND;
  558. indexes[2] = NEWT_ARG_LAST;
  559. list_for_each_entry(chain, &self->val, list) {
  560. char ipstr[BITS_PER_LONG / 4 + 1], *str;
  561. if (chain->ip >= PERF_CONTEXT_MAX)
  562. continue;
  563. if (!i++ && sort__first_dimension == SORT_SYM)
  564. continue;
  565. str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  566. newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
  567. }
  568. indexes[1] = parent_idx;
  569. indexes[2] = NEWT_ARG_APPEND;
  570. indexes[3] = NEWT_ARG_LAST;
  571. __callchain__append_graph_browser(self, tree, total, indexes, 2);
  572. }
  573. static void hist_entry__append_callchain_browser(struct hist_entry *self,
  574. newtComponent tree, u64 total, int parent_idx)
  575. {
  576. struct rb_node *rb_node;
  577. int indexes[1024] = { [0] = parent_idx, };
  578. int idx = 0;
  579. struct callchain_node *chain;
  580. rb_node = rb_first(&self->sorted_chain);
  581. while (rb_node) {
  582. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  583. switch (callchain_param.mode) {
  584. case CHAIN_FLAT:
  585. break;
  586. case CHAIN_GRAPH_ABS: /* falldown */
  587. case CHAIN_GRAPH_REL:
  588. callchain__append_graph_browser(chain, tree, total, indexes, idx++);
  589. break;
  590. case CHAIN_NONE:
  591. default:
  592. break;
  593. }
  594. rb_node = rb_next(rb_node);
  595. }
  596. }
  597. static size_t hist_entry__append_browser(struct hist_entry *self,
  598. newtComponent tree,
  599. struct hists *hists)
  600. {
  601. char s[256];
  602. size_t ret;
  603. if (symbol_conf.exclude_other && !self->parent)
  604. return 0;
  605. ret = hist_entry__snprintf(self, s, sizeof(s), hists, NULL,
  606. false, 0, false, hists->stats.total_period);
  607. if (symbol_conf.use_callchain) {
  608. int indexes[2];
  609. indexes[0] = NEWT_ARG_APPEND;
  610. indexes[1] = NEWT_ARG_LAST;
  611. newt_checkbox_tree__add(tree, s, &self->ms, indexes);
  612. } else
  613. newtListboxAppendEntry(tree, s, &self->ms);
  614. return ret;
  615. }
  616. int hist_entry__tui_annotate(struct hist_entry *self)
  617. {
  618. struct ui_browser browser;
  619. struct newtExitStruct es;
  620. struct objdump_line *pos, *n;
  621. LIST_HEAD(head);
  622. int ret;
  623. if (self->ms.sym == NULL)
  624. return -1;
  625. if (self->ms.map->dso->annotate_warned)
  626. return -1;
  627. if (hist_entry__annotate(self, &head) < 0) {
  628. ui__error_window(browser__last_msg);
  629. return -1;
  630. }
  631. ui_helpline__push("Press <- or ESC to exit");
  632. memset(&browser, 0, sizeof(browser));
  633. browser.entries = &head;
  634. browser.refresh_entries = hist_entry__annotate_browser_refresh;
  635. browser.seek = ui_browser__list_head_seek;
  636. browser.priv = self;
  637. list_for_each_entry(pos, &head, node) {
  638. size_t line_len = strlen(pos->line);
  639. if (browser.width < line_len)
  640. browser.width = line_len;
  641. ++browser.nr_entries;
  642. }
  643. browser.width += 18; /* Percentage */
  644. ui_browser__show(&browser, self->ms.sym->name);
  645. newtFormAddHotKey(browser.form, ' ');
  646. ret = ui_browser__run(&browser, &es);
  647. newtFormDestroy(browser.form);
  648. newtPopWindow();
  649. list_for_each_entry_safe(pos, n, &head, node) {
  650. list_del(&pos->node);
  651. objdump_line__free(pos);
  652. }
  653. ui_helpline__pop();
  654. return ret;
  655. }
  656. static const void *newt__symbol_tree_get_current(newtComponent self)
  657. {
  658. if (symbol_conf.use_callchain)
  659. return newtCheckboxTreeGetCurrent(self);
  660. return newtListboxGetCurrent(self);
  661. }
  662. static void hist_browser__selection(newtComponent self, void *data)
  663. {
  664. const struct map_symbol **symbol_ptr = data;
  665. *symbol_ptr = newt__symbol_tree_get_current(self);
  666. }
  667. struct hist_browser {
  668. newtComponent form, tree;
  669. const struct map_symbol *selection;
  670. };
  671. static struct hist_browser *hist_browser__new(void)
  672. {
  673. struct hist_browser *self = malloc(sizeof(*self));
  674. if (self != NULL)
  675. self->form = NULL;
  676. return self;
  677. }
  678. static void hist_browser__delete(struct hist_browser *self)
  679. {
  680. newtFormDestroy(self->form);
  681. newtPopWindow();
  682. free(self);
  683. }
  684. static int hist_browser__populate(struct hist_browser *self, struct hists *hists,
  685. const char *title)
  686. {
  687. int max_len = 0, idx, cols, rows;
  688. struct ui_progress *progress;
  689. struct rb_node *nd;
  690. u64 curr_hist = 0;
  691. char seq[] = ".", unit;
  692. char str[256];
  693. unsigned long nr_events = hists->stats.nr_events[PERF_RECORD_SAMPLE];
  694. if (self->form) {
  695. newtFormDestroy(self->form);
  696. newtPopWindow();
  697. }
  698. nr_events = convert_unit(nr_events, &unit);
  699. snprintf(str, sizeof(str), "Events: %lu%c ",
  700. nr_events, unit);
  701. newtDrawRootText(0, 0, str);
  702. newtGetScreenSize(NULL, &rows);
  703. if (symbol_conf.use_callchain)
  704. self->tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq,
  705. NEWT_FLAG_SCROLL);
  706. else
  707. self->tree = newtListbox(0, 0, rows - 5,
  708. (NEWT_FLAG_SCROLL |
  709. NEWT_FLAG_RETURNEXIT));
  710. newtComponentAddCallback(self->tree, hist_browser__selection,
  711. &self->selection);
  712. progress = ui_progress__new("Adding entries to the browser...",
  713. hists->nr_entries);
  714. if (progress == NULL)
  715. return -1;
  716. idx = 0;
  717. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  718. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  719. int len;
  720. if (h->filtered)
  721. continue;
  722. len = hist_entry__append_browser(h, self->tree, hists);
  723. if (len > max_len)
  724. max_len = len;
  725. if (symbol_conf.use_callchain)
  726. hist_entry__append_callchain_browser(h, self->tree,
  727. hists->stats.total_period, idx++);
  728. ++curr_hist;
  729. if (curr_hist % 5)
  730. ui_progress__update(progress, curr_hist);
  731. }
  732. ui_progress__delete(progress);
  733. newtGetScreenSize(&cols, &rows);
  734. if (max_len > cols)
  735. max_len = cols - 3;
  736. if (!symbol_conf.use_callchain)
  737. newtListboxSetWidth(self->tree, max_len);
  738. newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0),
  739. rows - 5, title);
  740. self->form = newt_form__new();
  741. if (self->form == NULL)
  742. return -1;
  743. newtFormAddHotKey(self->form, 'A');
  744. newtFormAddHotKey(self->form, 'a');
  745. newtFormAddHotKey(self->form, 'D');
  746. newtFormAddHotKey(self->form, 'd');
  747. newtFormAddHotKey(self->form, 'T');
  748. newtFormAddHotKey(self->form, 't');
  749. newtFormAddHotKey(self->form, '?');
  750. newtFormAddHotKey(self->form, 'H');
  751. newtFormAddHotKey(self->form, 'h');
  752. newtFormAddHotKey(self->form, NEWT_KEY_F1);
  753. newtFormAddHotKey(self->form, NEWT_KEY_RIGHT);
  754. newtFormAddHotKey(self->form, NEWT_KEY_TAB);
  755. newtFormAddHotKey(self->form, NEWT_KEY_UNTAB);
  756. newtFormAddComponents(self->form, self->tree, NULL);
  757. self->selection = newt__symbol_tree_get_current(self->tree);
  758. return 0;
  759. }
  760. static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
  761. {
  762. int *indexes;
  763. if (!symbol_conf.use_callchain)
  764. goto out;
  765. indexes = newtCheckboxTreeFindItem(self->tree, (void *)self->selection);
  766. if (indexes) {
  767. bool is_hist_entry = indexes[1] == NEWT_ARG_LAST;
  768. free(indexes);
  769. if (is_hist_entry)
  770. goto out;
  771. }
  772. return NULL;
  773. out:
  774. return container_of(self->selection, struct hist_entry, ms);
  775. }
  776. static struct thread *hist_browser__selected_thread(struct hist_browser *self)
  777. {
  778. struct hist_entry *he = hist_browser__selected_entry(self);
  779. return he ? he->thread : NULL;
  780. }
  781. static int hist_browser__title(char *bf, size_t size, const char *ev_name,
  782. const struct dso *dso, const struct thread *thread)
  783. {
  784. int printed = 0;
  785. if (thread)
  786. printed += snprintf(bf + printed, size - printed,
  787. "Thread: %s(%d)",
  788. (thread->comm_set ? thread->comm : ""),
  789. thread->pid);
  790. if (dso)
  791. printed += snprintf(bf + printed, size - printed,
  792. "%sDSO: %s", thread ? " " : "",
  793. dso->short_name);
  794. return printed ?: snprintf(bf, size, "Event: %s", ev_name);
  795. }
  796. int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
  797. {
  798. struct hist_browser *browser = hist_browser__new();
  799. struct pstack *fstack;
  800. const struct thread *thread_filter = NULL;
  801. const struct dso *dso_filter = NULL;
  802. struct newtExitStruct es;
  803. char msg[160];
  804. int key = -1;
  805. if (browser == NULL)
  806. return -1;
  807. fstack = pstack__new(2);
  808. if (fstack == NULL)
  809. goto out;
  810. ui_helpline__push(helpline);
  811. hist_browser__title(msg, sizeof(msg), ev_name,
  812. dso_filter, thread_filter);
  813. if (hist_browser__populate(browser, self, msg) < 0)
  814. goto out_free_stack;
  815. while (1) {
  816. const struct thread *thread;
  817. const struct dso *dso;
  818. char *options[16];
  819. int nr_options = 0, choice = 0, i,
  820. annotate = -2, zoom_dso = -2, zoom_thread = -2;
  821. newtFormRun(browser->form, &es);
  822. thread = hist_browser__selected_thread(browser);
  823. dso = browser->selection->map ? browser->selection->map->dso : NULL;
  824. if (es.reason == NEWT_EXIT_HOTKEY) {
  825. key = es.u.key;
  826. switch (key) {
  827. case NEWT_KEY_F1:
  828. goto do_help;
  829. case NEWT_KEY_TAB:
  830. case NEWT_KEY_UNTAB:
  831. /*
  832. * Exit the browser, let hists__browser_tree
  833. * go to the next or previous
  834. */
  835. goto out_free_stack;
  836. default:;
  837. }
  838. key = toupper(key);
  839. switch (key) {
  840. case 'A':
  841. if (browser->selection->map == NULL &&
  842. browser->selection->map->dso->annotate_warned)
  843. continue;
  844. goto do_annotate;
  845. case 'D':
  846. goto zoom_dso;
  847. case 'T':
  848. goto zoom_thread;
  849. case 'H':
  850. case '?':
  851. do_help:
  852. ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
  853. "<- Zoom out\n"
  854. "a Annotate current symbol\n"
  855. "h/?/F1 Show this window\n"
  856. "d Zoom into current DSO\n"
  857. "t Zoom into current Thread\n"
  858. "q/CTRL+C Exit browser");
  859. continue;
  860. default:;
  861. }
  862. if (is_exit_key(key)) {
  863. if (key == NEWT_KEY_ESCAPE) {
  864. if (dialog_yesno("Do you really want to exit?"))
  865. break;
  866. else
  867. continue;
  868. } else
  869. break;
  870. }
  871. if (es.u.key == NEWT_KEY_LEFT) {
  872. const void *top;
  873. if (pstack__empty(fstack))
  874. continue;
  875. top = pstack__pop(fstack);
  876. if (top == &dso_filter)
  877. goto zoom_out_dso;
  878. if (top == &thread_filter)
  879. goto zoom_out_thread;
  880. continue;
  881. }
  882. }
  883. if (browser->selection->sym != NULL &&
  884. !browser->selection->map->dso->annotate_warned &&
  885. asprintf(&options[nr_options], "Annotate %s",
  886. browser->selection->sym->name) > 0)
  887. annotate = nr_options++;
  888. if (thread != NULL &&
  889. asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
  890. (thread_filter ? "out of" : "into"),
  891. (thread->comm_set ? thread->comm : ""),
  892. thread->pid) > 0)
  893. zoom_thread = nr_options++;
  894. if (dso != NULL &&
  895. asprintf(&options[nr_options], "Zoom %s %s DSO",
  896. (dso_filter ? "out of" : "into"),
  897. (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
  898. zoom_dso = nr_options++;
  899. options[nr_options++] = (char *)"Exit";
  900. choice = popup_menu(nr_options, options);
  901. for (i = 0; i < nr_options - 1; ++i)
  902. free(options[i]);
  903. if (choice == nr_options - 1)
  904. break;
  905. if (choice == -1)
  906. continue;
  907. if (choice == annotate) {
  908. struct hist_entry *he;
  909. do_annotate:
  910. if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
  911. browser->selection->map->dso->annotate_warned = 1;
  912. ui_helpline__puts("No vmlinux file found, can't "
  913. "annotate with just a "
  914. "kallsyms file");
  915. continue;
  916. }
  917. he = hist_browser__selected_entry(browser);
  918. if (he == NULL)
  919. continue;
  920. hist_entry__tui_annotate(he);
  921. } else if (choice == zoom_dso) {
  922. zoom_dso:
  923. if (dso_filter) {
  924. pstack__remove(fstack, &dso_filter);
  925. zoom_out_dso:
  926. ui_helpline__pop();
  927. dso_filter = NULL;
  928. } else {
  929. if (dso == NULL)
  930. continue;
  931. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
  932. dso->kernel ? "the Kernel" : dso->short_name);
  933. dso_filter = dso;
  934. pstack__push(fstack, &dso_filter);
  935. }
  936. hists__filter_by_dso(self, dso_filter);
  937. hist_browser__title(msg, sizeof(msg), ev_name,
  938. dso_filter, thread_filter);
  939. if (hist_browser__populate(browser, self, msg) < 0)
  940. goto out;
  941. } else if (choice == zoom_thread) {
  942. zoom_thread:
  943. if (thread_filter) {
  944. pstack__remove(fstack, &thread_filter);
  945. zoom_out_thread:
  946. ui_helpline__pop();
  947. thread_filter = NULL;
  948. } else {
  949. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
  950. thread->comm_set ? thread->comm : "",
  951. thread->pid);
  952. thread_filter = thread;
  953. pstack__push(fstack, &thread_filter);
  954. }
  955. hists__filter_by_thread(self, thread_filter);
  956. hist_browser__title(msg, sizeof(msg), ev_name,
  957. dso_filter, thread_filter);
  958. if (hist_browser__populate(browser, self, msg) < 0)
  959. goto out;
  960. }
  961. }
  962. out_free_stack:
  963. pstack__delete(fstack);
  964. out:
  965. hist_browser__delete(browser);
  966. return key;
  967. }
  968. int hists__tui_browse_tree(struct rb_root *self, const char *help)
  969. {
  970. struct rb_node *first = rb_first(self), *nd = first, *next;
  971. int key = 0;
  972. while (nd) {
  973. struct hists *hists = rb_entry(nd, struct hists, rb_node);
  974. const char *ev_name = __event_name(hists->type, hists->config);
  975. key = hists__browse(hists, help, ev_name);
  976. if (is_exit_key(key))
  977. break;
  978. switch (key) {
  979. case NEWT_KEY_TAB:
  980. next = rb_next(nd);
  981. if (next)
  982. nd = next;
  983. break;
  984. case NEWT_KEY_UNTAB:
  985. if (nd == first)
  986. continue;
  987. nd = rb_prev(nd);
  988. default:
  989. break;
  990. }
  991. }
  992. return key;
  993. }
  994. static struct newtPercentTreeColors {
  995. const char *topColorFg, *topColorBg;
  996. const char *mediumColorFg, *mediumColorBg;
  997. const char *normalColorFg, *normalColorBg;
  998. const char *selColorFg, *selColorBg;
  999. const char *codeColorFg, *codeColorBg;
  1000. } defaultPercentTreeColors = {
  1001. "red", "lightgray",
  1002. "green", "lightgray",
  1003. "black", "lightgray",
  1004. "lightgray", "magenta",
  1005. "blue", "lightgray",
  1006. };
  1007. void setup_browser(void)
  1008. {
  1009. struct newtPercentTreeColors *c = &defaultPercentTreeColors;
  1010. if (!isatty(1) || !use_browser || dump_trace) {
  1011. use_browser = 0;
  1012. setup_pager();
  1013. return;
  1014. }
  1015. use_browser = 1;
  1016. newtInit();
  1017. newtCls();
  1018. ui_helpline__puts(" ");
  1019. sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
  1020. sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
  1021. sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
  1022. sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
  1023. sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
  1024. }
  1025. void exit_browser(bool wait_for_ok)
  1026. {
  1027. if (use_browser > 0) {
  1028. if (wait_for_ok) {
  1029. char title[] = "Fatal Error", ok[] = "Ok";
  1030. newtWinMessage(title, ok, browser__last_msg);
  1031. }
  1032. newtFinished();
  1033. }
  1034. }