1aca7a94dSNamhyung Kim #include "../../util/util.h" 2aca7a94dSNamhyung Kim #include "../browser.h" 3aca7a94dSNamhyung Kim #include "../helpline.h" 4aca7a94dSNamhyung Kim #include "../libslang.h" 5aca7a94dSNamhyung Kim #include "../ui.h" 6aca7a94dSNamhyung Kim #include "../util.h" 7aca7a94dSNamhyung Kim #include "../../util/annotate.h" 8aca7a94dSNamhyung Kim #include "../../util/hist.h" 9aca7a94dSNamhyung Kim #include "../../util/sort.h" 10aca7a94dSNamhyung Kim #include "../../util/symbol.h" 11aca7a94dSNamhyung Kim #include <pthread.h> 12aca7a94dSNamhyung Kim #include <newt.h> 13aca7a94dSNamhyung Kim 14b793a401SArnaldo Carvalho de Melo struct browser_disasm_line { 15b793a401SArnaldo Carvalho de Melo struct rb_node rb_node; 16b793a401SArnaldo Carvalho de Melo double percent; 17b793a401SArnaldo Carvalho de Melo u32 idx; 18b793a401SArnaldo Carvalho de Melo int idx_asm; 19b793a401SArnaldo Carvalho de Melo bool jump_target; 20b793a401SArnaldo Carvalho de Melo }; 21b793a401SArnaldo Carvalho de Melo 22aca7a94dSNamhyung Kim struct annotate_browser { 23aca7a94dSNamhyung Kim struct ui_browser b; 24aca7a94dSNamhyung Kim struct rb_root entries; 25aca7a94dSNamhyung Kim struct rb_node *curr_hot; 2629ed6e76SArnaldo Carvalho de Melo struct disasm_line *selection; 27b793a401SArnaldo Carvalho de Melo struct disasm_line **offsets; 28aca7a94dSNamhyung Kim u64 start; 29aca7a94dSNamhyung Kim int nr_asm_entries; 30aca7a94dSNamhyung Kim int nr_entries; 31aca7a94dSNamhyung Kim bool hide_src_code; 32aca7a94dSNamhyung Kim bool use_offset; 33aca7a94dSNamhyung Kim bool searching_backwards; 3461e04b33SArnaldo Carvalho de Melo u8 offset_width; 35aca7a94dSNamhyung Kim char search_bf[128]; 36aca7a94dSNamhyung Kim }; 37aca7a94dSNamhyung Kim 38887c0066SArnaldo Carvalho de Melo static inline struct browser_disasm_line *disasm_line__browser(struct disasm_line *dl) 39aca7a94dSNamhyung Kim { 40887c0066SArnaldo Carvalho de Melo return (struct browser_disasm_line *)(dl + 1); 41aca7a94dSNamhyung Kim } 42aca7a94dSNamhyung Kim 4329ed6e76SArnaldo Carvalho de Melo static bool disasm_line__filter(struct ui_browser *browser, void *entry) 44aca7a94dSNamhyung Kim { 45aca7a94dSNamhyung Kim struct annotate_browser *ab = container_of(browser, struct annotate_browser, b); 46aca7a94dSNamhyung Kim 47aca7a94dSNamhyung Kim if (ab->hide_src_code) { 4829ed6e76SArnaldo Carvalho de Melo struct disasm_line *dl = list_entry(entry, struct disasm_line, node); 4929ed6e76SArnaldo Carvalho de Melo return dl->offset == -1; 50aca7a94dSNamhyung Kim } 51aca7a94dSNamhyung Kim 52aca7a94dSNamhyung Kim return false; 53aca7a94dSNamhyung Kim } 54aca7a94dSNamhyung Kim 55aca7a94dSNamhyung Kim static void annotate_browser__write(struct ui_browser *self, void *entry, int row) 56aca7a94dSNamhyung Kim { 57aca7a94dSNamhyung Kim struct annotate_browser *ab = container_of(self, struct annotate_browser, b); 5829ed6e76SArnaldo Carvalho de Melo struct disasm_line *dl = list_entry(entry, struct disasm_line, node); 59b793a401SArnaldo Carvalho de Melo struct browser_disasm_line *bdl = disasm_line__browser(dl); 60aca7a94dSNamhyung Kim bool current_entry = ui_browser__is_current_entry(self, row); 61aca7a94dSNamhyung Kim bool change_color = (!ab->hide_src_code && 62aca7a94dSNamhyung Kim (!current_entry || (self->use_navkeypressed && 63aca7a94dSNamhyung Kim !self->navkeypressed))); 64aca7a94dSNamhyung Kim int width = self->width; 65aca7a94dSNamhyung Kim 6629ed6e76SArnaldo Carvalho de Melo if (dl->offset != -1) { 67887c0066SArnaldo Carvalho de Melo ui_browser__set_percent_color(self, bdl->percent, current_entry); 68887c0066SArnaldo Carvalho de Melo slsmg_printf(" %7.2f ", bdl->percent); 69aca7a94dSNamhyung Kim } else { 70aca7a94dSNamhyung Kim ui_browser__set_percent_color(self, 0, current_entry); 71aca7a94dSNamhyung Kim slsmg_write_nstring(" ", 9); 72aca7a94dSNamhyung Kim } 73aca7a94dSNamhyung Kim 7459d038d5SArnaldo Carvalho de Melo ui_browser__write_graph(self, SLSMG_VLINE_CHAR); 75cf2dacc5SArnaldo Carvalho de Melo SLsmg_write_char(' '); 76aca7a94dSNamhyung Kim 77aca7a94dSNamhyung Kim /* The scroll bar isn't being used */ 78aca7a94dSNamhyung Kim if (!self->navkeypressed) 79aca7a94dSNamhyung Kim width += 1; 80aca7a94dSNamhyung Kim 8129ed6e76SArnaldo Carvalho de Melo if (dl->offset != -1 && change_color) 82aca7a94dSNamhyung Kim ui_browser__set_color(self, HE_COLORSET_CODE); 83aca7a94dSNamhyung Kim 8429ed6e76SArnaldo Carvalho de Melo if (!*dl->line) 858bf39cb8SArnaldo Carvalho de Melo slsmg_write_nstring(" ", width - 10); 8629ed6e76SArnaldo Carvalho de Melo else if (dl->offset == -1) 878bf39cb8SArnaldo Carvalho de Melo slsmg_write_nstring(dl->line, width - 10); 88aca7a94dSNamhyung Kim else { 898bf39cb8SArnaldo Carvalho de Melo char bf[256]; 9029ed6e76SArnaldo Carvalho de Melo u64 addr = dl->offset; 91aca7a94dSNamhyung Kim int printed, color = -1; 92aca7a94dSNamhyung Kim 93aca7a94dSNamhyung Kim if (!ab->use_offset) 94aca7a94dSNamhyung Kim addr += ab->start; 95aca7a94dSNamhyung Kim 9661e04b33SArnaldo Carvalho de Melo if (!ab->use_offset) { 97aca7a94dSNamhyung Kim printed = scnprintf(bf, sizeof(bf), " %" PRIx64 ":", addr); 9861e04b33SArnaldo Carvalho de Melo } else { 9961e04b33SArnaldo Carvalho de Melo if (bdl->jump_target) { 10061e04b33SArnaldo Carvalho de Melo printed = scnprintf(bf, sizeof(bf), " %*" PRIx64 ":", 10161e04b33SArnaldo Carvalho de Melo ab->offset_width, addr); 10261e04b33SArnaldo Carvalho de Melo } else { 10361e04b33SArnaldo Carvalho de Melo printed = scnprintf(bf, sizeof(bf), " %*s ", 10461e04b33SArnaldo Carvalho de Melo ab->offset_width, " "); 10561e04b33SArnaldo Carvalho de Melo } 10661e04b33SArnaldo Carvalho de Melo } 107b793a401SArnaldo Carvalho de Melo 108aca7a94dSNamhyung Kim if (change_color) 109aca7a94dSNamhyung Kim color = ui_browser__set_color(self, HE_COLORSET_ADDR); 110aca7a94dSNamhyung Kim slsmg_write_nstring(bf, printed); 111aca7a94dSNamhyung Kim if (change_color) 112aca7a94dSNamhyung Kim ui_browser__set_color(self, color); 11328548d78SArnaldo Carvalho de Melo if (dl->ins && dl->ins->ops->scnprintf) { 11451a0d455SArnaldo Carvalho de Melo if (ins__is_jump(dl->ins)) { 11544d1a3edSArnaldo Carvalho de Melo bool fwd = dl->ops.target.offset > (u64)dl->offset; 11651a0d455SArnaldo Carvalho de Melo 11759d038d5SArnaldo Carvalho de Melo ui_browser__write_graph(self, fwd ? SLSMG_DARROW_CHAR : 11851a0d455SArnaldo Carvalho de Melo SLSMG_UARROW_CHAR); 11951a0d455SArnaldo Carvalho de Melo SLsmg_write_char(' '); 12051a0d455SArnaldo Carvalho de Melo } else { 12151a0d455SArnaldo Carvalho de Melo slsmg_write_nstring(" ", 2); 12251a0d455SArnaldo Carvalho de Melo } 12351a0d455SArnaldo Carvalho de Melo 124c7e6ead7SArnaldo Carvalho de Melo dl->ins->ops->scnprintf(dl->ins, bf, sizeof(bf), &dl->ops, 125c7e6ead7SArnaldo Carvalho de Melo !ab->use_offset); 1264ea08b52SArnaldo Carvalho de Melo } else { 1274ea08b52SArnaldo Carvalho de Melo if (strcmp(dl->name, "retq")) { 1284ea08b52SArnaldo Carvalho de Melo slsmg_write_nstring(" ", 2); 1294ea08b52SArnaldo Carvalho de Melo } else { 13059d038d5SArnaldo Carvalho de Melo ui_browser__write_graph(self, SLSMG_LARROW_CHAR); 1314ea08b52SArnaldo Carvalho de Melo SLsmg_write_char(' '); 1324ea08b52SArnaldo Carvalho de Melo } 13328548d78SArnaldo Carvalho de Melo 1344ea08b52SArnaldo Carvalho de Melo scnprintf(bf, sizeof(bf), "%-6.6s %s", dl->name, dl->ops.raw); 1354ea08b52SArnaldo Carvalho de Melo } 1364ea08b52SArnaldo Carvalho de Melo 1374ea08b52SArnaldo Carvalho de Melo slsmg_write_nstring(bf, width - 12 - printed); 138aca7a94dSNamhyung Kim } 139aca7a94dSNamhyung Kim 140aca7a94dSNamhyung Kim if (current_entry) 14129ed6e76SArnaldo Carvalho de Melo ab->selection = dl; 142aca7a94dSNamhyung Kim } 143aca7a94dSNamhyung Kim 144a3f895beSArnaldo Carvalho de Melo static void annotate_browser__draw_current_loop(struct ui_browser *browser) 145a3f895beSArnaldo Carvalho de Melo { 146a3f895beSArnaldo Carvalho de Melo struct annotate_browser *ab = container_of(browser, struct annotate_browser, b); 147a3f895beSArnaldo Carvalho de Melo struct map_symbol *ms = browser->priv; 148a3f895beSArnaldo Carvalho de Melo struct symbol *sym = ms->sym; 149a3f895beSArnaldo Carvalho de Melo struct annotation *notes = symbol__annotation(sym); 150a3f895beSArnaldo Carvalho de Melo struct disasm_line *cursor = ab->selection, *pos = cursor, *target; 151a3f895beSArnaldo Carvalho de Melo struct browser_disasm_line *bcursor = disasm_line__browser(cursor), 152a3f895beSArnaldo Carvalho de Melo *btarget, *bpos; 153a3f895beSArnaldo Carvalho de Melo unsigned int from, to, start_width = 2; 154a3f895beSArnaldo Carvalho de Melo 155a3f895beSArnaldo Carvalho de Melo list_for_each_entry_from(pos, ¬es->src->source, node) { 156*38b31bd0SArnaldo Carvalho de Melo if (!pos->ins || !ins__is_jump(pos->ins) || 157*38b31bd0SArnaldo Carvalho de Melo !disasm_line__has_offset(pos)) 158a3f895beSArnaldo Carvalho de Melo continue; 159a3f895beSArnaldo Carvalho de Melo 16044d1a3edSArnaldo Carvalho de Melo target = ab->offsets[pos->ops.target.offset]; 161a3f895beSArnaldo Carvalho de Melo if (!target) 162a3f895beSArnaldo Carvalho de Melo continue; 163a3f895beSArnaldo Carvalho de Melo 164a3f895beSArnaldo Carvalho de Melo btarget = disasm_line__browser(target); 165a3f895beSArnaldo Carvalho de Melo if (btarget->idx <= bcursor->idx) 166a3f895beSArnaldo Carvalho de Melo goto found; 167a3f895beSArnaldo Carvalho de Melo } 168a3f895beSArnaldo Carvalho de Melo 169a3f895beSArnaldo Carvalho de Melo return; 170a3f895beSArnaldo Carvalho de Melo 171a3f895beSArnaldo Carvalho de Melo found: 172a3f895beSArnaldo Carvalho de Melo bpos = disasm_line__browser(pos); 173a3f895beSArnaldo Carvalho de Melo if (ab->hide_src_code) { 174a3f895beSArnaldo Carvalho de Melo from = bpos->idx_asm; 175a3f895beSArnaldo Carvalho de Melo to = btarget->idx_asm; 176a3f895beSArnaldo Carvalho de Melo } else { 177a3f895beSArnaldo Carvalho de Melo from = (u64)bpos->idx; 178a3f895beSArnaldo Carvalho de Melo to = (u64)btarget->idx; 179a3f895beSArnaldo Carvalho de Melo } 180a3f895beSArnaldo Carvalho de Melo 181a3f895beSArnaldo Carvalho de Melo ui_browser__set_color(browser, HE_COLORSET_CODE); 182a3f895beSArnaldo Carvalho de Melo 183a3f895beSArnaldo Carvalho de Melo if (!bpos->jump_target) 184a3f895beSArnaldo Carvalho de Melo start_width += ab->offset_width + 1; 185a3f895beSArnaldo Carvalho de Melo 186a3f895beSArnaldo Carvalho de Melo __ui_browser__line_arrow_up(browser, 10, from, to, start_width); 187a3f895beSArnaldo Carvalho de Melo } 188a3f895beSArnaldo Carvalho de Melo 189a3f895beSArnaldo Carvalho de Melo static unsigned int annotate_browser__refresh(struct ui_browser *browser) 190a3f895beSArnaldo Carvalho de Melo { 191a3f895beSArnaldo Carvalho de Melo int ret = ui_browser__list_head_refresh(browser); 192a3f895beSArnaldo Carvalho de Melo 193a3f895beSArnaldo Carvalho de Melo annotate_browser__draw_current_loop(browser); 194a3f895beSArnaldo Carvalho de Melo 195a3f895beSArnaldo Carvalho de Melo return ret; 196a3f895beSArnaldo Carvalho de Melo } 197a3f895beSArnaldo Carvalho de Melo 19829ed6e76SArnaldo Carvalho de Melo static double disasm_line__calc_percent(struct disasm_line *dl, struct symbol *sym, int evidx) 199aca7a94dSNamhyung Kim { 200aca7a94dSNamhyung Kim double percent = 0.0; 201aca7a94dSNamhyung Kim 20229ed6e76SArnaldo Carvalho de Melo if (dl->offset != -1) { 203aca7a94dSNamhyung Kim int len = sym->end - sym->start; 204aca7a94dSNamhyung Kim unsigned int hits = 0; 205aca7a94dSNamhyung Kim struct annotation *notes = symbol__annotation(sym); 206aca7a94dSNamhyung Kim struct source_line *src_line = notes->src->lines; 207aca7a94dSNamhyung Kim struct sym_hist *h = annotation__histogram(notes, evidx); 20829ed6e76SArnaldo Carvalho de Melo s64 offset = dl->offset; 20929ed6e76SArnaldo Carvalho de Melo struct disasm_line *next; 210aca7a94dSNamhyung Kim 21129ed6e76SArnaldo Carvalho de Melo next = disasm__get_next_ip_line(¬es->src->source, dl); 212aca7a94dSNamhyung Kim while (offset < (s64)len && 213aca7a94dSNamhyung Kim (next == NULL || offset < next->offset)) { 214aca7a94dSNamhyung Kim if (src_line) { 215aca7a94dSNamhyung Kim percent += src_line[offset].percent; 216aca7a94dSNamhyung Kim } else 217aca7a94dSNamhyung Kim hits += h->addr[offset]; 218aca7a94dSNamhyung Kim 219aca7a94dSNamhyung Kim ++offset; 220aca7a94dSNamhyung Kim } 221aca7a94dSNamhyung Kim /* 222aca7a94dSNamhyung Kim * If the percentage wasn't already calculated in 223aca7a94dSNamhyung Kim * symbol__get_source_line, do it now: 224aca7a94dSNamhyung Kim */ 225aca7a94dSNamhyung Kim if (src_line == NULL && h->sum) 226aca7a94dSNamhyung Kim percent = 100.0 * hits / h->sum; 227aca7a94dSNamhyung Kim } 228aca7a94dSNamhyung Kim 229aca7a94dSNamhyung Kim return percent; 230aca7a94dSNamhyung Kim } 231aca7a94dSNamhyung Kim 232887c0066SArnaldo Carvalho de Melo static void disasm_rb_tree__insert(struct rb_root *root, struct browser_disasm_line *bdl) 233aca7a94dSNamhyung Kim { 23429ed6e76SArnaldo Carvalho de Melo struct rb_node **p = &root->rb_node; 235aca7a94dSNamhyung Kim struct rb_node *parent = NULL; 236887c0066SArnaldo Carvalho de Melo struct browser_disasm_line *l; 237aca7a94dSNamhyung Kim 238aca7a94dSNamhyung Kim while (*p != NULL) { 239aca7a94dSNamhyung Kim parent = *p; 240887c0066SArnaldo Carvalho de Melo l = rb_entry(parent, struct browser_disasm_line, rb_node); 241887c0066SArnaldo Carvalho de Melo if (bdl->percent < l->percent) 242aca7a94dSNamhyung Kim p = &(*p)->rb_left; 243aca7a94dSNamhyung Kim else 244aca7a94dSNamhyung Kim p = &(*p)->rb_right; 245aca7a94dSNamhyung Kim } 246887c0066SArnaldo Carvalho de Melo rb_link_node(&bdl->rb_node, parent, p); 247887c0066SArnaldo Carvalho de Melo rb_insert_color(&bdl->rb_node, root); 248aca7a94dSNamhyung Kim } 249aca7a94dSNamhyung Kim 250aca7a94dSNamhyung Kim static void annotate_browser__set_top(struct annotate_browser *self, 25129ed6e76SArnaldo Carvalho de Melo struct disasm_line *pos, u32 idx) 252aca7a94dSNamhyung Kim { 253aca7a94dSNamhyung Kim unsigned back; 254aca7a94dSNamhyung Kim 255aca7a94dSNamhyung Kim ui_browser__refresh_dimensions(&self->b); 256aca7a94dSNamhyung Kim back = self->b.height / 2; 257aca7a94dSNamhyung Kim self->b.top_idx = self->b.index = idx; 258aca7a94dSNamhyung Kim 259aca7a94dSNamhyung Kim while (self->b.top_idx != 0 && back != 0) { 26029ed6e76SArnaldo Carvalho de Melo pos = list_entry(pos->node.prev, struct disasm_line, node); 261aca7a94dSNamhyung Kim 26229ed6e76SArnaldo Carvalho de Melo if (disasm_line__filter(&self->b, &pos->node)) 263aca7a94dSNamhyung Kim continue; 264aca7a94dSNamhyung Kim 265aca7a94dSNamhyung Kim --self->b.top_idx; 266aca7a94dSNamhyung Kim --back; 267aca7a94dSNamhyung Kim } 268aca7a94dSNamhyung Kim 269aca7a94dSNamhyung Kim self->b.top = pos; 270aca7a94dSNamhyung Kim self->b.navkeypressed = true; 271aca7a94dSNamhyung Kim } 272aca7a94dSNamhyung Kim 273aca7a94dSNamhyung Kim static void annotate_browser__set_rb_top(struct annotate_browser *browser, 274aca7a94dSNamhyung Kim struct rb_node *nd) 275aca7a94dSNamhyung Kim { 276887c0066SArnaldo Carvalho de Melo struct browser_disasm_line *bpos; 27729ed6e76SArnaldo Carvalho de Melo struct disasm_line *pos; 278aca7a94dSNamhyung Kim 279887c0066SArnaldo Carvalho de Melo bpos = rb_entry(nd, struct browser_disasm_line, rb_node); 280887c0066SArnaldo Carvalho de Melo pos = ((struct disasm_line *)bpos) - 1; 281887c0066SArnaldo Carvalho de Melo annotate_browser__set_top(browser, pos, bpos->idx); 282aca7a94dSNamhyung Kim browser->curr_hot = nd; 283aca7a94dSNamhyung Kim } 284aca7a94dSNamhyung Kim 285aca7a94dSNamhyung Kim static void annotate_browser__calc_percent(struct annotate_browser *browser, 286aca7a94dSNamhyung Kim int evidx) 287aca7a94dSNamhyung Kim { 288aca7a94dSNamhyung Kim struct map_symbol *ms = browser->b.priv; 289aca7a94dSNamhyung Kim struct symbol *sym = ms->sym; 290aca7a94dSNamhyung Kim struct annotation *notes = symbol__annotation(sym); 29129ed6e76SArnaldo Carvalho de Melo struct disasm_line *pos; 292aca7a94dSNamhyung Kim 293aca7a94dSNamhyung Kim browser->entries = RB_ROOT; 294aca7a94dSNamhyung Kim 295aca7a94dSNamhyung Kim pthread_mutex_lock(¬es->lock); 296aca7a94dSNamhyung Kim 297aca7a94dSNamhyung Kim list_for_each_entry(pos, ¬es->src->source, node) { 298887c0066SArnaldo Carvalho de Melo struct browser_disasm_line *bpos = disasm_line__browser(pos); 299887c0066SArnaldo Carvalho de Melo bpos->percent = disasm_line__calc_percent(pos, sym, evidx); 300887c0066SArnaldo Carvalho de Melo if (bpos->percent < 0.01) { 301887c0066SArnaldo Carvalho de Melo RB_CLEAR_NODE(&bpos->rb_node); 302aca7a94dSNamhyung Kim continue; 303aca7a94dSNamhyung Kim } 304887c0066SArnaldo Carvalho de Melo disasm_rb_tree__insert(&browser->entries, bpos); 305aca7a94dSNamhyung Kim } 306aca7a94dSNamhyung Kim pthread_mutex_unlock(¬es->lock); 307aca7a94dSNamhyung Kim 308aca7a94dSNamhyung Kim browser->curr_hot = rb_last(&browser->entries); 309aca7a94dSNamhyung Kim } 310aca7a94dSNamhyung Kim 311aca7a94dSNamhyung Kim static bool annotate_browser__toggle_source(struct annotate_browser *browser) 312aca7a94dSNamhyung Kim { 31329ed6e76SArnaldo Carvalho de Melo struct disasm_line *dl; 314887c0066SArnaldo Carvalho de Melo struct browser_disasm_line *bdl; 315aca7a94dSNamhyung Kim off_t offset = browser->b.index - browser->b.top_idx; 316aca7a94dSNamhyung Kim 317aca7a94dSNamhyung Kim browser->b.seek(&browser->b, offset, SEEK_CUR); 31829ed6e76SArnaldo Carvalho de Melo dl = list_entry(browser->b.top, struct disasm_line, node); 319887c0066SArnaldo Carvalho de Melo bdl = disasm_line__browser(dl); 320aca7a94dSNamhyung Kim 321aca7a94dSNamhyung Kim if (browser->hide_src_code) { 322887c0066SArnaldo Carvalho de Melo if (bdl->idx_asm < offset) 323887c0066SArnaldo Carvalho de Melo offset = bdl->idx; 324aca7a94dSNamhyung Kim 325aca7a94dSNamhyung Kim browser->b.nr_entries = browser->nr_entries; 326aca7a94dSNamhyung Kim browser->hide_src_code = false; 327aca7a94dSNamhyung Kim browser->b.seek(&browser->b, -offset, SEEK_CUR); 328887c0066SArnaldo Carvalho de Melo browser->b.top_idx = bdl->idx - offset; 329887c0066SArnaldo Carvalho de Melo browser->b.index = bdl->idx; 330aca7a94dSNamhyung Kim } else { 331887c0066SArnaldo Carvalho de Melo if (bdl->idx_asm < 0) { 332aca7a94dSNamhyung Kim ui_helpline__puts("Only available for assembly lines."); 333aca7a94dSNamhyung Kim browser->b.seek(&browser->b, -offset, SEEK_CUR); 334aca7a94dSNamhyung Kim return false; 335aca7a94dSNamhyung Kim } 336aca7a94dSNamhyung Kim 337887c0066SArnaldo Carvalho de Melo if (bdl->idx_asm < offset) 338887c0066SArnaldo Carvalho de Melo offset = bdl->idx_asm; 339aca7a94dSNamhyung Kim 340aca7a94dSNamhyung Kim browser->b.nr_entries = browser->nr_asm_entries; 341aca7a94dSNamhyung Kim browser->hide_src_code = true; 342aca7a94dSNamhyung Kim browser->b.seek(&browser->b, -offset, SEEK_CUR); 343887c0066SArnaldo Carvalho de Melo browser->b.top_idx = bdl->idx_asm - offset; 344887c0066SArnaldo Carvalho de Melo browser->b.index = bdl->idx_asm; 345aca7a94dSNamhyung Kim } 346aca7a94dSNamhyung Kim 347aca7a94dSNamhyung Kim return true; 348aca7a94dSNamhyung Kim } 349aca7a94dSNamhyung Kim 350aca7a94dSNamhyung Kim static bool annotate_browser__callq(struct annotate_browser *browser, 351aca7a94dSNamhyung Kim int evidx, void (*timer)(void *arg), 352aca7a94dSNamhyung Kim void *arg, int delay_secs) 353aca7a94dSNamhyung Kim { 354aca7a94dSNamhyung Kim struct map_symbol *ms = browser->b.priv; 355657bcaf5SArnaldo Carvalho de Melo struct disasm_line *dl = browser->selection; 356aca7a94dSNamhyung Kim struct symbol *sym = ms->sym; 357aca7a94dSNamhyung Kim struct annotation *notes; 358aca7a94dSNamhyung Kim struct symbol *target; 359aca7a94dSNamhyung Kim u64 ip; 360aca7a94dSNamhyung Kim 361d86b0597SArnaldo Carvalho de Melo if (!ins__is_call(dl->ins)) 362aca7a94dSNamhyung Kim return false; 363aca7a94dSNamhyung Kim 36444d1a3edSArnaldo Carvalho de Melo ip = ms->map->map_ip(ms->map, dl->ops.target.addr); 365aca7a94dSNamhyung Kim target = map__find_symbol(ms->map, ip, NULL); 366aca7a94dSNamhyung Kim if (target == NULL) { 367aca7a94dSNamhyung Kim ui_helpline__puts("The called function was not found."); 368aca7a94dSNamhyung Kim return true; 369aca7a94dSNamhyung Kim } 370aca7a94dSNamhyung Kim 371aca7a94dSNamhyung Kim notes = symbol__annotation(target); 372aca7a94dSNamhyung Kim pthread_mutex_lock(¬es->lock); 373aca7a94dSNamhyung Kim 374aca7a94dSNamhyung Kim if (notes->src == NULL && symbol__alloc_hist(target) < 0) { 375aca7a94dSNamhyung Kim pthread_mutex_unlock(¬es->lock); 376aca7a94dSNamhyung Kim ui__warning("Not enough memory for annotating '%s' symbol!\n", 377aca7a94dSNamhyung Kim target->name); 378aca7a94dSNamhyung Kim return true; 379aca7a94dSNamhyung Kim } 380aca7a94dSNamhyung Kim 381aca7a94dSNamhyung Kim pthread_mutex_unlock(¬es->lock); 382aca7a94dSNamhyung Kim symbol__tui_annotate(target, ms->map, evidx, timer, arg, delay_secs); 383aca7a94dSNamhyung Kim ui_browser__show_title(&browser->b, sym->name); 384aca7a94dSNamhyung Kim return true; 385aca7a94dSNamhyung Kim } 386aca7a94dSNamhyung Kim 38729ed6e76SArnaldo Carvalho de Melo static 38829ed6e76SArnaldo Carvalho de Melo struct disasm_line *annotate_browser__find_offset(struct annotate_browser *browser, 389aca7a94dSNamhyung Kim s64 offset, s64 *idx) 390aca7a94dSNamhyung Kim { 391aca7a94dSNamhyung Kim struct map_symbol *ms = browser->b.priv; 392aca7a94dSNamhyung Kim struct symbol *sym = ms->sym; 393aca7a94dSNamhyung Kim struct annotation *notes = symbol__annotation(sym); 39429ed6e76SArnaldo Carvalho de Melo struct disasm_line *pos; 395aca7a94dSNamhyung Kim 396aca7a94dSNamhyung Kim *idx = 0; 397aca7a94dSNamhyung Kim list_for_each_entry(pos, ¬es->src->source, node) { 398aca7a94dSNamhyung Kim if (pos->offset == offset) 399aca7a94dSNamhyung Kim return pos; 40029ed6e76SArnaldo Carvalho de Melo if (!disasm_line__filter(&browser->b, &pos->node)) 401aca7a94dSNamhyung Kim ++*idx; 402aca7a94dSNamhyung Kim } 403aca7a94dSNamhyung Kim 404aca7a94dSNamhyung Kim return NULL; 405aca7a94dSNamhyung Kim } 406aca7a94dSNamhyung Kim 407aca7a94dSNamhyung Kim static bool annotate_browser__jump(struct annotate_browser *browser) 408aca7a94dSNamhyung Kim { 409657bcaf5SArnaldo Carvalho de Melo struct disasm_line *dl = browser->selection; 4104f9d0325SArnaldo Carvalho de Melo s64 idx; 411aca7a94dSNamhyung Kim 412d86b0597SArnaldo Carvalho de Melo if (!ins__is_jump(dl->ins)) 413aca7a94dSNamhyung Kim return false; 414aca7a94dSNamhyung Kim 41544d1a3edSArnaldo Carvalho de Melo dl = annotate_browser__find_offset(browser, dl->ops.target.offset, &idx); 41629ed6e76SArnaldo Carvalho de Melo if (dl == NULL) { 417aca7a94dSNamhyung Kim ui_helpline__puts("Invallid jump offset"); 418aca7a94dSNamhyung Kim return true; 419aca7a94dSNamhyung Kim } 420aca7a94dSNamhyung Kim 42129ed6e76SArnaldo Carvalho de Melo annotate_browser__set_top(browser, dl, idx); 422aca7a94dSNamhyung Kim 423aca7a94dSNamhyung Kim return true; 424aca7a94dSNamhyung Kim } 425aca7a94dSNamhyung Kim 42629ed6e76SArnaldo Carvalho de Melo static 42729ed6e76SArnaldo Carvalho de Melo struct disasm_line *annotate_browser__find_string(struct annotate_browser *browser, 428aca7a94dSNamhyung Kim char *s, s64 *idx) 429aca7a94dSNamhyung Kim { 430aca7a94dSNamhyung Kim struct map_symbol *ms = browser->b.priv; 431aca7a94dSNamhyung Kim struct symbol *sym = ms->sym; 432aca7a94dSNamhyung Kim struct annotation *notes = symbol__annotation(sym); 43329ed6e76SArnaldo Carvalho de Melo struct disasm_line *pos = browser->selection; 434aca7a94dSNamhyung Kim 435aca7a94dSNamhyung Kim *idx = browser->b.index; 436aca7a94dSNamhyung Kim list_for_each_entry_continue(pos, ¬es->src->source, node) { 43729ed6e76SArnaldo Carvalho de Melo if (disasm_line__filter(&browser->b, &pos->node)) 438aca7a94dSNamhyung Kim continue; 439aca7a94dSNamhyung Kim 440aca7a94dSNamhyung Kim ++*idx; 441aca7a94dSNamhyung Kim 442aca7a94dSNamhyung Kim if (pos->line && strstr(pos->line, s) != NULL) 443aca7a94dSNamhyung Kim return pos; 444aca7a94dSNamhyung Kim } 445aca7a94dSNamhyung Kim 446aca7a94dSNamhyung Kim return NULL; 447aca7a94dSNamhyung Kim } 448aca7a94dSNamhyung Kim 449aca7a94dSNamhyung Kim static bool __annotate_browser__search(struct annotate_browser *browser) 450aca7a94dSNamhyung Kim { 45129ed6e76SArnaldo Carvalho de Melo struct disasm_line *dl; 452aca7a94dSNamhyung Kim s64 idx; 453aca7a94dSNamhyung Kim 45429ed6e76SArnaldo Carvalho de Melo dl = annotate_browser__find_string(browser, browser->search_bf, &idx); 45529ed6e76SArnaldo Carvalho de Melo if (dl == NULL) { 456aca7a94dSNamhyung Kim ui_helpline__puts("String not found!"); 457aca7a94dSNamhyung Kim return false; 458aca7a94dSNamhyung Kim } 459aca7a94dSNamhyung Kim 46029ed6e76SArnaldo Carvalho de Melo annotate_browser__set_top(browser, dl, idx); 461aca7a94dSNamhyung Kim browser->searching_backwards = false; 462aca7a94dSNamhyung Kim return true; 463aca7a94dSNamhyung Kim } 464aca7a94dSNamhyung Kim 46529ed6e76SArnaldo Carvalho de Melo static 46629ed6e76SArnaldo Carvalho de Melo struct disasm_line *annotate_browser__find_string_reverse(struct annotate_browser *browser, 467aca7a94dSNamhyung Kim char *s, s64 *idx) 468aca7a94dSNamhyung Kim { 469aca7a94dSNamhyung Kim struct map_symbol *ms = browser->b.priv; 470aca7a94dSNamhyung Kim struct symbol *sym = ms->sym; 471aca7a94dSNamhyung Kim struct annotation *notes = symbol__annotation(sym); 47229ed6e76SArnaldo Carvalho de Melo struct disasm_line *pos = browser->selection; 473aca7a94dSNamhyung Kim 474aca7a94dSNamhyung Kim *idx = browser->b.index; 475aca7a94dSNamhyung Kim list_for_each_entry_continue_reverse(pos, ¬es->src->source, node) { 47629ed6e76SArnaldo Carvalho de Melo if (disasm_line__filter(&browser->b, &pos->node)) 477aca7a94dSNamhyung Kim continue; 478aca7a94dSNamhyung Kim 479aca7a94dSNamhyung Kim --*idx; 480aca7a94dSNamhyung Kim 481aca7a94dSNamhyung Kim if (pos->line && strstr(pos->line, s) != NULL) 482aca7a94dSNamhyung Kim return pos; 483aca7a94dSNamhyung Kim } 484aca7a94dSNamhyung Kim 485aca7a94dSNamhyung Kim return NULL; 486aca7a94dSNamhyung Kim } 487aca7a94dSNamhyung Kim 488aca7a94dSNamhyung Kim static bool __annotate_browser__search_reverse(struct annotate_browser *browser) 489aca7a94dSNamhyung Kim { 49029ed6e76SArnaldo Carvalho de Melo struct disasm_line *dl; 491aca7a94dSNamhyung Kim s64 idx; 492aca7a94dSNamhyung Kim 49329ed6e76SArnaldo Carvalho de Melo dl = annotate_browser__find_string_reverse(browser, browser->search_bf, &idx); 49429ed6e76SArnaldo Carvalho de Melo if (dl == NULL) { 495aca7a94dSNamhyung Kim ui_helpline__puts("String not found!"); 496aca7a94dSNamhyung Kim return false; 497aca7a94dSNamhyung Kim } 498aca7a94dSNamhyung Kim 49929ed6e76SArnaldo Carvalho de Melo annotate_browser__set_top(browser, dl, idx); 500aca7a94dSNamhyung Kim browser->searching_backwards = true; 501aca7a94dSNamhyung Kim return true; 502aca7a94dSNamhyung Kim } 503aca7a94dSNamhyung Kim 504aca7a94dSNamhyung Kim static bool annotate_browser__search_window(struct annotate_browser *browser, 505aca7a94dSNamhyung Kim int delay_secs) 506aca7a94dSNamhyung Kim { 507aca7a94dSNamhyung Kim if (ui_browser__input_window("Search", "String: ", browser->search_bf, 508aca7a94dSNamhyung Kim "ENTER: OK, ESC: Cancel", 509aca7a94dSNamhyung Kim delay_secs * 2) != K_ENTER || 510aca7a94dSNamhyung Kim !*browser->search_bf) 511aca7a94dSNamhyung Kim return false; 512aca7a94dSNamhyung Kim 513aca7a94dSNamhyung Kim return true; 514aca7a94dSNamhyung Kim } 515aca7a94dSNamhyung Kim 516aca7a94dSNamhyung Kim static bool annotate_browser__search(struct annotate_browser *browser, int delay_secs) 517aca7a94dSNamhyung Kim { 518aca7a94dSNamhyung Kim if (annotate_browser__search_window(browser, delay_secs)) 519aca7a94dSNamhyung Kim return __annotate_browser__search(browser); 520aca7a94dSNamhyung Kim 521aca7a94dSNamhyung Kim return false; 522aca7a94dSNamhyung Kim } 523aca7a94dSNamhyung Kim 524aca7a94dSNamhyung Kim static bool annotate_browser__continue_search(struct annotate_browser *browser, 525aca7a94dSNamhyung Kim int delay_secs) 526aca7a94dSNamhyung Kim { 527aca7a94dSNamhyung Kim if (!*browser->search_bf) 528aca7a94dSNamhyung Kim return annotate_browser__search(browser, delay_secs); 529aca7a94dSNamhyung Kim 530aca7a94dSNamhyung Kim return __annotate_browser__search(browser); 531aca7a94dSNamhyung Kim } 532aca7a94dSNamhyung Kim 533aca7a94dSNamhyung Kim static bool annotate_browser__search_reverse(struct annotate_browser *browser, 534aca7a94dSNamhyung Kim int delay_secs) 535aca7a94dSNamhyung Kim { 536aca7a94dSNamhyung Kim if (annotate_browser__search_window(browser, delay_secs)) 537aca7a94dSNamhyung Kim return __annotate_browser__search_reverse(browser); 538aca7a94dSNamhyung Kim 539aca7a94dSNamhyung Kim return false; 540aca7a94dSNamhyung Kim } 541aca7a94dSNamhyung Kim 542aca7a94dSNamhyung Kim static 543aca7a94dSNamhyung Kim bool annotate_browser__continue_search_reverse(struct annotate_browser *browser, 544aca7a94dSNamhyung Kim int delay_secs) 545aca7a94dSNamhyung Kim { 546aca7a94dSNamhyung Kim if (!*browser->search_bf) 547aca7a94dSNamhyung Kim return annotate_browser__search_reverse(browser, delay_secs); 548aca7a94dSNamhyung Kim 549aca7a94dSNamhyung Kim return __annotate_browser__search_reverse(browser); 550aca7a94dSNamhyung Kim } 551aca7a94dSNamhyung Kim 552aca7a94dSNamhyung Kim static int annotate_browser__run(struct annotate_browser *self, int evidx, 553aca7a94dSNamhyung Kim void(*timer)(void *arg), 554aca7a94dSNamhyung Kim void *arg, int delay_secs) 555aca7a94dSNamhyung Kim { 556aca7a94dSNamhyung Kim struct rb_node *nd = NULL; 557aca7a94dSNamhyung Kim struct map_symbol *ms = self->b.priv; 558aca7a94dSNamhyung Kim struct symbol *sym = ms->sym; 559aca7a94dSNamhyung Kim const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, " 560aca7a94dSNamhyung Kim "H: Go to hottest line, ->/ENTER: Line action, " 561aca7a94dSNamhyung Kim "O: Toggle offset view, " 562aca7a94dSNamhyung Kim "S: Toggle source code view"; 563aca7a94dSNamhyung Kim int key; 564aca7a94dSNamhyung Kim 565aca7a94dSNamhyung Kim if (ui_browser__show(&self->b, sym->name, help) < 0) 566aca7a94dSNamhyung Kim return -1; 567aca7a94dSNamhyung Kim 568aca7a94dSNamhyung Kim annotate_browser__calc_percent(self, evidx); 569aca7a94dSNamhyung Kim 570aca7a94dSNamhyung Kim if (self->curr_hot) { 571aca7a94dSNamhyung Kim annotate_browser__set_rb_top(self, self->curr_hot); 572aca7a94dSNamhyung Kim self->b.navkeypressed = false; 573aca7a94dSNamhyung Kim } 574aca7a94dSNamhyung Kim 575aca7a94dSNamhyung Kim nd = self->curr_hot; 576aca7a94dSNamhyung Kim 577aca7a94dSNamhyung Kim while (1) { 578aca7a94dSNamhyung Kim key = ui_browser__run(&self->b, delay_secs); 579aca7a94dSNamhyung Kim 580aca7a94dSNamhyung Kim if (delay_secs != 0) { 581aca7a94dSNamhyung Kim annotate_browser__calc_percent(self, evidx); 582aca7a94dSNamhyung Kim /* 583aca7a94dSNamhyung Kim * Current line focus got out of the list of most active 584aca7a94dSNamhyung Kim * lines, NULL it so that if TAB|UNTAB is pressed, we 585aca7a94dSNamhyung Kim * move to curr_hot (current hottest line). 586aca7a94dSNamhyung Kim */ 587aca7a94dSNamhyung Kim if (nd != NULL && RB_EMPTY_NODE(nd)) 588aca7a94dSNamhyung Kim nd = NULL; 589aca7a94dSNamhyung Kim } 590aca7a94dSNamhyung Kim 591aca7a94dSNamhyung Kim switch (key) { 592aca7a94dSNamhyung Kim case K_TIMER: 593aca7a94dSNamhyung Kim if (timer != NULL) 594aca7a94dSNamhyung Kim timer(arg); 595aca7a94dSNamhyung Kim 596aca7a94dSNamhyung Kim if (delay_secs != 0) 597aca7a94dSNamhyung Kim symbol__annotate_decay_histogram(sym, evidx); 598aca7a94dSNamhyung Kim continue; 599aca7a94dSNamhyung Kim case K_TAB: 600aca7a94dSNamhyung Kim if (nd != NULL) { 601aca7a94dSNamhyung Kim nd = rb_prev(nd); 602aca7a94dSNamhyung Kim if (nd == NULL) 603aca7a94dSNamhyung Kim nd = rb_last(&self->entries); 604aca7a94dSNamhyung Kim } else 605aca7a94dSNamhyung Kim nd = self->curr_hot; 606aca7a94dSNamhyung Kim break; 607aca7a94dSNamhyung Kim case K_UNTAB: 608aca7a94dSNamhyung Kim if (nd != NULL) 609aca7a94dSNamhyung Kim nd = rb_next(nd); 610aca7a94dSNamhyung Kim if (nd == NULL) 611aca7a94dSNamhyung Kim nd = rb_first(&self->entries); 612aca7a94dSNamhyung Kim else 613aca7a94dSNamhyung Kim nd = self->curr_hot; 614aca7a94dSNamhyung Kim break; 615aca7a94dSNamhyung Kim case 'H': 616aca7a94dSNamhyung Kim case 'h': 617aca7a94dSNamhyung Kim nd = self->curr_hot; 618aca7a94dSNamhyung Kim break; 619aca7a94dSNamhyung Kim case 'S': 620aca7a94dSNamhyung Kim case 's': 621aca7a94dSNamhyung Kim if (annotate_browser__toggle_source(self)) 622aca7a94dSNamhyung Kim ui_helpline__puts(help); 623aca7a94dSNamhyung Kim continue; 624aca7a94dSNamhyung Kim case 'O': 625aca7a94dSNamhyung Kim case 'o': 626aca7a94dSNamhyung Kim self->use_offset = !self->use_offset; 627aca7a94dSNamhyung Kim continue; 628aca7a94dSNamhyung Kim case '/': 629aca7a94dSNamhyung Kim if (annotate_browser__search(self, delay_secs)) { 630aca7a94dSNamhyung Kim show_help: 631aca7a94dSNamhyung Kim ui_helpline__puts(help); 632aca7a94dSNamhyung Kim } 633aca7a94dSNamhyung Kim continue; 634aca7a94dSNamhyung Kim case 'n': 635aca7a94dSNamhyung Kim if (self->searching_backwards ? 636aca7a94dSNamhyung Kim annotate_browser__continue_search_reverse(self, delay_secs) : 637aca7a94dSNamhyung Kim annotate_browser__continue_search(self, delay_secs)) 638aca7a94dSNamhyung Kim goto show_help; 639aca7a94dSNamhyung Kim continue; 640aca7a94dSNamhyung Kim case '?': 641aca7a94dSNamhyung Kim if (annotate_browser__search_reverse(self, delay_secs)) 642aca7a94dSNamhyung Kim goto show_help; 643aca7a94dSNamhyung Kim continue; 644aca7a94dSNamhyung Kim case K_ENTER: 645aca7a94dSNamhyung Kim case K_RIGHT: 646aca7a94dSNamhyung Kim if (self->selection == NULL) 647aca7a94dSNamhyung Kim ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org"); 648aca7a94dSNamhyung Kim else if (self->selection->offset == -1) 649aca7a94dSNamhyung Kim ui_helpline__puts("Actions are only available for assembly lines."); 650c4cceae3SArnaldo Carvalho de Melo else if (!self->selection->ins) { 651c4cceae3SArnaldo Carvalho de Melo if (strcmp(self->selection->name, "retq")) 652c4cceae3SArnaldo Carvalho de Melo goto show_sup_ins; 653c4cceae3SArnaldo Carvalho de Melo goto out; 654c4cceae3SArnaldo Carvalho de Melo } else if (!(annotate_browser__jump(self) || 655c4cceae3SArnaldo Carvalho de Melo annotate_browser__callq(self, evidx, timer, arg, delay_secs))) { 656c4cceae3SArnaldo Carvalho de Melo show_sup_ins: 657c4cceae3SArnaldo Carvalho de Melo ui_helpline__puts("Actions are only available for 'callq', 'retq' & jump instructions."); 658c4cceae3SArnaldo Carvalho de Melo } 659aca7a94dSNamhyung Kim continue; 660aca7a94dSNamhyung Kim case K_LEFT: 661aca7a94dSNamhyung Kim case K_ESC: 662aca7a94dSNamhyung Kim case 'q': 663aca7a94dSNamhyung Kim case CTRL('c'): 664aca7a94dSNamhyung Kim goto out; 665aca7a94dSNamhyung Kim default: 666aca7a94dSNamhyung Kim continue; 667aca7a94dSNamhyung Kim } 668aca7a94dSNamhyung Kim 669aca7a94dSNamhyung Kim if (nd != NULL) 670aca7a94dSNamhyung Kim annotate_browser__set_rb_top(self, nd); 671aca7a94dSNamhyung Kim } 672aca7a94dSNamhyung Kim out: 673aca7a94dSNamhyung Kim ui_browser__hide(&self->b); 674aca7a94dSNamhyung Kim return key; 675aca7a94dSNamhyung Kim } 676aca7a94dSNamhyung Kim 677aca7a94dSNamhyung Kim int hist_entry__tui_annotate(struct hist_entry *he, int evidx, 678aca7a94dSNamhyung Kim void(*timer)(void *arg), void *arg, int delay_secs) 679aca7a94dSNamhyung Kim { 680aca7a94dSNamhyung Kim return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx, 681aca7a94dSNamhyung Kim timer, arg, delay_secs); 682aca7a94dSNamhyung Kim } 683aca7a94dSNamhyung Kim 684b793a401SArnaldo Carvalho de Melo static void annotate_browser__mark_jump_targets(struct annotate_browser *browser, 685b793a401SArnaldo Carvalho de Melo size_t size) 686b793a401SArnaldo Carvalho de Melo { 687b793a401SArnaldo Carvalho de Melo u64 offset; 688b793a401SArnaldo Carvalho de Melo 689b793a401SArnaldo Carvalho de Melo for (offset = 0; offset < size; ++offset) { 690b793a401SArnaldo Carvalho de Melo struct disasm_line *dl = browser->offsets[offset], *dlt; 691b793a401SArnaldo Carvalho de Melo struct browser_disasm_line *bdlt; 692b793a401SArnaldo Carvalho de Melo 693*38b31bd0SArnaldo Carvalho de Melo if (!dl || !dl->ins || !ins__is_jump(dl->ins) || 694*38b31bd0SArnaldo Carvalho de Melo !disasm_line__has_offset(dl)) 695b793a401SArnaldo Carvalho de Melo continue; 696b793a401SArnaldo Carvalho de Melo 69744d1a3edSArnaldo Carvalho de Melo if (dl->ops.target.offset >= size) { 698b793a401SArnaldo Carvalho de Melo ui__error("jump to after symbol!\n" 699b793a401SArnaldo Carvalho de Melo "size: %zx, jump target: %" PRIx64, 70044d1a3edSArnaldo Carvalho de Melo size, dl->ops.target.offset); 701b793a401SArnaldo Carvalho de Melo continue; 702b793a401SArnaldo Carvalho de Melo } 703b793a401SArnaldo Carvalho de Melo 70444d1a3edSArnaldo Carvalho de Melo dlt = browser->offsets[dl->ops.target.offset]; 7059481ede9SArnaldo Carvalho de Melo /* 7069481ede9SArnaldo Carvalho de Melo * FIXME: Oops, no jump target? Buggy disassembler? Or do we 7079481ede9SArnaldo Carvalho de Melo * have to adjust to the previous offset? 7089481ede9SArnaldo Carvalho de Melo */ 7099481ede9SArnaldo Carvalho de Melo if (dlt == NULL) 7109481ede9SArnaldo Carvalho de Melo continue; 7119481ede9SArnaldo Carvalho de Melo 712b793a401SArnaldo Carvalho de Melo bdlt = disasm_line__browser(dlt); 713b793a401SArnaldo Carvalho de Melo bdlt->jump_target = true; 714b793a401SArnaldo Carvalho de Melo } 715b793a401SArnaldo Carvalho de Melo 716b793a401SArnaldo Carvalho de Melo } 717b793a401SArnaldo Carvalho de Melo 718aca7a94dSNamhyung Kim int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx, 719aca7a94dSNamhyung Kim void(*timer)(void *arg), void *arg, 720aca7a94dSNamhyung Kim int delay_secs) 721aca7a94dSNamhyung Kim { 72229ed6e76SArnaldo Carvalho de Melo struct disasm_line *pos, *n; 723aca7a94dSNamhyung Kim struct annotation *notes; 724b793a401SArnaldo Carvalho de Melo const size_t size = symbol__size(sym); 725aca7a94dSNamhyung Kim struct map_symbol ms = { 726aca7a94dSNamhyung Kim .map = map, 727aca7a94dSNamhyung Kim .sym = sym, 728aca7a94dSNamhyung Kim }; 729aca7a94dSNamhyung Kim struct annotate_browser browser = { 730aca7a94dSNamhyung Kim .b = { 731a3f895beSArnaldo Carvalho de Melo .refresh = annotate_browser__refresh, 732aca7a94dSNamhyung Kim .seek = ui_browser__list_head_seek, 733aca7a94dSNamhyung Kim .write = annotate_browser__write, 73429ed6e76SArnaldo Carvalho de Melo .filter = disasm_line__filter, 735aca7a94dSNamhyung Kim .priv = &ms, 736aca7a94dSNamhyung Kim .use_navkeypressed = true, 737aca7a94dSNamhyung Kim }, 7388bf39cb8SArnaldo Carvalho de Melo .use_offset = true, 739aca7a94dSNamhyung Kim }; 740b793a401SArnaldo Carvalho de Melo int ret = -1; 741aca7a94dSNamhyung Kim 742aca7a94dSNamhyung Kim if (sym == NULL) 743aca7a94dSNamhyung Kim return -1; 744aca7a94dSNamhyung Kim 745aca7a94dSNamhyung Kim if (map->dso->annotate_warned) 746aca7a94dSNamhyung Kim return -1; 747aca7a94dSNamhyung Kim 748b793a401SArnaldo Carvalho de Melo browser.offsets = zalloc(size * sizeof(struct disasm_line *)); 749b793a401SArnaldo Carvalho de Melo if (browser.offsets == NULL) { 750b793a401SArnaldo Carvalho de Melo ui__error("Not enough memory!"); 751b793a401SArnaldo Carvalho de Melo return -1; 752b793a401SArnaldo Carvalho de Melo } 753b793a401SArnaldo Carvalho de Melo 754887c0066SArnaldo Carvalho de Melo if (symbol__annotate(sym, map, sizeof(struct browser_disasm_line)) < 0) { 755aca7a94dSNamhyung Kim ui__error("%s", ui_helpline__last_msg); 756b793a401SArnaldo Carvalho de Melo goto out_free_offsets; 757aca7a94dSNamhyung Kim } 758aca7a94dSNamhyung Kim 759aca7a94dSNamhyung Kim ui_helpline__push("Press <- or ESC to exit"); 760aca7a94dSNamhyung Kim 761aca7a94dSNamhyung Kim notes = symbol__annotation(sym); 762aca7a94dSNamhyung Kim browser.start = map__rip_2objdump(map, sym->start); 763aca7a94dSNamhyung Kim 764aca7a94dSNamhyung Kim list_for_each_entry(pos, ¬es->src->source, node) { 765887c0066SArnaldo Carvalho de Melo struct browser_disasm_line *bpos; 766aca7a94dSNamhyung Kim size_t line_len = strlen(pos->line); 767aca7a94dSNamhyung Kim 768aca7a94dSNamhyung Kim if (browser.b.width < line_len) 769aca7a94dSNamhyung Kim browser.b.width = line_len; 770887c0066SArnaldo Carvalho de Melo bpos = disasm_line__browser(pos); 771887c0066SArnaldo Carvalho de Melo bpos->idx = browser.nr_entries++; 772b793a401SArnaldo Carvalho de Melo if (pos->offset != -1) { 773887c0066SArnaldo Carvalho de Melo bpos->idx_asm = browser.nr_asm_entries++; 77497148a97SArnaldo Carvalho de Melo /* 77597148a97SArnaldo Carvalho de Melo * FIXME: short term bandaid to cope with assembly 77697148a97SArnaldo Carvalho de Melo * routines that comes with labels in the same column 77797148a97SArnaldo Carvalho de Melo * as the address in objdump, sigh. 77897148a97SArnaldo Carvalho de Melo * 77997148a97SArnaldo Carvalho de Melo * E.g. copy_user_generic_unrolled 78097148a97SArnaldo Carvalho de Melo */ 78197148a97SArnaldo Carvalho de Melo if (pos->offset < (s64)size) 782b793a401SArnaldo Carvalho de Melo browser.offsets[pos->offset] = pos; 783b793a401SArnaldo Carvalho de Melo } else 784887c0066SArnaldo Carvalho de Melo bpos->idx_asm = -1; 785aca7a94dSNamhyung Kim } 786aca7a94dSNamhyung Kim 787b793a401SArnaldo Carvalho de Melo annotate_browser__mark_jump_targets(&browser, size); 788b793a401SArnaldo Carvalho de Melo 78961e04b33SArnaldo Carvalho de Melo browser.offset_width = hex_width(size); 790aca7a94dSNamhyung Kim browser.b.nr_entries = browser.nr_entries; 791aca7a94dSNamhyung Kim browser.b.entries = ¬es->src->source, 792aca7a94dSNamhyung Kim browser.b.width += 18; /* Percentage */ 793aca7a94dSNamhyung Kim ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs); 794aca7a94dSNamhyung Kim list_for_each_entry_safe(pos, n, ¬es->src->source, node) { 795aca7a94dSNamhyung Kim list_del(&pos->node); 79629ed6e76SArnaldo Carvalho de Melo disasm_line__free(pos); 797aca7a94dSNamhyung Kim } 798b793a401SArnaldo Carvalho de Melo 799b793a401SArnaldo Carvalho de Melo out_free_offsets: 800b793a401SArnaldo Carvalho de Melo free(browser.offsets); 801aca7a94dSNamhyung Kim return ret; 802aca7a94dSNamhyung Kim } 803