annotate.c 11 KB

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