newt.c 29 KB

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