annotate.c 11 KB

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