newt.c 26 KB

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