annotate.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 "../../annotate.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. };
  21. struct objdump_line_rb_node {
  22. struct rb_node rb_node;
  23. double percent;
  24. u32 idx;
  25. };
  26. static inline
  27. struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
  28. {
  29. return (struct objdump_line_rb_node *)(self + 1);
  30. }
  31. static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
  32. {
  33. struct objdump_line *ol = rb_entry(entry, struct objdump_line, node);
  34. bool current_entry = ui_browser__is_current_entry(self, row);
  35. int width = self->width;
  36. if (ol->offset != -1) {
  37. struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
  38. ui_browser__set_percent_color(self, olrb->percent, current_entry);
  39. slsmg_printf(" %7.2f ", olrb->percent);
  40. if (!current_entry)
  41. ui_browser__set_color(self, HE_COLORSET_CODE);
  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. }
  53. static double objdump_line__calc_percent(struct objdump_line *self,
  54. struct symbol *sym, int evidx)
  55. {
  56. double percent = 0.0;
  57. if (self->offset != -1) {
  58. int len = sym->end - sym->start;
  59. unsigned int hits = 0;
  60. struct annotation *notes = symbol__annotation(sym);
  61. struct source_line *src_line = notes->src->lines;
  62. struct sym_hist *h = annotation__histogram(notes, evidx);
  63. s64 offset = self->offset;
  64. struct objdump_line *next;
  65. next = objdump__get_next_ip_line(&notes->src->source, self);
  66. while (offset < (s64)len &&
  67. (next == NULL || offset < next->offset)) {
  68. if (src_line) {
  69. percent += src_line[offset].percent;
  70. } else
  71. hits += h->addr[offset];
  72. ++offset;
  73. }
  74. /*
  75. * If the percentage wasn't already calculated in
  76. * symbol__get_source_line, do it now:
  77. */
  78. if (src_line == NULL && h->sum)
  79. percent = 100.0 * hits / h->sum;
  80. }
  81. return percent;
  82. }
  83. static void objdump__insert_line(struct rb_root *self,
  84. struct objdump_line_rb_node *line)
  85. {
  86. struct rb_node **p = &self->rb_node;
  87. struct rb_node *parent = NULL;
  88. struct objdump_line_rb_node *l;
  89. while (*p != NULL) {
  90. parent = *p;
  91. l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
  92. if (line->percent < l->percent)
  93. p = &(*p)->rb_left;
  94. else
  95. p = &(*p)->rb_right;
  96. }
  97. rb_link_node(&line->rb_node, parent, p);
  98. rb_insert_color(&line->rb_node, self);
  99. }
  100. static void annotate_browser__set_top(struct annotate_browser *self,
  101. struct rb_node *nd)
  102. {
  103. struct objdump_line_rb_node *rbpos;
  104. struct objdump_line *pos;
  105. unsigned back;
  106. ui_browser__refresh_dimensions(&self->b);
  107. back = self->b.height / 2;
  108. rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
  109. pos = ((struct objdump_line *)rbpos) - 1;
  110. self->b.top_idx = self->b.index = rbpos->idx;
  111. while (self->b.top_idx != 0 && back != 0) {
  112. pos = list_entry(pos->node.prev, struct objdump_line, node);
  113. --self->b.top_idx;
  114. --back;
  115. }
  116. self->b.top = pos;
  117. self->curr_hot = nd;
  118. }
  119. static int annotate_browser__run(struct annotate_browser *self)
  120. {
  121. struct rb_node *nd;
  122. struct symbol *sym = self->b.priv;
  123. int key;
  124. if (ui_browser__show(&self->b, sym->name,
  125. "<-, -> or ESC: exit, TAB/shift+TAB: cycle thru samples") < 0)
  126. return -1;
  127. /*
  128. * To allow builtin-annotate to cycle thru multiple symbols by
  129. * examining the exit key for this function.
  130. */
  131. ui_browser__add_exit_key(&self->b, NEWT_KEY_RIGHT);
  132. nd = self->curr_hot;
  133. if (nd) {
  134. int tabs[] = { NEWT_KEY_TAB, NEWT_KEY_UNTAB, 0 };
  135. ui_browser__add_exit_keys(&self->b, tabs);
  136. }
  137. while (1) {
  138. key = ui_browser__run(&self->b);
  139. switch (key) {
  140. case NEWT_KEY_TAB:
  141. nd = rb_prev(nd);
  142. if (nd == NULL)
  143. nd = rb_last(&self->entries);
  144. annotate_browser__set_top(self, nd);
  145. break;
  146. case NEWT_KEY_UNTAB:
  147. nd = rb_next(nd);
  148. if (nd == NULL)
  149. nd = rb_first(&self->entries);
  150. annotate_browser__set_top(self, nd);
  151. break;
  152. default:
  153. goto out;
  154. }
  155. }
  156. out:
  157. ui_browser__hide(&self->b);
  158. return key;
  159. }
  160. int hist_entry__tui_annotate(struct hist_entry *he, int evidx)
  161. {
  162. return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx);
  163. }
  164. int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx)
  165. {
  166. struct objdump_line *pos, *n;
  167. struct objdump_line_rb_node *rbpos;
  168. struct annotation *notes = symbol__annotation(sym);
  169. struct annotate_browser browser = {
  170. .b = {
  171. .entries = &notes->src->source,
  172. .refresh = ui_browser__list_head_refresh,
  173. .seek = ui_browser__list_head_seek,
  174. .write = annotate_browser__write,
  175. .priv = sym,
  176. },
  177. };
  178. int ret;
  179. if (sym == NULL)
  180. return -1;
  181. if (map->dso->annotate_warned)
  182. return -1;
  183. if (symbol__annotate(sym, map, sizeof(*rbpos)) < 0) {
  184. ui__error_window(ui_helpline__last_msg);
  185. return -1;
  186. }
  187. ui_helpline__push("Press <- or ESC to exit");
  188. list_for_each_entry(pos, &notes->src->source, node) {
  189. size_t line_len = strlen(pos->line);
  190. if (browser.b.width < line_len)
  191. browser.b.width = line_len;
  192. rbpos = objdump_line__rb(pos);
  193. rbpos->idx = browser.b.nr_entries++;
  194. rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
  195. if (rbpos->percent < 0.01)
  196. continue;
  197. objdump__insert_line(&browser.entries, rbpos);
  198. }
  199. /*
  200. * Position the browser at the hottest line.
  201. */
  202. browser.curr_hot = rb_last(&browser.entries);
  203. if (browser.curr_hot)
  204. annotate_browser__set_top(&browser, browser.curr_hot);
  205. browser.b.width += 18; /* Percentage */
  206. ret = annotate_browser__run(&browser);
  207. list_for_each_entry_safe(pos, n, &notes->src->source, node) {
  208. list_del(&pos->node);
  209. objdump_line__free(pos);
  210. }
  211. return ret;
  212. }