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