newt.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  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 <signal.h>
  15. #include <stdlib.h>
  16. #include <elf.h>
  17. #include <newt.h>
  18. #include <sys/ttydefaults.h>
  19. #include "cache.h"
  20. #include "hist.h"
  21. #include "pstack.h"
  22. #include "session.h"
  23. #include "sort.h"
  24. #include "symbol.h"
  25. #if SLANG_VERSION < 20104
  26. #define slsmg_printf(msg, args...) SLsmg_printf((char *)msg, ##args)
  27. #define slsmg_write_nstring(msg, len) SLsmg_write_nstring((char *)msg, len)
  28. #define sltt_set_color(obj, name, fg, bg) SLtt_set_color(obj,(char *)name,\
  29. (char *)fg, (char *)bg)
  30. #else
  31. #define slsmg_printf SLsmg_printf
  32. #define slsmg_write_nstring SLsmg_write_nstring
  33. #define sltt_set_color SLtt_set_color
  34. #endif
  35. struct ui_progress {
  36. newtComponent form, scale;
  37. };
  38. struct ui_progress *ui_progress__new(const char *title, u64 total)
  39. {
  40. struct ui_progress *self = malloc(sizeof(*self));
  41. if (self != NULL) {
  42. int cols;
  43. if (use_browser <= 0)
  44. return self;
  45. newtGetScreenSize(&cols, NULL);
  46. cols -= 4;
  47. newtCenteredWindow(cols, 1, title);
  48. self->form = newtForm(NULL, NULL, 0);
  49. if (self->form == NULL)
  50. goto out_free_self;
  51. self->scale = newtScale(0, 0, cols, total);
  52. if (self->scale == NULL)
  53. goto out_free_form;
  54. newtFormAddComponent(self->form, self->scale);
  55. newtRefresh();
  56. }
  57. return self;
  58. out_free_form:
  59. newtFormDestroy(self->form);
  60. out_free_self:
  61. free(self);
  62. return NULL;
  63. }
  64. void ui_progress__update(struct ui_progress *self, u64 curr)
  65. {
  66. /*
  67. * FIXME: We should have a per UI backend way of showing progress,
  68. * stdio will just show a percentage as NN%, etc.
  69. */
  70. if (use_browser <= 0)
  71. return;
  72. newtScaleSet(self->scale, curr);
  73. newtRefresh();
  74. }
  75. void ui_progress__delete(struct ui_progress *self)
  76. {
  77. if (use_browser > 0) {
  78. newtFormDestroy(self->form);
  79. newtPopWindow();
  80. }
  81. free(self);
  82. }
  83. static void ui_helpline__pop(void)
  84. {
  85. newtPopHelpLine();
  86. }
  87. static void ui_helpline__push(const char *msg)
  88. {
  89. newtPushHelpLine(msg);
  90. }
  91. static void ui_helpline__vpush(const char *fmt, va_list ap)
  92. {
  93. char *s;
  94. if (vasprintf(&s, fmt, ap) < 0)
  95. vfprintf(stderr, fmt, ap);
  96. else {
  97. ui_helpline__push(s);
  98. free(s);
  99. }
  100. }
  101. static void ui_helpline__fpush(const char *fmt, ...)
  102. {
  103. va_list ap;
  104. va_start(ap, fmt);
  105. ui_helpline__vpush(fmt, ap);
  106. va_end(ap);
  107. }
  108. static void ui_helpline__puts(const char *msg)
  109. {
  110. ui_helpline__pop();
  111. ui_helpline__push(msg);
  112. }
  113. static char browser__last_msg[1024];
  114. int browser__show_help(const char *format, va_list ap)
  115. {
  116. int ret;
  117. static int backlog;
  118. ret = vsnprintf(browser__last_msg + backlog,
  119. sizeof(browser__last_msg) - backlog, format, ap);
  120. backlog += ret;
  121. if (browser__last_msg[backlog - 1] == '\n') {
  122. ui_helpline__puts(browser__last_msg);
  123. newtRefresh();
  124. backlog = 0;
  125. }
  126. return ret;
  127. }
  128. static void newt_form__set_exit_keys(newtComponent self)
  129. {
  130. newtFormAddHotKey(self, NEWT_KEY_LEFT);
  131. newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
  132. newtFormAddHotKey(self, 'Q');
  133. newtFormAddHotKey(self, 'q');
  134. newtFormAddHotKey(self, CTRL('c'));
  135. }
  136. static newtComponent newt_form__new(void)
  137. {
  138. newtComponent self = newtForm(NULL, NULL, 0);
  139. if (self)
  140. newt_form__set_exit_keys(self);
  141. return self;
  142. }
  143. static int popup_menu(int argc, char * const argv[])
  144. {
  145. struct newtExitStruct es;
  146. int i, rc = -1, max_len = 5;
  147. newtComponent listbox, form = newt_form__new();
  148. if (form == NULL)
  149. return -1;
  150. listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
  151. if (listbox == NULL)
  152. goto out_destroy_form;
  153. newtFormAddComponent(form, listbox);
  154. for (i = 0; i < argc; ++i) {
  155. int len = strlen(argv[i]);
  156. if (len > max_len)
  157. max_len = len;
  158. if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
  159. goto out_destroy_form;
  160. }
  161. newtCenteredWindow(max_len, argc, NULL);
  162. newtFormRun(form, &es);
  163. rc = newtListboxGetCurrent(listbox) - NULL;
  164. if (es.reason == NEWT_EXIT_HOTKEY)
  165. rc = -1;
  166. newtPopWindow();
  167. out_destroy_form:
  168. newtFormDestroy(form);
  169. return rc;
  170. }
  171. static int ui__help_window(const char *text)
  172. {
  173. struct newtExitStruct es;
  174. newtComponent tb, form = newt_form__new();
  175. int rc = -1;
  176. int max_len = 0, nr_lines = 0;
  177. const char *t;
  178. if (form == NULL)
  179. return -1;
  180. t = text;
  181. while (1) {
  182. const char *sep = strchr(t, '\n');
  183. int len;
  184. if (sep == NULL)
  185. sep = strchr(t, '\0');
  186. len = sep - t;
  187. if (max_len < len)
  188. max_len = len;
  189. ++nr_lines;
  190. if (*sep == '\0')
  191. break;
  192. t = sep + 1;
  193. }
  194. tb = newtTextbox(0, 0, max_len, nr_lines, 0);
  195. if (tb == NULL)
  196. goto out_destroy_form;
  197. newtTextboxSetText(tb, text);
  198. newtFormAddComponent(form, tb);
  199. newtCenteredWindow(max_len, nr_lines, NULL);
  200. newtFormRun(form, &es);
  201. newtPopWindow();
  202. rc = 0;
  203. out_destroy_form:
  204. newtFormDestroy(form);
  205. return rc;
  206. }
  207. static bool dialog_yesno(const char *msg)
  208. {
  209. /* newtWinChoice should really be accepting const char pointers... */
  210. char yes[] = "Yes", no[] = "No";
  211. return newtWinChoice(NULL, yes, no, (char *)msg) == 1;
  212. }
  213. static void ui__error_window(const char *fmt, ...)
  214. {
  215. va_list ap;
  216. va_start(ap, fmt);
  217. newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
  218. va_end(ap);
  219. }
  220. #define HE_COLORSET_TOP 50
  221. #define HE_COLORSET_MEDIUM 51
  222. #define HE_COLORSET_NORMAL 52
  223. #define HE_COLORSET_SELECTED 53
  224. #define HE_COLORSET_CODE 54
  225. static int ui_browser__percent_color(double percent, bool current)
  226. {
  227. if (current)
  228. return HE_COLORSET_SELECTED;
  229. if (percent >= MIN_RED)
  230. return HE_COLORSET_TOP;
  231. if (percent >= MIN_GREEN)
  232. return HE_COLORSET_MEDIUM;
  233. return HE_COLORSET_NORMAL;
  234. }
  235. struct ui_browser {
  236. newtComponent form, sb;
  237. u64 index, first_visible_entry_idx;
  238. void *first_visible_entry, *entries;
  239. u16 top, left, width, height;
  240. void *priv;
  241. unsigned int (*refresh)(struct ui_browser *self);
  242. void (*write)(struct ui_browser *self, void *entry, int row);
  243. void (*seek)(struct ui_browser *self,
  244. off_t offset, int whence);
  245. u32 nr_entries;
  246. };
  247. static void ui_browser__list_head_seek(struct ui_browser *self,
  248. off_t offset, int whence)
  249. {
  250. struct list_head *head = self->entries;
  251. struct list_head *pos;
  252. switch (whence) {
  253. case SEEK_SET:
  254. pos = head->next;
  255. break;
  256. case SEEK_CUR:
  257. pos = self->first_visible_entry;
  258. break;
  259. case SEEK_END:
  260. pos = head->prev;
  261. break;
  262. default:
  263. return;
  264. }
  265. if (offset > 0) {
  266. while (offset-- != 0)
  267. pos = pos->next;
  268. } else {
  269. while (offset++ != 0)
  270. pos = pos->prev;
  271. }
  272. self->first_visible_entry = pos;
  273. }
  274. static void ui_browser__rb_tree_seek(struct ui_browser *self,
  275. off_t offset, int whence)
  276. {
  277. struct rb_root *root = self->entries;
  278. struct rb_node *nd;
  279. switch (whence) {
  280. case SEEK_SET:
  281. nd = rb_first(root);
  282. break;
  283. case SEEK_CUR:
  284. nd = self->first_visible_entry;
  285. break;
  286. case SEEK_END:
  287. nd = rb_last(root);
  288. break;
  289. default:
  290. return;
  291. }
  292. if (offset > 0) {
  293. while (offset-- != 0)
  294. nd = rb_next(nd);
  295. } else {
  296. while (offset++ != 0)
  297. nd = rb_prev(nd);
  298. }
  299. self->first_visible_entry = nd;
  300. }
  301. static unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self)
  302. {
  303. struct rb_node *nd;
  304. int row = 0;
  305. if (self->first_visible_entry == NULL)
  306. self->first_visible_entry = rb_first(self->entries);
  307. nd = self->first_visible_entry;
  308. while (nd != NULL) {
  309. SLsmg_gotorc(self->top + row, self->left);
  310. self->write(self, nd, row);
  311. if (++row == self->height)
  312. break;
  313. nd = rb_next(nd);
  314. }
  315. return row;
  316. }
  317. static bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
  318. {
  319. return (self->first_visible_entry_idx + row) == self->index;
  320. }
  321. static void ui_browser__refresh_dimensions(struct ui_browser *self)
  322. {
  323. int cols, rows;
  324. newtGetScreenSize(&cols, &rows);
  325. if (self->width > cols - 4)
  326. self->width = cols - 4;
  327. self->height = rows - 5;
  328. if (self->height > self->nr_entries)
  329. self->height = self->nr_entries;
  330. self->top = (rows - self->height) / 2;
  331. self->left = (cols - self->width) / 2;
  332. }
  333. static void ui_browser__reset_index(struct ui_browser *self)
  334. {
  335. self->index = self->first_visible_entry_idx = 0;
  336. self->seek(self, 0, SEEK_SET);
  337. }
  338. static int ui_browser__show(struct ui_browser *self, const char *title)
  339. {
  340. if (self->form != NULL) {
  341. newtFormDestroy(self->form);
  342. newtPopWindow();
  343. }
  344. ui_browser__refresh_dimensions(self);
  345. newtCenteredWindow(self->width, self->height, title);
  346. self->form = newt_form__new();
  347. if (self->form == NULL)
  348. return -1;
  349. self->sb = newtVerticalScrollbar(self->width, 0, self->height,
  350. HE_COLORSET_NORMAL,
  351. HE_COLORSET_SELECTED);
  352. if (self->sb == NULL)
  353. return -1;
  354. newtFormAddHotKey(self->form, NEWT_KEY_UP);
  355. newtFormAddHotKey(self->form, NEWT_KEY_DOWN);
  356. newtFormAddHotKey(self->form, NEWT_KEY_PGUP);
  357. newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
  358. newtFormAddHotKey(self->form, NEWT_KEY_HOME);
  359. newtFormAddHotKey(self->form, NEWT_KEY_END);
  360. newtFormAddComponent(self->form, self->sb);
  361. return 0;
  362. }
  363. static int objdump_line__show(struct objdump_line *self, struct list_head *head,
  364. int width, struct hist_entry *he, int len,
  365. bool current_entry)
  366. {
  367. if (self->offset != -1) {
  368. struct symbol *sym = he->ms.sym;
  369. unsigned int hits = 0;
  370. double percent = 0.0;
  371. int color;
  372. struct sym_priv *priv = symbol__priv(sym);
  373. struct sym_ext *sym_ext = priv->ext;
  374. struct sym_hist *h = priv->hist;
  375. s64 offset = self->offset;
  376. struct objdump_line *next = objdump__get_next_ip_line(head, self);
  377. while (offset < (s64)len &&
  378. (next == NULL || offset < next->offset)) {
  379. if (sym_ext) {
  380. percent += sym_ext[offset].percent;
  381. } else
  382. hits += h->ip[offset];
  383. ++offset;
  384. }
  385. if (sym_ext == NULL && h->sum)
  386. percent = 100.0 * hits / h->sum;
  387. color = ui_browser__percent_color(percent, current_entry);
  388. SLsmg_set_color(color);
  389. slsmg_printf(" %7.2f ", percent);
  390. if (!current_entry)
  391. SLsmg_set_color(HE_COLORSET_CODE);
  392. } else {
  393. int color = ui_browser__percent_color(0, current_entry);
  394. SLsmg_set_color(color);
  395. slsmg_write_nstring(" ", 9);
  396. }
  397. SLsmg_write_char(':');
  398. slsmg_write_nstring(" ", 8);
  399. if (!*self->line)
  400. slsmg_write_nstring(" ", width - 18);
  401. else
  402. slsmg_write_nstring(self->line, width - 18);
  403. return 0;
  404. }
  405. static int ui_browser__refresh(struct ui_browser *self)
  406. {
  407. int row;
  408. newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
  409. row = self->refresh(self);
  410. SLsmg_set_color(HE_COLORSET_NORMAL);
  411. SLsmg_fill_region(self->top + row, self->left,
  412. self->height - row, self->width, ' ');
  413. return 0;
  414. }
  415. static int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
  416. {
  417. if (ui_browser__refresh(self) < 0)
  418. return -1;
  419. while (1) {
  420. off_t offset;
  421. newtFormRun(self->form, es);
  422. if (es->reason != NEWT_EXIT_HOTKEY)
  423. break;
  424. if (is_exit_key(es->u.key))
  425. return es->u.key;
  426. switch (es->u.key) {
  427. case NEWT_KEY_DOWN:
  428. if (self->index == self->nr_entries - 1)
  429. break;
  430. ++self->index;
  431. if (self->index == self->first_visible_entry_idx + self->height) {
  432. ++self->first_visible_entry_idx;
  433. self->seek(self, +1, SEEK_CUR);
  434. }
  435. break;
  436. case NEWT_KEY_UP:
  437. if (self->index == 0)
  438. break;
  439. --self->index;
  440. if (self->index < self->first_visible_entry_idx) {
  441. --self->first_visible_entry_idx;
  442. self->seek(self, -1, SEEK_CUR);
  443. }
  444. break;
  445. case NEWT_KEY_PGDN:
  446. case ' ':
  447. if (self->first_visible_entry_idx + self->height > self->nr_entries - 1)
  448. break;
  449. offset = self->height;
  450. if (self->index + offset > self->nr_entries - 1)
  451. offset = self->nr_entries - 1 - self->index;
  452. self->index += offset;
  453. self->first_visible_entry_idx += offset;
  454. self->seek(self, +offset, SEEK_CUR);
  455. break;
  456. case NEWT_KEY_PGUP:
  457. if (self->first_visible_entry_idx == 0)
  458. break;
  459. if (self->first_visible_entry_idx < self->height)
  460. offset = self->first_visible_entry_idx;
  461. else
  462. offset = self->height;
  463. self->index -= offset;
  464. self->first_visible_entry_idx -= offset;
  465. self->seek(self, -offset, SEEK_CUR);
  466. break;
  467. case NEWT_KEY_HOME:
  468. ui_browser__reset_index(self);
  469. break;
  470. case NEWT_KEY_END:
  471. offset = self->height - 1;
  472. if (offset >= self->nr_entries)
  473. offset = self->nr_entries - 1;
  474. self->index = self->nr_entries - 1;
  475. self->first_visible_entry_idx = self->index - offset;
  476. self->seek(self, -offset, SEEK_END);
  477. break;
  478. default:
  479. return es->u.key;
  480. }
  481. if (ui_browser__refresh(self) < 0)
  482. return -1;
  483. }
  484. return 0;
  485. }
  486. static char *callchain_list__sym_name(struct callchain_list *self,
  487. char *bf, size_t bfsize)
  488. {
  489. if (self->ms.sym)
  490. return self->ms.sym->name;
  491. snprintf(bf, bfsize, "%#Lx", self->ip);
  492. return bf;
  493. }
  494. static unsigned int hist_entry__annotate_browser_refresh(struct ui_browser *self)
  495. {
  496. struct objdump_line *pos;
  497. struct list_head *head = self->entries;
  498. struct hist_entry *he = self->priv;
  499. int row = 0;
  500. int len = he->ms.sym->end - he->ms.sym->start;
  501. if (self->first_visible_entry == NULL || self->first_visible_entry == self->entries)
  502. self->first_visible_entry = head->next;
  503. pos = list_entry(self->first_visible_entry, struct objdump_line, node);
  504. list_for_each_entry_from(pos, head, node) {
  505. bool current_entry = ui_browser__is_current_entry(self, row);
  506. SLsmg_gotorc(self->top + row, self->left);
  507. objdump_line__show(pos, head, self->width,
  508. he, len, current_entry);
  509. if (++row == self->height)
  510. break;
  511. }
  512. return row;
  513. }
  514. int hist_entry__tui_annotate(struct hist_entry *self)
  515. {
  516. struct ui_browser browser;
  517. struct newtExitStruct es;
  518. struct objdump_line *pos, *n;
  519. LIST_HEAD(head);
  520. int ret;
  521. if (self->ms.sym == NULL)
  522. return -1;
  523. if (self->ms.map->dso->annotate_warned)
  524. return -1;
  525. if (hist_entry__annotate(self, &head) < 0) {
  526. ui__error_window(browser__last_msg);
  527. return -1;
  528. }
  529. ui_helpline__push("Press <- or ESC to exit");
  530. memset(&browser, 0, sizeof(browser));
  531. browser.entries = &head;
  532. browser.refresh = hist_entry__annotate_browser_refresh;
  533. browser.seek = ui_browser__list_head_seek;
  534. browser.priv = self;
  535. list_for_each_entry(pos, &head, node) {
  536. size_t line_len = strlen(pos->line);
  537. if (browser.width < line_len)
  538. browser.width = line_len;
  539. ++browser.nr_entries;
  540. }
  541. browser.width += 18; /* Percentage */
  542. ui_browser__show(&browser, self->ms.sym->name);
  543. newtFormAddHotKey(browser.form, ' ');
  544. ret = ui_browser__run(&browser, &es);
  545. newtFormDestroy(browser.form);
  546. newtPopWindow();
  547. list_for_each_entry_safe(pos, n, &head, node) {
  548. list_del(&pos->node);
  549. objdump_line__free(pos);
  550. }
  551. ui_helpline__pop();
  552. return ret;
  553. }
  554. /* -------------------------------------------------------------------- */
  555. struct map_browser {
  556. struct ui_browser b;
  557. struct map *map;
  558. u16 namelen;
  559. u8 addrlen;
  560. };
  561. static void map_browser__write(struct ui_browser *self, void *nd, int row)
  562. {
  563. struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
  564. struct map_browser *mb = container_of(self, struct map_browser, b);
  565. bool current_entry = ui_browser__is_current_entry(self, row);
  566. int color = ui_browser__percent_color(0, current_entry);
  567. SLsmg_set_color(color);
  568. slsmg_printf("%*llx %*llx %c ",
  569. mb->addrlen, sym->start, mb->addrlen, sym->end,
  570. sym->binding == STB_GLOBAL ? 'g' :
  571. sym->binding == STB_LOCAL ? 'l' : 'w');
  572. slsmg_write_nstring(sym->name, mb->namelen);
  573. }
  574. static int map__browse(struct map *self)
  575. {
  576. struct map_browser mb = {
  577. .b = {
  578. .entries = &self->dso->symbols[self->type],
  579. .refresh = ui_browser__rb_tree_refresh,
  580. .seek = ui_browser__rb_tree_seek,
  581. .write = map_browser__write,
  582. },
  583. };
  584. struct newtExitStruct es;
  585. struct rb_node *nd;
  586. char tmp[BITS_PER_LONG / 4];
  587. u64 maxaddr = 0;
  588. int ret;
  589. ui_helpline__push("Press <- or ESC to exit");
  590. for (nd = rb_first(mb.b.entries); nd; nd = rb_next(nd)) {
  591. struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
  592. if (mb.namelen < pos->namelen)
  593. mb.namelen = pos->namelen;
  594. if (maxaddr < pos->end)
  595. maxaddr = pos->end;
  596. ++mb.b.nr_entries;
  597. }
  598. mb.addrlen = snprintf(tmp, sizeof(tmp), "%llx", maxaddr);
  599. mb.b.width += mb.addrlen * 2 + 4 + mb.namelen;
  600. ui_browser__show(&mb.b, self->dso->long_name);
  601. ret = ui_browser__run(&mb.b, &es);
  602. newtFormDestroy(mb.b.form);
  603. newtPopWindow();
  604. ui_helpline__pop();
  605. return ret;
  606. }
  607. /* -------------------------------------------------------------------- */
  608. struct hist_browser {
  609. struct ui_browser b;
  610. struct hists *hists;
  611. struct hist_entry *he_selection;
  612. struct map_symbol *selection;
  613. };
  614. static void hist_browser__reset(struct hist_browser *self);
  615. static int hist_browser__run(struct hist_browser *self, const char *title,
  616. struct newtExitStruct *es);
  617. static unsigned int hist_browser__refresh(struct ui_browser *self);
  618. static void ui_browser__hists_seek(struct ui_browser *self,
  619. off_t offset, int whence);
  620. static struct hist_browser *hist_browser__new(struct hists *hists)
  621. {
  622. struct hist_browser *self = zalloc(sizeof(*self));
  623. if (self) {
  624. self->hists = hists;
  625. self->b.refresh = hist_browser__refresh;
  626. self->b.seek = ui_browser__hists_seek;
  627. }
  628. return self;
  629. }
  630. static void hist_browser__delete(struct hist_browser *self)
  631. {
  632. newtFormDestroy(self->b.form);
  633. newtPopWindow();
  634. free(self);
  635. }
  636. static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
  637. {
  638. return self->he_selection;
  639. }
  640. static struct thread *hist_browser__selected_thread(struct hist_browser *self)
  641. {
  642. return self->he_selection->thread;
  643. }
  644. static int hist_browser__title(char *bf, size_t size, const char *ev_name,
  645. const struct dso *dso, const struct thread *thread)
  646. {
  647. int printed = 0;
  648. if (thread)
  649. printed += snprintf(bf + printed, size - printed,
  650. "Thread: %s(%d)",
  651. (thread->comm_set ? thread->comm : ""),
  652. thread->pid);
  653. if (dso)
  654. printed += snprintf(bf + printed, size - printed,
  655. "%sDSO: %s", thread ? " " : "",
  656. dso->short_name);
  657. return printed ?: snprintf(bf, size, "Event: %s", ev_name);
  658. }
  659. int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
  660. {
  661. struct hist_browser *browser = hist_browser__new(self);
  662. struct pstack *fstack;
  663. const struct thread *thread_filter = NULL;
  664. const struct dso *dso_filter = NULL;
  665. struct newtExitStruct es;
  666. char msg[160];
  667. int key = -1;
  668. if (browser == NULL)
  669. return -1;
  670. fstack = pstack__new(2);
  671. if (fstack == NULL)
  672. goto out;
  673. ui_helpline__push(helpline);
  674. hist_browser__title(msg, sizeof(msg), ev_name,
  675. dso_filter, thread_filter);
  676. while (1) {
  677. const struct thread *thread;
  678. const struct dso *dso;
  679. char *options[16];
  680. int nr_options = 0, choice = 0, i,
  681. annotate = -2, zoom_dso = -2, zoom_thread = -2,
  682. browse_map = -2;
  683. if (hist_browser__run(browser, msg, &es))
  684. break;
  685. thread = hist_browser__selected_thread(browser);
  686. dso = browser->selection->map ? browser->selection->map->dso : NULL;
  687. if (es.reason == NEWT_EXIT_HOTKEY) {
  688. key = es.u.key;
  689. switch (key) {
  690. case NEWT_KEY_F1:
  691. goto do_help;
  692. case NEWT_KEY_TAB:
  693. case NEWT_KEY_UNTAB:
  694. /*
  695. * Exit the browser, let hists__browser_tree
  696. * go to the next or previous
  697. */
  698. goto out_free_stack;
  699. default:;
  700. }
  701. key = toupper(key);
  702. switch (key) {
  703. case 'A':
  704. if (browser->selection->map == NULL &&
  705. browser->selection->map->dso->annotate_warned)
  706. continue;
  707. goto do_annotate;
  708. case 'D':
  709. goto zoom_dso;
  710. case 'T':
  711. goto zoom_thread;
  712. case 'H':
  713. case '?':
  714. do_help:
  715. ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
  716. "<- Zoom out\n"
  717. "a Annotate current symbol\n"
  718. "h/?/F1 Show this window\n"
  719. "d Zoom into current DSO\n"
  720. "t Zoom into current Thread\n"
  721. "q/CTRL+C Exit browser");
  722. continue;
  723. default:;
  724. }
  725. if (is_exit_key(key)) {
  726. if (key == NEWT_KEY_ESCAPE) {
  727. if (dialog_yesno("Do you really want to exit?"))
  728. break;
  729. else
  730. continue;
  731. } else
  732. break;
  733. }
  734. if (es.u.key == NEWT_KEY_LEFT) {
  735. const void *top;
  736. if (pstack__empty(fstack))
  737. continue;
  738. top = pstack__pop(fstack);
  739. if (top == &dso_filter)
  740. goto zoom_out_dso;
  741. if (top == &thread_filter)
  742. goto zoom_out_thread;
  743. continue;
  744. }
  745. }
  746. if (browser->selection->sym != NULL &&
  747. !browser->selection->map->dso->annotate_warned &&
  748. asprintf(&options[nr_options], "Annotate %s",
  749. browser->selection->sym->name) > 0)
  750. annotate = nr_options++;
  751. if (thread != NULL &&
  752. asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
  753. (thread_filter ? "out of" : "into"),
  754. (thread->comm_set ? thread->comm : ""),
  755. thread->pid) > 0)
  756. zoom_thread = nr_options++;
  757. if (dso != NULL &&
  758. asprintf(&options[nr_options], "Zoom %s %s DSO",
  759. (dso_filter ? "out of" : "into"),
  760. (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
  761. zoom_dso = nr_options++;
  762. if (browser->selection->map != NULL &&
  763. asprintf(&options[nr_options], "Browse map details") > 0)
  764. browse_map = nr_options++;
  765. options[nr_options++] = (char *)"Exit";
  766. choice = popup_menu(nr_options, options);
  767. for (i = 0; i < nr_options - 1; ++i)
  768. free(options[i]);
  769. if (choice == nr_options - 1)
  770. break;
  771. if (choice == -1)
  772. continue;
  773. if (choice == annotate) {
  774. struct hist_entry *he;
  775. do_annotate:
  776. if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
  777. browser->selection->map->dso->annotate_warned = 1;
  778. ui_helpline__puts("No vmlinux file found, can't "
  779. "annotate with just a "
  780. "kallsyms file");
  781. continue;
  782. }
  783. he = hist_browser__selected_entry(browser);
  784. if (he == NULL)
  785. continue;
  786. hist_entry__tui_annotate(he);
  787. } else if (choice == browse_map)
  788. map__browse(browser->selection->map);
  789. else if (choice == zoom_dso) {
  790. zoom_dso:
  791. if (dso_filter) {
  792. pstack__remove(fstack, &dso_filter);
  793. zoom_out_dso:
  794. ui_helpline__pop();
  795. dso_filter = NULL;
  796. } else {
  797. if (dso == NULL)
  798. continue;
  799. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
  800. dso->kernel ? "the Kernel" : dso->short_name);
  801. dso_filter = dso;
  802. pstack__push(fstack, &dso_filter);
  803. }
  804. hists__filter_by_dso(self, dso_filter);
  805. hist_browser__title(msg, sizeof(msg), ev_name,
  806. dso_filter, thread_filter);
  807. hist_browser__reset(browser);
  808. } else if (choice == zoom_thread) {
  809. zoom_thread:
  810. if (thread_filter) {
  811. pstack__remove(fstack, &thread_filter);
  812. zoom_out_thread:
  813. ui_helpline__pop();
  814. thread_filter = NULL;
  815. } else {
  816. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
  817. thread->comm_set ? thread->comm : "",
  818. thread->pid);
  819. thread_filter = thread;
  820. pstack__push(fstack, &thread_filter);
  821. }
  822. hists__filter_by_thread(self, thread_filter);
  823. hist_browser__title(msg, sizeof(msg), ev_name,
  824. dso_filter, thread_filter);
  825. hist_browser__reset(browser);
  826. }
  827. }
  828. out_free_stack:
  829. pstack__delete(fstack);
  830. out:
  831. hist_browser__delete(browser);
  832. return key;
  833. }
  834. int hists__tui_browse_tree(struct rb_root *self, const char *help)
  835. {
  836. struct rb_node *first = rb_first(self), *nd = first, *next;
  837. int key = 0;
  838. while (nd) {
  839. struct hists *hists = rb_entry(nd, struct hists, rb_node);
  840. const char *ev_name = __event_name(hists->type, hists->config);
  841. key = hists__browse(hists, help, ev_name);
  842. if (is_exit_key(key))
  843. break;
  844. switch (key) {
  845. case NEWT_KEY_TAB:
  846. next = rb_next(nd);
  847. if (next)
  848. nd = next;
  849. break;
  850. case NEWT_KEY_UNTAB:
  851. if (nd == first)
  852. continue;
  853. nd = rb_prev(nd);
  854. default:
  855. break;
  856. }
  857. }
  858. return key;
  859. }
  860. static struct newtPercentTreeColors {
  861. const char *topColorFg, *topColorBg;
  862. const char *mediumColorFg, *mediumColorBg;
  863. const char *normalColorFg, *normalColorBg;
  864. const char *selColorFg, *selColorBg;
  865. const char *codeColorFg, *codeColorBg;
  866. } defaultPercentTreeColors = {
  867. "red", "lightgray",
  868. "green", "lightgray",
  869. "black", "lightgray",
  870. "lightgray", "magenta",
  871. "blue", "lightgray",
  872. };
  873. static void newt_suspend(void *d __used)
  874. {
  875. newtSuspend();
  876. raise(SIGTSTP);
  877. newtResume();
  878. }
  879. void setup_browser(void)
  880. {
  881. struct newtPercentTreeColors *c = &defaultPercentTreeColors;
  882. if (!isatty(1) || !use_browser || dump_trace) {
  883. use_browser = 0;
  884. setup_pager();
  885. return;
  886. }
  887. use_browser = 1;
  888. newtInit();
  889. newtCls();
  890. newtSetSuspendCallback(newt_suspend, NULL);
  891. ui_helpline__puts(" ");
  892. sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
  893. sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
  894. sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
  895. sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
  896. sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
  897. }
  898. void exit_browser(bool wait_for_ok)
  899. {
  900. if (use_browser > 0) {
  901. if (wait_for_ok) {
  902. char title[] = "Fatal Error", ok[] = "Ok";
  903. newtWinMessage(title, ok, browser__last_msg);
  904. }
  905. newtFinished();
  906. }
  907. }
  908. static void hist_browser__refresh_dimensions(struct hist_browser *self)
  909. {
  910. /* 3 == +/- toggle symbol before actual hist_entry rendering */
  911. self->b.width = 3 + (hists__sort_list_width(self->hists) +
  912. sizeof("[k]"));
  913. }
  914. static void hist_browser__reset(struct hist_browser *self)
  915. {
  916. self->b.nr_entries = self->hists->nr_entries;
  917. hist_browser__refresh_dimensions(self);
  918. ui_browser__reset_index(&self->b);
  919. }
  920. static char tree__folded_sign(bool unfolded)
  921. {
  922. return unfolded ? '-' : '+';
  923. }
  924. static char map_symbol__folded(const struct map_symbol *self)
  925. {
  926. return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
  927. }
  928. static char hist_entry__folded(const struct hist_entry *self)
  929. {
  930. return map_symbol__folded(&self->ms);
  931. }
  932. static char callchain_list__folded(const struct callchain_list *self)
  933. {
  934. return map_symbol__folded(&self->ms);
  935. }
  936. static bool map_symbol__toggle_fold(struct map_symbol *self)
  937. {
  938. if (!self->has_children)
  939. return false;
  940. self->unfolded = !self->unfolded;
  941. return true;
  942. }
  943. #define LEVEL_OFFSET_STEP 3
  944. static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
  945. struct callchain_node *chain_node,
  946. u64 total, int level,
  947. unsigned short row,
  948. off_t *row_offset,
  949. bool *is_current_entry)
  950. {
  951. struct rb_node *node;
  952. int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
  953. u64 new_total, remaining;
  954. if (callchain_param.mode == CHAIN_GRAPH_REL)
  955. new_total = chain_node->children_hit;
  956. else
  957. new_total = total;
  958. remaining = new_total;
  959. node = rb_first(&chain_node->rb_root);
  960. while (node) {
  961. struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
  962. struct rb_node *next = rb_next(node);
  963. u64 cumul = cumul_hits(child);
  964. struct callchain_list *chain;
  965. char folded_sign = ' ';
  966. int first = true;
  967. int extra_offset = 0;
  968. remaining -= cumul;
  969. list_for_each_entry(chain, &child->val, list) {
  970. char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
  971. const char *str;
  972. int color;
  973. bool was_first = first;
  974. if (first) {
  975. first = false;
  976. chain->ms.has_children = chain->list.next != &child->val ||
  977. rb_first(&child->rb_root) != NULL;
  978. } else {
  979. extra_offset = LEVEL_OFFSET_STEP;
  980. chain->ms.has_children = chain->list.next == &child->val &&
  981. rb_first(&child->rb_root) != NULL;
  982. }
  983. folded_sign = callchain_list__folded(chain);
  984. if (*row_offset != 0) {
  985. --*row_offset;
  986. goto do_next;
  987. }
  988. alloc_str = NULL;
  989. str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  990. if (was_first) {
  991. double percent = cumul * 100.0 / new_total;
  992. if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
  993. str = "Not enough memory!";
  994. else
  995. str = alloc_str;
  996. }
  997. color = HE_COLORSET_NORMAL;
  998. width = self->b.width - (offset + extra_offset + 2);
  999. if (ui_browser__is_current_entry(&self->b, row)) {
  1000. self->selection = &chain->ms;
  1001. color = HE_COLORSET_SELECTED;
  1002. *is_current_entry = true;
  1003. }
  1004. SLsmg_set_color(color);
  1005. SLsmg_gotorc(self->b.top + row, self->b.left);
  1006. slsmg_write_nstring(" ", offset + extra_offset);
  1007. slsmg_printf("%c ", folded_sign);
  1008. slsmg_write_nstring(str, width);
  1009. free(alloc_str);
  1010. if (++row == self->b.height)
  1011. goto out;
  1012. do_next:
  1013. if (folded_sign == '+')
  1014. break;
  1015. }
  1016. if (folded_sign == '-') {
  1017. const int new_level = level + (extra_offset ? 2 : 1);
  1018. row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
  1019. new_level, row, row_offset,
  1020. is_current_entry);
  1021. }
  1022. if (row == self->b.height)
  1023. goto out;
  1024. node = next;
  1025. }
  1026. out:
  1027. return row - first_row;
  1028. }
  1029. static int hist_browser__show_callchain_node(struct hist_browser *self,
  1030. struct callchain_node *node,
  1031. int level, unsigned short row,
  1032. off_t *row_offset,
  1033. bool *is_current_entry)
  1034. {
  1035. struct callchain_list *chain;
  1036. int first_row = row,
  1037. offset = level * LEVEL_OFFSET_STEP,
  1038. width = self->b.width - offset;
  1039. char folded_sign = ' ';
  1040. list_for_each_entry(chain, &node->val, list) {
  1041. char ipstr[BITS_PER_LONG / 4 + 1], *s;
  1042. int color;
  1043. /*
  1044. * FIXME: This should be moved to somewhere else,
  1045. * probably when the callchain is created, so as not to
  1046. * traverse it all over again
  1047. */
  1048. chain->ms.has_children = rb_first(&node->rb_root) != NULL;
  1049. folded_sign = callchain_list__folded(chain);
  1050. if (*row_offset != 0) {
  1051. --*row_offset;
  1052. continue;
  1053. }
  1054. color = HE_COLORSET_NORMAL;
  1055. if (ui_browser__is_current_entry(&self->b, row)) {
  1056. self->selection = &chain->ms;
  1057. color = HE_COLORSET_SELECTED;
  1058. *is_current_entry = true;
  1059. }
  1060. s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  1061. SLsmg_gotorc(self->b.top + row, self->b.left);
  1062. SLsmg_set_color(color);
  1063. slsmg_write_nstring(" ", offset);
  1064. slsmg_printf("%c ", folded_sign);
  1065. slsmg_write_nstring(s, width - 2);
  1066. if (++row == self->b.height)
  1067. goto out;
  1068. }
  1069. if (folded_sign == '-')
  1070. row += hist_browser__show_callchain_node_rb_tree(self, node,
  1071. self->hists->stats.total_period,
  1072. level + 1, row,
  1073. row_offset,
  1074. is_current_entry);
  1075. out:
  1076. return row - first_row;
  1077. }
  1078. static int hist_browser__show_callchain(struct hist_browser *self,
  1079. struct rb_root *chain,
  1080. int level, unsigned short row,
  1081. off_t *row_offset,
  1082. bool *is_current_entry)
  1083. {
  1084. struct rb_node *nd;
  1085. int first_row = row;
  1086. for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
  1087. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  1088. row += hist_browser__show_callchain_node(self, node, level,
  1089. row, row_offset,
  1090. is_current_entry);
  1091. if (row == self->b.height)
  1092. break;
  1093. }
  1094. return row - first_row;
  1095. }
  1096. static int hist_browser__show_entry(struct hist_browser *self,
  1097. struct hist_entry *entry,
  1098. unsigned short row)
  1099. {
  1100. char s[256];
  1101. double percent;
  1102. int printed = 0;
  1103. int color, width = self->b.width;
  1104. char folded_sign = ' ';
  1105. bool current_entry = ui_browser__is_current_entry(&self->b, row);
  1106. off_t row_offset = entry->row_offset;
  1107. if (current_entry) {
  1108. self->he_selection = entry;
  1109. self->selection = &entry->ms;
  1110. }
  1111. if (symbol_conf.use_callchain) {
  1112. entry->ms.has_children = !RB_EMPTY_ROOT(&entry->sorted_chain);
  1113. folded_sign = hist_entry__folded(entry);
  1114. }
  1115. if (row_offset == 0) {
  1116. hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false,
  1117. 0, false, self->hists->stats.total_period);
  1118. percent = (entry->period * 100.0) / self->hists->stats.total_period;
  1119. color = HE_COLORSET_SELECTED;
  1120. if (!current_entry) {
  1121. if (percent >= MIN_RED)
  1122. color = HE_COLORSET_TOP;
  1123. else if (percent >= MIN_GREEN)
  1124. color = HE_COLORSET_MEDIUM;
  1125. else
  1126. color = HE_COLORSET_NORMAL;
  1127. }
  1128. SLsmg_set_color(color);
  1129. SLsmg_gotorc(self->b.top + row, self->b.left);
  1130. if (symbol_conf.use_callchain) {
  1131. slsmg_printf("%c ", folded_sign);
  1132. width -= 2;
  1133. }
  1134. slsmg_write_nstring(s, width);
  1135. ++row;
  1136. ++printed;
  1137. } else
  1138. --row_offset;
  1139. if (folded_sign == '-' && row != self->b.height) {
  1140. printed += hist_browser__show_callchain(self, &entry->sorted_chain,
  1141. 1, row, &row_offset,
  1142. &current_entry);
  1143. if (current_entry)
  1144. self->he_selection = entry;
  1145. }
  1146. return printed;
  1147. }
  1148. static unsigned int hist_browser__refresh(struct ui_browser *self)
  1149. {
  1150. unsigned row = 0;
  1151. struct rb_node *nd;
  1152. struct hist_browser *hb = container_of(self, struct hist_browser, b);
  1153. if (self->first_visible_entry == NULL)
  1154. self->first_visible_entry = rb_first(&hb->hists->entries);
  1155. for (nd = self->first_visible_entry; nd; nd = rb_next(nd)) {
  1156. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1157. if (h->filtered)
  1158. continue;
  1159. row += hist_browser__show_entry(hb, h, row);
  1160. if (row == self->height)
  1161. break;
  1162. }
  1163. return row;
  1164. }
  1165. static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
  1166. {
  1167. struct rb_node *nd = rb_first(&self->rb_root);
  1168. for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
  1169. struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
  1170. struct callchain_list *chain;
  1171. int first = true;
  1172. list_for_each_entry(chain, &child->val, list) {
  1173. if (first) {
  1174. first = false;
  1175. chain->ms.has_children = chain->list.next != &child->val ||
  1176. rb_first(&child->rb_root) != NULL;
  1177. } else
  1178. chain->ms.has_children = chain->list.next == &child->val &&
  1179. rb_first(&child->rb_root) != NULL;
  1180. }
  1181. callchain_node__init_have_children_rb_tree(child);
  1182. }
  1183. }
  1184. static void callchain_node__init_have_children(struct callchain_node *self)
  1185. {
  1186. struct callchain_list *chain;
  1187. list_for_each_entry(chain, &self->val, list)
  1188. chain->ms.has_children = rb_first(&self->rb_root) != NULL;
  1189. callchain_node__init_have_children_rb_tree(self);
  1190. }
  1191. static void callchain__init_have_children(struct rb_root *self)
  1192. {
  1193. struct rb_node *nd;
  1194. for (nd = rb_first(self); nd; nd = rb_next(nd)) {
  1195. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  1196. callchain_node__init_have_children(node);
  1197. }
  1198. }
  1199. static void hist_entry__init_have_children(struct hist_entry *self)
  1200. {
  1201. if (!self->init_have_children) {
  1202. callchain__init_have_children(&self->sorted_chain);
  1203. self->init_have_children = true;
  1204. }
  1205. }
  1206. static struct rb_node *hists__filter_entries(struct rb_node *nd)
  1207. {
  1208. while (nd != NULL) {
  1209. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1210. if (!h->filtered)
  1211. return nd;
  1212. nd = rb_next(nd);
  1213. }
  1214. return NULL;
  1215. }
  1216. static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
  1217. {
  1218. while (nd != NULL) {
  1219. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1220. if (!h->filtered)
  1221. return nd;
  1222. nd = rb_prev(nd);
  1223. }
  1224. return NULL;
  1225. }
  1226. static void ui_browser__hists_seek(struct ui_browser *self,
  1227. off_t offset, int whence)
  1228. {
  1229. struct hist_entry *h;
  1230. struct rb_node *nd;
  1231. bool first = true;
  1232. switch (whence) {
  1233. case SEEK_SET:
  1234. nd = hists__filter_entries(rb_first(self->entries));
  1235. break;
  1236. case SEEK_CUR:
  1237. nd = self->first_visible_entry;
  1238. goto do_offset;
  1239. case SEEK_END:
  1240. nd = hists__filter_prev_entries(rb_last(self->entries));
  1241. first = false;
  1242. break;
  1243. default:
  1244. return;
  1245. }
  1246. /*
  1247. * Moves not relative to the first visible entry invalidates its
  1248. * row_offset:
  1249. */
  1250. h = rb_entry(self->first_visible_entry, struct hist_entry, rb_node);
  1251. h->row_offset = 0;
  1252. /*
  1253. * Here we have to check if nd is expanded (+), if it is we can't go
  1254. * the next top level hist_entry, instead we must compute an offset of
  1255. * what _not_ to show and not change the first visible entry.
  1256. *
  1257. * This offset increments when we are going from top to bottom and
  1258. * decreases when we're going from bottom to top.
  1259. *
  1260. * As we don't have backpointers to the top level in the callchains
  1261. * structure, we need to always print the whole hist_entry callchain,
  1262. * skipping the first ones that are before the first visible entry
  1263. * and stop when we printed enough lines to fill the screen.
  1264. */
  1265. do_offset:
  1266. if (offset > 0) {
  1267. do {
  1268. h = rb_entry(nd, struct hist_entry, rb_node);
  1269. if (h->ms.unfolded) {
  1270. u16 remaining = h->nr_rows - h->row_offset;
  1271. if (offset > remaining) {
  1272. offset -= remaining;
  1273. h->row_offset = 0;
  1274. } else {
  1275. h->row_offset += offset;
  1276. offset = 0;
  1277. self->first_visible_entry = nd;
  1278. break;
  1279. }
  1280. }
  1281. nd = hists__filter_entries(rb_next(nd));
  1282. if (nd == NULL)
  1283. break;
  1284. --offset;
  1285. self->first_visible_entry = nd;
  1286. } while (offset != 0);
  1287. } else if (offset < 0) {
  1288. while (1) {
  1289. h = rb_entry(nd, struct hist_entry, rb_node);
  1290. if (h->ms.unfolded) {
  1291. if (first) {
  1292. if (-offset > h->row_offset) {
  1293. offset += h->row_offset;
  1294. h->row_offset = 0;
  1295. } else {
  1296. h->row_offset += offset;
  1297. offset = 0;
  1298. self->first_visible_entry = nd;
  1299. break;
  1300. }
  1301. } else {
  1302. if (-offset > h->nr_rows) {
  1303. offset += h->nr_rows;
  1304. h->row_offset = 0;
  1305. } else {
  1306. h->row_offset = h->nr_rows + offset;
  1307. offset = 0;
  1308. self->first_visible_entry = nd;
  1309. break;
  1310. }
  1311. }
  1312. }
  1313. nd = hists__filter_prev_entries(rb_prev(nd));
  1314. if (nd == NULL)
  1315. break;
  1316. ++offset;
  1317. self->first_visible_entry = nd;
  1318. if (offset == 0) {
  1319. /*
  1320. * Last unfiltered hist_entry, check if it is
  1321. * unfolded, if it is then we should have
  1322. * row_offset at its last entry.
  1323. */
  1324. h = rb_entry(nd, struct hist_entry, rb_node);
  1325. if (h->ms.unfolded)
  1326. h->row_offset = h->nr_rows;
  1327. break;
  1328. }
  1329. first = false;
  1330. }
  1331. } else {
  1332. self->first_visible_entry = nd;
  1333. h = rb_entry(nd, struct hist_entry, rb_node);
  1334. h->row_offset = 0;
  1335. }
  1336. }
  1337. static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
  1338. {
  1339. int n = 0;
  1340. struct rb_node *nd;
  1341. for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
  1342. struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
  1343. struct callchain_list *chain;
  1344. char folded_sign = ' '; /* No children */
  1345. list_for_each_entry(chain, &child->val, list) {
  1346. ++n;
  1347. /* We need this because we may not have children */
  1348. folded_sign = callchain_list__folded(chain);
  1349. if (folded_sign == '+')
  1350. break;
  1351. }
  1352. if (folded_sign == '-') /* Have children and they're unfolded */
  1353. n += callchain_node__count_rows_rb_tree(child);
  1354. }
  1355. return n;
  1356. }
  1357. static int callchain_node__count_rows(struct callchain_node *node)
  1358. {
  1359. struct callchain_list *chain;
  1360. bool unfolded = false;
  1361. int n = 0;
  1362. list_for_each_entry(chain, &node->val, list) {
  1363. ++n;
  1364. unfolded = chain->ms.unfolded;
  1365. }
  1366. if (unfolded)
  1367. n += callchain_node__count_rows_rb_tree(node);
  1368. return n;
  1369. }
  1370. static int callchain__count_rows(struct rb_root *chain)
  1371. {
  1372. struct rb_node *nd;
  1373. int n = 0;
  1374. for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
  1375. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  1376. n += callchain_node__count_rows(node);
  1377. }
  1378. return n;
  1379. }
  1380. static bool hist_browser__toggle_fold(struct hist_browser *self)
  1381. {
  1382. if (map_symbol__toggle_fold(self->selection)) {
  1383. struct hist_entry *he = self->he_selection;
  1384. hist_entry__init_have_children(he);
  1385. self->hists->nr_entries -= he->nr_rows;
  1386. if (he->ms.unfolded)
  1387. he->nr_rows = callchain__count_rows(&he->sorted_chain);
  1388. else
  1389. he->nr_rows = 0;
  1390. self->hists->nr_entries += he->nr_rows;
  1391. self->b.nr_entries = self->hists->nr_entries;
  1392. return true;
  1393. }
  1394. /* If it doesn't have children, no toggling performed */
  1395. return false;
  1396. }
  1397. static int hist_browser__run(struct hist_browser *self, const char *title,
  1398. struct newtExitStruct *es)
  1399. {
  1400. char str[256], unit;
  1401. unsigned long nr_events = self->hists->stats.nr_events[PERF_RECORD_SAMPLE];
  1402. self->b.entries = &self->hists->entries;
  1403. self->b.nr_entries = self->hists->nr_entries;
  1404. hist_browser__refresh_dimensions(self);
  1405. nr_events = convert_unit(nr_events, &unit);
  1406. snprintf(str, sizeof(str), "Events: %lu%c ",
  1407. nr_events, unit);
  1408. newtDrawRootText(0, 0, str);
  1409. if (ui_browser__show(&self->b, title) < 0)
  1410. return -1;
  1411. newtFormAddHotKey(self->b.form, 'A');
  1412. newtFormAddHotKey(self->b.form, 'a');
  1413. newtFormAddHotKey(self->b.form, '?');
  1414. newtFormAddHotKey(self->b.form, 'h');
  1415. newtFormAddHotKey(self->b.form, 'H');
  1416. newtFormAddHotKey(self->b.form, 'd');
  1417. newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT);
  1418. newtFormAddHotKey(self->b.form, NEWT_KEY_RIGHT);
  1419. newtFormAddHotKey(self->b.form, NEWT_KEY_ENTER);
  1420. while (1) {
  1421. ui_browser__run(&self->b, es);
  1422. if (es->reason != NEWT_EXIT_HOTKEY)
  1423. break;
  1424. switch (es->u.key) {
  1425. case 'd': { /* Debug */
  1426. static int seq;
  1427. struct hist_entry *h = rb_entry(self->b.first_visible_entry,
  1428. struct hist_entry, rb_node);
  1429. ui_helpline__pop();
  1430. ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
  1431. seq++, self->b.nr_entries,
  1432. self->hists->nr_entries,
  1433. self->b.height,
  1434. self->b.index,
  1435. self->b.first_visible_entry_idx,
  1436. h->row_offset, h->nr_rows);
  1437. }
  1438. continue;
  1439. case NEWT_KEY_ENTER:
  1440. if (hist_browser__toggle_fold(self))
  1441. break;
  1442. /* fall thru */
  1443. default:
  1444. return 0;
  1445. }
  1446. }
  1447. return 0;
  1448. }