annotate.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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 (strcmp(dl->name, "callq"))
  226. return false;
  227. ip = strtoull(dl->operands, NULL, 16);
  228. ip = ms->map->map_ip(ms->map, ip);
  229. target = map__find_symbol(ms->map, ip, NULL);
  230. if (target == NULL) {
  231. ui_helpline__puts("The called function was not found.");
  232. return true;
  233. }
  234. notes = symbol__annotation(target);
  235. pthread_mutex_lock(&notes->lock);
  236. if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
  237. pthread_mutex_unlock(&notes->lock);
  238. ui__warning("Not enough memory for annotating '%s' symbol!\n",
  239. target->name);
  240. return true;
  241. }
  242. pthread_mutex_unlock(&notes->lock);
  243. symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
  244. ui_browser__show_title(&browser->b, sym->name);
  245. return true;
  246. }
  247. static
  248. struct disasm_line *annotate_browser__find_offset(struct annotate_browser *browser,
  249. s64 offset, s64 *idx)
  250. {
  251. struct map_symbol *ms = browser->b.priv;
  252. struct symbol *sym = ms->sym;
  253. struct annotation *notes = symbol__annotation(sym);
  254. struct disasm_line *pos;
  255. *idx = 0;
  256. list_for_each_entry(pos, &notes->src->source, node) {
  257. if (pos->offset == offset)
  258. return pos;
  259. if (!disasm_line__filter(&browser->b, &pos->node))
  260. ++*idx;
  261. }
  262. return NULL;
  263. }
  264. static bool annotate_browser__jump(struct annotate_browser *browser)
  265. {
  266. const char *jumps[] = { "je", "jne", "ja", "jmpq", "js", "jmp", NULL };
  267. struct disasm_line *dl = browser->selection;
  268. s64 idx, offset;
  269. char *s;
  270. int i = 0;
  271. while (jumps[i] && strcmp(dl->name, jumps[i]))
  272. ++i;
  273. if (jumps[i] == NULL)
  274. return false;
  275. s = strchr(dl->operands, '+');
  276. if (s++ == NULL) {
  277. ui_helpline__puts("Invallid jump instruction.");
  278. return true;
  279. }
  280. offset = strtoll(s, NULL, 16);
  281. dl = annotate_browser__find_offset(browser, offset, &idx);
  282. if (dl == NULL) {
  283. ui_helpline__puts("Invallid jump offset");
  284. return true;
  285. }
  286. annotate_browser__set_top(browser, dl, idx);
  287. return true;
  288. }
  289. static
  290. struct disasm_line *annotate_browser__find_string(struct annotate_browser *browser,
  291. char *s, s64 *idx)
  292. {
  293. struct map_symbol *ms = browser->b.priv;
  294. struct symbol *sym = ms->sym;
  295. struct annotation *notes = symbol__annotation(sym);
  296. struct disasm_line *pos = browser->selection;
  297. *idx = browser->b.index;
  298. list_for_each_entry_continue(pos, &notes->src->source, node) {
  299. if (disasm_line__filter(&browser->b, &pos->node))
  300. continue;
  301. ++*idx;
  302. if (pos->line && strstr(pos->line, s) != NULL)
  303. return pos;
  304. }
  305. return NULL;
  306. }
  307. static bool __annotate_browser__search(struct annotate_browser *browser)
  308. {
  309. struct disasm_line *dl;
  310. s64 idx;
  311. dl = annotate_browser__find_string(browser, browser->search_bf, &idx);
  312. if (dl == NULL) {
  313. ui_helpline__puts("String not found!");
  314. return false;
  315. }
  316. annotate_browser__set_top(browser, dl, idx);
  317. browser->searching_backwards = false;
  318. return true;
  319. }
  320. static
  321. struct disasm_line *annotate_browser__find_string_reverse(struct annotate_browser *browser,
  322. char *s, s64 *idx)
  323. {
  324. struct map_symbol *ms = browser->b.priv;
  325. struct symbol *sym = ms->sym;
  326. struct annotation *notes = symbol__annotation(sym);
  327. struct disasm_line *pos = browser->selection;
  328. *idx = browser->b.index;
  329. list_for_each_entry_continue_reverse(pos, &notes->src->source, node) {
  330. if (disasm_line__filter(&browser->b, &pos->node))
  331. continue;
  332. --*idx;
  333. if (pos->line && strstr(pos->line, s) != NULL)
  334. return pos;
  335. }
  336. return NULL;
  337. }
  338. static bool __annotate_browser__search_reverse(struct annotate_browser *browser)
  339. {
  340. struct disasm_line *dl;
  341. s64 idx;
  342. dl = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx);
  343. if (dl == NULL) {
  344. ui_helpline__puts("String not found!");
  345. return false;
  346. }
  347. annotate_browser__set_top(browser, dl, idx);
  348. browser->searching_backwards = true;
  349. return true;
  350. }
  351. static bool annotate_browser__search_window(struct annotate_browser *browser,
  352. int delay_secs)
  353. {
  354. if (ui_browser__input_window("Search", "String: ", browser->search_bf,
  355. "ENTER: OK, ESC: Cancel",
  356. delay_secs * 2) != K_ENTER ||
  357. !*browser->search_bf)
  358. return false;
  359. return true;
  360. }
  361. static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs)
  362. {
  363. if (annotate_browser__search_window(browser, delay_secs))
  364. return __annotate_browser__search(browser);
  365. return false;
  366. }
  367. static bool annotate_browser__continue_search(struct annotate_browser *browser,
  368. int delay_secs)
  369. {
  370. if (!*browser->search_bf)
  371. return annotate_browser__search(browser, delay_secs);
  372. return __annotate_browser__search(browser);
  373. }
  374. static bool annotate_browser__search_reverse(struct annotate_browser *browser,
  375. int delay_secs)
  376. {
  377. if (annotate_browser__search_window(browser, delay_secs))
  378. return __annotate_browser__search_reverse(browser);
  379. return false;
  380. }
  381. static
  382. bool annotate_browser__continue_search_reverse(struct annotate_browser *browser,
  383. int delay_secs)
  384. {
  385. if (!*browser->search_bf)
  386. return annotate_browser__search_reverse(browser, delay_secs);
  387. return __annotate_browser__search_reverse(browser);
  388. }
  389. static int annotate_browser__run(struct annotate_browser *self, int evidx,
  390. void(*timer)(void *arg),
  391. void *arg, int delay_secs)
  392. {
  393. struct rb_node *nd = NULL;
  394. struct map_symbol *ms = self->b.priv;
  395. struct symbol *sym = ms->sym;
  396. const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
  397. "H: Go to hottest line, ->/ENTER: Line action, "
  398. "O: Toggle offset view, "
  399. "S: Toggle source code view";
  400. int key;
  401. if (ui_browser__show(&self->b, sym->name, help) < 0)
  402. return -1;
  403. annotate_browser__calc_percent(self, evidx);
  404. if (self->curr_hot) {
  405. annotate_browser__set_rb_top(self, self->curr_hot);
  406. self->b.navkeypressed = false;
  407. }
  408. nd = self->curr_hot;
  409. while (1) {
  410. key = ui_browser__run(&self->b, delay_secs);
  411. if (delay_secs != 0) {
  412. annotate_browser__calc_percent(self, evidx);
  413. /*
  414. * Current line focus got out of the list of most active
  415. * lines, NULL it so that if TAB|UNTAB is pressed, we
  416. * move to curr_hot (current hottest line).
  417. */
  418. if (nd != NULL && RB_EMPTY_NODE(nd))
  419. nd = NULL;
  420. }
  421. switch (key) {
  422. case K_TIMER:
  423. if (timer != NULL)
  424. timer(arg);
  425. if (delay_secs != 0)
  426. symbol__annotate_decay_histogram(sym, evidx);
  427. continue;
  428. case K_TAB:
  429. if (nd != NULL) {
  430. nd = rb_prev(nd);
  431. if (nd == NULL)
  432. nd = rb_last(&self->entries);
  433. } else
  434. nd = self->curr_hot;
  435. break;
  436. case K_UNTAB:
  437. if (nd != NULL)
  438. nd = rb_next(nd);
  439. if (nd == NULL)
  440. nd = rb_first(&self->entries);
  441. else
  442. nd = self->curr_hot;
  443. break;
  444. case 'H':
  445. case 'h':
  446. nd = self->curr_hot;
  447. break;
  448. case 'S':
  449. case 's':
  450. if (annotate_browser__toggle_source(self))
  451. ui_helpline__puts(help);
  452. continue;
  453. case 'O':
  454. case 'o':
  455. self->use_offset = !self->use_offset;
  456. continue;
  457. case '/':
  458. if (annotate_browser__search(self, delay_secs)) {
  459. show_help:
  460. ui_helpline__puts(help);
  461. }
  462. continue;
  463. case 'n':
  464. if (self->searching_backwards ?
  465. annotate_browser__continue_search_reverse(self, delay_secs) :
  466. annotate_browser__continue_search(self, delay_secs))
  467. goto show_help;
  468. continue;
  469. case '?':
  470. if (annotate_browser__search_reverse(self, delay_secs))
  471. goto show_help;
  472. continue;
  473. case K_ENTER:
  474. case K_RIGHT:
  475. if (self->selection == NULL)
  476. ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
  477. else if (self->selection->offset == -1)
  478. ui_helpline__puts("Actions are only available for assembly lines.");
  479. else if (!(annotate_browser__jump(self) ||
  480. annotate_browser__callq(self, evidx, timer, arg, delay_secs)))
  481. ui_helpline__puts("Actions are only available for the 'callq' and jump instructions.");
  482. continue;
  483. case K_LEFT:
  484. case K_ESC:
  485. case 'q':
  486. case CTRL('c'):
  487. goto out;
  488. default:
  489. continue;
  490. }
  491. if (nd != NULL)
  492. annotate_browser__set_rb_top(self, nd);
  493. }
  494. out:
  495. ui_browser__hide(&self->b);
  496. return key;
  497. }
  498. int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
  499. void(*timer)(void *arg), void *arg, int delay_secs)
  500. {
  501. return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
  502. timer, arg, delay_secs);
  503. }
  504. int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
  505. void(*timer)(void *arg), void *arg,
  506. int delay_secs)
  507. {
  508. struct disasm_line *pos, *n;
  509. struct annotation *notes;
  510. struct map_symbol ms = {
  511. .map = map,
  512. .sym = sym,
  513. };
  514. struct annotate_browser browser = {
  515. .b = {
  516. .refresh = ui_browser__list_head_refresh,
  517. .seek = ui_browser__list_head_seek,
  518. .write = annotate_browser__write,
  519. .filter = disasm_line__filter,
  520. .priv = &ms,
  521. .use_navkeypressed = true,
  522. },
  523. };
  524. int ret;
  525. if (sym == NULL)
  526. return -1;
  527. if (map->dso->annotate_warned)
  528. return -1;
  529. if (symbol__annotate(sym, map, sizeof(struct disasm_line_rb_node)) < 0) {
  530. ui__error("%s", ui_helpline__last_msg);
  531. return -1;
  532. }
  533. ui_helpline__push("Press <- or ESC to exit");
  534. notes = symbol__annotation(sym);
  535. browser.start = map__rip_2objdump(map, sym->start);
  536. list_for_each_entry(pos, &notes->src->source, node) {
  537. struct disasm_line_rb_node *rbpos;
  538. size_t line_len = strlen(pos->line);
  539. if (browser.b.width < line_len)
  540. browser.b.width = line_len;
  541. rbpos = disasm_line__rb(pos);
  542. rbpos->idx = browser.nr_entries++;
  543. if (pos->offset != -1)
  544. rbpos->idx_asm = browser.nr_asm_entries++;
  545. else
  546. rbpos->idx_asm = -1;
  547. }
  548. browser.b.nr_entries = browser.nr_entries;
  549. browser.b.entries = &notes->src->source,
  550. browser.b.width += 18; /* Percentage */
  551. ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
  552. list_for_each_entry_safe(pos, n, &notes->src->source, node) {
  553. list_del(&pos->node);
  554. disasm_line__free(pos);
  555. }
  556. return ret;
  557. }