annotate.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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 annotate_browser {
  14. struct ui_browser b;
  15. struct rb_root entries;
  16. struct rb_node *curr_hot;
  17. struct disasm_line *selection;
  18. u64 start;
  19. int nr_asm_entries;
  20. int nr_entries;
  21. bool hide_src_code;
  22. bool use_offset;
  23. bool searching_backwards;
  24. char search_bf[128];
  25. };
  26. struct browser_disasm_line {
  27. struct rb_node rb_node;
  28. double percent;
  29. u32 idx;
  30. int idx_asm;
  31. };
  32. static inline struct browser_disasm_line *disasm_line__browser(struct disasm_line *dl)
  33. {
  34. return (struct browser_disasm_line *)(dl + 1);
  35. }
  36. static bool disasm_line__filter(struct ui_browser *browser, void *entry)
  37. {
  38. struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
  39. if (ab->hide_src_code) {
  40. struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
  41. return dl->offset == -1;
  42. }
  43. return false;
  44. }
  45. static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
  46. {
  47. struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
  48. struct disasm_line *dl = list_entry(entry, struct disasm_line, node);
  49. bool current_entry = ui_browser__is_current_entry(self, row);
  50. bool change_color = (!ab->hide_src_code &&
  51. (!current_entry || (self->use_navkeypressed &&
  52. !self->navkeypressed)));
  53. int width = self->width;
  54. if (dl->offset != -1) {
  55. struct browser_disasm_line *bdl = disasm_line__browser(dl);
  56. ui_browser__set_percent_color(self, bdl->percent, current_entry);
  57. slsmg_printf(" %7.2f ", bdl->percent);
  58. } else {
  59. ui_browser__set_percent_color(self, 0, current_entry);
  60. slsmg_write_nstring(" ", 9);
  61. }
  62. SLsmg_write_char(':');
  63. slsmg_write_nstring(" ", 8);
  64. /* The scroll bar isn't being used */
  65. if (!self->navkeypressed)
  66. width += 1;
  67. if (dl->offset != -1 && change_color)
  68. ui_browser__set_color(self, HE_COLORSET_CODE);
  69. if (!*dl->line)
  70. slsmg_write_nstring(" ", width - 18);
  71. else if (dl->offset == -1)
  72. slsmg_write_nstring(dl->line, width - 18);
  73. else {
  74. char bf[256], *line = dl->line;
  75. u64 addr = dl->offset;
  76. int printed, color = -1;
  77. if (!ab->use_offset)
  78. addr += ab->start;
  79. printed = scnprintf(bf, sizeof(bf), " %" PRIx64 ":", addr);
  80. if (change_color)
  81. color = ui_browser__set_color(self, HE_COLORSET_ADDR);
  82. slsmg_write_nstring(bf, printed);
  83. if (change_color)
  84. ui_browser__set_color(self, color);
  85. if (dl->ins && dl->ins->ops->scnprintf) {
  86. dl->ins->ops->scnprintf(dl->ins, bf, sizeof(bf),
  87. !ab->use_offset ? dl->operands : NULL,
  88. dl->target);
  89. line = bf;
  90. slsmg_write_nstring(" ", 7);
  91. printed += 7;
  92. }
  93. slsmg_write_nstring(line, width - 18 - printed);
  94. }
  95. if (current_entry)
  96. ab->selection = dl;
  97. }
  98. static double disasm_line__calc_percent(struct disasm_line *dl, struct symbol *sym, int evidx)
  99. {
  100. double percent = 0.0;
  101. if (dl->offset != -1) {
  102. int len = sym->end - sym->start;
  103. unsigned int hits = 0;
  104. struct annotation *notes = symbol__annotation(sym);
  105. struct source_line *src_line = notes->src->lines;
  106. struct sym_hist *h = annotation__histogram(notes, evidx);
  107. s64 offset = dl->offset;
  108. struct disasm_line *next;
  109. next = disasm__get_next_ip_line(&notes->src->source, dl);
  110. while (offset < (s64)len &&
  111. (next == NULL || offset < next->offset)) {
  112. if (src_line) {
  113. percent += src_line[offset].percent;
  114. } else
  115. hits += h->addr[offset];
  116. ++offset;
  117. }
  118. /*
  119. * If the percentage wasn't already calculated in
  120. * symbol__get_source_line, do it now:
  121. */
  122. if (src_line == NULL && h->sum)
  123. percent = 100.0 * hits / h->sum;
  124. }
  125. return percent;
  126. }
  127. static void disasm_rb_tree__insert(struct rb_root *root, struct browser_disasm_line *bdl)
  128. {
  129. struct rb_node **p = &root->rb_node;
  130. struct rb_node *parent = NULL;
  131. struct browser_disasm_line *l;
  132. while (*p != NULL) {
  133. parent = *p;
  134. l = rb_entry(parent, struct browser_disasm_line, rb_node);
  135. if (bdl->percent < l->percent)
  136. p = &(*p)->rb_left;
  137. else
  138. p = &(*p)->rb_right;
  139. }
  140. rb_link_node(&bdl->rb_node, parent, p);
  141. rb_insert_color(&bdl->rb_node, root);
  142. }
  143. static void annotate_browser__set_top(struct annotate_browser *self,
  144. struct disasm_line *pos, u32 idx)
  145. {
  146. unsigned back;
  147. ui_browser__refresh_dimensions(&self->b);
  148. back = self->b.height / 2;
  149. self->b.top_idx = self->b.index = idx;
  150. while (self->b.top_idx != 0 && back != 0) {
  151. pos = list_entry(pos->node.prev, struct disasm_line, node);
  152. if (disasm_line__filter(&self->b, &pos->node))
  153. continue;
  154. --self->b.top_idx;
  155. --back;
  156. }
  157. self->b.top = pos;
  158. self->b.navkeypressed = true;
  159. }
  160. static void annotate_browser__set_rb_top(struct annotate_browser *browser,
  161. struct rb_node *nd)
  162. {
  163. struct browser_disasm_line *bpos;
  164. struct disasm_line *pos;
  165. bpos = rb_entry(nd, struct browser_disasm_line, rb_node);
  166. pos = ((struct disasm_line *)bpos) - 1;
  167. annotate_browser__set_top(browser, pos, bpos->idx);
  168. browser->curr_hot = nd;
  169. }
  170. static void annotate_browser__calc_percent(struct annotate_browser *browser,
  171. int evidx)
  172. {
  173. struct map_symbol *ms = browser->b.priv;
  174. struct symbol *sym = ms->sym;
  175. struct annotation *notes = symbol__annotation(sym);
  176. struct disasm_line *pos;
  177. browser->entries = RB_ROOT;
  178. pthread_mutex_lock(&notes->lock);
  179. list_for_each_entry(pos, &notes->src->source, node) {
  180. struct browser_disasm_line *bpos = disasm_line__browser(pos);
  181. bpos->percent = disasm_line__calc_percent(pos, sym, evidx);
  182. if (bpos->percent < 0.01) {
  183. RB_CLEAR_NODE(&bpos->rb_node);
  184. continue;
  185. }
  186. disasm_rb_tree__insert(&browser->entries, bpos);
  187. }
  188. pthread_mutex_unlock(&notes->lock);
  189. browser->curr_hot = rb_last(&browser->entries);
  190. }
  191. static bool annotate_browser__toggle_source(struct annotate_browser *browser)
  192. {
  193. struct disasm_line *dl;
  194. struct browser_disasm_line *bdl;
  195. off_t offset = browser->b.index - browser->b.top_idx;
  196. browser->b.seek(&browser->b, offset, SEEK_CUR);
  197. dl = list_entry(browser->b.top, struct disasm_line, node);
  198. bdl = disasm_line__browser(dl);
  199. if (browser->hide_src_code) {
  200. if (bdl->idx_asm < offset)
  201. offset = bdl->idx;
  202. browser->b.nr_entries = browser->nr_entries;
  203. browser->hide_src_code = false;
  204. browser->b.seek(&browser->b, -offset, SEEK_CUR);
  205. browser->b.top_idx = bdl->idx - offset;
  206. browser->b.index = bdl->idx;
  207. } else {
  208. if (bdl->idx_asm < 0) {
  209. ui_helpline__puts("Only available for assembly lines.");
  210. browser->b.seek(&browser->b, -offset, SEEK_CUR);
  211. return false;
  212. }
  213. if (bdl->idx_asm < offset)
  214. offset = bdl->idx_asm;
  215. browser->b.nr_entries = browser->nr_asm_entries;
  216. browser->hide_src_code = true;
  217. browser->b.seek(&browser->b, -offset, SEEK_CUR);
  218. browser->b.top_idx = bdl->idx_asm - offset;
  219. browser->b.index = bdl->idx_asm;
  220. }
  221. return true;
  222. }
  223. static bool annotate_browser__callq(struct annotate_browser *browser,
  224. int evidx, void (*timer)(void *arg),
  225. void *arg, int delay_secs)
  226. {
  227. struct map_symbol *ms = browser->b.priv;
  228. struct disasm_line *dl = browser->selection;
  229. struct symbol *sym = ms->sym;
  230. struct annotation *notes;
  231. struct symbol *target;
  232. u64 ip;
  233. if (!ins__is_call(dl->ins))
  234. return false;
  235. ip = ms->map->map_ip(ms->map, dl->target);
  236. target = map__find_symbol(ms->map, ip, NULL);
  237. if (target == NULL) {
  238. ui_helpline__puts("The called function was not found.");
  239. return true;
  240. }
  241. notes = symbol__annotation(target);
  242. pthread_mutex_lock(&notes->lock);
  243. if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
  244. pthread_mutex_unlock(&notes->lock);
  245. ui__warning("Not enough memory for annotating '%s' symbol!\n",
  246. target->name);
  247. return true;
  248. }
  249. pthread_mutex_unlock(&notes->lock);
  250. symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
  251. ui_browser__show_title(&browser->b, sym->name);
  252. return true;
  253. }
  254. static
  255. struct disasm_line *annotate_browser__find_offset(struct annotate_browser *browser,
  256. s64 offset, s64 *idx)
  257. {
  258. struct map_symbol *ms = browser->b.priv;
  259. struct symbol *sym = ms->sym;
  260. struct annotation *notes = symbol__annotation(sym);
  261. struct disasm_line *pos;
  262. *idx = 0;
  263. list_for_each_entry(pos, &notes->src->source, node) {
  264. if (pos->offset == offset)
  265. return pos;
  266. if (!disasm_line__filter(&browser->b, &pos->node))
  267. ++*idx;
  268. }
  269. return NULL;
  270. }
  271. static bool annotate_browser__jump(struct annotate_browser *browser)
  272. {
  273. struct disasm_line *dl = browser->selection;
  274. s64 idx;
  275. if (!ins__is_jump(dl->ins))
  276. return false;
  277. dl = annotate_browser__find_offset(browser, dl->target, &idx);
  278. if (dl == NULL) {
  279. ui_helpline__puts("Invallid jump offset");
  280. return true;
  281. }
  282. annotate_browser__set_top(browser, dl, idx);
  283. return true;
  284. }
  285. static
  286. struct disasm_line *annotate_browser__find_string(struct annotate_browser *browser,
  287. char *s, s64 *idx)
  288. {
  289. struct map_symbol *ms = browser->b.priv;
  290. struct symbol *sym = ms->sym;
  291. struct annotation *notes = symbol__annotation(sym);
  292. struct disasm_line *pos = browser->selection;
  293. *idx = browser->b.index;
  294. list_for_each_entry_continue(pos, &notes->src->source, node) {
  295. if (disasm_line__filter(&browser->b, &pos->node))
  296. continue;
  297. ++*idx;
  298. if (pos->line && strstr(pos->line, s) != NULL)
  299. return pos;
  300. }
  301. return NULL;
  302. }
  303. static bool __annotate_browser__search(struct annotate_browser *browser)
  304. {
  305. struct disasm_line *dl;
  306. s64 idx;
  307. dl = annotate_browser__find_string(browser, browser->search_bf, &idx);
  308. if (dl == NULL) {
  309. ui_helpline__puts("String not found!");
  310. return false;
  311. }
  312. annotate_browser__set_top(browser, dl, idx);
  313. browser->searching_backwards = false;
  314. return true;
  315. }
  316. static
  317. struct disasm_line *annotate_browser__find_string_reverse(struct annotate_browser *browser,
  318. char *s, 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 = browser->selection;
  324. *idx = browser->b.index;
  325. list_for_each_entry_continue_reverse(pos, &notes->src->source, node) {
  326. if (disasm_line__filter(&browser->b, &pos->node))
  327. continue;
  328. --*idx;
  329. if (pos->line && strstr(pos->line, s) != NULL)
  330. return pos;
  331. }
  332. return NULL;
  333. }
  334. static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
  335. {
  336. struct disasm_line *dl;
  337. s64 idx;
  338. dl = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
  339. if (dl == NULL) {
  340. ui_helpline__puts("String not found!");
  341. return false;
  342. }
  343. annotate_browser__set_top(browser, dl, idx);
  344. browser->searching_backwards = true;
  345. return true;
  346. }
  347. static bool annotate_browser__search_window(struct annotate_browser *browser,
  348. int delay_secs)
  349. {
  350. if (ui_browser__input_window("Search", "String: ", browser->search_bf,
  351. "ENTER: OK, ESC: Cancel",
  352. delay_secs * 2) != K_ENTER ||
  353. !*browser->search_bf)
  354. return false;
  355. return true;
  356. }
  357. static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs)
  358. {
  359. if (annotate_browser__search_window(browser, delay_secs))
  360. return __annotate_browser__search(browser);
  361. return false;
  362. }
  363. static bool annotate_browser__continue_search(struct annotate_browser *browser,
  364. int delay_secs)
  365. {
  366. if (!*browser->search_bf)
  367. return annotate_browser__search(browser, delay_secs);
  368. return __annotate_browser__search(browser);
  369. }
  370. static bool annotate_browser__search_reverse(struct annotate_browser *browser,
  371. int delay_secs)
  372. {
  373. if (annotate_browser__search_window(browser, delay_secs))
  374. return __annotate_browser__search_reverse(browser);
  375. return false;
  376. }
  377. static
  378. bool annotate_browser__continue_search_reverse(struct annotate_browser *browser,
  379. int delay_secs)
  380. {
  381. if (!*browser->search_bf)
  382. return annotate_browser__search_reverse(browser, delay_secs);
  383. return __annotate_browser__search_reverse(browser);
  384. }
  385. static int annotate_browser__run(struct annotate_browser *self, int evidx,
  386. void(*timer)(void *arg),
  387. void *arg, int delay_secs)
  388. {
  389. struct rb_node *nd = NULL;
  390. struct map_symbol *ms = self->b.priv;
  391. struct symbol *sym = ms->sym;
  392. const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
  393. "H: Go to hottest line, ->/ENTER: Line action, "
  394. "O: Toggle offset view, "
  395. "S: Toggle source code view";
  396. int key;
  397. if (ui_browser__show(&self->b, sym->name, help) < 0)
  398. return -1;
  399. annotate_browser__calc_percent(self, evidx);
  400. if (self->curr_hot) {
  401. annotate_browser__set_rb_top(self, self->curr_hot);
  402. self->b.navkeypressed = false;
  403. }
  404. nd = self->curr_hot;
  405. while (1) {
  406. key = ui_browser__run(&self->b, delay_secs);
  407. if (delay_secs != 0) {
  408. annotate_browser__calc_percent(self, evidx);
  409. /*
  410. * Current line focus got out of the list of most active
  411. * lines, NULL it so that if TAB|UNTAB is pressed, we
  412. * move to curr_hot (current hottest line).
  413. */
  414. if (nd != NULL && RB_EMPTY_NODE(nd))
  415. nd = NULL;
  416. }
  417. switch (key) {
  418. case K_TIMER:
  419. if (timer != NULL)
  420. timer(arg);
  421. if (delay_secs != 0)
  422. symbol__annotate_decay_histogram(sym, evidx);
  423. continue;
  424. case K_TAB:
  425. if (nd != NULL) {
  426. nd = rb_prev(nd);
  427. if (nd == NULL)
  428. nd = rb_last(&self->entries);
  429. } else
  430. nd = self->curr_hot;
  431. break;
  432. case K_UNTAB:
  433. if (nd != NULL)
  434. nd = rb_next(nd);
  435. if (nd == NULL)
  436. nd = rb_first(&self->entries);
  437. else
  438. nd = self->curr_hot;
  439. break;
  440. case 'H':
  441. case 'h':
  442. nd = self->curr_hot;
  443. break;
  444. case 'S':
  445. case 's':
  446. if (annotate_browser__toggle_source(self))
  447. ui_helpline__puts(help);
  448. continue;
  449. case 'O':
  450. case 'o':
  451. self->use_offset = !self->use_offset;
  452. continue;
  453. case '/':
  454. if (annotate_browser__search(self, delay_secs)) {
  455. show_help:
  456. ui_helpline__puts(help);
  457. }
  458. continue;
  459. case 'n':
  460. if (self->searching_backwards ?
  461. annotate_browser__continue_search_reverse(self, delay_secs) :
  462. annotate_browser__continue_search(self, delay_secs))
  463. goto show_help;
  464. continue;
  465. case '?':
  466. if (annotate_browser__search_reverse(self, delay_secs))
  467. goto show_help;
  468. continue;
  469. case K_ENTER:
  470. case K_RIGHT:
  471. if (self->selection == NULL)
  472. ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
  473. else if (self->selection->offset == -1)
  474. ui_helpline__puts("Actions are only available for assembly lines.");
  475. else if (!self->selection->ins ||
  476. !(annotate_browser__jump(self) ||
  477. annotate_browser__callq(self, evidx, timer, arg, delay_secs)))
  478. ui_helpline__puts("Actions are only available for the 'callq' and jump instructions.");
  479. continue;
  480. case K_LEFT:
  481. case K_ESC:
  482. case 'q':
  483. case CTRL('c'):
  484. goto out;
  485. default:
  486. continue;
  487. }
  488. if (nd != NULL)
  489. annotate_browser__set_rb_top(self, nd);
  490. }
  491. out:
  492. ui_browser__hide(&self->b);
  493. return key;
  494. }
  495. int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
  496. void(*timer)(void *arg), void *arg, int delay_secs)
  497. {
  498. return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
  499. timer, arg, delay_secs);
  500. }
  501. int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
  502. void(*timer)(void *arg), void *arg,
  503. int delay_secs)
  504. {
  505. struct disasm_line *pos, *n;
  506. struct annotation *notes;
  507. struct map_symbol ms = {
  508. .map = map,
  509. .sym = sym,
  510. };
  511. struct annotate_browser browser = {
  512. .b = {
  513. .refresh = ui_browser__list_head_refresh,
  514. .seek = ui_browser__list_head_seek,
  515. .write = annotate_browser__write,
  516. .filter = disasm_line__filter,
  517. .priv = &ms,
  518. .use_navkeypressed = true,
  519. },
  520. };
  521. int ret;
  522. if (sym == NULL)
  523. return -1;
  524. if (map->dso->annotate_warned)
  525. return -1;
  526. if (symbol__annotate(sym, map, sizeof(struct browser_disasm_line)) < 0) {
  527. ui__error("%s", ui_helpline__last_msg);
  528. return -1;
  529. }
  530. ui_helpline__push("Press <- or ESC to exit");
  531. notes = symbol__annotation(sym);
  532. browser.start = map__rip_2objdump(map, sym->start);
  533. list_for_each_entry(pos, &notes->src->source, node) {
  534. struct browser_disasm_line *bpos;
  535. size_t line_len = strlen(pos->line);
  536. if (browser.b.width < line_len)
  537. browser.b.width = line_len;
  538. bpos = disasm_line__browser(pos);
  539. bpos->idx = browser.nr_entries++;
  540. if (pos->offset != -1)
  541. bpos->idx_asm = browser.nr_asm_entries++;
  542. else
  543. bpos->idx_asm = -1;
  544. }
  545. browser.b.nr_entries = browser.nr_entries;
  546. browser.b.entries = &notes->src->source,
  547. browser.b.width += 18; /* Percentage */
  548. ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
  549. list_for_each_entry_safe(pos, n, &notes->src->source, node) {
  550. list_del(&pos->node);
  551. disasm_line__free(pos);
  552. }
  553. return ret;
  554. }