annotate.c 20 KB

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