annotate.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. --self->b.top_idx;
  146. --back;
  147. }
  148. self->b.top = pos;
  149. }
  150. static void annotate_browser__set_rb_top(struct annotate_browser *browser,
  151. struct rb_node *nd)
  152. {
  153. struct objdump_line_rb_node *rbpos;
  154. struct objdump_line *pos;
  155. rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
  156. pos = ((struct objdump_line *)rbpos) - 1;
  157. annotate_browser__set_top(browser, pos, rbpos->idx);
  158. browser->curr_hot = nd;
  159. }
  160. static void annotate_browser__calc_percent(struct annotate_browser *browser,
  161. int evidx)
  162. {
  163. struct map_symbol *ms = browser->b.priv;
  164. struct symbol *sym = ms->sym;
  165. struct annotation *notes = symbol__annotation(sym);
  166. struct objdump_line *pos;
  167. browser->entries = RB_ROOT;
  168. pthread_mutex_lock(&notes->lock);
  169. list_for_each_entry(pos, &notes->src->source, node) {
  170. struct objdump_line_rb_node *rbpos = objdump_line__rb(pos);
  171. rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
  172. if (rbpos->percent < 0.01) {
  173. RB_CLEAR_NODE(&rbpos->rb_node);
  174. continue;
  175. }
  176. objdump__insert_line(&browser->entries, rbpos);
  177. }
  178. pthread_mutex_unlock(&notes->lock);
  179. browser->curr_hot = rb_last(&browser->entries);
  180. }
  181. static bool annotate_browser__toggle_source(struct annotate_browser *browser)
  182. {
  183. struct objdump_line *ol;
  184. struct objdump_line_rb_node *olrb;
  185. off_t offset = browser->b.index - browser->b.top_idx;
  186. browser->b.seek(&browser->b, offset, SEEK_CUR);
  187. ol = list_entry(browser->b.top, struct objdump_line, node);
  188. olrb = objdump_line__rb(ol);
  189. if (browser->hide_src_code) {
  190. if (olrb->idx_asm < offset)
  191. offset = olrb->idx;
  192. browser->b.nr_entries = browser->nr_entries;
  193. browser->hide_src_code = false;
  194. browser->b.seek(&browser->b, -offset, SEEK_CUR);
  195. browser->b.top_idx = olrb->idx - offset;
  196. browser->b.index = olrb->idx;
  197. } else {
  198. if (olrb->idx_asm < 0) {
  199. ui_helpline__puts("Only available for assembly lines.");
  200. browser->b.seek(&browser->b, -offset, SEEK_CUR);
  201. return false;
  202. }
  203. if (olrb->idx_asm < offset)
  204. offset = olrb->idx_asm;
  205. browser->b.nr_entries = browser->nr_asm_entries;
  206. browser->hide_src_code = true;
  207. browser->b.seek(&browser->b, -offset, SEEK_CUR);
  208. browser->b.top_idx = olrb->idx_asm - offset;
  209. browser->b.index = olrb->idx_asm;
  210. }
  211. return true;
  212. }
  213. static bool annotate_browser__callq(struct annotate_browser *browser,
  214. int evidx, void (*timer)(void *arg),
  215. void *arg, int delay_secs)
  216. {
  217. struct map_symbol *ms = browser->b.priv;
  218. struct symbol *sym = ms->sym;
  219. struct annotation *notes;
  220. struct symbol *target;
  221. char *s = strstr(browser->selection->line, "callq ");
  222. u64 ip;
  223. if (s == NULL)
  224. return false;
  225. s = strchr(s, ' ');
  226. if (s++ == NULL) {
  227. ui_helpline__puts("Invallid callq instruction.");
  228. return true;
  229. }
  230. ip = strtoull(s, NULL, 16);
  231. ip = ms->map->map_ip(ms->map, ip);
  232. target = map__find_symbol(ms->map, ip, NULL);
  233. if (target == NULL) {
  234. ui_helpline__puts("The called function was not found.");
  235. return true;
  236. }
  237. notes = symbol__annotation(target);
  238. pthread_mutex_lock(&notes->lock);
  239. if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
  240. pthread_mutex_unlock(&notes->lock);
  241. ui__warning("Not enough memory for annotating '%s' symbol!\n",
  242. target->name);
  243. return true;
  244. }
  245. pthread_mutex_unlock(&notes->lock);
  246. symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
  247. ui_browser__show_title(&browser->b, sym->name);
  248. return true;
  249. }
  250. static int annotate_browser__run(struct annotate_browser *self, int evidx,
  251. void(*timer)(void *arg),
  252. void *arg, int delay_secs)
  253. {
  254. struct rb_node *nd = NULL;
  255. struct map_symbol *ms = self->b.priv;
  256. struct symbol *sym = ms->sym;
  257. const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
  258. "H: Go to hottest line, ->/ENTER: Line action, "
  259. "O: Toggle offset view, "
  260. "S: Toggle source code view";
  261. int key;
  262. if (ui_browser__show(&self->b, sym->name, help) < 0)
  263. return -1;
  264. annotate_browser__calc_percent(self, evidx);
  265. if (self->curr_hot)
  266. annotate_browser__set_rb_top(self, self->curr_hot);
  267. nd = self->curr_hot;
  268. while (1) {
  269. key = ui_browser__run(&self->b, delay_secs);
  270. if (delay_secs != 0) {
  271. annotate_browser__calc_percent(self, evidx);
  272. /*
  273. * Current line focus got out of the list of most active
  274. * lines, NULL it so that if TAB|UNTAB is pressed, we
  275. * move to curr_hot (current hottest line).
  276. */
  277. if (nd != NULL && RB_EMPTY_NODE(nd))
  278. nd = NULL;
  279. }
  280. switch (key) {
  281. case K_TIMER:
  282. if (timer != NULL)
  283. timer(arg);
  284. if (delay_secs != 0)
  285. symbol__annotate_decay_histogram(sym, evidx);
  286. continue;
  287. case K_TAB:
  288. if (nd != NULL) {
  289. nd = rb_prev(nd);
  290. if (nd == NULL)
  291. nd = rb_last(&self->entries);
  292. } else
  293. nd = self->curr_hot;
  294. break;
  295. case K_UNTAB:
  296. if (nd != NULL)
  297. nd = rb_next(nd);
  298. if (nd == NULL)
  299. nd = rb_first(&self->entries);
  300. else
  301. nd = self->curr_hot;
  302. break;
  303. case 'H':
  304. case 'h':
  305. nd = self->curr_hot;
  306. break;
  307. case 'S':
  308. case 's':
  309. if (annotate_browser__toggle_source(self))
  310. ui_helpline__puts(help);
  311. continue;
  312. case 'O':
  313. case 'o':
  314. self->use_offset = !self->use_offset;
  315. continue;
  316. case K_ENTER:
  317. case K_RIGHT:
  318. if (self->selection == NULL)
  319. ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
  320. else if (self->selection->offset == -1)
  321. ui_helpline__puts("Actions are only available for assembly lines.");
  322. else if (!annotate_browser__callq(self, evidx, timer, arg, delay_secs))
  323. ui_helpline__puts("Actions are only available for the 'callq' instruction.");
  324. continue;
  325. case K_LEFT:
  326. case K_ESC:
  327. case 'q':
  328. case CTRL('c'):
  329. goto out;
  330. default:
  331. continue;
  332. }
  333. if (nd != NULL)
  334. annotate_browser__set_rb_top(self, nd);
  335. }
  336. out:
  337. ui_browser__hide(&self->b);
  338. return key;
  339. }
  340. int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
  341. void(*timer)(void *arg), void *arg, int delay_secs)
  342. {
  343. return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
  344. timer, arg, delay_secs);
  345. }
  346. int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
  347. void(*timer)(void *arg), void *arg,
  348. int delay_secs)
  349. {
  350. struct objdump_line *pos, *n;
  351. struct annotation *notes;
  352. struct map_symbol ms = {
  353. .map = map,
  354. .sym = sym,
  355. };
  356. struct annotate_browser browser = {
  357. .b = {
  358. .refresh = ui_browser__list_head_refresh,
  359. .seek = ui_browser__list_head_seek,
  360. .write = annotate_browser__write,
  361. .filter = objdump_line__filter,
  362. .priv = &ms,
  363. .use_navkeypressed = true,
  364. },
  365. };
  366. int ret;
  367. if (sym == NULL)
  368. return -1;
  369. if (map->dso->annotate_warned)
  370. return -1;
  371. if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
  372. ui__error("%s", ui_helpline__last_msg);
  373. return -1;
  374. }
  375. ui_helpline__push("Press <- or ESC to exit");
  376. notes = symbol__annotation(sym);
  377. browser.start = map__rip_2objdump(map, sym->start);
  378. list_for_each_entry(pos, &notes->src->source, node) {
  379. struct objdump_line_rb_node *rbpos;
  380. size_t line_len = strlen(pos->line);
  381. if (browser.b.width < line_len)
  382. browser.b.width = line_len;
  383. rbpos = objdump_line__rb(pos);
  384. rbpos->idx = browser.nr_entries++;
  385. if (pos->offset != -1)
  386. rbpos->idx_asm = browser.nr_asm_entries++;
  387. else
  388. rbpos->idx_asm = -1;
  389. }
  390. browser.b.nr_entries = browser.nr_entries;
  391. browser.b.entries = &notes->src->source,
  392. browser.b.width += 18; /* Percentage */
  393. ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
  394. list_for_each_entry_safe(pos, n, &notes->src->source, node) {
  395. list_del(&pos->node);
  396. objdump_line__free(pos);
  397. }
  398. return ret;
  399. }