newt.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #undef _GNU_SOURCE
  4. /*
  5. * slang versions <= 2.0.6 have a "#if HAVE_LONG_LONG" that breaks
  6. * the build if it isn't defined. Use the equivalent one that glibc
  7. * has on features.h.
  8. */
  9. #include <features.h>
  10. #ifndef HAVE_LONG_LONG
  11. #define HAVE_LONG_LONG __GLIBC_HAVE_LONG_LONG
  12. #endif
  13. #include <slang.h>
  14. #include <stdlib.h>
  15. #include <newt.h>
  16. #include <sys/ttydefaults.h>
  17. #include "cache.h"
  18. #include "hist.h"
  19. #include "pstack.h"
  20. #include "session.h"
  21. #include "sort.h"
  22. #include "symbol.h"
  23. #if SLANG_VERSION < 20104
  24. #define slsmg_printf(msg, args...) SLsmg_printf((char *)msg, ##args)
  25. #define slsmg_write_nstring(msg, len) SLsmg_write_nstring((char *)msg, len)
  26. #define sltt_set_color(obj, name, fg, bg) SLtt_set_color(obj,(char *)name,\
  27. (char *)fg, (char *)bg)
  28. #else
  29. #define slsmg_printf SLsmg_printf
  30. #define slsmg_write_nstring SLsmg_write_nstring
  31. #define sltt_set_color SLtt_set_color
  32. #endif
  33. struct ui_progress {
  34. newtComponent form, scale;
  35. };
  36. struct ui_progress *ui_progress__new(const char *title, u64 total)
  37. {
  38. struct ui_progress *self = malloc(sizeof(*self));
  39. if (self != NULL) {
  40. int cols;
  41. if (use_browser <= 0)
  42. return self;
  43. newtGetScreenSize(&cols, NULL);
  44. cols -= 4;
  45. newtCenteredWindow(cols, 1, title);
  46. self->form = newtForm(NULL, NULL, 0);
  47. if (self->form == NULL)
  48. goto out_free_self;
  49. self->scale = newtScale(0, 0, cols, total);
  50. if (self->scale == NULL)
  51. goto out_free_form;
  52. newtFormAddComponent(self->form, self->scale);
  53. newtRefresh();
  54. }
  55. return self;
  56. out_free_form:
  57. newtFormDestroy(self->form);
  58. out_free_self:
  59. free(self);
  60. return NULL;
  61. }
  62. void ui_progress__update(struct ui_progress *self, u64 curr)
  63. {
  64. /*
  65. * FIXME: We should have a per UI backend way of showing progress,
  66. * stdio will just show a percentage as NN%, etc.
  67. */
  68. if (use_browser <= 0)
  69. return;
  70. newtScaleSet(self->scale, curr);
  71. newtRefresh();
  72. }
  73. void ui_progress__delete(struct ui_progress *self)
  74. {
  75. if (use_browser > 0) {
  76. newtFormDestroy(self->form);
  77. newtPopWindow();
  78. }
  79. free(self);
  80. }
  81. static void ui_helpline__pop(void)
  82. {
  83. newtPopHelpLine();
  84. }
  85. static void ui_helpline__push(const char *msg)
  86. {
  87. newtPushHelpLine(msg);
  88. }
  89. static void ui_helpline__vpush(const char *fmt, va_list ap)
  90. {
  91. char *s;
  92. if (vasprintf(&s, fmt, ap) < 0)
  93. vfprintf(stderr, fmt, ap);
  94. else {
  95. ui_helpline__push(s);
  96. free(s);
  97. }
  98. }
  99. static void ui_helpline__fpush(const char *fmt, ...)
  100. {
  101. va_list ap;
  102. va_start(ap, fmt);
  103. ui_helpline__vpush(fmt, ap);
  104. va_end(ap);
  105. }
  106. static void ui_helpline__puts(const char *msg)
  107. {
  108. ui_helpline__pop();
  109. ui_helpline__push(msg);
  110. }
  111. static char browser__last_msg[1024];
  112. int browser__show_help(const char *format, va_list ap)
  113. {
  114. int ret;
  115. static int backlog;
  116. ret = vsnprintf(browser__last_msg + backlog,
  117. sizeof(browser__last_msg) - backlog, format, ap);
  118. backlog += ret;
  119. if (browser__last_msg[backlog - 1] == '\n') {
  120. ui_helpline__puts(browser__last_msg);
  121. newtRefresh();
  122. backlog = 0;
  123. }
  124. return ret;
  125. }
  126. static void newt_form__set_exit_keys(newtComponent self)
  127. {
  128. newtFormAddHotKey(self, NEWT_KEY_LEFT);
  129. newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
  130. newtFormAddHotKey(self, 'Q');
  131. newtFormAddHotKey(self, 'q');
  132. newtFormAddHotKey(self, CTRL('c'));
  133. }
  134. static newtComponent newt_form__new(void)
  135. {
  136. newtComponent self = newtForm(NULL, NULL, 0);
  137. if (self)
  138. newt_form__set_exit_keys(self);
  139. return self;
  140. }
  141. static int popup_menu(int argc, char * const argv[])
  142. {
  143. struct newtExitStruct es;
  144. int i, rc = -1, max_len = 5;
  145. newtComponent listbox, form = newt_form__new();
  146. if (form == NULL)
  147. return -1;
  148. listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
  149. if (listbox == NULL)
  150. goto out_destroy_form;
  151. newtFormAddComponent(form, listbox);
  152. for (i = 0; i < argc; ++i) {
  153. int len = strlen(argv[i]);
  154. if (len > max_len)
  155. max_len = len;
  156. if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
  157. goto out_destroy_form;
  158. }
  159. newtCenteredWindow(max_len, argc, NULL);
  160. newtFormRun(form, &es);
  161. rc = newtListboxGetCurrent(listbox) - NULL;
  162. if (es.reason == NEWT_EXIT_HOTKEY)
  163. rc = -1;
  164. newtPopWindow();
  165. out_destroy_form:
  166. newtFormDestroy(form);
  167. return rc;
  168. }
  169. static int ui__help_window(const char *text)
  170. {
  171. struct newtExitStruct es;
  172. newtComponent tb, form = newt_form__new();
  173. int rc = -1;
  174. int max_len = 0, nr_lines = 0;
  175. const char *t;
  176. if (form == NULL)
  177. return -1;
  178. t = text;
  179. while (1) {
  180. const char *sep = strchr(t, '\n');
  181. int len;
  182. if (sep == NULL)
  183. sep = strchr(t, '\0');
  184. len = sep - t;
  185. if (max_len < len)
  186. max_len = len;
  187. ++nr_lines;
  188. if (*sep == '\0')
  189. break;
  190. t = sep + 1;
  191. }
  192. tb = newtTextbox(0, 0, max_len, nr_lines, 0);
  193. if (tb == NULL)
  194. goto out_destroy_form;
  195. newtTextboxSetText(tb, text);
  196. newtFormAddComponent(form, tb);
  197. newtCenteredWindow(max_len, nr_lines, NULL);
  198. newtFormRun(form, &es);
  199. newtPopWindow();
  200. rc = 0;
  201. out_destroy_form:
  202. newtFormDestroy(form);
  203. return rc;
  204. }
  205. static bool dialog_yesno(const char *msg)
  206. {
  207. /* newtWinChoice should really be accepting const char pointers... */
  208. char yes[] = "Yes", no[] = "No";
  209. return newtWinChoice(NULL, yes, no, (char *)msg) == 1;
  210. }
  211. static void ui__error_window(const char *fmt, ...)
  212. {
  213. va_list ap;
  214. va_start(ap, fmt);
  215. newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
  216. va_end(ap);
  217. }
  218. #define HE_COLORSET_TOP 50
  219. #define HE_COLORSET_MEDIUM 51
  220. #define HE_COLORSET_NORMAL 52
  221. #define HE_COLORSET_SELECTED 53
  222. #define HE_COLORSET_CODE 54
  223. static int ui_browser__percent_color(double percent, bool current)
  224. {
  225. if (current)
  226. return HE_COLORSET_SELECTED;
  227. if (percent >= MIN_RED)
  228. return HE_COLORSET_TOP;
  229. if (percent >= MIN_GREEN)
  230. return HE_COLORSET_MEDIUM;
  231. return HE_COLORSET_NORMAL;
  232. }
  233. struct ui_browser {
  234. newtComponent form, sb;
  235. u64 index, first_visible_entry_idx;
  236. void *first_visible_entry, *entries;
  237. u16 top, left, width, height;
  238. void *priv;
  239. u32 nr_entries;
  240. };
  241. static void ui_browser__refresh_dimensions(struct ui_browser *self)
  242. {
  243. int cols, rows;
  244. newtGetScreenSize(&cols, &rows);
  245. if (self->width > cols - 4)
  246. self->width = cols - 4;
  247. self->height = rows - 5;
  248. if (self->height > self->nr_entries)
  249. self->height = self->nr_entries;
  250. self->top = (rows - self->height) / 2;
  251. self->left = (cols - self->width) / 2;
  252. }
  253. static void ui_browser__reset_index(struct ui_browser *self)
  254. {
  255. self->index = self->first_visible_entry_idx = 0;
  256. self->first_visible_entry = NULL;
  257. }
  258. static int objdump_line__show(struct objdump_line *self, struct list_head *head,
  259. int width, struct hist_entry *he, int len,
  260. bool current_entry)
  261. {
  262. if (self->offset != -1) {
  263. struct symbol *sym = he->ms.sym;
  264. unsigned int hits = 0;
  265. double percent = 0.0;
  266. int color;
  267. struct sym_priv *priv = symbol__priv(sym);
  268. struct sym_ext *sym_ext = priv->ext;
  269. struct sym_hist *h = priv->hist;
  270. s64 offset = self->offset;
  271. struct objdump_line *next = objdump__get_next_ip_line(head, self);
  272. while (offset < (s64)len &&
  273. (next == NULL || offset < next->offset)) {
  274. if (sym_ext) {
  275. percent += sym_ext[offset].percent;
  276. } else
  277. hits += h->ip[offset];
  278. ++offset;
  279. }
  280. if (sym_ext == NULL && h->sum)
  281. percent = 100.0 * hits / h->sum;
  282. color = ui_browser__percent_color(percent, current_entry);
  283. SLsmg_set_color(color);
  284. slsmg_printf(" %7.2f ", percent);
  285. if (!current_entry)
  286. SLsmg_set_color(HE_COLORSET_CODE);
  287. } else {
  288. int color = ui_browser__percent_color(0, current_entry);
  289. SLsmg_set_color(color);
  290. slsmg_write_nstring(" ", 9);
  291. }
  292. SLsmg_write_char(':');
  293. slsmg_write_nstring(" ", 8);
  294. if (!*self->line)
  295. slsmg_write_nstring(" ", width - 18);
  296. else
  297. slsmg_write_nstring(self->line, width - 18);
  298. return 0;
  299. }
  300. static int ui_browser__refresh_entries(struct ui_browser *self)
  301. {
  302. struct objdump_line *pos;
  303. struct list_head *head = self->entries;
  304. struct hist_entry *he = self->priv;
  305. int row = 0;
  306. int len = he->ms.sym->end - he->ms.sym->start;
  307. if (self->first_visible_entry == NULL || self->first_visible_entry == self->entries)
  308. self->first_visible_entry = head->next;
  309. pos = list_entry(self->first_visible_entry, struct objdump_line, node);
  310. list_for_each_entry_from(pos, head, node) {
  311. bool current_entry = (self->first_visible_entry_idx + row) == self->index;
  312. SLsmg_gotorc(self->top + row, self->left);
  313. objdump_line__show(pos, head, self->width,
  314. he, len, current_entry);
  315. if (++row == self->height)
  316. break;
  317. }
  318. SLsmg_set_color(HE_COLORSET_NORMAL);
  319. SLsmg_fill_region(self->top + row, self->left,
  320. self->height - row, self->width, ' ');
  321. return 0;
  322. }
  323. static int ui_browser__run(struct ui_browser *self, const char *title,
  324. struct newtExitStruct *es)
  325. {
  326. if (self->form) {
  327. newtFormDestroy(self->form);
  328. newtPopWindow();
  329. }
  330. ui_browser__refresh_dimensions(self);
  331. newtCenteredWindow(self->width + 2, self->height, title);
  332. self->form = newt_form__new();
  333. if (self->form == NULL)
  334. return -1;
  335. self->sb = newtVerticalScrollbar(self->width + 1, 0, self->height,
  336. HE_COLORSET_NORMAL,
  337. HE_COLORSET_SELECTED);
  338. if (self->sb == NULL)
  339. return -1;
  340. newtFormAddHotKey(self->form, NEWT_KEY_UP);
  341. newtFormAddHotKey(self->form, NEWT_KEY_DOWN);
  342. newtFormAddHotKey(self->form, NEWT_KEY_PGUP);
  343. newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
  344. newtFormAddHotKey(self->form, ' ');
  345. newtFormAddHotKey(self->form, NEWT_KEY_HOME);
  346. newtFormAddHotKey(self->form, NEWT_KEY_END);
  347. newtFormAddHotKey(self->form, NEWT_KEY_TAB);
  348. newtFormAddHotKey(self->form, NEWT_KEY_RIGHT);
  349. if (ui_browser__refresh_entries(self) < 0)
  350. return -1;
  351. newtFormAddComponent(self->form, self->sb);
  352. while (1) {
  353. unsigned int offset;
  354. newtFormRun(self->form, es);
  355. if (es->reason != NEWT_EXIT_HOTKEY)
  356. break;
  357. if (is_exit_key(es->u.key))
  358. return es->u.key;
  359. switch (es->u.key) {
  360. case NEWT_KEY_DOWN:
  361. if (self->index == self->nr_entries - 1)
  362. break;
  363. ++self->index;
  364. if (self->index == self->first_visible_entry_idx + self->height) {
  365. struct list_head *pos = self->first_visible_entry;
  366. ++self->first_visible_entry_idx;
  367. self->first_visible_entry = pos->next;
  368. }
  369. break;
  370. case NEWT_KEY_UP:
  371. if (self->index == 0)
  372. break;
  373. --self->index;
  374. if (self->index < self->first_visible_entry_idx) {
  375. struct list_head *pos = self->first_visible_entry;
  376. --self->first_visible_entry_idx;
  377. self->first_visible_entry = pos->prev;
  378. }
  379. break;
  380. case NEWT_KEY_PGDN:
  381. case ' ':
  382. if (self->first_visible_entry_idx + self->height > self->nr_entries - 1)
  383. break;
  384. offset = self->height;
  385. if (self->index + offset > self->nr_entries - 1)
  386. offset = self->nr_entries - 1 - self->index;
  387. self->index += offset;
  388. self->first_visible_entry_idx += offset;
  389. while (offset--) {
  390. struct list_head *pos = self->first_visible_entry;
  391. self->first_visible_entry = pos->next;
  392. }
  393. break;
  394. case NEWT_KEY_PGUP:
  395. if (self->first_visible_entry_idx == 0)
  396. break;
  397. if (self->first_visible_entry_idx < self->height)
  398. offset = self->first_visible_entry_idx;
  399. else
  400. offset = self->height;
  401. self->index -= offset;
  402. self->first_visible_entry_idx -= offset;
  403. while (offset--) {
  404. struct list_head *pos = self->first_visible_entry;
  405. self->first_visible_entry = pos->prev;
  406. }
  407. break;
  408. case NEWT_KEY_HOME:
  409. ui_browser__reset_index(self);
  410. break;
  411. case NEWT_KEY_END: {
  412. struct list_head *head = self->entries;
  413. offset = self->height - 1;
  414. if (offset > self->nr_entries)
  415. offset = self->nr_entries;
  416. self->index = self->first_visible_entry_idx = self->nr_entries - 1 - offset;
  417. self->first_visible_entry = head->prev;
  418. while (offset-- != 0) {
  419. struct list_head *pos = self->first_visible_entry;
  420. self->first_visible_entry = pos->prev;
  421. }
  422. }
  423. break;
  424. case NEWT_KEY_RIGHT:
  425. case NEWT_KEY_LEFT:
  426. case NEWT_KEY_TAB:
  427. return es->u.key;
  428. default:
  429. continue;
  430. }
  431. if (ui_browser__refresh_entries(self) < 0)
  432. return -1;
  433. }
  434. return 0;
  435. }
  436. /*
  437. * When debugging newt problems it was useful to be able to "unroll"
  438. * the calls to newtCheckBoxTreeAdd{Array,Item}, so that we can generate
  439. * a source file with the sequence of calls to these methods, to then
  440. * tweak the arrays to get the intended results, so I'm keeping this code
  441. * here, may be useful again in the future.
  442. */
  443. #undef NEWT_DEBUG
  444. static void newt_checkbox_tree__add(newtComponent tree, const char *str,
  445. void *priv, int *indexes)
  446. {
  447. #ifdef NEWT_DEBUG
  448. /* Print the newtCheckboxTreeAddArray to tinker with its index arrays */
  449. int i = 0, len = 40 - strlen(str);
  450. fprintf(stderr,
  451. "\tnewtCheckboxTreeAddItem(tree, %*.*s\"%s\", (void *)%p, 0, ",
  452. len, len, " ", str, priv);
  453. while (indexes[i] != NEWT_ARG_LAST) {
  454. if (indexes[i] != NEWT_ARG_APPEND)
  455. fprintf(stderr, " %d,", indexes[i]);
  456. else
  457. fprintf(stderr, " %s,", "NEWT_ARG_APPEND");
  458. ++i;
  459. }
  460. fprintf(stderr, " %s", " NEWT_ARG_LAST);\n");
  461. fflush(stderr);
  462. #endif
  463. newtCheckboxTreeAddArray(tree, str, priv, 0, indexes);
  464. }
  465. static char *callchain_list__sym_name(struct callchain_list *self,
  466. char *bf, size_t bfsize)
  467. {
  468. if (self->ms.sym)
  469. return self->ms.sym->name;
  470. snprintf(bf, bfsize, "%#Lx", self->ip);
  471. return bf;
  472. }
  473. static void __callchain__append_graph_browser(struct callchain_node *self,
  474. newtComponent tree, u64 total,
  475. int *indexes, int depth)
  476. {
  477. struct rb_node *node;
  478. u64 new_total, remaining;
  479. int idx = 0;
  480. if (callchain_param.mode == CHAIN_GRAPH_REL)
  481. new_total = self->children_hit;
  482. else
  483. new_total = total;
  484. remaining = new_total;
  485. node = rb_first(&self->rb_root);
  486. while (node) {
  487. struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
  488. struct rb_node *next = rb_next(node);
  489. u64 cumul = cumul_hits(child);
  490. struct callchain_list *chain;
  491. int first = true, printed = 0;
  492. int chain_idx = -1;
  493. remaining -= cumul;
  494. indexes[depth] = NEWT_ARG_APPEND;
  495. indexes[depth + 1] = NEWT_ARG_LAST;
  496. list_for_each_entry(chain, &child->val, list) {
  497. char ipstr[BITS_PER_LONG / 4 + 1],
  498. *alloc_str = NULL;
  499. const char *str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  500. if (first) {
  501. double percent = cumul * 100.0 / new_total;
  502. first = false;
  503. if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
  504. str = "Not enough memory!";
  505. else
  506. str = alloc_str;
  507. } else {
  508. indexes[depth] = idx;
  509. indexes[depth + 1] = NEWT_ARG_APPEND;
  510. indexes[depth + 2] = NEWT_ARG_LAST;
  511. ++chain_idx;
  512. }
  513. newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
  514. free(alloc_str);
  515. ++printed;
  516. }
  517. indexes[depth] = idx;
  518. if (chain_idx != -1)
  519. indexes[depth + 1] = chain_idx;
  520. if (printed != 0)
  521. ++idx;
  522. __callchain__append_graph_browser(child, tree, new_total, indexes,
  523. depth + (chain_idx != -1 ? 2 : 1));
  524. node = next;
  525. }
  526. }
  527. static void callchain__append_graph_browser(struct callchain_node *self,
  528. newtComponent tree, u64 total,
  529. int *indexes, int parent_idx)
  530. {
  531. struct callchain_list *chain;
  532. int i = 0;
  533. indexes[1] = NEWT_ARG_APPEND;
  534. indexes[2] = NEWT_ARG_LAST;
  535. list_for_each_entry(chain, &self->val, list) {
  536. char ipstr[BITS_PER_LONG / 4 + 1], *str;
  537. if (chain->ip >= PERF_CONTEXT_MAX)
  538. continue;
  539. if (!i++ && sort__first_dimension == SORT_SYM)
  540. continue;
  541. str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  542. newt_checkbox_tree__add(tree, str, &chain->ms, indexes);
  543. }
  544. indexes[1] = parent_idx;
  545. indexes[2] = NEWT_ARG_APPEND;
  546. indexes[3] = NEWT_ARG_LAST;
  547. __callchain__append_graph_browser(self, tree, total, indexes, 2);
  548. }
  549. static void hist_entry__append_callchain_browser(struct hist_entry *self,
  550. newtComponent tree, u64 total, int parent_idx)
  551. {
  552. struct rb_node *rb_node;
  553. int indexes[1024] = { [0] = parent_idx, };
  554. int idx = 0;
  555. struct callchain_node *chain;
  556. rb_node = rb_first(&self->sorted_chain);
  557. while (rb_node) {
  558. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  559. switch (callchain_param.mode) {
  560. case CHAIN_FLAT:
  561. break;
  562. case CHAIN_GRAPH_ABS: /* falldown */
  563. case CHAIN_GRAPH_REL:
  564. callchain__append_graph_browser(chain, tree, total, indexes, idx++);
  565. break;
  566. case CHAIN_NONE:
  567. default:
  568. break;
  569. }
  570. rb_node = rb_next(rb_node);
  571. }
  572. }
  573. static size_t hist_entry__append_browser(struct hist_entry *self,
  574. newtComponent tree, u64 total)
  575. {
  576. char s[256];
  577. size_t ret;
  578. if (symbol_conf.exclude_other && !self->parent)
  579. return 0;
  580. ret = hist_entry__snprintf(self, s, sizeof(s), NULL,
  581. false, 0, false, total);
  582. if (symbol_conf.use_callchain) {
  583. int indexes[2];
  584. indexes[0] = NEWT_ARG_APPEND;
  585. indexes[1] = NEWT_ARG_LAST;
  586. newt_checkbox_tree__add(tree, s, &self->ms, indexes);
  587. } else
  588. newtListboxAppendEntry(tree, s, &self->ms);
  589. return ret;
  590. }
  591. int hist_entry__tui_annotate(struct hist_entry *self)
  592. {
  593. struct ui_browser browser;
  594. struct newtExitStruct es;
  595. struct objdump_line *pos, *n;
  596. LIST_HEAD(head);
  597. int ret;
  598. if (self->ms.sym == NULL)
  599. return -1;
  600. if (self->ms.map->dso->annotate_warned)
  601. return -1;
  602. if (hist_entry__annotate(self, &head) < 0) {
  603. ui__error_window(browser__last_msg);
  604. return -1;
  605. }
  606. ui_helpline__push("Press <- or ESC to exit");
  607. memset(&browser, 0, sizeof(browser));
  608. browser.entries = &head;
  609. browser.priv = self;
  610. list_for_each_entry(pos, &head, node) {
  611. size_t line_len = strlen(pos->line);
  612. if (browser.width < line_len)
  613. browser.width = line_len;
  614. ++browser.nr_entries;
  615. }
  616. browser.width += 18; /* Percentage */
  617. ret = ui_browser__run(&browser, self->ms.sym->name, &es);
  618. newtFormDestroy(browser.form);
  619. newtPopWindow();
  620. list_for_each_entry_safe(pos, n, &head, node) {
  621. list_del(&pos->node);
  622. objdump_line__free(pos);
  623. }
  624. ui_helpline__pop();
  625. return ret;
  626. }
  627. static const void *newt__symbol_tree_get_current(newtComponent self)
  628. {
  629. if (symbol_conf.use_callchain)
  630. return newtCheckboxTreeGetCurrent(self);
  631. return newtListboxGetCurrent(self);
  632. }
  633. static void hist_browser__selection(newtComponent self, void *data)
  634. {
  635. const struct map_symbol **symbol_ptr = data;
  636. *symbol_ptr = newt__symbol_tree_get_current(self);
  637. }
  638. struct hist_browser {
  639. newtComponent form, tree;
  640. const struct map_symbol *selection;
  641. };
  642. static struct hist_browser *hist_browser__new(void)
  643. {
  644. struct hist_browser *self = malloc(sizeof(*self));
  645. if (self != NULL)
  646. self->form = NULL;
  647. return self;
  648. }
  649. static void hist_browser__delete(struct hist_browser *self)
  650. {
  651. newtFormDestroy(self->form);
  652. newtPopWindow();
  653. free(self);
  654. }
  655. static int hist_browser__populate(struct hist_browser *self, struct hists *hists,
  656. const char *title)
  657. {
  658. int max_len = 0, idx, cols, rows;
  659. struct ui_progress *progress;
  660. struct rb_node *nd;
  661. u64 curr_hist = 0;
  662. char seq[] = ".", unit;
  663. char str[256];
  664. unsigned long nr_events = hists->stats.nr_events[PERF_RECORD_SAMPLE];
  665. if (self->form) {
  666. newtFormDestroy(self->form);
  667. newtPopWindow();
  668. }
  669. nr_events = convert_unit(nr_events, &unit);
  670. snprintf(str, sizeof(str), "Events: %lu%c ",
  671. nr_events, unit);
  672. newtDrawRootText(0, 0, str);
  673. newtGetScreenSize(NULL, &rows);
  674. if (symbol_conf.use_callchain)
  675. self->tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq,
  676. NEWT_FLAG_SCROLL);
  677. else
  678. self->tree = newtListbox(0, 0, rows - 5,
  679. (NEWT_FLAG_SCROLL |
  680. NEWT_FLAG_RETURNEXIT));
  681. newtComponentAddCallback(self->tree, hist_browser__selection,
  682. &self->selection);
  683. progress = ui_progress__new("Adding entries to the browser...",
  684. hists->nr_entries);
  685. if (progress == NULL)
  686. return -1;
  687. idx = 0;
  688. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  689. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  690. int len;
  691. if (h->filtered)
  692. continue;
  693. len = hist_entry__append_browser(h, self->tree, hists->stats.total_period);
  694. if (len > max_len)
  695. max_len = len;
  696. if (symbol_conf.use_callchain)
  697. hist_entry__append_callchain_browser(h, self->tree,
  698. hists->stats.total_period, idx++);
  699. ++curr_hist;
  700. if (curr_hist % 5)
  701. ui_progress__update(progress, curr_hist);
  702. }
  703. ui_progress__delete(progress);
  704. newtGetScreenSize(&cols, &rows);
  705. if (max_len > cols)
  706. max_len = cols - 3;
  707. if (!symbol_conf.use_callchain)
  708. newtListboxSetWidth(self->tree, max_len);
  709. newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0),
  710. rows - 5, title);
  711. self->form = newt_form__new();
  712. if (self->form == NULL)
  713. return -1;
  714. newtFormAddHotKey(self->form, 'A');
  715. newtFormAddHotKey(self->form, 'a');
  716. newtFormAddHotKey(self->form, 'D');
  717. newtFormAddHotKey(self->form, 'd');
  718. newtFormAddHotKey(self->form, 'T');
  719. newtFormAddHotKey(self->form, 't');
  720. newtFormAddHotKey(self->form, '?');
  721. newtFormAddHotKey(self->form, 'H');
  722. newtFormAddHotKey(self->form, 'h');
  723. newtFormAddHotKey(self->form, NEWT_KEY_F1);
  724. newtFormAddHotKey(self->form, NEWT_KEY_RIGHT);
  725. newtFormAddHotKey(self->form, NEWT_KEY_TAB);
  726. newtFormAddHotKey(self->form, NEWT_KEY_UNTAB);
  727. newtFormAddComponents(self->form, self->tree, NULL);
  728. self->selection = newt__symbol_tree_get_current(self->tree);
  729. return 0;
  730. }
  731. static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
  732. {
  733. int *indexes;
  734. if (!symbol_conf.use_callchain)
  735. goto out;
  736. indexes = newtCheckboxTreeFindItem(self->tree, (void *)self->selection);
  737. if (indexes) {
  738. bool is_hist_entry = indexes[1] == NEWT_ARG_LAST;
  739. free(indexes);
  740. if (is_hist_entry)
  741. goto out;
  742. }
  743. return NULL;
  744. out:
  745. return container_of(self->selection, struct hist_entry, ms);
  746. }
  747. static struct thread *hist_browser__selected_thread(struct hist_browser *self)
  748. {
  749. struct hist_entry *he = hist_browser__selected_entry(self);
  750. return he ? he->thread : NULL;
  751. }
  752. static int hist_browser__title(char *bf, size_t size, const char *ev_name,
  753. const struct dso *dso, const struct thread *thread)
  754. {
  755. int printed = 0;
  756. if (thread)
  757. printed += snprintf(bf + printed, size - printed,
  758. "Thread: %s(%d)",
  759. (thread->comm_set ? thread->comm : ""),
  760. thread->pid);
  761. if (dso)
  762. printed += snprintf(bf + printed, size - printed,
  763. "%sDSO: %s", thread ? " " : "",
  764. dso->short_name);
  765. return printed ?: snprintf(bf, size, "Event: %s", ev_name);
  766. }
  767. int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
  768. {
  769. struct hist_browser *browser = hist_browser__new();
  770. struct pstack *fstack;
  771. const struct thread *thread_filter = NULL;
  772. const struct dso *dso_filter = NULL;
  773. struct newtExitStruct es;
  774. char msg[160];
  775. int key = -1;
  776. if (browser == NULL)
  777. return -1;
  778. fstack = pstack__new(2);
  779. if (fstack == NULL)
  780. goto out;
  781. ui_helpline__push(helpline);
  782. hist_browser__title(msg, sizeof(msg), ev_name,
  783. dso_filter, thread_filter);
  784. if (hist_browser__populate(browser, self, msg) < 0)
  785. goto out_free_stack;
  786. while (1) {
  787. const struct thread *thread;
  788. const struct dso *dso;
  789. char *options[16];
  790. int nr_options = 0, choice = 0, i,
  791. annotate = -2, zoom_dso = -2, zoom_thread = -2;
  792. newtFormRun(browser->form, &es);
  793. thread = hist_browser__selected_thread(browser);
  794. dso = browser->selection->map ? browser->selection->map->dso : NULL;
  795. if (es.reason == NEWT_EXIT_HOTKEY) {
  796. key = es.u.key;
  797. switch (key) {
  798. case NEWT_KEY_F1:
  799. goto do_help;
  800. case NEWT_KEY_TAB:
  801. case NEWT_KEY_UNTAB:
  802. /*
  803. * Exit the browser, let hists__browser_tree
  804. * go to the next or previous
  805. */
  806. goto out_free_stack;
  807. default:;
  808. }
  809. key = toupper(key);
  810. switch (key) {
  811. case 'A':
  812. if (browser->selection->map == NULL &&
  813. browser->selection->map->dso->annotate_warned)
  814. continue;
  815. goto do_annotate;
  816. case 'D':
  817. goto zoom_dso;
  818. case 'T':
  819. goto zoom_thread;
  820. case 'H':
  821. case '?':
  822. do_help:
  823. ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
  824. "<- Zoom out\n"
  825. "a Annotate current symbol\n"
  826. "h/?/F1 Show this window\n"
  827. "d Zoom into current DSO\n"
  828. "t Zoom into current Thread\n"
  829. "q/CTRL+C Exit browser");
  830. continue;
  831. default:;
  832. }
  833. if (is_exit_key(key)) {
  834. if (key == NEWT_KEY_ESCAPE) {
  835. if (dialog_yesno("Do you really want to exit?"))
  836. break;
  837. else
  838. continue;
  839. } else
  840. break;
  841. }
  842. if (es.u.key == NEWT_KEY_LEFT) {
  843. const void *top;
  844. if (pstack__empty(fstack))
  845. continue;
  846. top = pstack__pop(fstack);
  847. if (top == &dso_filter)
  848. goto zoom_out_dso;
  849. if (top == &thread_filter)
  850. goto zoom_out_thread;
  851. continue;
  852. }
  853. }
  854. if (browser->selection->sym != NULL &&
  855. !browser->selection->map->dso->annotate_warned &&
  856. asprintf(&options[nr_options], "Annotate %s",
  857. browser->selection->sym->name) > 0)
  858. annotate = nr_options++;
  859. if (thread != NULL &&
  860. asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
  861. (thread_filter ? "out of" : "into"),
  862. (thread->comm_set ? thread->comm : ""),
  863. thread->pid) > 0)
  864. zoom_thread = nr_options++;
  865. if (dso != NULL &&
  866. asprintf(&options[nr_options], "Zoom %s %s DSO",
  867. (dso_filter ? "out of" : "into"),
  868. (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
  869. zoom_dso = nr_options++;
  870. options[nr_options++] = (char *)"Exit";
  871. choice = popup_menu(nr_options, options);
  872. for (i = 0; i < nr_options - 1; ++i)
  873. free(options[i]);
  874. if (choice == nr_options - 1)
  875. break;
  876. if (choice == -1)
  877. continue;
  878. if (choice == annotate) {
  879. struct hist_entry *he;
  880. do_annotate:
  881. if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
  882. browser->selection->map->dso->annotate_warned = 1;
  883. ui_helpline__puts("No vmlinux file found, can't "
  884. "annotate with just a "
  885. "kallsyms file");
  886. continue;
  887. }
  888. he = hist_browser__selected_entry(browser);
  889. if (he == NULL)
  890. continue;
  891. hist_entry__tui_annotate(he);
  892. } else if (choice == zoom_dso) {
  893. zoom_dso:
  894. if (dso_filter) {
  895. pstack__remove(fstack, &dso_filter);
  896. zoom_out_dso:
  897. ui_helpline__pop();
  898. dso_filter = NULL;
  899. } else {
  900. if (dso == NULL)
  901. continue;
  902. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
  903. dso->kernel ? "the Kernel" : dso->short_name);
  904. dso_filter = dso;
  905. pstack__push(fstack, &dso_filter);
  906. }
  907. hists__filter_by_dso(self, dso_filter);
  908. hist_browser__title(msg, sizeof(msg), ev_name,
  909. dso_filter, thread_filter);
  910. if (hist_browser__populate(browser, self, msg) < 0)
  911. goto out;
  912. } else if (choice == zoom_thread) {
  913. zoom_thread:
  914. if (thread_filter) {
  915. pstack__remove(fstack, &thread_filter);
  916. zoom_out_thread:
  917. ui_helpline__pop();
  918. thread_filter = NULL;
  919. } else {
  920. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
  921. thread->comm_set ? thread->comm : "",
  922. thread->pid);
  923. thread_filter = thread;
  924. pstack__push(fstack, &thread_filter);
  925. }
  926. hists__filter_by_thread(self, thread_filter);
  927. hist_browser__title(msg, sizeof(msg), ev_name,
  928. dso_filter, thread_filter);
  929. if (hist_browser__populate(browser, self, msg) < 0)
  930. goto out;
  931. }
  932. }
  933. out_free_stack:
  934. pstack__delete(fstack);
  935. out:
  936. hist_browser__delete(browser);
  937. return key;
  938. }
  939. int hists__tui_browse_tree(struct rb_root *self, const char *help)
  940. {
  941. struct rb_node *first = rb_first(self), *nd = first, *next;
  942. int key = 0;
  943. while (nd) {
  944. struct hists *hists = rb_entry(nd, struct hists, rb_node);
  945. const char *ev_name = __event_name(hists->type, hists->config);
  946. key = hists__browse(hists, help, ev_name);
  947. if (is_exit_key(key))
  948. break;
  949. switch (key) {
  950. case NEWT_KEY_TAB:
  951. next = rb_next(nd);
  952. if (next)
  953. nd = next;
  954. break;
  955. case NEWT_KEY_UNTAB:
  956. if (nd == first)
  957. continue;
  958. nd = rb_prev(nd);
  959. default:
  960. break;
  961. }
  962. }
  963. return key;
  964. }
  965. static struct newtPercentTreeColors {
  966. const char *topColorFg, *topColorBg;
  967. const char *mediumColorFg, *mediumColorBg;
  968. const char *normalColorFg, *normalColorBg;
  969. const char *selColorFg, *selColorBg;
  970. const char *codeColorFg, *codeColorBg;
  971. } defaultPercentTreeColors = {
  972. "red", "lightgray",
  973. "green", "lightgray",
  974. "black", "lightgray",
  975. "lightgray", "magenta",
  976. "blue", "lightgray",
  977. };
  978. void setup_browser(void)
  979. {
  980. struct newtPercentTreeColors *c = &defaultPercentTreeColors;
  981. if (!isatty(1) || !use_browser || dump_trace) {
  982. use_browser = 0;
  983. setup_pager();
  984. return;
  985. }
  986. use_browser = 1;
  987. newtInit();
  988. newtCls();
  989. ui_helpline__puts(" ");
  990. sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg);
  991. sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg);
  992. sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg);
  993. sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg);
  994. sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg);
  995. }
  996. void exit_browser(bool wait_for_ok)
  997. {
  998. if (use_browser > 0) {
  999. if (wait_for_ok) {
  1000. char title[] = "Fatal Error", ok[] = "Ok";
  1001. newtWinMessage(title, ok, browser__last_msg);
  1002. }
  1003. newtFinished();
  1004. }
  1005. }