1 // SPDX-License-Identifier: GPL-2.0
4 #include <sys/ttydefaults.h>
6 #include <linux/bitops.h>
7 #include "../../util/util.h"
8 #include "../../util/debug.h"
9 #include "../../util/symbol.h"
10 #include "../browser.h"
11 #include "../helpline.h"
12 #include "../keysyms.h"
15 #include "sane_ctype.h"
23 static void map_browser__write(struct ui_browser
*browser
, void *nd
, int row
)
25 struct symbol
*sym
= rb_entry(nd
, struct symbol
, rb_node
);
26 struct map_browser
*mb
= container_of(browser
, struct map_browser
, b
);
27 bool current_entry
= ui_browser__is_current_entry(browser
, row
);
30 ui_browser__set_percent_color(browser
, 0, current_entry
);
31 ui_browser__printf(browser
, "%*" PRIx64
" %*" PRIx64
" %c ",
32 mb
->addrlen
, sym
->start
, mb
->addrlen
, sym
->end
,
33 sym
->binding
== STB_GLOBAL
? 'g' :
34 sym
->binding
== STB_LOCAL
? 'l' : 'w');
35 width
= browser
->width
- ((mb
->addrlen
* 2) + 4);
37 ui_browser__write_nstring(browser
, sym
->name
, width
);
40 /* FIXME uber-kludgy, see comment on cmd_report... */
41 static u32
*symbol__browser_index(struct symbol
*browser
)
43 return ((void *)browser
) - sizeof(struct rb_node
) - sizeof(u32
);
46 static int map_browser__search(struct map_browser
*browser
)
50 int err
= ui_browser__input_window("Search by name/addr",
51 "Prefix with 0x to search by address",
52 target
, "ENTER: OK, ESC: Cancel", 0);
56 if (target
[0] == '0' && tolower(target
[1]) == 'x') {
57 u64 addr
= strtoull(target
, NULL
, 16);
58 sym
= map__find_symbol(browser
->map
, addr
);
60 sym
= map__find_symbol_by_name(browser
->map
, target
);
63 u32
*idx
= symbol__browser_index(sym
);
65 browser
->b
.top
= &sym
->rb_node
;
66 browser
->b
.index
= browser
->b
.top_idx
= *idx
;
68 ui_helpline__fpush("%s not found!", target
);
73 static int map_browser__run(struct map_browser
*browser
)
77 if (ui_browser__show(&browser
->b
, browser
->map
->dso
->long_name
,
78 "Press ESC to exit, %s / to search",
79 verbose
> 0 ? "" : "restart with -v to use") < 0)
83 key
= ui_browser__run(&browser
->b
, 0);
88 map_browser__search(browser
);
99 ui_browser__hide(&browser
->b
);
103 int map__browse(struct map
*map
)
105 struct map_browser mb
= {
107 .entries
= &map
->dso
->symbols
[map
->type
],
108 .refresh
= ui_browser__rb_tree_refresh
,
109 .seek
= ui_browser__rb_tree_seek
,
110 .write
= map_browser__write
,
115 char tmp
[BITS_PER_LONG
/ 4];
118 for (nd
= rb_first(mb
.b
.entries
); nd
; nd
= rb_next(nd
)) {
119 struct symbol
*pos
= rb_entry(nd
, struct symbol
, rb_node
);
121 if (maxaddr
< pos
->end
)
124 u32
*idx
= symbol__browser_index(pos
);
125 *idx
= mb
.b
.nr_entries
;
130 mb
.addrlen
= snprintf(tmp
, sizeof(tmp
), "%" PRIx64
, maxaddr
);
131 return map_browser__run(&mb
);