hists.c 22 KB

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