Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 1 | #include "../../util.h" |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 2 | #include "../browser.h" |
| 3 | #include "../helpline.h" |
| 4 | #include "../libslang.h" |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 5 | #include "../ui.h" |
| 6 | #include "../util.h" |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 7 | #include "../../annotate.h" |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 8 | #include "../../hist.h" |
| 9 | #include "../../sort.h" |
| 10 | #include "../../symbol.h" |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 11 | #include <pthread.h> |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 12 | #include <newt.h> |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 13 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 14 | struct annotate_browser { |
| 15 | struct ui_browser b; |
| 16 | struct rb_root entries; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 17 | struct rb_node *curr_hot; |
Arnaldo Carvalho de Melo | 3495854 | 2011-10-05 19:35:54 -0300 | [diff] [blame] | 18 | struct objdump_line *selection; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 19 | u64 start; |
Arnaldo Carvalho de Melo | 0361fc2 | 2011-10-14 12:31:21 -0300 | [diff] [blame] | 20 | int nr_asm_entries; |
| 21 | int nr_entries; |
| 22 | bool hide_src_code; |
Arnaldo Carvalho de Melo | e235f3f | 2012-04-02 13:21:55 -0300 | [diff] [blame] | 23 | bool use_offset; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | struct objdump_line_rb_node { |
| 27 | struct rb_node rb_node; |
| 28 | double percent; |
| 29 | u32 idx; |
Arnaldo Carvalho de Melo | 0361fc2 | 2011-10-14 12:31:21 -0300 | [diff] [blame] | 30 | int idx_asm; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | static inline |
| 34 | struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self) |
| 35 | { |
| 36 | return (struct objdump_line_rb_node *)(self + 1); |
| 37 | } |
| 38 | |
Arnaldo Carvalho de Melo | 0361fc2 | 2011-10-14 12:31:21 -0300 | [diff] [blame] | 39 | static bool objdump_line__filter(struct ui_browser *browser, void *entry) |
| 40 | { |
| 41 | struct annotate_browser *ab = container_of(browser, struct annotate_browser, b); |
| 42 | |
| 43 | if (ab->hide_src_code) { |
| 44 | struct objdump_line *ol = list_entry(entry, struct objdump_line, node); |
| 45 | return ol->offset == -1; |
| 46 | } |
| 47 | |
| 48 | return false; |
| 49 | } |
| 50 | |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 51 | static void annotate_browser__write(struct ui_browser *self, void *entry, int row) |
| 52 | { |
Arnaldo Carvalho de Melo | 3495854 | 2011-10-05 19:35:54 -0300 | [diff] [blame] | 53 | struct annotate_browser *ab = container_of(self, struct annotate_browser, b); |
Arnaldo Carvalho de Melo | 0361fc2 | 2011-10-14 12:31:21 -0300 | [diff] [blame] | 54 | struct objdump_line *ol = list_entry(entry, struct objdump_line, node); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 55 | bool current_entry = ui_browser__is_current_entry(self, row); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 56 | bool change_color = (!ab->hide_src_code && |
| 57 | (!current_entry || (self->use_navkeypressed && |
| 58 | !self->navkeypressed))); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 59 | int width = self->width; |
| 60 | |
| 61 | if (ol->offset != -1) { |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 62 | struct objdump_line_rb_node *olrb = objdump_line__rb(ol); |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 63 | ui_browser__set_percent_color(self, olrb->percent, current_entry); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 64 | slsmg_printf(" %7.2f ", olrb->percent); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 65 | } else { |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 66 | ui_browser__set_percent_color(self, 0, current_entry); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 67 | slsmg_write_nstring(" ", 9); |
| 68 | } |
| 69 | |
| 70 | SLsmg_write_char(':'); |
| 71 | slsmg_write_nstring(" ", 8); |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 72 | |
| 73 | /* The scroll bar isn't being used */ |
| 74 | if (!self->navkeypressed) |
| 75 | width += 1; |
| 76 | |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 77 | if (ol->offset != -1 && change_color) |
| 78 | ui_browser__set_color(self, HE_COLORSET_CODE); |
Namhyung Kim | 58e817d | 2012-02-23 17:46:20 +0900 | [diff] [blame] | 79 | |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 80 | if (!*ol->line) |
| 81 | slsmg_write_nstring(" ", width - 18); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 82 | else if (ol->offset == -1) |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 83 | slsmg_write_nstring(ol->line, width - 18); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 84 | else { |
| 85 | char bf[64]; |
Arnaldo Carvalho de Melo | e235f3f | 2012-04-02 13:21:55 -0300 | [diff] [blame] | 86 | u64 addr = ol->offset; |
| 87 | int printed, color = -1; |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 88 | |
Arnaldo Carvalho de Melo | e235f3f | 2012-04-02 13:21:55 -0300 | [diff] [blame] | 89 | if (!ab->use_offset) |
| 90 | addr += ab->start; |
| 91 | |
| 92 | printed = scnprintf(bf, sizeof(bf), " %" PRIx64 ":", addr); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 93 | if (change_color) |
| 94 | color = ui_browser__set_color(self, HE_COLORSET_ADDR); |
| 95 | slsmg_write_nstring(bf, printed); |
| 96 | if (change_color) |
| 97 | ui_browser__set_color(self, color); |
| 98 | slsmg_write_nstring(ol->line, width - 18 - printed); |
| 99 | } |
Arnaldo Carvalho de Melo | b99976e | 2011-02-09 13:59:14 -0200 | [diff] [blame] | 100 | |
Namhyung Kim | 58e817d | 2012-02-23 17:46:20 +0900 | [diff] [blame] | 101 | if (current_entry) |
Arnaldo Carvalho de Melo | 3495854 | 2011-10-05 19:35:54 -0300 | [diff] [blame] | 102 | ab->selection = ol; |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 103 | } |
| 104 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 105 | static double objdump_line__calc_percent(struct objdump_line *self, |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 106 | struct symbol *sym, int evidx) |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 107 | { |
| 108 | double percent = 0.0; |
| 109 | |
| 110 | if (self->offset != -1) { |
| 111 | int len = sym->end - sym->start; |
| 112 | unsigned int hits = 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 113 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 114 | struct source_line *src_line = notes->src->lines; |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 115 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 116 | s64 offset = self->offset; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 117 | struct objdump_line *next; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 118 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 119 | next = objdump__get_next_ip_line(¬es->src->source, self); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 120 | while (offset < (s64)len && |
| 121 | (next == NULL || offset < next->offset)) { |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 122 | if (src_line) { |
| 123 | percent += src_line[offset].percent; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 124 | } else |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 125 | hits += h->addr[offset]; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 126 | |
| 127 | ++offset; |
| 128 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 129 | /* |
| 130 | * If the percentage wasn't already calculated in |
| 131 | * symbol__get_source_line, do it now: |
| 132 | */ |
| 133 | if (src_line == NULL && h->sum) |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 134 | percent = 100.0 * hits / h->sum; |
| 135 | } |
| 136 | |
| 137 | return percent; |
| 138 | } |
| 139 | |
| 140 | static void objdump__insert_line(struct rb_root *self, |
| 141 | struct objdump_line_rb_node *line) |
| 142 | { |
| 143 | struct rb_node **p = &self->rb_node; |
| 144 | struct rb_node *parent = NULL; |
| 145 | struct objdump_line_rb_node *l; |
| 146 | |
| 147 | while (*p != NULL) { |
| 148 | parent = *p; |
| 149 | l = rb_entry(parent, struct objdump_line_rb_node, rb_node); |
| 150 | if (line->percent < l->percent) |
| 151 | p = &(*p)->rb_left; |
| 152 | else |
| 153 | p = &(*p)->rb_right; |
| 154 | } |
| 155 | rb_link_node(&line->rb_node, parent, p); |
| 156 | rb_insert_color(&line->rb_node, self); |
| 157 | } |
| 158 | |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 159 | static void annotate_browser__set_top(struct annotate_browser *self, |
Arnaldo Carvalho de Melo | b0ffb2c | 2012-04-03 15:32:45 -0300 | [diff] [blame] | 160 | struct objdump_line *pos, u32 idx) |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 161 | { |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 162 | unsigned back; |
| 163 | |
| 164 | ui_browser__refresh_dimensions(&self->b); |
| 165 | back = self->b.height / 2; |
Arnaldo Carvalho de Melo | b0ffb2c | 2012-04-03 15:32:45 -0300 | [diff] [blame] | 166 | self->b.top_idx = self->b.index = idx; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 167 | |
| 168 | while (self->b.top_idx != 0 && back != 0) { |
| 169 | pos = list_entry(pos->node.prev, struct objdump_line, node); |
| 170 | |
Arnaldo Carvalho de Melo | 08be4ee | 2012-04-03 21:35:35 -0300 | [diff] [blame^] | 171 | if (objdump_line__filter(&self->b, &pos->node)) |
| 172 | continue; |
| 173 | |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 174 | --self->b.top_idx; |
| 175 | --back; |
| 176 | } |
| 177 | |
| 178 | self->b.top = pos; |
Arnaldo Carvalho de Melo | b0ffb2c | 2012-04-03 15:32:45 -0300 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static void annotate_browser__set_rb_top(struct annotate_browser *browser, |
| 182 | struct rb_node *nd) |
| 183 | { |
| 184 | struct objdump_line_rb_node *rbpos; |
| 185 | struct objdump_line *pos; |
| 186 | |
| 187 | rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node); |
| 188 | pos = ((struct objdump_line *)rbpos) - 1; |
| 189 | annotate_browser__set_top(browser, pos, rbpos->idx); |
| 190 | browser->curr_hot = nd; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 191 | } |
| 192 | |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 193 | static void annotate_browser__calc_percent(struct annotate_browser *browser, |
| 194 | int evidx) |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 195 | { |
Arnaldo Carvalho de Melo | 3495854 | 2011-10-05 19:35:54 -0300 | [diff] [blame] | 196 | struct map_symbol *ms = browser->b.priv; |
| 197 | struct symbol *sym = ms->sym; |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 198 | struct annotation *notes = symbol__annotation(sym); |
| 199 | struct objdump_line *pos; |
| 200 | |
| 201 | browser->entries = RB_ROOT; |
| 202 | |
| 203 | pthread_mutex_lock(¬es->lock); |
| 204 | |
| 205 | list_for_each_entry(pos, ¬es->src->source, node) { |
| 206 | struct objdump_line_rb_node *rbpos = objdump_line__rb(pos); |
| 207 | rbpos->percent = objdump_line__calc_percent(pos, sym, evidx); |
| 208 | if (rbpos->percent < 0.01) { |
| 209 | RB_CLEAR_NODE(&rbpos->rb_node); |
| 210 | continue; |
| 211 | } |
| 212 | objdump__insert_line(&browser->entries, rbpos); |
| 213 | } |
| 214 | pthread_mutex_unlock(¬es->lock); |
| 215 | |
| 216 | browser->curr_hot = rb_last(&browser->entries); |
| 217 | } |
| 218 | |
Arnaldo Carvalho de Melo | 0361fc2 | 2011-10-14 12:31:21 -0300 | [diff] [blame] | 219 | static bool annotate_browser__toggle_source(struct annotate_browser *browser) |
| 220 | { |
| 221 | struct objdump_line *ol; |
| 222 | struct objdump_line_rb_node *olrb; |
| 223 | off_t offset = browser->b.index - browser->b.top_idx; |
| 224 | |
| 225 | browser->b.seek(&browser->b, offset, SEEK_CUR); |
| 226 | ol = list_entry(browser->b.top, struct objdump_line, node); |
| 227 | olrb = objdump_line__rb(ol); |
| 228 | |
| 229 | if (browser->hide_src_code) { |
| 230 | if (olrb->idx_asm < offset) |
| 231 | offset = olrb->idx; |
| 232 | |
| 233 | browser->b.nr_entries = browser->nr_entries; |
| 234 | browser->hide_src_code = false; |
| 235 | browser->b.seek(&browser->b, -offset, SEEK_CUR); |
| 236 | browser->b.top_idx = olrb->idx - offset; |
| 237 | browser->b.index = olrb->idx; |
| 238 | } else { |
| 239 | if (olrb->idx_asm < 0) { |
| 240 | ui_helpline__puts("Only available for assembly lines."); |
| 241 | browser->b.seek(&browser->b, -offset, SEEK_CUR); |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | if (olrb->idx_asm < offset) |
| 246 | offset = olrb->idx_asm; |
| 247 | |
| 248 | browser->b.nr_entries = browser->nr_asm_entries; |
| 249 | browser->hide_src_code = true; |
| 250 | browser->b.seek(&browser->b, -offset, SEEK_CUR); |
| 251 | browser->b.top_idx = olrb->idx_asm - offset; |
| 252 | browser->b.index = olrb->idx_asm; |
| 253 | } |
| 254 | |
| 255 | return true; |
| 256 | } |
| 257 | |
Arnaldo Carvalho de Melo | 6052170 | 2012-04-02 13:58:33 -0300 | [diff] [blame] | 258 | static bool annotate_browser__callq(struct annotate_browser *browser, |
| 259 | int evidx, void (*timer)(void *arg), |
| 260 | void *arg, int delay_secs) |
| 261 | { |
| 262 | struct map_symbol *ms = browser->b.priv; |
| 263 | struct symbol *sym = ms->sym; |
| 264 | struct annotation *notes; |
| 265 | struct symbol *target; |
| 266 | char *s = strstr(browser->selection->line, "callq "); |
| 267 | u64 ip; |
| 268 | |
| 269 | if (s == NULL) |
| 270 | return false; |
| 271 | |
| 272 | s = strchr(s, ' '); |
| 273 | if (s++ == NULL) { |
| 274 | ui_helpline__puts("Invallid callq instruction."); |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | ip = strtoull(s, NULL, 16); |
| 279 | ip = ms->map->map_ip(ms->map, ip); |
| 280 | target = map__find_symbol(ms->map, ip, NULL); |
| 281 | if (target == NULL) { |
| 282 | ui_helpline__puts("The called function was not found."); |
| 283 | return true; |
| 284 | } |
| 285 | |
| 286 | notes = symbol__annotation(target); |
| 287 | pthread_mutex_lock(¬es->lock); |
| 288 | |
| 289 | if (notes->src == NULL && symbol__alloc_hist(target) < 0) { |
| 290 | pthread_mutex_unlock(¬es->lock); |
| 291 | ui__warning("Not enough memory for annotating '%s' symbol!\n", |
| 292 | target->name); |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | pthread_mutex_unlock(¬es->lock); |
| 297 | symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs); |
| 298 | ui_browser__show_title(&browser->b, sym->name); |
| 299 | return true; |
| 300 | } |
| 301 | |
Arnaldo Carvalho de Melo | 08be4ee | 2012-04-03 21:35:35 -0300 | [diff] [blame^] | 302 | static struct objdump_line * |
| 303 | annotate_browser__find_offset(struct annotate_browser *browser, |
| 304 | s64 offset, s64 *idx) |
| 305 | { |
| 306 | struct map_symbol *ms = browser->b.priv; |
| 307 | struct symbol *sym = ms->sym; |
| 308 | struct annotation *notes = symbol__annotation(sym); |
| 309 | struct objdump_line *pos; |
| 310 | |
| 311 | *idx = 0; |
| 312 | list_for_each_entry(pos, ¬es->src->source, node) { |
| 313 | if (pos->offset == offset) |
| 314 | return pos; |
| 315 | if (!objdump_line__filter(&browser->b, &pos->node)) |
| 316 | ++*idx; |
| 317 | } |
| 318 | |
| 319 | return NULL; |
| 320 | } |
| 321 | |
| 322 | static bool annotate_browser__jump(struct annotate_browser *browser) |
| 323 | { |
| 324 | const char *jumps[] = { "je ", "jne ", "ja ", "jmpq ", "js ", "jmp ", NULL }; |
| 325 | struct objdump_line *line; |
| 326 | s64 idx, offset; |
| 327 | char *s = NULL; |
| 328 | int i = 0; |
| 329 | |
| 330 | while (jumps[i]) { |
| 331 | s = strstr(browser->selection->line, jumps[i++]); |
| 332 | if (s) |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | if (s == NULL) |
| 337 | return false; |
| 338 | |
| 339 | s = strchr(s, '+'); |
| 340 | if (s++ == NULL) { |
| 341 | ui_helpline__puts("Invallid jump instruction."); |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | offset = strtoll(s, NULL, 16); |
| 346 | line = annotate_browser__find_offset(browser, offset, &idx); |
| 347 | if (line == NULL) { |
| 348 | ui_helpline__puts("Invallid jump offset"); |
| 349 | return true; |
| 350 | } |
| 351 | |
| 352 | annotate_browser__set_top(browser, line, idx); |
| 353 | |
| 354 | return true; |
| 355 | } |
| 356 | |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 357 | static int annotate_browser__run(struct annotate_browser *self, int evidx, |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 358 | void(*timer)(void *arg), |
Arnaldo Carvalho de Melo | 3495854 | 2011-10-05 19:35:54 -0300 | [diff] [blame] | 359 | void *arg, int delay_secs) |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 360 | { |
| 361 | struct rb_node *nd = NULL; |
Arnaldo Carvalho de Melo | 3495854 | 2011-10-05 19:35:54 -0300 | [diff] [blame] | 362 | struct map_symbol *ms = self->b.priv; |
| 363 | struct symbol *sym = ms->sym; |
Namhyung Kim | 824ac0e | 2012-02-23 17:46:23 +0900 | [diff] [blame] | 364 | const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, " |
| 365 | "H: Go to hottest line, ->/ENTER: Line action, " |
Arnaldo Carvalho de Melo | e235f3f | 2012-04-02 13:21:55 -0300 | [diff] [blame] | 366 | "O: Toggle offset view, " |
Namhyung Kim | 824ac0e | 2012-02-23 17:46:23 +0900 | [diff] [blame] | 367 | "S: Toggle source code view"; |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 368 | int key; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 369 | |
Arnaldo Carvalho de Melo | 0361fc2 | 2011-10-14 12:31:21 -0300 | [diff] [blame] | 370 | if (ui_browser__show(&self->b, sym->name, help) < 0) |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 371 | return -1; |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 372 | |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 373 | annotate_browser__calc_percent(self, evidx); |
| 374 | |
| 375 | if (self->curr_hot) |
Arnaldo Carvalho de Melo | b0ffb2c | 2012-04-03 15:32:45 -0300 | [diff] [blame] | 376 | annotate_browser__set_rb_top(self, self->curr_hot); |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 377 | |
| 378 | nd = self->curr_hot; |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 379 | |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 380 | while (1) { |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 381 | key = ui_browser__run(&self->b, delay_secs); |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 382 | |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 383 | if (delay_secs != 0) { |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 384 | annotate_browser__calc_percent(self, evidx); |
| 385 | /* |
| 386 | * Current line focus got out of the list of most active |
| 387 | * lines, NULL it so that if TAB|UNTAB is pressed, we |
| 388 | * move to curr_hot (current hottest line). |
| 389 | */ |
| 390 | if (nd != NULL && RB_EMPTY_NODE(nd)) |
| 391 | nd = NULL; |
| 392 | } |
| 393 | |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 394 | switch (key) { |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 395 | case K_TIMER: |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 396 | if (timer != NULL) |
| 397 | timer(arg); |
| 398 | |
| 399 | if (delay_secs != 0) |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 400 | symbol__annotate_decay_histogram(sym, evidx); |
| 401 | continue; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 402 | case K_TAB: |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 403 | if (nd != NULL) { |
| 404 | nd = rb_prev(nd); |
| 405 | if (nd == NULL) |
| 406 | nd = rb_last(&self->entries); |
| 407 | } else |
| 408 | nd = self->curr_hot; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 409 | break; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 410 | case K_UNTAB: |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 411 | if (nd != NULL) |
| 412 | nd = rb_next(nd); |
| 413 | if (nd == NULL) |
| 414 | nd = rb_first(&self->entries); |
| 415 | else |
| 416 | nd = self->curr_hot; |
| 417 | break; |
| 418 | case 'H': |
Namhyung Kim | ef7c537 | 2012-02-23 17:46:21 +0900 | [diff] [blame] | 419 | case 'h': |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 420 | nd = self->curr_hot; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 421 | break; |
Arnaldo Carvalho de Melo | 0361fc2 | 2011-10-14 12:31:21 -0300 | [diff] [blame] | 422 | case 'S': |
Namhyung Kim | ef7c537 | 2012-02-23 17:46:21 +0900 | [diff] [blame] | 423 | case 's': |
Arnaldo Carvalho de Melo | 0361fc2 | 2011-10-14 12:31:21 -0300 | [diff] [blame] | 424 | if (annotate_browser__toggle_source(self)) |
| 425 | ui_helpline__puts(help); |
| 426 | continue; |
Arnaldo Carvalho de Melo | e235f3f | 2012-04-02 13:21:55 -0300 | [diff] [blame] | 427 | case 'O': |
| 428 | case 'o': |
| 429 | self->use_offset = !self->use_offset; |
| 430 | continue; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 431 | case K_ENTER: |
| 432 | case K_RIGHT: |
Arnaldo Carvalho de Melo | 6052170 | 2012-04-02 13:58:33 -0300 | [diff] [blame] | 433 | if (self->selection == NULL) |
Arnaldo Carvalho de Melo | 234a537 | 2011-10-06 09:45:29 -0300 | [diff] [blame] | 434 | ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org"); |
Arnaldo Carvalho de Melo | 6052170 | 2012-04-02 13:58:33 -0300 | [diff] [blame] | 435 | else if (self->selection->offset == -1) |
Arnaldo Carvalho de Melo | 234a537 | 2011-10-06 09:45:29 -0300 | [diff] [blame] | 436 | ui_helpline__puts("Actions are only available for assembly lines."); |
Arnaldo Carvalho de Melo | 08be4ee | 2012-04-03 21:35:35 -0300 | [diff] [blame^] | 437 | else if (!(annotate_browser__jump(self) || |
| 438 | annotate_browser__callq(self, evidx, timer, arg, delay_secs))) |
| 439 | ui_helpline__puts("Actions are only available for the 'callq' and jump instructions."); |
Arnaldo Carvalho de Melo | fe46e64 | 2011-10-19 13:18:13 -0200 | [diff] [blame] | 440 | continue; |
Arnaldo Carvalho de Melo | cf95800 | 2011-10-20 16:59:15 -0200 | [diff] [blame] | 441 | case K_LEFT: |
| 442 | case K_ESC: |
Arnaldo Carvalho de Melo | ed7e566 | 2011-10-13 08:31:22 -0300 | [diff] [blame] | 443 | case 'q': |
| 444 | case CTRL('c'): |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 445 | goto out; |
Arnaldo Carvalho de Melo | ed7e566 | 2011-10-13 08:31:22 -0300 | [diff] [blame] | 446 | default: |
| 447 | continue; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 448 | } |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 449 | |
| 450 | if (nd != NULL) |
Arnaldo Carvalho de Melo | b0ffb2c | 2012-04-03 15:32:45 -0300 | [diff] [blame] | 451 | annotate_browser__set_rb_top(self, nd); |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 452 | } |
| 453 | out: |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 454 | ui_browser__hide(&self->b); |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 455 | return key; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 456 | } |
| 457 | |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 458 | int hist_entry__tui_annotate(struct hist_entry *he, int evidx, |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 459 | void(*timer)(void *arg), void *arg, int delay_secs) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 460 | { |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 461 | return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx, |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 462 | timer, arg, delay_secs); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 463 | } |
| 464 | |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 465 | int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx, |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 466 | void(*timer)(void *arg), void *arg, |
Arnaldo Carvalho de Melo | 3495854 | 2011-10-05 19:35:54 -0300 | [diff] [blame] | 467 | int delay_secs) |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 468 | { |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 469 | struct objdump_line *pos, *n; |
Lin Ming | db9a9cbc | 2011-04-08 14:31:26 +0800 | [diff] [blame] | 470 | struct annotation *notes; |
Arnaldo Carvalho de Melo | 3495854 | 2011-10-05 19:35:54 -0300 | [diff] [blame] | 471 | struct map_symbol ms = { |
| 472 | .map = map, |
| 473 | .sym = sym, |
| 474 | }; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 475 | struct annotate_browser browser = { |
| 476 | .b = { |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 477 | .refresh = ui_browser__list_head_refresh, |
| 478 | .seek = ui_browser__list_head_seek, |
| 479 | .write = annotate_browser__write, |
Arnaldo Carvalho de Melo | 0361fc2 | 2011-10-14 12:31:21 -0300 | [diff] [blame] | 480 | .filter = objdump_line__filter, |
Arnaldo Carvalho de Melo | 3495854 | 2011-10-05 19:35:54 -0300 | [diff] [blame] | 481 | .priv = &ms, |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame] | 482 | .use_navkeypressed = true, |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 483 | }, |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 484 | }; |
| 485 | int ret; |
| 486 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 487 | if (sym == NULL) |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 488 | return -1; |
| 489 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 490 | if (map->dso->annotate_warned) |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 491 | return -1; |
| 492 | |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 493 | if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) { |
Arnaldo Carvalho de Melo | ae55795 | 2011-10-26 08:00:55 -0200 | [diff] [blame] | 494 | ui__error("%s", ui_helpline__last_msg); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 495 | return -1; |
| 496 | } |
| 497 | |
| 498 | ui_helpline__push("Press <- or ESC to exit"); |
| 499 | |
Lin Ming | db9a9cbc | 2011-04-08 14:31:26 +0800 | [diff] [blame] | 500 | notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | 058b4cc | 2012-04-02 12:59:01 -0300 | [diff] [blame] | 501 | browser.start = map__rip_2objdump(map, sym->start); |
Lin Ming | db9a9cbc | 2011-04-08 14:31:26 +0800 | [diff] [blame] | 502 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 503 | list_for_each_entry(pos, ¬es->src->source, node) { |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 504 | struct objdump_line_rb_node *rbpos; |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 505 | size_t line_len = strlen(pos->line); |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 506 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 507 | if (browser.b.width < line_len) |
| 508 | browser.b.width = line_len; |
| 509 | rbpos = objdump_line__rb(pos); |
Arnaldo Carvalho de Melo | 0361fc2 | 2011-10-14 12:31:21 -0300 | [diff] [blame] | 510 | rbpos->idx = browser.nr_entries++; |
| 511 | if (pos->offset != -1) |
| 512 | rbpos->idx_asm = browser.nr_asm_entries++; |
| 513 | else |
| 514 | rbpos->idx_asm = -1; |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 515 | } |
| 516 | |
Arnaldo Carvalho de Melo | 0361fc2 | 2011-10-14 12:31:21 -0300 | [diff] [blame] | 517 | browser.b.nr_entries = browser.nr_entries; |
Lin Ming | db9a9cbc | 2011-04-08 14:31:26 +0800 | [diff] [blame] | 518 | browser.b.entries = ¬es->src->source, |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 519 | browser.b.width += 18; /* Percentage */ |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 520 | ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 521 | 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] | 522 | list_del(&pos->node); |
| 523 | objdump_line__free(pos); |
| 524 | } |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 525 | return ret; |
| 526 | } |