annotate.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "../../util/util.h"
  3. #include "../browser.h"
  4. #include "../helpline.h"
  5. #include "../ui.h"
  6. #include "../util.h"
  7. #include "../../util/annotate.h"
  8. #include "../../util/hist.h"
  9. #include "../../util/sort.h"
  10. #include "../../util/symbol.h"
  11. #include "../../util/evsel.h"
  12. #include "../../util/evlist.h"
  13. #include <inttypes.h>
  14. #include <pthread.h>
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. #include <sys/ttydefaults.h>
  18. #include <asm/bug.h>
  19. struct disasm_line_samples {
  20. double percent;
  21. struct sym_hist_entry he;
  22. };
  23. struct arch;
  24. struct annotate_browser {
  25. struct ui_browser b;
  26. struct rb_root entries;
  27. struct rb_node *curr_hot;
  28. struct annotation_line *selection;
  29. struct arch *arch;
  30. struct annotation_options *opts;
  31. bool searching_backwards;
  32. char search_bf[128];
  33. };
  34. static inline struct annotation *browser__annotation(struct ui_browser *browser)
  35. {
  36. struct map_symbol *ms = browser->priv;
  37. return symbol__annotation(ms->sym);
  38. }
  39. static bool disasm_line__filter(struct ui_browser *browser, void *entry)
  40. {
  41. struct annotation *notes = browser__annotation(browser);
  42. struct annotation_line *al = list_entry(entry, struct annotation_line, node);
  43. return annotation_line__filter(al, notes);
  44. }
  45. static int ui_browser__jumps_percent_color(struct ui_browser *browser, int nr, bool current)
  46. {
  47. struct annotation *notes = browser__annotation(browser);
  48. if (current && (!browser->use_navkeypressed || browser->navkeypressed))
  49. return HE_COLORSET_SELECTED;
  50. if (nr == notes->max_jump_sources)
  51. return HE_COLORSET_TOP;
  52. if (nr > 1)
  53. return HE_COLORSET_MEDIUM;
  54. return HE_COLORSET_NORMAL;
  55. }
  56. static int ui_browser__set_jumps_percent_color(void *browser, int nr, bool current)
  57. {
  58. int color = ui_browser__jumps_percent_color(browser, nr, current);
  59. return ui_browser__set_color(browser, color);
  60. }
  61. static int annotate_browser__set_color(void *browser, int color)
  62. {
  63. return ui_browser__set_color(browser, color);
  64. }
  65. static void annotate_browser__write_graph(void *browser, int graph)
  66. {
  67. ui_browser__write_graph(browser, graph);
  68. }
  69. static void annotate_browser__set_percent_color(void *browser, double percent, bool current)
  70. {
  71. ui_browser__set_percent_color(browser, percent, current);
  72. }
  73. static void annotate_browser__printf(void *browser, const char *fmt, ...)
  74. {
  75. va_list args;
  76. va_start(args, fmt);
  77. ui_browser__vprintf(browser, fmt, args);
  78. va_end(args);
  79. }
  80. static void annotate_browser__write(struct ui_browser *browser, void *entry, int row)
  81. {
  82. struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
  83. struct annotation *notes = browser__annotation(browser);
  84. struct annotation_line *al = list_entry(entry, struct annotation_line, node);
  85. const bool is_current_entry = ui_browser__is_current_entry(browser, row);
  86. struct annotation_write_ops ops = {
  87. .first_line = row == 0,
  88. .current_entry = is_current_entry,
  89. .change_color = (!notes->options->hide_src_code &&
  90. (!is_current_entry ||
  91. (browser->use_navkeypressed &&
  92. !browser->navkeypressed))),
  93. .width = browser->width,
  94. .obj = browser,
  95. .set_color = annotate_browser__set_color,
  96. .set_percent_color = annotate_browser__set_percent_color,
  97. .set_jumps_percent_color = ui_browser__set_jumps_percent_color,
  98. .printf = annotate_browser__printf,
  99. .write_graph = annotate_browser__write_graph,
  100. };
  101. /* The scroll bar isn't being used */
  102. if (!browser->navkeypressed)
  103. ops.width += 1;
  104. annotation_line__write(al, notes, &ops, ab->opts);
  105. if (ops.current_entry)
  106. ab->selection = al;
  107. }
  108. static bool is_fused(struct annotate_browser *ab, struct disasm_line *cursor)
  109. {
  110. struct disasm_line *pos = list_prev_entry(cursor, al.node);
  111. const char *name;
  112. if (!pos)
  113. return false;
  114. if (ins__is_lock(&pos->ins))
  115. name = pos->ops.locked.ins.name;
  116. else
  117. name = pos->ins.name;
  118. if (!name || !cursor->ins.name)
  119. return false;
  120. return ins__is_fused(ab->arch, name, cursor->ins.name);
  121. }
  122. static void annotate_browser__draw_current_jump(struct ui_browser *browser)
  123. {
  124. struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
  125. struct disasm_line *cursor = disasm_line(ab->selection);
  126. struct annotation_line *target;
  127. unsigned int from, to;
  128. struct map_symbol *ms = ab->b.priv;
  129. struct symbol *sym = ms->sym;
  130. struct annotation *notes = symbol__annotation(sym);
  131. u8 pcnt_width = annotation__pcnt_width(notes);
  132. int width;
  133. /* PLT symbols contain external offsets */
  134. if (strstr(sym->name, "@plt"))
  135. return;
  136. if (!disasm_line__is_valid_local_jump(cursor, sym))
  137. return;
  138. /*
  139. * This first was seen with a gcc function, _cpp_lex_token, that
  140. * has the usual jumps:
  141. *
  142. * │1159e6c: ↓ jne 115aa32 <_cpp_lex_token@@Base+0xf92>
  143. *
  144. * I.e. jumps to a label inside that function (_cpp_lex_token), and
  145. * those works, but also this kind:
  146. *
  147. * │1159e8b: ↓ jne c469be <cpp_named_operator2name@@Base+0xa72>
  148. *
  149. * I.e. jumps to another function, outside _cpp_lex_token, which
  150. * are not being correctly handled generating as a side effect references
  151. * to ab->offset[] entries that are set to NULL, so to make this code
  152. * more robust, check that here.
  153. *
  154. * A proper fix for will be put in place, looking at the function
  155. * name right after the '<' token and probably treating this like a
  156. * 'call' instruction.
  157. */
  158. target = notes->offsets[cursor->ops.target.offset];
  159. if (target == NULL) {
  160. ui_helpline__printf("WARN: jump target inconsistency, press 'o', notes->offsets[%#x] = NULL\n",
  161. cursor->ops.target.offset);
  162. return;
  163. }
  164. if (notes->options->hide_src_code) {
  165. from = cursor->al.idx_asm;
  166. to = target->idx_asm;
  167. } else {
  168. from = (u64)cursor->al.idx;
  169. to = (u64)target->idx;
  170. }
  171. width = annotation__cycles_width(notes);
  172. ui_browser__set_color(browser, HE_COLORSET_JUMP_ARROWS);
  173. __ui_browser__line_arrow(browser,
  174. pcnt_width + 2 + notes->widths.addr + width,
  175. from, to);
  176. if (is_fused(ab, cursor)) {
  177. ui_browser__mark_fused(browser,
  178. pcnt_width + 3 + notes->widths.addr + width,
  179. from - 1,
  180. to > from ? true : false);
  181. }
  182. }
  183. static unsigned int annotate_browser__refresh(struct ui_browser *browser)
  184. {
  185. struct annotation *notes = browser__annotation(browser);
  186. int ret = ui_browser__list_head_refresh(browser);
  187. int pcnt_width = annotation__pcnt_width(notes);
  188. if (notes->options->jump_arrows)
  189. annotate_browser__draw_current_jump(browser);
  190. ui_browser__set_color(browser, HE_COLORSET_NORMAL);
  191. __ui_browser__vline(browser, pcnt_width, 0, browser->rows - 1);
  192. return ret;
  193. }
  194. static int disasm__cmp(struct annotation_line *a, struct annotation_line *b)
  195. {
  196. int i;
  197. for (i = 0; i < a->data_nr; i++) {
  198. if (a->data[i].percent == b->data[i].percent)
  199. continue;
  200. return a->data[i].percent < b->data[i].percent;
  201. }
  202. return 0;
  203. }
  204. static void disasm_rb_tree__insert(struct rb_root *root, struct annotation_line *al)
  205. {
  206. struct rb_node **p = &root->rb_node;
  207. struct rb_node *parent = NULL;
  208. struct annotation_line *l;
  209. while (*p != NULL) {
  210. parent = *p;
  211. l = rb_entry(parent, struct annotation_line, rb_node);
  212. if (disasm__cmp(al, l))
  213. p = &(*p)->rb_left;
  214. else
  215. p = &(*p)->rb_right;
  216. }
  217. rb_link_node(&al->rb_node, parent, p);
  218. rb_insert_color(&al->rb_node, root);
  219. }
  220. static void annotate_browser__set_top(struct annotate_browser *browser,
  221. struct annotation_line *pos, u32 idx)
  222. {
  223. struct annotation *notes = browser__annotation(&browser->b);
  224. unsigned back;
  225. ui_browser__refresh_dimensions(&browser->b);
  226. back = browser->b.height / 2;
  227. browser->b.top_idx = browser->b.index = idx;
  228. while (browser->b.top_idx != 0 && back != 0) {
  229. pos = list_entry(pos->node.prev, struct annotation_line, node);
  230. if (annotation_line__filter(pos, notes))
  231. continue;
  232. --browser->b.top_idx;
  233. --back;
  234. }
  235. browser->b.top = pos;
  236. browser->b.navkeypressed = true;
  237. }
  238. static void annotate_browser__set_rb_top(struct annotate_browser *browser,
  239. struct rb_node *nd)
  240. {
  241. struct annotation *notes = browser__annotation(&browser->b);
  242. struct annotation_line * pos = rb_entry(nd, struct annotation_line, rb_node);
  243. u32 idx = pos->idx;
  244. if (notes->options->hide_src_code)
  245. idx = pos->idx_asm;
  246. annotate_browser__set_top(browser, pos, idx);
  247. browser->curr_hot = nd;
  248. }
  249. static void annotate_browser__calc_percent(struct annotate_browser *browser,
  250. struct perf_evsel *evsel)
  251. {
  252. struct map_symbol *ms = browser->b.priv;
  253. struct symbol *sym = ms->sym;
  254. struct annotation *notes = symbol__annotation(sym);
  255. struct disasm_line *pos;
  256. browser->entries = RB_ROOT;
  257. pthread_mutex_lock(&notes->lock);
  258. symbol__calc_percent(sym, evsel);
  259. list_for_each_entry(pos, &notes->src->source, al.node) {
  260. double max_percent = 0.0;
  261. int i;
  262. if (pos->al.offset == -1) {
  263. RB_CLEAR_NODE(&pos->al.rb_node);
  264. continue;
  265. }
  266. for (i = 0; i < pos->al.data_nr; i++) {
  267. double percent;
  268. percent = annotation_data__percent(&pos->al.data[i],
  269. browser->opts->percent_type);
  270. if (max_percent < percent)
  271. max_percent = percent;
  272. }
  273. if (max_percent < 0.01 && pos->al.ipc == 0) {
  274. RB_CLEAR_NODE(&pos->al.rb_node);
  275. continue;
  276. }
  277. disasm_rb_tree__insert(&browser->entries, &pos->al);
  278. }
  279. pthread_mutex_unlock(&notes->lock);
  280. browser->curr_hot = rb_last(&browser->entries);
  281. }
  282. static bool annotate_browser__toggle_source(struct annotate_browser *browser)
  283. {
  284. struct annotation *notes = browser__annotation(&browser->b);
  285. struct annotation_line *al;
  286. off_t offset = browser->b.index - browser->b.top_idx;
  287. browser->b.seek(&browser->b, offset, SEEK_CUR);
  288. al = list_entry(browser->b.top, struct annotation_line, node);
  289. if (notes->options->hide_src_code) {
  290. if (al->idx_asm < offset)
  291. offset = al->idx;
  292. browser->b.nr_entries = notes->nr_entries;
  293. notes->options->hide_src_code = false;
  294. browser->b.seek(&browser->b, -offset, SEEK_CUR);
  295. browser->b.top_idx = al->idx - offset;
  296. browser->b.index = al->idx;
  297. } else {
  298. if (al->idx_asm < 0) {
  299. ui_helpline__puts("Only available for assembly lines.");
  300. browser->b.seek(&browser->b, -offset, SEEK_CUR);
  301. return false;
  302. }
  303. if (al->idx_asm < offset)
  304. offset = al->idx_asm;
  305. browser->b.nr_entries = notes->nr_asm_entries;
  306. notes->options->hide_src_code = true;
  307. browser->b.seek(&browser->b, -offset, SEEK_CUR);
  308. browser->b.top_idx = al->idx_asm - offset;
  309. browser->b.index = al->idx_asm;
  310. }
  311. return true;
  312. }
  313. static void ui_browser__init_asm_mode(struct ui_browser *browser)
  314. {
  315. struct annotation *notes = browser__annotation(browser);
  316. ui_browser__reset_index(browser);
  317. browser->nr_entries = notes->nr_asm_entries;
  318. }
  319. #define SYM_TITLE_MAX_SIZE (PATH_MAX + 64)
  320. static int sym_title(struct symbol *sym, struct map *map, char *title,
  321. size_t sz, int percent_type)
  322. {
  323. return snprintf(title, sz, "%s %s [Percent: %s]", sym->name, map->dso->long_name,
  324. percent_type_str(percent_type));
  325. }
  326. /*
  327. * This can be called from external jumps, i.e. jumps from one functon
  328. * to another, like from the kernel's entry_SYSCALL_64 function to the
  329. * swapgs_restore_regs_and_return_to_usermode() function.
  330. *
  331. * So all we check here is that dl->ops.target.sym is set, if it is, just
  332. * go to that function and when exiting from its disassembly, come back
  333. * to the calling function.
  334. */
  335. static bool annotate_browser__callq(struct annotate_browser *browser,
  336. struct perf_evsel *evsel,
  337. struct hist_browser_timer *hbt)
  338. {
  339. struct map_symbol *ms = browser->b.priv;
  340. struct disasm_line *dl = disasm_line(browser->selection);
  341. struct annotation *notes;
  342. char title[SYM_TITLE_MAX_SIZE];
  343. if (!dl->ops.target.sym) {
  344. ui_helpline__puts("The called function was not found.");
  345. return true;
  346. }
  347. notes = symbol__annotation(dl->ops.target.sym);
  348. pthread_mutex_lock(&notes->lock);
  349. if (!symbol__hists(dl->ops.target.sym, evsel->evlist->nr_entries)) {
  350. pthread_mutex_unlock(&notes->lock);
  351. ui__warning("Not enough memory for annotating '%s' symbol!\n",
  352. dl->ops.target.sym->name);
  353. return true;
  354. }
  355. pthread_mutex_unlock(&notes->lock);
  356. symbol__tui_annotate(dl->ops.target.sym, ms->map, evsel, hbt, browser->opts);
  357. sym_title(ms->sym, ms->map, title, sizeof(title), browser->opts->percent_type);
  358. ui_browser__show_title(&browser->b, title);
  359. return true;
  360. }
  361. static
  362. struct disasm_line *annotate_browser__find_offset(struct annotate_browser *browser,
  363. s64 offset, s64 *idx)
  364. {
  365. struct annotation *notes = browser__annotation(&browser->b);
  366. struct disasm_line *pos;
  367. *idx = 0;
  368. list_for_each_entry(pos, &notes->src->source, al.node) {
  369. if (pos->al.offset == offset)
  370. return pos;
  371. if (!annotation_line__filter(&pos->al, notes))
  372. ++*idx;
  373. }
  374. return NULL;
  375. }
  376. static bool annotate_browser__jump(struct annotate_browser *browser,
  377. struct perf_evsel *evsel,
  378. struct hist_browser_timer *hbt)
  379. {
  380. struct disasm_line *dl = disasm_line(browser->selection);
  381. u64 offset;
  382. s64 idx;
  383. if (!ins__is_jump(&dl->ins))
  384. return false;
  385. if (dl->ops.target.outside) {
  386. annotate_browser__callq(browser, evsel, hbt);
  387. return true;
  388. }
  389. offset = dl->ops.target.offset;
  390. dl = annotate_browser__find_offset(browser, offset, &idx);
  391. if (dl == NULL) {
  392. ui_helpline__printf("Invalid jump offset: %" PRIx64, offset);
  393. return true;
  394. }
  395. annotate_browser__set_top(browser, &dl->al, idx);
  396. return true;
  397. }
  398. static
  399. struct annotation_line *annotate_browser__find_string(struct annotate_browser *browser,
  400. char *s, s64 *idx)
  401. {
  402. struct annotation *notes = browser__annotation(&browser->b);
  403. struct annotation_line *al = browser->selection;
  404. *idx = browser->b.index;
  405. list_for_each_entry_continue(al, &notes->src->source, node) {
  406. if (annotation_line__filter(al, notes))
  407. continue;
  408. ++*idx;
  409. if (al->line && strstr(al->line, s) != NULL)
  410. return al;
  411. }
  412. return NULL;
  413. }
  414. static bool __annotate_browser__search(struct annotate_browser *browser)
  415. {
  416. struct annotation_line *al;
  417. s64 idx;
  418. al = annotate_browser__find_string(browser, browser->search_bf, &idx);
  419. if (al == NULL) {
  420. ui_helpline__puts("String not found!");
  421. return false;
  422. }
  423. annotate_browser__set_top(browser, al, idx);
  424. browser->searching_backwards = false;
  425. return true;
  426. }
  427. static
  428. struct annotation_line *annotate_browser__find_string_reverse(struct annotate_browser *browser,
  429. char *s, s64 *idx)
  430. {
  431. struct annotation *notes = browser__annotation(&browser->b);
  432. struct annotation_line *al = browser->selection;
  433. *idx = browser->b.index;
  434. list_for_each_entry_continue_reverse(al, &notes->src->source, node) {
  435. if (annotation_line__filter(al, notes))
  436. continue;
  437. --*idx;
  438. if (al->line && strstr(al->line, s) != NULL)
  439. return al;
  440. }
  441. return NULL;
  442. }
  443. static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
  444. {
  445. struct annotation_line *al;
  446. s64 idx;
  447. al = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
  448. if (al == NULL) {
  449. ui_helpline__puts("String not found!");
  450. return false;
  451. }
  452. annotate_browser__set_top(browser, al, idx);
  453. browser->searching_backwards = true;
  454. return true;
  455. }
  456. static bool annotate_browser__search_window(struct annotate_browser *browser,
  457. int delay_secs)
  458. {
  459. if (ui_browser__input_window("Search", "String: ", browser->search_bf,
  460. "ENTER: OK, ESC: Cancel",
  461. delay_secs * 2) != K_ENTER ||
  462. !*browser->search_bf)
  463. return false;
  464. return true;
  465. }
  466. static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs)
  467. {
  468. if (annotate_browser__search_window(browser, delay_secs))
  469. return __annotate_browser__search(browser);
  470. return false;
  471. }
  472. static bool annotate_browser__continue_search(struct annotate_browser *browser,
  473. int delay_secs)
  474. {
  475. if (!*browser->search_bf)
  476. return annotate_browser__search(browser, delay_secs);
  477. return __annotate_browser__search(browser);
  478. }
  479. static bool annotate_browser__search_reverse(struct annotate_browser *browser,
  480. int delay_secs)
  481. {
  482. if (annotate_browser__search_window(browser, delay_secs))
  483. return __annotate_browser__search_reverse(browser);
  484. return false;
  485. }
  486. static
  487. bool annotate_browser__continue_search_reverse(struct annotate_browser *browser,
  488. int delay_secs)
  489. {
  490. if (!*browser->search_bf)
  491. return annotate_browser__search_reverse(browser, delay_secs);
  492. return __annotate_browser__search_reverse(browser);
  493. }
  494. static int annotate_browser__show(struct ui_browser *browser, char *title, const char *help)
  495. {
  496. struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
  497. struct map_symbol *ms = browser->priv;
  498. struct symbol *sym = ms->sym;
  499. char symbol_dso[SYM_TITLE_MAX_SIZE];
  500. if (ui_browser__show(browser, title, help) < 0)
  501. return -1;
  502. sym_title(sym, ms->map, symbol_dso, sizeof(symbol_dso), ab->opts->percent_type);
  503. ui_browser__gotorc_title(browser, 0, 0);
  504. ui_browser__set_color(browser, HE_COLORSET_ROOT);
  505. ui_browser__write_nstring(browser, symbol_dso, browser->width + 1);
  506. return 0;
  507. }
  508. static void
  509. switch_percent_type(struct annotation_options *opts, bool base)
  510. {
  511. switch (opts->percent_type) {
  512. case PERCENT_HITS_LOCAL:
  513. if (base)
  514. opts->percent_type = PERCENT_PERIOD_LOCAL;
  515. else
  516. opts->percent_type = PERCENT_HITS_GLOBAL;
  517. break;
  518. case PERCENT_HITS_GLOBAL:
  519. if (base)
  520. opts->percent_type = PERCENT_PERIOD_GLOBAL;
  521. else
  522. opts->percent_type = PERCENT_HITS_LOCAL;
  523. break;
  524. case PERCENT_PERIOD_LOCAL:
  525. if (base)
  526. opts->percent_type = PERCENT_HITS_LOCAL;
  527. else
  528. opts->percent_type = PERCENT_PERIOD_GLOBAL;
  529. break;
  530. case PERCENT_PERIOD_GLOBAL:
  531. if (base)
  532. opts->percent_type = PERCENT_HITS_GLOBAL;
  533. else
  534. opts->percent_type = PERCENT_PERIOD_LOCAL;
  535. break;
  536. default:
  537. WARN_ON(1);
  538. }
  539. }
  540. static int annotate_browser__run(struct annotate_browser *browser,
  541. struct perf_evsel *evsel,
  542. struct hist_browser_timer *hbt)
  543. {
  544. struct rb_node *nd = NULL;
  545. struct hists *hists = evsel__hists(evsel);
  546. struct map_symbol *ms = browser->b.priv;
  547. struct symbol *sym = ms->sym;
  548. struct annotation *notes = symbol__annotation(ms->sym);
  549. const char *help = "Press 'h' for help on key bindings";
  550. int delay_secs = hbt ? hbt->refresh : 0;
  551. char title[256];
  552. int key;
  553. hists__scnprintf_title(hists, title, sizeof(title));
  554. if (annotate_browser__show(&browser->b, title, help) < 0)
  555. return -1;
  556. annotate_browser__calc_percent(browser, evsel);
  557. if (browser->curr_hot) {
  558. annotate_browser__set_rb_top(browser, browser->curr_hot);
  559. browser->b.navkeypressed = false;
  560. }
  561. nd = browser->curr_hot;
  562. while (1) {
  563. key = ui_browser__run(&browser->b, delay_secs);
  564. if (delay_secs != 0) {
  565. annotate_browser__calc_percent(browser, evsel);
  566. /*
  567. * Current line focus got out of the list of most active
  568. * lines, NULL it so that if TAB|UNTAB is pressed, we
  569. * move to curr_hot (current hottest line).
  570. */
  571. if (nd != NULL && RB_EMPTY_NODE(nd))
  572. nd = NULL;
  573. }
  574. switch (key) {
  575. case K_TIMER:
  576. if (hbt)
  577. hbt->timer(hbt->arg);
  578. if (delay_secs != 0) {
  579. symbol__annotate_decay_histogram(sym, evsel->idx);
  580. hists__scnprintf_title(hists, title, sizeof(title));
  581. annotate_browser__show(&browser->b, title, help);
  582. }
  583. continue;
  584. case K_TAB:
  585. if (nd != NULL) {
  586. nd = rb_prev(nd);
  587. if (nd == NULL)
  588. nd = rb_last(&browser->entries);
  589. } else
  590. nd = browser->curr_hot;
  591. break;
  592. case K_UNTAB:
  593. if (nd != NULL) {
  594. nd = rb_next(nd);
  595. if (nd == NULL)
  596. nd = rb_first(&browser->entries);
  597. } else
  598. nd = browser->curr_hot;
  599. break;
  600. case K_F1:
  601. case 'h':
  602. ui_browser__help_window(&browser->b,
  603. "UP/DOWN/PGUP\n"
  604. "PGDN/SPACE Navigate\n"
  605. "q/ESC/CTRL+C Exit\n\n"
  606. "ENTER Go to target\n"
  607. "ESC Exit\n"
  608. "H Go to hottest instruction\n"
  609. "TAB/shift+TAB Cycle thru hottest instructions\n"
  610. "j Toggle showing jump to target arrows\n"
  611. "J Toggle showing number of jump sources on targets\n"
  612. "n Search next string\n"
  613. "o Toggle disassembler output/simplified view\n"
  614. "O Bump offset level (jump targets -> +call -> all -> cycle thru)\n"
  615. "s Toggle source code view\n"
  616. "t Circulate percent, total period, samples view\n"
  617. "c Show min/max cycle\n"
  618. "/ Search string\n"
  619. "k Toggle line numbers\n"
  620. "P Print to [symbol_name].annotation file.\n"
  621. "r Run available scripts\n"
  622. "p Toggle percent type [local/global]\n"
  623. "b Toggle percent base [period/hits]\n"
  624. "? Search string backwards\n");
  625. continue;
  626. case 'r':
  627. {
  628. script_browse(NULL);
  629. continue;
  630. }
  631. case 'k':
  632. notes->options->show_linenr = !notes->options->show_linenr;
  633. break;
  634. case 'H':
  635. nd = browser->curr_hot;
  636. break;
  637. case 's':
  638. if (annotate_browser__toggle_source(browser))
  639. ui_helpline__puts(help);
  640. continue;
  641. case 'o':
  642. notes->options->use_offset = !notes->options->use_offset;
  643. annotation__update_column_widths(notes);
  644. continue;
  645. case 'O':
  646. if (++notes->options->offset_level > ANNOTATION__MAX_OFFSET_LEVEL)
  647. notes->options->offset_level = ANNOTATION__MIN_OFFSET_LEVEL;
  648. continue;
  649. case 'j':
  650. notes->options->jump_arrows = !notes->options->jump_arrows;
  651. continue;
  652. case 'J':
  653. notes->options->show_nr_jumps = !notes->options->show_nr_jumps;
  654. annotation__update_column_widths(notes);
  655. continue;
  656. case '/':
  657. if (annotate_browser__search(browser, delay_secs)) {
  658. show_help:
  659. ui_helpline__puts(help);
  660. }
  661. continue;
  662. case 'n':
  663. if (browser->searching_backwards ?
  664. annotate_browser__continue_search_reverse(browser, delay_secs) :
  665. annotate_browser__continue_search(browser, delay_secs))
  666. goto show_help;
  667. continue;
  668. case '?':
  669. if (annotate_browser__search_reverse(browser, delay_secs))
  670. goto show_help;
  671. continue;
  672. case 'D': {
  673. static int seq;
  674. ui_helpline__pop();
  675. ui_helpline__fpush("%d: nr_ent=%d, height=%d, idx=%d, top_idx=%d, nr_asm_entries=%d",
  676. seq++, browser->b.nr_entries,
  677. browser->b.height,
  678. browser->b.index,
  679. browser->b.top_idx,
  680. notes->nr_asm_entries);
  681. }
  682. continue;
  683. case K_ENTER:
  684. case K_RIGHT:
  685. {
  686. struct disasm_line *dl = disasm_line(browser->selection);
  687. if (browser->selection == NULL)
  688. ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
  689. else if (browser->selection->offset == -1)
  690. ui_helpline__puts("Actions are only available for assembly lines.");
  691. else if (!dl->ins.ops)
  692. goto show_sup_ins;
  693. else if (ins__is_ret(&dl->ins))
  694. goto out;
  695. else if (!(annotate_browser__jump(browser, evsel, hbt) ||
  696. annotate_browser__callq(browser, evsel, hbt))) {
  697. show_sup_ins:
  698. ui_helpline__puts("Actions are only available for function call/return & jump/branch instructions.");
  699. }
  700. continue;
  701. }
  702. case 'P':
  703. map_symbol__annotation_dump(ms, evsel, browser->opts);
  704. continue;
  705. case 't':
  706. if (notes->options->show_total_period) {
  707. notes->options->show_total_period = false;
  708. notes->options->show_nr_samples = true;
  709. } else if (notes->options->show_nr_samples)
  710. notes->options->show_nr_samples = false;
  711. else
  712. notes->options->show_total_period = true;
  713. annotation__update_column_widths(notes);
  714. continue;
  715. case 'c':
  716. if (notes->options->show_minmax_cycle)
  717. notes->options->show_minmax_cycle = false;
  718. else
  719. notes->options->show_minmax_cycle = true;
  720. annotation__update_column_widths(notes);
  721. continue;
  722. case 'p':
  723. case 'b':
  724. switch_percent_type(browser->opts, key == 'b');
  725. hists__scnprintf_title(hists, title, sizeof(title));
  726. annotate_browser__show(&browser->b, title, help);
  727. continue;
  728. case K_LEFT:
  729. case K_ESC:
  730. case 'q':
  731. case CTRL('c'):
  732. goto out;
  733. default:
  734. continue;
  735. }
  736. if (nd != NULL)
  737. annotate_browser__set_rb_top(browser, nd);
  738. }
  739. out:
  740. ui_browser__hide(&browser->b);
  741. return key;
  742. }
  743. int map_symbol__tui_annotate(struct map_symbol *ms, struct perf_evsel *evsel,
  744. struct hist_browser_timer *hbt,
  745. struct annotation_options *opts)
  746. {
  747. return symbol__tui_annotate(ms->sym, ms->map, evsel, hbt, opts);
  748. }
  749. int hist_entry__tui_annotate(struct hist_entry *he, struct perf_evsel *evsel,
  750. struct hist_browser_timer *hbt,
  751. struct annotation_options *opts)
  752. {
  753. /* reset abort key so that it can get Ctrl-C as a key */
  754. SLang_reset_tty();
  755. SLang_init_tty(0, 0, 0);
  756. return map_symbol__tui_annotate(&he->ms, evsel, hbt, opts);
  757. }
  758. int symbol__tui_annotate(struct symbol *sym, struct map *map,
  759. struct perf_evsel *evsel,
  760. struct hist_browser_timer *hbt,
  761. struct annotation_options *opts)
  762. {
  763. struct annotation *notes = symbol__annotation(sym);
  764. struct map_symbol ms = {
  765. .map = map,
  766. .sym = sym,
  767. };
  768. struct annotate_browser browser = {
  769. .b = {
  770. .refresh = annotate_browser__refresh,
  771. .seek = ui_browser__list_head_seek,
  772. .write = annotate_browser__write,
  773. .filter = disasm_line__filter,
  774. .extra_title_lines = 1, /* for hists__scnprintf_title() */
  775. .priv = &ms,
  776. .use_navkeypressed = true,
  777. },
  778. .opts = opts,
  779. };
  780. int ret = -1, err;
  781. if (sym == NULL)
  782. return -1;
  783. if (map->dso->annotate_warned)
  784. return -1;
  785. err = symbol__annotate2(sym, map, evsel, opts, &browser.arch);
  786. if (err) {
  787. char msg[BUFSIZ];
  788. symbol__strerror_disassemble(sym, map, err, msg, sizeof(msg));
  789. ui__error("Couldn't annotate %s:\n%s", sym->name, msg);
  790. goto out_free_offsets;
  791. }
  792. ui_helpline__push("Press ESC to exit");
  793. browser.b.width = notes->max_line_len;
  794. browser.b.nr_entries = notes->nr_entries;
  795. browser.b.entries = &notes->src->source,
  796. browser.b.width += 18; /* Percentage */
  797. if (notes->options->hide_src_code)
  798. ui_browser__init_asm_mode(&browser.b);
  799. ret = annotate_browser__run(&browser, evsel, hbt);
  800. annotated_source__purge(notes->src);
  801. out_free_offsets:
  802. zfree(&notes->offsets);
  803. return ret;
  804. }