annotate.c 16 KB

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