1 #include <elf.h> 2 #include <inttypes.h> 3 #include <sys/ttydefaults.h> 4 #include <string.h> 5 #include <linux/bitops.h> 6 #include "../../util/util.h" 7 #include "../../util/debug.h" 8 #include "../../util/symbol.h" 9 #include "../browser.h" 10 #include "../helpline.h" 11 #include "../keysyms.h" 12 #include "map.h" 13 14 #include "sane_ctype.h" 15 16 struct map_browser { 17 struct ui_browser b; 18 struct map *map; 19 u8 addrlen; 20 }; 21 22 static void map_browser__write(struct ui_browser *browser, void *nd, int row) 23 { 24 struct symbol *sym = rb_entry(nd, struct symbol, rb_node); 25 struct map_browser *mb = container_of(browser, struct map_browser, b); 26 bool current_entry = ui_browser__is_current_entry(browser, row); 27 int width; 28 29 ui_browser__set_percent_color(browser, 0, current_entry); 30 ui_browser__printf(browser, "%*" PRIx64 " %*" PRIx64 " %c ", 31 mb->addrlen, sym->start, mb->addrlen, sym->end, 32 sym->binding == STB_GLOBAL ? 'g' : 33 sym->binding == STB_LOCAL ? 'l' : 'w'); 34 width = browser->width - ((mb->addrlen * 2) + 4); 35 if (width > 0) 36 ui_browser__write_nstring(browser, sym->name, width); 37 } 38 39 /* FIXME uber-kludgy, see comment on cmd_report... */ 40 static u32 *symbol__browser_index(struct symbol *browser) 41 { 42 return ((void *)browser) - sizeof(struct rb_node) - sizeof(u32); 43 } 44 45 static int map_browser__search(struct map_browser *browser) 46 { 47 char target[512]; 48 struct symbol *sym; 49 int err = ui_browser__input_window("Search by name/addr", 50 "Prefix with 0x to search by address", 51 target, "ENTER: OK, ESC: Cancel", 0); 52 if (err != K_ENTER) 53 return -1; 54 55 if (target[0] == '0' && tolower(target[1]) == 'x') { 56 u64 addr = strtoull(target, NULL, 16); 57 sym = map__find_symbol(browser->map, addr); 58 } else 59 sym = map__find_symbol_by_name(browser->map, target); 60 61 if (sym != NULL) { 62 u32 *idx = symbol__browser_index(sym); 63 64 browser->b.top = &sym->rb_node; 65 browser->b.index = browser->b.top_idx = *idx; 66 } else 67 ui_helpline__fpush("%s not found!", target); 68 69 return 0; 70 } 71 72 static int map_browser__run(struct map_browser *browser) 73 { 74 int key; 75 76 if (ui_browser__show(&browser->b, browser->map->dso->long_name, 77 "Press ESC to exit, %s / to search", 78 verbose > 0 ? "" : "restart with -v to use") < 0) 79 return -1; 80 81 while (1) { 82 key = ui_browser__run(&browser->b, 0); 83 84 switch (key) { 85 case '/': 86 if (verbose > 0) 87 map_browser__search(browser); 88 default: 89 break; 90 case K_LEFT: 91 case K_ESC: 92 case 'q': 93 case CTRL('c'): 94 goto out; 95 } 96 } 97 out: 98 ui_browser__hide(&browser->b); 99 return key; 100 } 101 102 int map__browse(struct map *map) 103 { 104 struct map_browser mb = { 105 .b = { 106 .entries = &map->dso->symbols[map->type], 107 .refresh = ui_browser__rb_tree_refresh, 108 .seek = ui_browser__rb_tree_seek, 109 .write = map_browser__write, 110 }, 111 .map = map, 112 }; 113 struct rb_node *nd; 114 char tmp[BITS_PER_LONG / 4]; 115 u64 maxaddr = 0; 116 117 for (nd = rb_first(mb.b.entries); nd; nd = rb_next(nd)) { 118 struct symbol *pos = rb_entry(nd, struct symbol, rb_node); 119 120 if (maxaddr < pos->end) 121 maxaddr = pos->end; 122 if (verbose > 0) { 123 u32 *idx = symbol__browser_index(pos); 124 *idx = mb.b.nr_entries; 125 } 126 ++mb.b.nr_entries; 127 } 128 129 mb.addrlen = snprintf(tmp, sizeof(tmp), "%" PRIx64, maxaddr); 130 return map_browser__run(&mb); 131 } 132