annotate.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 bool annotate_browser__callq(struct annotate_browser *browser,
  209. int evidx, void (*timer)(void *arg),
  210. void *arg, int delay_secs)
  211. {
  212. struct map_symbol *ms = browser->b.priv;
  213. struct symbol *sym = ms->sym;
  214. struct annotation *notes;
  215. struct symbol *target;
  216. char *s = strstr(browser->selection->line, "callq ");
  217. u64 ip;
  218. if (s == NULL)
  219. return false;
  220. s = strchr(s, ' ');
  221. if (s++ == NULL) {
  222. ui_helpline__puts("Invallid callq instruction.");
  223. return true;
  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. return true;
  231. }
  232. notes = symbol__annotation(target);
  233. pthread_mutex_lock(&notes->lock);
  234. if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
  235. pthread_mutex_unlock(&notes->lock);
  236. ui__warning("Not enough memory for annotating '%s' symbol!\n",
  237. target->name);
  238. return true;
  239. }
  240. pthread_mutex_unlock(&notes->lock);
  241. symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs);
  242. ui_browser__show_title(&browser->b, sym->name);
  243. return true;
  244. }
  245. static int annotate_browser__run(struct annotate_browser *self, int evidx,
  246. void(*timer)(void *arg),
  247. void *arg, int delay_secs)
  248. {
  249. struct rb_node *nd = NULL;
  250. struct map_symbol *ms = self->b.priv;
  251. struct symbol *sym = ms->sym;
  252. const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
  253. "H: Go to hottest line, ->/ENTER: Line action, "
  254. "O: Toggle offset view, "
  255. "S: Toggle source code view";
  256. int key;
  257. if (ui_browser__show(&self->b, sym->name, help) < 0)
  258. return -1;
  259. annotate_browser__calc_percent(self, evidx);
  260. if (self->curr_hot)
  261. annotate_browser__set_top(self, self->curr_hot);
  262. nd = self->curr_hot;
  263. while (1) {
  264. key = ui_browser__run(&self->b, delay_secs);
  265. if (delay_secs != 0) {
  266. annotate_browser__calc_percent(self, evidx);
  267. /*
  268. * Current line focus got out of the list of most active
  269. * lines, NULL it so that if TAB|UNTAB is pressed, we
  270. * move to curr_hot (current hottest line).
  271. */
  272. if (nd != NULL && RB_EMPTY_NODE(nd))
  273. nd = NULL;
  274. }
  275. switch (key) {
  276. case K_TIMER:
  277. if (timer != NULL)
  278. timer(arg);
  279. if (delay_secs != 0)
  280. symbol__annotate_decay_histogram(sym, evidx);
  281. continue;
  282. case K_TAB:
  283. if (nd != NULL) {
  284. nd = rb_prev(nd);
  285. if (nd == NULL)
  286. nd = rb_last(&self->entries);
  287. } else
  288. nd = self->curr_hot;
  289. break;
  290. case K_UNTAB:
  291. if (nd != NULL)
  292. nd = rb_next(nd);
  293. if (nd == NULL)
  294. nd = rb_first(&self->entries);
  295. else
  296. nd = self->curr_hot;
  297. break;
  298. case 'H':
  299. case 'h':
  300. nd = self->curr_hot;
  301. break;
  302. case 'S':
  303. case 's':
  304. if (annotate_browser__toggle_source(self))
  305. ui_helpline__puts(help);
  306. continue;
  307. case 'O':
  308. case 'o':
  309. self->use_offset = !self->use_offset;
  310. continue;
  311. case K_ENTER:
  312. case K_RIGHT:
  313. if (self->selection == NULL)
  314. ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
  315. else if (self->selection->offset == -1)
  316. ui_helpline__puts("Actions are only available for assembly lines.");
  317. else if (!annotate_browser__callq(self, evidx, timer, arg, delay_secs))
  318. ui_helpline__puts("Actions are only available for the 'callq' instruction.");
  319. continue;
  320. case K_LEFT:
  321. case K_ESC:
  322. case 'q':
  323. case CTRL('c'):
  324. goto out;
  325. default:
  326. continue;
  327. }
  328. if (nd != NULL)
  329. annotate_browser__set_top(self, nd);
  330. }
  331. out:
  332. ui_browser__hide(&self->b);
  333. return key;
  334. }
  335. int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
  336. void(*timer)(void *arg), void *arg, int delay_secs)
  337. {
  338. return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
  339. timer, arg, delay_secs);
  340. }
  341. int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
  342. void(*timer)(void *arg), void *arg,
  343. int delay_secs)
  344. {
  345. struct objdump_line *pos, *n;
  346. struct annotation *notes;
  347. struct map_symbol ms = {
  348. .map = map,
  349. .sym = sym,
  350. };
  351. struct annotate_browser browser = {
  352. .b = {
  353. .refresh = ui_browser__list_head_refresh,
  354. .seek = ui_browser__list_head_seek,
  355. .write = annotate_browser__write,
  356. .filter = objdump_line__filter,
  357. .priv = &ms,
  358. .use_navkeypressed = true,
  359. },
  360. };
  361. int ret;
  362. if (sym == NULL)
  363. return -1;
  364. if (map->dso->annotate_warned)
  365. return -1;
  366. if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
  367. ui__error("%s", ui_helpline__last_msg);
  368. return -1;
  369. }
  370. ui_helpline__push("Press <- or ESC to exit");
  371. notes = symbol__annotation(sym);
  372. browser.start = map__rip_2objdump(map, sym->start);
  373. list_for_each_entry(pos, &notes->src->source, node) {
  374. struct objdump_line_rb_node *rbpos;
  375. size_t line_len = strlen(pos->line);
  376. if (browser.b.width < line_len)
  377. browser.b.width = line_len;
  378. rbpos = objdump_line__rb(pos);
  379. rbpos->idx = browser.nr_entries++;
  380. if (pos->offset != -1)
  381. rbpos->idx_asm = browser.nr_asm_entries++;
  382. else
  383. rbpos->idx_asm = -1;
  384. }
  385. browser.b.nr_entries = browser.nr_entries;
  386. browser.b.entries = &notes->src->source,
  387. browser.b.width += 18; /* Percentage */
  388. ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
  389. list_for_each_entry_safe(pos, n, &notes->src->source, node) {
  390. list_del(&pos->node);
  391. objdump_line__free(pos);
  392. }
  393. return ret;
  394. }