annotate.c 11 KB

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