blob: 733daba60cd4727a8b060160b7a321e9c9fb2bf2 [file] [log] [blame]
Arnaldo Carvalho de Melob1b02672010-08-10 15:37:34 -03001#include "../libslang.h"
2#include <elf.h>
3#include <newt.h>
4#include <sys/ttydefaults.h>
5#include <ctype.h>
6#include <string.h>
7#include <linux/bitops.h>
8#include "../../debug.h"
9#include "../../symbol.h"
10#include "../browser.h"
11#include "../helpline.h"
12#include "map.h"
13
14static int ui_entry__read(const char *title, char *bf, size_t size, int width)
15{
16 struct newtExitStruct es;
17 newtComponent form, entry;
18 const char *result;
19 int err = -1;
20
21 newtCenteredWindow(width, 1, title);
22 form = newtForm(NULL, NULL, 0);
23 if (form == NULL)
24 return -1;
25
26 entry = newtEntry(0, 0, "0x", width, &result, NEWT_FLAG_SCROLL);
27 if (entry == NULL)
28 goto out_free_form;
29
30 newtFormAddComponent(form, entry);
31 newtFormAddHotKey(form, NEWT_KEY_ENTER);
32 newtFormAddHotKey(form, NEWT_KEY_ESCAPE);
33 newtFormAddHotKey(form, NEWT_KEY_LEFT);
34 newtFormAddHotKey(form, CTRL('c'));
35 newtFormRun(form, &es);
36
37 if (result != NULL) {
38 strncpy(bf, result, size);
39 err = 0;
40 }
41out_free_form:
42 newtPopWindow();
43 newtFormDestroy(form);
44 return 0;
45}
46
47struct map_browser {
48 struct ui_browser b;
49 struct map *map;
50 u16 namelen;
51 u8 addrlen;
52};
53
54static void map_browser__write(struct ui_browser *self, void *nd, int row)
55{
56 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
57 struct map_browser *mb = container_of(self, struct map_browser, b);
58 bool current_entry = ui_browser__is_current_entry(self, row);
Arnaldo Carvalho de Melob1b02672010-08-10 15:37:34 -030059
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030060 ui_browser__set_percent_color(self, 0, current_entry);
Arnaldo Carvalho de Melob1b02672010-08-10 15:37:34 -030061 slsmg_printf("%*llx %*llx %c ",
62 mb->addrlen, sym->start, mb->addrlen, sym->end,
63 sym->binding == STB_GLOBAL ? 'g' :
64 sym->binding == STB_LOCAL ? 'l' : 'w');
65 slsmg_write_nstring(sym->name, mb->namelen);
66}
67
68/* FIXME uber-kludgy, see comment on cmd_report... */
69static u32 *symbol__browser_index(struct symbol *self)
70{
71 return ((void *)self) - sizeof(struct rb_node) - sizeof(u32);
72}
73
74static int map_browser__search(struct map_browser *self)
75{
76 char target[512];
77 struct symbol *sym;
78 int err = ui_entry__read("Search by name/addr", target, sizeof(target), 40);
79
80 if (err)
81 return err;
82
83 if (target[0] == '0' && tolower(target[1]) == 'x') {
84 u64 addr = strtoull(target, NULL, 16);
85 sym = map__find_symbol(self->map, addr, NULL);
86 } else
87 sym = map__find_symbol_by_name(self->map, target, NULL);
88
89 if (sym != NULL) {
90 u32 *idx = symbol__browser_index(sym);
91
92 self->b.top = &sym->rb_node;
93 self->b.index = self->b.top_idx = *idx;
94 } else
95 ui_helpline__fpush("%s not found!", target);
96
97 return 0;
98}
99
100static int map_browser__run(struct map_browser *self, struct newtExitStruct *es)
101{
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300102 if (ui_browser__show(&self->b, self->map->dso->long_name,
103 "Press <- or ESC to exit, %s / to search",
104 verbose ? "" : "restart with -v to use") < 0)
Arnaldo Carvalho de Melob1b02672010-08-10 15:37:34 -0300105 return -1;
106
Arnaldo Carvalho de Melob1b02672010-08-10 15:37:34 -0300107 newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT);
108 newtFormAddHotKey(self->b.form, NEWT_KEY_ENTER);
109 if (verbose)
110 newtFormAddHotKey(self->b.form, '/');
111
112 while (1) {
113 ui_browser__run(&self->b, es);
114
115 if (es->reason != NEWT_EXIT_HOTKEY)
116 break;
117 if (verbose && es->u.key == '/')
118 map_browser__search(self);
119 else
120 break;
121 }
122
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300123 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob1b02672010-08-10 15:37:34 -0300124 return 0;
125}
126
127int map__browse(struct map *self)
128{
129 struct map_browser mb = {
130 .b = {
131 .entries = &self->dso->symbols[self->type],
132 .refresh = ui_browser__rb_tree_refresh,
133 .seek = ui_browser__rb_tree_seek,
134 .write = map_browser__write,
135 },
136 .map = self,
137 };
138 struct newtExitStruct es;
139 struct rb_node *nd;
140 char tmp[BITS_PER_LONG / 4];
141 u64 maxaddr = 0;
142
143 for (nd = rb_first(mb.b.entries); nd; nd = rb_next(nd)) {
144 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
145
146 if (mb.namelen < pos->namelen)
147 mb.namelen = pos->namelen;
148 if (maxaddr < pos->end)
149 maxaddr = pos->end;
150 if (verbose) {
151 u32 *idx = symbol__browser_index(pos);
152 *idx = mb.b.nr_entries;
153 }
154 ++mb.b.nr_entries;
155 }
156
157 mb.addrlen = snprintf(tmp, sizeof(tmp), "%llx", maxaddr);
158 mb.b.width += mb.addrlen * 2 + 4 + mb.namelen;
159 return map_browser__run(&mb, &es);
160}