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