newt.c 28 KB

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