annotate.c 20 KB

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