Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 1 | #include "../browser.h" |
| 2 | #include "../helpline.h" |
| 3 | #include "../libslang.h" |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 4 | #include "../../annotate.h" |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 5 | #include "../../hist.h" |
| 6 | #include "../../sort.h" |
| 7 | #include "../../symbol.h" |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 8 | #include "../../annotate.h" |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 9 | |
| 10 | static void ui__error_window(const char *fmt, ...) |
| 11 | { |
| 12 | va_list ap; |
| 13 | |
| 14 | va_start(ap, fmt); |
| 15 | newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap); |
| 16 | va_end(ap); |
| 17 | } |
| 18 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 19 | struct annotate_browser { |
| 20 | struct ui_browser b; |
| 21 | struct rb_root entries; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 22 | struct rb_node *curr_hot; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | struct objdump_line_rb_node { |
| 26 | struct rb_node rb_node; |
| 27 | double percent; |
| 28 | u32 idx; |
| 29 | }; |
| 30 | |
| 31 | static inline |
| 32 | struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self) |
| 33 | { |
| 34 | return (struct objdump_line_rb_node *)(self + 1); |
| 35 | } |
| 36 | |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 37 | static void annotate_browser__write(struct ui_browser *self, void *entry, int row) |
| 38 | { |
| 39 | struct objdump_line *ol = rb_entry(entry, struct objdump_line, node); |
| 40 | bool current_entry = ui_browser__is_current_entry(self, row); |
| 41 | int width = self->width; |
| 42 | |
| 43 | if (ol->offset != -1) { |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 44 | struct objdump_line_rb_node *olrb = objdump_line__rb(ol); |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 45 | ui_browser__set_percent_color(self, olrb->percent, current_entry); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 46 | slsmg_printf(" %7.2f ", olrb->percent); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 47 | if (!current_entry) |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 48 | ui_browser__set_color(self, HE_COLORSET_CODE); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 49 | } else { |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 50 | ui_browser__set_percent_color(self, 0, current_entry); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 51 | slsmg_write_nstring(" ", 9); |
| 52 | } |
| 53 | |
| 54 | SLsmg_write_char(':'); |
| 55 | slsmg_write_nstring(" ", 8); |
| 56 | if (!*ol->line) |
| 57 | slsmg_write_nstring(" ", width - 18); |
| 58 | else |
| 59 | slsmg_write_nstring(ol->line, width - 18); |
| 60 | } |
| 61 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 62 | static double objdump_line__calc_percent(struct objdump_line *self, |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 63 | struct symbol *sym, int evidx) |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 64 | { |
| 65 | double percent = 0.0; |
| 66 | |
| 67 | if (self->offset != -1) { |
| 68 | int len = sym->end - sym->start; |
| 69 | unsigned int hits = 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 70 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 71 | struct source_line *src_line = notes->src->lines; |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 72 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 73 | s64 offset = self->offset; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 74 | struct objdump_line *next; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 75 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 76 | next = objdump__get_next_ip_line(¬es->src->source, self); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 77 | while (offset < (s64)len && |
| 78 | (next == NULL || offset < next->offset)) { |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 79 | if (src_line) { |
| 80 | percent += src_line[offset].percent; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 81 | } else |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 82 | hits += h->addr[offset]; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 83 | |
| 84 | ++offset; |
| 85 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 86 | /* |
| 87 | * If the percentage wasn't already calculated in |
| 88 | * symbol__get_source_line, do it now: |
| 89 | */ |
| 90 | if (src_line == NULL && h->sum) |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 91 | percent = 100.0 * hits / h->sum; |
| 92 | } |
| 93 | |
| 94 | return percent; |
| 95 | } |
| 96 | |
| 97 | static void objdump__insert_line(struct rb_root *self, |
| 98 | struct objdump_line_rb_node *line) |
| 99 | { |
| 100 | struct rb_node **p = &self->rb_node; |
| 101 | struct rb_node *parent = NULL; |
| 102 | struct objdump_line_rb_node *l; |
| 103 | |
| 104 | while (*p != NULL) { |
| 105 | parent = *p; |
| 106 | l = rb_entry(parent, struct objdump_line_rb_node, rb_node); |
| 107 | if (line->percent < l->percent) |
| 108 | p = &(*p)->rb_left; |
| 109 | else |
| 110 | p = &(*p)->rb_right; |
| 111 | } |
| 112 | rb_link_node(&line->rb_node, parent, p); |
| 113 | rb_insert_color(&line->rb_node, self); |
| 114 | } |
| 115 | |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 116 | static void annotate_browser__set_top(struct annotate_browser *self, |
| 117 | struct rb_node *nd) |
| 118 | { |
| 119 | struct objdump_line_rb_node *rbpos; |
| 120 | struct objdump_line *pos; |
| 121 | unsigned back; |
| 122 | |
| 123 | ui_browser__refresh_dimensions(&self->b); |
| 124 | back = self->b.height / 2; |
| 125 | rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node); |
| 126 | pos = ((struct objdump_line *)rbpos) - 1; |
| 127 | self->b.top_idx = self->b.index = rbpos->idx; |
| 128 | |
| 129 | while (self->b.top_idx != 0 && back != 0) { |
| 130 | pos = list_entry(pos->node.prev, struct objdump_line, node); |
| 131 | |
| 132 | --self->b.top_idx; |
| 133 | --back; |
| 134 | } |
| 135 | |
| 136 | self->b.top = pos; |
| 137 | self->curr_hot = nd; |
| 138 | } |
| 139 | |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 140 | static int annotate_browser__run(struct annotate_browser *self) |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 141 | { |
| 142 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 143 | struct symbol *sym = self->b.priv; |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 144 | int key; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 145 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 146 | if (ui_browser__show(&self->b, sym->name, |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 147 | "<-, -> or ESC: exit, TAB/shift+TAB: cycle thru samples") < 0) |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 148 | return -1; |
Arnaldo Carvalho de Melo | 4c1c952 | 2010-08-12 12:37:51 -0300 | [diff] [blame] | 149 | /* |
| 150 | * To allow builtin-annotate to cycle thru multiple symbols by |
| 151 | * examining the exit key for this function. |
| 152 | */ |
| 153 | ui_browser__add_exit_key(&self->b, NEWT_KEY_RIGHT); |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 154 | |
| 155 | nd = self->curr_hot; |
| 156 | if (nd) { |
Arnaldo Carvalho de Melo | 4c1c952 | 2010-08-12 12:37:51 -0300 | [diff] [blame] | 157 | int tabs[] = { NEWT_KEY_TAB, NEWT_KEY_UNTAB, 0 }; |
| 158 | ui_browser__add_exit_keys(&self->b, tabs); |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | while (1) { |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 162 | key = ui_browser__run(&self->b); |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 163 | |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 164 | switch (key) { |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 165 | case NEWT_KEY_TAB: |
| 166 | nd = rb_prev(nd); |
| 167 | if (nd == NULL) |
| 168 | nd = rb_last(&self->entries); |
| 169 | annotate_browser__set_top(self, nd); |
| 170 | break; |
| 171 | case NEWT_KEY_UNTAB: |
| 172 | nd = rb_next(nd); |
| 173 | if (nd == NULL) |
| 174 | nd = rb_first(&self->entries); |
| 175 | annotate_browser__set_top(self, nd); |
| 176 | break; |
| 177 | default: |
| 178 | goto out; |
| 179 | } |
| 180 | } |
| 181 | out: |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 182 | ui_browser__hide(&self->b); |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 183 | return key; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 184 | } |
| 185 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 186 | int hist_entry__tui_annotate(struct hist_entry *he, int evidx) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 187 | { |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 188 | return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 189 | } |
| 190 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 191 | int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx) |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 192 | { |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 193 | struct objdump_line *pos, *n; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 194 | struct objdump_line_rb_node *rbpos; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 195 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 196 | struct annotate_browser browser = { |
| 197 | .b = { |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 198 | .entries = ¬es->src->source, |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 199 | .refresh = ui_browser__list_head_refresh, |
| 200 | .seek = ui_browser__list_head_seek, |
| 201 | .write = annotate_browser__write, |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 202 | .priv = sym, |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 203 | }, |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 204 | }; |
| 205 | int ret; |
| 206 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 207 | if (sym == NULL) |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 208 | return -1; |
| 209 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 210 | if (map->dso->annotate_warned) |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 211 | return -1; |
| 212 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 213 | if (symbol__annotate(sym, map, sizeof(*rbpos)) < 0) { |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 214 | ui__error_window(ui_helpline__last_msg); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 215 | return -1; |
| 216 | } |
| 217 | |
| 218 | ui_helpline__push("Press <- or ESC to exit"); |
| 219 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 220 | list_for_each_entry(pos, ¬es->src->source, node) { |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 221 | size_t line_len = strlen(pos->line); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 222 | if (browser.b.width < line_len) |
| 223 | browser.b.width = line_len; |
| 224 | rbpos = objdump_line__rb(pos); |
| 225 | rbpos->idx = browser.b.nr_entries++; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 226 | rbpos->percent = objdump_line__calc_percent(pos, sym, evidx); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 227 | if (rbpos->percent < 0.01) |
| 228 | continue; |
| 229 | objdump__insert_line(&browser.entries, rbpos); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 230 | } |
| 231 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 232 | /* |
| 233 | * Position the browser at the hottest line. |
| 234 | */ |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 235 | browser.curr_hot = rb_last(&browser.entries); |
| 236 | if (browser.curr_hot) |
| 237 | annotate_browser__set_top(&browser, browser.curr_hot); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 238 | |
| 239 | browser.b.width += 18; /* Percentage */ |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 240 | ret = annotate_browser__run(&browser); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame^] | 241 | list_for_each_entry_safe(pos, n, ¬es->src->source, node) { |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 242 | list_del(&pos->node); |
| 243 | objdump_line__free(pos); |
| 244 | } |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 245 | return ret; |
| 246 | } |