annotate.c 8.8 KB

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