newt.c 37 KB

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