newt.c 27 KB

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