annotate.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "../browser.h"
  2. #include "../helpline.h"
  3. #include "../libslang.h"
  4. #include "../../hist.h"
  5. #include "../../sort.h"
  6. #include "../../symbol.h"
  7. static void ui__error_window(const char *fmt, ...)
  8. {
  9. va_list ap;
  10. va_start(ap, fmt);
  11. newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
  12. va_end(ap);
  13. }
  14. static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
  15. {
  16. struct objdump_line *ol = rb_entry(entry, struct objdump_line, node);
  17. bool current_entry = ui_browser__is_current_entry(self, row);
  18. int width = self->width;
  19. if (ol->offset != -1) {
  20. struct hist_entry *he = self->priv;
  21. struct symbol *sym = he->ms.sym;
  22. int len = he->ms.sym->end - he->ms.sym->start;
  23. unsigned int hits = 0;
  24. double percent = 0.0;
  25. int color;
  26. struct sym_priv *priv = symbol__priv(sym);
  27. struct sym_ext *sym_ext = priv->ext;
  28. struct sym_hist *h = priv->hist;
  29. s64 offset = ol->offset;
  30. struct objdump_line *next = objdump__get_next_ip_line(self->entries, ol);
  31. while (offset < (s64)len &&
  32. (next == NULL || offset < next->offset)) {
  33. if (sym_ext) {
  34. percent += sym_ext[offset].percent;
  35. } else
  36. hits += h->ip[offset];
  37. ++offset;
  38. }
  39. if (sym_ext == NULL && h->sum)
  40. percent = 100.0 * hits / h->sum;
  41. color = ui_browser__percent_color(percent, current_entry);
  42. SLsmg_set_color(color);
  43. slsmg_printf(" %7.2f ", percent);
  44. if (!current_entry)
  45. SLsmg_set_color(HE_COLORSET_CODE);
  46. } else {
  47. int color = ui_browser__percent_color(0, current_entry);
  48. SLsmg_set_color(color);
  49. slsmg_write_nstring(" ", 9);
  50. }
  51. SLsmg_write_char(':');
  52. slsmg_write_nstring(" ", 8);
  53. if (!*ol->line)
  54. slsmg_write_nstring(" ", width - 18);
  55. else
  56. slsmg_write_nstring(ol->line, width - 18);
  57. }
  58. int hist_entry__tui_annotate(struct hist_entry *self)
  59. {
  60. struct newtExitStruct es;
  61. struct objdump_line *pos, *n;
  62. LIST_HEAD(head);
  63. struct ui_browser browser = {
  64. .entries = &head,
  65. .refresh = ui_browser__list_head_refresh,
  66. .seek = ui_browser__list_head_seek,
  67. .write = annotate_browser__write,
  68. .priv = self,
  69. };
  70. int ret;
  71. if (self->ms.sym == NULL)
  72. return -1;
  73. if (self->ms.map->dso->annotate_warned)
  74. return -1;
  75. if (hist_entry__annotate(self, &head) < 0) {
  76. ui__error_window(browser__last_msg);
  77. return -1;
  78. }
  79. ui_helpline__push("Press <- or ESC to exit");
  80. list_for_each_entry(pos, &head, node) {
  81. size_t line_len = strlen(pos->line);
  82. if (browser.width < line_len)
  83. browser.width = line_len;
  84. ++browser.nr_entries;
  85. }
  86. browser.width += 18; /* Percentage */
  87. ui_browser__show(&browser, self->ms.sym->name);
  88. newtFormAddHotKey(browser.form, ' ');
  89. ret = ui_browser__run(&browser, &es);
  90. newtFormDestroy(browser.form);
  91. newtPopWindow();
  92. list_for_each_entry_safe(pos, n, &head, node) {
  93. list_del(&pos->node);
  94. objdump_line__free(pos);
  95. }
  96. ui_helpline__pop();
  97. return ret;
  98. }