annotate.c 11 KB

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