annotate.c 11 KB

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