hists.c 23 KB

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