annotate.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. };
  23. struct objdump_line_rb_node {
  24. struct rb_node rb_node;
  25. double percent;
  26. u32 idx;
  27. int idx_asm;
  28. };
  29. static inline
  30. struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
  31. {
  32. return (struct objdump_line_rb_node *)(self + 1);
  33. }
  34. static bool objdump_line__filter(struct ui_browser *browser, void *entry)
  35. {
  36. struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
  37. if (ab->hide_src_code) {
  38. struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
  39. return ol->offset == -1;
  40. }
  41. return false;
  42. }
  43. static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
  44. {
  45. struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
  46. struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
  47. bool current_entry = ui_browser__is_current_entry(self, row);
  48. bool change_color = (!ab->hide_src_code &&
  49. (!current_entry || (self->use_navkeypressed &&
  50. !self->navkeypressed)));
  51. int width = self->width;
  52. if (ol->offset != -1) {
  53. struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
  54. ui_browser__set_percent_color(self, olrb->percent, current_entry);
  55. slsmg_printf(" %7.2f ", olrb->percent);
  56. } else {
  57. ui_browser__set_percent_color(self, 0, current_entry);
  58. slsmg_write_nstring(" ", 9);
  59. }
  60. SLsmg_write_char(':');
  61. slsmg_write_nstring(" ", 8);
  62. /* The scroll bar isn't being used */
  63. if (!self->navkeypressed)
  64. width += 1;
  65. if (ol->offset != -1 && change_color)
  66. ui_browser__set_color(self, HE_COLORSET_CODE);
  67. if (!*ol->line)
  68. slsmg_write_nstring(" ", width - 18);
  69. else if (ol->offset == -1)
  70. slsmg_write_nstring(ol->line, width - 18);
  71. else {
  72. char bf[64];
  73. u64 addr = ab->start + ol->offset;
  74. int printed = scnprintf(bf, sizeof(bf), " %" PRIx64 ":", addr);
  75. int color = -1;
  76. if (change_color)
  77. color = ui_browser__set_color(self, HE_COLORSET_ADDR);
  78. slsmg_write_nstring(bf, printed);
  79. if (change_color)
  80. ui_browser__set_color(self, color);
  81. slsmg_write_nstring(ol->line, width - 18 - printed);
  82. }
  83. if (current_entry)
  84. ab->selection = ol;
  85. }
  86. static double objdump_line__calc_percent(struct objdump_line *self,
  87. struct symbol *sym, int evidx)
  88. {
  89. double percent = 0.0;
  90. if (self->offset != -1) {
  91. int len = sym->end - sym->start;
  92. unsigned int hits = 0;
  93. struct annotation *notes = symbol__annotation(sym);
  94. struct source_line *src_line = notes->src->lines;
  95. struct sym_hist *h = annotation__histogram(notes, evidx);
  96. s64 offset = self->offset;
  97. struct objdump_line *next;
  98. next = objdump__get_next_ip_line(&notes->src->source, self);
  99. while (offset < (s64)len &&
  100. (next == NULL || offset < next->offset)) {
  101. if (src_line) {
  102. percent += src_line[offset].percent;
  103. } else
  104. hits += h->addr[offset];
  105. ++offset;
  106. }
  107. /*
  108. * If the percentage wasn't already calculated in
  109. * symbol__get_source_line, do it now:
  110. */
  111. if (src_line == NULL && h->sum)
  112. percent = 100.0 * hits / h->sum;
  113. }
  114. return percent;
  115. }
  116. static void objdump__insert_line(struct rb_root *self,
  117. struct objdump_line_rb_node *line)
  118. {
  119. struct rb_node **p = &self->rb_node;
  120. struct rb_node *parent = NULL;
  121. struct objdump_line_rb_node *l;
  122. while (*p != NULL) {
  123. parent = *p;
  124. l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
  125. if (line->percent < l->percent)
  126. p = &(*p)->rb_left;
  127. else
  128. p = &(*p)->rb_right;
  129. }
  130. rb_link_node(&line->rb_node, parent, p);
  131. rb_insert_color(&line->rb_node, self);
  132. }
  133. static void annotate_browser__set_top(struct annotate_browser *self,
  134. struct rb_node *nd)
  135. {
  136. struct objdump_line_rb_node *rbpos;
  137. struct objdump_line *pos;
  138. unsigned back;
  139. ui_browser__refresh_dimensions(&self->b);
  140. back = self->b.height / 2;
  141. rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
  142. pos = ((struct objdump_line *)rbpos) - 1;
  143. self->b.top_idx = self->b.index = rbpos->idx;
  144. while (self->b.top_idx != 0 && back != 0) {
  145. pos = list_entry(pos->node.prev, struct objdump_line, node);
  146. --self->b.top_idx;
  147. --back;
  148. }
  149. self->b.top = pos;
  150. self->curr_hot = nd;
  151. }
  152. static void annotate_browser__calc_percent(struct annotate_browser *browser,
  153. int evidx)
  154. {
  155. struct map_symbol *ms = browser->b.priv;
  156. struct symbol *sym = ms->sym;
  157. struct annotation *notes = symbol__annotation(sym);
  158. struct objdump_line *pos;
  159. browser->entries = RB_ROOT;
  160. pthread_mutex_lock(&notes->lock);
  161. list_for_each_entry(pos, &notes->src->source, node) {
  162. struct objdump_line_rb_node *rbpos = objdump_line__rb(pos);
  163. rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
  164. if (rbpos->percent < 0.01) {
  165. RB_CLEAR_NODE(&rbpos->rb_node);
  166. continue;
  167. }
  168. objdump__insert_line(&browser->entries, rbpos);
  169. }
  170. pthread_mutex_unlock(&notes->lock);
  171. browser->curr_hot = rb_last(&browser->entries);
  172. }
  173. static bool annotate_browser__toggle_source(struct annotate_browser *browser)
  174. {
  175. struct objdump_line *ol;
  176. struct objdump_line_rb_node *olrb;
  177. off_t offset = browser->b.index - browser->b.top_idx;
  178. browser->b.seek(&browser->b, offset, SEEK_CUR);
  179. ol = list_entry(browser->b.top, struct objdump_line, node);
  180. olrb = objdump_line__rb(ol);
  181. if (browser->hide_src_code) {
  182. if (olrb->idx_asm < offset)
  183. offset = olrb->idx;
  184. browser->b.nr_entries = browser->nr_entries;
  185. browser->hide_src_code = false;
  186. browser->b.seek(&browser->b, -offset, SEEK_CUR);
  187. browser->b.top_idx = olrb->idx - offset;
  188. browser->b.index = olrb->idx;
  189. } else {
  190. if (olrb->idx_asm < 0) {
  191. ui_helpline__puts("Only available for assembly lines.");
  192. browser->b.seek(&browser->b, -offset, SEEK_CUR);
  193. return false;
  194. }
  195. if (olrb->idx_asm < offset)
  196. offset = olrb->idx_asm;
  197. browser->b.nr_entries = browser->nr_asm_entries;
  198. browser->hide_src_code = true;
  199. browser->b.seek(&browser->b, -offset, SEEK_CUR);
  200. browser->b.top_idx = olrb->idx_asm - offset;
  201. browser->b.index = olrb->idx_asm;
  202. }
  203. return true;
  204. }
  205. static int annotate_browser__run(struct annotate_browser *self, int evidx,
  206. void(*timer)(void *arg),
  207. void *arg, int delay_secs)
  208. {
  209. struct rb_node *nd = NULL;
  210. struct map_symbol *ms = self->b.priv;
  211. struct symbol *sym = ms->sym;
  212. const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
  213. "H: Go to hottest line, ->/ENTER: Line action, "
  214. "S: Toggle source code view";
  215. int key;
  216. if (ui_browser__show(&self->b, sym->name, help) < 0)
  217. return -1;
  218. annotate_browser__calc_percent(self, evidx);
  219. if (self->curr_hot)
  220. annotate_browser__set_top(self, self->curr_hot);
  221. nd = self->curr_hot;
  222. while (1) {
  223. key = ui_browser__run(&self->b, delay_secs);
  224. if (delay_secs != 0) {
  225. annotate_browser__calc_percent(self, evidx);
  226. /*
  227. * Current line focus got out of the list of most active
  228. * lines, NULL it so that if TAB|UNTAB is pressed, we
  229. * move to curr_hot (current hottest line).
  230. */
  231. if (nd != NULL && RB_EMPTY_NODE(nd))
  232. nd = NULL;
  233. }
  234. switch (key) {
  235. case K_TIMER:
  236. if (timer != NULL)
  237. timer(arg);
  238. if (delay_secs != 0)
  239. symbol__annotate_decay_histogram(sym, evidx);
  240. continue;
  241. case K_TAB:
  242. if (nd != NULL) {
  243. nd = rb_prev(nd);
  244. if (nd == NULL)
  245. nd = rb_last(&self->entries);
  246. } else
  247. nd = self->curr_hot;
  248. break;
  249. case K_UNTAB:
  250. if (nd != NULL)
  251. nd = rb_next(nd);
  252. if (nd == NULL)
  253. nd = rb_first(&self->entries);
  254. else
  255. nd = self->curr_hot;
  256. break;
  257. case 'H':
  258. case 'h':
  259. nd = self->curr_hot;
  260. break;
  261. case 'S':
  262. case 's':
  263. if (annotate_browser__toggle_source(self))
  264. ui_helpline__puts(help);
  265. continue;
  266. case K_ENTER:
  267. case K_RIGHT:
  268. if (self->selection == NULL) {
  269. ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
  270. continue;
  271. }
  272. if (self->selection->offset == -1) {
  273. ui_helpline__puts("Actions are only available for assembly lines.");
  274. continue;
  275. } else {
  276. char *s = strstr(self->selection->line, "callq ");
  277. struct annotation *notes;
  278. struct symbol *target;
  279. u64 ip;
  280. if (s == NULL) {
  281. ui_helpline__puts("Actions are only available for the 'callq' instruction.");
  282. continue;
  283. }
  284. s = strchr(s, ' ');
  285. if (s++ == NULL) {
  286. ui_helpline__puts("Invallid callq instruction.");
  287. continue;
  288. }
  289. ip = strtoull(s, NULL, 16);
  290. ip = ms->map->map_ip(ms->map, ip);
  291. target = map__find_symbol(ms->map, ip, NULL);
  292. if (target == NULL) {
  293. ui_helpline__puts("The called function was not found.");
  294. continue;
  295. }
  296. notes = symbol__annotation(target);
  297. pthread_mutex_lock(&notes->lock);
  298. if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
  299. pthread_mutex_unlock(&notes->lock);
  300. ui__warning("Not enough memory for annotating '%s' symbol!\n",
  301. target->name);
  302. continue;
  303. }
  304. pthread_mutex_unlock(&notes->lock);
  305. symbol__tui_annotate(target, ms->map, evidx,
  306. timer, arg, delay_secs);
  307. ui_browser__show_title(&self->b, sym->name);
  308. }
  309. continue;
  310. case K_LEFT:
  311. case K_ESC:
  312. case 'q':
  313. case CTRL('c'):
  314. goto out;
  315. default:
  316. continue;
  317. }
  318. if (nd != NULL)
  319. annotate_browser__set_top(self, nd);
  320. }
  321. out:
  322. ui_browser__hide(&self->b);
  323. return key;
  324. }
  325. int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
  326. void(*timer)(void *arg), void *arg, int delay_secs)
  327. {
  328. return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
  329. timer, arg, delay_secs);
  330. }
  331. int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
  332. void(*timer)(void *arg), void *arg,
  333. int delay_secs)
  334. {
  335. struct objdump_line *pos, *n;
  336. struct annotation *notes;
  337. struct map_symbol ms = {
  338. .map = map,
  339. .sym = sym,
  340. };
  341. struct annotate_browser browser = {
  342. .b = {
  343. .refresh = ui_browser__list_head_refresh,
  344. .seek = ui_browser__list_head_seek,
  345. .write = annotate_browser__write,
  346. .filter = objdump_line__filter,
  347. .priv = &ms,
  348. .use_navkeypressed = true,
  349. },
  350. };
  351. int ret;
  352. if (sym == NULL)
  353. return -1;
  354. if (map->dso->annotate_warned)
  355. return -1;
  356. if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
  357. ui__error("%s", ui_helpline__last_msg);
  358. return -1;
  359. }
  360. ui_helpline__push("Press <- or ESC to exit");
  361. notes = symbol__annotation(sym);
  362. browser.start = map__rip_2objdump(map, sym->start);
  363. list_for_each_entry(pos, &notes->src->source, node) {
  364. struct objdump_line_rb_node *rbpos;
  365. size_t line_len = strlen(pos->line);
  366. if (browser.b.width < line_len)
  367. browser.b.width = line_len;
  368. rbpos = objdump_line__rb(pos);
  369. rbpos->idx = browser.nr_entries++;
  370. if (pos->offset != -1)
  371. rbpos->idx_asm = browser.nr_asm_entries++;
  372. else
  373. rbpos->idx_asm = -1;
  374. }
  375. browser.b.nr_entries = browser.nr_entries;
  376. browser.b.entries = &notes->src->source,
  377. browser.b.width += 18; /* Percentage */
  378. ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
  379. list_for_each_entry_safe(pos, n, &notes->src->source, node) {
  380. list_del(&pos->node);
  381. objdump_line__free(pos);
  382. }
  383. return ret;
  384. }