newt.c 35 KB

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