annotate.c 17 KB

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