newt.c 37 KB

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