annotate.c 20 KB

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