hists.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #undef _GNU_SOURCE
  4. #include "../libslang.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <newt.h>
  8. #include <linux/rbtree.h>
  9. #include "../../hist.h"
  10. #include "../../pstack.h"
  11. #include "../../sort.h"
  12. #include "../../util.h"
  13. #include "../browser.h"
  14. #include "../helpline.h"
  15. #include "../util.h"
  16. #include "map.h"
  17. struct hist_browser {
  18. struct ui_browser b;
  19. struct hists *hists;
  20. struct hist_entry *he_selection;
  21. struct map_symbol *selection;
  22. };
  23. static void hist_browser__refresh_dimensions(struct hist_browser *self)
  24. {
  25. /* 3 == +/- toggle symbol before actual hist_entry rendering */
  26. self->b.width = 3 + (hists__sort_list_width(self->hists) +
  27. sizeof("[k]"));
  28. }
  29. static void hist_browser__reset(struct hist_browser *self)
  30. {
  31. self->b.nr_entries = self->hists->nr_entries;
  32. hist_browser__refresh_dimensions(self);
  33. ui_browser__reset_index(&self->b);
  34. }
  35. static char tree__folded_sign(bool unfolded)
  36. {
  37. return unfolded ? '-' : '+';
  38. }
  39. static char map_symbol__folded(const struct map_symbol *self)
  40. {
  41. return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
  42. }
  43. static char hist_entry__folded(const struct hist_entry *self)
  44. {
  45. return map_symbol__folded(&self->ms);
  46. }
  47. static char callchain_list__folded(const struct callchain_list *self)
  48. {
  49. return map_symbol__folded(&self->ms);
  50. }
  51. static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
  52. {
  53. int n = 0;
  54. struct rb_node *nd;
  55. for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
  56. struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
  57. struct callchain_list *chain;
  58. char folded_sign = ' '; /* No children */
  59. list_for_each_entry(chain, &child->val, list) {
  60. ++n;
  61. /* We need this because we may not have children */
  62. folded_sign = callchain_list__folded(chain);
  63. if (folded_sign == '+')
  64. break;
  65. }
  66. if (folded_sign == '-') /* Have children and they're unfolded */
  67. n += callchain_node__count_rows_rb_tree(child);
  68. }
  69. return n;
  70. }
  71. static int callchain_node__count_rows(struct callchain_node *node)
  72. {
  73. struct callchain_list *chain;
  74. bool unfolded = false;
  75. int n = 0;
  76. list_for_each_entry(chain, &node->val, list) {
  77. ++n;
  78. unfolded = chain->ms.unfolded;
  79. }
  80. if (unfolded)
  81. n += callchain_node__count_rows_rb_tree(node);
  82. return n;
  83. }
  84. static int callchain__count_rows(struct rb_root *chain)
  85. {
  86. struct rb_node *nd;
  87. int n = 0;
  88. for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
  89. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  90. n += callchain_node__count_rows(node);
  91. }
  92. return n;
  93. }
  94. static bool map_symbol__toggle_fold(struct map_symbol *self)
  95. {
  96. if (!self->has_children)
  97. return false;
  98. self->unfolded = !self->unfolded;
  99. return true;
  100. }
  101. static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
  102. {
  103. struct rb_node *nd = rb_first(&self->rb_root);
  104. for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
  105. struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
  106. struct callchain_list *chain;
  107. int first = true;
  108. list_for_each_entry(chain, &child->val, list) {
  109. if (first) {
  110. first = false;
  111. chain->ms.has_children = chain->list.next != &child->val ||
  112. rb_first(&child->rb_root) != NULL;
  113. } else
  114. chain->ms.has_children = chain->list.next == &child->val &&
  115. rb_first(&child->rb_root) != NULL;
  116. }
  117. callchain_node__init_have_children_rb_tree(child);
  118. }
  119. }
  120. static void callchain_node__init_have_children(struct callchain_node *self)
  121. {
  122. struct callchain_list *chain;
  123. list_for_each_entry(chain, &self->val, list)
  124. chain->ms.has_children = rb_first(&self->rb_root) != NULL;
  125. callchain_node__init_have_children_rb_tree(self);
  126. }
  127. static void callchain__init_have_children(struct rb_root *self)
  128. {
  129. struct rb_node *nd;
  130. for (nd = rb_first(self); nd; nd = rb_next(nd)) {
  131. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  132. callchain_node__init_have_children(node);
  133. }
  134. }
  135. static void hist_entry__init_have_children(struct hist_entry *self)
  136. {
  137. if (!self->init_have_children) {
  138. callchain__init_have_children(&self->sorted_chain);
  139. self->init_have_children = true;
  140. }
  141. }
  142. static bool hist_browser__toggle_fold(struct hist_browser *self)
  143. {
  144. if (map_symbol__toggle_fold(self->selection)) {
  145. struct hist_entry *he = self->he_selection;
  146. hist_entry__init_have_children(he);
  147. self->hists->nr_entries -= he->nr_rows;
  148. if (he->ms.unfolded)
  149. he->nr_rows = callchain__count_rows(&he->sorted_chain);
  150. else
  151. he->nr_rows = 0;
  152. self->hists->nr_entries += he->nr_rows;
  153. self->b.nr_entries = self->hists->nr_entries;
  154. return true;
  155. }
  156. /* If it doesn't have children, no toggling performed */
  157. return false;
  158. }
  159. static int hist_browser__run(struct hist_browser *self, const char *title)
  160. {
  161. int key;
  162. int exit_keys[] = { 'a', '?', 'h', 'd', 'D', 't', NEWT_KEY_ENTER,
  163. NEWT_KEY_RIGHT, NEWT_KEY_LEFT, 0, };
  164. char str[256], unit;
  165. unsigned long nr_events = self->hists->stats.nr_events[PERF_RECORD_SAMPLE];
  166. self->b.entries = &self->hists->entries;
  167. self->b.nr_entries = self->hists->nr_entries;
  168. hist_browser__refresh_dimensions(self);
  169. nr_events = convert_unit(nr_events, &unit);
  170. snprintf(str, sizeof(str), "Events: %lu%c ",
  171. nr_events, unit);
  172. newtDrawRootText(0, 0, str);
  173. if (ui_browser__show(&self->b, title,
  174. "Press '?' for help on key bindings") < 0)
  175. return -1;
  176. ui_browser__add_exit_keys(&self->b, exit_keys);
  177. while (1) {
  178. key = ui_browser__run(&self->b);
  179. switch (key) {
  180. case 'D': { /* Debug */
  181. static int seq;
  182. struct hist_entry *h = rb_entry(self->b.top,
  183. struct hist_entry, rb_node);
  184. ui_helpline__pop();
  185. ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
  186. seq++, self->b.nr_entries,
  187. self->hists->nr_entries,
  188. self->b.height,
  189. self->b.index,
  190. self->b.top_idx,
  191. h->row_offset, h->nr_rows);
  192. }
  193. continue;
  194. case NEWT_KEY_ENTER:
  195. if (hist_browser__toggle_fold(self))
  196. break;
  197. /* fall thru */
  198. default:
  199. goto out;
  200. }
  201. }
  202. out:
  203. ui_browser__hide(&self->b);
  204. return key;
  205. }
  206. static char *callchain_list__sym_name(struct callchain_list *self,
  207. char *bf, size_t bfsize)
  208. {
  209. if (self->ms.sym)
  210. return self->ms.sym->name;
  211. snprintf(bf, bfsize, "%#Lx", self->ip);
  212. return bf;
  213. }
  214. #define LEVEL_OFFSET_STEP 3
  215. static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
  216. struct callchain_node *chain_node,
  217. u64 total, int level,
  218. unsigned short row,
  219. off_t *row_offset,
  220. bool *is_current_entry)
  221. {
  222. struct rb_node *node;
  223. int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
  224. u64 new_total, remaining;
  225. if (callchain_param.mode == CHAIN_GRAPH_REL)
  226. new_total = chain_node->children_hit;
  227. else
  228. new_total = total;
  229. remaining = new_total;
  230. node = rb_first(&chain_node->rb_root);
  231. while (node) {
  232. struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
  233. struct rb_node *next = rb_next(node);
  234. u64 cumul = cumul_hits(child);
  235. struct callchain_list *chain;
  236. char folded_sign = ' ';
  237. int first = true;
  238. int extra_offset = 0;
  239. remaining -= cumul;
  240. list_for_each_entry(chain, &child->val, list) {
  241. char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
  242. const char *str;
  243. int color;
  244. bool was_first = first;
  245. if (first) {
  246. first = false;
  247. chain->ms.has_children = chain->list.next != &child->val ||
  248. rb_first(&child->rb_root) != NULL;
  249. } else {
  250. extra_offset = LEVEL_OFFSET_STEP;
  251. chain->ms.has_children = chain->list.next == &child->val &&
  252. rb_first(&child->rb_root) != NULL;
  253. }
  254. folded_sign = callchain_list__folded(chain);
  255. if (*row_offset != 0) {
  256. --*row_offset;
  257. goto do_next;
  258. }
  259. alloc_str = NULL;
  260. str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  261. if (was_first) {
  262. double percent = cumul * 100.0 / new_total;
  263. if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
  264. str = "Not enough memory!";
  265. else
  266. str = alloc_str;
  267. }
  268. color = HE_COLORSET_NORMAL;
  269. width = self->b.width - (offset + extra_offset + 2);
  270. if (ui_browser__is_current_entry(&self->b, row)) {
  271. self->selection = &chain->ms;
  272. color = HE_COLORSET_SELECTED;
  273. *is_current_entry = true;
  274. }
  275. ui_browser__set_color(&self->b, color);
  276. ui_browser__gotorc(&self->b, row, 0);
  277. slsmg_write_nstring(" ", offset + extra_offset);
  278. slsmg_printf("%c ", folded_sign);
  279. slsmg_write_nstring(str, width);
  280. free(alloc_str);
  281. if (++row == self->b.height)
  282. goto out;
  283. do_next:
  284. if (folded_sign == '+')
  285. break;
  286. }
  287. if (folded_sign == '-') {
  288. const int new_level = level + (extra_offset ? 2 : 1);
  289. row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
  290. new_level, row, row_offset,
  291. is_current_entry);
  292. }
  293. if (row == self->b.height)
  294. goto out;
  295. node = next;
  296. }
  297. out:
  298. return row - first_row;
  299. }
  300. static int hist_browser__show_callchain_node(struct hist_browser *self,
  301. struct callchain_node *node,
  302. int level, unsigned short row,
  303. off_t *row_offset,
  304. bool *is_current_entry)
  305. {
  306. struct callchain_list *chain;
  307. int first_row = row,
  308. offset = level * LEVEL_OFFSET_STEP,
  309. width = self->b.width - offset;
  310. char folded_sign = ' ';
  311. list_for_each_entry(chain, &node->val, list) {
  312. char ipstr[BITS_PER_LONG / 4 + 1], *s;
  313. int color;
  314. /*
  315. * FIXME: This should be moved to somewhere else,
  316. * probably when the callchain is created, so as not to
  317. * traverse it all over again
  318. */
  319. chain->ms.has_children = rb_first(&node->rb_root) != NULL;
  320. folded_sign = callchain_list__folded(chain);
  321. if (*row_offset != 0) {
  322. --*row_offset;
  323. continue;
  324. }
  325. color = HE_COLORSET_NORMAL;
  326. if (ui_browser__is_current_entry(&self->b, row)) {
  327. self->selection = &chain->ms;
  328. color = HE_COLORSET_SELECTED;
  329. *is_current_entry = true;
  330. }
  331. s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
  332. ui_browser__gotorc(&self->b, row, 0);
  333. ui_browser__set_color(&self->b, color);
  334. slsmg_write_nstring(" ", offset);
  335. slsmg_printf("%c ", folded_sign);
  336. slsmg_write_nstring(s, width - 2);
  337. if (++row == self->b.height)
  338. goto out;
  339. }
  340. if (folded_sign == '-')
  341. row += hist_browser__show_callchain_node_rb_tree(self, node,
  342. self->hists->stats.total_period,
  343. level + 1, row,
  344. row_offset,
  345. is_current_entry);
  346. out:
  347. return row - first_row;
  348. }
  349. static int hist_browser__show_callchain(struct hist_browser *self,
  350. struct rb_root *chain,
  351. int level, unsigned short row,
  352. off_t *row_offset,
  353. bool *is_current_entry)
  354. {
  355. struct rb_node *nd;
  356. int first_row = row;
  357. for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
  358. struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
  359. row += hist_browser__show_callchain_node(self, node, level,
  360. row, row_offset,
  361. is_current_entry);
  362. if (row == self->b.height)
  363. break;
  364. }
  365. return row - first_row;
  366. }
  367. static int hist_browser__show_entry(struct hist_browser *self,
  368. struct hist_entry *entry,
  369. unsigned short row)
  370. {
  371. char s[256];
  372. double percent;
  373. int printed = 0;
  374. int color, width = self->b.width;
  375. char folded_sign = ' ';
  376. bool current_entry = ui_browser__is_current_entry(&self->b, row);
  377. off_t row_offset = entry->row_offset;
  378. if (current_entry) {
  379. self->he_selection = entry;
  380. self->selection = &entry->ms;
  381. }
  382. if (symbol_conf.use_callchain) {
  383. entry->ms.has_children = !RB_EMPTY_ROOT(&entry->sorted_chain);
  384. folded_sign = hist_entry__folded(entry);
  385. }
  386. if (row_offset == 0) {
  387. hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false,
  388. 0, false, self->hists->stats.total_period);
  389. percent = (entry->period * 100.0) / self->hists->stats.total_period;
  390. color = HE_COLORSET_SELECTED;
  391. if (!current_entry) {
  392. if (percent >= MIN_RED)
  393. color = HE_COLORSET_TOP;
  394. else if (percent >= MIN_GREEN)
  395. color = HE_COLORSET_MEDIUM;
  396. else
  397. color = HE_COLORSET_NORMAL;
  398. }
  399. ui_browser__set_color(&self->b, color);
  400. ui_browser__gotorc(&self->b, row, 0);
  401. if (symbol_conf.use_callchain) {
  402. slsmg_printf("%c ", folded_sign);
  403. width -= 2;
  404. }
  405. slsmg_write_nstring(s, width);
  406. ++row;
  407. ++printed;
  408. } else
  409. --row_offset;
  410. if (folded_sign == '-' && row != self->b.height) {
  411. printed += hist_browser__show_callchain(self, &entry->sorted_chain,
  412. 1, row, &row_offset,
  413. &current_entry);
  414. if (current_entry)
  415. self->he_selection = entry;
  416. }
  417. return printed;
  418. }
  419. static unsigned int hist_browser__refresh(struct ui_browser *self)
  420. {
  421. unsigned row = 0;
  422. struct rb_node *nd;
  423. struct hist_browser *hb = container_of(self, struct hist_browser, b);
  424. if (self->top == NULL)
  425. self->top = rb_first(&hb->hists->entries);
  426. for (nd = self->top; nd; nd = rb_next(nd)) {
  427. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  428. if (h->filtered)
  429. continue;
  430. row += hist_browser__show_entry(hb, h, row);
  431. if (row == self->height)
  432. break;
  433. }
  434. return row;
  435. }
  436. static struct rb_node *hists__filter_entries(struct rb_node *nd)
  437. {
  438. while (nd != NULL) {
  439. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  440. if (!h->filtered)
  441. return nd;
  442. nd = rb_next(nd);
  443. }
  444. return NULL;
  445. }
  446. static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
  447. {
  448. while (nd != NULL) {
  449. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  450. if (!h->filtered)
  451. return nd;
  452. nd = rb_prev(nd);
  453. }
  454. return NULL;
  455. }
  456. static void ui_browser__hists_seek(struct ui_browser *self,
  457. off_t offset, int whence)
  458. {
  459. struct hist_entry *h;
  460. struct rb_node *nd;
  461. bool first = true;
  462. switch (whence) {
  463. case SEEK_SET:
  464. nd = hists__filter_entries(rb_first(self->entries));
  465. break;
  466. case SEEK_CUR:
  467. nd = self->top;
  468. goto do_offset;
  469. case SEEK_END:
  470. nd = hists__filter_prev_entries(rb_last(self->entries));
  471. first = false;
  472. break;
  473. default:
  474. return;
  475. }
  476. /*
  477. * Moves not relative to the first visible entry invalidates its
  478. * row_offset:
  479. */
  480. h = rb_entry(self->top, struct hist_entry, rb_node);
  481. h->row_offset = 0;
  482. /*
  483. * Here we have to check if nd is expanded (+), if it is we can't go
  484. * the next top level hist_entry, instead we must compute an offset of
  485. * what _not_ to show and not change the first visible entry.
  486. *
  487. * This offset increments when we are going from top to bottom and
  488. * decreases when we're going from bottom to top.
  489. *
  490. * As we don't have backpointers to the top level in the callchains
  491. * structure, we need to always print the whole hist_entry callchain,
  492. * skipping the first ones that are before the first visible entry
  493. * and stop when we printed enough lines to fill the screen.
  494. */
  495. do_offset:
  496. if (offset > 0) {
  497. do {
  498. h = rb_entry(nd, struct hist_entry, rb_node);
  499. if (h->ms.unfolded) {
  500. u16 remaining = h->nr_rows - h->row_offset;
  501. if (offset > remaining) {
  502. offset -= remaining;
  503. h->row_offset = 0;
  504. } else {
  505. h->row_offset += offset;
  506. offset = 0;
  507. self->top = nd;
  508. break;
  509. }
  510. }
  511. nd = hists__filter_entries(rb_next(nd));
  512. if (nd == NULL)
  513. break;
  514. --offset;
  515. self->top = nd;
  516. } while (offset != 0);
  517. } else if (offset < 0) {
  518. while (1) {
  519. h = rb_entry(nd, struct hist_entry, rb_node);
  520. if (h->ms.unfolded) {
  521. if (first) {
  522. if (-offset > h->row_offset) {
  523. offset += h->row_offset;
  524. h->row_offset = 0;
  525. } else {
  526. h->row_offset += offset;
  527. offset = 0;
  528. self->top = nd;
  529. break;
  530. }
  531. } else {
  532. if (-offset > h->nr_rows) {
  533. offset += h->nr_rows;
  534. h->row_offset = 0;
  535. } else {
  536. h->row_offset = h->nr_rows + offset;
  537. offset = 0;
  538. self->top = nd;
  539. break;
  540. }
  541. }
  542. }
  543. nd = hists__filter_prev_entries(rb_prev(nd));
  544. if (nd == NULL)
  545. break;
  546. ++offset;
  547. self->top = nd;
  548. if (offset == 0) {
  549. /*
  550. * Last unfiltered hist_entry, check if it is
  551. * unfolded, if it is then we should have
  552. * row_offset at its last entry.
  553. */
  554. h = rb_entry(nd, struct hist_entry, rb_node);
  555. if (h->ms.unfolded)
  556. h->row_offset = h->nr_rows;
  557. break;
  558. }
  559. first = false;
  560. }
  561. } else {
  562. self->top = nd;
  563. h = rb_entry(nd, struct hist_entry, rb_node);
  564. h->row_offset = 0;
  565. }
  566. }
  567. static struct hist_browser *hist_browser__new(struct hists *hists)
  568. {
  569. struct hist_browser *self = zalloc(sizeof(*self));
  570. if (self) {
  571. self->hists = hists;
  572. self->b.refresh = hist_browser__refresh;
  573. self->b.seek = ui_browser__hists_seek;
  574. }
  575. return self;
  576. }
  577. static void hist_browser__delete(struct hist_browser *self)
  578. {
  579. free(self);
  580. }
  581. static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
  582. {
  583. return self->he_selection;
  584. }
  585. static struct thread *hist_browser__selected_thread(struct hist_browser *self)
  586. {
  587. return self->he_selection->thread;
  588. }
  589. static int hist_browser__title(char *bf, size_t size, const char *ev_name,
  590. const struct dso *dso, const struct thread *thread)
  591. {
  592. int printed = 0;
  593. if (thread)
  594. printed += snprintf(bf + printed, size - printed,
  595. "Thread: %s(%d)",
  596. (thread->comm_set ? thread->comm : ""),
  597. thread->pid);
  598. if (dso)
  599. printed += snprintf(bf + printed, size - printed,
  600. "%sDSO: %s", thread ? " " : "",
  601. dso->short_name);
  602. return printed ?: snprintf(bf, size, "Event: %s", ev_name);
  603. }
  604. int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
  605. {
  606. struct hist_browser *browser = hist_browser__new(self);
  607. struct pstack *fstack;
  608. const struct thread *thread_filter = NULL;
  609. const struct dso *dso_filter = NULL;
  610. char msg[160];
  611. int key = -1;
  612. if (browser == NULL)
  613. return -1;
  614. fstack = pstack__new(2);
  615. if (fstack == NULL)
  616. goto out;
  617. ui_helpline__push(helpline);
  618. hist_browser__title(msg, sizeof(msg), ev_name,
  619. dso_filter, thread_filter);
  620. while (1) {
  621. const struct thread *thread;
  622. const struct dso *dso;
  623. char *options[16];
  624. int nr_options = 0, choice = 0, i,
  625. annotate = -2, zoom_dso = -2, zoom_thread = -2,
  626. browse_map = -2;
  627. key = hist_browser__run(browser, msg);
  628. thread = hist_browser__selected_thread(browser);
  629. dso = browser->selection->map ? browser->selection->map->dso : NULL;
  630. switch (key) {
  631. case NEWT_KEY_F1:
  632. goto do_help;
  633. case NEWT_KEY_TAB:
  634. case NEWT_KEY_UNTAB:
  635. /*
  636. * Exit the browser, let hists__browser_tree
  637. * go to the next or previous
  638. */
  639. goto out_free_stack;
  640. case 'a':
  641. if (browser->selection->map == NULL &&
  642. browser->selection->map->dso->annotate_warned)
  643. continue;
  644. goto do_annotate;
  645. case 'd':
  646. goto zoom_dso;
  647. case 't':
  648. goto zoom_thread;
  649. case 'h':
  650. case '?':
  651. do_help:
  652. ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
  653. "<- Zoom out\n"
  654. "a Annotate current symbol\n"
  655. "h/?/F1 Show this window\n"
  656. "d Zoom into current DSO\n"
  657. "t Zoom into current Thread\n"
  658. "q/CTRL+C Exit browser");
  659. continue;
  660. case NEWT_KEY_ENTER:
  661. case NEWT_KEY_RIGHT:
  662. /* menu */
  663. break;
  664. case NEWT_KEY_LEFT: {
  665. const void *top;
  666. if (pstack__empty(fstack))
  667. continue;
  668. top = pstack__pop(fstack);
  669. if (top == &dso_filter)
  670. goto zoom_out_dso;
  671. if (top == &thread_filter)
  672. goto zoom_out_thread;
  673. continue;
  674. }
  675. case NEWT_KEY_ESCAPE:
  676. if (!ui__dialog_yesno("Do you really want to exit?"))
  677. continue;
  678. /* Fall thru */
  679. default:
  680. goto out_free_stack;
  681. }
  682. if (browser->selection->sym != NULL &&
  683. !browser->selection->map->dso->annotate_warned &&
  684. asprintf(&options[nr_options], "Annotate %s",
  685. browser->selection->sym->name) > 0)
  686. annotate = nr_options++;
  687. if (thread != NULL &&
  688. asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
  689. (thread_filter ? "out of" : "into"),
  690. (thread->comm_set ? thread->comm : ""),
  691. thread->pid) > 0)
  692. zoom_thread = nr_options++;
  693. if (dso != NULL &&
  694. asprintf(&options[nr_options], "Zoom %s %s DSO",
  695. (dso_filter ? "out of" : "into"),
  696. (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
  697. zoom_dso = nr_options++;
  698. if (browser->selection->map != NULL &&
  699. asprintf(&options[nr_options], "Browse map details") > 0)
  700. browse_map = nr_options++;
  701. options[nr_options++] = (char *)"Exit";
  702. choice = ui__popup_menu(nr_options, options);
  703. for (i = 0; i < nr_options - 1; ++i)
  704. free(options[i]);
  705. if (choice == nr_options - 1)
  706. break;
  707. if (choice == -1)
  708. continue;
  709. if (choice == annotate) {
  710. struct hist_entry *he;
  711. do_annotate:
  712. if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
  713. browser->selection->map->dso->annotate_warned = 1;
  714. ui_helpline__puts("No vmlinux file found, can't "
  715. "annotate with just a "
  716. "kallsyms file");
  717. continue;
  718. }
  719. he = hist_browser__selected_entry(browser);
  720. if (he == NULL)
  721. continue;
  722. hist_entry__tui_annotate(he);
  723. } else if (choice == browse_map)
  724. map__browse(browser->selection->map);
  725. else if (choice == zoom_dso) {
  726. zoom_dso:
  727. if (dso_filter) {
  728. pstack__remove(fstack, &dso_filter);
  729. zoom_out_dso:
  730. ui_helpline__pop();
  731. dso_filter = NULL;
  732. } else {
  733. if (dso == NULL)
  734. continue;
  735. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
  736. dso->kernel ? "the Kernel" : dso->short_name);
  737. dso_filter = dso;
  738. pstack__push(fstack, &dso_filter);
  739. }
  740. hists__filter_by_dso(self, dso_filter);
  741. hist_browser__title(msg, sizeof(msg), ev_name,
  742. dso_filter, thread_filter);
  743. hist_browser__reset(browser);
  744. } else if (choice == zoom_thread) {
  745. zoom_thread:
  746. if (thread_filter) {
  747. pstack__remove(fstack, &thread_filter);
  748. zoom_out_thread:
  749. ui_helpline__pop();
  750. thread_filter = NULL;
  751. } else {
  752. ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
  753. thread->comm_set ? thread->comm : "",
  754. thread->pid);
  755. thread_filter = thread;
  756. pstack__push(fstack, &thread_filter);
  757. }
  758. hists__filter_by_thread(self, thread_filter);
  759. hist_browser__title(msg, sizeof(msg), ev_name,
  760. dso_filter, thread_filter);
  761. hist_browser__reset(browser);
  762. }
  763. }
  764. out_free_stack:
  765. pstack__delete(fstack);
  766. out:
  767. hist_browser__delete(browser);
  768. return key;
  769. }
  770. int hists__tui_browse_tree(struct rb_root *self, const char *help)
  771. {
  772. struct rb_node *first = rb_first(self), *nd = first, *next;
  773. int key = 0;
  774. while (nd) {
  775. struct hists *hists = rb_entry(nd, struct hists, rb_node);
  776. const char *ev_name = __event_name(hists->type, hists->config);
  777. key = hists__browse(hists, help, ev_name);
  778. switch (key) {
  779. case NEWT_KEY_TAB:
  780. next = rb_next(nd);
  781. if (next)
  782. nd = next;
  783. break;
  784. case NEWT_KEY_UNTAB:
  785. if (nd == first)
  786. continue;
  787. nd = rb_prev(nd);
  788. default:
  789. return key;
  790. }
  791. }
  792. return key;
  793. }