newt.c 42 KB

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