newt.c 34 KB

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