newt.c 42 KB

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