annotate.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. #include "../../util/util.h"
  2. #include "../browser.h"
  3. #include "../helpline.h"
  4. #include "../libslang.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 <pthread.h>
  12. #include <newt.h>
  13. struct browser_disasm_line {
  14. struct rb_node rb_node;
  15. double percent;
  16. u32 idx;
  17. int idx_asm;
  18. int jump_sources;
  19. };
  20. static struct annotate_browser_opt {
  21. bool hide_src_code,
  22. use_offset,
  23. jump_arrows,
  24. show_nr_jumps;
  25. } annotate_browser__opts = {
  26. .use_offset = true,
  27. .jump_arrows = true,
  28. };
  29. struct annotate_browser {
  30. struct ui_browser b;
  31. struct rb_root entries;
  32. struct rb_node *curr_hot;
  33. struct disasm_line *selection;
  34. struct disasm_line **offsets;
  35. u64 start;
  36. int nr_asm_entries;
  37. int nr_entries;
  38. int max_jump_sources;
  39. int nr_jumps;
  40. bool searching_backwards;
  41. u8 addr_width;
  42. u8 jumps_width;
  43. u8 target_width;
  44. u8 min_addr_width;
  45. u8 max_addr_width;
  46. char search_bf[128];
  47. };
  48. static inline struct browser_disasm_line *disasm_line__browser(struct disasm_line *dl)
  49. {
  50. return (struct browser_disasm_line *)(dl + 1);
  51. }
  52. static bool disasm_line__filter(struct ui_browser *browser __maybe_unused,
  53. void *entry)
  54. {
  55. if (annotate_browser__opts.hide_src_code) {
  56. struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
  57. return dl->offset == -1;
  58. }
  59. return false;
  60. }
  61. static int annotate_browser__jumps_percent_color(struct annotate_browser *browser,
  62. int nr, bool current)
  63. {
  64. if (current && (!browser->b.use_navkeypressed || browser->b.navkeypressed))
  65. return HE_COLORSET_SELECTED;
  66. if (nr == browser->max_jump_sources)
  67. return HE_COLORSET_TOP;
  68. if (nr > 1)
  69. return HE_COLORSET_MEDIUM;
  70. return HE_COLORSET_NORMAL;
  71. }
  72. static int annotate_browser__set_jumps_percent_color(struct annotate_browser *browser,
  73. int nr, bool current)
  74. {
  75. int color = annotate_browser__jumps_percent_color(browser, nr, current);
  76. return ui_browser__set_color(&browser->b, color);
  77. }
  78. static void annotate_browser__write(struct ui_browser *browser, void *entry, int row)
  79. {
  80. struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
  81. struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
  82. struct browser_disasm_line *bdl = disasm_line__browser(dl);
  83. bool current_entry = ui_browser__is_current_entry(browser, row);
  84. bool change_color = (!annotate_browser__opts.hide_src_code &&
  85. (!current_entry || (browser->use_navkeypressed &&
  86. !browser->navkeypressed)));
  87. int width = browser->width, printed;
  88. char bf[256];
  89. if (dl->offset != -1 && bdl->percent != 0.0) {
  90. ui_browser__set_percent_color(browser, bdl->percent, current_entry);
  91. slsmg_printf("%6.2f ", bdl->percent);
  92. } else {
  93. ui_browser__set_percent_color(browser, 0, current_entry);
  94. slsmg_write_nstring(" ", 7);
  95. }
  96. SLsmg_write_char(' ');
  97. /* The scroll bar isn't being used */
  98. if (!browser->navkeypressed)
  99. width += 1;
  100. if (!*dl->line)
  101. slsmg_write_nstring(" ", width - 7);
  102. else if (dl->offset == -1) {
  103. printed = scnprintf(bf, sizeof(bf), "%*s ",
  104. ab->addr_width, " ");
  105. slsmg_write_nstring(bf, printed);
  106. slsmg_write_nstring(dl->line, width - printed - 6);
  107. } else {
  108. u64 addr = dl->offset;
  109. int color = -1;
  110. if (!annotate_browser__opts.use_offset)
  111. addr += ab->start;
  112. if (!annotate_browser__opts.use_offset) {
  113. printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr);
  114. } else {
  115. if (bdl->jump_sources) {
  116. if (annotate_browser__opts.show_nr_jumps) {
  117. int prev;
  118. printed = scnprintf(bf, sizeof(bf), "%*d ",
  119. ab->jumps_width,
  120. bdl->jump_sources);
  121. prev = annotate_browser__set_jumps_percent_color(ab, bdl->jump_sources,
  122. current_entry);
  123. slsmg_write_nstring(bf, printed);
  124. ui_browser__set_color(browser, prev);
  125. }
  126. printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ",
  127. ab->target_width, addr);
  128. } else {
  129. printed = scnprintf(bf, sizeof(bf), "%*s ",
  130. ab->addr_width, " ");
  131. }
  132. }
  133. if (change_color)
  134. color = ui_browser__set_color(browser, HE_COLORSET_ADDR);
  135. slsmg_write_nstring(bf, printed);
  136. if (change_color)
  137. ui_browser__set_color(browser, color);
  138. if (dl->ins && dl->ins->ops->scnprintf) {
  139. if (ins__is_jump(dl->ins)) {
  140. bool fwd = dl->ops.target.offset > (u64)dl->offset;
  141. ui_browser__write_graph(browser, fwd ? SLSMG_DARROW_CHAR :
  142. SLSMG_UARROW_CHAR);
  143. SLsmg_write_char(' ');
  144. } else if (ins__is_call(dl->ins)) {
  145. ui_browser__write_graph(browser, SLSMG_RARROW_CHAR);
  146. SLsmg_write_char(' ');
  147. } else {
  148. slsmg_write_nstring(" ", 2);
  149. }
  150. } else {
  151. if (strcmp(dl->name, "retq")) {
  152. slsmg_write_nstring(" ", 2);
  153. } else {
  154. ui_browser__write_graph(browser, SLSMG_LARROW_CHAR);
  155. SLsmg_write_char(' ');
  156. }
  157. }
  158. disasm_line__scnprintf(dl, bf, sizeof(bf), !annotate_browser__opts.use_offset);
  159. slsmg_write_nstring(bf, width - 10 - printed);
  160. }
  161. if (current_entry)
  162. ab->selection = dl;
  163. }
  164. static void annotate_browser__draw_current_jump(struct ui_browser *browser)
  165. {
  166. struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
  167. struct disasm_line *cursor = ab->selection, *target;
  168. struct browser_disasm_line *btarget, *bcursor;
  169. unsigned int from, to;
  170. if (!cursor || !cursor->ins || !ins__is_jump(cursor->ins) ||
  171. !disasm_line__has_offset(cursor))
  172. return;
  173. target = ab->offsets[cursor->ops.target.offset];
  174. if (!target)
  175. return;
  176. bcursor = disasm_line__browser(cursor);
  177. btarget = disasm_line__browser(target);
  178. if (annotate_browser__opts.hide_src_code) {
  179. from = bcursor->idx_asm;
  180. to = btarget->idx_asm;
  181. } else {
  182. from = (u64)bcursor->idx;
  183. to = (u64)btarget->idx;
  184. }
  185. ui_browser__set_color(browser, HE_COLORSET_CODE);
  186. __ui_browser__line_arrow(browser, 9 + ab->addr_width, from, to);
  187. }
  188. static unsigned int annotate_browser__refresh(struct ui_browser *browser)
  189. {
  190. int ret = ui_browser__list_head_refresh(browser);
  191. if (annotate_browser__opts.jump_arrows)
  192. annotate_browser__draw_current_jump(browser);
  193. ui_browser__set_color(browser, HE_COLORSET_NORMAL);
  194. __ui_browser__vline(browser, 7, 0, browser->height - 1);
  195. return ret;
  196. }
  197. static double disasm_line__calc_percent(struct disasm_line *dl, struct symbol *sym, int evidx)
  198. {
  199. double percent = 0.0;
  200. if (dl->offset != -1) {
  201. int len = sym->end - sym->start;
  202. unsigned int hits = 0;
  203. struct annotation *notes = symbol__annotation(sym);
  204. struct source_line *src_line = notes->src->lines;
  205. struct sym_hist *h = annotation__histogram(notes, evidx);
  206. s64 offset = dl->offset;
  207. struct disasm_line *next;
  208. next = disasm__get_next_ip_line(&notes->src->source, dl);
  209. while (offset < (s64)len &&
  210. (next == NULL || offset < next->offset)) {
  211. if (src_line) {
  212. percent += src_line[offset].percent;
  213. } else
  214. hits += h->addr[offset];
  215. ++offset;
  216. }
  217. /*
  218. * If the percentage wasn't already calculated in
  219. * symbol__get_source_line, do it now:
  220. */
  221. if (src_line == NULL && h->sum)
  222. percent = 100.0 * hits / h->sum;
  223. }
  224. return percent;
  225. }
  226. static void disasm_rb_tree__insert(struct rb_root *root, struct browser_disasm_line *bdl)
  227. {
  228. struct rb_node **p = &root->rb_node;
  229. struct rb_node *parent = NULL;
  230. struct browser_disasm_line *l;
  231. while (*p != NULL) {
  232. parent = *p;
  233. l = rb_entry(parent, struct browser_disasm_line, rb_node);
  234. if (bdl->percent < l->percent)
  235. p = &(*p)->rb_left;
  236. else
  237. p = &(*p)->rb_right;
  238. }
  239. rb_link_node(&bdl->rb_node, parent, p);
  240. rb_insert_color(&bdl->rb_node, root);
  241. }
  242. static void annotate_browser__set_top(struct annotate_browser *browser,
  243. struct disasm_line *pos, u32 idx)
  244. {
  245. unsigned back;
  246. ui_browser__refresh_dimensions(&browser->b);
  247. back = browser->b.height / 2;
  248. browser->b.top_idx = browser->b.index = idx;
  249. while (browser->b.top_idx != 0 && back != 0) {
  250. pos = list_entry(pos->node.prev, struct disasm_line, node);
  251. if (disasm_line__filter(&browser->b, &pos->node))
  252. continue;
  253. --browser->b.top_idx;
  254. --back;
  255. }
  256. browser->b.top = pos;
  257. browser->b.navkeypressed = true;
  258. }
  259. static void annotate_browser__set_rb_top(struct annotate_browser *browser,
  260. struct rb_node *nd)
  261. {
  262. struct browser_disasm_line *bpos;
  263. struct disasm_line *pos;
  264. u32 idx;
  265. bpos = rb_entry(nd, struct browser_disasm_line, rb_node);
  266. pos = ((struct disasm_line *)bpos) - 1;
  267. idx = bpos->idx;
  268. if (annotate_browser__opts.hide_src_code)
  269. idx = bpos->idx_asm;
  270. annotate_browser__set_top(browser, pos, idx);
  271. browser->curr_hot = nd;
  272. }
  273. static void annotate_browser__calc_percent(struct annotate_browser *browser,
  274. int evidx)
  275. {
  276. struct map_symbol *ms = browser->b.priv;
  277. struct symbol *sym = ms->sym;
  278. struct annotation *notes = symbol__annotation(sym);
  279. struct disasm_line *pos;
  280. browser->entries = RB_ROOT;
  281. pthread_mutex_lock(&notes->lock);
  282. list_for_each_entry(pos, &notes->src->source, node) {
  283. struct browser_disasm_line *bpos = disasm_line__browser(pos);
  284. bpos->percent = disasm_line__calc_percent(pos, sym, evidx);
  285. if (bpos->percent < 0.01) {
  286. RB_CLEAR_NODE(&bpos->rb_node);
  287. continue;
  288. }
  289. disasm_rb_tree__insert(&browser->entries, bpos);
  290. }
  291. pthread_mutex_unlock(&notes->lock);
  292. browser->curr_hot = rb_last(&browser->entries);
  293. }
  294. static bool annotate_browser__toggle_source(struct annotate_browser *browser)
  295. {
  296. struct disasm_line *dl;
  297. struct browser_disasm_line *bdl;
  298. off_t offset = browser->b.index - browser->b.top_idx;
  299. browser->b.seek(&browser->b, offset, SEEK_CUR);
  300. dl = list_entry(browser->b.top, struct disasm_line, node);
  301. bdl = disasm_line__browser(dl);
  302. if (annotate_browser__opts.hide_src_code) {
  303. if (bdl->idx_asm < offset)
  304. offset = bdl->idx;
  305. browser->b.nr_entries = browser->nr_entries;
  306. annotate_browser__opts.hide_src_code = false;
  307. browser->b.seek(&browser->b, -offset, SEEK_CUR);
  308. browser->b.top_idx = bdl->idx - offset;
  309. browser->b.index = bdl->idx;
  310. } else {
  311. if (bdl->idx_asm < 0) {
  312. ui_helpline__puts("Only available for assembly lines.");
  313. browser->b.seek(&browser->b, -offset, SEEK_CUR);
  314. return false;
  315. }
  316. if (bdl->idx_asm < offset)
  317. offset = bdl->idx_asm;
  318. browser->b.nr_entries = browser->nr_asm_entries;
  319. annotate_browser__opts.hide_src_code = true;
  320. browser->b.seek(&browser->b, -offset, SEEK_CUR);
  321. browser->b.top_idx = bdl->idx_asm - offset;
  322. browser->b.index = bdl->idx_asm;
  323. }
  324. return true;
  325. }
  326. static void annotate_browser__init_asm_mode(struct annotate_browser *browser)
  327. {
  328. ui_browser__reset_index(&browser->b);
  329. browser->b.nr_entries = browser->nr_asm_entries;
  330. }
  331. static bool annotate_browser__callq(struct annotate_browser *browser,
  332. int evidx, void (*timer)(void *arg),
  333. void *arg, int delay_secs)
  334. {
  335. struct map_symbol *ms = browser->b.priv;
  336. struct disasm_line *dl = browser->selection;
  337. struct symbol *sym = ms->sym;
  338. struct annotation *notes;
  339. struct symbol *target;
  340. u64 ip;
  341. if (!ins__is_call(dl->ins))
  342. return false;
  343. ip = ms->map->map_ip(ms->map, dl->ops.target.addr);
  344. target = map__find_symbol(ms->map, ip, NULL);
  345. if (target == NULL) {
  346. ui_helpline__puts("The called function was not found.");
  347. return true;
  348. }
  349. notes = symbol__annotation(target);
  350. pthread_mutex_lock(&notes->lock);
  351. if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
  352. pthread_mutex_unlock(&notes->lock);
  353. ui__warning("Not enough memory for annotating '%s' symbol!\n",
  354. target->name);
  355. return true;
  356. }
  357. pthread_mutex_unlock(&notes->lock);
  358. symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
  359. ui_browser__show_title(&browser->b, sym->name);
  360. return true;
  361. }
  362. static
  363. struct disasm_line *annotate_browser__find_offset(struct annotate_browser *browser,
  364. s64 offset, s64 *idx)
  365. {
  366. struct map_symbol *ms = browser->b.priv;
  367. struct symbol *sym = ms->sym;
  368. struct annotation *notes = symbol__annotation(sym);
  369. struct disasm_line *pos;
  370. *idx = 0;
  371. list_for_each_entry(pos, &notes->src->source, node) {
  372. if (pos->offset == offset)
  373. return pos;
  374. if (!disasm_line__filter(&browser->b, &pos->node))
  375. ++*idx;
  376. }
  377. return NULL;
  378. }
  379. static bool annotate_browser__jump(struct annotate_browser *browser)
  380. {
  381. struct disasm_line *dl = browser->selection;
  382. s64 idx;
  383. if (!ins__is_jump(dl->ins))
  384. return false;
  385. dl = annotate_browser__find_offset(browser, dl->ops.target.offset, &idx);
  386. if (dl == NULL) {
  387. ui_helpline__puts("Invallid jump offset");
  388. return true;
  389. }
  390. annotate_browser__set_top(browser, dl, idx);
  391. return true;
  392. }
  393. static
  394. struct disasm_line *annotate_browser__find_string(struct annotate_browser *browser,
  395. char *s, s64 *idx)
  396. {
  397. struct map_symbol *ms = browser->b.priv;
  398. struct symbol *sym = ms->sym;
  399. struct annotation *notes = symbol__annotation(sym);
  400. struct disasm_line *pos = browser->selection;
  401. *idx = browser->b.index;
  402. list_for_each_entry_continue(pos, &notes->src->source, node) {
  403. if (disasm_line__filter(&browser->b, &pos->node))
  404. continue;
  405. ++*idx;
  406. if (pos->line && strstr(pos->line, s) != NULL)
  407. return pos;
  408. }
  409. return NULL;
  410. }
  411. static bool __annotate_browser__search(struct annotate_browser *browser)
  412. {
  413. struct disasm_line *dl;
  414. s64 idx;
  415. dl = annotate_browser__find_string(browser, browser->search_bf, &idx);
  416. if (dl == NULL) {
  417. ui_helpline__puts("String not found!");
  418. return false;
  419. }
  420. annotate_browser__set_top(browser, dl, idx);
  421. browser->searching_backwards = false;
  422. return true;
  423. }
  424. static
  425. struct disasm_line *annotate_browser__find_string_reverse(struct annotate_browser *browser,
  426. char *s, s64 *idx)
  427. {
  428. struct map_symbol *ms = browser->b.priv;
  429. struct symbol *sym = ms->sym;
  430. struct annotation *notes = symbol__annotation(sym);
  431. struct disasm_line *pos = browser->selection;
  432. *idx = browser->b.index;
  433. list_for_each_entry_continue_reverse(pos, &notes->src->source, node) {
  434. if (disasm_line__filter(&browser->b, &pos->node))
  435. continue;
  436. --*idx;
  437. if (pos->line && strstr(pos->line, s) != NULL)
  438. return pos;
  439. }
  440. return NULL;
  441. }
  442. static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
  443. {
  444. struct disasm_line *dl;
  445. s64 idx;
  446. dl = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
  447. if (dl == NULL) {
  448. ui_helpline__puts("String not found!");
  449. return false;
  450. }
  451. annotate_browser__set_top(browser, dl, idx);
  452. browser->searching_backwards = true;
  453. return true;
  454. }
  455. static bool annotate_browser__search_window(struct annotate_browser *browser,
  456. int delay_secs)
  457. {
  458. if (ui_browser__input_window("Search", "String: ", browser->search_bf,
  459. "ENTER: OK, ESC: Cancel",
  460. delay_secs * 2) != K_ENTER ||
  461. !*browser->search_bf)
  462. return false;
  463. return true;
  464. }
  465. static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs)
  466. {
  467. if (annotate_browser__search_window(browser, delay_secs))
  468. return __annotate_browser__search(browser);
  469. return false;
  470. }
  471. static bool annotate_browser__continue_search(struct annotate_browser *browser,
  472. int delay_secs)
  473. {
  474. if (!*browser->search_bf)
  475. return annotate_browser__search(browser, delay_secs);
  476. return __annotate_browser__search(browser);
  477. }
  478. static bool annotate_browser__search_reverse(struct annotate_browser *browser,
  479. int delay_secs)
  480. {
  481. if (annotate_browser__search_window(browser, delay_secs))
  482. return __annotate_browser__search_reverse(browser);
  483. return false;
  484. }
  485. static
  486. bool annotate_browser__continue_search_reverse(struct annotate_browser *browser,
  487. int delay_secs)
  488. {
  489. if (!*browser->search_bf)
  490. return annotate_browser__search_reverse(browser, delay_secs);
  491. return __annotate_browser__search_reverse(browser);
  492. }
  493. static void annotate_browser__update_addr_width(struct annotate_browser *browser)
  494. {
  495. if (annotate_browser__opts.use_offset)
  496. browser->target_width = browser->min_addr_width;
  497. else
  498. browser->target_width = browser->max_addr_width;
  499. browser->addr_width = browser->target_width;
  500. if (annotate_browser__opts.show_nr_jumps)
  501. browser->addr_width += browser->jumps_width + 1;
  502. }
  503. static int annotate_browser__run(struct annotate_browser *browser, int evidx,
  504. void(*timer)(void *arg),
  505. void *arg, int delay_secs)
  506. {
  507. struct rb_node *nd = NULL;
  508. struct map_symbol *ms = browser->b.priv;
  509. struct symbol *sym = ms->sym;
  510. const char *help = "Press 'h' for help on key bindings";
  511. int key;
  512. if (ui_browser__show(&browser->b, sym->name, help) < 0)
  513. return -1;
  514. annotate_browser__calc_percent(browser, evidx);
  515. if (browser->curr_hot) {
  516. annotate_browser__set_rb_top(browser, browser->curr_hot);
  517. browser->b.navkeypressed = false;
  518. }
  519. nd = browser->curr_hot;
  520. while (1) {
  521. key = ui_browser__run(&browser->b, delay_secs);
  522. if (delay_secs != 0) {
  523. annotate_browser__calc_percent(browser, evidx);
  524. /*
  525. * Current line focus got out of the list of most active
  526. * lines, NULL it so that if TAB|UNTAB is pressed, we
  527. * move to curr_hot (current hottest line).
  528. */
  529. if (nd != NULL && RB_EMPTY_NODE(nd))
  530. nd = NULL;
  531. }
  532. switch (key) {
  533. case K_TIMER:
  534. if (timer != NULL)
  535. timer(arg);
  536. if (delay_secs != 0)
  537. symbol__annotate_decay_histogram(sym, evidx);
  538. continue;
  539. case K_TAB:
  540. if (nd != NULL) {
  541. nd = rb_prev(nd);
  542. if (nd == NULL)
  543. nd = rb_last(&browser->entries);
  544. } else
  545. nd = browser->curr_hot;
  546. break;
  547. case K_UNTAB:
  548. if (nd != NULL)
  549. nd = rb_next(nd);
  550. if (nd == NULL)
  551. nd = rb_first(&browser->entries);
  552. else
  553. nd = browser->curr_hot;
  554. break;
  555. case K_F1:
  556. case 'h':
  557. ui_browser__help_window(&browser->b,
  558. "UP/DOWN/PGUP\n"
  559. "PGDN/SPACE Navigate\n"
  560. "q/ESC/CTRL+C Exit\n\n"
  561. "-> Go to target\n"
  562. "<- Exit\n"
  563. "H Cycle thru hottest instructions\n"
  564. "j Toggle showing jump to target arrows\n"
  565. "J Toggle showing number of jump sources on targets\n"
  566. "n Search next string\n"
  567. "o Toggle disassembler output/simplified view\n"
  568. "s Toggle source code view\n"
  569. "/ Search string\n"
  570. "? Search previous string\n");
  571. continue;
  572. case 'H':
  573. nd = browser->curr_hot;
  574. break;
  575. case 's':
  576. if (annotate_browser__toggle_source(browser))
  577. ui_helpline__puts(help);
  578. continue;
  579. case 'o':
  580. annotate_browser__opts.use_offset = !annotate_browser__opts.use_offset;
  581. annotate_browser__update_addr_width(browser);
  582. continue;
  583. case 'j':
  584. annotate_browser__opts.jump_arrows = !annotate_browser__opts.jump_arrows;
  585. continue;
  586. case 'J':
  587. annotate_browser__opts.show_nr_jumps = !annotate_browser__opts.show_nr_jumps;
  588. annotate_browser__update_addr_width(browser);
  589. continue;
  590. case '/':
  591. if (annotate_browser__search(browser, delay_secs)) {
  592. show_help:
  593. ui_helpline__puts(help);
  594. }
  595. continue;
  596. case 'n':
  597. if (browser->searching_backwards ?
  598. annotate_browser__continue_search_reverse(browser, delay_secs) :
  599. annotate_browser__continue_search(browser, delay_secs))
  600. goto show_help;
  601. continue;
  602. case '?':
  603. if (annotate_browser__search_reverse(browser, delay_secs))
  604. goto show_help;
  605. continue;
  606. case 'D': {
  607. static int seq;
  608. ui_helpline__pop();
  609. ui_helpline__fpush("%d: nr_ent=%d, height=%d, idx=%d, top_idx=%d, nr_asm_entries=%d",
  610. seq++, browser->b.nr_entries,
  611. browser->b.height,
  612. browser->b.index,
  613. browser->b.top_idx,
  614. browser->nr_asm_entries);
  615. }
  616. continue;
  617. case K_ENTER:
  618. case K_RIGHT:
  619. if (browser->selection == NULL)
  620. ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
  621. else if (browser->selection->offset == -1)
  622. ui_helpline__puts("Actions are only available for assembly lines.");
  623. else if (!browser->selection->ins) {
  624. if (strcmp(browser->selection->name, "retq"))
  625. goto show_sup_ins;
  626. goto out;
  627. } else if (!(annotate_browser__jump(browser) ||
  628. annotate_browser__callq(browser, evidx, timer, arg, delay_secs))) {
  629. show_sup_ins:
  630. ui_helpline__puts("Actions are only available for 'callq', 'retq' & jump instructions.");
  631. }
  632. continue;
  633. case K_LEFT:
  634. case K_ESC:
  635. case 'q':
  636. case CTRL('c'):
  637. goto out;
  638. default:
  639. continue;
  640. }
  641. if (nd != NULL)
  642. annotate_browser__set_rb_top(browser, nd);
  643. }
  644. out:
  645. ui_browser__hide(&browser->b);
  646. return key;
  647. }
  648. int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
  649. void(*timer)(void *arg), void *arg, int delay_secs)
  650. {
  651. return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
  652. timer, arg, delay_secs);
  653. }
  654. static void annotate_browser__mark_jump_targets(struct annotate_browser *browser,
  655. size_t size)
  656. {
  657. u64 offset;
  658. for (offset = 0; offset < size; ++offset) {
  659. struct disasm_line *dl = browser->offsets[offset], *dlt;
  660. struct browser_disasm_line *bdlt;
  661. if (!dl || !dl->ins || !ins__is_jump(dl->ins) ||
  662. !disasm_line__has_offset(dl))
  663. continue;
  664. if (dl->ops.target.offset >= size) {
  665. ui__error("jump to after symbol!\n"
  666. "size: %zx, jump target: %" PRIx64,
  667. size, dl->ops.target.offset);
  668. continue;
  669. }
  670. dlt = browser->offsets[dl->ops.target.offset];
  671. /*
  672. * FIXME: Oops, no jump target? Buggy disassembler? Or do we
  673. * have to adjust to the previous offset?
  674. */
  675. if (dlt == NULL)
  676. continue;
  677. bdlt = disasm_line__browser(dlt);
  678. if (++bdlt->jump_sources > browser->max_jump_sources)
  679. browser->max_jump_sources = bdlt->jump_sources;
  680. ++browser->nr_jumps;
  681. }
  682. }
  683. static inline int width_jumps(int n)
  684. {
  685. if (n >= 100)
  686. return 5;
  687. if (n / 10)
  688. return 2;
  689. return 1;
  690. }
  691. int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
  692. void(*timer)(void *arg), void *arg,
  693. int delay_secs)
  694. {
  695. struct disasm_line *pos, *n;
  696. struct annotation *notes;
  697. size_t size;
  698. struct map_symbol ms = {
  699. .map = map,
  700. .sym = sym,
  701. };
  702. struct annotate_browser browser = {
  703. .b = {
  704. .refresh = annotate_browser__refresh,
  705. .seek = ui_browser__list_head_seek,
  706. .write = annotate_browser__write,
  707. .filter = disasm_line__filter,
  708. .priv = &ms,
  709. .use_navkeypressed = true,
  710. },
  711. };
  712. int ret = -1;
  713. if (sym == NULL)
  714. return -1;
  715. size = symbol__size(sym);
  716. if (map->dso->annotate_warned)
  717. return -1;
  718. browser.offsets = zalloc(size * sizeof(struct disasm_line *));
  719. if (browser.offsets == NULL) {
  720. ui__error("Not enough memory!");
  721. return -1;
  722. }
  723. if (symbol__annotate(sym, map, sizeof(struct browser_disasm_line)) < 0) {
  724. ui__error("%s", ui_helpline__last_msg);
  725. goto out_free_offsets;
  726. }
  727. ui_helpline__push("Press <- or ESC to exit");
  728. notes = symbol__annotation(sym);
  729. browser.start = map__rip_2objdump(map, sym->start);
  730. list_for_each_entry(pos, &notes->src->source, node) {
  731. struct browser_disasm_line *bpos;
  732. size_t line_len = strlen(pos->line);
  733. if (browser.b.width < line_len)
  734. browser.b.width = line_len;
  735. bpos = disasm_line__browser(pos);
  736. bpos->idx = browser.nr_entries++;
  737. if (pos->offset != -1) {
  738. bpos->idx_asm = browser.nr_asm_entries++;
  739. /*
  740. * FIXME: short term bandaid to cope with assembly
  741. * routines that comes with labels in the same column
  742. * as the address in objdump, sigh.
  743. *
  744. * E.g. copy_user_generic_unrolled
  745. */
  746. if (pos->offset < (s64)size)
  747. browser.offsets[pos->offset] = pos;
  748. } else
  749. bpos->idx_asm = -1;
  750. }
  751. annotate_browser__mark_jump_targets(&browser, size);
  752. browser.addr_width = browser.target_width = browser.min_addr_width = hex_width(size);
  753. browser.max_addr_width = hex_width(sym->end);
  754. browser.jumps_width = width_jumps(browser.max_jump_sources);
  755. browser.b.nr_entries = browser.nr_entries;
  756. browser.b.entries = &notes->src->source,
  757. browser.b.width += 18; /* Percentage */
  758. if (annotate_browser__opts.hide_src_code)
  759. annotate_browser__init_asm_mode(&browser);
  760. annotate_browser__update_addr_width(&browser);
  761. ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
  762. list_for_each_entry_safe(pos, n, &notes->src->source, node) {
  763. list_del(&pos->node);
  764. disasm_line__free(pos);
  765. }
  766. out_free_offsets:
  767. free(browser.offsets);
  768. return ret;
  769. }
  770. #define ANNOTATE_CFG(n) \
  771. { .name = #n, .value = &annotate_browser__opts.n, }
  772. /*
  773. * Keep the entries sorted, they are bsearch'ed
  774. */
  775. static struct annotate__config {
  776. const char *name;
  777. bool *value;
  778. } annotate__configs[] = {
  779. ANNOTATE_CFG(hide_src_code),
  780. ANNOTATE_CFG(jump_arrows),
  781. ANNOTATE_CFG(show_nr_jumps),
  782. ANNOTATE_CFG(use_offset),
  783. };
  784. #undef ANNOTATE_CFG
  785. static int annotate_config__cmp(const void *name, const void *cfgp)
  786. {
  787. const struct annotate__config *cfg = cfgp;
  788. return strcmp(name, cfg->name);
  789. }
  790. static int annotate__config(const char *var, const char *value,
  791. void *data __maybe_unused)
  792. {
  793. struct annotate__config *cfg;
  794. const char *name;
  795. if (prefixcmp(var, "annotate.") != 0)
  796. return 0;
  797. name = var + 9;
  798. cfg = bsearch(name, annotate__configs, ARRAY_SIZE(annotate__configs),
  799. sizeof(struct annotate__config), annotate_config__cmp);
  800. if (cfg == NULL)
  801. return -1;
  802. *cfg->value = perf_config_bool(name, value);
  803. return 0;
  804. }
  805. void annotate_browser__init(void)
  806. {
  807. perf_config(annotate__config, NULL);
  808. }