annotate.c 17 KB

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