annotate.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. #include "../../util.h"
  2. #include "../browser.h"
  3. #include "../helpline.h"
  4. #include "../libslang.h"
  5. #include "../ui.h"
  6. #include "../util.h"
  7. #include "../../annotate.h"
  8. #include "../../hist.h"
  9. #include "../../sort.h"
  10. #include "../../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. };
  24. struct objdump_line_rb_node {
  25. struct rb_node rb_node;
  26. double percent;
  27. u32 idx;
  28. int idx_asm;
  29. };
  30. static inline
  31. struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
  32. {
  33. return (struct objdump_line_rb_node *)(self + 1);
  34. }
  35. static bool objdump_line__filter(struct ui_browser *browser, void *entry)
  36. {
  37. struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
  38. if (ab->hide_src_code) {
  39. struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
  40. return ol->offset == -1;
  41. }
  42. return false;
  43. }
  44. static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
  45. {
  46. struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
  47. struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
  48. bool current_entry = ui_browser__is_current_entry(self, row);
  49. bool change_color = (!ab->hide_src_code &&
  50. (!current_entry || (self->use_navkeypressed &&
  51. !self->navkeypressed)));
  52. int width = self->width;
  53. if (ol->offset != -1) {
  54. struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
  55. ui_browser__set_percent_color(self, olrb->percent, current_entry);
  56. slsmg_printf(" %7.2f ", olrb->percent);
  57. } else {
  58. ui_browser__set_percent_color(self, 0, current_entry);
  59. slsmg_write_nstring(" ", 9);
  60. }
  61. SLsmg_write_char(':');
  62. slsmg_write_nstring(" ", 8);
  63. /* The scroll bar isn't being used */
  64. if (!self->navkeypressed)
  65. width += 1;
  66. if (ol->offset != -1 && change_color)
  67. ui_browser__set_color(self, HE_COLORSET_CODE);
  68. if (!*ol->line)
  69. slsmg_write_nstring(" ", width - 18);
  70. else if (ol->offset == -1)
  71. slsmg_write_nstring(ol->line, width - 18);
  72. else {
  73. char bf[64];
  74. u64 addr = ol->offset;
  75. int printed, color = -1;
  76. if (!ab->use_offset)
  77. addr += ab->start;
  78. printed = scnprintf(bf, sizeof(bf), " %" PRIx64 ":", addr);
  79. if (change_color)
  80. color = ui_browser__set_color(self, HE_COLORSET_ADDR);
  81. slsmg_write_nstring(bf, printed);
  82. if (change_color)
  83. ui_browser__set_color(self, color);
  84. slsmg_write_nstring(ol->line, width - 18 - printed);
  85. }
  86. if (current_entry)
  87. ab->selection = ol;
  88. }
  89. static double objdump_line__calc_percent(struct objdump_line *self,
  90. struct symbol *sym, int evidx)
  91. {
  92. double percent = 0.0;
  93. if (self->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 = self->offset;
  100. struct objdump_line *next;
  101. next = objdump__get_next_ip_line(&notes->src->source, self);
  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 objdump__insert_line(struct rb_root *self,
  120. struct objdump_line_rb_node *line)
  121. {
  122. struct rb_node **p = &self->rb_node;
  123. struct rb_node *parent = NULL;
  124. struct objdump_line_rb_node *l;
  125. while (*p != NULL) {
  126. parent = *p;
  127. l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
  128. if (line->percent < l->percent)
  129. p = &(*p)->rb_left;
  130. else
  131. p = &(*p)->rb_right;
  132. }
  133. rb_link_node(&line->rb_node, parent, p);
  134. rb_insert_color(&line->rb_node, self);
  135. }
  136. static void annotate_browser__set_top(struct annotate_browser *self,
  137. struct objdump_line *pos, u32 idx)
  138. {
  139. unsigned back;
  140. ui_browser__refresh_dimensions(&self->b);
  141. back = self->b.height / 2;
  142. self->b.top_idx = self->b.index = idx;
  143. while (self->b.top_idx != 0 && back != 0) {
  144. pos = list_entry(pos->node.prev, struct objdump_line, node);
  145. if (objdump_line__filter(&self->b, &pos->node))
  146. continue;
  147. --self->b.top_idx;
  148. --back;
  149. }
  150. self->b.top = pos;
  151. }
  152. static void annotate_browser__set_rb_top(struct annotate_browser *browser,
  153. struct rb_node *nd)
  154. {
  155. struct objdump_line_rb_node *rbpos;
  156. struct objdump_line *pos;
  157. rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
  158. pos = ((struct objdump_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 objdump_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 objdump_line_rb_node *rbpos = objdump_line__rb(pos);
  173. rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
  174. if (rbpos->percent < 0.01) {
  175. RB_CLEAR_NODE(&rbpos->rb_node);
  176. continue;
  177. }
  178. objdump__insert_line(&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 objdump_line *ol;
  186. struct objdump_line_rb_node *olrb;
  187. off_t offset = browser->b.index - browser->b.top_idx;
  188. browser->b.seek(&browser->b, offset, SEEK_CUR);
  189. ol = list_entry(browser->b.top, struct objdump_line, node);
  190. olrb = objdump_line__rb(ol);
  191. if (browser->hide_src_code) {
  192. if (olrb->idx_asm < offset)
  193. offset = olrb->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 = olrb->idx - offset;
  198. browser->b.index = olrb->idx;
  199. } else {
  200. if (olrb->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 (olrb->idx_asm < offset)
  206. offset = olrb->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 = olrb->idx_asm - offset;
  211. browser->b.index = olrb->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 struct objdump_line *
  253. 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 objdump_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 (!objdump_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 objdump_line *line;
  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. line = annotate_browser__find_offset(browser, offset, &idx);
  290. if (line == NULL) {
  291. ui_helpline__puts("Invallid jump offset");
  292. return true;
  293. }
  294. annotate_browser__set_top(browser, line, idx);
  295. return true;
  296. }
  297. static int annotate_browser__run(struct annotate_browser *self, int evidx,
  298. void(*timer)(void *arg),
  299. void *arg, int delay_secs)
  300. {
  301. struct rb_node *nd = NULL;
  302. struct map_symbol *ms = self->b.priv;
  303. struct symbol *sym = ms->sym;
  304. const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
  305. "H: Go to hottest line, ->/ENTER: Line action, "
  306. "O: Toggle offset view, "
  307. "S: Toggle source code view";
  308. int key;
  309. if (ui_browser__show(&self->b, sym->name, help) < 0)
  310. return -1;
  311. annotate_browser__calc_percent(self, evidx);
  312. if (self->curr_hot)
  313. annotate_browser__set_rb_top(self, self->curr_hot);
  314. nd = self->curr_hot;
  315. while (1) {
  316. key = ui_browser__run(&self->b, delay_secs);
  317. if (delay_secs != 0) {
  318. annotate_browser__calc_percent(self, evidx);
  319. /*
  320. * Current line focus got out of the list of most active
  321. * lines, NULL it so that if TAB|UNTAB is pressed, we
  322. * move to curr_hot (current hottest line).
  323. */
  324. if (nd != NULL && RB_EMPTY_NODE(nd))
  325. nd = NULL;
  326. }
  327. switch (key) {
  328. case K_TIMER:
  329. if (timer != NULL)
  330. timer(arg);
  331. if (delay_secs != 0)
  332. symbol__annotate_decay_histogram(sym, evidx);
  333. continue;
  334. case K_TAB:
  335. if (nd != NULL) {
  336. nd = rb_prev(nd);
  337. if (nd == NULL)
  338. nd = rb_last(&self->entries);
  339. } else
  340. nd = self->curr_hot;
  341. break;
  342. case K_UNTAB:
  343. if (nd != NULL)
  344. nd = rb_next(nd);
  345. if (nd == NULL)
  346. nd = rb_first(&self->entries);
  347. else
  348. nd = self->curr_hot;
  349. break;
  350. case 'H':
  351. case 'h':
  352. nd = self->curr_hot;
  353. break;
  354. case 'S':
  355. case 's':
  356. if (annotate_browser__toggle_source(self))
  357. ui_helpline__puts(help);
  358. continue;
  359. case 'O':
  360. case 'o':
  361. self->use_offset = !self->use_offset;
  362. continue;
  363. case K_ENTER:
  364. case K_RIGHT:
  365. if (self->selection == NULL)
  366. ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
  367. else if (self->selection->offset == -1)
  368. ui_helpline__puts("Actions are only available for assembly lines.");
  369. else if (!(annotate_browser__jump(self) ||
  370. annotate_browser__callq(self, evidx, timer, arg, delay_secs)))
  371. ui_helpline__puts("Actions are only available for the 'callq' and jump instructions.");
  372. continue;
  373. case K_LEFT:
  374. case K_ESC:
  375. case 'q':
  376. case CTRL('c'):
  377. goto out;
  378. default:
  379. continue;
  380. }
  381. if (nd != NULL)
  382. annotate_browser__set_rb_top(self, nd);
  383. }
  384. out:
  385. ui_browser__hide(&self->b);
  386. return key;
  387. }
  388. int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
  389. void(*timer)(void *arg), void *arg, int delay_secs)
  390. {
  391. return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
  392. timer, arg, delay_secs);
  393. }
  394. int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
  395. void(*timer)(void *arg), void *arg,
  396. int delay_secs)
  397. {
  398. struct objdump_line *pos, *n;
  399. struct annotation *notes;
  400. struct map_symbol ms = {
  401. .map = map,
  402. .sym = sym,
  403. };
  404. struct annotate_browser browser = {
  405. .b = {
  406. .refresh = ui_browser__list_head_refresh,
  407. .seek = ui_browser__list_head_seek,
  408. .write = annotate_browser__write,
  409. .filter = objdump_line__filter,
  410. .priv = &ms,
  411. .use_navkeypressed = true,
  412. },
  413. };
  414. int ret;
  415. if (sym == NULL)
  416. return -1;
  417. if (map->dso->annotate_warned)
  418. return -1;
  419. if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
  420. ui__error("%s", ui_helpline__last_msg);
  421. return -1;
  422. }
  423. ui_helpline__push("Press <- or ESC to exit");
  424. notes = symbol__annotation(sym);
  425. browser.start = map__rip_2objdump(map, sym->start);
  426. list_for_each_entry(pos, &notes->src->source, node) {
  427. struct objdump_line_rb_node *rbpos;
  428. size_t line_len = strlen(pos->line);
  429. if (browser.b.width < line_len)
  430. browser.b.width = line_len;
  431. rbpos = objdump_line__rb(pos);
  432. rbpos->idx = browser.nr_entries++;
  433. if (pos->offset != -1)
  434. rbpos->idx_asm = browser.nr_asm_entries++;
  435. else
  436. rbpos->idx_asm = -1;
  437. }
  438. browser.b.nr_entries = browser.nr_entries;
  439. browser.b.entries = &notes->src->source,
  440. browser.b.width += 18; /* Percentage */
  441. ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
  442. list_for_each_entry_safe(pos, n, &notes->src->source, node) {
  443. list_del(&pos->node);
  444. objdump_line__free(pos);
  445. }
  446. return ret;
  447. }