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 addr_width;
  34. u8 min_addr_width;
  35. u8 max_addr_width;
  36. char search_bf[128];
  37. };
  38. static inline struct browser_disasm_line *disasm_line__browser(struct disasm_line *dl)
  39. {
  40. return (struct browser_disasm_line *)(dl + 1);
  41. }
  42. static bool disasm_line__filter(struct ui_browser *browser, void *entry)
  43. {
  44. struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
  45. if (ab->hide_src_code) {
  46. struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
  47. return dl->offset == -1;
  48. }
  49. return false;
  50. }
  51. static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
  52. {
  53. struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
  54. struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
  55. struct browser_disasm_line *bdl = disasm_line__browser(dl);
  56. bool current_entry = ui_browser__is_current_entry(self, row);
  57. bool change_color = (!ab->hide_src_code &&
  58. (!current_entry || (self->use_navkeypressed &&
  59. !self->navkeypressed)));
  60. int width = self->width, printed;
  61. char bf[256];
  62. if (dl->offset != -1 && bdl->percent != 0.0) {
  63. ui_browser__set_percent_color(self, bdl->percent, current_entry);
  64. slsmg_printf("%6.2f ", bdl->percent);
  65. } else {
  66. ui_browser__set_percent_color(self, 0, current_entry);
  67. slsmg_write_nstring(" ", 7);
  68. }
  69. SLsmg_write_char(' ');
  70. /* The scroll bar isn't being used */
  71. if (!self->navkeypressed)
  72. width += 1;
  73. if (!*dl->line)
  74. slsmg_write_nstring(" ", width - 7);
  75. else if (dl->offset == -1) {
  76. printed = scnprintf(bf, sizeof(bf), "%*s ",
  77. ab->addr_width, " ");
  78. slsmg_write_nstring(bf, printed);
  79. slsmg_write_nstring(dl->line, width - printed - 6);
  80. } else {
  81. u64 addr = dl->offset;
  82. int color = -1;
  83. if (!ab->use_offset)
  84. addr += ab->start;
  85. if (!ab->use_offset) {
  86. printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr);
  87. } else {
  88. if (bdl->jump_target) {
  89. printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ",
  90. ab->addr_width, addr);
  91. } else {
  92. printed = scnprintf(bf, sizeof(bf), "%*s ",
  93. ab->addr_width, " ");
  94. }
  95. }
  96. if (change_color)
  97. color = ui_browser__set_color(self, HE_COLORSET_ADDR);
  98. slsmg_write_nstring(bf, printed);
  99. if (change_color)
  100. ui_browser__set_color(self, color);
  101. if (dl->ins && dl->ins->ops->scnprintf) {
  102. if (ins__is_jump(dl->ins)) {
  103. bool fwd = dl->ops.target.offset > (u64)dl->offset;
  104. ui_browser__write_graph(self, fwd ? SLSMG_DARROW_CHAR :
  105. SLSMG_UARROW_CHAR);
  106. SLsmg_write_char(' ');
  107. } else if (ins__is_call(dl->ins)) {
  108. ui_browser__write_graph(self, SLSMG_RARROW_CHAR);
  109. SLsmg_write_char(' ');
  110. } else {
  111. slsmg_write_nstring(" ", 2);
  112. }
  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. }
  121. disasm_line__scnprintf(dl, bf, sizeof(bf), !ab->use_offset);
  122. slsmg_write_nstring(bf, width - 10 - 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;
  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. __ui_browser__line_arrow(browser, 9 + ab->addr_width, from, to);
  150. }
  151. static unsigned int annotate_browser__refresh(struct ui_browser *browser)
  152. {
  153. struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
  154. int ret = ui_browser__list_head_refresh(browser);
  155. if (ab->jump_arrows)
  156. annotate_browser__draw_current_jump(browser);
  157. ui_browser__set_color(browser, HE_COLORSET_NORMAL);
  158. __ui_browser__vline(browser, 7, 0, browser->height - 1);
  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: Hottest line, ->/ENTER: Line action, "
  457. "O: Offset view, "
  458. "S: Source 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. if (self->use_offset)
  516. self->addr_width = self->min_addr_width;
  517. else
  518. self->addr_width = self->max_addr_width;
  519. continue;
  520. case 'j':
  521. self->jump_arrows = !self->jump_arrows;
  522. continue;
  523. case '/':
  524. if (annotate_browser__search(self, delay_secs)) {
  525. show_help:
  526. ui_helpline__puts(help);
  527. }
  528. continue;
  529. case 'n':
  530. if (self->searching_backwards ?
  531. annotate_browser__continue_search_reverse(self, delay_secs) :
  532. annotate_browser__continue_search(self, delay_secs))
  533. goto show_help;
  534. continue;
  535. case '?':
  536. if (annotate_browser__search_reverse(self, delay_secs))
  537. goto show_help;
  538. continue;
  539. case K_ENTER:
  540. case K_RIGHT:
  541. if (self->selection == NULL)
  542. ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
  543. else if (self->selection->offset == -1)
  544. ui_helpline__puts("Actions are only available for assembly lines.");
  545. else if (!self->selection->ins) {
  546. if (strcmp(self->selection->name, "retq"))
  547. goto show_sup_ins;
  548. goto out;
  549. } else if (!(annotate_browser__jump(self) ||
  550. annotate_browser__callq(self, evidx, timer, arg, delay_secs))) {
  551. show_sup_ins:
  552. ui_helpline__puts("Actions are only available for 'callq', 'retq' & jump instructions.");
  553. }
  554. continue;
  555. case K_LEFT:
  556. case K_ESC:
  557. case 'q':
  558. case CTRL('c'):
  559. goto out;
  560. default:
  561. continue;
  562. }
  563. if (nd != NULL)
  564. annotate_browser__set_rb_top(self, nd);
  565. }
  566. out:
  567. ui_browser__hide(&self->b);
  568. return key;
  569. }
  570. int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
  571. void(*timer)(void *arg), void *arg, int delay_secs)
  572. {
  573. return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
  574. timer, arg, delay_secs);
  575. }
  576. static void annotate_browser__mark_jump_targets(struct annotate_browser *browser,
  577. size_t size)
  578. {
  579. u64 offset;
  580. for (offset = 0; offset < size; ++offset) {
  581. struct disasm_line *dl = browser->offsets[offset], *dlt;
  582. struct browser_disasm_line *bdlt;
  583. if (!dl || !dl->ins || !ins__is_jump(dl->ins) ||
  584. !disasm_line__has_offset(dl))
  585. continue;
  586. if (dl->ops.target.offset >= size) {
  587. ui__error("jump to after symbol!\n"
  588. "size: %zx, jump target: %" PRIx64,
  589. size, dl->ops.target.offset);
  590. continue;
  591. }
  592. dlt = browser->offsets[dl->ops.target.offset];
  593. /*
  594. * FIXME: Oops, no jump target? Buggy disassembler? Or do we
  595. * have to adjust to the previous offset?
  596. */
  597. if (dlt == NULL)
  598. continue;
  599. bdlt = disasm_line__browser(dlt);
  600. bdlt->jump_target = true;
  601. }
  602. }
  603. int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
  604. void(*timer)(void *arg), void *arg,
  605. int delay_secs)
  606. {
  607. struct disasm_line *pos, *n;
  608. struct annotation *notes;
  609. const size_t size = symbol__size(sym);
  610. struct map_symbol ms = {
  611. .map = map,
  612. .sym = sym,
  613. };
  614. struct annotate_browser browser = {
  615. .b = {
  616. .refresh = annotate_browser__refresh,
  617. .seek = ui_browser__list_head_seek,
  618. .write = annotate_browser__write,
  619. .filter = disasm_line__filter,
  620. .priv = &ms,
  621. .use_navkeypressed = true,
  622. },
  623. .use_offset = true,
  624. .jump_arrows = true,
  625. };
  626. int ret = -1;
  627. if (sym == NULL)
  628. return -1;
  629. if (map->dso->annotate_warned)
  630. return -1;
  631. browser.offsets = zalloc(size * sizeof(struct disasm_line *));
  632. if (browser.offsets == NULL) {
  633. ui__error("Not enough memory!");
  634. return -1;
  635. }
  636. if (symbol__annotate(sym, map, sizeof(struct browser_disasm_line)) < 0) {
  637. ui__error("%s", ui_helpline__last_msg);
  638. goto out_free_offsets;
  639. }
  640. ui_helpline__push("Press <- or ESC to exit");
  641. notes = symbol__annotation(sym);
  642. browser.start = map__rip_2objdump(map, sym->start);
  643. list_for_each_entry(pos, &notes->src->source, node) {
  644. struct browser_disasm_line *bpos;
  645. size_t line_len = strlen(pos->line);
  646. if (browser.b.width < line_len)
  647. browser.b.width = line_len;
  648. bpos = disasm_line__browser(pos);
  649. bpos->idx = browser.nr_entries++;
  650. if (pos->offset != -1) {
  651. bpos->idx_asm = browser.nr_asm_entries++;
  652. /*
  653. * FIXME: short term bandaid to cope with assembly
  654. * routines that comes with labels in the same column
  655. * as the address in objdump, sigh.
  656. *
  657. * E.g. copy_user_generic_unrolled
  658. */
  659. if (pos->offset < (s64)size)
  660. browser.offsets[pos->offset] = pos;
  661. } else
  662. bpos->idx_asm = -1;
  663. }
  664. annotate_browser__mark_jump_targets(&browser, size);
  665. browser.addr_width = browser.min_addr_width = hex_width(size);
  666. browser.max_addr_width = hex_width(sym->end);
  667. browser.b.nr_entries = browser.nr_entries;
  668. browser.b.entries = &notes->src->source,
  669. browser.b.width += 18; /* Percentage */
  670. ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
  671. list_for_each_entry_safe(pos, n, &notes->src->source, node) {
  672. list_del(&pos->node);
  673. disasm_line__free(pos);
  674. }
  675. out_free_offsets:
  676. free(browser.offsets);
  677. return ret;
  678. }