newt.c 28 KB

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