annotate.c 22 KB

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